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