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