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