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