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