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