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