[OpenMP][NFC] Remove the need to include `OpenMPClause.h`
[lldb.git] / clang / lib / Serialization / ASTWriter.cpp
1 //===- ASTWriter.cpp - AST File Writer ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTWriter class, which writes AST files.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "clang/AST/OpenMPClause.h"
14 #include "clang/Serialization/ASTRecordWriter.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "MultiOnDiskHashTable.h"
18 #include "clang/AST/AbstractTypeWriter.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTUnresolvedSet.h"
21 #include "clang/AST/Attr.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclContextInternals.h"
26 #include "clang/AST/DeclFriend.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/LambdaCapture.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/RawCommentList.h"
35 #include "clang/AST/TemplateName.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLocVisitor.h"
38 #include "clang/Basic/Diagnostic.h"
39 #include "clang/Basic/DiagnosticOptions.h"
40 #include "clang/Basic/FileManager.h"
41 #include "clang/Basic/FileSystemOptions.h"
42 #include "clang/Basic/IdentifierTable.h"
43 #include "clang/Basic/LLVM.h"
44 #include "clang/Basic/Lambda.h"
45 #include "clang/Basic/LangOptions.h"
46 #include "clang/Basic/Module.h"
47 #include "clang/Basic/ObjCRuntime.h"
48 #include "clang/Basic/OpenCLOptions.h"
49 #include "clang/Basic/SourceLocation.h"
50 #include "clang/Basic/SourceManager.h"
51 #include "clang/Basic/SourceManagerInternals.h"
52 #include "clang/Basic/Specifiers.h"
53 #include "clang/Basic/TargetInfo.h"
54 #include "clang/Basic/TargetOptions.h"
55 #include "clang/Basic/Version.h"
56 #include "clang/Lex/HeaderSearch.h"
57 #include "clang/Lex/HeaderSearchOptions.h"
58 #include "clang/Lex/MacroInfo.h"
59 #include "clang/Lex/ModuleMap.h"
60 #include "clang/Lex/PreprocessingRecord.h"
61 #include "clang/Lex/Preprocessor.h"
62 #include "clang/Lex/PreprocessorOptions.h"
63 #include "clang/Lex/Token.h"
64 #include "clang/Sema/IdentifierResolver.h"
65 #include "clang/Sema/ObjCMethodList.h"
66 #include "clang/Sema/Sema.h"
67 #include "clang/Sema/Weak.h"
68 #include "clang/Serialization/ASTReader.h"
69 #include "clang/Serialization/InMemoryModuleCache.h"
70 #include "clang/Serialization/ModuleFile.h"
71 #include "clang/Serialization/ModuleFileExtension.h"
72 #include "clang/Serialization/SerializationDiagnostic.h"
73 #include "llvm/ADT/APFloat.h"
74 #include "llvm/ADT/APInt.h"
75 #include "llvm/ADT/APSInt.h"
76 #include "llvm/ADT/ArrayRef.h"
77 #include "llvm/ADT/DenseMap.h"
78 #include "llvm/ADT/Hashing.h"
79 #include "llvm/ADT/Optional.h"
80 #include "llvm/ADT/PointerIntPair.h"
81 #include "llvm/ADT/STLExtras.h"
82 #include "llvm/ADT/ScopeExit.h"
83 #include "llvm/ADT/SmallSet.h"
84 #include "llvm/ADT/SmallString.h"
85 #include "llvm/ADT/SmallVector.h"
86 #include "llvm/ADT/StringMap.h"
87 #include "llvm/ADT/StringRef.h"
88 #include "llvm/Bitstream/BitCodes.h"
89 #include "llvm/Bitstream/BitstreamWriter.h"
90 #include "llvm/Support/Casting.h"
91 #include "llvm/Support/Compression.h"
92 #include "llvm/Support/DJB.h"
93 #include "llvm/Support/Endian.h"
94 #include "llvm/Support/EndianStream.h"
95 #include "llvm/Support/Error.h"
96 #include "llvm/Support/ErrorHandling.h"
97 #include "llvm/Support/MemoryBuffer.h"
98 #include "llvm/Support/OnDiskHashTable.h"
99 #include "llvm/Support/Path.h"
100 #include "llvm/Support/SHA1.h"
101 #include "llvm/Support/VersionTuple.h"
102 #include "llvm/Support/raw_ostream.h"
103 #include <algorithm>
104 #include <cassert>
105 #include <cstdint>
106 #include <cstdlib>
107 #include <cstring>
108 #include <ctime>
109 #include <deque>
110 #include <limits>
111 #include <memory>
112 #include <queue>
113 #include <tuple>
114 #include <utility>
115 #include <vector>
116
117 using namespace clang;
118 using namespace clang::serialization;
119
120 template <typename T, typename Allocator>
121 static StringRef bytes(const std::vector<T, Allocator> &v) {
122   if (v.empty()) return StringRef();
123   return StringRef(reinterpret_cast<const char*>(&v[0]),
124                          sizeof(T) * v.size());
125 }
126
127 template <typename T>
128 static StringRef bytes(const SmallVectorImpl<T> &v) {
129   return StringRef(reinterpret_cast<const char*>(v.data()),
130                          sizeof(T) * v.size());
131 }
132
133 //===----------------------------------------------------------------------===//
134 // Type serialization
135 //===----------------------------------------------------------------------===//
136
137 static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
138   switch (id) {
139 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
140   case Type::CLASS_ID: return TYPE_##CODE_ID;
141 #include "clang/Serialization/TypeBitCodes.def"
142   case Type::Builtin:
143     llvm_unreachable("shouldn't be serializing a builtin type this way");
144   }
145   llvm_unreachable("bad type kind");
146 }
147
148 namespace {
149
150 class ASTTypeWriter {
151   ASTWriter &Writer;
152   ASTWriter::RecordData Record;
153   ASTRecordWriter BasicWriter;
154
155 public:
156   ASTTypeWriter(ASTWriter &Writer)
157     : Writer(Writer), BasicWriter(Writer, Record) {}
158
159   uint64_t write(QualType T) {
160     if (T.hasLocalNonFastQualifiers()) {
161       Qualifiers Qs = T.getLocalQualifiers();
162       BasicWriter.writeQualType(T.getLocalUnqualifiedType());
163       BasicWriter.writeQualifiers(Qs);
164       return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
165     }
166
167     const Type *typePtr = T.getTypePtr();
168     serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
169     atw.write(typePtr);
170     return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
171                             /*abbrev*/ 0);
172   }
173 };
174
175 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
176   ASTRecordWriter &Record;
177
178 public:
179   TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {}
180
181 #define ABSTRACT_TYPELOC(CLASS, PARENT)
182 #define TYPELOC(CLASS, PARENT) \
183     void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
184 #include "clang/AST/TypeLocNodes.def"
185
186   void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
187   void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
188 };
189
190 } // namespace
191
192 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
193   // nothing to do
194 }
195
196 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
197   Record.AddSourceLocation(TL.getBuiltinLoc());
198   if (TL.needsExtraLocalData()) {
199     Record.push_back(TL.getWrittenTypeSpec());
200     Record.push_back(TL.getWrittenSignSpec());
201     Record.push_back(TL.getWrittenWidthSpec());
202     Record.push_back(TL.hasModeAttr());
203   }
204 }
205
206 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
207   Record.AddSourceLocation(TL.getNameLoc());
208 }
209
210 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
211   Record.AddSourceLocation(TL.getStarLoc());
212 }
213
214 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
215   // nothing to do
216 }
217
218 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
219   // nothing to do
220 }
221
222 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
223   Record.AddSourceLocation(TL.getCaretLoc());
224 }
225
226 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
227   Record.AddSourceLocation(TL.getAmpLoc());
228 }
229
230 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
231   Record.AddSourceLocation(TL.getAmpAmpLoc());
232 }
233
234 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
235   Record.AddSourceLocation(TL.getStarLoc());
236   Record.AddTypeSourceInfo(TL.getClassTInfo());
237 }
238
239 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
240   Record.AddSourceLocation(TL.getLBracketLoc());
241   Record.AddSourceLocation(TL.getRBracketLoc());
242   Record.push_back(TL.getSizeExpr() ? 1 : 0);
243   if (TL.getSizeExpr())
244     Record.AddStmt(TL.getSizeExpr());
245 }
246
247 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
248   VisitArrayTypeLoc(TL);
249 }
250
251 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
252   VisitArrayTypeLoc(TL);
253 }
254
255 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
256   VisitArrayTypeLoc(TL);
257 }
258
259 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
260                                             DependentSizedArrayTypeLoc TL) {
261   VisitArrayTypeLoc(TL);
262 }
263
264 void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
265     DependentAddressSpaceTypeLoc TL) {
266   Record.AddSourceLocation(TL.getAttrNameLoc());
267   SourceRange range = TL.getAttrOperandParensRange();
268   Record.AddSourceLocation(range.getBegin());
269   Record.AddSourceLocation(range.getEnd());
270   Record.AddStmt(TL.getAttrExprOperand());
271 }
272
273 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
274                                         DependentSizedExtVectorTypeLoc TL) {
275   Record.AddSourceLocation(TL.getNameLoc());
276 }
277
278 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
279   Record.AddSourceLocation(TL.getNameLoc());
280 }
281
282 void TypeLocWriter::VisitDependentVectorTypeLoc(
283     DependentVectorTypeLoc TL) {
284   Record.AddSourceLocation(TL.getNameLoc());
285 }
286
287 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
288   Record.AddSourceLocation(TL.getNameLoc());
289 }
290
291 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
292   Record.AddSourceLocation(TL.getLocalRangeBegin());
293   Record.AddSourceLocation(TL.getLParenLoc());
294   Record.AddSourceLocation(TL.getRParenLoc());
295   Record.AddSourceRange(TL.getExceptionSpecRange());
296   Record.AddSourceLocation(TL.getLocalRangeEnd());
297   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
298     Record.AddDeclRef(TL.getParam(i));
299 }
300
301 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
302   VisitFunctionTypeLoc(TL);
303 }
304
305 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
306   VisitFunctionTypeLoc(TL);
307 }
308
309 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
310   Record.AddSourceLocation(TL.getNameLoc());
311 }
312
313 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
314   Record.AddSourceLocation(TL.getNameLoc());
315 }
316
317 void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
318   if (TL.getNumProtocols()) {
319     Record.AddSourceLocation(TL.getProtocolLAngleLoc());
320     Record.AddSourceLocation(TL.getProtocolRAngleLoc());
321   }
322   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
323     Record.AddSourceLocation(TL.getProtocolLoc(i));
324 }
325
326 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
327   Record.AddSourceLocation(TL.getTypeofLoc());
328   Record.AddSourceLocation(TL.getLParenLoc());
329   Record.AddSourceLocation(TL.getRParenLoc());
330 }
331
332 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
333   Record.AddSourceLocation(TL.getTypeofLoc());
334   Record.AddSourceLocation(TL.getLParenLoc());
335   Record.AddSourceLocation(TL.getRParenLoc());
336   Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
337 }
338
339 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
340   Record.AddSourceLocation(TL.getNameLoc());
341 }
342
343 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
344   Record.AddSourceLocation(TL.getKWLoc());
345   Record.AddSourceLocation(TL.getLParenLoc());
346   Record.AddSourceLocation(TL.getRParenLoc());
347   Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
348 }
349
350 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
351   Record.AddSourceLocation(TL.getNameLoc());
352   Record.push_back(TL.isConstrained());
353   if (TL.isConstrained()) {
354     Record.AddNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc());
355     Record.AddSourceLocation(TL.getTemplateKWLoc());
356     Record.AddSourceLocation(TL.getConceptNameLoc());
357     Record.AddDeclRef(TL.getFoundDecl());
358     Record.AddSourceLocation(TL.getLAngleLoc());
359     Record.AddSourceLocation(TL.getRAngleLoc());
360     for (unsigned I = 0; I < TL.getNumArgs(); ++I)
361       Record.AddTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
362                                         TL.getArgLocInfo(I));
363   }
364 }
365
366 void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
367     DeducedTemplateSpecializationTypeLoc TL) {
368   Record.AddSourceLocation(TL.getTemplateNameLoc());
369 }
370
371 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
372   Record.AddSourceLocation(TL.getNameLoc());
373 }
374
375 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
376   Record.AddSourceLocation(TL.getNameLoc());
377 }
378
379 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
380   Record.AddAttr(TL.getAttr());
381 }
382
383 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
384   Record.AddSourceLocation(TL.getNameLoc());
385 }
386
387 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
388                                             SubstTemplateTypeParmTypeLoc TL) {
389   Record.AddSourceLocation(TL.getNameLoc());
390 }
391
392 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
393                                           SubstTemplateTypeParmPackTypeLoc TL) {
394   Record.AddSourceLocation(TL.getNameLoc());
395 }
396
397 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
398                                            TemplateSpecializationTypeLoc TL) {
399   Record.AddSourceLocation(TL.getTemplateKeywordLoc());
400   Record.AddSourceLocation(TL.getTemplateNameLoc());
401   Record.AddSourceLocation(TL.getLAngleLoc());
402   Record.AddSourceLocation(TL.getRAngleLoc());
403   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
404     Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
405                                       TL.getArgLoc(i).getLocInfo());
406 }
407
408 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
409   Record.AddSourceLocation(TL.getLParenLoc());
410   Record.AddSourceLocation(TL.getRParenLoc());
411 }
412
413 void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
414   Record.AddSourceLocation(TL.getExpansionLoc());
415 }
416
417 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
418   Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
419   Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
420 }
421
422 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
423   Record.AddSourceLocation(TL.getNameLoc());
424 }
425
426 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
427   Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
428   Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
429   Record.AddSourceLocation(TL.getNameLoc());
430 }
431
432 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
433        DependentTemplateSpecializationTypeLoc TL) {
434   Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
435   Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
436   Record.AddSourceLocation(TL.getTemplateKeywordLoc());
437   Record.AddSourceLocation(TL.getTemplateNameLoc());
438   Record.AddSourceLocation(TL.getLAngleLoc());
439   Record.AddSourceLocation(TL.getRAngleLoc());
440   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
441     Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
442                                       TL.getArgLoc(I).getLocInfo());
443 }
444
445 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
446   Record.AddSourceLocation(TL.getEllipsisLoc());
447 }
448
449 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
450   Record.AddSourceLocation(TL.getNameLoc());
451 }
452
453 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
454   Record.push_back(TL.hasBaseTypeAsWritten());
455   Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
456   Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
457   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
458     Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
459   Record.AddSourceLocation(TL.getProtocolLAngleLoc());
460   Record.AddSourceLocation(TL.getProtocolRAngleLoc());
461   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
462     Record.AddSourceLocation(TL.getProtocolLoc(i));
463 }
464
465 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
466   Record.AddSourceLocation(TL.getStarLoc());
467 }
468
469 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
470   Record.AddSourceLocation(TL.getKWLoc());
471   Record.AddSourceLocation(TL.getLParenLoc());
472   Record.AddSourceLocation(TL.getRParenLoc());
473 }
474
475 void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
476   Record.AddSourceLocation(TL.getKWLoc());
477 }
478
479 void ASTWriter::WriteTypeAbbrevs() {
480   using namespace llvm;
481
482   std::shared_ptr<BitCodeAbbrev> Abv;
483
484   // Abbreviation for TYPE_EXT_QUAL
485   Abv = std::make_shared<BitCodeAbbrev>();
486   Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
487   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Type
488   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3));   // Quals
489   TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
490
491   // Abbreviation for TYPE_FUNCTION_PROTO
492   Abv = std::make_shared<BitCodeAbbrev>();
493   Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
494   // FunctionType
495   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // ReturnType
496   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
497   Abv->Add(BitCodeAbbrevOp(0));                         // HasRegParm
498   Abv->Add(BitCodeAbbrevOp(0));                         // RegParm
499   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
500   Abv->Add(BitCodeAbbrevOp(0));                         // ProducesResult
501   Abv->Add(BitCodeAbbrevOp(0));                         // NoCallerSavedRegs
502   Abv->Add(BitCodeAbbrevOp(0));                         // NoCfCheck
503   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // CmseNSCall
504   // FunctionProtoType
505   Abv->Add(BitCodeAbbrevOp(0));                         // IsVariadic
506   Abv->Add(BitCodeAbbrevOp(0));                         // HasTrailingReturn
507   Abv->Add(BitCodeAbbrevOp(0));                         // TypeQuals
508   Abv->Add(BitCodeAbbrevOp(0));                         // RefQualifier
509   Abv->Add(BitCodeAbbrevOp(EST_None));                  // ExceptionSpec
510   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // NumParams
511   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
512   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Params
513   TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
514 }
515
516 //===----------------------------------------------------------------------===//
517 // ASTWriter Implementation
518 //===----------------------------------------------------------------------===//
519
520 static void EmitBlockID(unsigned ID, const char *Name,
521                         llvm::BitstreamWriter &Stream,
522                         ASTWriter::RecordDataImpl &Record) {
523   Record.clear();
524   Record.push_back(ID);
525   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
526
527   // Emit the block name if present.
528   if (!Name || Name[0] == 0)
529     return;
530   Record.clear();
531   while (*Name)
532     Record.push_back(*Name++);
533   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
534 }
535
536 static void EmitRecordID(unsigned ID, const char *Name,
537                          llvm::BitstreamWriter &Stream,
538                          ASTWriter::RecordDataImpl &Record) {
539   Record.clear();
540   Record.push_back(ID);
541   while (*Name)
542     Record.push_back(*Name++);
543   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
544 }
545
546 static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
547                           ASTWriter::RecordDataImpl &Record) {
548 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
549   RECORD(STMT_STOP);
550   RECORD(STMT_NULL_PTR);
551   RECORD(STMT_REF_PTR);
552   RECORD(STMT_NULL);
553   RECORD(STMT_COMPOUND);
554   RECORD(STMT_CASE);
555   RECORD(STMT_DEFAULT);
556   RECORD(STMT_LABEL);
557   RECORD(STMT_ATTRIBUTED);
558   RECORD(STMT_IF);
559   RECORD(STMT_SWITCH);
560   RECORD(STMT_WHILE);
561   RECORD(STMT_DO);
562   RECORD(STMT_FOR);
563   RECORD(STMT_GOTO);
564   RECORD(STMT_INDIRECT_GOTO);
565   RECORD(STMT_CONTINUE);
566   RECORD(STMT_BREAK);
567   RECORD(STMT_RETURN);
568   RECORD(STMT_DECL);
569   RECORD(STMT_GCCASM);
570   RECORD(STMT_MSASM);
571   RECORD(EXPR_PREDEFINED);
572   RECORD(EXPR_DECL_REF);
573   RECORD(EXPR_INTEGER_LITERAL);
574   RECORD(EXPR_FLOATING_LITERAL);
575   RECORD(EXPR_IMAGINARY_LITERAL);
576   RECORD(EXPR_STRING_LITERAL);
577   RECORD(EXPR_CHARACTER_LITERAL);
578   RECORD(EXPR_PAREN);
579   RECORD(EXPR_PAREN_LIST);
580   RECORD(EXPR_UNARY_OPERATOR);
581   RECORD(EXPR_SIZEOF_ALIGN_OF);
582   RECORD(EXPR_ARRAY_SUBSCRIPT);
583   RECORD(EXPR_CALL);
584   RECORD(EXPR_MEMBER);
585   RECORD(EXPR_BINARY_OPERATOR);
586   RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
587   RECORD(EXPR_CONDITIONAL_OPERATOR);
588   RECORD(EXPR_IMPLICIT_CAST);
589   RECORD(EXPR_CSTYLE_CAST);
590   RECORD(EXPR_COMPOUND_LITERAL);
591   RECORD(EXPR_EXT_VECTOR_ELEMENT);
592   RECORD(EXPR_INIT_LIST);
593   RECORD(EXPR_DESIGNATED_INIT);
594   RECORD(EXPR_DESIGNATED_INIT_UPDATE);
595   RECORD(EXPR_IMPLICIT_VALUE_INIT);
596   RECORD(EXPR_NO_INIT);
597   RECORD(EXPR_VA_ARG);
598   RECORD(EXPR_ADDR_LABEL);
599   RECORD(EXPR_STMT);
600   RECORD(EXPR_CHOOSE);
601   RECORD(EXPR_GNU_NULL);
602   RECORD(EXPR_SHUFFLE_VECTOR);
603   RECORD(EXPR_BLOCK);
604   RECORD(EXPR_GENERIC_SELECTION);
605   RECORD(EXPR_OBJC_STRING_LITERAL);
606   RECORD(EXPR_OBJC_BOXED_EXPRESSION);
607   RECORD(EXPR_OBJC_ARRAY_LITERAL);
608   RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
609   RECORD(EXPR_OBJC_ENCODE);
610   RECORD(EXPR_OBJC_SELECTOR_EXPR);
611   RECORD(EXPR_OBJC_PROTOCOL_EXPR);
612   RECORD(EXPR_OBJC_IVAR_REF_EXPR);
613   RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
614   RECORD(EXPR_OBJC_KVC_REF_EXPR);
615   RECORD(EXPR_OBJC_MESSAGE_EXPR);
616   RECORD(STMT_OBJC_FOR_COLLECTION);
617   RECORD(STMT_OBJC_CATCH);
618   RECORD(STMT_OBJC_FINALLY);
619   RECORD(STMT_OBJC_AT_TRY);
620   RECORD(STMT_OBJC_AT_SYNCHRONIZED);
621   RECORD(STMT_OBJC_AT_THROW);
622   RECORD(EXPR_OBJC_BOOL_LITERAL);
623   RECORD(STMT_CXX_CATCH);
624   RECORD(STMT_CXX_TRY);
625   RECORD(STMT_CXX_FOR_RANGE);
626   RECORD(EXPR_CXX_OPERATOR_CALL);
627   RECORD(EXPR_CXX_MEMBER_CALL);
628   RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
629   RECORD(EXPR_CXX_CONSTRUCT);
630   RECORD(EXPR_CXX_TEMPORARY_OBJECT);
631   RECORD(EXPR_CXX_STATIC_CAST);
632   RECORD(EXPR_CXX_DYNAMIC_CAST);
633   RECORD(EXPR_CXX_REINTERPRET_CAST);
634   RECORD(EXPR_CXX_CONST_CAST);
635   RECORD(EXPR_CXX_FUNCTIONAL_CAST);
636   RECORD(EXPR_USER_DEFINED_LITERAL);
637   RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
638   RECORD(EXPR_CXX_BOOL_LITERAL);
639   RECORD(EXPR_CXX_NULL_PTR_LITERAL);
640   RECORD(EXPR_CXX_TYPEID_EXPR);
641   RECORD(EXPR_CXX_TYPEID_TYPE);
642   RECORD(EXPR_CXX_THIS);
643   RECORD(EXPR_CXX_THROW);
644   RECORD(EXPR_CXX_DEFAULT_ARG);
645   RECORD(EXPR_CXX_DEFAULT_INIT);
646   RECORD(EXPR_CXX_BIND_TEMPORARY);
647   RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
648   RECORD(EXPR_CXX_NEW);
649   RECORD(EXPR_CXX_DELETE);
650   RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
651   RECORD(EXPR_EXPR_WITH_CLEANUPS);
652   RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
653   RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
654   RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
655   RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
656   RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
657   RECORD(EXPR_CXX_EXPRESSION_TRAIT);
658   RECORD(EXPR_CXX_NOEXCEPT);
659   RECORD(EXPR_OPAQUE_VALUE);
660   RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
661   RECORD(EXPR_TYPE_TRAIT);
662   RECORD(EXPR_ARRAY_TYPE_TRAIT);
663   RECORD(EXPR_PACK_EXPANSION);
664   RECORD(EXPR_SIZEOF_PACK);
665   RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
666   RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
667   RECORD(EXPR_FUNCTION_PARM_PACK);
668   RECORD(EXPR_MATERIALIZE_TEMPORARY);
669   RECORD(EXPR_CUDA_KERNEL_CALL);
670   RECORD(EXPR_CXX_UUIDOF_EXPR);
671   RECORD(EXPR_CXX_UUIDOF_TYPE);
672   RECORD(EXPR_LAMBDA);
673 #undef RECORD
674 }
675
676 void ASTWriter::WriteBlockInfoBlock() {
677   RecordData Record;
678   Stream.EnterBlockInfoBlock();
679
680 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
681 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
682
683   // Control Block.
684   BLOCK(CONTROL_BLOCK);
685   RECORD(METADATA);
686   RECORD(MODULE_NAME);
687   RECORD(MODULE_DIRECTORY);
688   RECORD(MODULE_MAP_FILE);
689   RECORD(IMPORTS);
690   RECORD(ORIGINAL_FILE);
691   RECORD(ORIGINAL_PCH_DIR);
692   RECORD(ORIGINAL_FILE_ID);
693   RECORD(INPUT_FILE_OFFSETS);
694
695   BLOCK(OPTIONS_BLOCK);
696   RECORD(LANGUAGE_OPTIONS);
697   RECORD(TARGET_OPTIONS);
698   RECORD(FILE_SYSTEM_OPTIONS);
699   RECORD(HEADER_SEARCH_OPTIONS);
700   RECORD(PREPROCESSOR_OPTIONS);
701
702   BLOCK(INPUT_FILES_BLOCK);
703   RECORD(INPUT_FILE);
704   RECORD(INPUT_FILE_HASH);
705
706   // AST Top-Level Block.
707   BLOCK(AST_BLOCK);
708   RECORD(TYPE_OFFSET);
709   RECORD(DECL_OFFSET);
710   RECORD(IDENTIFIER_OFFSET);
711   RECORD(IDENTIFIER_TABLE);
712   RECORD(EAGERLY_DESERIALIZED_DECLS);
713   RECORD(MODULAR_CODEGEN_DECLS);
714   RECORD(SPECIAL_TYPES);
715   RECORD(STATISTICS);
716   RECORD(TENTATIVE_DEFINITIONS);
717   RECORD(SELECTOR_OFFSETS);
718   RECORD(METHOD_POOL);
719   RECORD(PP_COUNTER_VALUE);
720   RECORD(SOURCE_LOCATION_OFFSETS);
721   RECORD(SOURCE_LOCATION_PRELOADS);
722   RECORD(EXT_VECTOR_DECLS);
723   RECORD(UNUSED_FILESCOPED_DECLS);
724   RECORD(PPD_ENTITIES_OFFSETS);
725   RECORD(VTABLE_USES);
726   RECORD(PPD_SKIPPED_RANGES);
727   RECORD(REFERENCED_SELECTOR_POOL);
728   RECORD(TU_UPDATE_LEXICAL);
729   RECORD(SEMA_DECL_REFS);
730   RECORD(WEAK_UNDECLARED_IDENTIFIERS);
731   RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
732   RECORD(UPDATE_VISIBLE);
733   RECORD(DECL_UPDATE_OFFSETS);
734   RECORD(DECL_UPDATES);
735   RECORD(CUDA_SPECIAL_DECL_REFS);
736   RECORD(HEADER_SEARCH_TABLE);
737   RECORD(FP_PRAGMA_OPTIONS);
738   RECORD(OPENCL_EXTENSIONS);
739   RECORD(OPENCL_EXTENSION_TYPES);
740   RECORD(OPENCL_EXTENSION_DECLS);
741   RECORD(DELEGATING_CTORS);
742   RECORD(KNOWN_NAMESPACES);
743   RECORD(MODULE_OFFSET_MAP);
744   RECORD(SOURCE_MANAGER_LINE_TABLE);
745   RECORD(OBJC_CATEGORIES_MAP);
746   RECORD(FILE_SORTED_DECLS);
747   RECORD(IMPORTED_MODULES);
748   RECORD(OBJC_CATEGORIES);
749   RECORD(MACRO_OFFSET);
750   RECORD(INTERESTING_IDENTIFIERS);
751   RECORD(UNDEFINED_BUT_USED);
752   RECORD(LATE_PARSED_TEMPLATE);
753   RECORD(OPTIMIZE_PRAGMA_OPTIONS);
754   RECORD(MSSTRUCT_PRAGMA_OPTIONS);
755   RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
756   RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
757   RECORD(DELETE_EXPRS_TO_ANALYZE);
758   RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
759   RECORD(PP_CONDITIONAL_STACK);
760   RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
761
762   // SourceManager Block.
763   BLOCK(SOURCE_MANAGER_BLOCK);
764   RECORD(SM_SLOC_FILE_ENTRY);
765   RECORD(SM_SLOC_BUFFER_ENTRY);
766   RECORD(SM_SLOC_BUFFER_BLOB);
767   RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
768   RECORD(SM_SLOC_EXPANSION_ENTRY);
769
770   // Preprocessor Block.
771   BLOCK(PREPROCESSOR_BLOCK);
772   RECORD(PP_MACRO_DIRECTIVE_HISTORY);
773   RECORD(PP_MACRO_FUNCTION_LIKE);
774   RECORD(PP_MACRO_OBJECT_LIKE);
775   RECORD(PP_MODULE_MACRO);
776   RECORD(PP_TOKEN);
777
778   // Submodule Block.
779   BLOCK(SUBMODULE_BLOCK);
780   RECORD(SUBMODULE_METADATA);
781   RECORD(SUBMODULE_DEFINITION);
782   RECORD(SUBMODULE_UMBRELLA_HEADER);
783   RECORD(SUBMODULE_HEADER);
784   RECORD(SUBMODULE_TOPHEADER);
785   RECORD(SUBMODULE_UMBRELLA_DIR);
786   RECORD(SUBMODULE_IMPORTS);
787   RECORD(SUBMODULE_EXPORTS);
788   RECORD(SUBMODULE_REQUIRES);
789   RECORD(SUBMODULE_EXCLUDED_HEADER);
790   RECORD(SUBMODULE_LINK_LIBRARY);
791   RECORD(SUBMODULE_CONFIG_MACRO);
792   RECORD(SUBMODULE_CONFLICT);
793   RECORD(SUBMODULE_PRIVATE_HEADER);
794   RECORD(SUBMODULE_TEXTUAL_HEADER);
795   RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
796   RECORD(SUBMODULE_INITIALIZERS);
797   RECORD(SUBMODULE_EXPORT_AS);
798
799   // Comments Block.
800   BLOCK(COMMENTS_BLOCK);
801   RECORD(COMMENTS_RAW_COMMENT);
802
803   // Decls and Types block.
804   BLOCK(DECLTYPES_BLOCK);
805   RECORD(TYPE_EXT_QUAL);
806   RECORD(TYPE_COMPLEX);
807   RECORD(TYPE_POINTER);
808   RECORD(TYPE_BLOCK_POINTER);
809   RECORD(TYPE_LVALUE_REFERENCE);
810   RECORD(TYPE_RVALUE_REFERENCE);
811   RECORD(TYPE_MEMBER_POINTER);
812   RECORD(TYPE_CONSTANT_ARRAY);
813   RECORD(TYPE_INCOMPLETE_ARRAY);
814   RECORD(TYPE_VARIABLE_ARRAY);
815   RECORD(TYPE_VECTOR);
816   RECORD(TYPE_EXT_VECTOR);
817   RECORD(TYPE_FUNCTION_NO_PROTO);
818   RECORD(TYPE_FUNCTION_PROTO);
819   RECORD(TYPE_TYPEDEF);
820   RECORD(TYPE_TYPEOF_EXPR);
821   RECORD(TYPE_TYPEOF);
822   RECORD(TYPE_RECORD);
823   RECORD(TYPE_ENUM);
824   RECORD(TYPE_OBJC_INTERFACE);
825   RECORD(TYPE_OBJC_OBJECT_POINTER);
826   RECORD(TYPE_DECLTYPE);
827   RECORD(TYPE_ELABORATED);
828   RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
829   RECORD(TYPE_UNRESOLVED_USING);
830   RECORD(TYPE_INJECTED_CLASS_NAME);
831   RECORD(TYPE_OBJC_OBJECT);
832   RECORD(TYPE_TEMPLATE_TYPE_PARM);
833   RECORD(TYPE_TEMPLATE_SPECIALIZATION);
834   RECORD(TYPE_DEPENDENT_NAME);
835   RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
836   RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
837   RECORD(TYPE_PAREN);
838   RECORD(TYPE_MACRO_QUALIFIED);
839   RECORD(TYPE_PACK_EXPANSION);
840   RECORD(TYPE_ATTRIBUTED);
841   RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
842   RECORD(TYPE_AUTO);
843   RECORD(TYPE_UNARY_TRANSFORM);
844   RECORD(TYPE_ATOMIC);
845   RECORD(TYPE_DECAYED);
846   RECORD(TYPE_ADJUSTED);
847   RECORD(TYPE_OBJC_TYPE_PARAM);
848   RECORD(LOCAL_REDECLARATIONS);
849   RECORD(DECL_TYPEDEF);
850   RECORD(DECL_TYPEALIAS);
851   RECORD(DECL_ENUM);
852   RECORD(DECL_RECORD);
853   RECORD(DECL_ENUM_CONSTANT);
854   RECORD(DECL_FUNCTION);
855   RECORD(DECL_OBJC_METHOD);
856   RECORD(DECL_OBJC_INTERFACE);
857   RECORD(DECL_OBJC_PROTOCOL);
858   RECORD(DECL_OBJC_IVAR);
859   RECORD(DECL_OBJC_AT_DEFS_FIELD);
860   RECORD(DECL_OBJC_CATEGORY);
861   RECORD(DECL_OBJC_CATEGORY_IMPL);
862   RECORD(DECL_OBJC_IMPLEMENTATION);
863   RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
864   RECORD(DECL_OBJC_PROPERTY);
865   RECORD(DECL_OBJC_PROPERTY_IMPL);
866   RECORD(DECL_FIELD);
867   RECORD(DECL_MS_PROPERTY);
868   RECORD(DECL_VAR);
869   RECORD(DECL_IMPLICIT_PARAM);
870   RECORD(DECL_PARM_VAR);
871   RECORD(DECL_FILE_SCOPE_ASM);
872   RECORD(DECL_BLOCK);
873   RECORD(DECL_CONTEXT_LEXICAL);
874   RECORD(DECL_CONTEXT_VISIBLE);
875   RECORD(DECL_NAMESPACE);
876   RECORD(DECL_NAMESPACE_ALIAS);
877   RECORD(DECL_USING);
878   RECORD(DECL_USING_SHADOW);
879   RECORD(DECL_USING_DIRECTIVE);
880   RECORD(DECL_UNRESOLVED_USING_VALUE);
881   RECORD(DECL_UNRESOLVED_USING_TYPENAME);
882   RECORD(DECL_LINKAGE_SPEC);
883   RECORD(DECL_CXX_RECORD);
884   RECORD(DECL_CXX_METHOD);
885   RECORD(DECL_CXX_CONSTRUCTOR);
886   RECORD(DECL_CXX_DESTRUCTOR);
887   RECORD(DECL_CXX_CONVERSION);
888   RECORD(DECL_ACCESS_SPEC);
889   RECORD(DECL_FRIEND);
890   RECORD(DECL_FRIEND_TEMPLATE);
891   RECORD(DECL_CLASS_TEMPLATE);
892   RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
893   RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
894   RECORD(DECL_VAR_TEMPLATE);
895   RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
896   RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
897   RECORD(DECL_FUNCTION_TEMPLATE);
898   RECORD(DECL_TEMPLATE_TYPE_PARM);
899   RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
900   RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
901   RECORD(DECL_CONCEPT);
902   RECORD(DECL_REQUIRES_EXPR_BODY);
903   RECORD(DECL_TYPE_ALIAS_TEMPLATE);
904   RECORD(DECL_STATIC_ASSERT);
905   RECORD(DECL_CXX_BASE_SPECIFIERS);
906   RECORD(DECL_CXX_CTOR_INITIALIZERS);
907   RECORD(DECL_INDIRECTFIELD);
908   RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
909   RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
910   RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
911   RECORD(DECL_IMPORT);
912   RECORD(DECL_OMP_THREADPRIVATE);
913   RECORD(DECL_EMPTY);
914   RECORD(DECL_OBJC_TYPE_PARAM);
915   RECORD(DECL_OMP_CAPTUREDEXPR);
916   RECORD(DECL_PRAGMA_COMMENT);
917   RECORD(DECL_PRAGMA_DETECT_MISMATCH);
918   RECORD(DECL_OMP_DECLARE_REDUCTION);
919   RECORD(DECL_OMP_ALLOCATE);
920
921   // Statements and Exprs can occur in the Decls and Types block.
922   AddStmtsExprs(Stream, Record);
923
924   BLOCK(PREPROCESSOR_DETAIL_BLOCK);
925   RECORD(PPD_MACRO_EXPANSION);
926   RECORD(PPD_MACRO_DEFINITION);
927   RECORD(PPD_INCLUSION_DIRECTIVE);
928
929   // Decls and Types block.
930   BLOCK(EXTENSION_BLOCK);
931   RECORD(EXTENSION_METADATA);
932
933   BLOCK(UNHASHED_CONTROL_BLOCK);
934   RECORD(SIGNATURE);
935   RECORD(DIAGNOSTIC_OPTIONS);
936   RECORD(DIAG_PRAGMA_MAPPINGS);
937
938 #undef RECORD
939 #undef BLOCK
940   Stream.ExitBlock();
941 }
942
943 /// Prepares a path for being written to an AST file by converting it
944 /// to an absolute path and removing nested './'s.
945 ///
946 /// \return \c true if the path was changed.
947 static bool cleanPathForOutput(FileManager &FileMgr,
948                                SmallVectorImpl<char> &Path) {
949   bool Changed = FileMgr.makeAbsolutePath(Path);
950   return Changed | llvm::sys::path::remove_dots(Path);
951 }
952
953 /// Adjusts the given filename to only write out the portion of the
954 /// filename that is not part of the system root directory.
955 ///
956 /// \param Filename the file name to adjust.
957 ///
958 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
959 /// the returned filename will be adjusted by this root directory.
960 ///
961 /// \returns either the original filename (if it needs no adjustment) or the
962 /// adjusted filename (which points into the @p Filename parameter).
963 static const char *
964 adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
965   assert(Filename && "No file name to adjust?");
966
967   if (BaseDir.empty())
968     return Filename;
969
970   // Verify that the filename and the system root have the same prefix.
971   unsigned Pos = 0;
972   for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
973     if (Filename[Pos] != BaseDir[Pos])
974       return Filename; // Prefixes don't match.
975
976   // We hit the end of the filename before we hit the end of the system root.
977   if (!Filename[Pos])
978     return Filename;
979
980   // If there's not a path separator at the end of the base directory nor
981   // immediately after it, then this isn't within the base directory.
982   if (!llvm::sys::path::is_separator(Filename[Pos])) {
983     if (!llvm::sys::path::is_separator(BaseDir.back()))
984       return Filename;
985   } else {
986     // If the file name has a '/' at the current position, skip over the '/'.
987     // We distinguish relative paths from absolute paths by the
988     // absence of '/' at the beginning of relative paths.
989     //
990     // FIXME: This is wrong. We distinguish them by asking if the path is
991     // absolute, which isn't the same thing. And there might be multiple '/'s
992     // in a row. Use a better mechanism to indicate whether we have emitted an
993     // absolute or relative path.
994     ++Pos;
995   }
996
997   return Filename + Pos;
998 }
999
1000 ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1001   // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1002   llvm::SHA1 Hasher;
1003   Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1004   auto Hash = Hasher.result();
1005
1006   // Convert to an array [5*i32].
1007   ASTFileSignature Signature;
1008   auto LShift = [&](unsigned char Val, unsigned Shift) {
1009     return (uint32_t)Val << Shift;
1010   };
1011   for (int I = 0; I != 5; ++I)
1012     Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1013                    LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1014
1015   return Signature;
1016 }
1017
1018 ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1019                                                       ASTContext &Context) {
1020   // Flush first to prepare the PCM hash (signature).
1021   Stream.FlushToWord();
1022   auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1023
1024   // Enter the block and prepare to write records.
1025   RecordData Record;
1026   Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1027
1028   // For implicit modules, write the hash of the PCM as its signature.
1029   ASTFileSignature Signature;
1030   if (WritingModule &&
1031       PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1032     Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1033     Record.append(Signature.begin(), Signature.end());
1034     Stream.EmitRecord(SIGNATURE, Record);
1035     Record.clear();
1036   }
1037
1038   // Diagnostic options.
1039   const auto &Diags = Context.getDiagnostics();
1040   const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1041 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1042 #define ENUM_DIAGOPT(Name, Type, Bits, Default)                                \
1043   Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1044 #include "clang/Basic/DiagnosticOptions.def"
1045   Record.push_back(DiagOpts.Warnings.size());
1046   for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1047     AddString(DiagOpts.Warnings[I], Record);
1048   Record.push_back(DiagOpts.Remarks.size());
1049   for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1050     AddString(DiagOpts.Remarks[I], Record);
1051   // Note: we don't serialize the log or serialization file names, because they
1052   // are generally transient files and will almost always be overridden.
1053   Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1054
1055   // Write out the diagnostic/pragma mappings.
1056   WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1057
1058   // Leave the options block.
1059   Stream.ExitBlock();
1060   return Signature;
1061 }
1062
1063 /// Write the control block.
1064 void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1065                                   StringRef isysroot,
1066                                   const std::string &OutputFile) {
1067   using namespace llvm;
1068
1069   Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1070   RecordData Record;
1071
1072   // Metadata
1073   auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1074   MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1075   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1076   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1077   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1078   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1079   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1080   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1081   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PCHHasObjectFile
1082   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1083   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1084   unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1085   assert((!WritingModule || isysroot.empty()) &&
1086          "writing module as a relocatable PCH?");
1087   {
1088     RecordData::value_type Record[] = {
1089         METADATA,
1090         VERSION_MAJOR,
1091         VERSION_MINOR,
1092         CLANG_VERSION_MAJOR,
1093         CLANG_VERSION_MINOR,
1094         !isysroot.empty(),
1095         IncludeTimestamps,
1096         Context.getLangOpts().BuildingPCHWithObjectFile,
1097         ASTHasCompilerErrors};
1098     Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1099                               getClangFullRepositoryVersion());
1100   }
1101
1102   if (WritingModule) {
1103     // Module name
1104     auto Abbrev = std::make_shared<BitCodeAbbrev>();
1105     Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1106     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1107     unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1108     RecordData::value_type Record[] = {MODULE_NAME};
1109     Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1110   }
1111
1112   if (WritingModule && WritingModule->Directory) {
1113     SmallString<128> BaseDir(WritingModule->Directory->getName());
1114     cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1115
1116     // If the home of the module is the current working directory, then we
1117     // want to pick up the cwd of the build process loading the module, not
1118     // our cwd, when we load this module.
1119     if (!PP.getHeaderSearchInfo()
1120              .getHeaderSearchOpts()
1121              .ModuleMapFileHomeIsCwd ||
1122         WritingModule->Directory->getName() != StringRef(".")) {
1123       // Module directory.
1124       auto Abbrev = std::make_shared<BitCodeAbbrev>();
1125       Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1126       Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1127       unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1128
1129       RecordData::value_type Record[] = {MODULE_DIRECTORY};
1130       Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1131     }
1132
1133     // Write out all other paths relative to the base directory if possible.
1134     BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1135   } else if (!isysroot.empty()) {
1136     // Write out paths relative to the sysroot if possible.
1137     BaseDirectory = std::string(isysroot);
1138   }
1139
1140   // Module map file
1141   if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1142     Record.clear();
1143
1144     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1145     AddPath(WritingModule->PresumedModuleMapFile.empty()
1146                 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1147                 : StringRef(WritingModule->PresumedModuleMapFile),
1148             Record);
1149
1150     // Additional module map files.
1151     if (auto *AdditionalModMaps =
1152             Map.getAdditionalModuleMapFiles(WritingModule)) {
1153       Record.push_back(AdditionalModMaps->size());
1154       for (const FileEntry *F : *AdditionalModMaps)
1155         AddPath(F->getName(), Record);
1156     } else {
1157       Record.push_back(0);
1158     }
1159
1160     Stream.EmitRecord(MODULE_MAP_FILE, Record);
1161   }
1162
1163   // Imports
1164   if (Chain) {
1165     serialization::ModuleManager &Mgr = Chain->getModuleManager();
1166     Record.clear();
1167
1168     for (ModuleFile &M : Mgr) {
1169       // Skip modules that weren't directly imported.
1170       if (!M.isDirectlyImported())
1171         continue;
1172
1173       Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1174       AddSourceLocation(M.ImportLoc, Record);
1175
1176       // If we have calculated signature, there is no need to store
1177       // the size or timestamp.
1178       Record.push_back(M.Signature ? 0 : M.File->getSize());
1179       Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1180
1181       for (auto I : M.Signature)
1182         Record.push_back(I);
1183
1184       AddString(M.ModuleName, Record);
1185       AddPath(M.FileName, Record);
1186     }
1187     Stream.EmitRecord(IMPORTS, Record);
1188   }
1189
1190   // Write the options block.
1191   Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1192
1193   // Language options.
1194   Record.clear();
1195   const LangOptions &LangOpts = Context.getLangOpts();
1196 #define LANGOPT(Name, Bits, Default, Description) \
1197   Record.push_back(LangOpts.Name);
1198 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1199   Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1200 #include "clang/Basic/LangOptions.def"
1201 #define SANITIZER(NAME, ID)                                                    \
1202   Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1203 #include "clang/Basic/Sanitizers.def"
1204
1205   Record.push_back(LangOpts.ModuleFeatures.size());
1206   for (StringRef Feature : LangOpts.ModuleFeatures)
1207     AddString(Feature, Record);
1208
1209   Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1210   AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1211
1212   AddString(LangOpts.CurrentModule, Record);
1213
1214   // Comment options.
1215   Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1216   for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1217     AddString(I, Record);
1218   }
1219   Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1220
1221   // OpenMP offloading options.
1222   Record.push_back(LangOpts.OMPTargetTriples.size());
1223   for (auto &T : LangOpts.OMPTargetTriples)
1224     AddString(T.getTriple(), Record);
1225
1226   AddString(LangOpts.OMPHostIRFile, Record);
1227
1228   Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1229
1230   // Target options.
1231   Record.clear();
1232   const TargetInfo &Target = Context.getTargetInfo();
1233   const TargetOptions &TargetOpts = Target.getTargetOpts();
1234   AddString(TargetOpts.Triple, Record);
1235   AddString(TargetOpts.CPU, Record);
1236   AddString(TargetOpts.ABI, Record);
1237   Record.push_back(TargetOpts.FeaturesAsWritten.size());
1238   for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1239     AddString(TargetOpts.FeaturesAsWritten[I], Record);
1240   }
1241   Record.push_back(TargetOpts.Features.size());
1242   for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1243     AddString(TargetOpts.Features[I], Record);
1244   }
1245   Stream.EmitRecord(TARGET_OPTIONS, Record);
1246
1247   // File system options.
1248   Record.clear();
1249   const FileSystemOptions &FSOpts =
1250       Context.getSourceManager().getFileManager().getFileSystemOpts();
1251   AddString(FSOpts.WorkingDir, Record);
1252   Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1253
1254   // Header search options.
1255   Record.clear();
1256   const HeaderSearchOptions &HSOpts
1257     = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1258   AddString(HSOpts.Sysroot, Record);
1259
1260   // Include entries.
1261   Record.push_back(HSOpts.UserEntries.size());
1262   for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1263     const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1264     AddString(Entry.Path, Record);
1265     Record.push_back(static_cast<unsigned>(Entry.Group));
1266     Record.push_back(Entry.IsFramework);
1267     Record.push_back(Entry.IgnoreSysRoot);
1268   }
1269
1270   // System header prefixes.
1271   Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1272   for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1273     AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1274     Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1275   }
1276
1277   AddString(HSOpts.ResourceDir, Record);
1278   AddString(HSOpts.ModuleCachePath, Record);
1279   AddString(HSOpts.ModuleUserBuildPath, Record);
1280   Record.push_back(HSOpts.DisableModuleHash);
1281   Record.push_back(HSOpts.ImplicitModuleMaps);
1282   Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1283   Record.push_back(HSOpts.UseBuiltinIncludes);
1284   Record.push_back(HSOpts.UseStandardSystemIncludes);
1285   Record.push_back(HSOpts.UseStandardCXXIncludes);
1286   Record.push_back(HSOpts.UseLibcxx);
1287   // Write out the specific module cache path that contains the module files.
1288   AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1289   Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1290
1291   // Preprocessor options.
1292   Record.clear();
1293   const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1294
1295   // Macro definitions.
1296   Record.push_back(PPOpts.Macros.size());
1297   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1298     AddString(PPOpts.Macros[I].first, Record);
1299     Record.push_back(PPOpts.Macros[I].second);
1300   }
1301
1302   // Includes
1303   Record.push_back(PPOpts.Includes.size());
1304   for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1305     AddString(PPOpts.Includes[I], Record);
1306
1307   // Macro includes
1308   Record.push_back(PPOpts.MacroIncludes.size());
1309   for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1310     AddString(PPOpts.MacroIncludes[I], Record);
1311
1312   Record.push_back(PPOpts.UsePredefines);
1313   // Detailed record is important since it is used for the module cache hash.
1314   Record.push_back(PPOpts.DetailedRecord);
1315   AddString(PPOpts.ImplicitPCHInclude, Record);
1316   Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1317   Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1318
1319   // Leave the options block.
1320   Stream.ExitBlock();
1321
1322   // Original file name and file ID
1323   SourceManager &SM = Context.getSourceManager();
1324   if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1325     auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1326     FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1327     FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1328     FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1329     unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1330
1331     Record.clear();
1332     Record.push_back(ORIGINAL_FILE);
1333     Record.push_back(SM.getMainFileID().getOpaqueValue());
1334     EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1335   }
1336
1337   Record.clear();
1338   Record.push_back(SM.getMainFileID().getOpaqueValue());
1339   Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1340
1341   // Original PCH directory
1342   if (!OutputFile.empty() && OutputFile != "-") {
1343     auto Abbrev = std::make_shared<BitCodeAbbrev>();
1344     Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1345     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1346     unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1347
1348     SmallString<128> OutputPath(OutputFile);
1349
1350     SM.getFileManager().makeAbsolutePath(OutputPath);
1351     StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1352
1353     RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
1354     Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1355   }
1356
1357   WriteInputFiles(Context.SourceMgr,
1358                   PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1359                   PP.getLangOpts().Modules);
1360   Stream.ExitBlock();
1361 }
1362
1363 namespace  {
1364
1365 /// An input file.
1366 struct InputFileEntry {
1367   const FileEntry *File;
1368   bool IsSystemFile;
1369   bool IsTransient;
1370   bool BufferOverridden;
1371   bool IsTopLevelModuleMap;
1372   uint32_t ContentHash[2];
1373 };
1374
1375 } // namespace
1376
1377 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1378                                 HeaderSearchOptions &HSOpts,
1379                                 bool Modules) {
1380   using namespace llvm;
1381
1382   Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1383
1384   // Create input-file abbreviation.
1385   auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1386   IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1387   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1388   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1389   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1390   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1391   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1392   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1393   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1394   unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1395
1396   // Create input file hash abbreviation.
1397   auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1398   IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1399   IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1400   IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1401   unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1402
1403   // Get all ContentCache objects for files, sorted by whether the file is a
1404   // system one or not. System files go at the back, users files at the front.
1405   std::deque<InputFileEntry> SortedFiles;
1406   for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1407     // Get this source location entry.
1408     const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1409     assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1410
1411     // We only care about file entries that were not overridden.
1412     if (!SLoc->isFile())
1413       continue;
1414     const SrcMgr::FileInfo &File = SLoc->getFile();
1415     const SrcMgr::ContentCache *Cache = File.getContentCache();
1416     if (!Cache->OrigEntry)
1417       continue;
1418
1419     InputFileEntry Entry;
1420     Entry.File = Cache->OrigEntry;
1421     Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1422     Entry.IsTransient = Cache->IsTransient;
1423     Entry.BufferOverridden = Cache->BufferOverridden;
1424     Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1425                                 File.getIncludeLoc().isInvalid();
1426
1427     auto ContentHash = hash_code(-1);
1428     if (PP->getHeaderSearchInfo()
1429             .getHeaderSearchOpts()
1430             .ValidateASTInputFilesContent) {
1431       auto *MemBuff = Cache->getRawBuffer();
1432       if (MemBuff)
1433         ContentHash = hash_value(MemBuff->getBuffer());
1434       else
1435         // FIXME: The path should be taken from the FileEntryRef.
1436         PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1437             << Entry.File->getName();
1438     }
1439     auto CH = llvm::APInt(64, ContentHash);
1440     Entry.ContentHash[0] =
1441         static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1442     Entry.ContentHash[1] =
1443         static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1444
1445     if (Entry.IsSystemFile)
1446       SortedFiles.push_back(Entry);
1447     else
1448       SortedFiles.push_front(Entry);
1449   }
1450
1451   unsigned UserFilesNum = 0;
1452   // Write out all of the input files.
1453   std::vector<uint64_t> InputFileOffsets;
1454   for (const auto &Entry : SortedFiles) {
1455     uint32_t &InputFileID = InputFileIDs[Entry.File];
1456     if (InputFileID != 0)
1457       continue; // already recorded this file.
1458
1459     // Record this entry's offset.
1460     InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1461
1462     InputFileID = InputFileOffsets.size();
1463
1464     if (!Entry.IsSystemFile)
1465       ++UserFilesNum;
1466
1467     // Emit size/modification time for this file.
1468     // And whether this file was overridden.
1469     {
1470       RecordData::value_type Record[] = {
1471           INPUT_FILE,
1472           InputFileOffsets.size(),
1473           (uint64_t)Entry.File->getSize(),
1474           (uint64_t)getTimestampForOutput(Entry.File),
1475           Entry.BufferOverridden,
1476           Entry.IsTransient,
1477           Entry.IsTopLevelModuleMap};
1478
1479       // FIXME: The path should be taken from the FileEntryRef.
1480       EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1481     }
1482
1483     // Emit content hash for this file.
1484     {
1485       RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1486                                          Entry.ContentHash[1]};
1487       Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1488     }
1489   }
1490
1491   Stream.ExitBlock();
1492
1493   // Create input file offsets abbreviation.
1494   auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1495   OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1496   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1497   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1498                                                                 //   input files
1499   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));   // Array
1500   unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1501
1502   // Write input file offsets.
1503   RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1504                                      InputFileOffsets.size(), UserFilesNum};
1505   Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1506 }
1507
1508 //===----------------------------------------------------------------------===//
1509 // Source Manager Serialization
1510 //===----------------------------------------------------------------------===//
1511
1512 /// Create an abbreviation for the SLocEntry that refers to a
1513 /// file.
1514 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1515   using namespace llvm;
1516
1517   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1518   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1519   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1520   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1521   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1522   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1523   // FileEntry fields.
1524   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1525   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1526   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1527   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1528   return Stream.EmitAbbrev(std::move(Abbrev));
1529 }
1530
1531 /// Create an abbreviation for the SLocEntry that refers to a
1532 /// buffer.
1533 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1534   using namespace llvm;
1535
1536   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1537   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1538   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1539   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1540   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1541   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1542   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1543   return Stream.EmitAbbrev(std::move(Abbrev));
1544 }
1545
1546 /// Create an abbreviation for the SLocEntry that refers to a
1547 /// buffer's blob.
1548 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1549                                            bool Compressed) {
1550   using namespace llvm;
1551
1552   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1553   Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1554                                          : SM_SLOC_BUFFER_BLOB));
1555   if (Compressed)
1556     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1557   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1558   return Stream.EmitAbbrev(std::move(Abbrev));
1559 }
1560
1561 /// Create an abbreviation for the SLocEntry that refers to a macro
1562 /// expansion.
1563 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1564   using namespace llvm;
1565
1566   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1567   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1568   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1569   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1570   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1571   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
1572   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1573   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1574   return Stream.EmitAbbrev(std::move(Abbrev));
1575 }
1576
1577 namespace {
1578
1579   // Trait used for the on-disk hash table of header search information.
1580   class HeaderFileInfoTrait {
1581     ASTWriter &Writer;
1582
1583     // Keep track of the framework names we've used during serialization.
1584     SmallVector<char, 128> FrameworkStringData;
1585     llvm::StringMap<unsigned> FrameworkNameOffset;
1586
1587   public:
1588     HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1589
1590     struct key_type {
1591       StringRef Filename;
1592       off_t Size;
1593       time_t ModTime;
1594     };
1595     using key_type_ref = const key_type &;
1596
1597     using UnresolvedModule =
1598         llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1599
1600     struct data_type {
1601       const HeaderFileInfo &HFI;
1602       ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1603       UnresolvedModule Unresolved;
1604     };
1605     using data_type_ref = const data_type &;
1606
1607     using hash_value_type = unsigned;
1608     using offset_type = unsigned;
1609
1610     hash_value_type ComputeHash(key_type_ref key) {
1611       // The hash is based only on size/time of the file, so that the reader can
1612       // match even when symlinking or excess path elements ("foo/../", "../")
1613       // change the form of the name. However, complete path is still the key.
1614       return llvm::hash_combine(key.Size, key.ModTime);
1615     }
1616
1617     std::pair<unsigned, unsigned>
1618     EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1619       using namespace llvm::support;
1620
1621       endian::Writer LE(Out, little);
1622       unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
1623       LE.write<uint16_t>(KeyLen);
1624       unsigned DataLen = 1 + 2 + 4 + 4;
1625       for (auto ModInfo : Data.KnownHeaders)
1626         if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1627           DataLen += 4;
1628       if (Data.Unresolved.getPointer())
1629         DataLen += 4;
1630       LE.write<uint8_t>(DataLen);
1631       return std::make_pair(KeyLen, DataLen);
1632     }
1633
1634     void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1635       using namespace llvm::support;
1636
1637       endian::Writer LE(Out, little);
1638       LE.write<uint64_t>(key.Size);
1639       KeyLen -= 8;
1640       LE.write<uint64_t>(key.ModTime);
1641       KeyLen -= 8;
1642       Out.write(key.Filename.data(), KeyLen);
1643     }
1644
1645     void EmitData(raw_ostream &Out, key_type_ref key,
1646                   data_type_ref Data, unsigned DataLen) {
1647       using namespace llvm::support;
1648
1649       endian::Writer LE(Out, little);
1650       uint64_t Start = Out.tell(); (void)Start;
1651
1652       unsigned char Flags = (Data.HFI.isImport << 5)
1653                           | (Data.HFI.isPragmaOnce << 4)
1654                           | (Data.HFI.DirInfo << 1)
1655                           | Data.HFI.IndexHeaderMapHeader;
1656       LE.write<uint8_t>(Flags);
1657       LE.write<uint16_t>(Data.HFI.NumIncludes);
1658
1659       if (!Data.HFI.ControllingMacro)
1660         LE.write<uint32_t>(Data.HFI.ControllingMacroID);
1661       else
1662         LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1663
1664       unsigned Offset = 0;
1665       if (!Data.HFI.Framework.empty()) {
1666         // If this header refers into a framework, save the framework name.
1667         llvm::StringMap<unsigned>::iterator Pos
1668           = FrameworkNameOffset.find(Data.HFI.Framework);
1669         if (Pos == FrameworkNameOffset.end()) {
1670           Offset = FrameworkStringData.size() + 1;
1671           FrameworkStringData.append(Data.HFI.Framework.begin(),
1672                                      Data.HFI.Framework.end());
1673           FrameworkStringData.push_back(0);
1674
1675           FrameworkNameOffset[Data.HFI.Framework] = Offset;
1676         } else
1677           Offset = Pos->second;
1678       }
1679       LE.write<uint32_t>(Offset);
1680
1681       auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1682         if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1683           uint32_t Value = (ModID << 2) | (unsigned)Role;
1684           assert((Value >> 2) == ModID && "overflow in header module info");
1685           LE.write<uint32_t>(Value);
1686         }
1687       };
1688
1689       // FIXME: If the header is excluded, we should write out some
1690       // record of that fact.
1691       for (auto ModInfo : Data.KnownHeaders)
1692         EmitModule(ModInfo.getModule(), ModInfo.getRole());
1693       if (Data.Unresolved.getPointer())
1694         EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
1695
1696       assert(Out.tell() - Start == DataLen && "Wrong data length");
1697     }
1698
1699     const char *strings_begin() const { return FrameworkStringData.begin(); }
1700     const char *strings_end() const { return FrameworkStringData.end(); }
1701   };
1702
1703 } // namespace
1704
1705 /// Write the header search block for the list of files that
1706 ///
1707 /// \param HS The header search structure to save.
1708 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1709   HeaderFileInfoTrait GeneratorTrait(*this);
1710   llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1711   SmallVector<const char *, 4> SavedStrings;
1712   unsigned NumHeaderSearchEntries = 0;
1713
1714   // Find all unresolved headers for the current module. We generally will
1715   // have resolved them before we get here, but not necessarily: we might be
1716   // compiling a preprocessed module, where there is no requirement for the
1717   // original files to exist any more.
1718   const HeaderFileInfo Empty; // So we can take a reference.
1719   if (WritingModule) {
1720     llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1721     while (!Worklist.empty()) {
1722       Module *M = Worklist.pop_back_val();
1723       if (!M->isAvailable())
1724         continue;
1725
1726       // Map to disk files where possible, to pick up any missing stat
1727       // information. This also means we don't need to check the unresolved
1728       // headers list when emitting resolved headers in the first loop below.
1729       // FIXME: It'd be preferable to avoid doing this if we were given
1730       // sufficient stat information in the module map.
1731       HS.getModuleMap().resolveHeaderDirectives(M);
1732
1733       // If the file didn't exist, we can still create a module if we were given
1734       // enough information in the module map.
1735       for (auto U : M->MissingHeaders) {
1736         // Check that we were given enough information to build a module
1737         // without this file existing on disk.
1738         if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1739           PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1740             << WritingModule->getFullModuleName() << U.Size.hasValue()
1741             << U.FileName;
1742           continue;
1743         }
1744
1745         // Form the effective relative pathname for the file.
1746         SmallString<128> Filename(M->Directory->getName());
1747         llvm::sys::path::append(Filename, U.FileName);
1748         PreparePathForOutput(Filename);
1749
1750         StringRef FilenameDup = strdup(Filename.c_str());
1751         SavedStrings.push_back(FilenameDup.data());
1752
1753         HeaderFileInfoTrait::key_type Key = {
1754           FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
1755         };
1756         HeaderFileInfoTrait::data_type Data = {
1757           Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
1758         };
1759         // FIXME: Deal with cases where there are multiple unresolved header
1760         // directives in different submodules for the same header.
1761         Generator.insert(Key, Data, GeneratorTrait);
1762         ++NumHeaderSearchEntries;
1763       }
1764
1765       Worklist.append(M->submodule_begin(), M->submodule_end());
1766     }
1767   }
1768
1769   SmallVector<const FileEntry *, 16> FilesByUID;
1770   HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1771
1772   if (FilesByUID.size() > HS.header_file_size())
1773     FilesByUID.resize(HS.header_file_size());
1774
1775   for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1776     const FileEntry *File = FilesByUID[UID];
1777     if (!File)
1778       continue;
1779
1780     // Get the file info. This will load info from the external source if
1781     // necessary. Skip emitting this file if we have no information on it
1782     // as a header file (in which case HFI will be null) or if it hasn't
1783     // changed since it was loaded. Also skip it if it's for a modular header
1784     // from a different module; in that case, we rely on the module(s)
1785     // containing the header to provide this information.
1786     const HeaderFileInfo *HFI =
1787         HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1788     if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
1789       continue;
1790
1791     // Massage the file path into an appropriate form.
1792     StringRef Filename = File->getName();
1793     SmallString<128> FilenameTmp(Filename);
1794     if (PreparePathForOutput(FilenameTmp)) {
1795       // If we performed any translation on the file name at all, we need to
1796       // save this string, since the generator will refer to it later.
1797       Filename = StringRef(strdup(FilenameTmp.c_str()));
1798       SavedStrings.push_back(Filename.data());
1799     }
1800
1801     HeaderFileInfoTrait::key_type Key = {
1802       Filename, File->getSize(), getTimestampForOutput(File)
1803     };
1804     HeaderFileInfoTrait::data_type Data = {
1805       *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
1806     };
1807     Generator.insert(Key, Data, GeneratorTrait);
1808     ++NumHeaderSearchEntries;
1809   }
1810
1811   // Create the on-disk hash table in a buffer.
1812   SmallString<4096> TableData;
1813   uint32_t BucketOffset;
1814   {
1815     using namespace llvm::support;
1816
1817     llvm::raw_svector_ostream Out(TableData);
1818     // Make sure that no bucket is at offset 0
1819     endian::write<uint32_t>(Out, 0, little);
1820     BucketOffset = Generator.Emit(Out, GeneratorTrait);
1821   }
1822
1823   // Create a blob abbreviation
1824   using namespace llvm;
1825
1826   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1827   Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1828   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1829   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1830   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1831   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1832   unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1833
1834   // Write the header search table
1835   RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1836                                      NumHeaderSearchEntries, TableData.size()};
1837   TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
1838   Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
1839
1840   // Free all of the strings we had to duplicate.
1841   for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1842     free(const_cast<char *>(SavedStrings[I]));
1843 }
1844
1845 static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
1846                      unsigned SLocBufferBlobCompressedAbbrv,
1847                      unsigned SLocBufferBlobAbbrv) {
1848   using RecordDataType = ASTWriter::RecordData::value_type;
1849
1850   // Compress the buffer if possible. We expect that almost all PCM
1851   // consumers will not want its contents.
1852   SmallString<0> CompressedBuffer;
1853   if (llvm::zlib::isAvailable()) {
1854     llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
1855     if (!E) {
1856       RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
1857                                  Blob.size() - 1};
1858       Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
1859                                 CompressedBuffer);
1860       return;
1861     }
1862     llvm::consumeError(std::move(E));
1863   }
1864
1865   RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
1866   Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
1867 }
1868
1869 /// Writes the block containing the serialized form of the
1870 /// source manager.
1871 ///
1872 /// TODO: We should probably use an on-disk hash table (stored in a
1873 /// blob), indexed based on the file name, so that we only create
1874 /// entries for files that we actually need. In the common case (no
1875 /// errors), we probably won't have to create file entries for any of
1876 /// the files in the AST.
1877 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1878                                         const Preprocessor &PP) {
1879   RecordData Record;
1880
1881   // Enter the source manager block.
1882   Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
1883
1884   // Abbreviations for the various kinds of source-location entries.
1885   unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1886   unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1887   unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
1888   unsigned SLocBufferBlobCompressedAbbrv =
1889       CreateSLocBufferBlobAbbrev(Stream, true);
1890   unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
1891
1892   // Write out the source location entry table. We skip the first
1893   // entry, which is always the same dummy entry.
1894   std::vector<uint32_t> SLocEntryOffsets;
1895   RecordData PreloadSLocs;
1896   SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1897   for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
1898        I != N; ++I) {
1899     // Get this source location entry.
1900     const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1901     FileID FID = FileID::get(I);
1902     assert(&SourceMgr.getSLocEntry(FID) == SLoc);
1903
1904     // Record the offset of this source-location entry.
1905     SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1906
1907     // Figure out which record code to use.
1908     unsigned Code;
1909     if (SLoc->isFile()) {
1910       const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1911       if (Cache->OrigEntry) {
1912         Code = SM_SLOC_FILE_ENTRY;
1913       } else
1914         Code = SM_SLOC_BUFFER_ENTRY;
1915     } else
1916       Code = SM_SLOC_EXPANSION_ENTRY;
1917     Record.clear();
1918     Record.push_back(Code);
1919
1920     // Starting offset of this entry within this module, so skip the dummy.
1921     Record.push_back(SLoc->getOffset() - 2);
1922     if (SLoc->isFile()) {
1923       const SrcMgr::FileInfo &File = SLoc->getFile();
1924       AddSourceLocation(File.getIncludeLoc(), Record);
1925       Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1926       Record.push_back(File.hasLineDirectives());
1927
1928       const SrcMgr::ContentCache *Content = File.getContentCache();
1929       bool EmitBlob = false;
1930       if (Content->OrigEntry) {
1931         assert(Content->OrigEntry == Content->ContentsEntry &&
1932                "Writing to AST an overridden file is not supported");
1933
1934         // The source location entry is a file. Emit input file ID.
1935         assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1936         Record.push_back(InputFileIDs[Content->OrigEntry]);
1937
1938         Record.push_back(File.NumCreatedFIDs);
1939
1940         FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
1941         if (FDI != FileDeclIDs.end()) {
1942           Record.push_back(FDI->second->FirstDeclIndex);
1943           Record.push_back(FDI->second->DeclIDs.size());
1944         } else {
1945           Record.push_back(0);
1946           Record.push_back(0);
1947         }
1948
1949         Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
1950
1951         if (Content->BufferOverridden || Content->IsTransient)
1952           EmitBlob = true;
1953       } else {
1954         // The source location entry is a buffer. The blob associated
1955         // with this entry contains the contents of the buffer.
1956
1957         // We add one to the size so that we capture the trailing NULL
1958         // that is required by llvm::MemoryBuffer::getMemBuffer (on
1959         // the reader side).
1960         const llvm::MemoryBuffer *Buffer =
1961             Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
1962         StringRef Name = Buffer->getBufferIdentifier();
1963         Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1964                                   StringRef(Name.data(), Name.size() + 1));
1965         EmitBlob = true;
1966
1967         if (Name == "<built-in>")
1968           PreloadSLocs.push_back(SLocEntryOffsets.size());
1969       }
1970
1971       if (EmitBlob) {
1972         // Include the implicit terminating null character in the on-disk buffer
1973         // if we're writing it uncompressed.
1974         const llvm::MemoryBuffer *Buffer =
1975             Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
1976         StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
1977         emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
1978                  SLocBufferBlobAbbrv);
1979       }
1980     } else {
1981       // The source location entry is a macro expansion.
1982       const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
1983       AddSourceLocation(Expansion.getSpellingLoc(), Record);
1984       AddSourceLocation(Expansion.getExpansionLocStart(), Record);
1985       AddSourceLocation(Expansion.isMacroArgExpansion()
1986                             ? SourceLocation()
1987                             : Expansion.getExpansionLocEnd(),
1988                         Record);
1989       Record.push_back(Expansion.isExpansionTokenRange());
1990
1991       // Compute the token length for this macro expansion.
1992       unsigned NextOffset = SourceMgr.getNextLocalOffset();
1993       if (I + 1 != N)
1994         NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
1995       Record.push_back(NextOffset - SLoc->getOffset() - 1);
1996       Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
1997     }
1998   }
1999
2000   Stream.ExitBlock();
2001
2002   if (SLocEntryOffsets.empty())
2003     return;
2004
2005   // Write the source-location offsets table into the AST block. This
2006   // table is used for lazily loading source-location information.
2007   using namespace llvm;
2008
2009   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2010   Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2011   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2012   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2013   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2014   unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2015   {
2016     RecordData::value_type Record[] = {
2017         SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2018         SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2019     Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2020                               bytes(SLocEntryOffsets));
2021   }
2022   // Write the source location entry preloads array, telling the AST
2023   // reader which source locations entries it should load eagerly.
2024   Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
2025
2026   // Write the line table. It depends on remapping working, so it must come
2027   // after the source location offsets.
2028   if (SourceMgr.hasLineTable()) {
2029     LineTableInfo &LineTable = SourceMgr.getLineTable();
2030
2031     Record.clear();
2032
2033     // Emit the needed file names.
2034     llvm::DenseMap<int, int> FilenameMap;
2035     FilenameMap[-1] = -1; // For unspecified filenames.
2036     for (const auto &L : LineTable) {
2037       if (L.first.ID < 0)
2038         continue;
2039       for (auto &LE : L.second) {
2040         if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2041                                               FilenameMap.size() - 1)).second)
2042           AddPath(LineTable.getFilename(LE.FilenameID), Record);
2043       }
2044     }
2045     Record.push_back(0);
2046
2047     // Emit the line entries
2048     for (const auto &L : LineTable) {
2049       // Only emit entries for local files.
2050       if (L.first.ID < 0)
2051         continue;
2052
2053       // Emit the file ID
2054       Record.push_back(L.first.ID);
2055
2056       // Emit the line entries
2057       Record.push_back(L.second.size());
2058       for (const auto &LE : L.second) {
2059         Record.push_back(LE.FileOffset);
2060         Record.push_back(LE.LineNo);
2061         Record.push_back(FilenameMap[LE.FilenameID]);
2062         Record.push_back((unsigned)LE.FileKind);
2063         Record.push_back(LE.IncludeOffset);
2064       }
2065     }
2066
2067     Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2068   }
2069 }
2070
2071 //===----------------------------------------------------------------------===//
2072 // Preprocessor Serialization
2073 //===----------------------------------------------------------------------===//
2074
2075 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2076                               const Preprocessor &PP) {
2077   if (MacroInfo *MI = MD->getMacroInfo())
2078     if (MI->isBuiltinMacro())
2079       return true;
2080
2081   if (IsModule) {
2082     SourceLocation Loc = MD->getLocation();
2083     if (Loc.isInvalid())
2084       return true;
2085     if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2086       return true;
2087   }
2088
2089   return false;
2090 }
2091
2092 /// Writes the block containing the serialized form of the
2093 /// preprocessor.
2094 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2095   PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2096   if (PPRec)
2097     WritePreprocessorDetail(*PPRec);
2098
2099   RecordData Record;
2100   RecordData ModuleMacroRecord;
2101
2102   // If the preprocessor __COUNTER__ value has been bumped, remember it.
2103   if (PP.getCounterValue() != 0) {
2104     RecordData::value_type Record[] = {PP.getCounterValue()};
2105     Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2106   }
2107
2108   if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2109     assert(!IsModule);
2110     auto SkipInfo = PP.getPreambleSkipInfo();
2111     if (SkipInfo.hasValue()) {
2112       Record.push_back(true);
2113       AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2114       AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2115       Record.push_back(SkipInfo->FoundNonSkipPortion);
2116       Record.push_back(SkipInfo->FoundElse);
2117       AddSourceLocation(SkipInfo->ElseLoc, Record);
2118     } else {
2119       Record.push_back(false);
2120     }
2121     for (const auto &Cond : PP.getPreambleConditionalStack()) {
2122       AddSourceLocation(Cond.IfLoc, Record);
2123       Record.push_back(Cond.WasSkipping);
2124       Record.push_back(Cond.FoundNonSkip);
2125       Record.push_back(Cond.FoundElse);
2126     }
2127     Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2128     Record.clear();
2129   }
2130
2131   // Enter the preprocessor block.
2132   Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2133
2134   // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2135   // FIXME: Include a location for the use, and say which one was used.
2136   if (PP.SawDateOrTime())
2137     PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2138
2139   // Loop over all the macro directives that are live at the end of the file,
2140   // emitting each to the PP section.
2141
2142   // Construct the list of identifiers with macro directives that need to be
2143   // serialized.
2144   SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2145   for (auto &Id : PP.getIdentifierTable())
2146     if (Id.second->hadMacroDefinition() &&
2147         (!Id.second->isFromAST() ||
2148          Id.second->hasChangedSinceDeserialization()))
2149       MacroIdentifiers.push_back(Id.second);
2150   // Sort the set of macro definitions that need to be serialized by the
2151   // name of the macro, to provide a stable ordering.
2152   llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2153
2154   // Emit the macro directives as a list and associate the offset with the
2155   // identifier they belong to.
2156   for (const IdentifierInfo *Name : MacroIdentifiers) {
2157     MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2158     auto StartOffset = Stream.GetCurrentBitNo();
2159
2160     // Emit the macro directives in reverse source order.
2161     for (; MD; MD = MD->getPrevious()) {
2162       // Once we hit an ignored macro, we're done: the rest of the chain
2163       // will all be ignored macros.
2164       if (shouldIgnoreMacro(MD, IsModule, PP))
2165         break;
2166
2167       AddSourceLocation(MD->getLocation(), Record);
2168       Record.push_back(MD->getKind());
2169       if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2170         Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2171       } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2172         Record.push_back(VisMD->isPublic());
2173       }
2174     }
2175
2176     // Write out any exported module macros.
2177     bool EmittedModuleMacros = false;
2178     // We write out exported module macros for PCH as well.
2179     auto Leafs = PP.getLeafModuleMacros(Name);
2180     SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2181     llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2182     while (!Worklist.empty()) {
2183       auto *Macro = Worklist.pop_back_val();
2184
2185       // Emit a record indicating this submodule exports this macro.
2186       ModuleMacroRecord.push_back(
2187           getSubmoduleID(Macro->getOwningModule()));
2188       ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2189       for (auto *M : Macro->overrides())
2190         ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2191
2192       Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2193       ModuleMacroRecord.clear();
2194
2195       // Enqueue overridden macros once we've visited all their ancestors.
2196       for (auto *M : Macro->overrides())
2197         if (++Visits[M] == M->getNumOverridingMacros())
2198           Worklist.push_back(M);
2199
2200       EmittedModuleMacros = true;
2201     }
2202
2203     if (Record.empty() && !EmittedModuleMacros)
2204       continue;
2205
2206     IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2207     Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2208     Record.clear();
2209   }
2210
2211   /// Offsets of each of the macros into the bitstream, indexed by
2212   /// the local macro ID
2213   ///
2214   /// For each identifier that is associated with a macro, this map
2215   /// provides the offset into the bitstream where that macro is
2216   /// defined.
2217   std::vector<uint32_t> MacroOffsets;
2218
2219   for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2220     const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2221     MacroInfo *MI = MacroInfosToEmit[I].MI;
2222     MacroID ID = MacroInfosToEmit[I].ID;
2223
2224     if (ID < FirstMacroID) {
2225       assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2226       continue;
2227     }
2228
2229     // Record the local offset of this macro.
2230     unsigned Index = ID - FirstMacroID;
2231     if (Index == MacroOffsets.size())
2232       MacroOffsets.push_back(Stream.GetCurrentBitNo());
2233     else {
2234       if (Index > MacroOffsets.size())
2235         MacroOffsets.resize(Index + 1);
2236
2237       MacroOffsets[Index] = Stream.GetCurrentBitNo();
2238     }
2239
2240     AddIdentifierRef(Name, Record);
2241     AddSourceLocation(MI->getDefinitionLoc(), Record);
2242     AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2243     Record.push_back(MI->isUsed());
2244     Record.push_back(MI->isUsedForHeaderGuard());
2245     unsigned Code;
2246     if (MI->isObjectLike()) {
2247       Code = PP_MACRO_OBJECT_LIKE;
2248     } else {
2249       Code = PP_MACRO_FUNCTION_LIKE;
2250
2251       Record.push_back(MI->isC99Varargs());
2252       Record.push_back(MI->isGNUVarargs());
2253       Record.push_back(MI->hasCommaPasting());
2254       Record.push_back(MI->getNumParams());
2255       for (const IdentifierInfo *Param : MI->params())
2256         AddIdentifierRef(Param, Record);
2257     }
2258
2259     // If we have a detailed preprocessing record, record the macro definition
2260     // ID that corresponds to this macro.
2261     if (PPRec)
2262       Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2263
2264     Stream.EmitRecord(Code, Record);
2265     Record.clear();
2266
2267     // Emit the tokens array.
2268     for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2269       // Note that we know that the preprocessor does not have any annotation
2270       // tokens in it because they are created by the parser, and thus can't
2271       // be in a macro definition.
2272       const Token &Tok = MI->getReplacementToken(TokNo);
2273       AddToken(Tok, Record);
2274       Stream.EmitRecord(PP_TOKEN, Record);
2275       Record.clear();
2276     }
2277     ++NumMacros;
2278   }
2279
2280   Stream.ExitBlock();
2281
2282   // Write the offsets table for macro IDs.
2283   using namespace llvm;
2284
2285   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2286   Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2287   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2288   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2289   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2290
2291   unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2292   {
2293     RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2294                                        FirstMacroID - NUM_PREDEF_MACRO_IDS};
2295     Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2296   }
2297 }
2298
2299 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
2300   if (PPRec.local_begin() == PPRec.local_end())
2301     return;
2302
2303   SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2304
2305   // Enter the preprocessor block.
2306   Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2307
2308   // If the preprocessor has a preprocessing record, emit it.
2309   unsigned NumPreprocessingRecords = 0;
2310   using namespace llvm;
2311
2312   // Set up the abbreviation for
2313   unsigned InclusionAbbrev = 0;
2314   {
2315     auto Abbrev = std::make_shared<BitCodeAbbrev>();
2316     Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2317     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2318     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2319     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2320     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2321     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2322     InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2323   }
2324
2325   unsigned FirstPreprocessorEntityID
2326     = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2327     + NUM_PREDEF_PP_ENTITY_IDS;
2328   unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2329   RecordData Record;
2330   for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2331                                   EEnd = PPRec.local_end();
2332        E != EEnd;
2333        (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2334     Record.clear();
2335
2336     PreprocessedEntityOffsets.push_back(
2337         PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
2338
2339     if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2340       // Record this macro definition's ID.
2341       MacroDefinitions[MD] = NextPreprocessorEntityID;
2342
2343       AddIdentifierRef(MD->getName(), Record);
2344       Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2345       continue;
2346     }
2347
2348     if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2349       Record.push_back(ME->isBuiltinMacro());
2350       if (ME->isBuiltinMacro())
2351         AddIdentifierRef(ME->getName(), Record);
2352       else
2353         Record.push_back(MacroDefinitions[ME->getDefinition()]);
2354       Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2355       continue;
2356     }
2357
2358     if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2359       Record.push_back(PPD_INCLUSION_DIRECTIVE);
2360       Record.push_back(ID->getFileName().size());
2361       Record.push_back(ID->wasInQuotes());
2362       Record.push_back(static_cast<unsigned>(ID->getKind()));
2363       Record.push_back(ID->importedModule());
2364       SmallString<64> Buffer;
2365       Buffer += ID->getFileName();
2366       // Check that the FileEntry is not null because it was not resolved and
2367       // we create a PCH even with compiler errors.
2368       if (ID->getFile())
2369         Buffer += ID->getFile()->getName();
2370       Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2371       continue;
2372     }
2373
2374     llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2375   }
2376   Stream.ExitBlock();
2377
2378   // Write the offsets table for the preprocessing record.
2379   if (NumPreprocessingRecords > 0) {
2380     assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2381
2382     // Write the offsets table for identifier IDs.
2383     using namespace llvm;
2384
2385     auto Abbrev = std::make_shared<BitCodeAbbrev>();
2386     Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2387     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2388     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2389     unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2390
2391     RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2392                                        FirstPreprocessorEntityID -
2393                                            NUM_PREDEF_PP_ENTITY_IDS};
2394     Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2395                               bytes(PreprocessedEntityOffsets));
2396   }
2397
2398   // Write the skipped region table for the preprocessing record.
2399   ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2400   if (SkippedRanges.size() > 0) {
2401     std::vector<PPSkippedRange> SerializedSkippedRanges;
2402     SerializedSkippedRanges.reserve(SkippedRanges.size());
2403     for (auto const& Range : SkippedRanges)
2404       SerializedSkippedRanges.emplace_back(Range);
2405
2406     using namespace llvm;
2407     auto Abbrev = std::make_shared<BitCodeAbbrev>();
2408     Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2409     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2410     unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2411
2412     Record.clear();
2413     Record.push_back(PPD_SKIPPED_RANGES);
2414     Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2415                               bytes(SerializedSkippedRanges));
2416   }
2417 }
2418
2419 unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
2420   if (!Mod)
2421     return 0;
2422
2423   llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2424   if (Known != SubmoduleIDs.end())
2425     return Known->second;
2426
2427   auto *Top = Mod->getTopLevelModule();
2428   if (Top != WritingModule &&
2429       (getLangOpts().CompilingPCH ||
2430        !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
2431     return 0;
2432
2433   return SubmoduleIDs[Mod] = NextSubmoduleID++;
2434 }
2435
2436 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2437   // FIXME: This can easily happen, if we have a reference to a submodule that
2438   // did not result in us loading a module file for that submodule. For
2439   // instance, a cross-top-level-module 'conflict' declaration will hit this.
2440   unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2441   assert((ID || !Mod) &&
2442          "asked for module ID for non-local, non-imported module");
2443   return ID;
2444 }
2445
2446 /// Compute the number of modules within the given tree (including the
2447 /// given module).
2448 static unsigned getNumberOfModules(Module *Mod) {
2449   unsigned ChildModules = 0;
2450   for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
2451        Sub != SubEnd; ++Sub)
2452     ChildModules += getNumberOfModules(*Sub);
2453
2454   return ChildModules + 1;
2455 }
2456
2457 void ASTWriter::WriteSubmodules(Module *WritingModule) {
2458   // Enter the submodule description block.
2459   Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2460
2461   // Write the abbreviations needed for the submodules block.
2462   using namespace llvm;
2463
2464   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2465   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2466   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2467   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2468   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
2469   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2470   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2471   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2472   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2473   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2474   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2475   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2476   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2477   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2478   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2479   unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2480
2481   Abbrev = std::make_shared<BitCodeAbbrev>();
2482   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2483   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2484   unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2485
2486   Abbrev = std::make_shared<BitCodeAbbrev>();
2487   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2488   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2489   unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2490
2491   Abbrev = std::make_shared<BitCodeAbbrev>();
2492   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2493   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2494   unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2495
2496   Abbrev = std::make_shared<BitCodeAbbrev>();
2497   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2498   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2499   unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2500
2501   Abbrev = std::make_shared<BitCodeAbbrev>();
2502   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2503   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2504   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Feature
2505   unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2506
2507   Abbrev = std::make_shared<BitCodeAbbrev>();
2508   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2509   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2510   unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2511
2512   Abbrev = std::make_shared<BitCodeAbbrev>();
2513   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2514   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2515   unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2516
2517   Abbrev = std::make_shared<BitCodeAbbrev>();
2518   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2519   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2520   unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2521
2522   Abbrev = std::make_shared<BitCodeAbbrev>();
2523   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2524   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2525   unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2526
2527   Abbrev = std::make_shared<BitCodeAbbrev>();
2528   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2529   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2530   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Name
2531   unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2532
2533   Abbrev = std::make_shared<BitCodeAbbrev>();
2534   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2535   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2536   unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2537
2538   Abbrev = std::make_shared<BitCodeAbbrev>();
2539   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2540   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));  // Other module
2541   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Message
2542   unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2543
2544   Abbrev = std::make_shared<BitCodeAbbrev>();
2545   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2546   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2547   unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2548
2549   // Write the submodule metadata block.
2550   RecordData::value_type Record[] = {
2551       getNumberOfModules(WritingModule),
2552       FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2553   Stream.EmitRecord(SUBMODULE_METADATA, Record);
2554
2555   // Write all of the submodules.
2556   std::queue<Module *> Q;
2557   Q.push(WritingModule);
2558   while (!Q.empty()) {
2559     Module *Mod = Q.front();
2560     Q.pop();
2561     unsigned ID = getSubmoduleID(Mod);
2562
2563     uint64_t ParentID = 0;
2564     if (Mod->Parent) {
2565       assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2566       ParentID = SubmoduleIDs[Mod->Parent];
2567     }
2568
2569     // Emit the definition of the block.
2570     {
2571       RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2572                                          ID,
2573                                          ParentID,
2574                                          (RecordData::value_type)Mod->Kind,
2575                                          Mod->IsFramework,
2576                                          Mod->IsExplicit,
2577                                          Mod->IsSystem,
2578                                          Mod->IsExternC,
2579                                          Mod->InferSubmodules,
2580                                          Mod->InferExplicitSubmodules,
2581                                          Mod->InferExportWildcard,
2582                                          Mod->ConfigMacrosExhaustive,
2583                                          Mod->ModuleMapIsPrivate};
2584       Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2585     }
2586
2587     // Emit the requirements.
2588     for (const auto &R : Mod->Requirements) {
2589       RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2590       Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2591     }
2592
2593     // Emit the umbrella header, if there is one.
2594     if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
2595       RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2596       Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2597                                 UmbrellaHeader.NameAsWritten);
2598     } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
2599       RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2600       Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2601                                 UmbrellaDir.NameAsWritten);
2602     }
2603
2604     // Emit the headers.
2605     struct {
2606       unsigned RecordKind;
2607       unsigned Abbrev;
2608       Module::HeaderKind HeaderKind;
2609     } HeaderLists[] = {
2610       {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2611       {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2612       {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2613       {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2614         Module::HK_PrivateTextual},
2615       {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2616     };
2617     for (auto &HL : HeaderLists) {
2618       RecordData::value_type Record[] = {HL.RecordKind};
2619       for (auto &H : Mod->Headers[HL.HeaderKind])
2620         Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2621     }
2622
2623     // Emit the top headers.
2624     {
2625       auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2626       RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2627       for (auto *H : TopHeaders)
2628         Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
2629     }
2630
2631     // Emit the imports.
2632     if (!Mod->Imports.empty()) {
2633       RecordData Record;
2634       for (auto *I : Mod->Imports)
2635         Record.push_back(getSubmoduleID(I));
2636       Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2637     }
2638
2639     // Emit the exports.
2640     if (!Mod->Exports.empty()) {
2641       RecordData Record;
2642       for (const auto &E : Mod->Exports) {
2643         // FIXME: This may fail; we don't require that all exported modules
2644         // are local or imported.
2645         Record.push_back(getSubmoduleID(E.getPointer()));
2646         Record.push_back(E.getInt());
2647       }
2648       Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2649     }
2650
2651     //FIXME: How do we emit the 'use'd modules?  They may not be submodules.
2652     // Might be unnecessary as use declarations are only used to build the
2653     // module itself.
2654
2655     // Emit the link libraries.
2656     for (const auto &LL : Mod->LinkLibraries) {
2657       RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2658                                          LL.IsFramework};
2659       Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2660     }
2661
2662     // Emit the conflicts.
2663     for (const auto &C : Mod->Conflicts) {
2664       // FIXME: This may fail; we don't require that all conflicting modules
2665       // are local or imported.
2666       RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2667                                          getSubmoduleID(C.Other)};
2668       Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2669     }
2670
2671     // Emit the configuration macros.
2672     for (const auto &CM : Mod->ConfigMacros) {
2673       RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2674       Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
2675     }
2676
2677     // Emit the initializers, if any.
2678     RecordData Inits;
2679     for (Decl *D : Context->getModuleInitializers(Mod))
2680       Inits.push_back(GetDeclRef(D));
2681     if (!Inits.empty())
2682       Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2683
2684     // Emit the name of the re-exported module, if any.
2685     if (!Mod->ExportAsModule.empty()) {
2686       RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
2687       Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
2688     }
2689
2690     // Queue up the submodules of this module.
2691     for (auto *M : Mod->submodules())
2692       Q.push(M);
2693   }
2694
2695   Stream.ExitBlock();
2696
2697   assert((NextSubmoduleID - FirstSubmoduleID ==
2698           getNumberOfModules(WritingModule)) &&
2699          "Wrong # of submodules; found a reference to a non-local, "
2700          "non-imported submodule?");
2701 }
2702
2703 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2704                                               bool isModule) {
2705   llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2706       DiagStateIDMap;
2707   unsigned CurrID = 0;
2708   RecordData Record;
2709
2710   auto EncodeDiagStateFlags =
2711       [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
2712     unsigned Result = (unsigned)DS->ExtBehavior;
2713     for (unsigned Val :
2714          {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
2715           (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
2716           (unsigned)DS->SuppressSystemWarnings})
2717       Result = (Result << 1) | Val;
2718     return Result;
2719   };
2720
2721   unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
2722   Record.push_back(Flags);
2723
2724   auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2725                           bool IncludeNonPragmaStates) {
2726     // Ensure that the diagnostic state wasn't modified since it was created.
2727     // We will not correctly round-trip this information otherwise.
2728     assert(Flags == EncodeDiagStateFlags(State) &&
2729            "diag state flags vary in single AST file");
2730
2731     unsigned &DiagStateID = DiagStateIDMap[State];
2732     Record.push_back(DiagStateID);
2733
2734     if (DiagStateID == 0) {
2735       DiagStateID = ++CurrID;
2736
2737       // Add a placeholder for the number of mappings.
2738       auto SizeIdx = Record.size();
2739       Record.emplace_back();
2740       for (const auto &I : *State) {
2741         if (I.second.isPragma() || IncludeNonPragmaStates) {
2742           Record.push_back(I.first);
2743           Record.push_back(I.second.serialize());
2744         }
2745       }
2746       // Update the placeholder.
2747       Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
2748     }
2749   };
2750
2751   AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2752
2753   // Reserve a spot for the number of locations with state transitions.
2754   auto NumLocationsIdx = Record.size();
2755   Record.emplace_back();
2756
2757   // Emit the state transitions.
2758   unsigned NumLocations = 0;
2759   for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2760     if (!FileIDAndFile.first.isValid() ||
2761         !FileIDAndFile.second.HasLocalTransitions)
2762       continue;
2763     ++NumLocations;
2764
2765     SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
2766     assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
2767     AddSourceLocation(Loc, Record);
2768
2769     Record.push_back(FileIDAndFile.second.StateTransitions.size());
2770     for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2771       Record.push_back(StatePoint.Offset);
2772       AddDiagState(StatePoint.State, false);
2773     }
2774   }
2775
2776   // Backpatch the number of locations.
2777   Record[NumLocationsIdx] = NumLocations;
2778
2779   // Emit CurDiagStateLoc.  Do it last in order to match source order.
2780   //
2781   // This also protects against a hypothetical corner case with simulating
2782   // -Werror settings for implicit modules in the ASTReader, where reading
2783   // CurDiagState out of context could change whether warning pragmas are
2784   // treated as errors.
2785   AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2786   AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2787
2788   Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
2789 }
2790
2791 //===----------------------------------------------------------------------===//
2792 // Type Serialization
2793 //===----------------------------------------------------------------------===//
2794
2795 /// Write the representation of a type to the AST stream.
2796 void ASTWriter::WriteType(QualType T) {
2797   TypeIdx &IdxRef = TypeIdxs[T];
2798   if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2799     IdxRef = TypeIdx(NextTypeID++);
2800   TypeIdx Idx = IdxRef;
2801
2802   assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
2803
2804   // Emit the type's representation.
2805   uint64_t Offset = ASTTypeWriter(*this).write(T);
2806
2807   // Record the offset for this type.
2808   unsigned Index = Idx.getIndex() - FirstTypeID;
2809   if (TypeOffsets.size() == Index)
2810     TypeOffsets.push_back(Offset);
2811   else if (TypeOffsets.size() < Index) {
2812     TypeOffsets.resize(Index + 1);
2813     TypeOffsets[Index] = Offset;
2814   } else {
2815     llvm_unreachable("Types emitted in wrong order");
2816   }
2817 }
2818
2819 //===----------------------------------------------------------------------===//
2820 // Declaration Serialization
2821 //===----------------------------------------------------------------------===//
2822
2823 /// Write the block containing all of the declaration IDs
2824 /// lexically declared within the given DeclContext.
2825 ///
2826 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2827 /// bitstream, or 0 if no block was written.
2828 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
2829                                                  DeclContext *DC) {
2830   if (DC->decls_empty())
2831     return 0;
2832
2833   uint64_t Offset = Stream.GetCurrentBitNo();
2834   SmallVector<uint32_t, 128> KindDeclPairs;
2835   for (const auto *D : DC->decls()) {
2836     KindDeclPairs.push_back(D->getKind());
2837     KindDeclPairs.push_back(GetDeclRef(D));
2838   }
2839
2840   ++NumLexicalDeclContexts;
2841   RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
2842   Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2843                             bytes(KindDeclPairs));
2844   return Offset;
2845 }
2846
2847 void ASTWriter::WriteTypeDeclOffsets() {
2848   using namespace llvm;
2849
2850   // Write the type offsets array
2851   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2852   Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
2853   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2854   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
2855   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2856   unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2857   {
2858     RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2859                                        FirstTypeID - NUM_PREDEF_TYPE_IDS};
2860     Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2861   }
2862
2863   // Write the declaration offsets array
2864   Abbrev = std::make_shared<BitCodeAbbrev>();
2865   Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
2866   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2867   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
2868   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2869   unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2870   {
2871     RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2872                                        FirstDeclID - NUM_PREDEF_DECL_IDS};
2873     Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2874   }
2875 }
2876
2877 void ASTWriter::WriteFileDeclIDsMap() {
2878   using namespace llvm;
2879
2880   SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2881       FileDeclIDs.begin(), FileDeclIDs.end());
2882   llvm::sort(SortedFileDeclIDs, llvm::less_first());
2883
2884   // Join the vectors of DeclIDs from all files.
2885   SmallVector<DeclID, 256> FileGroupedDeclIDs;
2886   for (auto &FileDeclEntry : SortedFileDeclIDs) {
2887     DeclIDInFileInfo &Info = *FileDeclEntry.second;
2888     Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2889     for (auto &LocDeclEntry : Info.DeclIDs)
2890       FileGroupedDeclIDs.push_back(LocDeclEntry.second);
2891   }
2892
2893   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2894   Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2895   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2896   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2897   unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
2898   RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2899                                      FileGroupedDeclIDs.size()};
2900   Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
2901 }
2902
2903 void ASTWriter::WriteComments() {
2904   Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
2905   auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
2906   if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
2907     return;
2908   RecordData Record;
2909   for (const auto &FO : Context->Comments.OrderedComments) {
2910     for (const auto &OC : FO.second) {
2911       const RawComment *I = OC.second;
2912       Record.clear();
2913       AddSourceRange(I->getSourceRange(), Record);
2914       Record.push_back(I->getKind());
2915       Record.push_back(I->isTrailingComment());
2916       Record.push_back(I->isAlmostTrailingComment());
2917       Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2918     }
2919   }
2920 }
2921
2922 //===----------------------------------------------------------------------===//
2923 // Global Method Pool and Selector Serialization
2924 //===----------------------------------------------------------------------===//
2925
2926 namespace {
2927
2928 // Trait used for the on-disk hash table used in the method pool.
2929 class ASTMethodPoolTrait {
2930   ASTWriter &Writer;
2931
2932 public:
2933   using key_type = Selector;
2934   using key_type_ref = key_type;
2935
2936   struct data_type {
2937     SelectorID ID;
2938     ObjCMethodList Instance, Factory;
2939   };
2940   using data_type_ref = const data_type &;
2941
2942   using hash_value_type = unsigned;
2943   using offset_type = unsigned;
2944
2945   explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
2946
2947   static hash_value_type ComputeHash(Selector Sel) {
2948     return serialization::ComputeHash(Sel);
2949   }
2950
2951   std::pair<unsigned, unsigned>
2952     EmitKeyDataLength(raw_ostream& Out, Selector Sel,
2953                       data_type_ref Methods) {
2954     using namespace llvm::support;
2955
2956     endian::Writer LE(Out, little);
2957     unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2958     LE.write<uint16_t>(KeyLen);
2959     unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2960     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2961          Method = Method->getNext())
2962       if (Method->getMethod())
2963         DataLen += 4;
2964     for (const ObjCMethodList *Method = &Methods.Factory; Method;
2965          Method = Method->getNext())
2966       if (Method->getMethod())
2967         DataLen += 4;
2968     LE.write<uint16_t>(DataLen);
2969     return std::make_pair(KeyLen, DataLen);
2970   }
2971
2972   void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
2973     using namespace llvm::support;
2974
2975     endian::Writer LE(Out, little);
2976     uint64_t Start = Out.tell();
2977     assert((Start >> 32) == 0 && "Selector key offset too large");
2978     Writer.SetSelectorOffset(Sel, Start);
2979     unsigned N = Sel.getNumArgs();
2980     LE.write<uint16_t>(N);
2981     if (N == 0)
2982       N = 1;
2983     for (unsigned I = 0; I != N; ++I)
2984       LE.write<uint32_t>(
2985           Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2986   }
2987
2988   void EmitData(raw_ostream& Out, key_type_ref,
2989                 data_type_ref Methods, unsigned DataLen) {
2990     using namespace llvm::support;
2991
2992     endian::Writer LE(Out, little);
2993     uint64_t Start = Out.tell(); (void)Start;
2994     LE.write<uint32_t>(Methods.ID);
2995     unsigned NumInstanceMethods = 0;
2996     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2997          Method = Method->getNext())
2998       if (Method->getMethod())
2999         ++NumInstanceMethods;
3000
3001     unsigned NumFactoryMethods = 0;
3002     for (const ObjCMethodList *Method = &Methods.Factory; Method;
3003          Method = Method->getNext())
3004       if (Method->getMethod())
3005         ++NumFactoryMethods;
3006
3007     unsigned InstanceBits = Methods.Instance.getBits();
3008     assert(InstanceBits < 4);
3009     unsigned InstanceHasMoreThanOneDeclBit =
3010         Methods.Instance.hasMoreThanOneDecl();
3011     unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3012                                 (InstanceHasMoreThanOneDeclBit << 2) |
3013                                 InstanceBits;
3014     unsigned FactoryBits = Methods.Factory.getBits();
3015     assert(FactoryBits < 4);
3016     unsigned FactoryHasMoreThanOneDeclBit =
3017         Methods.Factory.hasMoreThanOneDecl();
3018     unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3019                                (FactoryHasMoreThanOneDeclBit << 2) |
3020                                FactoryBits;
3021     LE.write<uint16_t>(FullInstanceBits);
3022     LE.write<uint16_t>(FullFactoryBits);
3023     for (const ObjCMethodList *Method = &Methods.Instance; Method;
3024          Method = Method->getNext())
3025       if (Method->getMethod())
3026         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3027     for (const ObjCMethodList *Method = &Methods.Factory; Method;
3028          Method = Method->getNext())
3029       if (Method->getMethod())
3030         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3031
3032     assert(Out.tell() - Start == DataLen && "Data length is wrong");
3033   }
3034 };
3035
3036 } // namespace
3037
3038 /// Write ObjC data: selectors and the method pool.
3039 ///
3040 /// The method pool contains both instance and factory methods, stored
3041 /// in an on-disk hash table indexed by the selector. The hash table also
3042 /// contains an empty entry for every other selector known to Sema.
3043 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3044   using namespace llvm;
3045
3046   // Do we have to do anything at all?
3047   if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
3048     return;
3049   unsigned NumTableEntries = 0;
3050   // Create and write out the blob that contains selectors and the method pool.
3051   {
3052     llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3053     ASTMethodPoolTrait Trait(*this);
3054
3055     // Create the on-disk hash table representation. We walk through every
3056     // selector we've seen and look it up in the method pool.
3057     SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3058     for (auto &SelectorAndID : SelectorIDs) {
3059       Selector S = SelectorAndID.first;
3060       SelectorID ID = SelectorAndID.second;
3061       Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
3062       ASTMethodPoolTrait::data_type Data = {
3063         ID,
3064         ObjCMethodList(),
3065         ObjCMethodList()
3066       };
3067       if (F != SemaRef.MethodPool.end()) {
3068         Data.Instance = F->second.first;
3069         Data.Factory = F->second.second;
3070       }
3071       // Only write this selector if it's not in an existing AST or something
3072       // changed.
3073       if (Chain && ID < FirstSelectorID) {
3074         // Selector already exists. Did it change?
3075         bool changed = false;
3076         for (ObjCMethodList *M = &Data.Instance;
3077              !changed && M && M->getMethod(); M = M->getNext()) {
3078           if (!M->getMethod()->isFromASTFile())
3079             changed = true;
3080         }
3081         for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
3082              M = M->getNext()) {
3083           if (!M->getMethod()->isFromASTFile())
3084             changed = true;
3085         }
3086         if (!changed)
3087           continue;
3088       } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3089         // A new method pool entry.
3090         ++NumTableEntries;
3091       }
3092       Generator.insert(S, Data, Trait);
3093     }
3094
3095     // Create the on-disk hash table in a buffer.
3096     SmallString<4096> MethodPool;
3097     uint32_t BucketOffset;
3098     {
3099       using namespace llvm::support;
3100
3101       ASTMethodPoolTrait Trait(*this);
3102       llvm::raw_svector_ostream Out(MethodPool);
3103       // Make sure that no bucket is at offset 0
3104       endian::write<uint32_t>(Out, 0, little);
3105       BucketOffset = Generator.Emit(Out, Trait);
3106     }
3107
3108     // Create a blob abbreviation
3109     auto Abbrev = std::make_shared<BitCodeAbbrev>();
3110     Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3111     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3112     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3113     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3114     unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3115
3116     // Write the method pool
3117     {
3118       RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3119                                          NumTableEntries};
3120       Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3121     }
3122
3123     // Create a blob abbreviation for the selector table offsets.
3124     Abbrev = std::make_shared<BitCodeAbbrev>();
3125     Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3126     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3127     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3128     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3129     unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3130
3131     // Write the selector offsets table.
3132     {
3133       RecordData::value_type Record[] = {
3134           SELECTOR_OFFSETS, SelectorOffsets.size(),
3135           FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3136       Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3137                                 bytes(SelectorOffsets));
3138     }
3139   }
3140 }
3141
3142 /// Write the selectors referenced in @selector expression into AST file.
3143 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3144   using namespace llvm;
3145
3146   if (SemaRef.ReferencedSelectors.empty())
3147     return;
3148
3149   RecordData Record;
3150   ASTRecordWriter Writer(*this, Record);
3151
3152   // Note: this writes out all references even for a dependent AST. But it is
3153   // very tricky to fix, and given that @selector shouldn't really appear in
3154   // headers, probably not worth it. It's not a correctness issue.
3155   for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3156     Selector Sel = SelectorAndLocation.first;
3157     SourceLocation Loc = SelectorAndLocation.second;
3158     Writer.AddSelectorRef(Sel);
3159     Writer.AddSourceLocation(Loc);
3160   }
3161   Writer.Emit(REFERENCED_SELECTOR_POOL);
3162 }
3163
3164 //===----------------------------------------------------------------------===//
3165 // Identifier Table Serialization
3166 //===----------------------------------------------------------------------===//
3167
3168 /// Determine the declaration that should be put into the name lookup table to
3169 /// represent the given declaration in this module. This is usually D itself,
3170 /// but if D was imported and merged into a local declaration, we want the most
3171 /// recent local declaration instead. The chosen declaration will be the most
3172 /// recent declaration in any module that imports this one.
3173 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3174                                         NamedDecl *D) {
3175   if (!LangOpts.Modules || !D->isFromASTFile())
3176     return D;
3177
3178   if (Decl *Redecl = D->getPreviousDecl()) {
3179     // For Redeclarable decls, a prior declaration might be local.
3180     for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3181       // If we find a local decl, we're done.
3182       if (!Redecl->isFromASTFile()) {
3183         // Exception: in very rare cases (for injected-class-names), not all
3184         // redeclarations are in the same semantic context. Skip ones in a
3185         // different context. They don't go in this lookup table at all.
3186         if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3187                 D->getDeclContext()->getRedeclContext()))
3188           continue;
3189         return cast<NamedDecl>(Redecl);
3190       }
3191
3192       // If we find a decl from a (chained-)PCH stop since we won't find a
3193       // local one.
3194       if (Redecl->getOwningModuleID() == 0)
3195         break;
3196     }
3197   } else if (Decl *First = D->getCanonicalDecl()) {
3198     // For Mergeable decls, the first decl might be local.
3199     if (!First->isFromASTFile())
3200       return cast<NamedDecl>(First);
3201   }
3202
3203   // All declarations are imported. Our most recent declaration will also be
3204   // the most recent one in anyone who imports us.
3205   return D;
3206 }
3207
3208 namespace {
3209
3210 class ASTIdentifierTableTrait {
3211   ASTWriter &Writer;
3212   Preprocessor &PP;
3213   IdentifierResolver &IdResolver;
3214   bool IsModule;
3215   bool NeedDecls;
3216   ASTWriter::RecordData *InterestingIdentifierOffsets;
3217
3218   /// Determines whether this is an "interesting" identifier that needs a
3219   /// full IdentifierInfo structure written into the hash table. Notably, this
3220   /// doesn't check whether the name has macros defined; use PublicMacroIterator
3221   /// to check that.
3222   bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3223     if (MacroOffset ||
3224         II->isPoisoned() ||
3225         (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
3226         II->hasRevertedTokenIDToIdentifier() ||
3227         (NeedDecls && II->getFETokenInfo()))
3228       return true;
3229
3230     return false;
3231   }
3232
3233 public:
3234   using key_type = IdentifierInfo *;
3235   using key_type_ref = key_type;
3236
3237   using data_type = IdentID;
3238   using data_type_ref = data_type;
3239
3240   using hash_value_type = unsigned;
3241   using offset_type = unsigned;
3242
3243   ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3244                           IdentifierResolver &IdResolver, bool IsModule,
3245                           ASTWriter::RecordData *InterestingIdentifierOffsets)
3246       : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3247         NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3248         InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3249
3250   bool needDecls() const { return NeedDecls; }
3251
3252   static hash_value_type ComputeHash(const IdentifierInfo* II) {
3253     return llvm::djbHash(II->getName());
3254   }
3255
3256   bool isInterestingIdentifier(const IdentifierInfo *II) {
3257     auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3258     return isInterestingIdentifier(II, MacroOffset);
3259   }
3260
3261   bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3262     return isInterestingIdentifier(II, 0);
3263   }
3264
3265   std::pair<unsigned, unsigned>
3266   EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3267     unsigned KeyLen = II->getLength() + 1;
3268     unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3269     auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3270     if (isInterestingIdentifier(II, MacroOffset)) {
3271       DataLen += 2; // 2 bytes for builtin ID
3272       DataLen += 2; // 2 bytes for flags
3273       if (MacroOffset)
3274         DataLen += 4; // MacroDirectives offset.
3275
3276       if (NeedDecls) {
3277         for (IdentifierResolver::iterator D = IdResolver.begin(II),
3278                                        DEnd = IdResolver.end();
3279              D != DEnd; ++D)
3280           DataLen += 4;
3281       }
3282     }
3283
3284     using namespace llvm::support;
3285
3286     endian::Writer LE(Out, little);
3287
3288     assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
3289     LE.write<uint16_t>(DataLen);
3290     // We emit the key length after the data length so that every
3291     // string is preceded by a 16-bit length. This matches the PTH
3292     // format for storing identifiers.
3293     LE.write<uint16_t>(KeyLen);
3294     return std::make_pair(KeyLen, DataLen);
3295   }
3296
3297   void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3298                unsigned KeyLen) {
3299     // Record the location of the key data.  This is used when generating
3300     // the mapping from persistent IDs to strings.
3301     Writer.SetIdentifierOffset(II, Out.tell());
3302
3303     // Emit the offset of the key/data length information to the interesting
3304     // identifiers table if necessary.
3305     if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3306       InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3307
3308     Out.write(II->getNameStart(), KeyLen);
3309   }
3310
3311   void EmitData(raw_ostream& Out, IdentifierInfo* II,
3312                 IdentID ID, unsigned) {
3313     using namespace llvm::support;
3314
3315     endian::Writer LE(Out, little);
3316
3317     auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3318     if (!isInterestingIdentifier(II, MacroOffset)) {
3319       LE.write<uint32_t>(ID << 1);
3320       return;
3321     }
3322
3323     LE.write<uint32_t>((ID << 1) | 0x01);
3324     uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3325     assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3326     LE.write<uint16_t>(Bits);
3327     Bits = 0;
3328     bool HadMacroDefinition = MacroOffset != 0;
3329     Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3330     Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3331     Bits = (Bits << 1) | unsigned(II->isPoisoned());
3332     Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
3333     Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3334     Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3335     LE.write<uint16_t>(Bits);
3336
3337     if (HadMacroDefinition)
3338       LE.write<uint32_t>(MacroOffset);
3339
3340     if (NeedDecls) {
3341       // Emit the declaration IDs in reverse order, because the
3342       // IdentifierResolver provides the declarations as they would be
3343       // visible (e.g., the function "stat" would come before the struct
3344       // "stat"), but the ASTReader adds declarations to the end of the list
3345       // (so we need to see the struct "stat" before the function "stat").
3346       // Only emit declarations that aren't from a chained PCH, though.
3347       SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3348                                          IdResolver.end());
3349       for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3350                                                           DEnd = Decls.rend();
3351            D != DEnd; ++D)
3352         LE.write<uint32_t>(
3353             Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3354     }
3355   }
3356 };
3357
3358 } // namespace
3359
3360 /// Write the identifier table into the AST file.
3361 ///
3362 /// The identifier table consists of a blob containing string data
3363 /// (the actual identifiers themselves) and a separate "offsets" index
3364 /// that maps identifier IDs to locations within the blob.
3365 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3366                                      IdentifierResolver &IdResolver,
3367                                      bool IsModule) {
3368   using namespace llvm;
3369
3370   RecordData InterestingIdents;
3371
3372   // Create and write out the blob that contains the identifier
3373   // strings.
3374   {
3375     llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3376     ASTIdentifierTableTrait Trait(
3377         *this, PP, IdResolver, IsModule,
3378         (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
3379
3380     // Look for any identifiers that were named while processing the
3381     // headers, but are otherwise not needed. We add these to the hash
3382     // table to enable checking of the predefines buffer in the case
3383     // where the user adds new macro definitions when building the AST
3384     // file.
3385     SmallVector<const IdentifierInfo *, 128> IIs;
3386     for (const auto &ID : PP.getIdentifierTable())
3387       IIs.push_back(ID.second);
3388     // Sort the identifiers lexicographically before getting them references so
3389     // that their order is stable.
3390     llvm::sort(IIs, llvm::deref<std::less<>>());
3391     for (const IdentifierInfo *II : IIs)
3392       if (Trait.isInterestingNonMacroIdentifier(II))
3393         getIdentifierRef(II);
3394
3395     // Create the on-disk hash table representation. We only store offsets
3396     // for identifiers that appear here for the first time.
3397     IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3398     for (auto IdentIDPair : IdentifierIDs) {
3399       auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3400       IdentID ID = IdentIDPair.second;
3401       assert(II && "NULL identifier in identifier table");
3402       // Write out identifiers if either the ID is local or the identifier has
3403       // changed since it was loaded.
3404       if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3405           || II->hasChangedSinceDeserialization() ||
3406           (Trait.needDecls() &&
3407            II->hasFETokenInfoChangedSinceDeserialization()))
3408         Generator.insert(II, ID, Trait);
3409     }
3410
3411     // Create the on-disk hash table in a buffer.
3412     SmallString<4096> IdentifierTable;
3413     uint32_t BucketOffset;
3414     {
3415       using namespace llvm::support;
3416
3417       llvm::raw_svector_ostream Out(IdentifierTable);
3418       // Make sure that no bucket is at offset 0
3419       endian::write<uint32_t>(Out, 0, little);
3420       BucketOffset = Generator.Emit(Out, Trait);
3421     }
3422
3423     // Create a blob abbreviation
3424     auto Abbrev = std::make_shared<BitCodeAbbrev>();
3425     Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3426     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3427     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3428     unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3429
3430     // Write the identifier table
3431     RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3432     Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3433   }
3434
3435   // Write the offsets table for identifier IDs.
3436   auto Abbrev = std::make_shared<BitCodeAbbrev>();
3437   Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3438   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3439   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3440   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3441   unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3442
3443 #ifndef NDEBUG
3444   for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3445     assert(IdentifierOffsets[I] && "Missing identifier offset?");
3446 #endif
3447
3448   RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3449                                      IdentifierOffsets.size(),
3450                                      FirstIdentID - NUM_PREDEF_IDENT_IDS};
3451   Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3452                             bytes(IdentifierOffsets));
3453
3454   // In C++, write the list of interesting identifiers (those that are
3455   // defined as macros, poisoned, or similar unusual things).
3456   if (!InterestingIdents.empty())
3457     Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3458 }
3459
3460 //===----------------------------------------------------------------------===//
3461 // DeclContext's Name Lookup Table Serialization
3462 //===----------------------------------------------------------------------===//
3463
3464 namespace {
3465
3466 // Trait used for the on-disk hash table used in the method pool.
3467 class ASTDeclContextNameLookupTrait {
3468   ASTWriter &Writer;
3469   llvm::SmallVector<DeclID, 64> DeclIDs;
3470
3471 public:
3472   using key_type = DeclarationNameKey;
3473   using key_type_ref = key_type;
3474
3475   /// A start and end index into DeclIDs, representing a sequence of decls.
3476   using data_type = std::pair<unsigned, unsigned>;
3477   using data_type_ref = const data_type &;
3478
3479   using hash_value_type = unsigned;
3480   using offset_type = unsigned;
3481
3482   explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3483
3484   template<typename Coll>
3485   data_type getData(const Coll &Decls) {
3486     unsigned Start = DeclIDs.size();
3487     for (NamedDecl *D : Decls) {
3488       DeclIDs.push_back(
3489           Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3490     }
3491     return std::make_pair(Start, DeclIDs.size());
3492   }
3493
3494   data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3495     unsigned Start = DeclIDs.size();
3496     for (auto ID : FromReader)
3497       DeclIDs.push_back(ID);
3498     return std::make_pair(Start, DeclIDs.size());
3499   }
3500
3501   static bool EqualKey(key_type_ref a, key_type_ref b) {
3502     return a == b;
3503   }
3504
3505   hash_value_type ComputeHash(DeclarationNameKey Name) {
3506     return Name.getHash();
3507   }
3508
3509   void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3510     assert(Writer.hasChain() &&
3511            "have reference to loaded module file but no chain?");
3512
3513     using namespace llvm::support;
3514
3515     endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
3516   }
3517
3518   std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3519                                                   DeclarationNameKey Name,
3520                                                   data_type_ref Lookup) {
3521     using namespace llvm::support;
3522
3523     endian::Writer LE(Out, little);
3524     unsigned KeyLen = 1;
3525     switch (Name.getKind()) {
3526     case DeclarationName::Identifier:
3527     case DeclarationName::ObjCZeroArgSelector:
3528     case DeclarationName::ObjCOneArgSelector:
3529     case DeclarationName::ObjCMultiArgSelector:
3530     case DeclarationName::CXXLiteralOperatorName:
3531     case DeclarationName::CXXDeductionGuideName:
3532       KeyLen += 4;
3533       break;
3534     case DeclarationName::CXXOperatorName:
3535       KeyLen += 1;
3536       break;
3537     case DeclarationName::CXXConstructorName:
3538     case DeclarationName::CXXDestructorName:
3539     case DeclarationName::CXXConversionFunctionName:
3540     case DeclarationName::CXXUsingDirective:
3541       break;
3542     }
3543     LE.write<uint16_t>(KeyLen);
3544
3545     // 4 bytes for each DeclID.
3546     unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3547     assert(uint16_t(DataLen) == DataLen &&
3548            "too many decls for serialized lookup result");
3549     LE.write<uint16_t>(DataLen);
3550
3551     return std::make_pair(KeyLen, DataLen);
3552   }
3553
3554   void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3555     using namespace llvm::support;
3556
3557     endian::Writer LE(Out, little);
3558     LE.write<uint8_t>(Name.getKind());
3559     switch (Name.getKind()) {
3560     case DeclarationName::Identifier:
3561     case DeclarationName::CXXLiteralOperatorName:
3562     case DeclarationName::CXXDeductionGuideName:
3563       LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3564       return;
3565     case DeclarationName::ObjCZeroArgSelector:
3566     case DeclarationName::ObjCOneArgSelector:
3567     case DeclarationName::ObjCMultiArgSelector:
3568       LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3569       return;
3570     case DeclarationName::CXXOperatorName:
3571       assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3572              "Invalid operator?");
3573       LE.write<uint8_t>(Name.getOperatorKind());
3574       return;
3575     case DeclarationName::CXXConstructorName:
3576     case DeclarationName::CXXDestructorName:
3577     case DeclarationName::CXXConversionFunctionName:
3578     case DeclarationName::CXXUsingDirective:
3579       return;
3580     }
3581
3582     llvm_unreachable("Invalid name kind?");
3583   }
3584
3585   void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3586                 unsigned DataLen) {
3587     using namespace llvm::support;
3588
3589     endian::Writer LE(Out, little);
3590     uint64_t Start = Out.tell(); (void)Start;
3591     for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3592       LE.write<uint32_t>(DeclIDs[I]);
3593     assert(Out.tell() - Start == DataLen && "Data length is wrong");
3594   }
3595 };
3596
3597 } // namespace
3598
3599 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3600                                        DeclContext *DC) {
3601   return Result.hasExternalDecls() &&
3602          DC->hasNeedToReconcileExternalVisibleStorage();
3603 }
3604
3605 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3606                                                DeclContext *DC) {
3607   for (auto *D : Result.getLookupResult())
3608     if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3609       return false;
3610
3611   return true;
3612 }
3613
3614 void
3615 ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3616                                    llvm::SmallVectorImpl<char> &LookupTable) {
3617   assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3618          !ConstDC->hasLazyExternalLexicalLookups() &&
3619          "must call buildLookups first");
3620
3621   // FIXME: We need to build the lookups table, which is logically const.
3622   auto *DC = const_cast<DeclContext*>(ConstDC);
3623   assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3624
3625   // Create the on-disk hash table representation.
3626   MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3627                                 ASTDeclContextNameLookupTrait> Generator;
3628   ASTDeclContext