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 data(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 data(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(MACRO_TABLE);
931   RECORD(LATE_PARSED_TEMPLATE);
932   RECORD(OPTIMIZE_PRAGMA_OPTIONS);
933 
934   // SourceManager Block.
935   BLOCK(SOURCE_MANAGER_BLOCK);
936   RECORD(SM_SLOC_FILE_ENTRY);
937   RECORD(SM_SLOC_BUFFER_ENTRY);
938   RECORD(SM_SLOC_BUFFER_BLOB);
939   RECORD(SM_SLOC_EXPANSION_ENTRY);
940 
941   // Preprocessor Block.
942   BLOCK(PREPROCESSOR_BLOCK);
943   RECORD(PP_MACRO_DIRECTIVE_HISTORY);
944   RECORD(PP_MACRO_OBJECT_LIKE);
945   RECORD(PP_MACRO_FUNCTION_LIKE);
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, data(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, data(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 namespace {
1976 class ASTMacroTableTrait {
1977 public:
1978   typedef IdentID key_type;
1979   typedef key_type key_type_ref;
1980 
1981   struct Data {
1982     uint32_t MacroDirectivesOffset;
1983   };
1984 
1985   typedef Data data_type;
1986   typedef const data_type &data_type_ref;
1987   typedef unsigned hash_value_type;
1988   typedef unsigned offset_type;
1989 
1990   static hash_value_type ComputeHash(IdentID IdID) {
1991     return llvm::hash_value(IdID);
1992   }
1993 
1994   std::pair<unsigned,unsigned>
1995   static EmitKeyDataLength(raw_ostream& Out,
1996                            key_type_ref Key, data_type_ref Data) {
1997     unsigned KeyLen = 4; // IdentID.
1998     unsigned DataLen = 4; // MacroDirectivesOffset.
1999     return std::make_pair(KeyLen, DataLen);
2000   }
2001 
2002   static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
2003     using namespace llvm::support;
2004     endian::Writer<little>(Out).write<uint32_t>(Key);
2005   }
2006 
2007   static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
2008                        unsigned) {
2009     using namespace llvm::support;
2010     endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
2011   }
2012 };
2013 } // end anonymous namespace
2014 
2015 static int compareMacroDirectives(
2016     const std::pair<const IdentifierInfo *, MacroDirective *> *X,
2017     const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
2018   return X->first->getName().compare(Y->first->getName());
2019 }
2020 
2021 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2022                               const Preprocessor &PP) {
2023   if (MacroInfo *MI = MD->getMacroInfo())
2024     if (MI->isBuiltinMacro())
2025       return true;
2026 
2027   if (IsModule) {
2028     // Re-export any imported directives.
2029     if (MD->isImported())
2030       return false;
2031 
2032     SourceLocation Loc = MD->getLocation();
2033     if (Loc.isInvalid())
2034       return true;
2035     if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2036       return true;
2037   }
2038 
2039   return false;
2040 }
2041 
2042 /// \brief Writes the block containing the serialized form of the
2043 /// preprocessor.
2044 ///
2045 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2046   PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2047   if (PPRec)
2048     WritePreprocessorDetail(*PPRec);
2049 
2050   RecordData Record;
2051 
2052   // If the preprocessor __COUNTER__ value has been bumped, remember it.
2053   if (PP.getCounterValue() != 0) {
2054     Record.push_back(PP.getCounterValue());
2055     Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2056     Record.clear();
2057   }
2058 
2059   // Enter the preprocessor block.
2060   Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2061 
2062   // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2063   // FIXME: use diagnostics subsystem for localization etc.
2064   if (PP.SawDateOrTime())
2065     fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
2066 
2067 
2068   // Loop over all the macro directives that are live at the end of the file,
2069   // emitting each to the PP section.
2070 
2071   // Construct the list of macro directives that need to be serialized.
2072   SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
2073     MacroDirectives;
2074   for (Preprocessor::macro_iterator
2075          I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2076          E = PP.macro_end(/*IncludeExternalMacros=*/false);
2077        I != E; ++I) {
2078     MacroDirectives.push_back(std::make_pair(I->first, I->second));
2079   }
2080 
2081   // Sort the set of macro definitions that need to be serialized by the
2082   // name of the macro, to provide a stable ordering.
2083   llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2084                        &compareMacroDirectives);
2085 
2086   llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
2087 
2088   // Emit the macro directives as a list and associate the offset with the
2089   // identifier they belong to.
2090   for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2091     const IdentifierInfo *Name = MacroDirectives[I].first;
2092     uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2093     MacroDirective *MD = MacroDirectives[I].second;
2094 
2095     // If the macro or identifier need no updates, don't write the macro history
2096     // for this one.
2097     // FIXME: Chain the macro history instead of re-writing it.
2098     if (MD->isFromPCH() &&
2099         Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2100       continue;
2101 
2102     // Emit the macro directives in reverse source order.
2103     for (; MD; MD = MD->getPrevious()) {
2104       if (shouldIgnoreMacro(MD, IsModule, PP))
2105         continue;
2106 
2107       AddSourceLocation(MD->getLocation(), Record);
2108       Record.push_back(MD->getKind());
2109       if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2110         MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2111         Record.push_back(InfoID);
2112         Record.push_back(DefMD->getOwningModuleID());
2113         Record.push_back(DefMD->isAmbiguous());
2114       } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2115         Record.push_back(UndefMD->getOwningModuleID());
2116       } else {
2117         auto *VisMD = cast<VisibilityMacroDirective>(MD);
2118         Record.push_back(VisMD->isPublic());
2119       }
2120 
2121       if (MD->isImported()) {
2122         auto Overrides = MD->getOverriddenModules();
2123         Record.push_back(Overrides.size());
2124         Record.append(Overrides.begin(), Overrides.end());
2125       }
2126     }
2127     if (Record.empty())
2128       continue;
2129 
2130     Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2131     Record.clear();
2132 
2133     IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2134 
2135     IdentID NameID = getIdentifierRef(Name);
2136     ASTMacroTableTrait::Data data;
2137     data.MacroDirectivesOffset = MacroDirectiveOffset;
2138     Generator.insert(NameID, data);
2139   }
2140 
2141   /// \brief Offsets of each of the macros into the bitstream, indexed by
2142   /// the local macro ID
2143   ///
2144   /// For each identifier that is associated with a macro, this map
2145   /// provides the offset into the bitstream where that macro is
2146   /// defined.
2147   std::vector<uint32_t> MacroOffsets;
2148 
2149   for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2150     const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2151     MacroInfo *MI = MacroInfosToEmit[I].MI;
2152     MacroID ID = MacroInfosToEmit[I].ID;
2153 
2154     if (ID < FirstMacroID) {
2155       assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2156       continue;
2157     }
2158 
2159     // Record the local offset of this macro.
2160     unsigned Index = ID - FirstMacroID;
2161     if (Index == MacroOffsets.size())
2162       MacroOffsets.push_back(Stream.GetCurrentBitNo());
2163     else {
2164       if (Index > MacroOffsets.size())
2165         MacroOffsets.resize(Index + 1);
2166 
2167       MacroOffsets[Index] = Stream.GetCurrentBitNo();
2168     }
2169 
2170     AddIdentifierRef(Name, Record);
2171     Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2172     AddSourceLocation(MI->getDefinitionLoc(), Record);
2173     AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2174     Record.push_back(MI->isUsed());
2175     Record.push_back(MI->isUsedForHeaderGuard());
2176     unsigned Code;
2177     if (MI->isObjectLike()) {
2178       Code = PP_MACRO_OBJECT_LIKE;
2179     } else {
2180       Code = PP_MACRO_FUNCTION_LIKE;
2181 
2182       Record.push_back(MI->isC99Varargs());
2183       Record.push_back(MI->isGNUVarargs());
2184       Record.push_back(MI->hasCommaPasting());
2185       Record.push_back(MI->getNumArgs());
2186       for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2187            I != E; ++I)
2188         AddIdentifierRef(*I, Record);
2189     }
2190 
2191     // If we have a detailed preprocessing record, record the macro definition
2192     // ID that corresponds to this macro.
2193     if (PPRec)
2194       Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2195 
2196     Stream.EmitRecord(Code, Record);
2197     Record.clear();
2198 
2199     // Emit the tokens array.
2200     for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2201       // Note that we know that the preprocessor does not have any annotation
2202       // tokens in it because they are created by the parser, and thus can't
2203       // be in a macro definition.
2204       const Token &Tok = MI->getReplacementToken(TokNo);
2205       AddToken(Tok, Record);
2206       Stream.EmitRecord(PP_TOKEN, Record);
2207       Record.clear();
2208     }
2209     ++NumMacros;
2210   }
2211 
2212   Stream.ExitBlock();
2213 
2214   // Create the on-disk hash table in a buffer.
2215   SmallString<4096> MacroTable;
2216   uint32_t BucketOffset;
2217   {
2218     using namespace llvm::support;
2219     llvm::raw_svector_ostream Out(MacroTable);
2220     // Make sure that no bucket is at offset 0
2221     endian::Writer<little>(Out).write<uint32_t>(0);
2222     BucketOffset = Generator.Emit(Out);
2223   }
2224 
2225   // Write the macro table
2226   using namespace llvm;
2227   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2228   Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2229   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2230   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2231   unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2232 
2233   Record.push_back(MACRO_TABLE);
2234   Record.push_back(BucketOffset);
2235   Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable);
2236   Record.clear();
2237 
2238   // Write the offsets table for macro IDs.
2239   using namespace llvm;
2240   Abbrev = new BitCodeAbbrev();
2241   Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2242   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2243   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2244   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2245 
2246   unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2247   Record.clear();
2248   Record.push_back(MACRO_OFFSET);
2249   Record.push_back(MacroOffsets.size());
2250   Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2251   Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2252                             data(MacroOffsets));
2253 }
2254 
2255 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
2256   if (PPRec.local_begin() == PPRec.local_end())
2257     return;
2258 
2259   SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2260 
2261   // Enter the preprocessor block.
2262   Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2263 
2264   // If the preprocessor has a preprocessing record, emit it.
2265   unsigned NumPreprocessingRecords = 0;
2266   using namespace llvm;
2267 
2268   // Set up the abbreviation for
2269   unsigned InclusionAbbrev = 0;
2270   {
2271     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2272     Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2273     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2274     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2275     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2276     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2277     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2278     InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2279   }
2280 
2281   unsigned FirstPreprocessorEntityID
2282     = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2283     + NUM_PREDEF_PP_ENTITY_IDS;
2284   unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2285   RecordData Record;
2286   for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2287                                   EEnd = PPRec.local_end();
2288        E != EEnd;
2289        (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2290     Record.clear();
2291 
2292     PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2293                                                      Stream.GetCurrentBitNo()));
2294 
2295     if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
2296       // Record this macro definition's ID.
2297       MacroDefinitions[MD] = NextPreprocessorEntityID;
2298 
2299       AddIdentifierRef(MD->getName(), Record);
2300       Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2301       continue;
2302     }
2303 
2304     if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
2305       Record.push_back(ME->isBuiltinMacro());
2306       if (ME->isBuiltinMacro())
2307         AddIdentifierRef(ME->getName(), Record);
2308       else
2309         Record.push_back(MacroDefinitions[ME->getDefinition()]);
2310       Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2311       continue;
2312     }
2313 
2314     if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2315       Record.push_back(PPD_INCLUSION_DIRECTIVE);
2316       Record.push_back(ID->getFileName().size());
2317       Record.push_back(ID->wasInQuotes());
2318       Record.push_back(static_cast<unsigned>(ID->getKind()));
2319       Record.push_back(ID->importedModule());
2320       SmallString<64> Buffer;
2321       Buffer += ID->getFileName();
2322       // Check that the FileEntry is not null because it was not resolved and
2323       // we create a PCH even with compiler errors.
2324       if (ID->getFile())
2325         Buffer += ID->getFile()->getName();
2326       Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2327       continue;
2328     }
2329 
2330     llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2331   }
2332   Stream.ExitBlock();
2333 
2334   // Write the offsets table for the preprocessing record.
2335   if (NumPreprocessingRecords > 0) {
2336     assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2337 
2338     // Write the offsets table for identifier IDs.
2339     using namespace llvm;
2340     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2341     Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2342     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2343     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2344     unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2345 
2346     Record.clear();
2347     Record.push_back(PPD_ENTITIES_OFFSETS);
2348     Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
2349     Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2350                               data(PreprocessedEntityOffsets));
2351   }
2352 }
2353 
2354 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2355   llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2356   if (Known != SubmoduleIDs.end())
2357     return Known->second;
2358 
2359   return SubmoduleIDs[Mod] = NextSubmoduleID++;
2360 }
2361 
2362 unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2363   if (!Mod)
2364     return 0;
2365 
2366   llvm::DenseMap<Module *, unsigned>::const_iterator
2367     Known = SubmoduleIDs.find(Mod);
2368   if (Known != SubmoduleIDs.end())
2369     return Known->second;
2370 
2371   return 0;
2372 }
2373 
2374 /// \brief Compute the number of modules within the given tree (including the
2375 /// given module).
2376 static unsigned getNumberOfModules(Module *Mod) {
2377   unsigned ChildModules = 0;
2378   for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2379                                SubEnd = Mod->submodule_end();
2380        Sub != SubEnd; ++Sub)
2381     ChildModules += getNumberOfModules(*Sub);
2382 
2383   return ChildModules + 1;
2384 }
2385 
2386 void ASTWriter::WriteSubmodules(Module *WritingModule) {
2387   // Determine the dependencies of our module and each of it's submodules.
2388   // FIXME: This feels like it belongs somewhere else, but there are no
2389   // other consumers of this information.
2390   SourceManager &SrcMgr = PP->getSourceManager();
2391   ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2392   for (const auto *I : Context->local_imports()) {
2393     if (Module *ImportedFrom
2394           = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2395                                                          SrcMgr))) {
2396       ImportedFrom->Imports.push_back(I->getImportedModule());
2397     }
2398   }
2399 
2400   // Enter the submodule description block.
2401   Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2402 
2403   // Write the abbreviations needed for the submodules block.
2404   using namespace llvm;
2405   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2406   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2407   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2408   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2409   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2410   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2411   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2412   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2413   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2414   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2415   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2416   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2417   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2418   unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2419 
2420   Abbrev = new BitCodeAbbrev();
2421   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2422   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2423   unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2424 
2425   Abbrev = new BitCodeAbbrev();
2426   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2427   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2428   unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2429 
2430   Abbrev = new BitCodeAbbrev();
2431   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2432   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2433   unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2434 
2435   Abbrev = new BitCodeAbbrev();
2436   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2437   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2438   unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2439 
2440   Abbrev = new BitCodeAbbrev();
2441   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2442   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2443   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Feature
2444   unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2445 
2446   Abbrev = new BitCodeAbbrev();
2447   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2448   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2449   unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2450 
2451   Abbrev = new BitCodeAbbrev();
2452   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2453   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2454   unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2455 
2456   Abbrev = new BitCodeAbbrev();
2457   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2458   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2459   unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2460 
2461   Abbrev = new BitCodeAbbrev();
2462   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2463   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2464   unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2465 
2466   Abbrev = new BitCodeAbbrev();
2467   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2468   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2469   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Name
2470   unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2471 
2472   Abbrev = new BitCodeAbbrev();
2473   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2474   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2475   unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2476 
2477   Abbrev = new BitCodeAbbrev();
2478   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2479   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));  // Other module
2480   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Message
2481   unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2482 
2483   // Write the submodule metadata block.
2484   RecordData Record;
2485   Record.push_back(getNumberOfModules(WritingModule));
2486   Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2487   Stream.EmitRecord(SUBMODULE_METADATA, Record);
2488 
2489   // Write all of the submodules.
2490   std::queue<Module *> Q;
2491   Q.push(WritingModule);
2492   while (!Q.empty()) {
2493     Module *Mod = Q.front();
2494     Q.pop();
2495     unsigned ID = getSubmoduleID(Mod);
2496 
2497     // Emit the definition of the block.
2498     Record.clear();
2499     Record.push_back(SUBMODULE_DEFINITION);
2500     Record.push_back(ID);
2501     if (Mod->Parent) {
2502       assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2503       Record.push_back(SubmoduleIDs[Mod->Parent]);
2504     } else {
2505       Record.push_back(0);
2506     }
2507     Record.push_back(Mod->IsFramework);
2508     Record.push_back(Mod->IsExplicit);
2509     Record.push_back(Mod->IsSystem);
2510     Record.push_back(Mod->IsExternC);
2511     Record.push_back(Mod->InferSubmodules);
2512     Record.push_back(Mod->InferExplicitSubmodules);
2513     Record.push_back(Mod->InferExportWildcard);
2514     Record.push_back(Mod->ConfigMacrosExhaustive);
2515     Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2516 
2517     // Emit the requirements.
2518     for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
2519       Record.clear();
2520       Record.push_back(SUBMODULE_REQUIRES);
2521       Record.push_back(Mod->Requirements[I].second);
2522       Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2523                                 Mod->Requirements[I].first);
2524     }
2525 
2526     // Emit the umbrella header, if there is one.
2527     if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
2528       Record.clear();
2529       Record.push_back(SUBMODULE_UMBRELLA_HEADER);
2530       Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2531                                 UmbrellaHeader->getName());
2532     } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2533       Record.clear();
2534       Record.push_back(SUBMODULE_UMBRELLA_DIR);
2535       Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2536                                 UmbrellaDir->getName());
2537     }
2538 
2539     // Emit the headers.
2540     struct {
2541       unsigned RecordKind;
2542       unsigned Abbrev;
2543       Module::HeaderKind HeaderKind;
2544     } HeaderLists[] = {
2545       {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2546       {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2547       {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2548       {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2549         Module::HK_PrivateTextual},
2550       {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2551     };
2552     for (auto &HL : HeaderLists) {
2553       Record.clear();
2554       Record.push_back(HL.RecordKind);
2555       for (auto &H : Mod->Headers[HL.HeaderKind])
2556         Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2557     }
2558 
2559     // Emit the top headers.
2560     {
2561       auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2562       Record.clear();
2563       Record.push_back(SUBMODULE_TOPHEADER);
2564       for (auto *H : TopHeaders)
2565         Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
2566     }
2567 
2568     // Emit the imports.
2569     if (!Mod->Imports.empty()) {
2570       Record.clear();
2571       for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2572         unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
2573         assert(ImportedID && "Unknown submodule!");
2574         Record.push_back(ImportedID);
2575       }
2576       Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2577     }
2578 
2579     // Emit the exports.
2580     if (!Mod->Exports.empty()) {
2581       Record.clear();
2582       for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2583         if (Module *Exported = Mod->Exports[I].getPointer()) {
2584           unsigned ExportedID = SubmoduleIDs[Exported];
2585           assert(ExportedID > 0 && "Unknown submodule ID?");
2586           Record.push_back(ExportedID);
2587         } else {
2588           Record.push_back(0);
2589         }
2590 
2591         Record.push_back(Mod->Exports[I].getInt());
2592       }
2593       Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2594     }
2595 
2596     //FIXME: How do we emit the 'use'd modules?  They may not be submodules.
2597     // Might be unnecessary as use declarations are only used to build the
2598     // module itself.
2599 
2600     // Emit the link libraries.
2601     for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2602       Record.clear();
2603       Record.push_back(SUBMODULE_LINK_LIBRARY);
2604       Record.push_back(Mod->LinkLibraries[I].IsFramework);
2605       Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2606                                 Mod->LinkLibraries[I].Library);
2607     }
2608 
2609     // Emit the conflicts.
2610     for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2611       Record.clear();
2612       Record.push_back(SUBMODULE_CONFLICT);
2613       unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2614       assert(OtherID && "Unknown submodule!");
2615       Record.push_back(OtherID);
2616       Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2617                                 Mod->Conflicts[I].Message);
2618     }
2619 
2620     // Emit the configuration macros.
2621     for (unsigned I = 0, N =  Mod->ConfigMacros.size(); I != N; ++I) {
2622       Record.clear();
2623       Record.push_back(SUBMODULE_CONFIG_MACRO);
2624       Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2625                                 Mod->ConfigMacros[I]);
2626     }
2627 
2628     // Queue up the submodules of this module.
2629     for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2630                                  SubEnd = Mod->submodule_end();
2631          Sub != SubEnd; ++Sub)
2632       Q.push(*Sub);
2633   }
2634 
2635   Stream.ExitBlock();
2636 
2637   assert((NextSubmoduleID - FirstSubmoduleID
2638             == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
2639 }
2640 
2641 serialization::SubmoduleID
2642 ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
2643   if (Loc.isInvalid() || !WritingModule)
2644     return 0; // No submodule
2645 
2646   // Find the module that owns this location.
2647   ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2648   Module *OwningMod
2649     = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
2650   if (!OwningMod)
2651     return 0;
2652 
2653   // Check whether this submodule is part of our own module.
2654   if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
2655     return 0;
2656 
2657   return getSubmoduleID(OwningMod);
2658 }
2659 
2660 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2661                                               bool isModule) {
2662   // Make sure set diagnostic pragmas don't affect the translation unit that
2663   // imports the module.
2664   // FIXME: Make diagnostic pragma sections work properly with modules.
2665   if (isModule)
2666     return;
2667 
2668   llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2669       DiagStateIDMap;
2670   unsigned CurrID = 0;
2671   DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
2672   RecordData Record;
2673   for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
2674          I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2675          I != E; ++I) {
2676     const DiagnosticsEngine::DiagStatePoint &point = *I;
2677     if (point.Loc.isInvalid())
2678       continue;
2679 
2680     Record.push_back(point.Loc.getRawEncoding());
2681     unsigned &DiagStateID = DiagStateIDMap[point.State];
2682     Record.push_back(DiagStateID);
2683 
2684     if (DiagStateID == 0) {
2685       DiagStateID = ++CurrID;
2686       for (DiagnosticsEngine::DiagState::const_iterator
2687              I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2688         if (I->second.isPragma()) {
2689           Record.push_back(I->first);
2690           Record.push_back((unsigned)I->second.getSeverity());
2691         }
2692       }
2693       Record.push_back(-1); // mark the end of the diag/map pairs for this
2694                             // location.
2695     }
2696   }
2697 
2698   if (!Record.empty())
2699     Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
2700 }
2701 
2702 void ASTWriter::WriteCXXCtorInitializersOffsets() {
2703   if (CXXCtorInitializersOffsets.empty())
2704     return;
2705 
2706   RecordData Record;
2707 
2708   // Create a blob abbreviation for the C++ ctor initializer offsets.
2709   using namespace llvm;
2710 
2711   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2712   Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2713   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2714   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2715   unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2716 
2717   // Write the base specifier offsets table.
2718   Record.clear();
2719   Record.push_back(CXX_CTOR_INITIALIZERS_OFFSETS);
2720   Record.push_back(CXXCtorInitializersOffsets.size());
2721   Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
2722                             data(CXXCtorInitializersOffsets));
2723 }
2724 
2725 void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2726   if (CXXBaseSpecifiersOffsets.empty())
2727     return;
2728 
2729   RecordData Record;
2730 
2731   // Create a blob abbreviation for the C++ base specifiers offsets.
2732   using namespace llvm;
2733 
2734   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2735   Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2736   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2737   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2738   unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2739 
2740   // Write the base specifier offsets table.
2741   Record.clear();
2742   Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2743   Record.push_back(CXXBaseSpecifiersOffsets.size());
2744   Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2745                             data(CXXBaseSpecifiersOffsets));
2746 }
2747 
2748 //===----------------------------------------------------------------------===//
2749 // Type Serialization
2750 //===----------------------------------------------------------------------===//
2751 
2752 /// \brief Write the representation of a type to the AST stream.
2753 void ASTWriter::WriteType(QualType T) {
2754   TypeIdx &Idx = TypeIdxs[T];
2755   if (Idx.getIndex() == 0) // we haven't seen this type before.
2756     Idx = TypeIdx(NextTypeID++);
2757 
2758   assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
2759 
2760   // Record the offset for this type.
2761   unsigned Index = Idx.getIndex() - FirstTypeID;
2762   if (TypeOffsets.size() == Index)
2763     TypeOffsets.push_back(Stream.GetCurrentBitNo());
2764   else if (TypeOffsets.size() < Index) {
2765     TypeOffsets.resize(Index + 1);
2766     TypeOffsets[Index] = Stream.GetCurrentBitNo();
2767   }
2768 
2769   RecordData Record;
2770 
2771   // Emit the type's representation.
2772   ASTTypeWriter W(*this, Record);
2773   W.AbbrevToUse = 0;
2774 
2775   if (T.hasLocalNonFastQualifiers()) {
2776     Qualifiers Qs = T.getLocalQualifiers();
2777     AddTypeRef(T.getLocalUnqualifiedType(), Record);
2778     Record.push_back(Qs.getAsOpaqueValue());
2779     W.Code = TYPE_EXT_QUAL;
2780     W.AbbrevToUse = TypeExtQualAbbrev;
2781   } else {
2782     switch (T->getTypeClass()) {
2783       // For all of the concrete, non-dependent types, call the
2784       // appropriate visitor function.
2785 #define TYPE(Class, Base) \
2786     case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
2787 #define ABSTRACT_TYPE(Class, Base)
2788 #include "clang/AST/TypeNodes.def"
2789     }
2790   }
2791 
2792   // Emit the serialized record.
2793   Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
2794 
2795   // Flush any expressions that were written as part of this type.
2796   FlushStmts();
2797 }
2798 
2799 //===----------------------------------------------------------------------===//
2800 // Declaration Serialization
2801 //===----------------------------------------------------------------------===//
2802 
2803 /// \brief Write the block containing all of the declaration IDs
2804 /// lexically declared within the given DeclContext.
2805 ///
2806 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2807 /// bistream, or 0 if no block was written.
2808 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
2809                                                  DeclContext *DC) {
2810   if (DC->decls_empty())
2811     return 0;
2812 
2813   uint64_t Offset = Stream.GetCurrentBitNo();
2814   RecordData Record;
2815   Record.push_back(DECL_CONTEXT_LEXICAL);
2816   SmallVector<KindDeclIDPair, 64> Decls;
2817   for (const auto *D : DC->decls())
2818     Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
2819 
2820   ++NumLexicalDeclContexts;
2821   Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
2822   return Offset;
2823 }
2824 
2825 void ASTWriter::WriteTypeDeclOffsets() {
2826   using namespace llvm;
2827   RecordData Record;
2828 
2829   // Write the type offsets array
2830   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2831   Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
2832   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2833   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
2834   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2835   unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2836   Record.clear();
2837   Record.push_back(TYPE_OFFSET);
2838   Record.push_back(TypeOffsets.size());
2839   Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
2840   Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
2841 
2842   // Write the declaration offsets array
2843   Abbrev = new BitCodeAbbrev();
2844   Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
2845   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2846   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
2847   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2848   unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2849   Record.clear();
2850   Record.push_back(DECL_OFFSET);
2851   Record.push_back(DeclOffsets.size());
2852   Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
2853   Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
2854 }
2855 
2856 void ASTWriter::WriteFileDeclIDsMap() {
2857   using namespace llvm;
2858   RecordData Record;
2859 
2860   // Join the vectors of DeclIDs from all files.
2861   SmallVector<DeclID, 256> FileSortedIDs;
2862   for (FileDeclIDsTy::iterator
2863          FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2864     DeclIDInFileInfo &Info = *FI->second;
2865     Info.FirstDeclIndex = FileSortedIDs.size();
2866     for (LocDeclIDsTy::iterator
2867            DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2868       FileSortedIDs.push_back(DI->second);
2869   }
2870 
2871   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2872   Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2873   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2874   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2875   unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2876   Record.push_back(FILE_SORTED_DECLS);
2877   Record.push_back(FileSortedIDs.size());
2878   Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2879 }
2880 
2881 void ASTWriter::WriteComments() {
2882   Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
2883   ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
2884   RecordData Record;
2885   for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2886                                         E = RawComments.end();
2887        I != E; ++I) {
2888     Record.clear();
2889     AddSourceRange((*I)->getSourceRange(), Record);
2890     Record.push_back((*I)->getKind());
2891     Record.push_back((*I)->isTrailingComment());
2892     Record.push_back((*I)->isAlmostTrailingComment());
2893     Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2894   }
2895   Stream.ExitBlock();
2896 }
2897 
2898 //===----------------------------------------------------------------------===//
2899 // Global Method Pool and Selector Serialization
2900 //===----------------------------------------------------------------------===//
2901 
2902 namespace {
2903 // Trait used for the on-disk hash table used in the method pool.
2904 class ASTMethodPoolTrait {
2905   ASTWriter &Writer;
2906 
2907 public:
2908   typedef Selector key_type;
2909   typedef key_type key_type_ref;
2910 
2911   struct data_type {
2912     SelectorID ID;
2913     ObjCMethodList Instance, Factory;
2914   };
2915   typedef const data_type& data_type_ref;
2916 
2917   typedef unsigned hash_value_type;
2918   typedef unsigned offset_type;
2919 
2920   explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
2921 
2922   static hash_value_type ComputeHash(Selector Sel) {
2923     return serialization::ComputeHash(Sel);
2924   }
2925 
2926   std::pair<unsigned,unsigned>
2927     EmitKeyDataLength(raw_ostream& Out, Selector Sel,
2928                       data_type_ref Methods) {
2929     using namespace llvm::support;
2930     endian::Writer<little> LE(Out);
2931     unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2932     LE.write<uint16_t>(KeyLen);
2933     unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2934     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2935          Method = Method->getNext())
2936       if (Method->getMethod())
2937         DataLen += 4;
2938     for (const ObjCMethodList *Method = &Methods.Factory; Method;
2939          Method = Method->getNext())
2940       if (Method->getMethod())
2941         DataLen += 4;
2942     LE.write<uint16_t>(DataLen);
2943     return std::make_pair(KeyLen, DataLen);
2944   }
2945 
2946   void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
2947     using namespace llvm::support;
2948     endian::Writer<little> LE(Out);
2949     uint64_t Start = Out.tell();
2950     assert((Start >> 32) == 0 && "Selector key offset too large");
2951     Writer.SetSelectorOffset(Sel, Start);
2952     unsigned N = Sel.getNumArgs();
2953     LE.write<uint16_t>(N);
2954     if (N == 0)
2955       N = 1;
2956     for (unsigned I = 0; I != N; ++I)
2957       LE.write<uint32_t>(
2958           Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2959   }
2960 
2961   void EmitData(raw_ostream& Out, key_type_ref,
2962                 data_type_ref Methods, unsigned DataLen) {
2963     using namespace llvm::support;
2964     endian::Writer<little> LE(Out);
2965     uint64_t Start = Out.tell(); (void)Start;
2966     LE.write<uint32_t>(Methods.ID);
2967     unsigned NumInstanceMethods = 0;
2968     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2969          Method = Method->getNext())
2970       if (Method->getMethod())
2971         ++NumInstanceMethods;
2972 
2973     unsigned NumFactoryMethods = 0;
2974     for (const ObjCMethodList *Method = &Methods.Factory; Method;
2975          Method = Method->getNext())
2976       if (Method->getMethod())
2977         ++NumFactoryMethods;
2978 
2979     unsigned InstanceBits = Methods.Instance.getBits();
2980     assert(InstanceBits < 4);
2981     unsigned InstanceHasMoreThanOneDeclBit =
2982         Methods.Instance.hasMoreThanOneDecl();
2983     unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2984                                 (InstanceHasMoreThanOneDeclBit << 2) |
2985                                 InstanceBits;
2986     unsigned FactoryBits = Methods.Factory.getBits();
2987     assert(FactoryBits < 4);
2988     unsigned FactoryHasMoreThanOneDeclBit =
2989         Methods.Factory.hasMoreThanOneDecl();
2990     unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2991                                (FactoryHasMoreThanOneDeclBit << 2) |
2992                                FactoryBits;
2993     LE.write<uint16_t>(FullInstanceBits);
2994     LE.write<uint16_t>(FullFactoryBits);
2995     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2996          Method = Method->getNext())
2997       if (Method->getMethod())
2998         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
2999     for (const ObjCMethodList *Method = &Methods.Factory; Method;
3000          Method = Method->getNext())
3001       if (Method->getMethod())
3002         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3003 
3004     assert(Out.tell() - Start == DataLen && "Data length is wrong");
3005   }
3006 };
3007 } // end anonymous namespace
3008 
3009 /// \brief Write ObjC data: selectors and the method pool.
3010 ///
3011 /// The method pool contains both instance and factory methods, stored
3012 /// in an on-disk hash table indexed by the selector. The hash table also
3013 /// contains an empty entry for every other selector known to Sema.
3014 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3015   using namespace llvm;
3016 
3017   // Do we have to do anything at all?
3018   if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
3019     return;
3020   unsigned NumTableEntries = 0;
3021   // Create and write out the blob that contains selectors and the method pool.
3022   {
3023     llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3024     ASTMethodPoolTrait Trait(*this);
3025 
3026     // Create the on-disk hash table representation. We walk through every
3027     // selector we've seen and look it up in the method pool.
3028     SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3029     for (llvm::DenseMap<Selector, SelectorID>::iterator
3030              I = SelectorIDs.begin(), E = SelectorIDs.end();
3031          I != E; ++I) {
3032       Selector S = I->first;
3033       Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
3034       ASTMethodPoolTrait::data_type Data = {
3035         I->second,
3036         ObjCMethodList(),
3037         ObjCMethodList()
3038       };
3039       if (F != SemaRef.MethodPool.end()) {
3040         Data.Instance = F->second.first;
3041         Data.Factory = F->second.second;
3042       }
3043       // Only write this selector if it's not in an existing AST or something
3044       // changed.
3045       if (Chain && I->second < FirstSelectorID) {
3046         // Selector already exists. Did it change?
3047         bool changed = false;
3048         for (ObjCMethodList *M = &Data.Instance;
3049              !changed && M && M->getMethod(); M = M->getNext()) {
3050           if (!M->getMethod()->isFromASTFile())
3051             changed = true;
3052         }
3053         for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
3054              M = M->getNext()) {
3055           if (!M->getMethod()->isFromASTFile())
3056             changed = true;
3057         }
3058         if (!changed)
3059           continue;
3060       } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3061         // A new method pool entry.
3062         ++NumTableEntries;
3063       }
3064       Generator.insert(S, Data, Trait);
3065     }
3066 
3067     // Create the on-disk hash table in a buffer.
3068     SmallString<4096> MethodPool;
3069     uint32_t BucketOffset;
3070     {
3071       using namespace llvm::support;
3072       ASTMethodPoolTrait Trait(*this);
3073       llvm::raw_svector_ostream Out(MethodPool);
3074       // Make sure that no bucket is at offset 0
3075       endian::Writer<little>(Out).write<uint32_t>(0);
3076       BucketOffset = Generator.Emit(Out, Trait);
3077     }
3078 
3079     // Create a blob abbreviation
3080     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3081     Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3082     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3083     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3084     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3085     unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3086 
3087     // Write the method pool
3088     RecordData Record;
3089     Record.push_back(METHOD_POOL);
3090     Record.push_back(BucketOffset);
3091     Record.push_back(NumTableEntries);
3092     Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3093 
3094     // Create a blob abbreviation for the selector table offsets.
3095     Abbrev = new BitCodeAbbrev();
3096     Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3097     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3098     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3099     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3100     unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3101 
3102     // Write the selector offsets table.
3103     Record.clear();
3104     Record.push_back(SELECTOR_OFFSETS);
3105     Record.push_back(SelectorOffsets.size());
3106     Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
3107     Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3108                               data(SelectorOffsets));
3109   }
3110 }
3111 
3112 /// \brief Write the selectors referenced in @selector expression into AST file.
3113 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3114   using namespace llvm;
3115   if (SemaRef.ReferencedSelectors.empty())
3116     return;
3117 
3118   RecordData Record;
3119 
3120   // Note: this writes out all references even for a dependent AST. But it is
3121   // very tricky to fix, and given that @selector shouldn't really appear in
3122   // headers, probably not worth it. It's not a correctness issue.
3123   for (DenseMap<Selector, SourceLocation>::iterator S =
3124        SemaRef.ReferencedSelectors.begin(),
3125        E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3126     Selector Sel = (*S).first;
3127     SourceLocation Loc = (*S).second;
3128     AddSelectorRef(Sel, Record);
3129     AddSourceLocation(Loc, Record);
3130   }
3131   Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
3132 }
3133 
3134 //===----------------------------------------------------------------------===//
3135 // Identifier Table Serialization
3136 //===----------------------------------------------------------------------===//
3137 
3138 /// Determine the declaration that should be put into the name lookup table to
3139 /// represent the given declaration in this module. This is usually D itself,
3140 /// but if D was imported and merged into a local declaration, we want the most
3141 /// recent local declaration instead. The chosen declaration will be the most
3142 /// recent declaration in any module that imports this one.
3143 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3144                                         NamedDecl *D) {
3145   if (!LangOpts.Modules || !D->isFromASTFile())
3146     return D;
3147 
3148   if (Decl *Redecl = D->getPreviousDecl()) {
3149     // For Redeclarable decls, a prior declaration might be local.
3150     for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3151       if (!Redecl->isFromASTFile())
3152         return cast<NamedDecl>(Redecl);
3153       // If we find a decl from a (chained-)PCH stop since we won't find a
3154       // local one.
3155       if (D->getOwningModuleID() == 0)
3156         break;
3157     }
3158   } else if (Decl *First = D->getCanonicalDecl()) {
3159     // For Mergeable decls, the first decl might be local.
3160     if (!First->isFromASTFile())
3161       return cast<NamedDecl>(First);
3162   }
3163 
3164   // All declarations are imported. Our most recent declaration will also be
3165   // the most recent one in anyone who imports us.
3166   return D;
3167 }
3168 
3169 namespace {
3170 class ASTIdentifierTableTrait {
3171   ASTWriter &Writer;
3172   Preprocessor &PP;
3173   IdentifierResolver &IdResolver;
3174   bool IsModule;
3175 
3176   /// \brief Determines whether this is an "interesting" identifier
3177   /// that needs a full IdentifierInfo structure written into the hash
3178   /// table.
3179   bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
3180     if (II->isPoisoned() ||
3181         II->isExtensionToken() ||
3182         II->getObjCOrBuiltinID() ||
3183         II->hasRevertedTokenIDToIdentifier() ||
3184         II->getFETokenInfo<void>())
3185       return true;
3186 
3187     return hadMacroDefinition(II, Macro);
3188   }
3189 
3190   bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
3191     if (!II->hadMacroDefinition())
3192       return false;
3193 
3194     if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3195       if (!IsModule)
3196         return !shouldIgnoreMacro(Macro, IsModule, PP);
3197 
3198       MacroState State;
3199       if (getFirstPublicSubmoduleMacro(Macro, State))
3200         return true;
3201     }
3202 
3203     return false;
3204   }
3205 
3206   enum class SubmoduleMacroState {
3207     /// We've seen nothing about this macro.
3208     None,
3209     /// We've seen a public visibility directive.
3210     Public,
3211     /// We've either exported a macro for this module or found that the
3212     /// module's definition of this macro is private.
3213     Done
3214   };
3215   typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
3216 
3217   MacroDirective *
3218   getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3219     if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3220       return NextMD;
3221     return nullptr;
3222   }
3223 
3224   MacroDirective *
3225   getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3226     if (MacroDirective *NextMD =
3227             getPublicSubmoduleMacro(MD->getPrevious(), State))
3228       return NextMD;
3229     return nullptr;
3230   }
3231 
3232   /// \brief Traverses the macro directives history and returns the next
3233   /// public macro definition or undefinition that has not been found so far.
3234   ///
3235   /// A macro that is defined in submodule A and undefined in submodule B
3236   /// will still be considered as defined/exported from submodule A.
3237   MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
3238                                           MacroState &State) {
3239     if (!MD)
3240       return nullptr;
3241 
3242     Optional<bool> IsPublic;
3243     for (; MD; MD = MD->getPrevious()) {
3244       // Once we hit an ignored macro, we're done: the rest of the chain
3245       // will all be ignored macros.
3246       if (shouldIgnoreMacro(MD, IsModule, PP))
3247         break;
3248 
3249       // If this macro was imported, re-export it.
3250       if (MD->isImported())
3251         return MD;
3252 
3253       SubmoduleID ModID = getSubmoduleID(MD);
3254       auto &S = State[ModID];
3255       assert(ModID && "found macro in no submodule");
3256 
3257       if (S == SubmoduleMacroState::Done)
3258         continue;
3259 
3260       if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3261         // The latest visibility directive for a name in a submodule affects all
3262         // the directives that come before it.
3263         if (S == SubmoduleMacroState::None)
3264           S = VisMD->isPublic() ? SubmoduleMacroState::Public
3265                                 : SubmoduleMacroState::Done;
3266       } else {
3267         S = SubmoduleMacroState::Done;
3268         return MD;
3269       }
3270     }
3271 
3272     return nullptr;
3273   }
3274 
3275   ArrayRef<SubmoduleID>
3276   getOverriddenSubmodules(MacroDirective *MD,
3277                           SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3278     assert(!isa<VisibilityMacroDirective>(MD) &&
3279            "only #define and #undef can override");
3280     if (MD->isImported())
3281       return MD->getOverriddenModules();
3282 
3283     ScratchSpace.clear();
3284     SubmoduleID ModID = getSubmoduleID(MD);
3285     for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3286       if (shouldIgnoreMacro(MD, IsModule, PP))
3287         break;
3288 
3289       // If this is a definition from a submodule import, that submodule's
3290       // definition is overridden by the definition or undefinition that we
3291       // started with.
3292       if (MD->isImported()) {
3293         if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3294           SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3295           assert(DefModuleID && "imported macro has no owning module");
3296           ScratchSpace.push_back(DefModuleID);
3297         } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3298           // If we override a #undef, we override anything that #undef overrides.
3299           // We don't need to override it, since an active #undef doesn't affect
3300           // the meaning of a macro.
3301           auto Overrides = UndefMD->getOverriddenModules();
3302           ScratchSpace.insert(ScratchSpace.end(),
3303                               Overrides.begin(), Overrides.end());
3304         }
3305       }
3306 
3307       // Stop once we leave the original macro's submodule.
3308       //
3309       // Either this submodule #included another submodule of the same
3310       // module or it just happened to be built after the other module.
3311       // In the former case, we override the submodule's macro.
3312       //
3313       // FIXME: In the latter case, we shouldn't do so, but we can't tell
3314       // these cases apart.
3315       //
3316       // FIXME: We can leave this submodule and re-enter it if it #includes a
3317       // header within a different submodule of the same module. In such cases
3318       // the overrides list will be incomplete.
3319       SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3320       if (DirectiveModuleID != ModID) {
3321         if (DirectiveModuleID && !MD->isImported())
3322           ScratchSpace.push_back(DirectiveModuleID);
3323         break;
3324       }
3325     }
3326 
3327     std::sort(ScratchSpace.begin(), ScratchSpace.end());
3328     ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3329                        ScratchSpace.end());
3330     return ScratchSpace;
3331   }
3332 
3333   SubmoduleID getSubmoduleID(MacroDirective *MD) {
3334     return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
3335   }
3336 
3337 public:
3338   typedef IdentifierInfo* key_type;
3339   typedef key_type  key_type_ref;
3340 
3341   typedef IdentID data_type;
3342   typedef data_type data_type_ref;
3343 
3344   typedef unsigned hash_value_type;
3345   typedef unsigned offset_type;
3346 
3347   ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3348                           IdentifierResolver &IdResolver, bool IsModule)
3349     : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
3350 
3351   static hash_value_type ComputeHash(const IdentifierInfo* II) {
3352     return llvm::HashString(II->getName());
3353   }
3354 
3355   std::pair<unsigned,unsigned>
3356   EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3357     unsigned KeyLen = II->getLength() + 1;
3358     unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3359     MacroDirective *Macro = nullptr;
3360     if (isInterestingIdentifier(II, Macro)) {
3361       DataLen += 2; // 2 bytes for builtin ID
3362       DataLen += 2; // 2 bytes for flags
3363       if (hadMacroDefinition(II, Macro)) {
3364         DataLen += 4; // MacroDirectives offset.
3365         if (IsModule) {
3366           MacroState State;
3367           SmallVector<SubmoduleID, 16> Scratch;
3368           for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3369                MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
3370             DataLen += 4; // MacroInfo ID or ModuleID.
3371             if (unsigned NumOverrides =
3372                     getOverriddenSubmodules(MD, Scratch).size())
3373               DataLen += 4 * (1 + NumOverrides);
3374           }
3375           DataLen += 4; // 0 terminator.
3376         }
3377       }
3378 
3379       for (IdentifierResolver::iterator D = IdResolver.begin(II),
3380                                      DEnd = IdResolver.end();
3381            D != DEnd; ++D)
3382         DataLen += 4;
3383     }
3384     using namespace llvm::support;
3385     endian::Writer<little> LE(Out);
3386 
3387     assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
3388     LE.write<uint16_t>(DataLen);
3389     // We emit the key length after the data length so that every
3390     // string is preceded by a 16-bit length. This matches the PTH
3391     // format for storing identifiers.
3392     LE.write<uint16_t>(KeyLen);
3393     return std::make_pair(KeyLen, DataLen);
3394   }
3395 
3396   void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3397                unsigned KeyLen) {
3398     // Record the location of the key data.  This is used when generating
3399     // the mapping from persistent IDs to strings.
3400     Writer.SetIdentifierOffset(II, Out.tell());
3401     Out.write(II->getNameStart(), KeyLen);
3402   }
3403 
3404   static void emitMacroOverrides(raw_ostream &Out,
3405                                  ArrayRef<SubmoduleID> Overridden) {
3406     if (!Overridden.empty()) {
3407       using namespace llvm::support;
3408       endian::Writer<little> LE(Out);
3409       LE.write<uint32_t>(Overridden.size() | 0x80000000U);
3410       for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3411         assert(Overridden[I] && "zero module ID for override");
3412         LE.write<uint32_t>(Overridden[I]);
3413       }
3414     }
3415   }
3416 
3417   void EmitData(raw_ostream& Out, IdentifierInfo* II,
3418                 IdentID ID, unsigned) {
3419     using namespace llvm::support;
3420     endian::Writer<little> LE(Out);
3421     MacroDirective *Macro = nullptr;
3422     if (!isInterestingIdentifier(II, Macro)) {
3423       LE.write<uint32_t>(ID << 1);
3424       return;
3425     }
3426 
3427     LE.write<uint32_t>((ID << 1) | 0x01);
3428     uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3429     assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3430     LE.write<uint16_t>(Bits);
3431     Bits = 0;
3432     bool HadMacroDefinition = hadMacroDefinition(II, Macro);
3433     Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3434     Bits = (Bits << 1) | unsigned(IsModule);
3435     Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3436     Bits = (Bits << 1) | unsigned(II->isPoisoned());
3437     Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3438     Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3439     LE.write<uint16_t>(Bits);
3440 
3441     if (HadMacroDefinition) {
3442       LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
3443       if (IsModule) {
3444         // Write the IDs of macros coming from different submodules.
3445         MacroState State;
3446         SmallVector<SubmoduleID, 16> Scratch;
3447         for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3448              MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
3449           if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3450             // FIXME: If this macro directive was created by #pragma pop_macros,
3451             // or if it was created implicitly by resolving conflicting macros,
3452             // it may be for a different submodule from the one in the MacroInfo
3453             // object. If so, we should write out its owning ModuleID.
3454             MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
3455             assert(InfoID);
3456             LE.write<uint32_t>(InfoID << 1);
3457           } else {
3458             auto *UndefMD = cast<UndefMacroDirective>(MD);
3459             SubmoduleID Mod = UndefMD->isImported()
3460                                   ? UndefMD->getOwningModuleID()
3461                                   : getSubmoduleID(UndefMD);
3462             LE.write<uint32_t>((Mod << 1) | 1);
3463           }
3464           emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
3465         }
3466         LE.write<uint32_t>((uint32_t)-1);
3467       }
3468     }
3469 
3470     // Emit the declaration IDs in reverse order, because the
3471     // IdentifierResolver provides the declarations as they would be
3472     // visible (e.g., the function "stat" would come before the struct
3473     // "stat"), but the ASTReader adds declarations to the end of the list
3474     // (so we need to see the struct "stat" before the function "stat").
3475     // Only emit declarations that aren't from a chained PCH, though.
3476     SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II), IdResolver.end());
3477     for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3478                                                         DEnd = Decls.rend();
3479          D != DEnd; ++D)
3480       LE.write<uint32_t>(
3481           Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3482   }
3483 };
3484 } // end anonymous namespace
3485 
3486 /// \brief Write the identifier table into the AST file.
3487 ///
3488 /// The identifier table consists of a blob containing string data
3489 /// (the actual identifiers themselves) and a separate "offsets" index
3490 /// that maps identifier IDs to locations within the blob.
3491 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3492                                      IdentifierResolver &IdResolver,
3493                                      bool IsModule) {
3494   using namespace llvm;
3495 
3496   // Create and write out the blob that contains the identifier
3497   // strings.
3498   {
3499     llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3500     ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
3501 
3502     // Look for any identifiers that were named while processing the
3503     // headers, but are otherwise not needed. We add these to the hash
3504     // table to enable checking of the predefines buffer in the case
3505     // where the user adds new macro definitions when building the AST
3506     // file.
3507     for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3508                                 IDEnd = PP.getIdentifierTable().end();
3509          ID != IDEnd; ++ID)
3510       getIdentifierRef(ID->second);
3511 
3512     // Create the on-disk hash table representation. We only store offsets
3513     // for identifiers that appear here for the first time.
3514     IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3515     for (auto IdentIDPair : IdentifierIDs) {
3516       IdentifierInfo *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3517       IdentID ID = IdentIDPair.second;
3518       assert(II && "NULL identifier in identifier table");
3519       if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3520         Generator.insert(II, ID, Trait);
3521     }
3522 
3523     // Create the on-disk hash table in a buffer.
3524     SmallString<4096> IdentifierTable;
3525     uint32_t BucketOffset;
3526     {
3527       using namespace llvm::support;
3528       ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
3529       llvm::raw_svector_ostream Out(IdentifierTable);
3530       // Make sure that no bucket is at offset 0
3531       endian::Writer<little>(Out).write<uint32_t>(0);
3532       BucketOffset = Generator.Emit(Out, Trait);
3533     }
3534 
3535     // Create a blob abbreviation
3536     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3537     Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3538     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3539     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3540     unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
3541 
3542     // Write the identifier table
3543     RecordData Record;
3544     Record.push_back(IDENTIFIER_TABLE);
3545     Record.push_back(BucketOffset);
3546     Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3547   }
3548 
3549   // Write the offsets table for identifier IDs.
3550   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3551   Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3552   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3553   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3554   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3555   unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3556 
3557 #ifndef NDEBUG
3558   for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3559     assert(IdentifierOffsets[I] && "Missing identifier offset?");
3560 #endif
3561 
3562   RecordData Record;
3563   Record.push_back(IDENTIFIER_OFFSET);
3564   Record.push_back(IdentifierOffsets.size());
3565   Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
3566   Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3567                             data(IdentifierOffsets));
3568 }
3569 
3570 //===----------------------------------------------------------------------===//
3571 // DeclContext's Name Lookup Table Serialization
3572 //===----------------------------------------------------------------------===//
3573 
3574 namespace {
3575 // Trait used for the on-disk hash table used in the method pool.
3576 class ASTDeclContextNameLookupTrait {
3577   ASTWriter &Writer;
3578 
3579 public:
3580   typedef DeclarationName key_type;
3581   typedef key_type key_type_ref;
3582 
3583   typedef DeclContext::lookup_result data_type;
3584   typedef const data_type& data_type_ref;
3585 
3586   typedef unsigned hash_value_type;
3587   typedef unsigned offset_type;
3588 
3589   explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3590 
3591   hash_value_type ComputeHash(DeclarationName Name) {
3592     llvm::FoldingSetNodeID ID;
3593     ID.AddInteger(Name.getNameKind());
3594 
3595     switch (Name.getNameKind()) {
3596     case DeclarationName::Identifier:
3597       ID.AddString(Name.getAsIdentifierInfo()->getName());
3598       break;
3599     case DeclarationName::ObjCZeroArgSelector:
3600     case DeclarationName::ObjCOneArgSelector:
3601     case DeclarationName::ObjCMultiArgSelector:
3602       ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3603       break;
3604     case DeclarationName::CXXConstructorName:
3605     case DeclarationName::CXXDestructorName:
3606     case DeclarationName::CXXConversionFunctionName:
3607       break;
3608     case DeclarationName::CXXOperatorName:
3609       ID.AddInteger(Name.getCXXOverloadedOperator());
3610       break;
3611     case DeclarationName::CXXLiteralOperatorName:
3612       ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3613     case DeclarationName::CXXUsingDirective:
3614       break;
3615     }
3616 
3617     return ID.ComputeHash();
3618   }
3619 
3620   std::pair<unsigned,unsigned>
3621     EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
3622                       data_type_ref Lookup) {
3623     using namespace llvm::support;
3624     endian::Writer<little> LE(Out);
3625     unsigned KeyLen = 1;
3626     switch (Name.getNameKind()) {
3627     case DeclarationName::Identifier:
3628     case DeclarationName::ObjCZeroArgSelector:
3629     case DeclarationName::ObjCOneArgSelector:
3630     case DeclarationName::ObjCMultiArgSelector:
3631     case DeclarationName::CXXLiteralOperatorName:
3632       KeyLen += 4;
3633       break;
3634     case DeclarationName::CXXOperatorName:
3635       KeyLen += 1;
3636       break;
3637     case DeclarationName::CXXConstructorName:
3638     case DeclarationName::CXXDestructorName:
3639     case DeclarationName::CXXConversionFunctionName:
3640     case DeclarationName::CXXUsingDirective:
3641       break;
3642     }
3643     LE.write<uint16_t>(KeyLen);
3644 
3645     // 2 bytes for num of decls and 4 for each DeclID.
3646     unsigned DataLen = 2 + 4 * Lookup.size();
3647     LE.write<uint16_t>(DataLen);
3648 
3649     return std::make_pair(KeyLen, DataLen);
3650   }
3651 
3652   void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
3653     using namespace llvm::support;
3654     endian::Writer<little> LE(Out);
3655     LE.write<uint8_t>(Name.getNameKind());
3656     switch (Name.getNameKind()) {
3657     case DeclarationName::Identifier:
3658       LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
3659       return;
3660     case DeclarationName::ObjCZeroArgSelector:
3661     case DeclarationName::ObjCOneArgSelector:
3662     case DeclarationName::ObjCMultiArgSelector:
3663       LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
3664       return;
3665     case DeclarationName::CXXOperatorName:
3666       assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3667              "Invalid operator?");
3668       LE.write<uint8_t>(Name.getCXXOverloadedOperator());
3669       return;
3670     case DeclarationName::CXXLiteralOperatorName:
3671       LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
3672       return;
3673     case DeclarationName::CXXConstructorName:
3674     case DeclarationName::CXXDestructorName:
3675     case DeclarationName::CXXConversionFunctionName:
3676     case DeclarationName::CXXUsingDirective:
3677       return;
3678     }
3679 
3680     llvm_unreachable("Invalid name kind?");
3681   }
3682 
3683   void EmitData(raw_ostream& Out, key_type_ref,
3684                 data_type Lookup, unsigned DataLen) {
3685     using namespace llvm::support;
3686     endian::Writer<little> LE(Out);
3687     uint64_t Start = Out.tell(); (void)Start;
3688     LE.write<uint16_t>(Lookup.size());
3689     for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3690          I != E; ++I)
3691       LE.write<uint32_t>(
3692           Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I)));
3693 
3694     assert(Out.tell() - Start == DataLen && "Data length is wrong");
3695   }
3696 };
3697 } // end anonymous namespace
3698 
3699 template<typename Visitor>
3700 void ASTWriter::visitLocalLookupResults(const DeclContext *ConstDC,
3701                                         Visitor AddLookupResult) {
3702   // FIXME: We need to build the lookups table, which is logically const.
3703   DeclContext *DC = const_cast<DeclContext*>(ConstDC);
3704   assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3705 
3706   SmallVector<DeclarationName, 16> ExternalNames;
3707   for (auto &Lookup : *DC->buildLookup()) {
3708     if (Lookup.second.hasExternalDecls() ||
3709         DC->NeedToReconcileExternalVisibleStorage) {
3710       // If there are no local declarations in our lookup result, we don't
3711       // need to write an entry for the name at all unless we're rewriting
3712       // the decl context. If we can't write out a lookup set without
3713       // performing more deserialization, just skip this entry.
3714       if (!isRewritten(cast<Decl>(DC))) {
3715         bool AllFromASTFile = true;
3716         for (auto *D : Lookup.second.getLookupResult()) {
3717           AllFromASTFile &=
3718               getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile();
3719           if (!AllFromASTFile)
3720             break;
3721         }
3722         if (AllFromASTFile)
3723           continue;
3724       }
3725 
3726       // We don't know for sure what declarations are found by this name,
3727       // because the external source might have a different set from the set
3728       // that are in the lookup map, and we can't update it now without
3729       // risking invalidating our lookup iterator. So add it to a queue to
3730       // deal with later.
3731       ExternalNames.push_back(Lookup.first);
3732       continue;
3733     }
3734 
3735     AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3736   }
3737 
3738   // Add the names we needed to defer. Note, this shouldn't add any new decls
3739   // to the list we need to serialize: any new declarations we find here should
3740   // be imported from an external source.
3741   // FIXME: What if the external source isn't an ASTReader?
3742   for (const auto &Name : ExternalNames)
3743     AddLookupResult(Name, DC->lookup(Name));
3744 }
3745 
3746 void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
3747   if (UpdatedDeclContexts.insert(DC).second && WritingAST) {
3748     // Ensure we emit all the visible declarations.
3749     visitLocalLookupResults(DC, [&](DeclarationName Name,
3750                                     DeclContext::lookup_result Result) {
3751       for (auto *Decl : Result)
3752         GetDeclRef(getDeclForLocalLookup(getLangOpts(), Decl));
3753     });
3754   }
3755 }
3756 
3757 uint32_t
3758 ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3759                                    llvm::SmallVectorImpl<char> &LookupTable) {
3760   assert(!DC->HasLazyLocalLexicalLookups &&
3761          !DC->HasLazyExternalLexicalLookups &&
3762          "must call buildLookups first");
3763 
3764   llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3765       Generator;
3766   ASTDeclContextNameLookupTrait Trait(*this);
3767 
3768   // Create the on-disk hash table representation.
3769   DeclarationName ConstructorName;
3770   DeclarationName ConversionName;
3771   SmallVector<NamedDecl *, 8> ConstructorDecls;
3772   SmallVector<NamedDecl *, 4> ConversionDecls;
3773 
3774   visitLocalLookupResults(DC, [&](DeclarationName Name,
3775                                   DeclContext::lookup_result Result) {
3776     if (Result.empty())
3777       return;
3778 
3779     // Different DeclarationName values of certain kinds are mapped to
3780     // identical serialized keys, because we don't want to use type
3781     // identifiers in the keys (since type ids are local to the module).
3782     switch (Name.getNameKind()) {
3783     case DeclarationName::CXXConstructorName:
3784       // There may be different CXXConstructorName DeclarationName values
3785       // in a DeclContext because a UsingDecl that inherits constructors
3786       // has the DeclarationName of the inherited constructors.
3787       if (!ConstructorName)
3788         ConstructorName = Name;
3789       ConstructorDecls.append(Result.begin(), Result.end());
3790       return;
3791 
3792     case DeclarationName::CXXConversionFunctionName:
3793       if (!ConversionName)
3794         ConversionName = Name;
3795       ConversionDecls.append(Result.begin(), Result.end());
3796       return;
3797 
3798     default:
3799       break;
3800     }
3801 
3802     Generator.insert(Name, Result, Trait);
3803   });
3804 
3805   // Add the constructors.
3806   if (!ConstructorDecls.empty()) {
3807     Generator.insert(ConstructorName,
3808                      DeclContext::lookup_result(ConstructorDecls),
3809                      Trait);
3810   }
3811 
3812   // Add the conversion functions.
3813   if (!ConversionDecls.empty()) {
3814     Generator.insert(ConversionName,
3815                      DeclContext::lookup_result(ConversionDecls),
3816                      Trait);
3817   }
3818 
3819   // Create the on-disk hash table in a buffer.
3820   llvm::raw_svector_ostream Out(LookupTable);
3821   // Make sure that no bucket is at offset 0
3822   using namespace llvm::support;
3823   endian::Writer<little>(Out).write<uint32_t>(0);
3824   return Generator.Emit(Out, Trait);
3825 }
3826 
3827 /// \brief Write the block containing all of the declaration IDs
3828 /// visible from the given DeclContext.
3829 ///
3830 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
3831 /// bitstream, or 0 if no block was written.
3832 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3833                                                  DeclContext *DC) {
3834   if (DC->getPrimaryContext() != DC)
3835     return 0;
3836 
3837   // Since there is no name lookup into functions or methods, don't bother to
3838   // build a visible-declarations table for these entities.
3839   if (DC->isFunctionOrMethod())
3840     return 0;
3841 
3842   // If not in C++, we perform name lookup for the translation unit via the
3843   // IdentifierInfo chains, don't bother to build a visible-declarations table.
3844   if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
3845     return 0;
3846 
3847   // Serialize the contents of the mapping used for lookup. Note that,
3848   // although we have two very different code paths, the serialized
3849   // representation is the same for both cases: a declaration name,
3850   // followed by a size, followed by references to the visible
3851   // declarations that have that name.
3852   uint64_t Offset = Stream.GetCurrentBitNo();
3853   StoredDeclsMap *Map = DC->buildLookup();
3854   if (!Map || Map->empty())
3855     return 0;
3856 
3857   // Create the on-disk hash table in a buffer.
3858   SmallString<4096> LookupTable;
3859   uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
3860 
3861   // Write the lookup table
3862   RecordData Record;
3863   Record.push_back(DECL_CONTEXT_VISIBLE);
3864   Record.push_back(BucketOffset);
3865   Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3866                             LookupTable);
3867   ++NumVisibleDeclContexts;
3868   return Offset;
3869 }
3870 
3871 /// \brief Write an UPDATE_VISIBLE block for the given context.
3872 ///
3873 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3874 /// DeclContext in a dependent AST file. As such, they only exist for the TU
3875 /// (in C++), for namespaces, and for classes with forward-declared unscoped
3876 /// enumeration members (in C++11).
3877 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
3878   StoredDeclsMap *Map = DC->getLookupPtr();
3879   if (!Map || Map->empty())
3880     return;
3881 
3882   // Create the on-disk hash table in a buffer.
3883   SmallString<4096> LookupTable;
3884   uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
3885 
3886   // Write the lookup table
3887   RecordData Record;
3888   Record.push_back(UPDATE_VISIBLE);
3889   Record.push_back(getDeclID(cast<Decl>(DC)));
3890   Record.push_back(BucketOffset);
3891   Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
3892 }
3893 
3894 /// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3895 void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3896   RecordData Record;
3897   Record.push_back(Opts.fp_contract);
3898   Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3899 }
3900 
3901 /// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3902 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
3903   if (!SemaRef.Context.getLangOpts().OpenCL)
3904     return;
3905 
3906   const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3907   RecordData Record;
3908 #define OPENCLEXT(nm)  Record.push_back(Opts.nm);
3909 #include "clang/Basic/OpenCLExtensions.def"
3910   Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3911 }
3912 
3913 void ASTWriter::WriteRedeclarations() {
3914   RecordData LocalRedeclChains;
3915   SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3916 
3917   for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3918     Decl *First = Redeclarations[I];
3919     assert(First->isFirstDecl() && "Not the first declaration?");
3920 
3921     Decl *MostRecent = First->getMostRecentDecl();
3922 
3923     // If we only have a single declaration, there is no point in storing
3924     // a redeclaration chain.
3925     if (First == MostRecent)
3926       continue;
3927 
3928     unsigned Offset = LocalRedeclChains.size();
3929     unsigned Size = 0;
3930     LocalRedeclChains.push_back(0); // Placeholder for the size.
3931 
3932     // Collect the set of local redeclarations of this declaration.
3933     for (Decl *Prev = MostRecent; Prev != First;
3934          Prev = Prev->getPreviousDecl()) {
3935       if (!Prev->isFromASTFile()) {
3936         AddDeclRef(Prev, LocalRedeclChains);
3937         ++Size;
3938       }
3939     }
3940 
3941     LocalRedeclChains[Offset] = Size;
3942 
3943     // Reverse the set of local redeclarations, so that we store them in
3944     // order (since we found them in reverse order).
3945     std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3946 
3947     // Add the mapping from the first ID from the AST to the set of local
3948     // declarations.
3949     LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3950     LocalRedeclsMap.push_back(Info);
3951 
3952     assert(N == Redeclarations.size() &&
3953            "Deserialized a declaration we shouldn't have");
3954   }
3955 
3956   if (LocalRedeclChains.empty())
3957     return;
3958 
3959   // Sort the local redeclarations map by the first declaration ID,
3960   // since the reader will be performing binary searches on this information.
3961   llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3962 
3963   // Emit the local redeclarations map.
3964   using namespace llvm;
3965   llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3966   Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3967   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3968   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3969   unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3970 
3971   RecordData Record;
3972   Record.push_back(LOCAL_REDECLARATIONS_MAP);
3973   Record.push_back(LocalRedeclsMap.size());
3974   Stream.EmitRecordWithBlob(AbbrevID, Record,
3975     reinterpret_cast<char*>(LocalRedeclsMap.data()),
3976     LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3977 
3978   // Emit the redeclaration chains.
3979   Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3980 }
3981 
3982 void ASTWriter::WriteObjCCategories() {
3983   SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3984   RecordData Categories;
3985 
3986   for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3987     unsigned Size = 0;
3988     unsigned StartIndex = Categories.size();
3989 
3990     ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3991 
3992     // Allocate space for the size.
3993     Categories.push_back(0);
3994 
3995     // Add the categories.
3996     for (ObjCInterfaceDecl::known_categories_iterator
3997            Cat = Class->known_categories_begin(),
3998            CatEnd = Class->known_categories_end();
3999          Cat != CatEnd; ++Cat, ++Size) {
4000       assert(getDeclID(*Cat) != 0 && "Bogus category");
4001       AddDeclRef(*Cat, Categories);
4002     }
4003 
4004     // Update the size.
4005     Categories[StartIndex] = Size;
4006 
4007     // Record this interface -> category map.
4008     ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4009     CategoriesMap.push_back(CatInfo);
4010   }
4011 
4012   // Sort the categories map by the definition ID, since the reader will be
4013   // performing binary searches on this information.
4014   llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4015 
4016   // Emit the categories map.
4017   using namespace llvm;
4018   llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4019   Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4020   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4021   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4022   unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
4023 
4024   RecordData Record;
4025   Record.push_back(OBJC_CATEGORIES_MAP);
4026   Record.push_back(CategoriesMap.size());
4027   Stream.EmitRecordWithBlob(AbbrevID, Record,
4028                             reinterpret_cast<char*>(CategoriesMap.data()),
4029                             CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4030 
4031   // Emit the category lists.
4032   Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4033 }
4034 
4035 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4036   Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4037 
4038   if (LPTMap.empty())
4039     return;
4040 
4041   RecordData Record;
4042   for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4043                                               ItEnd = LPTMap.end();
4044        It != ItEnd; ++It) {
4045     LateParsedTemplate *LPT = It->second;
4046     AddDeclRef(It->first, Record);
4047     AddDeclRef(LPT->D, Record);
4048     Record.push_back(LPT->Toks.size());
4049 
4050     for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4051                                 TokEnd = LPT->Toks.end();
4052          TokIt != TokEnd; ++TokIt) {
4053       AddToken(*TokIt, Record);
4054     }
4055   }
4056   Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4057 }
4058 
4059 /// \brief Write the state of 'pragma clang optimize' at the end of the module.
4060 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4061   RecordData Record;
4062   SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4063   AddSourceLocation(PragmaLoc, Record);
4064   Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4065 }
4066 
4067 //===----------------------------------------------------------------------===//
4068 // General Serialization Routines
4069 //===----------------------------------------------------------------------===//
4070 
4071 /// \brief Write a record containing the given attributes.
4072 void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4073                                 RecordDataImpl &Record) {
4074   Record.push_back(Attrs.size());
4075   for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4076                                         e = Attrs.end(); i != e; ++i){
4077     const Attr *A = *i;
4078     Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
4079     AddSourceRange(A->getRange(), Record);
4080 
4081 #include "clang/Serialization/AttrPCHWrite.inc"
4082 
4083   }
4084 }
4085 
4086 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4087   AddSourceLocation(Tok.getLocation(), Record);
4088   Record.push_back(Tok.getLength());
4089 
4090   // FIXME: When reading literal tokens, reconstruct the literal pointer
4091   // if it is needed.
4092   AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4093   // FIXME: Should translate token kind to a stable encoding.
4094   Record.push_back(Tok.getKind());
4095   // FIXME: Should translate token flags to a stable encoding.
4096   Record.push_back(Tok.getFlags());
4097 }
4098 
4099 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4100   Record.push_back(Str.size());
4101   Record.insert(Record.end(), Str.begin(), Str.end());
4102 }
4103 
4104 bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4105   assert(Context && "should have context when outputting path");
4106 
4107   bool Changed =
4108       cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4109 
4110   // Remove a prefix to make the path relative, if relevant.
4111   const char *PathBegin = Path.data();
4112   const char *PathPtr =
4113       adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4114   if (PathPtr != PathBegin) {
4115     Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4116     Changed = true;
4117   }
4118 
4119   return Changed;
4120 }
4121 
4122 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4123   SmallString<128> FilePath(Path);
4124   PreparePathForOutput(FilePath);
4125   AddString(FilePath, Record);
4126 }
4127 
4128 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
4129                                    StringRef Path) {
4130   SmallString<128> FilePath(Path);
4131   PreparePathForOutput(FilePath);
4132   Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4133 }
4134 
4135 void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4136                                 RecordDataImpl &Record) {
4137   Record.push_back(Version.getMajor());
4138   if (Optional<unsigned> Minor = Version.getMinor())
4139     Record.push_back(*Minor + 1);
4140   else
4141     Record.push_back(0);
4142   if (Optional<unsigned> Subminor = Version.getSubminor())
4143     Record.push_back(*Subminor + 1);
4144   else
4145     Record.push_back(0);
4146 }
4147 
4148 /// \brief Note that the identifier II occurs at the given offset
4149 /// within the identifier table.
4150 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4151   IdentID ID = IdentifierIDs[II];
4152   // Only store offsets new to this AST file. Other identifier names are looked
4153   // up earlier in the chain and thus don't need an offset.
4154   if (ID >= FirstIdentID)
4155     IdentifierOffsets[ID - FirstIdentID] = Offset;
4156 }
4157 
4158 /// \brief Note that the selector Sel occurs at the given offset
4159 /// within the method pool/selector table.
4160 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4161   unsigned ID = SelectorIDs[Sel];
4162   assert(ID && "Unknown selector");
4163   // Don't record offsets for selectors that are also available in a different
4164   // file.
4165   if (ID < FirstSelectorID)
4166     return;
4167   SelectorOffsets[ID - FirstSelectorID] = Offset;
4168 }
4169 
4170 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
4171     : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4172       WritingModule(nullptr), WritingAST(false),
4173       DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4174       FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4175       FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4176       FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4177       FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4178       FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4179       NextSubmoduleID(FirstSubmoduleID),
4180       FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4181       CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4182       NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4183       NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
4184       TypeExtQualAbbrev(0),
4185       TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4186       DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
4187       UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
4188       DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
4189       DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4190       CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4191       ExprImplicitCastAbbrev(0) {}
4192 
4193 ASTWriter::~ASTWriter() {
4194   llvm::DeleteContainerSeconds(FileDeclIDs);
4195 }
4196 
4197 const LangOptions &ASTWriter::getLangOpts() const {
4198   assert(WritingAST && "can't determine lang opts when not writing AST");
4199   return Context->getLangOpts();
4200 }
4201 
4202 void ASTWriter::WriteAST(Sema &SemaRef,
4203                          const std::string &OutputFile,
4204                          Module *WritingModule, StringRef isysroot,
4205                          bool hasErrors) {
4206   WritingAST = true;
4207 
4208   ASTHasCompilerErrors = hasErrors;
4209 
4210   // Emit the file header.
4211   Stream.Emit((unsigned)'C', 8);
4212   Stream.Emit((unsigned)'P', 8);
4213   Stream.Emit((unsigned)'C', 8);
4214   Stream.Emit((unsigned)'H', 8);
4215 
4216   WriteBlockInfoBlock();
4217 
4218   Context = &SemaRef.Context;
4219   PP = &SemaRef.PP;
4220   this->WritingModule = WritingModule;
4221   WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
4222   Context = nullptr;
4223   PP = nullptr;
4224   this->WritingModule = nullptr;
4225   this->BaseDirectory.clear();
4226 
4227   WritingAST = false;
4228 }
4229 
4230 template<typename Vector>
4231 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4232                                ASTWriter::RecordData &Record) {
4233   for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4234        I != E; ++I) {
4235     Writer.AddDeclRef(*I, Record);
4236   }
4237 }
4238 
4239 void ASTWriter::WriteASTCore(Sema &SemaRef,
4240                              StringRef isysroot,
4241                              const std::string &OutputFile,
4242                              Module *WritingModule) {
4243   using namespace llvm;
4244 
4245   bool isModule = WritingModule != nullptr;
4246 
4247   // Make sure that the AST reader knows to finalize itself.
4248   if (Chain)
4249     Chain->finalizeForWriting();
4250 
4251   ASTContext &Context = SemaRef.Context;
4252   Preprocessor &PP = SemaRef.PP;
4253 
4254   // Set up predefined declaration IDs.
4255   DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
4256   if (Context.ObjCIdDecl)
4257     DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
4258   if (Context.ObjCSelDecl)
4259     DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
4260   if (Context.ObjCClassDecl)
4261     DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
4262   if (Context.ObjCProtocolClassDecl)
4263     DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
4264   if (Context.Int128Decl)
4265     DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4266   if (Context.UInt128Decl)
4267     DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
4268   if (Context.ObjCInstanceTypeDecl)
4269     DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
4270   if (Context.BuiltinVaListDecl)
4271     DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4272   if (Context.ExternCContext)
4273     DeclIDs[Context.ExternCContext] = PREDEF_DECL_EXTERN_C_CONTEXT_ID;
4274 
4275   if (!Chain) {
4276     // Make sure that we emit IdentifierInfos (and any attached
4277     // declarations) for builtins. We don't need to do this when we're
4278     // emitting chained PCH files, because all of the builtins will be
4279     // in the original PCH file.
4280     // FIXME: Modules won't like this at all.
4281     IdentifierTable &Table = PP.getIdentifierTable();
4282     SmallVector<const char *, 32> BuiltinNames;
4283     if (!Context.getLangOpts().NoBuiltin) {
4284       Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4285     }
4286     for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4287       getIdentifierRef(&Table.get(BuiltinNames[I]));
4288   }
4289 
4290   // If we saw any DeclContext updates before we started writing the AST file,
4291   // make sure all visible decls in those DeclContexts are written out.
4292   if (!UpdatedDeclContexts.empty()) {
4293     auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4294     UpdatedDeclContexts.clear();
4295     for (auto *DC : OldUpdatedDeclContexts)
4296       AddUpdatedDeclContext(DC);
4297   }
4298 
4299   // Build a record containing all of the tentative definitions in this file, in
4300   // TentativeDefinitions order.  Generally, this record will be empty for
4301   // headers.
4302   RecordData TentativeDefinitions;
4303   AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4304 
4305   // Build a record containing all of the file scoped decls in this file.
4306   RecordData UnusedFileScopedDecls;
4307   if (!isModule)
4308     AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4309                        UnusedFileScopedDecls);
4310 
4311   // Build a record containing all of the delegating constructors we still need
4312   // to resolve.
4313   RecordData DelegatingCtorDecls;
4314   if (!isModule)
4315     AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4316 
4317   // Write the set of weak, undeclared identifiers. We always write the
4318   // entire table, since later PCH files in a PCH chain are only interested in
4319   // the results at the end of the chain.
4320   RecordData WeakUndeclaredIdentifiers;
4321   if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
4322     for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
4323          I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4324          E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4325       AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4326       AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4327       AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4328       WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4329     }
4330   }
4331 
4332   // Build a record containing all of the ext_vector declarations.
4333   RecordData ExtVectorDecls;
4334   AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4335 
4336   // Build a record containing all of the VTable uses information.
4337   RecordData VTableUses;
4338   if (!SemaRef.VTableUses.empty()) {
4339     for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4340       AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4341       AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4342       VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4343     }
4344   }
4345 
4346   // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4347   RecordData UnusedLocalTypedefNameCandidates;
4348   for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4349     AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4350 
4351   // Build a record containing all of pending implicit instantiations.
4352   RecordData PendingInstantiations;
4353   for (std::deque<Sema::PendingImplicitInstantiation>::iterator
4354          I = SemaRef.PendingInstantiations.begin(),
4355          N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4356     AddDeclRef(I->first, PendingInstantiations);
4357     AddSourceLocation(I->second, PendingInstantiations);
4358   }
4359   assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4360          "There are local ones at end of translation unit!");
4361 
4362   // Build a record containing some declaration references.
4363   RecordData SemaDeclRefs;
4364   if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4365     AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4366     AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4367   }
4368 
4369   RecordData CUDASpecialDeclRefs;
4370   if (Context.getcudaConfigureCallDecl()) {
4371     AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4372   }
4373 
4374   // Build a record containing all of the known namespaces.
4375   RecordData KnownNamespaces;
4376   for (llvm::MapVector<NamespaceDecl*, bool>::iterator
4377             I = SemaRef.KnownNamespaces.begin(),
4378          IEnd = SemaRef.KnownNamespaces.end();
4379        I != IEnd; ++I) {
4380     if (!I->second)
4381       AddDeclRef(I->first, KnownNamespaces);
4382   }
4383 
4384   // Build a record of all used, undefined objects that require definitions.
4385   RecordData UndefinedButUsed;
4386 
4387   SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4388   SemaRef.getUndefinedButUsed(Undefined);
4389   for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4390          I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
4391     AddDeclRef(I->first, UndefinedButUsed);
4392     AddSourceLocation(I->second, UndefinedButUsed);
4393   }
4394 
4395   // Write the control block
4396   WriteControlBlock(PP, Context, isysroot, OutputFile);
4397 
4398   // Write the remaining AST contents.
4399   RecordData Record;
4400   Stream.EnterSubblock(AST_BLOCK_ID, 5);
4401 
4402   // This is so that older clang versions, before the introduction
4403   // of the control block, can read and reject the newer PCH format.
4404   Record.clear();
4405   Record.push_back(VERSION_MAJOR);
4406   Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4407 
4408   // Create a lexical update block containing all of the declarations in the
4409   // translation unit that do not come from other AST files.
4410   const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4411   SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4412   for (const auto *I : TU->noload_decls()) {
4413     if (!I->isFromASTFile())
4414       NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
4415   }
4416 
4417   llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4418   Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4419   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4420   unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4421   Record.clear();
4422   Record.push_back(TU_UPDATE_LEXICAL);
4423   Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4424                             data(NewGlobalDecls));
4425 
4426   // And a visible updates block for the translation unit.
4427   Abv = new llvm::BitCodeAbbrev();
4428   Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4429   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4430   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4431   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4432   UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4433   WriteDeclContextVisibleUpdate(TU);
4434 
4435   // If we have any extern "C" names, write out a visible update for them.
4436   if (Context.ExternCContext)
4437     WriteDeclContextVisibleUpdate(Context.ExternCContext);
4438 
4439   // If the translation unit has an anonymous namespace, and we don't already
4440   // have an update block for it, write it as an update block.
4441   // FIXME: Why do we not do this if there's already an update block?
4442   if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4443     ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4444     if (Record.empty())
4445       Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4446   }
4447 
4448   // Add update records for all mangling numbers and static local numbers.
4449   // These aren't really update records, but this is a convenient way of
4450   // tagging this rare extra data onto the declarations.
4451   for (const auto &Number : Context.MangleNumbers)
4452     if (!Number.first->isFromASTFile())
4453       DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4454                                                      Number.second));
4455   for (const auto &Number : Context.StaticLocalNumbers)
4456     if (!Number.first->isFromASTFile())
4457       DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4458                                                      Number.second));
4459 
4460   // Make sure visible decls, added to DeclContexts previously loaded from
4461   // an AST file, are registered for serialization.
4462   for (SmallVectorImpl<const Decl *>::iterator
4463          I = UpdatingVisibleDecls.begin(),
4464          E = UpdatingVisibleDecls.end(); I != E; ++I) {
4465     GetDeclRef(*I);
4466   }
4467 
4468   // Make sure all decls associated with an identifier are registered for
4469   // serialization.
4470   llvm::SmallVector<const IdentifierInfo*, 256> IIsToVisit;
4471   for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4472                               IDEnd = PP.getIdentifierTable().end();
4473        ID != IDEnd; ++ID) {
4474     const IdentifierInfo *II = ID->second;
4475     if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4476       IIsToVisit.push_back(II);
4477   }
4478   for (const IdentifierInfo *II : IIsToVisit) {
4479     for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4480                                    DEnd = SemaRef.IdResolver.end();
4481          D != DEnd; ++D) {
4482       GetDeclRef(*D);
4483     }
4484   }
4485 
4486   // Form the record of special types.
4487   RecordData SpecialTypes;
4488   AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
4489   AddTypeRef(Context.getFILEType(), SpecialTypes);
4490   AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4491   AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4492   AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4493   AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
4494   AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
4495   AddTypeRef(Context.getucontext_tType(), SpecialTypes);
4496 
4497   if (Chain) {
4498     // Write the mapping information describing our module dependencies and how
4499     // each of those modules were mapped into our own offset/ID space, so that
4500     // the reader can build the appropriate mapping to its own offset/ID space.
4501     // The map consists solely of a blob with the following format:
4502     // *(module-name-len:i16 module-name:len*i8
4503     //   source-location-offset:i32
4504     //   identifier-id:i32
4505     //   preprocessed-entity-id:i32
4506     //   macro-definition-id:i32
4507     //   submodule-id:i32
4508     //   selector-id:i32
4509     //   declaration-id:i32
4510     //   c++-base-specifiers-id:i32
4511     //   type-id:i32)
4512     //
4513     llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4514     Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4515     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4516     unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
4517     SmallString<2048> Buffer;
4518     {
4519       llvm::raw_svector_ostream Out(Buffer);
4520       for (ModuleFile *M : Chain->ModuleMgr) {
4521         using namespace llvm::support;
4522         endian::Writer<little> LE(Out);
4523         StringRef FileName = M->FileName;
4524         LE.write<uint16_t>(FileName.size());
4525         Out.write(FileName.data(), FileName.size());
4526 
4527         // Note: if a base ID was uint max, it would not be possible to load
4528         // another module after it or have more than one entity inside it.
4529         uint32_t None = std::numeric_limits<uint32_t>::max();
4530 
4531         auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4532           assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4533           if (ShouldWrite)
4534             LE.write<uint32_t>(BaseID);
4535           else
4536             LE.write<uint32_t>(None);
4537         };
4538 
4539         // These values should be unique within a chain, since they will be read
4540         // as keys into ContinuousRangeMaps.
4541         writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4542         writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4543         writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4544         writeBaseIDOrNone(M->BasePreprocessedEntityID,
4545                           M->NumPreprocessedEntities);
4546         writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4547         writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4548         writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4549         writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
4550       }
4551     }
4552     Record.clear();
4553     Record.push_back(MODULE_OFFSET_MAP);
4554     Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4555                               Buffer.data(), Buffer.size());
4556   }
4557 
4558   RecordData DeclUpdatesOffsetsRecord;
4559 
4560   // Keep writing types, declarations, and declaration update records
4561   // until we've emitted all of them.
4562   Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4563   WriteTypeAbbrevs();
4564   WriteDeclAbbrevs();
4565   for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4566                                   E = DeclsToRewrite.end();
4567        I != E; ++I)
4568     DeclTypesToEmit.push(const_cast<Decl*>(*I));
4569   do {
4570     WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4571     while (!DeclTypesToEmit.empty()) {
4572       DeclOrType DOT = DeclTypesToEmit.front();
4573       DeclTypesToEmit.pop();
4574       if (DOT.isType())
4575         WriteType(DOT.getType());
4576       else
4577         WriteDecl(Context, DOT.getDecl());
4578     }
4579   } while (!DeclUpdates.empty());
4580   Stream.ExitBlock();
4581 
4582   DoneWritingDeclsAndTypes = true;
4583 
4584   // These things can only be done once we've written out decls and types.
4585   WriteTypeDeclOffsets();
4586   if (!DeclUpdatesOffsetsRecord.empty())
4587     Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4588   WriteCXXBaseSpecifiersOffsets();
4589   WriteCXXCtorInitializersOffsets();
4590   WriteFileDeclIDsMap();
4591   WriteSourceManagerBlock(Context.getSourceManager(), PP);
4592 
4593   WriteComments();
4594   WritePreprocessor(PP, isModule);
4595   WriteHeaderSearch(PP.getHeaderSearchInfo());
4596   WriteSelectors(SemaRef);
4597   WriteReferencedSelectorsPool(SemaRef);
4598   WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
4599   WriteFPPragmaOptions(SemaRef.getFPOptions());
4600   WriteOpenCLExtensions(SemaRef);
4601   WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
4602 
4603   // If we're emitting a module, write out the submodule information.
4604   if (WritingModule)
4605     WriteSubmodules(WritingModule);
4606 
4607   Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4608 
4609   // Write the record containing external, unnamed definitions.
4610   if (!EagerlyDeserializedDecls.empty())
4611     Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
4612 
4613   // Write the record containing tentative definitions.
4614   if (!TentativeDefinitions.empty())
4615     Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
4616 
4617   // Write the record containing unused file scoped decls.
4618   if (!UnusedFileScopedDecls.empty())
4619     Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
4620 
4621   // Write the record containing weak undeclared identifiers.
4622   if (!WeakUndeclaredIdentifiers.empty())
4623     Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
4624                       WeakUndeclaredIdentifiers);
4625 
4626   // Write the record containing ext_vector type names.
4627   if (!ExtVectorDecls.empty())
4628     Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
4629 
4630   // Write the record containing VTable uses information.
4631   if (!VTableUses.empty())
4632     Stream.EmitRecord(VTABLE_USES, VTableUses);
4633 
4634   // Write the record containing potentially unused local typedefs.
4635   if (!UnusedLocalTypedefNameCandidates.empty())
4636     Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4637                       UnusedLocalTypedefNameCandidates);
4638 
4639   // Write the record containing pending implicit instantiations.
4640   if (!PendingInstantiations.empty())
4641     Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
4642 
4643   // Write the record containing declaration references of Sema.
4644   if (!SemaDeclRefs.empty())
4645     Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
4646 
4647   // Write the record containing CUDA-specific declaration references.
4648   if (!CUDASpecialDeclRefs.empty())
4649     Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
4650 
4651   // Write the delegating constructors.
4652   if (!DelegatingCtorDecls.empty())
4653     Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
4654 
4655   // Write the known namespaces.
4656   if (!KnownNamespaces.empty())
4657     Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
4658 
4659   // Write the undefined internal functions and variables, and inline functions.
4660   if (!UndefinedButUsed.empty())
4661     Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
4662 
4663   // Write the visible updates to DeclContexts.
4664   for (auto *DC : UpdatedDeclContexts)
4665     WriteDeclContextVisibleUpdate(DC);
4666 
4667   if (!WritingModule) {
4668     // Write the submodules that were imported, if any.
4669     struct ModuleInfo {
4670       uint64_t ID;
4671       Module *M;
4672       ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4673     };
4674     llvm::SmallVector<ModuleInfo, 64> Imports;
4675     for (const auto *I : Context.local_imports()) {
4676       assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4677       Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4678                          I->getImportedModule()));
4679     }
4680 
4681     if (!Imports.empty()) {
4682       auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4683         return A.ID < B.ID;
4684       };
4685       auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4686         return A.ID == B.ID;
4687       };
4688 
4689       // Sort and deduplicate module IDs.
4690       std::sort(Imports.begin(), Imports.end(), Cmp);
4691       Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
4692                     Imports.end());
4693 
4694       RecordData ImportedModules;
4695       for (const auto &Import : Imports) {
4696         ImportedModules.push_back(Import.ID);
4697         // FIXME: If the module has macros imported then later has declarations
4698         // imported, this location won't be the right one as a location for the
4699         // declaration imports.
4700         AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4701       }
4702 
4703       Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4704     }
4705   }
4706 
4707   WriteDeclReplacementsBlock();
4708   WriteRedeclarations();
4709   WriteObjCCategories();
4710   WriteLateParsedTemplates(SemaRef);
4711   if(!WritingModule)
4712     WriteOptimizePragmaOptions(SemaRef);
4713 
4714   // Some simple statistics
4715   Record.clear();
4716   Record.push_back(NumStatements);
4717   Record.push_back(NumMacros);
4718   Record.push_back(NumLexicalDeclContexts);
4719   Record.push_back(NumVisibleDeclContexts);
4720   Stream.EmitRecord(STATISTICS, Record);
4721   Stream.ExitBlock();
4722 }
4723 
4724 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
4725   if (DeclUpdates.empty())
4726     return;
4727 
4728   DeclUpdateMap LocalUpdates;
4729   LocalUpdates.swap(DeclUpdates);
4730 
4731   for (auto &DeclUpdate : LocalUpdates) {
4732     const Decl *D = DeclUpdate.first;
4733     if (isRewritten(D))
4734       continue; // The decl will be written completely,no need to store updates.
4735 
4736     bool HasUpdatedBody = false;
4737     RecordData Record;
4738     for (auto &Update : DeclUpdate.second) {
4739       DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4740 
4741       Record.push_back(Kind);
4742       switch (Kind) {
4743       case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4744       case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4745       case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4746         assert(Update.getDecl() && "no decl to add?");
4747         Record.push_back(GetDeclRef(Update.getDecl()));
4748         break;
4749 
4750       case UPD_CXX_ADDED_FUNCTION_DEFINITION:
4751         // An updated body is emitted last, so that the reader doesn't need
4752         // to skip over the lazy body to reach statements for other records.
4753         Record.pop_back();
4754         HasUpdatedBody = true;
4755         break;
4756 
4757       case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4758         AddSourceLocation(Update.getLoc(), Record);
4759         break;
4760 
4761       case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4762         auto *RD = cast<CXXRecordDecl>(D);
4763         AddUpdatedDeclContext(RD->getPrimaryContext());
4764         AddCXXDefinitionData(RD, Record);
4765         Record.push_back(WriteDeclContextLexicalBlock(
4766             *Context, const_cast<CXXRecordDecl *>(RD)));
4767 
4768         // This state is sometimes updated by template instantiation, when we
4769         // switch from the specialization referring to the template declaration
4770         // to it referring to the template definition.
4771         if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4772           Record.push_back(MSInfo->getTemplateSpecializationKind());
4773           AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4774         } else {
4775           auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4776           Record.push_back(Spec->getTemplateSpecializationKind());
4777           AddSourceLocation(Spec->getPointOfInstantiation(), Record);
4778 
4779           // The instantiation might have been resolved to a partial
4780           // specialization. If so, record which one.
4781           auto From = Spec->getInstantiatedFrom();
4782           if (auto PartialSpec =
4783                 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4784             Record.push_back(true);
4785             AddDeclRef(PartialSpec, Record);
4786             AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4787                                     Record);
4788           } else {
4789             Record.push_back(false);
4790           }
4791         }
4792         Record.push_back(RD->getTagKind());
4793         AddSourceLocation(RD->getLocation(), Record);
4794         AddSourceLocation(RD->getLocStart(), Record);
4795         AddSourceLocation(RD->getRBraceLoc(), Record);
4796 
4797         // Instantiation may change attributes; write them all out afresh.
4798         Record.push_back(D->hasAttrs());
4799         if (Record.back())
4800           WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4801                                              D->getAttrs().size()), Record);
4802 
4803         // FIXME: Ensure we don't get here for explicit instantiations.
4804         break;
4805       }
4806 
4807       case UPD_CXX_RESOLVED_DTOR_DELETE:
4808         AddDeclRef(Update.getDecl(), Record);
4809         break;
4810 
4811       case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4812         addExceptionSpec(
4813             *this,
4814             cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4815             Record);
4816         break;
4817 
4818       case UPD_CXX_DEDUCED_RETURN_TYPE:
4819         Record.push_back(GetOrCreateTypeID(Update.getType()));
4820         break;
4821 
4822       case UPD_DECL_MARKED_USED:
4823         break;
4824 
4825       case UPD_MANGLING_NUMBER:
4826       case UPD_STATIC_LOCAL_NUMBER:
4827         Record.push_back(Update.getNumber());
4828         break;
4829       case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4830         AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4831                        Record);
4832         break;
4833       }
4834     }
4835 
4836     if (HasUpdatedBody) {
4837       const FunctionDecl *Def = cast<FunctionDecl>(D);
4838       Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
4839       Record.push_back(Def->isInlined());
4840       AddSourceLocation(Def->getInnerLocStart(), Record);
4841       AddFunctionDefinition(Def, Record);
4842     }
4843 
4844     OffsetsRecord.push_back(GetDeclRef(D));
4845     OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4846 
4847     Stream.EmitRecord(DECL_UPDATES, Record);
4848 
4849     FlushPendingAfterDecl();
4850   }
4851 }
4852 
4853 void ASTWriter::WriteDeclReplacementsBlock() {
4854   if (ReplacedDecls.empty())
4855     return;
4856 
4857   RecordData Record;
4858   for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4859          I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
4860     Record.push_back(I->ID);
4861     Record.push_back(I->Offset);
4862     Record.push_back(I->Loc);
4863   }
4864   Stream.EmitRecord(DECL_REPLACEMENTS, Record);
4865 }
4866 
4867 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
4868   Record.push_back(Loc.getRawEncoding());
4869 }
4870 
4871 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
4872   AddSourceLocation(Range.getBegin(), Record);
4873   AddSourceLocation(Range.getEnd(), Record);
4874 }
4875 
4876 void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
4877   Record.push_back(Value.getBitWidth());
4878   const uint64_t *Words = Value.getRawData();
4879   Record.append(Words, Words + Value.getNumWords());
4880 }
4881 
4882 void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
4883   Record.push_back(Value.isUnsigned());
4884   AddAPInt(Value, Record);
4885 }
4886 
4887 void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
4888   AddAPInt(Value.bitcastToAPInt(), Record);
4889 }
4890 
4891 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
4892   Record.push_back(getIdentifierRef(II));
4893 }
4894 
4895 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
4896   if (!II)
4897     return 0;
4898 
4899   IdentID &ID = IdentifierIDs[II];
4900   if (ID == 0)
4901     ID = NextIdentID++;
4902   return ID;
4903 }
4904 
4905 MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
4906   // Don't emit builtin macros like __LINE__ to the AST file unless they
4907   // have been redefined by the header (in which case they are not
4908   // isBuiltinMacro).
4909   if (!MI || MI->isBuiltinMacro())
4910     return 0;
4911 
4912   MacroID &ID = MacroIDs[MI];
4913   if (ID == 0) {
4914     ID = NextMacroID++;
4915     MacroInfoToEmitData Info = { Name, MI, ID };
4916     MacroInfosToEmit.push_back(Info);
4917   }
4918   return ID;
4919 }
4920 
4921 MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4922   if (!MI || MI->isBuiltinMacro())
4923     return 0;
4924 
4925   assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4926   return MacroIDs[MI];
4927 }
4928 
4929 uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4930   assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4931   return IdentMacroDirectivesOffsetMap[Name];
4932 }
4933 
4934 void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
4935   Record.push_back(getSelectorRef(SelRef));
4936 }
4937 
4938 SelectorID ASTWriter::getSelectorRef(Selector Sel) {
4939   if (Sel.getAsOpaquePtr() == nullptr) {
4940     return 0;
4941   }
4942 
4943   SelectorID SID = SelectorIDs[Sel];
4944   if (SID == 0 && Chain) {
4945     // This might trigger a ReadSelector callback, which will set the ID for
4946     // this selector.
4947     Chain->LoadSelector(Sel);
4948     SID = SelectorIDs[Sel];
4949   }
4950   if (SID == 0) {
4951     SID = NextSelectorID++;
4952     SelectorIDs[Sel] = SID;
4953   }
4954   return SID;
4955 }
4956 
4957 void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
4958   AddDeclRef(Temp->getDestructor(), Record);
4959 }
4960 
4961 void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4962                                           RecordDataImpl &Record) {
4963   assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4964   CXXCtorInitializersToWrite.push_back(
4965       QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4966   Record.push_back(NextCXXCtorInitializersID++);
4967 }
4968 
4969 void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4970                                         CXXBaseSpecifier const *BasesEnd,
4971                                         RecordDataImpl &Record) {
4972   assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4973   CXXBaseSpecifiersToWrite.push_back(
4974                                 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4975                                                         Bases, BasesEnd));
4976   Record.push_back(NextCXXBaseSpecifiersID++);
4977 }
4978 
4979 void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
4980                                            const TemplateArgumentLocInfo &Arg,
4981                                            RecordDataImpl &Record) {
4982   switch (Kind) {
4983   case TemplateArgument::Expression:
4984     AddStmt(Arg.getAsExpr());
4985     break;
4986   case TemplateArgument::Type:
4987     AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
4988     break;
4989   case TemplateArgument::Template:
4990     AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
4991     AddSourceLocation(Arg.getTemplateNameLoc(), Record);
4992     break;
4993   case TemplateArgument::TemplateExpansion:
4994     AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
4995     AddSourceLocation(Arg.getTemplateNameLoc(), Record);
4996     AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
4997     break;
4998   case TemplateArgument::Null:
4999   case TemplateArgument::Integral:
5000   case TemplateArgument::Declaration:
5001   case TemplateArgument::NullPtr:
5002   case TemplateArgument::Pack:
5003     // FIXME: Is this right?
5004     break;
5005   }
5006 }
5007 
5008 void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
5009                                        RecordDataImpl &Record) {
5010   AddTemplateArgument(Arg.getArgument(), Record);
5011 
5012   if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5013     bool InfoHasSameExpr
5014       = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5015     Record.push_back(InfoHasSameExpr);
5016     if (InfoHasSameExpr)
5017       return; // Avoid storing the same expr twice.
5018   }
5019   AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
5020                              Record);
5021 }
5022 
5023 void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
5024                                   RecordDataImpl &Record) {
5025   if (!TInfo) {
5026     AddTypeRef(QualType(), Record);
5027     return;
5028   }
5029 
5030   AddTypeLoc(TInfo->getTypeLoc(), Record);
5031 }
5032 
5033 void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
5034   AddTypeRef(TL.getType(), Record);
5035 
5036   TypeLocWriter TLW(*this, Record);
5037   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
5038     TLW.Visit(TL);
5039 }
5040 
5041 void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
5042   Record.push_back(GetOrCreateTypeID(T));
5043 }
5044 
5045 TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
5046   assert(Context);
5047   return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5048     if (T.isNull())
5049       return TypeIdx();
5050     assert(!T.getLocalFastQualifiers());
5051 
5052     TypeIdx &Idx = TypeIdxs[T];
5053     if (Idx.getIndex() == 0) {
5054       if (DoneWritingDeclsAndTypes) {
5055         assert(0 && "New type seen after serializing all the types to emit!");
5056         return TypeIdx();
5057       }
5058 
5059       // We haven't seen this type before. Assign it a new ID and put it
5060       // into the queue of types to emit.
5061       Idx = TypeIdx(NextTypeID++);
5062       DeclTypesToEmit.push(T);
5063     }
5064     return Idx;
5065   });
5066 }
5067 
5068 TypeID ASTWriter::getTypeID(QualType T) const {
5069   assert(Context);
5070   return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5071     if (T.isNull())
5072       return TypeIdx();
5073     assert(!T.getLocalFastQualifiers());
5074 
5075     TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5076     assert(I != TypeIdxs.end() && "Type not emitted!");
5077     return I->second;
5078   });
5079 }
5080 
5081 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
5082   Record.push_back(GetDeclRef(D));
5083 }
5084 
5085 DeclID ASTWriter::GetDeclRef(const Decl *D) {
5086   assert(WritingAST && "Cannot request a declaration ID before AST writing");
5087 
5088   if (!D) {
5089     return 0;
5090   }
5091 
5092   // If D comes from an AST file, its declaration ID is already known and
5093   // fixed.
5094   if (D->isFromASTFile())
5095     return D->getGlobalID();
5096 
5097   assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
5098   DeclID &ID = DeclIDs[D];
5099   if (ID == 0) {
5100     if (DoneWritingDeclsAndTypes) {
5101       assert(0 && "New decl seen after serializing all the decls to emit!");
5102       return 0;
5103     }
5104 
5105     // We haven't seen this declaration before. Give it a new ID and
5106     // enqueue it in the list of declarations to emit.
5107     ID = NextDeclID++;
5108     DeclTypesToEmit.push(const_cast<Decl *>(D));
5109   }
5110 
5111   return ID;
5112 }
5113 
5114 DeclID ASTWriter::getDeclID(const Decl *D) {
5115   if (!D)
5116     return 0;
5117 
5118   // If D comes from an AST file, its declaration ID is already known and
5119   // fixed.
5120   if (D->isFromASTFile())
5121     return D->getGlobalID();
5122 
5123   assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5124   return DeclIDs[D];
5125 }
5126 
5127 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5128   assert(ID);
5129   assert(D);
5130 
5131   SourceLocation Loc = D->getLocation();
5132   if (Loc.isInvalid())
5133     return;
5134 
5135   // We only keep track of the file-level declarations of each file.
5136   if (!D->getLexicalDeclContext()->isFileContext())
5137     return;
5138   // FIXME: ParmVarDecls that are part of a function type of a parameter of
5139   // a function/objc method, should not have TU as lexical context.
5140   if (isa<ParmVarDecl>(D))
5141     return;
5142 
5143   SourceManager &SM = Context->getSourceManager();
5144   SourceLocation FileLoc = SM.getFileLoc(Loc);
5145   assert(SM.isLocalSourceLocation(FileLoc));
5146   FileID FID;
5147   unsigned Offset;
5148   std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5149   if (FID.isInvalid())
5150     return;
5151   assert(SM.getSLocEntry(FID).isFile());
5152 
5153   DeclIDInFileInfo *&Info = FileDeclIDs[FID];
5154   if (!Info)
5155     Info = new DeclIDInFileInfo();
5156 
5157   std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5158   LocDeclIDsTy &Decls = Info->DeclIDs;
5159 
5160   if (Decls.empty() || Decls.back().first <= Offset) {
5161     Decls.push_back(LocDecl);
5162     return;
5163   }
5164 
5165   LocDeclIDsTy::iterator I =
5166       std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
5167 
5168   Decls.insert(I, LocDecl);
5169 }
5170 
5171 void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
5172   // FIXME: Emit a stable enum for NameKind.  0 = Identifier etc.
5173   Record.push_back(Name.getNameKind());
5174   switch (Name.getNameKind()) {
5175   case DeclarationName::Identifier:
5176     AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5177     break;
5178 
5179   case DeclarationName::ObjCZeroArgSelector:
5180   case DeclarationName::ObjCOneArgSelector:
5181   case DeclarationName::ObjCMultiArgSelector:
5182     AddSelectorRef(Name.getObjCSelector(), Record);
5183     break;
5184 
5185   case DeclarationName::CXXConstructorName:
5186   case DeclarationName::CXXDestructorName:
5187   case DeclarationName::CXXConversionFunctionName:
5188     AddTypeRef(Name.getCXXNameType(), Record);
5189     break;
5190 
5191   case DeclarationName::CXXOperatorName:
5192     Record.push_back(Name.getCXXOverloadedOperator());
5193     break;
5194 
5195   case DeclarationName::CXXLiteralOperatorName:
5196     AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5197     break;
5198 
5199   case DeclarationName::CXXUsingDirective:
5200     // No extra data to emit
5201     break;
5202   }
5203 }
5204 
5205 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5206   assert(needsAnonymousDeclarationNumber(D) &&
5207          "expected an anonymous declaration");
5208 
5209   // Number the anonymous declarations within this context, if we've not
5210   // already done so.
5211   auto It = AnonymousDeclarationNumbers.find(D);
5212   if (It == AnonymousDeclarationNumbers.end()) {
5213     auto *DC = D->getLexicalDeclContext();
5214     numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5215       AnonymousDeclarationNumbers[ND] = Number;
5216     });
5217 
5218     It = AnonymousDeclarationNumbers.find(D);
5219     assert(It != AnonymousDeclarationNumbers.end() &&
5220            "declaration not found within its lexical context");
5221   }
5222 
5223   return It->second;
5224 }
5225 
5226 void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5227                                      DeclarationName Name, RecordDataImpl &Record) {
5228   switch (Name.getNameKind()) {
5229   case DeclarationName::CXXConstructorName:
5230   case DeclarationName::CXXDestructorName:
5231   case DeclarationName::CXXConversionFunctionName:
5232     AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5233     break;
5234 
5235   case DeclarationName::CXXOperatorName:
5236     AddSourceLocation(
5237        SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5238        Record);
5239     AddSourceLocation(
5240         SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5241         Record);
5242     break;
5243 
5244   case DeclarationName::CXXLiteralOperatorName:
5245     AddSourceLocation(
5246      SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5247      Record);
5248     break;
5249 
5250   case DeclarationName::Identifier:
5251   case DeclarationName::ObjCZeroArgSelector:
5252   case DeclarationName::ObjCOneArgSelector:
5253   case DeclarationName::ObjCMultiArgSelector:
5254   case DeclarationName::CXXUsingDirective:
5255     break;
5256   }
5257 }
5258 
5259 void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
5260                                        RecordDataImpl &Record) {
5261   AddDeclarationName(NameInfo.getName(), Record);
5262   AddSourceLocation(NameInfo.getLoc(), Record);
5263   AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5264 }
5265 
5266 void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
5267                                  RecordDataImpl &Record) {
5268   AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
5269   Record.push_back(Info.NumTemplParamLists);
5270   for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5271     AddTemplateParameterList(Info.TemplParamLists[i], Record);
5272 }
5273 
5274 void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
5275                                        RecordDataImpl &Record) {
5276   // Nested name specifiers usually aren't too long. I think that 8 would
5277   // typically accommodate the vast majority.
5278   SmallVector<NestedNameSpecifier *, 8> NestedNames;
5279 
5280   // Push each of the NNS's onto a stack for serialization in reverse order.
5281   while (NNS) {
5282     NestedNames.push_back(NNS);
5283     NNS = NNS->getPrefix();
5284   }
5285 
5286   Record.push_back(NestedNames.size());
5287   while(!NestedNames.empty()) {
5288     NNS = NestedNames.pop_back_val();
5289     NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5290     Record.push_back(Kind);
5291     switch (Kind) {
5292     case NestedNameSpecifier::Identifier:
5293       AddIdentifierRef(NNS->getAsIdentifier(), Record);
5294       break;
5295 
5296     case NestedNameSpecifier::Namespace:
5297       AddDeclRef(NNS->getAsNamespace(), Record);
5298       break;
5299 
5300     case NestedNameSpecifier::NamespaceAlias:
5301       AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5302       break;
5303 
5304     case NestedNameSpecifier::TypeSpec:
5305     case NestedNameSpecifier::TypeSpecWithTemplate:
5306       AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5307       Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5308       break;
5309 
5310     case NestedNameSpecifier::Global:
5311       // Don't need to write an associated value.
5312       break;
5313 
5314     case NestedNameSpecifier::Super:
5315       AddDeclRef(NNS->getAsRecordDecl(), Record);
5316       break;
5317     }
5318   }
5319 }
5320 
5321 void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5322                                           RecordDataImpl &Record) {
5323   // Nested name specifiers usually aren't too long. I think that 8 would
5324   // typically accommodate the vast majority.
5325   SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5326 
5327   // Push each of the nested-name-specifiers's onto a stack for
5328   // serialization in reverse order.
5329   while (NNS) {
5330     NestedNames.push_back(NNS);
5331     NNS = NNS.getPrefix();
5332   }
5333 
5334   Record.push_back(NestedNames.size());
5335   while(!NestedNames.empty()) {
5336     NNS = NestedNames.pop_back_val();
5337     NestedNameSpecifier::SpecifierKind Kind
5338       = NNS.getNestedNameSpecifier()->getKind();
5339     Record.push_back(Kind);
5340     switch (Kind) {
5341     case NestedNameSpecifier::Identifier:
5342       AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5343       AddSourceRange(NNS.getLocalSourceRange(), Record);
5344       break;
5345 
5346     case NestedNameSpecifier::Namespace:
5347       AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5348       AddSourceRange(NNS.getLocalSourceRange(), Record);
5349       break;
5350 
5351     case NestedNameSpecifier::NamespaceAlias:
5352       AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5353       AddSourceRange(NNS.getLocalSourceRange(), Record);
5354       break;
5355 
5356     case NestedNameSpecifier::TypeSpec:
5357     case NestedNameSpecifier::TypeSpecWithTemplate:
5358       Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5359       AddTypeLoc(NNS.getTypeLoc(), Record);
5360       AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5361       break;
5362 
5363     case NestedNameSpecifier::Global:
5364       AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5365       break;
5366 
5367     case NestedNameSpecifier::Super:
5368       AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5369       AddSourceRange(NNS.getLocalSourceRange(), Record);
5370       break;
5371     }
5372   }
5373 }
5374 
5375 void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
5376   TemplateName::NameKind Kind = Name.getKind();
5377   Record.push_back(Kind);
5378   switch (Kind) {
5379   case TemplateName::Template:
5380     AddDeclRef(Name.getAsTemplateDecl(), Record);
5381     break;
5382 
5383   case TemplateName::OverloadedTemplate: {
5384     OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5385     Record.push_back(OvT->size());
5386     for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5387            I != E; ++I)
5388       AddDeclRef(*I, Record);
5389     break;
5390   }
5391 
5392   case TemplateName::QualifiedTemplate: {
5393     QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5394     AddNestedNameSpecifier(QualT->getQualifier(), Record);
5395     Record.push_back(QualT->hasTemplateKeyword());
5396     AddDeclRef(QualT->getTemplateDecl(), Record);
5397     break;
5398   }
5399 
5400   case TemplateName::DependentTemplate: {
5401     DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5402     AddNestedNameSpecifier(DepT->getQualifier(), Record);
5403     Record.push_back(DepT->isIdentifier());
5404     if (DepT->isIdentifier())
5405       AddIdentifierRef(DepT->getIdentifier(), Record);
5406     else
5407       Record.push_back(DepT->getOperator());
5408     break;
5409   }
5410 
5411   case TemplateName::SubstTemplateTemplateParm: {
5412     SubstTemplateTemplateParmStorage *subst
5413       = Name.getAsSubstTemplateTemplateParm();
5414     AddDeclRef(subst->getParameter(), Record);
5415     AddTemplateName(subst->getReplacement(), Record);
5416     break;
5417   }
5418 
5419   case TemplateName::SubstTemplateTemplateParmPack: {
5420     SubstTemplateTemplateParmPackStorage *SubstPack
5421       = Name.getAsSubstTemplateTemplateParmPack();
5422     AddDeclRef(SubstPack->getParameterPack(), Record);
5423     AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5424     break;
5425   }
5426   }
5427 }
5428 
5429 void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
5430                                     RecordDataImpl &Record) {
5431   Record.push_back(Arg.getKind());
5432   switch (Arg.getKind()) {
5433   case TemplateArgument::Null:
5434     break;
5435   case TemplateArgument::Type:
5436     AddTypeRef(Arg.getAsType(), Record);
5437     break;
5438   case TemplateArgument::Declaration:
5439     AddDeclRef(Arg.getAsDecl(), Record);
5440     AddTypeRef(Arg.getParamTypeForDecl(), Record);
5441     break;
5442   case TemplateArgument::NullPtr:
5443     AddTypeRef(Arg.getNullPtrType(), Record);
5444     break;
5445   case TemplateArgument::Integral:
5446     AddAPSInt(Arg.getAsIntegral(), Record);
5447     AddTypeRef(Arg.getIntegralType(), Record);
5448     break;
5449   case TemplateArgument::Template:
5450     AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5451     break;
5452   case TemplateArgument::TemplateExpansion:
5453     AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5454     if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
5455       Record.push_back(*NumExpansions + 1);
5456     else
5457       Record.push_back(0);
5458     break;
5459   case TemplateArgument::Expression:
5460     AddStmt(Arg.getAsExpr());
5461     break;
5462   case TemplateArgument::Pack:
5463     Record.push_back(Arg.pack_size());
5464     for (const auto &P : Arg.pack_elements())
5465       AddTemplateArgument(P, Record);
5466     break;
5467   }
5468 }
5469 
5470 void
5471 ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
5472                                     RecordDataImpl &Record) {
5473   assert(TemplateParams && "No TemplateParams!");
5474   AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5475   AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5476   AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5477   Record.push_back(TemplateParams->size());
5478   for (TemplateParameterList::const_iterator
5479          P = TemplateParams->begin(), PEnd = TemplateParams->end();
5480          P != PEnd; ++P)
5481     AddDeclRef(*P, Record);
5482 }
5483 
5484 /// \brief Emit a template argument list.
5485 void
5486 ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
5487                                    RecordDataImpl &Record) {
5488   assert(TemplateArgs && "No TemplateArgs!");
5489   Record.push_back(TemplateArgs->size());
5490   for (int i=0, e = TemplateArgs->size(); i != e; ++i)
5491     AddTemplateArgument(TemplateArgs->get(i), Record);
5492 }
5493 
5494 void
5495 ASTWriter::AddASTTemplateArgumentListInfo
5496 (const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5497   assert(ASTTemplArgList && "No ASTTemplArgList!");
5498   AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5499   AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5500   Record.push_back(ASTTemplArgList->NumTemplateArgs);
5501   const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5502   for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5503     AddTemplateArgumentLoc(TemplArgs[i], Record);
5504 }
5505 
5506 void
5507 ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
5508   Record.push_back(Set.size());
5509   for (ASTUnresolvedSet::const_iterator
5510          I = Set.begin(), E = Set.end(); I != E; ++I) {
5511     AddDeclRef(I.getDecl(), Record);
5512     Record.push_back(I.getAccess());
5513   }
5514 }
5515 
5516 void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
5517                                     RecordDataImpl &Record) {
5518   Record.push_back(Base.isVirtual());
5519   Record.push_back(Base.isBaseOfClass());
5520   Record.push_back(Base.getAccessSpecifierAsWritten());
5521   Record.push_back(Base.getInheritConstructors());
5522   AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
5523   AddSourceRange(Base.getSourceRange(), Record);
5524   AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5525                                           : SourceLocation(),
5526                     Record);
5527 }
5528 
5529 void ASTWriter::FlushCXXBaseSpecifiers() {
5530   RecordData Record;
5531   unsigned N = CXXBaseSpecifiersToWrite.size();
5532   for (unsigned I = 0; I != N; ++I) {
5533     Record.clear();
5534 
5535     // Record the offset of this base-specifier set.
5536     unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
5537     if (Index == CXXBaseSpecifiersOffsets.size())
5538       CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5539     else {
5540       if (Index > CXXBaseSpecifiersOffsets.size())
5541         CXXBaseSpecifiersOffsets.resize(Index + 1);
5542       CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5543     }
5544 
5545     const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5546                         *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5547     Record.push_back(BEnd - B);
5548     for (; B != BEnd; ++B)
5549       AddCXXBaseSpecifier(*B, Record);
5550     Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
5551 
5552     // Flush any expressions that were written as part of the base specifiers.
5553     FlushStmts();
5554   }
5555 
5556   assert(N == CXXBaseSpecifiersToWrite.size() &&
5557          "added more base specifiers while writing base specifiers");
5558   CXXBaseSpecifiersToWrite.clear();
5559 }
5560 
5561 void ASTWriter::AddCXXCtorInitializers(
5562                              const CXXCtorInitializer * const *CtorInitializers,
5563                              unsigned NumCtorInitializers,
5564                              RecordDataImpl &Record) {
5565   Record.push_back(NumCtorInitializers);
5566   for (unsigned i=0; i != NumCtorInitializers; ++i) {
5567     const CXXCtorInitializer *Init = CtorInitializers[i];
5568 
5569     if (Init->isBaseInitializer()) {
5570       Record.push_back(CTOR_INITIALIZER_BASE);
5571       AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
5572       Record.push_back(Init->isBaseVirtual());
5573     } else if (Init->isDelegatingInitializer()) {
5574       Record.push_back(CTOR_INITIALIZER_DELEGATING);
5575       AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
5576     } else if (Init->isMemberInitializer()){
5577       Record.push_back(CTOR_INITIALIZER_MEMBER);
5578       AddDeclRef(Init->getMember(), Record);
5579     } else {
5580       Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5581       AddDeclRef(Init->getIndirectMember(), Record);
5582     }
5583 
5584     AddSourceLocation(Init->getMemberLocation(), Record);
5585     AddStmt(Init->getInit());
5586     AddSourceLocation(Init->getLParenLoc(), Record);
5587     AddSourceLocation(Init->getRParenLoc(), Record);
5588     Record.push_back(Init->isWritten());
5589     if (Init->isWritten()) {
5590       Record.push_back(Init->getSourceOrder());
5591     } else {
5592       Record.push_back(Init->getNumArrayIndices());
5593       for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5594         AddDeclRef(Init->getArrayIndex(i), Record);
5595     }
5596   }
5597 }
5598 
5599 void ASTWriter::FlushCXXCtorInitializers() {
5600   RecordData Record;
5601 
5602   unsigned N = CXXCtorInitializersToWrite.size();
5603   (void)N; // Silence unused warning in non-assert builds.
5604   for (auto &Init : CXXCtorInitializersToWrite) {
5605     Record.clear();
5606 
5607     // Record the offset of this mem-initializer list.
5608     unsigned Index = Init.ID - 1;
5609     if (Index == CXXCtorInitializersOffsets.size())
5610       CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5611     else {
5612       if (Index > CXXCtorInitializersOffsets.size())
5613         CXXCtorInitializersOffsets.resize(Index + 1);
5614       CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5615     }
5616 
5617     AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5618     Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5619 
5620     // Flush any expressions that were written as part of the initializers.
5621     FlushStmts();
5622   }
5623 
5624   assert(N == CXXCtorInitializersToWrite.size() &&
5625          "added more ctor initializers while writing ctor initializers");
5626   CXXCtorInitializersToWrite.clear();
5627 }
5628 
5629 void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5630   auto &Data = D->data();
5631   Record.push_back(Data.IsLambda);
5632   Record.push_back(Data.UserDeclaredConstructor);
5633   Record.push_back(Data.UserDeclaredSpecialMembers);
5634   Record.push_back(Data.Aggregate);
5635   Record.push_back(Data.PlainOldData);
5636   Record.push_back(Data.Empty);
5637   Record.push_back(Data.Polymorphic);
5638   Record.push_back(Data.Abstract);
5639   Record.push_back(Data.IsStandardLayout);
5640   Record.push_back(Data.HasNoNonEmptyBases);
5641   Record.push_back(Data.HasPrivateFields);
5642   Record.push_back(Data.HasProtectedFields);
5643   Record.push_back(Data.HasPublicFields);
5644   Record.push_back(Data.HasMutableFields);
5645   Record.push_back(Data.HasVariantMembers);
5646   Record.push_back(Data.HasOnlyCMembers);
5647   Record.push_back(Data.HasInClassInitializer);
5648   Record.push_back(Data.HasUninitializedReferenceMember);
5649   Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5650   Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5651   Record.push_back(Data.NeedOverloadResolutionForDestructor);
5652   Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5653   Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5654   Record.push_back(Data.DefaultedDestructorIsDeleted);
5655   Record.push_back(Data.HasTrivialSpecialMembers);
5656   Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
5657   Record.push_back(Data.HasIrrelevantDestructor);
5658   Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
5659   Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5660   Record.push_back(Data.HasConstexprDefaultConstructor);
5661   Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
5662   Record.push_back(Data.ComputedVisibleConversions);
5663   Record.push_back(Data.UserProvidedDefaultConstructor);
5664   Record.push_back(Data.DeclaredSpecialMembers);
5665   Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5666   Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5667   Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5668   Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
5669   // IsLambda bit is already saved.
5670 
5671   Record.push_back(Data.NumBases);
5672   if (Data.NumBases > 0)
5673     AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5674                             Record);
5675 
5676   // FIXME: Make VBases lazily computed when needed to avoid storing them.
5677   Record.push_back(Data.NumVBases);
5678   if (Data.NumVBases > 0)
5679     AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5680                             Record);
5681 
5682   AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5683   AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
5684   // Data.Definition is the owning decl, no need to write it.
5685   AddDeclRef(D->getFirstFriend(), Record);
5686 
5687   // Add lambda-specific data.
5688   if (Data.IsLambda) {
5689     auto &Lambda = D->getLambdaData();
5690     Record.push_back(Lambda.Dependent);
5691     Record.push_back(Lambda.IsGenericLambda);
5692     Record.push_back(Lambda.CaptureDefault);
5693     Record.push_back(Lambda.NumCaptures);
5694     Record.push_back(Lambda.NumExplicitCaptures);
5695     Record.push_back(Lambda.ManglingNumber);
5696     AddDeclRef(Lambda.ContextDecl, Record);
5697     AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
5698     for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5699       const LambdaCapture &Capture = Lambda.Captures[I];
5700       AddSourceLocation(Capture.getLocation(), Record);
5701       Record.push_back(Capture.isImplicit());
5702       Record.push_back(Capture.getCaptureKind());
5703       switch (Capture.getCaptureKind()) {
5704       case LCK_This:
5705       case LCK_VLAType:
5706         break;
5707       case LCK_ByCopy:
5708       case LCK_ByRef:
5709         VarDecl *Var =
5710             Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
5711         AddDeclRef(Var, Record);
5712         AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5713                                                     : SourceLocation(),
5714                           Record);
5715         break;
5716       }
5717     }
5718   }
5719 }
5720 
5721 void ASTWriter::ReaderInitialized(ASTReader *Reader) {
5722   assert(Reader && "Cannot remove chain");
5723   assert((!Chain || Chain == Reader) && "Cannot replace chain");
5724   assert(FirstDeclID == NextDeclID &&
5725          FirstTypeID == NextTypeID &&
5726          FirstIdentID == NextIdentID &&
5727          FirstMacroID == NextMacroID &&
5728          FirstSubmoduleID == NextSubmoduleID &&
5729          FirstSelectorID == NextSelectorID &&
5730          "Setting chain after writing has started.");
5731 
5732   Chain = Reader;
5733 
5734   // Note, this will get called multiple times, once one the reader starts up
5735   // and again each time it's done reading a PCH or module.
5736   FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5737   FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5738   FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
5739   FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
5740   FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
5741   FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
5742   NextDeclID = FirstDeclID;
5743   NextTypeID = FirstTypeID;
5744   NextIdentID = FirstIdentID;
5745   NextMacroID = FirstMacroID;
5746   NextSelectorID = FirstSelectorID;
5747   NextSubmoduleID = FirstSubmoduleID;
5748 }
5749 
5750 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
5751   // Always keep the highest ID. See \p TypeRead() for more information.
5752   IdentID &StoredID = IdentifierIDs[II];
5753   if (ID > StoredID)
5754     StoredID = ID;
5755 }
5756 
5757 void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
5758   // Always keep the highest ID. See \p TypeRead() for more information.
5759   MacroID &StoredID = MacroIDs[MI];
5760   if (ID > StoredID)
5761     StoredID = ID;
5762 }
5763 
5764 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
5765   // Always take the highest-numbered type index. This copes with an interesting
5766   // case for chained AST writing where we schedule writing the type and then,
5767   // later, deserialize the type from another AST. In this case, we want to
5768   // keep the higher-numbered entry so that we can properly write it out to
5769   // the AST file.
5770   TypeIdx &StoredIdx = TypeIdxs[T];
5771   if (Idx.getIndex() >= StoredIdx.getIndex())
5772     StoredIdx = Idx;
5773 }
5774 
5775 void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
5776   // Always keep the highest ID. See \p TypeRead() for more information.
5777   SelectorID &StoredID = SelectorIDs[S];
5778   if (ID > StoredID)
5779     StoredID = ID;
5780 }
5781 
5782 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
5783                                     MacroDefinition *MD) {
5784   assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
5785   MacroDefinitions[MD] = ID;
5786 }
5787 
5788 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5789   assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5790   SubmoduleIDs[Mod] = ID;
5791 }
5792 
5793 void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
5794   assert(D->isCompleteDefinition());
5795   assert(!WritingAST && "Already writing the AST!");
5796   if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5797     // We are interested when a PCH decl is modified.
5798     if (RD->isFromASTFile()) {
5799       // A forward reference was mutated into a definition. Rewrite it.
5800       // FIXME: This happens during template instantiation, should we
5801       // have created a new definition decl instead ?
5802       assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5803              "completed a tag from another module but not by instantiation?");
5804       DeclUpdates[RD].push_back(
5805           DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
5806     }
5807   }
5808 }
5809 
5810 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5811   // TU and namespaces are handled elsewhere.
5812   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5813     return;
5814 
5815   if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
5816     return; // Not a source decl added to a DeclContext from PCH.
5817 
5818   assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
5819   assert(!WritingAST && "Already writing the AST!");
5820   AddUpdatedDeclContext(DC);
5821   UpdatingVisibleDecls.push_back(D);
5822 }
5823 
5824 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5825   assert(D->isImplicit());
5826   if (!(!D->isFromASTFile() && RD->isFromASTFile()))
5827     return; // Not a source member added to a class from PCH.
5828   if (!isa<CXXMethodDecl>(D))
5829     return; // We are interested in lazily declared implicit methods.
5830 
5831   // A decl coming from PCH was modified.
5832   assert(RD->isCompleteDefinition());
5833   assert(!WritingAST && "Already writing the AST!");
5834   DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
5835 }
5836 
5837 void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5838                                      const ClassTemplateSpecializationDecl *D) {
5839   // The specializations set is kept in the canonical template.
5840   TD = TD->getCanonicalDecl();
5841   if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5842     return; // Not a source specialization added to a template from PCH.
5843 
5844   assert(!WritingAST && "Already writing the AST!");
5845   DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5846                                        D));
5847 }
5848 
5849 void ASTWriter::AddedCXXTemplateSpecialization(
5850     const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5851   // The specializations set is kept in the canonical template.
5852   TD = TD->getCanonicalDecl();
5853   if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5854     return; // Not a source specialization added to a template from PCH.
5855 
5856   assert(!WritingAST && "Already writing the AST!");
5857   DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5858                                        D));
5859 }
5860 
5861 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5862                                                const FunctionDecl *D) {
5863   // The specializations set is kept in the canonical template.
5864   TD = TD->getCanonicalDecl();
5865   if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5866     return; // Not a source specialization added to a template from PCH.
5867 
5868   assert(!WritingAST && "Already writing the AST!");
5869   DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5870                                        D));
5871 }
5872 
5873 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5874   assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5875   if (!Chain) return;
5876   Chain->forEachFormerlyCanonicalImportedDecl(FD, [&](const Decl *D) {
5877     // If we don't already know the exception specification for this redecl
5878     // chain, add an update record for it.
5879     if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5880                                       ->getType()
5881                                       ->castAs<FunctionProtoType>()
5882                                       ->getExceptionSpecType()))
5883       DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5884   });
5885 }
5886 
5887 void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5888   assert(!WritingAST && "Already writing the AST!");
5889   if (!Chain) return;
5890   Chain->forEachFormerlyCanonicalImportedDecl(FD, [&](const Decl *D) {
5891     DeclUpdates[D].push_back(
5892         DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5893   });
5894 }
5895 
5896 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5897                                        const FunctionDecl *Delete) {
5898   assert(!WritingAST && "Already writing the AST!");
5899   assert(Delete && "Not given an operator delete");
5900   if (!Chain) return;
5901   Chain->forEachFormerlyCanonicalImportedDecl(DD, [&](const Decl *D) {
5902     DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5903   });
5904 }
5905 
5906 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
5907   assert(!WritingAST && "Already writing the AST!");
5908   if (!D->isFromASTFile())
5909     return; // Declaration not imported from PCH.
5910 
5911   // Implicit function decl from a PCH was defined.
5912   DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
5913 }
5914 
5915 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5916   assert(!WritingAST && "Already writing the AST!");
5917   if (!D->isFromASTFile())
5918     return;
5919 
5920   DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
5921 }
5922 
5923 void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
5924   assert(!WritingAST && "Already writing the AST!");
5925   if (!D->isFromASTFile())
5926     return;
5927 
5928   // Since the actual instantiation is delayed, this really means that we need
5929   // to update the instantiation location.
5930   DeclUpdates[D].push_back(
5931       DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5932        D->getMemberSpecializationInfo()->getPointOfInstantiation()));
5933 }
5934 
5935 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5936                                              const ObjCInterfaceDecl *IFD) {
5937   assert(!WritingAST && "Already writing the AST!");
5938   if (!IFD->isFromASTFile())
5939     return; // Declaration not imported from PCH.
5940 
5941   assert(IFD->getDefinition() && "Category on a class without a definition?");
5942   ObjCClassesWithCategories.insert(
5943     const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
5944 }
5945 
5946 
5947 void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5948                                           const ObjCPropertyDecl *OrigProp,
5949                                           const ObjCCategoryDecl *ClassExt) {
5950   const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5951   if (!D)
5952     return;
5953 
5954   assert(!WritingAST && "Already writing the AST!");
5955   if (!D->isFromASTFile())
5956     return; // Declaration not imported from PCH.
5957 
5958   RewriteDecl(D);
5959 }
5960 
5961 void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5962   assert(!WritingAST && "Already writing the AST!");
5963   if (!D->isFromASTFile())
5964     return;
5965 
5966   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
5967 }
5968 
5969 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5970   assert(!WritingAST && "Already writing the AST!");
5971   if (!D->isFromASTFile())
5972     return;
5973 
5974   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5975 }
5976