xref: /freebsd/contrib/llvm-project/clang/lib/Serialization/ASTWriter.cpp (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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 "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "MultiOnDiskHashTable.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTUnresolvedSet.h"
18 #include "clang/AST/AbstractTypeWriter.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclContextInternals.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/LambdaCapture.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/OpenMPClause.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateName.h"
35 #include "clang/AST/Type.h"
36 #include "clang/AST/TypeLocVisitor.h"
37 #include "clang/Basic/Diagnostic.h"
38 #include "clang/Basic/DiagnosticOptions.h"
39 #include "clang/Basic/FileManager.h"
40 #include "clang/Basic/FileSystemOptions.h"
41 #include "clang/Basic/IdentifierTable.h"
42 #include "clang/Basic/LLVM.h"
43 #include "clang/Basic/Lambda.h"
44 #include "clang/Basic/LangOptions.h"
45 #include "clang/Basic/Module.h"
46 #include "clang/Basic/ObjCRuntime.h"
47 #include "clang/Basic/OpenCLOptions.h"
48 #include "clang/Basic/SourceLocation.h"
49 #include "clang/Basic/SourceManager.h"
50 #include "clang/Basic/SourceManagerInternals.h"
51 #include "clang/Basic/Specifiers.h"
52 #include "clang/Basic/TargetInfo.h"
53 #include "clang/Basic/TargetOptions.h"
54 #include "clang/Basic/Version.h"
55 #include "clang/Lex/HeaderSearch.h"
56 #include "clang/Lex/HeaderSearchOptions.h"
57 #include "clang/Lex/MacroInfo.h"
58 #include "clang/Lex/ModuleMap.h"
59 #include "clang/Lex/PreprocessingRecord.h"
60 #include "clang/Lex/Preprocessor.h"
61 #include "clang/Lex/PreprocessorOptions.h"
62 #include "clang/Lex/Token.h"
63 #include "clang/Sema/IdentifierResolver.h"
64 #include "clang/Sema/ObjCMethodList.h"
65 #include "clang/Sema/Sema.h"
66 #include "clang/Sema/Weak.h"
67 #include "clang/Serialization/ASTBitCodes.h"
68 #include "clang/Serialization/ASTReader.h"
69 #include "clang/Serialization/ASTRecordWriter.h"
70 #include "clang/Serialization/InMemoryModuleCache.h"
71 #include "clang/Serialization/ModuleFile.h"
72 #include "clang/Serialization/ModuleFileExtension.h"
73 #include "clang/Serialization/SerializationDiagnostic.h"
74 #include "llvm/ADT/APFloat.h"
75 #include "llvm/ADT/APInt.h"
76 #include "llvm/ADT/APSInt.h"
77 #include "llvm/ADT/ArrayRef.h"
78 #include "llvm/ADT/DenseMap.h"
79 #include "llvm/ADT/Hashing.h"
80 #include "llvm/ADT/PointerIntPair.h"
81 #include "llvm/ADT/STLExtras.h"
82 #include "llvm/ADT/ScopeExit.h"
83 #include "llvm/ADT/SmallPtrSet.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/LEB128.h"
98 #include "llvm/Support/MemoryBuffer.h"
99 #include "llvm/Support/OnDiskHashTable.h"
100 #include "llvm/Support/Path.h"
101 #include "llvm/Support/SHA1.h"
102 #include "llvm/Support/TimeProfiler.h"
103 #include "llvm/Support/VersionTuple.h"
104 #include "llvm/Support/raw_ostream.h"
105 #include <algorithm>
106 #include <cassert>
107 #include <cstdint>
108 #include <cstdlib>
109 #include <cstring>
110 #include <ctime>
111 #include <limits>
112 #include <memory>
113 #include <optional>
114 #include <queue>
115 #include <tuple>
116 #include <utility>
117 #include <vector>
118 
119 using namespace clang;
120 using namespace clang::serialization;
121 
122 template <typename T, typename Allocator>
123 static StringRef bytes(const std::vector<T, Allocator> &v) {
124   if (v.empty()) return StringRef();
125   return StringRef(reinterpret_cast<const char*>(&v[0]),
126                          sizeof(T) * v.size());
127 }
128 
129 template <typename T>
130 static StringRef bytes(const SmallVectorImpl<T> &v) {
131   return StringRef(reinterpret_cast<const char*>(v.data()),
132                          sizeof(T) * v.size());
133 }
134 
135 static std::string bytes(const std::vector<bool> &V) {
136   std::string Str;
137   Str.reserve(V.size() / 8);
138   for (unsigned I = 0, E = V.size(); I < E;) {
139     char Byte = 0;
140     for (unsigned Bit = 0; Bit < 8 && I < E; ++Bit, ++I)
141       Byte |= V[I] << Bit;
142     Str += Byte;
143   }
144   return Str;
145 }
146 
147 //===----------------------------------------------------------------------===//
148 // Type serialization
149 //===----------------------------------------------------------------------===//
150 
151 static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
152   switch (id) {
153 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
154   case Type::CLASS_ID: return TYPE_##CODE_ID;
155 #include "clang/Serialization/TypeBitCodes.def"
156   case Type::Builtin:
157     llvm_unreachable("shouldn't be serializing a builtin type this way");
158   }
159   llvm_unreachable("bad type kind");
160 }
161 
162 namespace {
163 
164 std::set<const FileEntry *> GetAffectingModuleMaps(const Preprocessor &PP,
165                                                    Module *RootModule) {
166   SmallVector<const Module *> ModulesToProcess{RootModule};
167 
168   const HeaderSearch &HS = PP.getHeaderSearchInfo();
169 
170   SmallVector<OptionalFileEntryRef, 16> FilesByUID;
171   HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
172 
173   if (FilesByUID.size() > HS.header_file_size())
174     FilesByUID.resize(HS.header_file_size());
175 
176   for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
177     OptionalFileEntryRef File = FilesByUID[UID];
178     if (!File)
179       continue;
180 
181     const HeaderFileInfo *HFI =
182         HS.getExistingFileInfo(*File, /*WantExternal*/ false);
183     if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
184       continue;
185 
186     for (const auto &KH : HS.findResolvedModulesForHeader(*File)) {
187       if (!KH.getModule())
188         continue;
189       ModulesToProcess.push_back(KH.getModule());
190     }
191   }
192 
193   const ModuleMap &MM = HS.getModuleMap();
194   SourceManager &SourceMgr = PP.getSourceManager();
195 
196   std::set<const FileEntry *> ModuleMaps{};
197   auto CollectIncludingModuleMaps = [&](FileEntryRef F) {
198     if (!ModuleMaps.insert(F).second)
199       return;
200     FileID FID = SourceMgr.translateFile(F);
201     SourceLocation Loc = SourceMgr.getIncludeLoc(FID);
202     // The include location of inferred module maps can point into the header
203     // file that triggered the inferring. Cut off the walk if that's the case.
204     while (Loc.isValid() && isModuleMap(SourceMgr.getFileCharacteristic(Loc))) {
205       FID = SourceMgr.getFileID(Loc);
206       if (!ModuleMaps.insert(*SourceMgr.getFileEntryRefForID(FID)).second)
207         break;
208       Loc = SourceMgr.getIncludeLoc(FID);
209     }
210   };
211 
212   std::set<const Module *> ProcessedModules;
213   auto CollectIncludingMapsFromAncestors = [&](const Module *M) {
214     for (const Module *Mod = M; Mod; Mod = Mod->Parent) {
215       if (!ProcessedModules.insert(Mod).second)
216         break;
217       // The containing module map is affecting, because it's being pointed
218       // into by Module::DefinitionLoc.
219       if (auto ModuleMapFile = MM.getContainingModuleMapFile(Mod))
220         CollectIncludingModuleMaps(*ModuleMapFile);
221       // For inferred modules, the module map that allowed inferring is not in
222       // the include chain of the virtual containing module map file. It did
223       // affect the compilation, though.
224       if (auto ModuleMapFile = MM.getModuleMapFileForUniquing(Mod))
225         CollectIncludingModuleMaps(*ModuleMapFile);
226     }
227   };
228 
229   for (const Module *CurrentModule : ModulesToProcess) {
230     CollectIncludingMapsFromAncestors(CurrentModule);
231     for (const Module *ImportedModule : CurrentModule->Imports)
232       CollectIncludingMapsFromAncestors(ImportedModule);
233     for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses)
234       CollectIncludingMapsFromAncestors(UndeclaredModule);
235   }
236 
237   return ModuleMaps;
238 }
239 
240 class ASTTypeWriter {
241   ASTWriter &Writer;
242   ASTWriter::RecordData Record;
243   ASTRecordWriter BasicWriter;
244 
245 public:
246   ASTTypeWriter(ASTWriter &Writer)
247     : Writer(Writer), BasicWriter(Writer, Record) {}
248 
249   uint64_t write(QualType T) {
250     if (T.hasLocalNonFastQualifiers()) {
251       Qualifiers Qs = T.getLocalQualifiers();
252       BasicWriter.writeQualType(T.getLocalUnqualifiedType());
253       BasicWriter.writeQualifiers(Qs);
254       return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
255     }
256 
257     const Type *typePtr = T.getTypePtr();
258     serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
259     atw.write(typePtr);
260     return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
261                             /*abbrev*/ 0);
262   }
263 };
264 
265 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
266   using LocSeq = SourceLocationSequence;
267 
268   ASTRecordWriter &Record;
269   LocSeq *Seq;
270 
271   void addSourceLocation(SourceLocation Loc) {
272     Record.AddSourceLocation(Loc, Seq);
273   }
274   void addSourceRange(SourceRange Range) { Record.AddSourceRange(Range, Seq); }
275 
276 public:
277   TypeLocWriter(ASTRecordWriter &Record, LocSeq *Seq)
278       : Record(Record), Seq(Seq) {}
279 
280 #define ABSTRACT_TYPELOC(CLASS, PARENT)
281 #define TYPELOC(CLASS, PARENT) \
282     void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
283 #include "clang/AST/TypeLocNodes.def"
284 
285   void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
286   void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
287 };
288 
289 } // namespace
290 
291 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
292   // nothing to do
293 }
294 
295 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
296   addSourceLocation(TL.getBuiltinLoc());
297   if (TL.needsExtraLocalData()) {
298     Record.push_back(TL.getWrittenTypeSpec());
299     Record.push_back(static_cast<uint64_t>(TL.getWrittenSignSpec()));
300     Record.push_back(static_cast<uint64_t>(TL.getWrittenWidthSpec()));
301     Record.push_back(TL.hasModeAttr());
302   }
303 }
304 
305 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
306   addSourceLocation(TL.getNameLoc());
307 }
308 
309 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
310   addSourceLocation(TL.getStarLoc());
311 }
312 
313 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
314   // nothing to do
315 }
316 
317 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
318   // nothing to do
319 }
320 
321 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
322   addSourceLocation(TL.getCaretLoc());
323 }
324 
325 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
326   addSourceLocation(TL.getAmpLoc());
327 }
328 
329 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
330   addSourceLocation(TL.getAmpAmpLoc());
331 }
332 
333 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
334   addSourceLocation(TL.getStarLoc());
335   Record.AddTypeSourceInfo(TL.getClassTInfo());
336 }
337 
338 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
339   addSourceLocation(TL.getLBracketLoc());
340   addSourceLocation(TL.getRBracketLoc());
341   Record.push_back(TL.getSizeExpr() ? 1 : 0);
342   if (TL.getSizeExpr())
343     Record.AddStmt(TL.getSizeExpr());
344 }
345 
346 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
347   VisitArrayTypeLoc(TL);
348 }
349 
350 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
351   VisitArrayTypeLoc(TL);
352 }
353 
354 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
355   VisitArrayTypeLoc(TL);
356 }
357 
358 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
359                                             DependentSizedArrayTypeLoc TL) {
360   VisitArrayTypeLoc(TL);
361 }
362 
363 void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
364     DependentAddressSpaceTypeLoc TL) {
365   addSourceLocation(TL.getAttrNameLoc());
366   SourceRange range = TL.getAttrOperandParensRange();
367   addSourceLocation(range.getBegin());
368   addSourceLocation(range.getEnd());
369   Record.AddStmt(TL.getAttrExprOperand());
370 }
371 
372 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
373                                         DependentSizedExtVectorTypeLoc TL) {
374   addSourceLocation(TL.getNameLoc());
375 }
376 
377 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
378   addSourceLocation(TL.getNameLoc());
379 }
380 
381 void TypeLocWriter::VisitDependentVectorTypeLoc(
382     DependentVectorTypeLoc TL) {
383   addSourceLocation(TL.getNameLoc());
384 }
385 
386 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
387   addSourceLocation(TL.getNameLoc());
388 }
389 
390 void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
391   addSourceLocation(TL.getAttrNameLoc());
392   SourceRange range = TL.getAttrOperandParensRange();
393   addSourceLocation(range.getBegin());
394   addSourceLocation(range.getEnd());
395   Record.AddStmt(TL.getAttrRowOperand());
396   Record.AddStmt(TL.getAttrColumnOperand());
397 }
398 
399 void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
400     DependentSizedMatrixTypeLoc TL) {
401   addSourceLocation(TL.getAttrNameLoc());
402   SourceRange range = TL.getAttrOperandParensRange();
403   addSourceLocation(range.getBegin());
404   addSourceLocation(range.getEnd());
405   Record.AddStmt(TL.getAttrRowOperand());
406   Record.AddStmt(TL.getAttrColumnOperand());
407 }
408 
409 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
410   addSourceLocation(TL.getLocalRangeBegin());
411   addSourceLocation(TL.getLParenLoc());
412   addSourceLocation(TL.getRParenLoc());
413   addSourceRange(TL.getExceptionSpecRange());
414   addSourceLocation(TL.getLocalRangeEnd());
415   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
416     Record.AddDeclRef(TL.getParam(i));
417 }
418 
419 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
420   VisitFunctionTypeLoc(TL);
421 }
422 
423 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
424   VisitFunctionTypeLoc(TL);
425 }
426 
427 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
428   addSourceLocation(TL.getNameLoc());
429 }
430 
431 void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) {
432   addSourceLocation(TL.getNameLoc());
433 }
434 
435 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
436   addSourceLocation(TL.getNameLoc());
437 }
438 
439 void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
440   if (TL.getNumProtocols()) {
441     addSourceLocation(TL.getProtocolLAngleLoc());
442     addSourceLocation(TL.getProtocolRAngleLoc());
443   }
444   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
445     addSourceLocation(TL.getProtocolLoc(i));
446 }
447 
448 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
449   addSourceLocation(TL.getTypeofLoc());
450   addSourceLocation(TL.getLParenLoc());
451   addSourceLocation(TL.getRParenLoc());
452 }
453 
454 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
455   addSourceLocation(TL.getTypeofLoc());
456   addSourceLocation(TL.getLParenLoc());
457   addSourceLocation(TL.getRParenLoc());
458   Record.AddTypeSourceInfo(TL.getUnmodifiedTInfo());
459 }
460 
461 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
462   addSourceLocation(TL.getDecltypeLoc());
463   addSourceLocation(TL.getRParenLoc());
464 }
465 
466 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
467   addSourceLocation(TL.getKWLoc());
468   addSourceLocation(TL.getLParenLoc());
469   addSourceLocation(TL.getRParenLoc());
470   Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
471 }
472 
473 void ASTRecordWriter::AddConceptReference(const ConceptReference *CR) {
474   assert(CR);
475   AddNestedNameSpecifierLoc(CR->getNestedNameSpecifierLoc());
476   AddSourceLocation(CR->getTemplateKWLoc());
477   AddDeclarationNameInfo(CR->getConceptNameInfo());
478   AddDeclRef(CR->getFoundDecl());
479   AddDeclRef(CR->getNamedConcept());
480   push_back(CR->getTemplateArgsAsWritten() != nullptr);
481   if (CR->getTemplateArgsAsWritten())
482     AddASTTemplateArgumentListInfo(CR->getTemplateArgsAsWritten());
483 }
484 
485 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
486   addSourceLocation(TL.getNameLoc());
487   auto *CR = TL.getConceptReference();
488   Record.push_back(TL.isConstrained() && CR);
489   if (TL.isConstrained() && CR)
490     Record.AddConceptReference(CR);
491   Record.push_back(TL.isDecltypeAuto());
492   if (TL.isDecltypeAuto())
493     addSourceLocation(TL.getRParenLoc());
494 }
495 
496 void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
497     DeducedTemplateSpecializationTypeLoc TL) {
498   addSourceLocation(TL.getTemplateNameLoc());
499 }
500 
501 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
502   addSourceLocation(TL.getNameLoc());
503 }
504 
505 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
506   addSourceLocation(TL.getNameLoc());
507 }
508 
509 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
510   Record.AddAttr(TL.getAttr());
511 }
512 
513 void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
514   // Nothing to do.
515 }
516 
517 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
518   addSourceLocation(TL.getNameLoc());
519 }
520 
521 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
522                                             SubstTemplateTypeParmTypeLoc TL) {
523   addSourceLocation(TL.getNameLoc());
524 }
525 
526 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
527                                           SubstTemplateTypeParmPackTypeLoc TL) {
528   addSourceLocation(TL.getNameLoc());
529 }
530 
531 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
532                                            TemplateSpecializationTypeLoc TL) {
533   addSourceLocation(TL.getTemplateKeywordLoc());
534   addSourceLocation(TL.getTemplateNameLoc());
535   addSourceLocation(TL.getLAngleLoc());
536   addSourceLocation(TL.getRAngleLoc());
537   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
538     Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
539                                       TL.getArgLoc(i).getLocInfo());
540 }
541 
542 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
543   addSourceLocation(TL.getLParenLoc());
544   addSourceLocation(TL.getRParenLoc());
545 }
546 
547 void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
548   addSourceLocation(TL.getExpansionLoc());
549 }
550 
551 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
552   addSourceLocation(TL.getElaboratedKeywordLoc());
553   Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
554 }
555 
556 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
557   addSourceLocation(TL.getNameLoc());
558 }
559 
560 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
561   addSourceLocation(TL.getElaboratedKeywordLoc());
562   Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
563   addSourceLocation(TL.getNameLoc());
564 }
565 
566 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
567        DependentTemplateSpecializationTypeLoc TL) {
568   addSourceLocation(TL.getElaboratedKeywordLoc());
569   Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
570   addSourceLocation(TL.getTemplateKeywordLoc());
571   addSourceLocation(TL.getTemplateNameLoc());
572   addSourceLocation(TL.getLAngleLoc());
573   addSourceLocation(TL.getRAngleLoc());
574   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
575     Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
576                                       TL.getArgLoc(I).getLocInfo());
577 }
578 
579 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
580   addSourceLocation(TL.getEllipsisLoc());
581 }
582 
583 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
584   addSourceLocation(TL.getNameLoc());
585   addSourceLocation(TL.getNameEndLoc());
586 }
587 
588 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
589   Record.push_back(TL.hasBaseTypeAsWritten());
590   addSourceLocation(TL.getTypeArgsLAngleLoc());
591   addSourceLocation(TL.getTypeArgsRAngleLoc());
592   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
593     Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
594   addSourceLocation(TL.getProtocolLAngleLoc());
595   addSourceLocation(TL.getProtocolRAngleLoc());
596   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
597     addSourceLocation(TL.getProtocolLoc(i));
598 }
599 
600 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
601   addSourceLocation(TL.getStarLoc());
602 }
603 
604 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
605   addSourceLocation(TL.getKWLoc());
606   addSourceLocation(TL.getLParenLoc());
607   addSourceLocation(TL.getRParenLoc());
608 }
609 
610 void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
611   addSourceLocation(TL.getKWLoc());
612 }
613 
614 void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
615   addSourceLocation(TL.getNameLoc());
616 }
617 void TypeLocWriter::VisitDependentBitIntTypeLoc(
618     clang::DependentBitIntTypeLoc TL) {
619   addSourceLocation(TL.getNameLoc());
620 }
621 
622 void ASTWriter::WriteTypeAbbrevs() {
623   using namespace llvm;
624 
625   std::shared_ptr<BitCodeAbbrev> Abv;
626 
627   // Abbreviation for TYPE_EXT_QUAL
628   Abv = std::make_shared<BitCodeAbbrev>();
629   Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
630   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Type
631   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3));   // Quals
632   TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
633 }
634 
635 //===----------------------------------------------------------------------===//
636 // ASTWriter Implementation
637 //===----------------------------------------------------------------------===//
638 
639 static void EmitBlockID(unsigned ID, const char *Name,
640                         llvm::BitstreamWriter &Stream,
641                         ASTWriter::RecordDataImpl &Record) {
642   Record.clear();
643   Record.push_back(ID);
644   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
645 
646   // Emit the block name if present.
647   if (!Name || Name[0] == 0)
648     return;
649   Record.clear();
650   while (*Name)
651     Record.push_back(*Name++);
652   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
653 }
654 
655 static void EmitRecordID(unsigned ID, const char *Name,
656                          llvm::BitstreamWriter &Stream,
657                          ASTWriter::RecordDataImpl &Record) {
658   Record.clear();
659   Record.push_back(ID);
660   while (*Name)
661     Record.push_back(*Name++);
662   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
663 }
664 
665 static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
666                           ASTWriter::RecordDataImpl &Record) {
667 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
668   RECORD(STMT_STOP);
669   RECORD(STMT_NULL_PTR);
670   RECORD(STMT_REF_PTR);
671   RECORD(STMT_NULL);
672   RECORD(STMT_COMPOUND);
673   RECORD(STMT_CASE);
674   RECORD(STMT_DEFAULT);
675   RECORD(STMT_LABEL);
676   RECORD(STMT_ATTRIBUTED);
677   RECORD(STMT_IF);
678   RECORD(STMT_SWITCH);
679   RECORD(STMT_WHILE);
680   RECORD(STMT_DO);
681   RECORD(STMT_FOR);
682   RECORD(STMT_GOTO);
683   RECORD(STMT_INDIRECT_GOTO);
684   RECORD(STMT_CONTINUE);
685   RECORD(STMT_BREAK);
686   RECORD(STMT_RETURN);
687   RECORD(STMT_DECL);
688   RECORD(STMT_GCCASM);
689   RECORD(STMT_MSASM);
690   RECORD(EXPR_PREDEFINED);
691   RECORD(EXPR_DECL_REF);
692   RECORD(EXPR_INTEGER_LITERAL);
693   RECORD(EXPR_FIXEDPOINT_LITERAL);
694   RECORD(EXPR_FLOATING_LITERAL);
695   RECORD(EXPR_IMAGINARY_LITERAL);
696   RECORD(EXPR_STRING_LITERAL);
697   RECORD(EXPR_CHARACTER_LITERAL);
698   RECORD(EXPR_PAREN);
699   RECORD(EXPR_PAREN_LIST);
700   RECORD(EXPR_UNARY_OPERATOR);
701   RECORD(EXPR_SIZEOF_ALIGN_OF);
702   RECORD(EXPR_ARRAY_SUBSCRIPT);
703   RECORD(EXPR_CALL);
704   RECORD(EXPR_MEMBER);
705   RECORD(EXPR_BINARY_OPERATOR);
706   RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
707   RECORD(EXPR_CONDITIONAL_OPERATOR);
708   RECORD(EXPR_IMPLICIT_CAST);
709   RECORD(EXPR_CSTYLE_CAST);
710   RECORD(EXPR_COMPOUND_LITERAL);
711   RECORD(EXPR_EXT_VECTOR_ELEMENT);
712   RECORD(EXPR_INIT_LIST);
713   RECORD(EXPR_DESIGNATED_INIT);
714   RECORD(EXPR_DESIGNATED_INIT_UPDATE);
715   RECORD(EXPR_IMPLICIT_VALUE_INIT);
716   RECORD(EXPR_NO_INIT);
717   RECORD(EXPR_VA_ARG);
718   RECORD(EXPR_ADDR_LABEL);
719   RECORD(EXPR_STMT);
720   RECORD(EXPR_CHOOSE);
721   RECORD(EXPR_GNU_NULL);
722   RECORD(EXPR_SHUFFLE_VECTOR);
723   RECORD(EXPR_BLOCK);
724   RECORD(EXPR_GENERIC_SELECTION);
725   RECORD(EXPR_OBJC_STRING_LITERAL);
726   RECORD(EXPR_OBJC_BOXED_EXPRESSION);
727   RECORD(EXPR_OBJC_ARRAY_LITERAL);
728   RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
729   RECORD(EXPR_OBJC_ENCODE);
730   RECORD(EXPR_OBJC_SELECTOR_EXPR);
731   RECORD(EXPR_OBJC_PROTOCOL_EXPR);
732   RECORD(EXPR_OBJC_IVAR_REF_EXPR);
733   RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
734   RECORD(EXPR_OBJC_KVC_REF_EXPR);
735   RECORD(EXPR_OBJC_MESSAGE_EXPR);
736   RECORD(STMT_OBJC_FOR_COLLECTION);
737   RECORD(STMT_OBJC_CATCH);
738   RECORD(STMT_OBJC_FINALLY);
739   RECORD(STMT_OBJC_AT_TRY);
740   RECORD(STMT_OBJC_AT_SYNCHRONIZED);
741   RECORD(STMT_OBJC_AT_THROW);
742   RECORD(EXPR_OBJC_BOOL_LITERAL);
743   RECORD(STMT_CXX_CATCH);
744   RECORD(STMT_CXX_TRY);
745   RECORD(STMT_CXX_FOR_RANGE);
746   RECORD(EXPR_CXX_OPERATOR_CALL);
747   RECORD(EXPR_CXX_MEMBER_CALL);
748   RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
749   RECORD(EXPR_CXX_CONSTRUCT);
750   RECORD(EXPR_CXX_TEMPORARY_OBJECT);
751   RECORD(EXPR_CXX_STATIC_CAST);
752   RECORD(EXPR_CXX_DYNAMIC_CAST);
753   RECORD(EXPR_CXX_REINTERPRET_CAST);
754   RECORD(EXPR_CXX_CONST_CAST);
755   RECORD(EXPR_CXX_ADDRSPACE_CAST);
756   RECORD(EXPR_CXX_FUNCTIONAL_CAST);
757   RECORD(EXPR_USER_DEFINED_LITERAL);
758   RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
759   RECORD(EXPR_CXX_BOOL_LITERAL);
760   RECORD(EXPR_CXX_PAREN_LIST_INIT);
761   RECORD(EXPR_CXX_NULL_PTR_LITERAL);
762   RECORD(EXPR_CXX_TYPEID_EXPR);
763   RECORD(EXPR_CXX_TYPEID_TYPE);
764   RECORD(EXPR_CXX_THIS);
765   RECORD(EXPR_CXX_THROW);
766   RECORD(EXPR_CXX_DEFAULT_ARG);
767   RECORD(EXPR_CXX_DEFAULT_INIT);
768   RECORD(EXPR_CXX_BIND_TEMPORARY);
769   RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
770   RECORD(EXPR_CXX_NEW);
771   RECORD(EXPR_CXX_DELETE);
772   RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
773   RECORD(EXPR_EXPR_WITH_CLEANUPS);
774   RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
775   RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
776   RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
777   RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
778   RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
779   RECORD(EXPR_CXX_EXPRESSION_TRAIT);
780   RECORD(EXPR_CXX_NOEXCEPT);
781   RECORD(EXPR_OPAQUE_VALUE);
782   RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
783   RECORD(EXPR_TYPE_TRAIT);
784   RECORD(EXPR_ARRAY_TYPE_TRAIT);
785   RECORD(EXPR_PACK_EXPANSION);
786   RECORD(EXPR_SIZEOF_PACK);
787   RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
788   RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
789   RECORD(EXPR_FUNCTION_PARM_PACK);
790   RECORD(EXPR_MATERIALIZE_TEMPORARY);
791   RECORD(EXPR_CUDA_KERNEL_CALL);
792   RECORD(EXPR_CXX_UUIDOF_EXPR);
793   RECORD(EXPR_CXX_UUIDOF_TYPE);
794   RECORD(EXPR_LAMBDA);
795 #undef RECORD
796 }
797 
798 void ASTWriter::WriteBlockInfoBlock() {
799   RecordData Record;
800   Stream.EnterBlockInfoBlock();
801 
802 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
803 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
804 
805   // Control Block.
806   BLOCK(CONTROL_BLOCK);
807   RECORD(METADATA);
808   RECORD(MODULE_NAME);
809   RECORD(MODULE_DIRECTORY);
810   RECORD(MODULE_MAP_FILE);
811   RECORD(IMPORTS);
812   RECORD(ORIGINAL_FILE);
813   RECORD(ORIGINAL_FILE_ID);
814   RECORD(INPUT_FILE_OFFSETS);
815 
816   BLOCK(OPTIONS_BLOCK);
817   RECORD(LANGUAGE_OPTIONS);
818   RECORD(TARGET_OPTIONS);
819   RECORD(FILE_SYSTEM_OPTIONS);
820   RECORD(HEADER_SEARCH_OPTIONS);
821   RECORD(PREPROCESSOR_OPTIONS);
822 
823   BLOCK(INPUT_FILES_BLOCK);
824   RECORD(INPUT_FILE);
825   RECORD(INPUT_FILE_HASH);
826 
827   // AST Top-Level Block.
828   BLOCK(AST_BLOCK);
829   RECORD(TYPE_OFFSET);
830   RECORD(DECL_OFFSET);
831   RECORD(IDENTIFIER_OFFSET);
832   RECORD(IDENTIFIER_TABLE);
833   RECORD(EAGERLY_DESERIALIZED_DECLS);
834   RECORD(MODULAR_CODEGEN_DECLS);
835   RECORD(SPECIAL_TYPES);
836   RECORD(STATISTICS);
837   RECORD(TENTATIVE_DEFINITIONS);
838   RECORD(SELECTOR_OFFSETS);
839   RECORD(METHOD_POOL);
840   RECORD(PP_COUNTER_VALUE);
841   RECORD(SOURCE_LOCATION_OFFSETS);
842   RECORD(EXT_VECTOR_DECLS);
843   RECORD(UNUSED_FILESCOPED_DECLS);
844   RECORD(PPD_ENTITIES_OFFSETS);
845   RECORD(VTABLE_USES);
846   RECORD(PPD_SKIPPED_RANGES);
847   RECORD(REFERENCED_SELECTOR_POOL);
848   RECORD(TU_UPDATE_LEXICAL);
849   RECORD(SEMA_DECL_REFS);
850   RECORD(WEAK_UNDECLARED_IDENTIFIERS);
851   RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
852   RECORD(UPDATE_VISIBLE);
853   RECORD(DECL_UPDATE_OFFSETS);
854   RECORD(DECL_UPDATES);
855   RECORD(CUDA_SPECIAL_DECL_REFS);
856   RECORD(HEADER_SEARCH_TABLE);
857   RECORD(FP_PRAGMA_OPTIONS);
858   RECORD(OPENCL_EXTENSIONS);
859   RECORD(OPENCL_EXTENSION_TYPES);
860   RECORD(OPENCL_EXTENSION_DECLS);
861   RECORD(DELEGATING_CTORS);
862   RECORD(KNOWN_NAMESPACES);
863   RECORD(MODULE_OFFSET_MAP);
864   RECORD(SOURCE_MANAGER_LINE_TABLE);
865   RECORD(OBJC_CATEGORIES_MAP);
866   RECORD(FILE_SORTED_DECLS);
867   RECORD(IMPORTED_MODULES);
868   RECORD(OBJC_CATEGORIES);
869   RECORD(MACRO_OFFSET);
870   RECORD(INTERESTING_IDENTIFIERS);
871   RECORD(UNDEFINED_BUT_USED);
872   RECORD(LATE_PARSED_TEMPLATE);
873   RECORD(OPTIMIZE_PRAGMA_OPTIONS);
874   RECORD(MSSTRUCT_PRAGMA_OPTIONS);
875   RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
876   RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
877   RECORD(DELETE_EXPRS_TO_ANALYZE);
878   RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
879   RECORD(PP_CONDITIONAL_STACK);
880   RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
881   RECORD(PP_ASSUME_NONNULL_LOC);
882 
883   // SourceManager Block.
884   BLOCK(SOURCE_MANAGER_BLOCK);
885   RECORD(SM_SLOC_FILE_ENTRY);
886   RECORD(SM_SLOC_BUFFER_ENTRY);
887   RECORD(SM_SLOC_BUFFER_BLOB);
888   RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
889   RECORD(SM_SLOC_EXPANSION_ENTRY);
890 
891   // Preprocessor Block.
892   BLOCK(PREPROCESSOR_BLOCK);
893   RECORD(PP_MACRO_DIRECTIVE_HISTORY);
894   RECORD(PP_MACRO_FUNCTION_LIKE);
895   RECORD(PP_MACRO_OBJECT_LIKE);
896   RECORD(PP_MODULE_MACRO);
897   RECORD(PP_TOKEN);
898 
899   // Submodule Block.
900   BLOCK(SUBMODULE_BLOCK);
901   RECORD(SUBMODULE_METADATA);
902   RECORD(SUBMODULE_DEFINITION);
903   RECORD(SUBMODULE_UMBRELLA_HEADER);
904   RECORD(SUBMODULE_HEADER);
905   RECORD(SUBMODULE_TOPHEADER);
906   RECORD(SUBMODULE_UMBRELLA_DIR);
907   RECORD(SUBMODULE_IMPORTS);
908   RECORD(SUBMODULE_AFFECTING_MODULES);
909   RECORD(SUBMODULE_EXPORTS);
910   RECORD(SUBMODULE_REQUIRES);
911   RECORD(SUBMODULE_EXCLUDED_HEADER);
912   RECORD(SUBMODULE_LINK_LIBRARY);
913   RECORD(SUBMODULE_CONFIG_MACRO);
914   RECORD(SUBMODULE_CONFLICT);
915   RECORD(SUBMODULE_PRIVATE_HEADER);
916   RECORD(SUBMODULE_TEXTUAL_HEADER);
917   RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
918   RECORD(SUBMODULE_INITIALIZERS);
919   RECORD(SUBMODULE_EXPORT_AS);
920 
921   // Comments Block.
922   BLOCK(COMMENTS_BLOCK);
923   RECORD(COMMENTS_RAW_COMMENT);
924 
925   // Decls and Types block.
926   BLOCK(DECLTYPES_BLOCK);
927   RECORD(TYPE_EXT_QUAL);
928   RECORD(TYPE_COMPLEX);
929   RECORD(TYPE_POINTER);
930   RECORD(TYPE_BLOCK_POINTER);
931   RECORD(TYPE_LVALUE_REFERENCE);
932   RECORD(TYPE_RVALUE_REFERENCE);
933   RECORD(TYPE_MEMBER_POINTER);
934   RECORD(TYPE_CONSTANT_ARRAY);
935   RECORD(TYPE_INCOMPLETE_ARRAY);
936   RECORD(TYPE_VARIABLE_ARRAY);
937   RECORD(TYPE_VECTOR);
938   RECORD(TYPE_EXT_VECTOR);
939   RECORD(TYPE_FUNCTION_NO_PROTO);
940   RECORD(TYPE_FUNCTION_PROTO);
941   RECORD(TYPE_TYPEDEF);
942   RECORD(TYPE_TYPEOF_EXPR);
943   RECORD(TYPE_TYPEOF);
944   RECORD(TYPE_RECORD);
945   RECORD(TYPE_ENUM);
946   RECORD(TYPE_OBJC_INTERFACE);
947   RECORD(TYPE_OBJC_OBJECT_POINTER);
948   RECORD(TYPE_DECLTYPE);
949   RECORD(TYPE_ELABORATED);
950   RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
951   RECORD(TYPE_UNRESOLVED_USING);
952   RECORD(TYPE_INJECTED_CLASS_NAME);
953   RECORD(TYPE_OBJC_OBJECT);
954   RECORD(TYPE_TEMPLATE_TYPE_PARM);
955   RECORD(TYPE_TEMPLATE_SPECIALIZATION);
956   RECORD(TYPE_DEPENDENT_NAME);
957   RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
958   RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
959   RECORD(TYPE_PAREN);
960   RECORD(TYPE_MACRO_QUALIFIED);
961   RECORD(TYPE_PACK_EXPANSION);
962   RECORD(TYPE_ATTRIBUTED);
963   RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
964   RECORD(TYPE_AUTO);
965   RECORD(TYPE_UNARY_TRANSFORM);
966   RECORD(TYPE_ATOMIC);
967   RECORD(TYPE_DECAYED);
968   RECORD(TYPE_ADJUSTED);
969   RECORD(TYPE_OBJC_TYPE_PARAM);
970   RECORD(LOCAL_REDECLARATIONS);
971   RECORD(DECL_TYPEDEF);
972   RECORD(DECL_TYPEALIAS);
973   RECORD(DECL_ENUM);
974   RECORD(DECL_RECORD);
975   RECORD(DECL_ENUM_CONSTANT);
976   RECORD(DECL_FUNCTION);
977   RECORD(DECL_OBJC_METHOD);
978   RECORD(DECL_OBJC_INTERFACE);
979   RECORD(DECL_OBJC_PROTOCOL);
980   RECORD(DECL_OBJC_IVAR);
981   RECORD(DECL_OBJC_AT_DEFS_FIELD);
982   RECORD(DECL_OBJC_CATEGORY);
983   RECORD(DECL_OBJC_CATEGORY_IMPL);
984   RECORD(DECL_OBJC_IMPLEMENTATION);
985   RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
986   RECORD(DECL_OBJC_PROPERTY);
987   RECORD(DECL_OBJC_PROPERTY_IMPL);
988   RECORD(DECL_FIELD);
989   RECORD(DECL_MS_PROPERTY);
990   RECORD(DECL_VAR);
991   RECORD(DECL_IMPLICIT_PARAM);
992   RECORD(DECL_PARM_VAR);
993   RECORD(DECL_FILE_SCOPE_ASM);
994   RECORD(DECL_BLOCK);
995   RECORD(DECL_CONTEXT_LEXICAL);
996   RECORD(DECL_CONTEXT_VISIBLE);
997   RECORD(DECL_NAMESPACE);
998   RECORD(DECL_NAMESPACE_ALIAS);
999   RECORD(DECL_USING);
1000   RECORD(DECL_USING_SHADOW);
1001   RECORD(DECL_USING_DIRECTIVE);
1002   RECORD(DECL_UNRESOLVED_USING_VALUE);
1003   RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1004   RECORD(DECL_LINKAGE_SPEC);
1005   RECORD(DECL_CXX_RECORD);
1006   RECORD(DECL_CXX_METHOD);
1007   RECORD(DECL_CXX_CONSTRUCTOR);
1008   RECORD(DECL_CXX_DESTRUCTOR);
1009   RECORD(DECL_CXX_CONVERSION);
1010   RECORD(DECL_ACCESS_SPEC);
1011   RECORD(DECL_FRIEND);
1012   RECORD(DECL_FRIEND_TEMPLATE);
1013   RECORD(DECL_CLASS_TEMPLATE);
1014   RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1015   RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
1016   RECORD(DECL_VAR_TEMPLATE);
1017   RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1018   RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
1019   RECORD(DECL_FUNCTION_TEMPLATE);
1020   RECORD(DECL_TEMPLATE_TYPE_PARM);
1021   RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1022   RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1023   RECORD(DECL_CONCEPT);
1024   RECORD(DECL_REQUIRES_EXPR_BODY);
1025   RECORD(DECL_TYPE_ALIAS_TEMPLATE);
1026   RECORD(DECL_STATIC_ASSERT);
1027   RECORD(DECL_CXX_BASE_SPECIFIERS);
1028   RECORD(DECL_CXX_CTOR_INITIALIZERS);
1029   RECORD(DECL_INDIRECTFIELD);
1030   RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1031   RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1032   RECORD(DECL_IMPORT);
1033   RECORD(DECL_OMP_THREADPRIVATE);
1034   RECORD(DECL_EMPTY);
1035   RECORD(DECL_OBJC_TYPE_PARAM);
1036   RECORD(DECL_OMP_CAPTUREDEXPR);
1037   RECORD(DECL_PRAGMA_COMMENT);
1038   RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1039   RECORD(DECL_OMP_DECLARE_REDUCTION);
1040   RECORD(DECL_OMP_ALLOCATE);
1041   RECORD(DECL_HLSL_BUFFER);
1042 
1043   // Statements and Exprs can occur in the Decls and Types block.
1044   AddStmtsExprs(Stream, Record);
1045 
1046   BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1047   RECORD(PPD_MACRO_EXPANSION);
1048   RECORD(PPD_MACRO_DEFINITION);
1049   RECORD(PPD_INCLUSION_DIRECTIVE);
1050 
1051   // Decls and Types block.
1052   BLOCK(EXTENSION_BLOCK);
1053   RECORD(EXTENSION_METADATA);
1054 
1055   BLOCK(UNHASHED_CONTROL_BLOCK);
1056   RECORD(SIGNATURE);
1057   RECORD(AST_BLOCK_HASH);
1058   RECORD(DIAGNOSTIC_OPTIONS);
1059   RECORD(HEADER_SEARCH_PATHS);
1060   RECORD(DIAG_PRAGMA_MAPPINGS);
1061 
1062 #undef RECORD
1063 #undef BLOCK
1064   Stream.ExitBlock();
1065 }
1066 
1067 /// Prepares a path for being written to an AST file by converting it
1068 /// to an absolute path and removing nested './'s.
1069 ///
1070 /// \return \c true if the path was changed.
1071 static bool cleanPathForOutput(FileManager &FileMgr,
1072                                SmallVectorImpl<char> &Path) {
1073   bool Changed = FileMgr.makeAbsolutePath(Path);
1074   return Changed | llvm::sys::path::remove_dots(Path);
1075 }
1076 
1077 /// Adjusts the given filename to only write out the portion of the
1078 /// filename that is not part of the system root directory.
1079 ///
1080 /// \param Filename the file name to adjust.
1081 ///
1082 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1083 /// the returned filename will be adjusted by this root directory.
1084 ///
1085 /// \returns either the original filename (if it needs no adjustment) or the
1086 /// adjusted filename (which points into the @p Filename parameter).
1087 static const char *
1088 adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1089   assert(Filename && "No file name to adjust?");
1090 
1091   if (BaseDir.empty())
1092     return Filename;
1093 
1094   // Verify that the filename and the system root have the same prefix.
1095   unsigned Pos = 0;
1096   for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1097     if (Filename[Pos] != BaseDir[Pos])
1098       return Filename; // Prefixes don't match.
1099 
1100   // We hit the end of the filename before we hit the end of the system root.
1101   if (!Filename[Pos])
1102     return Filename;
1103 
1104   // If there's not a path separator at the end of the base directory nor
1105   // immediately after it, then this isn't within the base directory.
1106   if (!llvm::sys::path::is_separator(Filename[Pos])) {
1107     if (!llvm::sys::path::is_separator(BaseDir.back()))
1108       return Filename;
1109   } else {
1110     // If the file name has a '/' at the current position, skip over the '/'.
1111     // We distinguish relative paths from absolute paths by the
1112     // absence of '/' at the beginning of relative paths.
1113     //
1114     // FIXME: This is wrong. We distinguish them by asking if the path is
1115     // absolute, which isn't the same thing. And there might be multiple '/'s
1116     // in a row. Use a better mechanism to indicate whether we have emitted an
1117     // absolute or relative path.
1118     ++Pos;
1119   }
1120 
1121   return Filename + Pos;
1122 }
1123 
1124 std::pair<ASTFileSignature, ASTFileSignature>
1125 ASTWriter::createSignature() const {
1126   StringRef AllBytes(Buffer.data(), Buffer.size());
1127 
1128   llvm::SHA1 Hasher;
1129   Hasher.update(AllBytes.slice(ASTBlockRange.first, ASTBlockRange.second));
1130   ASTFileSignature ASTBlockHash = ASTFileSignature::create(Hasher.result());
1131 
1132   // Add the remaining bytes:
1133   //  1. Before the unhashed control block.
1134   Hasher.update(AllBytes.slice(0, UnhashedControlBlockRange.first));
1135   //  2. Between the unhashed control block and the AST block.
1136   Hasher.update(
1137       AllBytes.slice(UnhashedControlBlockRange.second, ASTBlockRange.first));
1138   //  3. After the AST block.
1139   Hasher.update(AllBytes.slice(ASTBlockRange.second, StringRef::npos));
1140   ASTFileSignature Signature = ASTFileSignature::create(Hasher.result());
1141 
1142   return std::make_pair(ASTBlockHash, Signature);
1143 }
1144 
1145 ASTFileSignature ASTWriter::backpatchSignature() {
1146   if (!WritingModule ||
1147       !PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent)
1148     return {};
1149 
1150   // For implicit modules, write the hash of the PCM as its signature.
1151 
1152   auto BackpatchSignatureAt = [&](const ASTFileSignature &S, uint64_t BitNo) {
1153     for (uint8_t Byte : S) {
1154       Stream.BackpatchByte(BitNo, Byte);
1155       BitNo += 8;
1156     }
1157   };
1158 
1159   ASTFileSignature ASTBlockHash;
1160   ASTFileSignature Signature;
1161   std::tie(ASTBlockHash, Signature) = createSignature();
1162 
1163   BackpatchSignatureAt(ASTBlockHash, ASTBlockHashOffset);
1164   BackpatchSignatureAt(Signature, SignatureOffset);
1165 
1166   return Signature;
1167 }
1168 
1169 void ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1170                                           ASTContext &Context) {
1171   using namespace llvm;
1172 
1173   // Flush first to prepare the PCM hash (signature).
1174   Stream.FlushToWord();
1175   UnhashedControlBlockRange.first = Stream.GetCurrentBitNo() >> 3;
1176 
1177   // Enter the block and prepare to write records.
1178   RecordData Record;
1179   Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1180 
1181   // For implicit modules, write the hash of the PCM as its signature.
1182   if (WritingModule &&
1183       PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1184     // At this point, we don't know the actual signature of the file or the AST
1185     // block - we're only able to compute those at the end of the serialization
1186     // process. Let's store dummy signatures for now, and replace them with the
1187     // real ones later on.
1188     // The bitstream VBR-encodes record elements, which makes backpatching them
1189     // really difficult. Let's store the signatures as blobs instead - they are
1190     // guaranteed to be word-aligned, and we control their format/encoding.
1191     auto Dummy = ASTFileSignature::createDummy();
1192     SmallString<128> Blob{Dummy.begin(), Dummy.end()};
1193 
1194     auto Abbrev = std::make_shared<BitCodeAbbrev>();
1195     Abbrev->Add(BitCodeAbbrevOp(AST_BLOCK_HASH));
1196     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1197     unsigned ASTBlockHashAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1198 
1199     Abbrev = std::make_shared<BitCodeAbbrev>();
1200     Abbrev->Add(BitCodeAbbrevOp(SIGNATURE));
1201     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1202     unsigned SignatureAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1203 
1204     Record.push_back(AST_BLOCK_HASH);
1205     Stream.EmitRecordWithBlob(ASTBlockHashAbbrev, Record, Blob);
1206     ASTBlockHashOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1207     Record.clear();
1208 
1209     Record.push_back(SIGNATURE);
1210     Stream.EmitRecordWithBlob(SignatureAbbrev, Record, Blob);
1211     SignatureOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1212     Record.clear();
1213   }
1214 
1215   const auto &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1216 
1217   // Diagnostic options.
1218   const auto &Diags = Context.getDiagnostics();
1219   const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1220   if (!HSOpts.ModulesSkipDiagnosticOptions) {
1221 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1222 #define ENUM_DIAGOPT(Name, Type, Bits, Default)                                \
1223   Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1224 #include "clang/Basic/DiagnosticOptions.def"
1225     Record.push_back(DiagOpts.Warnings.size());
1226     for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1227       AddString(DiagOpts.Warnings[I], Record);
1228     Record.push_back(DiagOpts.Remarks.size());
1229     for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1230       AddString(DiagOpts.Remarks[I], Record);
1231     // Note: we don't serialize the log or serialization file names, because
1232     // they are generally transient files and will almost always be overridden.
1233     Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1234     Record.clear();
1235   }
1236 
1237   // Header search paths.
1238   if (!HSOpts.ModulesSkipHeaderSearchPaths) {
1239     // Include entries.
1240     Record.push_back(HSOpts.UserEntries.size());
1241     for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1242       const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1243       AddString(Entry.Path, Record);
1244       Record.push_back(static_cast<unsigned>(Entry.Group));
1245       Record.push_back(Entry.IsFramework);
1246       Record.push_back(Entry.IgnoreSysRoot);
1247     }
1248 
1249     // System header prefixes.
1250     Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1251     for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1252       AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1253       Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1254     }
1255 
1256     // VFS overlay files.
1257     Record.push_back(HSOpts.VFSOverlayFiles.size());
1258     for (StringRef VFSOverlayFile : HSOpts.VFSOverlayFiles)
1259       AddString(VFSOverlayFile, Record);
1260 
1261     Stream.EmitRecord(HEADER_SEARCH_PATHS, Record);
1262   }
1263 
1264   if (!HSOpts.ModulesSkipPragmaDiagnosticMappings)
1265     WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1266 
1267   // Header search entry usage.
1268   auto HSEntryUsage = PP.getHeaderSearchInfo().computeUserEntryUsage();
1269   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1270   Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1271   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1272   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));      // Bit vector.
1273   unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1274   {
1275     RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1276                                        HSEntryUsage.size()};
1277     Stream.EmitRecordWithBlob(HSUsageAbbrevCode, Record, bytes(HSEntryUsage));
1278   }
1279 
1280   // Leave the options block.
1281   Stream.ExitBlock();
1282   UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
1283 }
1284 
1285 /// Write the control block.
1286 void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1287                                   StringRef isysroot) {
1288   using namespace llvm;
1289 
1290   Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1291   RecordData Record;
1292 
1293   // Metadata
1294   auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1295   MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1296   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1297   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1298   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1299   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1300   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1301   // Standard C++ module
1302   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1303   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1304   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1305   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1306   unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1307   assert((!WritingModule || isysroot.empty()) &&
1308          "writing module as a relocatable PCH?");
1309   {
1310     RecordData::value_type Record[] = {METADATA,
1311                                        VERSION_MAJOR,
1312                                        VERSION_MINOR,
1313                                        CLANG_VERSION_MAJOR,
1314                                        CLANG_VERSION_MINOR,
1315                                        !isysroot.empty(),
1316                                        isWritingStdCXXNamedModules(),
1317                                        IncludeTimestamps,
1318                                        ASTHasCompilerErrors};
1319     Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1320                               getClangFullRepositoryVersion());
1321   }
1322 
1323   if (WritingModule) {
1324     // Module name
1325     auto Abbrev = std::make_shared<BitCodeAbbrev>();
1326     Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1327     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1328     unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1329     RecordData::value_type Record[] = {MODULE_NAME};
1330     Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1331   }
1332 
1333   if (WritingModule && WritingModule->Directory) {
1334     SmallString<128> BaseDir;
1335     if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1336       // Use the current working directory as the base path for all inputs.
1337       auto CWD =
1338           Context.getSourceManager().getFileManager().getOptionalDirectoryRef(
1339               ".");
1340       BaseDir.assign(CWD->getName());
1341     } else {
1342       BaseDir.assign(WritingModule->Directory->getName());
1343     }
1344     cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1345 
1346     // If the home of the module is the current working directory, then we
1347     // want to pick up the cwd of the build process loading the module, not
1348     // our cwd, when we load this module.
1349     if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1350         (!PP.getHeaderSearchInfo()
1351               .getHeaderSearchOpts()
1352               .ModuleMapFileHomeIsCwd ||
1353          WritingModule->Directory->getName() != StringRef("."))) {
1354       // Module directory.
1355       auto Abbrev = std::make_shared<BitCodeAbbrev>();
1356       Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1357       Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1358       unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1359 
1360       RecordData::value_type Record[] = {MODULE_DIRECTORY};
1361       Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1362     }
1363 
1364     // Write out all other paths relative to the base directory if possible.
1365     BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1366   } else if (!isysroot.empty()) {
1367     // Write out paths relative to the sysroot if possible.
1368     BaseDirectory = std::string(isysroot);
1369   }
1370 
1371   // Module map file
1372   if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1373     Record.clear();
1374 
1375     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1376     AddPath(WritingModule->PresumedModuleMapFile.empty()
1377                 ? Map.getModuleMapFileForUniquing(WritingModule)
1378                       ->getNameAsRequested()
1379                 : StringRef(WritingModule->PresumedModuleMapFile),
1380             Record);
1381 
1382     // Additional module map files.
1383     if (auto *AdditionalModMaps =
1384             Map.getAdditionalModuleMapFiles(WritingModule)) {
1385       Record.push_back(AdditionalModMaps->size());
1386       SmallVector<FileEntryRef, 1> ModMaps(AdditionalModMaps->begin(),
1387                                            AdditionalModMaps->end());
1388       llvm::sort(ModMaps, [](FileEntryRef A, FileEntryRef B) {
1389         return A.getName() < B.getName();
1390       });
1391       for (FileEntryRef F : ModMaps)
1392         AddPath(F.getName(), Record);
1393     } else {
1394       Record.push_back(0);
1395     }
1396 
1397     Stream.EmitRecord(MODULE_MAP_FILE, Record);
1398   }
1399 
1400   // Imports
1401   if (Chain) {
1402     serialization::ModuleManager &Mgr = Chain->getModuleManager();
1403     Record.clear();
1404 
1405     for (ModuleFile &M : Mgr) {
1406       // Skip modules that weren't directly imported.
1407       if (!M.isDirectlyImported())
1408         continue;
1409 
1410       Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1411       Record.push_back(M.StandardCXXModule);
1412       AddSourceLocation(M.ImportLoc, Record);
1413 
1414       // We don't want to hard code the information about imported modules
1415       // in the C++20 named modules.
1416       if (!M.StandardCXXModule) {
1417         // If we have calculated signature, there is no need to store
1418         // the size or timestamp.
1419         Record.push_back(M.Signature ? 0 : M.File.getSize());
1420         Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1421         llvm::append_range(Record, M.Signature);
1422       }
1423 
1424       AddString(M.ModuleName, Record);
1425 
1426       if (!M.StandardCXXModule)
1427         AddPath(M.FileName, Record);
1428     }
1429     Stream.EmitRecord(IMPORTS, Record);
1430   }
1431 
1432   // Write the options block.
1433   Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1434 
1435   // Language options.
1436   Record.clear();
1437   const LangOptions &LangOpts = Context.getLangOpts();
1438 #define LANGOPT(Name, Bits, Default, Description) \
1439   Record.push_back(LangOpts.Name);
1440 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1441   Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1442 #include "clang/Basic/LangOptions.def"
1443 #define SANITIZER(NAME, ID)                                                    \
1444   Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1445 #include "clang/Basic/Sanitizers.def"
1446 
1447   Record.push_back(LangOpts.ModuleFeatures.size());
1448   for (StringRef Feature : LangOpts.ModuleFeatures)
1449     AddString(Feature, Record);
1450 
1451   Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1452   AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1453 
1454   AddString(LangOpts.CurrentModule, Record);
1455 
1456   // Comment options.
1457   Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1458   for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1459     AddString(I, Record);
1460   }
1461   Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1462 
1463   // OpenMP offloading options.
1464   Record.push_back(LangOpts.OMPTargetTriples.size());
1465   for (auto &T : LangOpts.OMPTargetTriples)
1466     AddString(T.getTriple(), Record);
1467 
1468   AddString(LangOpts.OMPHostIRFile, Record);
1469 
1470   Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1471 
1472   // Target options.
1473   Record.clear();
1474   const TargetInfo &Target = Context.getTargetInfo();
1475   const TargetOptions &TargetOpts = Target.getTargetOpts();
1476   AddString(TargetOpts.Triple, Record);
1477   AddString(TargetOpts.CPU, Record);
1478   AddString(TargetOpts.TuneCPU, Record);
1479   AddString(TargetOpts.ABI, Record);
1480   Record.push_back(TargetOpts.FeaturesAsWritten.size());
1481   for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1482     AddString(TargetOpts.FeaturesAsWritten[I], Record);
1483   }
1484   Record.push_back(TargetOpts.Features.size());
1485   for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1486     AddString(TargetOpts.Features[I], Record);
1487   }
1488   Stream.EmitRecord(TARGET_OPTIONS, Record);
1489 
1490   // File system options.
1491   Record.clear();
1492   const FileSystemOptions &FSOpts =
1493       Context.getSourceManager().getFileManager().getFileSystemOpts();
1494   AddString(FSOpts.WorkingDir, Record);
1495   Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1496 
1497   // Header search options.
1498   Record.clear();
1499   const HeaderSearchOptions &HSOpts =
1500       PP.getHeaderSearchInfo().getHeaderSearchOpts();
1501 
1502   AddString(HSOpts.Sysroot, Record);
1503   AddString(HSOpts.ResourceDir, Record);
1504   AddString(HSOpts.ModuleCachePath, Record);
1505   AddString(HSOpts.ModuleUserBuildPath, Record);
1506   Record.push_back(HSOpts.DisableModuleHash);
1507   Record.push_back(HSOpts.ImplicitModuleMaps);
1508   Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1509   Record.push_back(HSOpts.EnablePrebuiltImplicitModules);
1510   Record.push_back(HSOpts.UseBuiltinIncludes);
1511   Record.push_back(HSOpts.UseStandardSystemIncludes);
1512   Record.push_back(HSOpts.UseStandardCXXIncludes);
1513   Record.push_back(HSOpts.UseLibcxx);
1514   // Write out the specific module cache path that contains the module files.
1515   AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1516   Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1517 
1518   // Preprocessor options.
1519   Record.clear();
1520   const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1521 
1522   // If we're building an implicit module with a context hash, the importer is
1523   // guaranteed to have the same macros defined on the command line. Skip
1524   // writing them.
1525   bool SkipMacros = BuildingImplicitModule && !HSOpts.DisableModuleHash;
1526   bool WriteMacros = !SkipMacros;
1527   Record.push_back(WriteMacros);
1528   if (WriteMacros) {
1529     // Macro definitions.
1530     Record.push_back(PPOpts.Macros.size());
1531     for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1532       AddString(PPOpts.Macros[I].first, Record);
1533       Record.push_back(PPOpts.Macros[I].second);
1534     }
1535   }
1536 
1537   // Includes
1538   Record.push_back(PPOpts.Includes.size());
1539   for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1540     AddString(PPOpts.Includes[I], Record);
1541 
1542   // Macro includes
1543   Record.push_back(PPOpts.MacroIncludes.size());
1544   for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1545     AddString(PPOpts.MacroIncludes[I], Record);
1546 
1547   Record.push_back(PPOpts.UsePredefines);
1548   // Detailed record is important since it is used for the module cache hash.
1549   Record.push_back(PPOpts.DetailedRecord);
1550   AddString(PPOpts.ImplicitPCHInclude, Record);
1551   Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1552   Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1553 
1554   // Leave the options block.
1555   Stream.ExitBlock();
1556 
1557   // Original file name and file ID
1558   SourceManager &SM = Context.getSourceManager();
1559   if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID())) {
1560     auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1561     FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1562     FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1563     FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1564     unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1565 
1566     Record.clear();
1567     Record.push_back(ORIGINAL_FILE);
1568     AddFileID(SM.getMainFileID(), Record);
1569     EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1570   }
1571 
1572   Record.clear();
1573   AddFileID(SM.getMainFileID(), Record);
1574   Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1575 
1576   WriteInputFiles(Context.SourceMgr,
1577                   PP.getHeaderSearchInfo().getHeaderSearchOpts());
1578   Stream.ExitBlock();
1579 }
1580 
1581 namespace  {
1582 
1583 /// An input file.
1584 struct InputFileEntry {
1585   FileEntryRef File;
1586   bool IsSystemFile;
1587   bool IsTransient;
1588   bool BufferOverridden;
1589   bool IsTopLevel;
1590   bool IsModuleMap;
1591   uint32_t ContentHash[2];
1592 
1593   InputFileEntry(FileEntryRef File) : File(File) {}
1594 };
1595 
1596 } // namespace
1597 
1598 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1599                                 HeaderSearchOptions &HSOpts) {
1600   using namespace llvm;
1601 
1602   Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1603 
1604   // Create input-file abbreviation.
1605   auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1606   IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1607   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1608   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1609   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1610   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1611   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1612   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1613   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1614   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1615   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
1616   unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1617 
1618   // Create input file hash abbreviation.
1619   auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1620   IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1621   IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1622   IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1623   unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1624 
1625   uint64_t InputFilesOffsetBase = Stream.GetCurrentBitNo();
1626 
1627   // Get all ContentCache objects for files.
1628   std::vector<InputFileEntry> UserFiles;
1629   std::vector<InputFileEntry> SystemFiles;
1630   for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1631     // Get this source location entry.
1632     const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1633     assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1634 
1635     // We only care about file entries that were not overridden.
1636     if (!SLoc->isFile())
1637       continue;
1638     const SrcMgr::FileInfo &File = SLoc->getFile();
1639     const SrcMgr::ContentCache *Cache = &File.getContentCache();
1640     if (!Cache->OrigEntry)
1641       continue;
1642 
1643     // Do not emit input files that do not affect current module.
1644     if (!IsSLocAffecting[I])
1645       continue;
1646 
1647     InputFileEntry Entry(*Cache->OrigEntry);
1648     Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1649     Entry.IsTransient = Cache->IsTransient;
1650     Entry.BufferOverridden = Cache->BufferOverridden;
1651     Entry.IsTopLevel = File.getIncludeLoc().isInvalid();
1652     Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
1653 
1654     auto ContentHash = hash_code(-1);
1655     if (PP->getHeaderSearchInfo()
1656             .getHeaderSearchOpts()
1657             .ValidateASTInputFilesContent) {
1658       auto MemBuff = Cache->getBufferIfLoaded();
1659       if (MemBuff)
1660         ContentHash = hash_value(MemBuff->getBuffer());
1661       else
1662         PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1663             << Entry.File.getName();
1664     }
1665     auto CH = llvm::APInt(64, ContentHash);
1666     Entry.ContentHash[0] =
1667         static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1668     Entry.ContentHash[1] =
1669         static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1670 
1671     if (Entry.IsSystemFile)
1672       SystemFiles.push_back(Entry);
1673     else
1674       UserFiles.push_back(Entry);
1675   }
1676 
1677   // User files go at the front, system files at the back.
1678   auto SortedFiles = llvm::concat<InputFileEntry>(std::move(UserFiles),
1679                                                   std::move(SystemFiles));
1680 
1681   unsigned UserFilesNum = 0;
1682   // Write out all of the input files.
1683   std::vector<uint64_t> InputFileOffsets;
1684   for (const auto &Entry : SortedFiles) {
1685     uint32_t &InputFileID = InputFileIDs[Entry.File];
1686     if (InputFileID != 0)
1687       continue; // already recorded this file.
1688 
1689     // Record this entry's offset.
1690     InputFileOffsets.push_back(Stream.GetCurrentBitNo() - InputFilesOffsetBase);
1691 
1692     InputFileID = InputFileOffsets.size();
1693 
1694     if (!Entry.IsSystemFile)
1695       ++UserFilesNum;
1696 
1697     // Emit size/modification time for this file.
1698     // And whether this file was overridden.
1699     {
1700       SmallString<128> NameAsRequested = Entry.File.getNameAsRequested();
1701       SmallString<128> Name = Entry.File.getName();
1702 
1703       PreparePathForOutput(NameAsRequested);
1704       PreparePathForOutput(Name);
1705 
1706       if (Name == NameAsRequested)
1707         Name.clear();
1708 
1709       RecordData::value_type Record[] = {
1710           INPUT_FILE,
1711           InputFileOffsets.size(),
1712           (uint64_t)Entry.File.getSize(),
1713           (uint64_t)getTimestampForOutput(Entry.File),
1714           Entry.BufferOverridden,
1715           Entry.IsTransient,
1716           Entry.IsTopLevel,
1717           Entry.IsModuleMap,
1718           NameAsRequested.size()};
1719 
1720       Stream.EmitRecordWithBlob(IFAbbrevCode, Record,
1721                                 (NameAsRequested + Name).str());
1722     }
1723 
1724     // Emit content hash for this file.
1725     {
1726       RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1727                                          Entry.ContentHash[1]};
1728       Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1729     }
1730   }
1731 
1732   Stream.ExitBlock();
1733 
1734   // Create input file offsets abbreviation.
1735   auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1736   OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1737   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1738   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1739                                                                 //   input files
1740   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));   // Array
1741   unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1742 
1743   // Write input file offsets.
1744   RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1745                                      InputFileOffsets.size(), UserFilesNum};
1746   Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1747 }
1748 
1749 //===----------------------------------------------------------------------===//
1750 // Source Manager Serialization
1751 //===----------------------------------------------------------------------===//
1752 
1753 /// Create an abbreviation for the SLocEntry that refers to a
1754 /// file.
1755 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1756   using namespace llvm;
1757 
1758   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1759   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1760   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1761   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1762   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1763   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1764   // FileEntry fields.
1765   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1766   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1767   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1768   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1769   return Stream.EmitAbbrev(std::move(Abbrev));
1770 }
1771 
1772 /// Create an abbreviation for the SLocEntry that refers to a
1773 /// buffer.
1774 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1775   using namespace llvm;
1776 
1777   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1778   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1779   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1780   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1781   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1782   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1783   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1784   return Stream.EmitAbbrev(std::move(Abbrev));
1785 }
1786 
1787 /// Create an abbreviation for the SLocEntry that refers to a
1788 /// buffer's blob.
1789 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1790                                            bool Compressed) {
1791   using namespace llvm;
1792 
1793   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1794   Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1795                                          : SM_SLOC_BUFFER_BLOB));
1796   if (Compressed)
1797     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1798   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1799   return Stream.EmitAbbrev(std::move(Abbrev));
1800 }
1801 
1802 /// Create an abbreviation for the SLocEntry that refers to a macro
1803 /// expansion.
1804 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1805   using namespace llvm;
1806 
1807   auto Abbrev = std::make_shared<BitCodeAbbrev>();
1808   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1809   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1810   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1811   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
1812   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
1813   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1814   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1815   return Stream.EmitAbbrev(std::move(Abbrev));
1816 }
1817 
1818 /// Emit key length and data length as ULEB-encoded data, and return them as a
1819 /// pair.
1820 static std::pair<unsigned, unsigned>
1821 emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
1822   llvm::encodeULEB128(KeyLen, Out);
1823   llvm::encodeULEB128(DataLen, Out);
1824   return std::make_pair(KeyLen, DataLen);
1825 }
1826 
1827 namespace {
1828 
1829   // Trait used for the on-disk hash table of header search information.
1830   class HeaderFileInfoTrait {
1831     ASTWriter &Writer;
1832 
1833     // Keep track of the framework names we've used during serialization.
1834     SmallString<128> FrameworkStringData;
1835     llvm::StringMap<unsigned> FrameworkNameOffset;
1836 
1837   public:
1838     HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1839 
1840     struct key_type {
1841       StringRef Filename;
1842       off_t Size;
1843       time_t ModTime;
1844     };
1845     using key_type_ref = const key_type &;
1846 
1847     using UnresolvedModule =
1848         llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1849 
1850     struct data_type {
1851       const HeaderFileInfo &HFI;
1852       bool AlreadyIncluded;
1853       ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1854       UnresolvedModule Unresolved;
1855     };
1856     using data_type_ref = const data_type &;
1857 
1858     using hash_value_type = unsigned;
1859     using offset_type = unsigned;
1860 
1861     hash_value_type ComputeHash(key_type_ref key) {
1862       // The hash is based only on size/time of the file, so that the reader can
1863       // match even when symlinking or excess path elements ("foo/../", "../")
1864       // change the form of the name. However, complete path is still the key.
1865       return llvm::hash_combine(key.Size, key.ModTime);
1866     }
1867 
1868     std::pair<unsigned, unsigned>
1869     EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1870       unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
1871       unsigned DataLen = 1 + 4 + 4;
1872       for (auto ModInfo : Data.KnownHeaders)
1873         if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1874           DataLen += 4;
1875       if (Data.Unresolved.getPointer())
1876         DataLen += 4;
1877       return emitULEBKeyDataLength(KeyLen, DataLen, Out);
1878     }
1879 
1880     void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1881       using namespace llvm::support;
1882 
1883       endian::Writer LE(Out, llvm::endianness::little);
1884       LE.write<uint64_t>(key.Size);
1885       KeyLen -= 8;
1886       LE.write<uint64_t>(key.ModTime);
1887       KeyLen -= 8;
1888       Out.write(key.Filename.data(), KeyLen);
1889     }
1890 
1891     void EmitData(raw_ostream &Out, key_type_ref key,
1892                   data_type_ref Data, unsigned DataLen) {
1893       using namespace llvm::support;
1894 
1895       endian::Writer LE(Out, llvm::endianness::little);
1896       uint64_t Start = Out.tell(); (void)Start;
1897 
1898       unsigned char Flags = (Data.AlreadyIncluded << 6)
1899                           | (Data.HFI.isImport << 5)
1900                           | (Writer.isWritingStdCXXNamedModules() ? 0 :
1901                              Data.HFI.isPragmaOnce << 4)
1902                           | (Data.HFI.DirInfo << 1)
1903                           | Data.HFI.IndexHeaderMapHeader;
1904       LE.write<uint8_t>(Flags);
1905 
1906       if (!Data.HFI.ControllingMacro)
1907         LE.write<uint32_t>(Data.HFI.ControllingMacroID);
1908       else
1909         LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1910 
1911       unsigned Offset = 0;
1912       if (!Data.HFI.Framework.empty()) {
1913         // If this header refers into a framework, save the framework name.
1914         llvm::StringMap<unsigned>::iterator Pos
1915           = FrameworkNameOffset.find(Data.HFI.Framework);
1916         if (Pos == FrameworkNameOffset.end()) {
1917           Offset = FrameworkStringData.size() + 1;
1918           FrameworkStringData.append(Data.HFI.Framework);
1919           FrameworkStringData.push_back(0);
1920 
1921           FrameworkNameOffset[Data.HFI.Framework] = Offset;
1922         } else
1923           Offset = Pos->second;
1924       }
1925       LE.write<uint32_t>(Offset);
1926 
1927       auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1928         if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1929           uint32_t Value = (ModID << 3) | (unsigned)Role;
1930           assert((Value >> 3) == ModID && "overflow in header module info");
1931           LE.write<uint32_t>(Value);
1932         }
1933       };
1934 
1935       for (auto ModInfo : Data.KnownHeaders)
1936         EmitModule(ModInfo.getModule(), ModInfo.getRole());
1937       if (Data.Unresolved.getPointer())
1938         EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
1939 
1940       assert(Out.tell() - Start == DataLen && "Wrong data length");
1941     }
1942 
1943     const char *strings_begin() const { return FrameworkStringData.begin(); }
1944     const char *strings_end() const { return FrameworkStringData.end(); }
1945   };
1946 
1947 } // namespace
1948 
1949 /// Write the header search block for the list of files that
1950 ///
1951 /// \param HS The header search structure to save.
1952 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1953   HeaderFileInfoTrait GeneratorTrait(*this);
1954   llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1955   SmallVector<const char *, 4> SavedStrings;
1956   unsigned NumHeaderSearchEntries = 0;
1957 
1958   // Find all unresolved headers for the current module. We generally will
1959   // have resolved them before we get here, but not necessarily: we might be
1960   // compiling a preprocessed module, where there is no requirement for the
1961   // original files to exist any more.
1962   const HeaderFileInfo Empty; // So we can take a reference.
1963   if (WritingModule) {
1964     llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1965     while (!Worklist.empty()) {
1966       Module *M = Worklist.pop_back_val();
1967       // We don't care about headers in unimportable submodules.
1968       if (M->isUnimportable())
1969         continue;
1970 
1971       // Map to disk files where possible, to pick up any missing stat
1972       // information. This also means we don't need to check the unresolved
1973       // headers list when emitting resolved headers in the first loop below.
1974       // FIXME: It'd be preferable to avoid doing this if we were given
1975       // sufficient stat information in the module map.
1976       HS.getModuleMap().resolveHeaderDirectives(M, /*File=*/std::nullopt);
1977 
1978       // If the file didn't exist, we can still create a module if we were given
1979       // enough information in the module map.
1980       for (const auto &U : M->MissingHeaders) {
1981         // Check that we were given enough information to build a module
1982         // without this file existing on disk.
1983         if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1984           PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1985               << WritingModule->getFullModuleName() << U.Size.has_value()
1986               << U.FileName;
1987           continue;
1988         }
1989 
1990         // Form the effective relative pathname for the file.
1991         SmallString<128> Filename(M->Directory->getName());
1992         llvm::sys::path::append(Filename, U.FileName);
1993         PreparePathForOutput(Filename);
1994 
1995         StringRef FilenameDup = strdup(Filename.c_str());
1996         SavedStrings.push_back(FilenameDup.data());
1997 
1998         HeaderFileInfoTrait::key_type Key = {
1999             FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0};
2000         HeaderFileInfoTrait::data_type Data = {
2001             Empty, false, {}, {M, ModuleMap::headerKindToRole(U.Kind)}};
2002         // FIXME: Deal with cases where there are multiple unresolved header
2003         // directives in different submodules for the same header.
2004         Generator.insert(Key, Data, GeneratorTrait);
2005         ++NumHeaderSearchEntries;
2006       }
2007       auto SubmodulesRange = M->submodules();
2008       Worklist.append(SubmodulesRange.begin(), SubmodulesRange.end());
2009     }
2010   }
2011 
2012   SmallVector<OptionalFileEntryRef, 16> FilesByUID;
2013   HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
2014 
2015   if (FilesByUID.size() > HS.header_file_size())
2016     FilesByUID.resize(HS.header_file_size());
2017 
2018   for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2019     OptionalFileEntryRef File = FilesByUID[UID];
2020     if (!File)
2021       continue;
2022 
2023     // Get the file info. This will load info from the external source if
2024     // necessary. Skip emitting this file if we have no information on it
2025     // as a header file (in which case HFI will be null) or if it hasn't
2026     // changed since it was loaded. Also skip it if it's for a modular header
2027     // from a different module; in that case, we rely on the module(s)
2028     // containing the header to provide this information.
2029     const HeaderFileInfo *HFI =
2030         HS.getExistingFileInfo(*File, /*WantExternal*/!Chain);
2031     if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
2032       continue;
2033 
2034     // Massage the file path into an appropriate form.
2035     StringRef Filename = File->getName();
2036     SmallString<128> FilenameTmp(Filename);
2037     if (PreparePathForOutput(FilenameTmp)) {
2038       // If we performed any translation on the file name at all, we need to
2039       // save this string, since the generator will refer to it later.
2040       Filename = StringRef(strdup(FilenameTmp.c_str()));
2041       SavedStrings.push_back(Filename.data());
2042     }
2043 
2044     bool Included = PP->alreadyIncluded(*File);
2045 
2046     HeaderFileInfoTrait::key_type Key = {
2047       Filename, File->getSize(), getTimestampForOutput(*File)
2048     };
2049     HeaderFileInfoTrait::data_type Data = {
2050       *HFI, Included, HS.getModuleMap().findResolvedModulesForHeader(*File), {}
2051     };
2052     Generator.insert(Key, Data, GeneratorTrait);
2053     ++NumHeaderSearchEntries;
2054   }
2055 
2056   // Create the on-disk hash table in a buffer.
2057   SmallString<4096> TableData;
2058   uint32_t BucketOffset;
2059   {
2060     using namespace llvm::support;
2061 
2062     llvm::raw_svector_ostream Out(TableData);
2063     // Make sure that no bucket is at offset 0
2064     endian::write<uint32_t>(Out, 0, llvm::endianness::little);
2065     BucketOffset = Generator.Emit(Out, GeneratorTrait);
2066   }
2067 
2068   // Create a blob abbreviation
2069   using namespace llvm;
2070 
2071   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2072   Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2073   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2074   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2075   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2076   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2077   unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2078 
2079   // Write the header search table
2080   RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2081                                      NumHeaderSearchEntries, TableData.size()};
2082   TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
2083   Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
2084 
2085   // Free all of the strings we had to duplicate.
2086   for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
2087     free(const_cast<char *>(SavedStrings[I]));
2088 }
2089 
2090 static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2091                      unsigned SLocBufferBlobCompressedAbbrv,
2092                      unsigned SLocBufferBlobAbbrv) {
2093   using RecordDataType = ASTWriter::RecordData::value_type;
2094 
2095   // Compress the buffer if possible. We expect that almost all PCM
2096   // consumers will not want its contents.
2097   SmallVector<uint8_t, 0> CompressedBuffer;
2098   if (llvm::compression::zstd::isAvailable()) {
2099     llvm::compression::zstd::compress(
2100         llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer, 9);
2101     RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2102     Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2103                               llvm::toStringRef(CompressedBuffer));
2104     return;
2105   }
2106   if (llvm::compression::zlib::isAvailable()) {
2107     llvm::compression::zlib::compress(
2108         llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer);
2109     RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2110     Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2111                               llvm::toStringRef(CompressedBuffer));
2112     return;
2113   }
2114 
2115   RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2116   Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2117 }
2118 
2119 /// Writes the block containing the serialized form of the
2120 /// source manager.
2121 ///
2122 /// TODO: We should probably use an on-disk hash table (stored in a
2123 /// blob), indexed based on the file name, so that we only create
2124 /// entries for files that we actually need. In the common case (no
2125 /// errors), we probably won't have to create file entries for any of
2126 /// the files in the AST.
2127 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2128                                         const Preprocessor &PP) {
2129   RecordData Record;
2130 
2131   // Enter the source manager block.
2132   Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
2133   const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
2134 
2135   // Abbreviations for the various kinds of source-location entries.
2136   unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2137   unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
2138   unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2139   unsigned SLocBufferBlobCompressedAbbrv =
2140       CreateSLocBufferBlobAbbrev(Stream, true);
2141   unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
2142 
2143   // Write out the source location entry table. We skip the first
2144   // entry, which is always the same dummy entry.
2145   std::vector<uint32_t> SLocEntryOffsets;
2146   uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
2147   SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2148   for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
2149        I != N; ++I) {
2150     // Get this source location entry.
2151     const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
2152     FileID FID = FileID::get(I);
2153     assert(&SourceMgr.getSLocEntry(FID) == SLoc);
2154 
2155     // Record the offset of this source-location entry.
2156     uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
2157     assert((Offset >> 32) == 0 && "SLocEntry offset too large");
2158 
2159     // Figure out which record code to use.
2160     unsigned Code;
2161     if (SLoc->isFile()) {
2162       const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
2163       if (Cache->OrigEntry) {
2164         Code = SM_SLOC_FILE_ENTRY;
2165       } else
2166         Code = SM_SLOC_BUFFER_ENTRY;
2167     } else
2168       Code = SM_SLOC_EXPANSION_ENTRY;
2169     Record.clear();
2170     Record.push_back(Code);
2171 
2172     if (SLoc->isFile()) {
2173       const SrcMgr::FileInfo &File = SLoc->getFile();
2174       const SrcMgr::ContentCache *Content = &File.getContentCache();
2175       // Do not emit files that were not listed as inputs.
2176       if (!IsSLocAffecting[I])
2177         continue;
2178       SLocEntryOffsets.push_back(Offset);
2179       // Starting offset of this entry within this module, so skip the dummy.
2180       Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2181       AddSourceLocation(File.getIncludeLoc(), Record);
2182       Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2183       Record.push_back(File.hasLineDirectives());
2184 
2185       bool EmitBlob = false;
2186       if (Content->OrigEntry) {
2187         assert(Content->OrigEntry == Content->ContentsEntry &&
2188                "Writing to AST an overridden file is not supported");
2189 
2190         // The source location entry is a file. Emit input file ID.
2191         assert(InputFileIDs[*Content->OrigEntry] != 0 && "Missed file entry");
2192         Record.push_back(InputFileIDs[*Content->OrigEntry]);
2193 
2194         Record.push_back(getAdjustedNumCreatedFIDs(FID));
2195 
2196         FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
2197         if (FDI != FileDeclIDs.end()) {
2198           Record.push_back(FDI->second->FirstDeclIndex);
2199           Record.push_back(FDI->second->DeclIDs.size());
2200         } else {
2201           Record.push_back(0);
2202           Record.push_back(0);
2203         }
2204 
2205         Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2206 
2207         if (Content->BufferOverridden || Content->IsTransient)
2208           EmitBlob = true;
2209       } else {
2210         // The source location entry is a buffer. The blob associated
2211         // with this entry contains the contents of the buffer.
2212 
2213         // We add one to the size so that we capture the trailing NULL
2214         // that is required by llvm::MemoryBuffer::getMemBuffer (on
2215         // the reader side).
2216         std::optional<llvm::MemoryBufferRef> Buffer =
2217             Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2218         StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2219         Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
2220                                   StringRef(Name.data(), Name.size() + 1));
2221         EmitBlob = true;
2222       }
2223 
2224       if (EmitBlob) {
2225         // Include the implicit terminating null character in the on-disk buffer
2226         // if we're writing it uncompressed.
2227         std::optional<llvm::MemoryBufferRef> Buffer =
2228             Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2229         if (!Buffer)
2230           Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2231         StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2232         emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2233                  SLocBufferBlobAbbrv);
2234       }
2235     } else {
2236       // The source location entry is a macro expansion.
2237       const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2238       SLocEntryOffsets.push_back(Offset);
2239       // Starting offset of this entry within this module, so skip the dummy.
2240       Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2241       LocSeq::State Seq;
2242       AddSourceLocation(Expansion.getSpellingLoc(), Record, Seq);
2243       AddSourceLocation(Expansion.getExpansionLocStart(), Record, Seq);
2244       AddSourceLocation(Expansion.isMacroArgExpansion()
2245                             ? SourceLocation()
2246                             : Expansion.getExpansionLocEnd(),
2247                         Record, Seq);
2248       Record.push_back(Expansion.isExpansionTokenRange());
2249 
2250       // Compute the token length for this macro expansion.
2251       SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
2252       if (I + 1 != N)
2253         NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2254       Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
2255       Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
2256     }
2257   }
2258 
2259   Stream.ExitBlock();
2260 
2261   if (SLocEntryOffsets.empty())
2262     return;
2263 
2264   // Write the source-location offsets table into the AST block. This
2265   // table is used for lazily loading source-location information.
2266   using namespace llvm;
2267 
2268   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2269   Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2270   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2271   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2272   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2273   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2274   unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2275   {
2276     RecordData::value_type Record[] = {
2277         SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2278         getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2279         SLocEntryOffsetsBase - SourceManagerBlockOffset};
2280     Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2281                               bytes(SLocEntryOffsets));
2282   }
2283 
2284   // Write the line table. It depends on remapping working, so it must come
2285   // after the source location offsets.
2286   if (SourceMgr.hasLineTable()) {
2287     LineTableInfo &LineTable = SourceMgr.getLineTable();
2288 
2289     Record.clear();
2290 
2291     // Emit the needed file names.
2292     llvm::DenseMap<int, int> FilenameMap;
2293     FilenameMap[-1] = -1; // For unspecified filenames.
2294     for (const auto &L : LineTable) {
2295       if (L.first.ID < 0)
2296         continue;
2297       for (auto &LE : L.second) {
2298         if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2299                                               FilenameMap.size() - 1)).second)
2300           AddPath(LineTable.getFilename(LE.FilenameID), Record);
2301       }
2302     }
2303     Record.push_back(0);
2304 
2305     // Emit the line entries
2306     for (const auto &L : LineTable) {
2307       // Only emit entries for local files.
2308       if (L.first.ID < 0)
2309         continue;
2310 
2311       AddFileID(L.first, Record);
2312 
2313       // Emit the line entries
2314       Record.push_back(L.second.size());
2315       for (const auto &LE : L.second) {
2316         Record.push_back(LE.FileOffset);
2317         Record.push_back(LE.LineNo);
2318         Record.push_back(FilenameMap[LE.FilenameID]);
2319         Record.push_back((unsigned)LE.FileKind);
2320         Record.push_back(LE.IncludeOffset);
2321       }
2322     }
2323 
2324     Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2325   }
2326 }
2327 
2328 //===----------------------------------------------------------------------===//
2329 // Preprocessor Serialization
2330 //===----------------------------------------------------------------------===//
2331 
2332 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2333                               const Preprocessor &PP) {
2334   if (MacroInfo *MI = MD->getMacroInfo())
2335     if (MI->isBuiltinMacro())
2336       return true;
2337 
2338   if (IsModule) {
2339     SourceLocation Loc = MD->getLocation();
2340     if (Loc.isInvalid())
2341       return true;
2342     if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2343       return true;
2344   }
2345 
2346   return false;
2347 }
2348 
2349 /// Writes the block containing the serialized form of the
2350 /// preprocessor.
2351 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2352   uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2353 
2354   PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2355   if (PPRec)
2356     WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
2357 
2358   RecordData Record;
2359   RecordData ModuleMacroRecord;
2360 
2361   // If the preprocessor __COUNTER__ value has been bumped, remember it.
2362   if (PP.getCounterValue() != 0) {
2363     RecordData::value_type Record[] = {PP.getCounterValue()};
2364     Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2365   }
2366 
2367   // If we have a recorded #pragma assume_nonnull, remember it so it can be
2368   // replayed when the preamble terminates into the main file.
2369   SourceLocation AssumeNonNullLoc =
2370       PP.getPreambleRecordedPragmaAssumeNonNullLoc();
2371   if (AssumeNonNullLoc.isValid()) {
2372     assert(PP.isRecordingPreamble());
2373     AddSourceLocation(AssumeNonNullLoc, Record);
2374     Stream.EmitRecord(PP_ASSUME_NONNULL_LOC, Record);
2375     Record.clear();
2376   }
2377 
2378   if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2379     assert(!IsModule);
2380     auto SkipInfo = PP.getPreambleSkipInfo();
2381     if (SkipInfo) {
2382       Record.push_back(true);
2383       AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2384       AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2385       Record.push_back(SkipInfo->FoundNonSkipPortion);
2386       Record.push_back(SkipInfo->FoundElse);
2387       AddSourceLocation(SkipInfo->ElseLoc, Record);
2388     } else {
2389       Record.push_back(false);
2390     }
2391     for (const auto &Cond : PP.getPreambleConditionalStack()) {
2392       AddSourceLocation(Cond.IfLoc, Record);
2393       Record.push_back(Cond.WasSkipping);
2394       Record.push_back(Cond.FoundNonSkip);
2395       Record.push_back(Cond.FoundElse);
2396     }
2397     Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2398     Record.clear();
2399   }
2400 
2401   // Enter the preprocessor block.
2402   Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2403 
2404   // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2405   // FIXME: Include a location for the use, and say which one was used.
2406   if (PP.SawDateOrTime())
2407     PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2408 
2409   // Loop over all the macro directives that are live at the end of the file,
2410   // emitting each to the PP section.
2411 
2412   // Construct the list of identifiers with macro directives that need to be
2413   // serialized.
2414   SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2415   // It is meaningless to emit macros for named modules. It only wastes times
2416   // and spaces.
2417   if (!isWritingStdCXXNamedModules())
2418     for (auto &Id : PP.getIdentifierTable())
2419       if (Id.second->hadMacroDefinition() &&
2420           (!Id.second->isFromAST() ||
2421           Id.second->hasChangedSinceDeserialization()))
2422         MacroIdentifiers.push_back(Id.second);
2423   // Sort the set of macro definitions that need to be serialized by the
2424   // name of the macro, to provide a stable ordering.
2425   llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2426 
2427   // Emit the macro directives as a list and associate the offset with the
2428   // identifier they belong to.
2429   for (const IdentifierInfo *Name : MacroIdentifiers) {
2430     MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2431     uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2432     assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2433 
2434     // Write out any exported module macros.
2435     bool EmittedModuleMacros = false;
2436     // C+=20 Header Units are compiled module interfaces, but they preserve
2437     // macros that are live (i.e. have a defined value) at the end of the
2438     // compilation.  So when writing a header unit, we preserve only the final
2439     // value of each macro (and discard any that are undefined).  Header units
2440     // do not have sub-modules (although they might import other header units).
2441     // PCH files, conversely, retain the history of each macro's define/undef
2442     // and of leaf macros in sub modules.
2443     if (IsModule && WritingModule->isHeaderUnit()) {
2444       // This is for the main TU when it is a C++20 header unit.
2445       // We preserve the final state of defined macros, and we do not emit ones
2446       // that are undefined.
2447       if (!MD || shouldIgnoreMacro(MD, IsModule, PP) ||
2448           MD->getKind() == MacroDirective::MD_Undefine)
2449         continue;
2450       AddSourceLocation(MD->getLocation(), Record);
2451       Record.push_back(MD->getKind());
2452       if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2453         Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2454       } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2455         Record.push_back(VisMD->isPublic());
2456       }
2457       ModuleMacroRecord.push_back(getSubmoduleID(WritingModule));
2458       ModuleMacroRecord.push_back(getMacroRef(MD->getMacroInfo(), Name));
2459       Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2460       ModuleMacroRecord.clear();
2461       EmittedModuleMacros = true;
2462     } else {
2463       // Emit the macro directives in reverse source order.
2464       for (; MD; MD = MD->getPrevious()) {
2465         // Once we hit an ignored macro, we're done: the rest of the chain
2466         // will all be ignored macros.
2467         if (shouldIgnoreMacro(MD, IsModule, PP))
2468           break;
2469         AddSourceLocation(MD->getLocation(), Record);
2470         Record.push_back(MD->getKind());
2471         if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2472           Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2473         } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2474           Record.push_back(VisMD->isPublic());
2475         }
2476       }
2477 
2478       // We write out exported module macros for PCH as well.
2479       auto Leafs = PP.getLeafModuleMacros(Name);
2480       SmallVector<ModuleMacro *, 8> Worklist(Leafs.begin(), Leafs.end());
2481       llvm::DenseMap<ModuleMacro *, unsigned> Visits;
2482       while (!Worklist.empty()) {
2483         auto *Macro = Worklist.pop_back_val();
2484 
2485         // Emit a record indicating this submodule exports this macro.
2486         ModuleMacroRecord.push_back(getSubmoduleID(Macro->getOwningModule()));
2487         ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2488         for (auto *M : Macro->overrides())
2489           ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2490 
2491         Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2492         ModuleMacroRecord.clear();
2493 
2494         // Enqueue overridden macros once we've visited all their ancestors.
2495         for (auto *M : Macro->overrides())
2496           if (++Visits[M] == M->getNumOverridingMacros())
2497             Worklist.push_back(M);
2498 
2499         EmittedModuleMacros = true;
2500       }
2501     }
2502     if (Record.empty() && !EmittedModuleMacros)
2503       continue;
2504 
2505     IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2506     Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2507     Record.clear();
2508   }
2509 
2510   /// Offsets of each of the macros into the bitstream, indexed by
2511   /// the local macro ID
2512   ///
2513   /// For each identifier that is associated with a macro, this map
2514   /// provides the offset into the bitstream where that macro is
2515   /// defined.
2516   std::vector<uint32_t> MacroOffsets;
2517 
2518   for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2519     const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2520     MacroInfo *MI = MacroInfosToEmit[I].MI;
2521     MacroID ID = MacroInfosToEmit[I].ID;
2522 
2523     if (ID < FirstMacroID) {
2524       assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2525       continue;
2526     }
2527 
2528     // Record the local offset of this macro.
2529     unsigned Index = ID - FirstMacroID;
2530     if (Index >= MacroOffsets.size())
2531       MacroOffsets.resize(Index + 1);
2532 
2533     uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2534     assert((Offset >> 32) == 0 && "Macro offset too large");
2535     MacroOffsets[Index] = Offset;
2536 
2537     AddIdentifierRef(Name, Record);
2538     AddSourceLocation(MI->getDefinitionLoc(), Record);
2539     AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2540     Record.push_back(MI->isUsed());
2541     Record.push_back(MI->isUsedForHeaderGuard());
2542     Record.push_back(MI->getNumTokens());
2543     unsigned Code;
2544     if (MI->isObjectLike()) {
2545       Code = PP_MACRO_OBJECT_LIKE;
2546     } else {
2547       Code = PP_MACRO_FUNCTION_LIKE;
2548 
2549       Record.push_back(MI->isC99Varargs());
2550       Record.push_back(MI->isGNUVarargs());
2551       Record.push_back(MI->hasCommaPasting());
2552       Record.push_back(MI->getNumParams());
2553       for (const IdentifierInfo *Param : MI->params())
2554         AddIdentifierRef(Param, Record);
2555     }
2556 
2557     // If we have a detailed preprocessing record, record the macro definition
2558     // ID that corresponds to this macro.
2559     if (PPRec)
2560       Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2561 
2562     Stream.EmitRecord(Code, Record);
2563     Record.clear();
2564 
2565     // Emit the tokens array.
2566     for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2567       // Note that we know that the preprocessor does not have any annotation
2568       // tokens in it because they are created by the parser, and thus can't
2569       // be in a macro definition.
2570       const Token &Tok = MI->getReplacementToken(TokNo);
2571       AddToken(Tok, Record);
2572       Stream.EmitRecord(PP_TOKEN, Record);
2573       Record.clear();
2574     }
2575     ++NumMacros;
2576   }
2577 
2578   Stream.ExitBlock();
2579 
2580   // Write the offsets table for macro IDs.
2581   using namespace llvm;
2582 
2583   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2584   Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2585   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2586   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2587   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32));   // base offset
2588   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2589 
2590   unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2591   {
2592     RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2593                                        FirstMacroID - NUM_PREDEF_MACRO_IDS,
2594                                        MacroOffsetsBase - ASTBlockStartOffset};
2595     Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2596   }
2597 }
2598 
2599 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2600                                         uint64_t MacroOffsetsBase) {
2601   if (PPRec.local_begin() == PPRec.local_end())
2602     return;
2603 
2604   SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2605 
2606   // Enter the preprocessor block.
2607   Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2608 
2609   // If the preprocessor has a preprocessing record, emit it.
2610   unsigned NumPreprocessingRecords = 0;
2611   using namespace llvm;
2612 
2613   // Set up the abbreviation for
2614   unsigned InclusionAbbrev = 0;
2615   {
2616     auto Abbrev = std::make_shared<BitCodeAbbrev>();
2617     Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2618     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2619     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2620     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2621     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2622     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2623     InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2624   }
2625 
2626   unsigned FirstPreprocessorEntityID
2627     = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2628     + NUM_PREDEF_PP_ENTITY_IDS;
2629   unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2630   RecordData Record;
2631   for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2632                                   EEnd = PPRec.local_end();
2633        E != EEnd;
2634        (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2635     Record.clear();
2636 
2637     uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2638     assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2639     PreprocessedEntityOffsets.push_back(
2640         PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
2641 
2642     if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2643       // Record this macro definition's ID.
2644       MacroDefinitions[MD] = NextPreprocessorEntityID;
2645 
2646       AddIdentifierRef(MD->getName(), Record);
2647       Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2648       continue;
2649     }
2650 
2651     if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2652       Record.push_back(ME->isBuiltinMacro());
2653       if (ME->isBuiltinMacro())
2654         AddIdentifierRef(ME->getName(), Record);
2655       else
2656         Record.push_back(MacroDefinitions[ME->getDefinition()]);
2657       Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2658       continue;
2659     }
2660 
2661     if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2662       Record.push_back(PPD_INCLUSION_DIRECTIVE);
2663       Record.push_back(ID->getFileName().size());
2664       Record.push_back(ID->wasInQuotes());
2665       Record.push_back(static_cast<unsigned>(ID->getKind()));
2666       Record.push_back(ID->importedModule());
2667       SmallString<64> Buffer;
2668       Buffer += ID->getFileName();
2669       // Check that the FileEntry is not null because it was not resolved and
2670       // we create a PCH even with compiler errors.
2671       if (ID->getFile())
2672         Buffer += ID->getFile()->getName();
2673       Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2674       continue;
2675     }
2676 
2677     llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2678   }
2679   Stream.ExitBlock();
2680 
2681   // Write the offsets table for the preprocessing record.
2682   if (NumPreprocessingRecords > 0) {
2683     assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2684 
2685     // Write the offsets table for identifier IDs.
2686     using namespace llvm;
2687 
2688     auto Abbrev = std::make_shared<BitCodeAbbrev>();
2689     Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2690     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2691     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2692     unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2693 
2694     RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2695                                        FirstPreprocessorEntityID -
2696                                            NUM_PREDEF_PP_ENTITY_IDS};
2697     Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2698                               bytes(PreprocessedEntityOffsets));
2699   }
2700 
2701   // Write the skipped region table for the preprocessing record.
2702   ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2703   if (SkippedRanges.size() > 0) {
2704     std::vector<PPSkippedRange> SerializedSkippedRanges;
2705     SerializedSkippedRanges.reserve(SkippedRanges.size());
2706     for (auto const& Range : SkippedRanges)
2707       SerializedSkippedRanges.emplace_back(Range);
2708 
2709     using namespace llvm;
2710     auto Abbrev = std::make_shared<BitCodeAbbrev>();
2711     Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2712     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2713     unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2714 
2715     Record.clear();
2716     Record.push_back(PPD_SKIPPED_RANGES);
2717     Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2718                               bytes(SerializedSkippedRanges));
2719   }
2720 }
2721 
2722 unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module *Mod) {
2723   if (!Mod)
2724     return 0;
2725 
2726   auto Known = SubmoduleIDs.find(Mod);
2727   if (Known != SubmoduleIDs.end())
2728     return Known->second;
2729 
2730   auto *Top = Mod->getTopLevelModule();
2731   if (Top != WritingModule &&
2732       (getLangOpts().CompilingPCH ||
2733        !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
2734     return 0;
2735 
2736   return SubmoduleIDs[Mod] = NextSubmoduleID++;
2737 }
2738 
2739 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2740   unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2741   // FIXME: This can easily happen, if we have a reference to a submodule that
2742   // did not result in us loading a module file for that submodule. For
2743   // instance, a cross-top-level-module 'conflict' declaration will hit this.
2744   // assert((ID || !Mod) &&
2745   //        "asked for module ID for non-local, non-imported module");
2746   return ID;
2747 }
2748 
2749 /// Compute the number of modules within the given tree (including the
2750 /// given module).
2751 static unsigned getNumberOfModules(Module *Mod) {
2752   unsigned ChildModules = 0;
2753   for (auto *Submodule : Mod->submodules())
2754     ChildModules += getNumberOfModules(Submodule);
2755 
2756   return ChildModules + 1;
2757 }
2758 
2759 void ASTWriter::WriteSubmodules(Module *WritingModule) {
2760   // Enter the submodule description block.
2761   Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2762 
2763   // Write the abbreviations needed for the submodules block.
2764   using namespace llvm;
2765 
2766   auto Abbrev = std::make_shared<BitCodeAbbrev>();
2767   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2768   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2769   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2770   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Kind
2771   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
2772   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2773   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2774   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2775   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2776   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2777   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2778   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2779   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2780   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2781   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NamedModuleHasN...
2782   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2783   unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2784 
2785   Abbrev = std::make_shared<BitCodeAbbrev>();
2786   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2787   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2788   unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2789 
2790   Abbrev = std::make_shared<BitCodeAbbrev>();
2791   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2792   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2793   unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2794 
2795   Abbrev = std::make_shared<BitCodeAbbrev>();
2796   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2797   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2798   unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2799 
2800   Abbrev = std::make_shared<BitCodeAbbrev>();
2801   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2802   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2803   unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2804 
2805   Abbrev = std::make_shared<BitCodeAbbrev>();
2806   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2807   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2808   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Feature
2809   unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2810 
2811   Abbrev = std::make_shared<BitCodeAbbrev>();
2812   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2813   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2814   unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2815 
2816   Abbrev = std::make_shared<BitCodeAbbrev>();
2817   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2818   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2819   unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2820 
2821   Abbrev = std::make_shared<BitCodeAbbrev>();
2822   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2823   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2824   unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2825 
2826   Abbrev = std::make_shared<BitCodeAbbrev>();
2827   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2828   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2829   unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2830 
2831   Abbrev = std::make_shared<BitCodeAbbrev>();
2832   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2833   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2834   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Name
2835   unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2836 
2837   Abbrev = std::make_shared<BitCodeAbbrev>();
2838   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2839   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2840   unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2841 
2842   Abbrev = std::make_shared<BitCodeAbbrev>();
2843   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2844   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));  // Other module
2845   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Message
2846   unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2847 
2848   Abbrev = std::make_shared<BitCodeAbbrev>();
2849   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2850   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2851   unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2852 
2853   // Write the submodule metadata block.
2854   RecordData::value_type Record[] = {
2855       getNumberOfModules(WritingModule),
2856       FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2857   Stream.EmitRecord(SUBMODULE_METADATA, Record);
2858 
2859   // Write all of the submodules.
2860   std::queue<Module *> Q;
2861   Q.push(WritingModule);
2862   while (!Q.empty()) {
2863     Module *Mod = Q.front();
2864     Q.pop();
2865     unsigned ID = getSubmoduleID(Mod);
2866 
2867     uint64_t ParentID = 0;
2868     if (Mod->Parent) {
2869       assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2870       ParentID = SubmoduleIDs[Mod->Parent];
2871     }
2872 
2873     uint64_t DefinitionLoc =
2874         SourceLocationEncoding::encode(getAdjustedLocation(Mod->DefinitionLoc));
2875 
2876     // Emit the definition of the block.
2877     {
2878       RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2879                                          ID,
2880                                          ParentID,
2881                                          (RecordData::value_type)Mod->Kind,
2882                                          DefinitionLoc,
2883                                          Mod->IsFramework,
2884                                          Mod->IsExplicit,
2885                                          Mod->IsSystem,
2886                                          Mod->IsExternC,
2887                                          Mod->InferSubmodules,
2888                                          Mod->InferExplicitSubmodules,
2889                                          Mod->InferExportWildcard,
2890                                          Mod->ConfigMacrosExhaustive,
2891                                          Mod->ModuleMapIsPrivate,
2892                                          Mod->NamedModuleHasInit};
2893       Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2894     }
2895 
2896     // Emit the requirements.
2897     for (const auto &R : Mod->Requirements) {
2898       RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2899       Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2900     }
2901 
2902     // Emit the umbrella header, if there is one.
2903     if (std::optional<Module::Header> UmbrellaHeader =
2904             Mod->getUmbrellaHeaderAsWritten()) {
2905       RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2906       Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2907                                 UmbrellaHeader->NameAsWritten);
2908     } else if (std::optional<Module::DirectoryName> UmbrellaDir =
2909                    Mod->getUmbrellaDirAsWritten()) {
2910       RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2911       Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2912                                 UmbrellaDir->NameAsWritten);
2913     }
2914 
2915     // Emit the headers.
2916     struct {
2917       unsigned RecordKind;
2918       unsigned Abbrev;
2919       Module::HeaderKind HeaderKind;
2920     } HeaderLists[] = {
2921       {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2922       {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2923       {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2924       {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2925         Module::HK_PrivateTextual},
2926       {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2927     };
2928     for (auto &HL : HeaderLists) {
2929       RecordData::value_type Record[] = {HL.RecordKind};
2930       for (auto &H : Mod->Headers[HL.HeaderKind])
2931         Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2932     }
2933 
2934     // Emit the top headers.
2935     {
2936       RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2937       for (FileEntryRef H : Mod->getTopHeaders(PP->getFileManager())) {
2938         SmallString<128> HeaderName(H.getName());
2939         PreparePathForOutput(HeaderName);
2940         Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
2941       }
2942     }
2943 
2944     // Emit the imports.
2945     if (!Mod->Imports.empty()) {
2946       RecordData Record;
2947       for (auto *I : Mod->Imports)
2948         Record.push_back(getSubmoduleID(I));
2949       Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2950     }
2951 
2952     // Emit the modules affecting compilation that were not imported.
2953     if (!Mod->AffectingClangModules.empty()) {
2954       RecordData Record;
2955       for (auto *I : Mod->AffectingClangModules)
2956         Record.push_back(getSubmoduleID(I));
2957       Stream.EmitRecord(SUBMODULE_AFFECTING_MODULES, Record);
2958     }
2959 
2960     // Emit the exports.
2961     if (!Mod->Exports.empty()) {
2962       RecordData Record;
2963       for (const auto &E : Mod->Exports) {
2964         // FIXME: This may fail; we don't require that all exported modules
2965         // are local or imported.
2966         Record.push_back(getSubmoduleID(E.getPointer()));
2967         Record.push_back(E.getInt());
2968       }
2969       Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2970     }
2971 
2972     //FIXME: How do we emit the 'use'd modules?  They may not be submodules.
2973     // Might be unnecessary as use declarations are only used to build the
2974     // module itself.
2975 
2976     // TODO: Consider serializing undeclared uses of modules.
2977 
2978     // Emit the link libraries.
2979     for (const auto &LL : Mod->LinkLibraries) {
2980       RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2981                                          LL.IsFramework};
2982       Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2983     }
2984 
2985     // Emit the conflicts.
2986     for (const auto &C : Mod->Conflicts) {
2987       // FIXME: This may fail; we don't require that all conflicting modules
2988       // are local or imported.
2989       RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2990                                          getSubmoduleID(C.Other)};
2991       Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2992     }
2993 
2994     // Emit the configuration macros.
2995     for (const auto &CM : Mod->ConfigMacros) {
2996       RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2997       Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
2998     }
2999 
3000     // Emit the initializers, if any.
3001     RecordData Inits;
3002     for (Decl *D : Context->getModuleInitializers(Mod))
3003       Inits.push_back(GetDeclRef(D));
3004     if (!Inits.empty())
3005       Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3006 
3007     // Emit the name of the re-exported module, if any.
3008     if (!Mod->ExportAsModule.empty()) {
3009       RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3010       Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3011     }
3012 
3013     // Queue up the submodules of this module.
3014     for (auto *M : Mod->submodules())
3015       Q.push(M);
3016   }
3017 
3018   Stream.ExitBlock();
3019 
3020   assert((NextSubmoduleID - FirstSubmoduleID ==
3021           getNumberOfModules(WritingModule)) &&
3022          "Wrong # of submodules; found a reference to a non-local, "
3023          "non-imported submodule?");
3024 }
3025 
3026 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3027                                               bool isModule) {
3028   llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3029       DiagStateIDMap;
3030   unsigned CurrID = 0;
3031   RecordData Record;
3032 
3033   auto EncodeDiagStateFlags =
3034       [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3035     unsigned Result = (unsigned)DS->ExtBehavior;
3036     for (unsigned Val :
3037          {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3038           (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3039           (unsigned)DS->SuppressSystemWarnings})
3040       Result = (Result << 1) | Val;
3041     return Result;
3042   };
3043 
3044   unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3045   Record.push_back(Flags);
3046 
3047   auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3048                           bool IncludeNonPragmaStates) {
3049     // Ensure that the diagnostic state wasn't modified since it was created.
3050     // We will not correctly round-trip this information otherwise.
3051     assert(Flags == EncodeDiagStateFlags(State) &&
3052            "diag state flags vary in single AST file");
3053 
3054     // If we ever serialize non-pragma mappings outside the initial state, the
3055     // code below will need to consider more than getDefaultMapping.
3056     assert(!IncludeNonPragmaStates ||
3057            State == Diag.DiagStatesByLoc.FirstDiagState);
3058 
3059     unsigned &DiagStateID = DiagStateIDMap[State];
3060     Record.push_back(DiagStateID);
3061 
3062     if (DiagStateID == 0) {
3063       DiagStateID = ++CurrID;
3064       SmallVector<std::pair<unsigned, DiagnosticMapping>> Mappings;
3065 
3066       // Add a placeholder for the number of mappings.
3067       auto SizeIdx = Record.size();
3068       Record.emplace_back();
3069       for (const auto &I : *State) {
3070         // Maybe skip non-pragmas.
3071         if (!I.second.isPragma() && !IncludeNonPragmaStates)
3072           continue;
3073         // Skip default mappings. We have a mapping for every diagnostic ever
3074         // emitted, regardless of whether it was customized.
3075         if (!I.second.isPragma() &&
3076             I.second == DiagnosticIDs::getDefaultMapping(I.first))
3077           continue;
3078         Mappings.push_back(I);
3079       }
3080 
3081       // Sort by diag::kind for deterministic output.
3082       llvm::sort(Mappings, [](const auto &LHS, const auto &RHS) {
3083         return LHS.first < RHS.first;
3084       });
3085 
3086       for (const auto &I : Mappings) {
3087         Record.push_back(I.first);
3088         Record.push_back(I.second.serialize());
3089       }
3090       // Update the placeholder.
3091       Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
3092     }
3093   };
3094 
3095   AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
3096 
3097   // Reserve a spot for the number of locations with state transitions.
3098   auto NumLocationsIdx = Record.size();
3099   Record.emplace_back();
3100 
3101   // Emit the state transitions.
3102   unsigned NumLocations = 0;
3103   for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3104     if (!FileIDAndFile.first.isValid() ||
3105         !FileIDAndFile.second.HasLocalTransitions)
3106       continue;
3107     ++NumLocations;
3108 
3109     SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3110     assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3111     AddSourceLocation(Loc, Record);
3112 
3113     Record.push_back(FileIDAndFile.second.StateTransitions.size());
3114     for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3115       Record.push_back(getAdjustedOffset(StatePoint.Offset));
3116       AddDiagState(StatePoint.State, false);
3117     }
3118   }
3119 
3120   // Backpatch the number of locations.
3121   Record[NumLocationsIdx] = NumLocations;
3122 
3123   // Emit CurDiagStateLoc.  Do it last in order to match source order.
3124   //
3125   // This also protects against a hypothetical corner case with simulating
3126   // -Werror settings for implicit modules in the ASTReader, where reading
3127   // CurDiagState out of context could change whether warning pragmas are
3128   // treated as errors.
3129   AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3130   AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3131 
3132   Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
3133 }
3134 
3135 //===----------------------------------------------------------------------===//
3136 // Type Serialization
3137 //===----------------------------------------------------------------------===//
3138 
3139 /// Write the representation of a type to the AST stream.
3140 void ASTWriter::WriteType(QualType T) {
3141   TypeIdx &IdxRef = TypeIdxs[T];
3142   if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3143     IdxRef = TypeIdx(NextTypeID++);
3144   TypeIdx Idx = IdxRef;
3145 
3146   assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
3147 
3148   // Emit the type's representation.
3149   uint64_t Offset = ASTTypeWriter(*this).write(T) - DeclTypesBlockStartOffset;
3150 
3151   // Record the offset for this type.
3152   unsigned Index = Idx.getIndex() - FirstTypeID;
3153   if (TypeOffsets.size() == Index)
3154     TypeOffsets.emplace_back(Offset);
3155   else if (TypeOffsets.size() < Index) {
3156     TypeOffsets.resize(Index + 1);
3157     TypeOffsets[Index].setBitOffset(Offset);
3158   } else {
3159     llvm_unreachable("Types emitted in wrong order");
3160   }
3161 }
3162 
3163 //===----------------------------------------------------------------------===//
3164 // Declaration Serialization
3165 //===----------------------------------------------------------------------===//
3166 
3167 /// Write the block containing all of the declaration IDs
3168 /// lexically declared within the given DeclContext.
3169 ///
3170 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3171 /// bitstream, or 0 if no block was written.
3172 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3173                                                  DeclContext *DC) {
3174   if (DC->decls_empty())
3175     return 0;
3176 
3177   uint64_t Offset = Stream.GetCurrentBitNo();
3178   SmallVector<uint32_t, 128> KindDeclPairs;
3179   for (const auto *D : DC->decls()) {
3180     KindDeclPairs.push_back(D->getKind());
3181     KindDeclPairs.push_back(GetDeclRef(D));
3182   }
3183 
3184   ++NumLexicalDeclContexts;
3185   RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3186   Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3187                             bytes(KindDeclPairs));
3188   return Offset;
3189 }
3190 
3191 void ASTWriter::WriteTypeDeclOffsets() {
3192   using namespace llvm;
3193 
3194   // Write the type offsets array
3195   auto Abbrev = std::make_shared<BitCodeAbbrev>();
3196   Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
3197   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3198   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
3199   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3200   unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3201   {
3202     RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3203                                        FirstTypeID - NUM_PREDEF_TYPE_IDS};
3204     Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3205   }
3206 
3207   // Write the declaration offsets array
3208   Abbrev = std::make_shared<BitCodeAbbrev>();
3209   Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
3210   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3211   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
3212   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3213   unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3214   {
3215     RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3216                                        FirstDeclID - NUM_PREDEF_DECL_IDS};
3217     Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3218   }
3219 }
3220 
3221 void ASTWriter::WriteFileDeclIDsMap() {
3222   using namespace llvm;
3223 
3224   SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
3225   SortedFileDeclIDs.reserve(FileDeclIDs.size());
3226   for (const auto &P : FileDeclIDs)
3227     SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
3228   llvm::sort(SortedFileDeclIDs, llvm::less_first());
3229 
3230   // Join the vectors of DeclIDs from all files.
3231   SmallVector<DeclID, 256> FileGroupedDeclIDs;
3232   for (auto &FileDeclEntry : SortedFileDeclIDs) {
3233     DeclIDInFileInfo &Info = *FileDeclEntry.second;
3234     Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3235     llvm::stable_sort(Info.DeclIDs);
3236     for (auto &LocDeclEntry : Info.DeclIDs)
3237       FileGroupedDeclIDs.push_back(LocDeclEntry.second);
3238   }
3239 
3240   auto Abbrev = std::make_shared<BitCodeAbbrev>();
3241   Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
3242   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3243   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3244   unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
3245   RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3246                                      FileGroupedDeclIDs.size()};
3247   Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
3248 }
3249 
3250 void ASTWriter::WriteComments() {
3251   Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
3252   auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3253   if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3254     return;
3255 
3256   // Don't write comments to BMI to reduce the size of BMI.
3257   // If language services (e.g., clangd) want such abilities,
3258   // we can offer a special option then.
3259   if (isWritingStdCXXNamedModules())
3260     return;
3261 
3262   RecordData Record;
3263   for (const auto &FO : Context->Comments.OrderedComments) {
3264     for (const auto &OC : FO.second) {
3265       const RawComment *I = OC.second;
3266       Record.clear();
3267       AddSourceRange(I->getSourceRange(), Record);
3268       Record.push_back(I->getKind());
3269       Record.push_back(I->isTrailingComment());
3270       Record.push_back(I->isAlmostTrailingComment());
3271       Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3272     }
3273   }
3274 }
3275 
3276 //===----------------------------------------------------------------------===//
3277 // Global Method Pool and Selector Serialization
3278 //===----------------------------------------------------------------------===//
3279 
3280 namespace {
3281 
3282 // Trait used for the on-disk hash table used in the method pool.
3283 class ASTMethodPoolTrait {
3284   ASTWriter &Writer;
3285 
3286 public:
3287   using key_type = Selector;
3288   using key_type_ref = key_type;
3289 
3290   struct data_type {
3291     SelectorID ID;
3292     ObjCMethodList Instance, Factory;
3293   };
3294   using data_type_ref = const data_type &;
3295 
3296   using hash_value_type = unsigned;
3297   using offset_type = unsigned;
3298 
3299   explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3300 
3301   static hash_value_type ComputeHash(Selector Sel) {
3302     return serialization::ComputeHash(Sel);
3303   }
3304 
3305   std::pair<unsigned, unsigned>
3306     EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3307                       data_type_ref Methods) {
3308     unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
3309     unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3310     for (const ObjCMethodList *Method = &Methods.Instance; Method;
3311          Method = Method->getNext())
3312       if (ShouldWriteMethodListNode(Method))
3313         DataLen += 4;
3314     for (const ObjCMethodList *Method = &Methods.Factory; Method;
3315          Method = Method->getNext())
3316       if (ShouldWriteMethodListNode(Method))
3317         DataLen += 4;
3318     return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3319   }
3320 
3321   void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3322     using namespace llvm::support;
3323 
3324     endian::Writer LE(Out, llvm::endianness::little);
3325     uint64_t Start = Out.tell();
3326     assert((Start >> 32) == 0 && "Selector key offset too large");
3327     Writer.SetSelectorOffset(Sel, Start);
3328     unsigned N = Sel.getNumArgs();
3329     LE.write<uint16_t>(N);
3330     if (N == 0)
3331       N = 1;
3332     for (unsigned I = 0; I != N; ++I)
3333       LE.write<uint32_t>(
3334           Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
3335   }
3336 
3337   void EmitData(raw_ostream& Out, key_type_ref,
3338                 data_type_ref Methods, unsigned DataLen) {
3339     using namespace llvm::support;
3340 
3341     endian::Writer LE(Out, llvm::endianness::little);
3342     uint64_t Start = Out.tell(); (void)Start;
3343     LE.write<uint32_t>(Methods.ID);
3344     unsigned NumInstanceMethods = 0;
3345     for (const ObjCMethodList *Method = &Methods.Instance; Method;
3346          Method = Method->getNext())
3347       if (ShouldWriteMethodListNode(Method))
3348         ++NumInstanceMethods;
3349 
3350     unsigned NumFactoryMethods = 0;
3351     for (const ObjCMethodList *Method = &Methods.Factory; Method;
3352          Method = Method->getNext())
3353       if (ShouldWriteMethodListNode(Method))
3354         ++NumFactoryMethods;
3355 
3356     unsigned InstanceBits = Methods.Instance.getBits();
3357     assert(InstanceBits < 4);
3358     unsigned InstanceHasMoreThanOneDeclBit =
3359         Methods.Instance.hasMoreThanOneDecl();
3360     unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3361                                 (InstanceHasMoreThanOneDeclBit << 2) |
3362                                 InstanceBits;
3363     unsigned FactoryBits = Methods.Factory.getBits();
3364     assert(FactoryBits < 4);
3365     unsigned FactoryHasMoreThanOneDeclBit =
3366         Methods.Factory.hasMoreThanOneDecl();
3367     unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3368                                (FactoryHasMoreThanOneDeclBit << 2) |
3369                                FactoryBits;
3370     LE.write<uint16_t>(FullInstanceBits);
3371     LE.write<uint16_t>(FullFactoryBits);
3372     for (const ObjCMethodList *Method = &Methods.Instance; Method;
3373          Method = Method->getNext())
3374       if (ShouldWriteMethodListNode(Method))
3375         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3376     for (const ObjCMethodList *Method = &Methods.Factory; Method;
3377          Method = Method->getNext())
3378       if (ShouldWriteMethodListNode(Method))
3379         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3380 
3381     assert(Out.tell() - Start == DataLen && "Data length is wrong");
3382   }
3383 
3384 private:
3385   static bool ShouldWriteMethodListNode(const ObjCMethodList *Node) {
3386     return (Node->getMethod() && !Node->getMethod()->isFromASTFile());
3387   }
3388 };
3389 
3390 } // namespace
3391 
3392 /// Write ObjC data: selectors and the method pool.
3393 ///
3394 /// The method pool contains both instance and factory methods, stored
3395 /// in an on-disk hash table indexed by the selector. The hash table also
3396 /// contains an empty entry for every other selector known to Sema.
3397 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3398   using namespace llvm;
3399 
3400   // Do we have to do anything at all?
3401   if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
3402     return;
3403   unsigned NumTableEntries = 0;
3404   // Create and write out the blob that contains selectors and the method pool.
3405   {
3406     llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3407     ASTMethodPoolTrait Trait(*this);
3408 
3409     // Create the on-disk hash table representation. We walk through every
3410     // selector we've seen and look it up in the method pool.
3411     SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3412     for (auto &SelectorAndID : SelectorIDs) {
3413       Selector S = SelectorAndID.first;
3414       SelectorID ID = SelectorAndID.second;
3415       Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
3416       ASTMethodPoolTrait::data_type Data = {
3417         ID,
3418         ObjCMethodList(),
3419         ObjCMethodList()
3420       };
3421       if (F != SemaRef.MethodPool.end()) {
3422         Data.Instance = F->second.first;
3423         Data.Factory = F->second.second;
3424       }
3425       // Only write this selector if it's not in an existing AST or something
3426       // changed.
3427       if (Chain && ID < FirstSelectorID) {
3428         // Selector already exists. Did it change?
3429         bool changed = false;
3430         for (ObjCMethodList *M = &Data.Instance; M && M->getMethod();
3431              M = M->getNext()) {
3432           if (!M->getMethod()->isFromASTFile()) {
3433             changed = true;
3434             Data.Instance = *M;
3435             break;
3436           }
3437         }
3438         for (ObjCMethodList *M = &Data.Factory; M && M->getMethod();
3439              M = M->getNext()) {
3440           if (!M->getMethod()->isFromASTFile()) {
3441             changed = true;
3442             Data.Factory = *M;
3443             break;
3444           }
3445         }
3446         if (!changed)
3447           continue;
3448       } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3449         // A new method pool entry.
3450         ++NumTableEntries;
3451       }
3452       Generator.insert(S, Data, Trait);
3453     }
3454 
3455     // Create the on-disk hash table in a buffer.
3456     SmallString<4096> MethodPool;
3457     uint32_t BucketOffset;
3458     {
3459       using namespace llvm::support;
3460 
3461       ASTMethodPoolTrait Trait(*this);
3462       llvm::raw_svector_ostream Out(MethodPool);
3463       // Make sure that no bucket is at offset 0
3464       endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3465       BucketOffset = Generator.Emit(Out, Trait);
3466     }
3467 
3468     // Create a blob abbreviation
3469     auto Abbrev = std::make_shared<BitCodeAbbrev>();
3470     Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3471     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3472     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3473     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3474     unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3475 
3476     // Write the method pool
3477     {
3478       RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3479                                          NumTableEntries};
3480       Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3481     }
3482 
3483     // Create a blob abbreviation for the selector table offsets.
3484     Abbrev = std::make_shared<BitCodeAbbrev>();
3485     Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3486     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3487     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3488     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3489     unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3490 
3491     // Write the selector offsets table.
3492     {
3493       RecordData::value_type Record[] = {
3494           SELECTOR_OFFSETS, SelectorOffsets.size(),
3495           FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3496       Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3497                                 bytes(SelectorOffsets));
3498     }
3499   }
3500 }
3501 
3502 /// Write the selectors referenced in @selector expression into AST file.
3503 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3504   using namespace llvm;
3505 
3506   if (SemaRef.ReferencedSelectors.empty())
3507     return;
3508 
3509   RecordData Record;
3510   ASTRecordWriter Writer(*this, Record);
3511 
3512   // Note: this writes out all references even for a dependent AST. But it is
3513   // very tricky to fix, and given that @selector shouldn't really appear in
3514   // headers, probably not worth it. It's not a correctness issue.
3515   for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3516     Selector Sel = SelectorAndLocation.first;
3517     SourceLocation Loc = SelectorAndLocation.second;
3518     Writer.AddSelectorRef(Sel);
3519     Writer.AddSourceLocation(Loc);
3520   }
3521   Writer.Emit(REFERENCED_SELECTOR_POOL);
3522 }
3523 
3524 //===----------------------------------------------------------------------===//
3525 // Identifier Table Serialization
3526 //===----------------------------------------------------------------------===//
3527 
3528 /// Determine the declaration that should be put into the name lookup table to
3529 /// represent the given declaration in this module. This is usually D itself,
3530 /// but if D was imported and merged into a local declaration, we want the most
3531 /// recent local declaration instead. The chosen declaration will be the most
3532 /// recent declaration in any module that imports this one.
3533 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3534                                         NamedDecl *D) {
3535   if (!LangOpts.Modules || !D->isFromASTFile())
3536     return D;
3537 
3538   if (Decl *Redecl = D->getPreviousDecl()) {
3539     // For Redeclarable decls, a prior declaration might be local.
3540     for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3541       // If we find a local decl, we're done.
3542       if (!Redecl->isFromASTFile()) {
3543         // Exception: in very rare cases (for injected-class-names), not all
3544         // redeclarations are in the same semantic context. Skip ones in a
3545         // different context. They don't go in this lookup table at all.
3546         if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3547                 D->getDeclContext()->getRedeclContext()))
3548           continue;
3549         return cast<NamedDecl>(Redecl);
3550       }
3551 
3552       // If we find a decl from a (chained-)PCH stop since we won't find a
3553       // local one.
3554       if (Redecl->getOwningModuleID() == 0)
3555         break;
3556     }
3557   } else if (Decl *First = D->getCanonicalDecl()) {
3558     // For Mergeable decls, the first decl might be local.
3559     if (!First->isFromASTFile())
3560       return cast<NamedDecl>(First);
3561   }
3562 
3563   // All declarations are imported. Our most recent declaration will also be
3564   // the most recent one in anyone who imports us.
3565   return D;
3566 }
3567 
3568 namespace {
3569 
3570 class ASTIdentifierTableTrait {
3571   ASTWriter &Writer;
3572   Preprocessor &PP;
3573   IdentifierResolver &IdResolver;
3574   bool IsModule;
3575   bool NeedDecls;
3576   ASTWriter::RecordData *InterestingIdentifierOffsets;
3577 
3578   /// Determines whether this is an "interesting" identifier that needs a
3579   /// full IdentifierInfo structure written into the hash table. Notably, this
3580   /// doesn't check whether the name has macros defined; use PublicMacroIterator
3581   /// to check that.
3582   bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3583     if (MacroOffset || II->isPoisoned() ||
3584         (!IsModule && II->getObjCOrBuiltinID()) ||
3585         II->hasRevertedTokenIDToIdentifier() ||
3586         (NeedDecls && II->getFETokenInfo()))
3587       return true;
3588 
3589     return false;
3590   }
3591 
3592 public:
3593   using key_type = IdentifierInfo *;
3594   using key_type_ref = key_type;
3595 
3596   using data_type = IdentID;
3597   using data_type_ref = data_type;
3598 
3599   using hash_value_type = unsigned;
3600   using offset_type = unsigned;
3601 
3602   ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3603                           IdentifierResolver &IdResolver, bool IsModule,
3604                           ASTWriter::RecordData *InterestingIdentifierOffsets)
3605       : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3606         NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3607         InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3608 
3609   bool needDecls() const { return NeedDecls; }
3610 
3611   static hash_value_type ComputeHash(const IdentifierInfo* II) {
3612     return llvm::djbHash(II->getName());
3613   }
3614 
3615   bool isInterestingIdentifier(const IdentifierInfo *II) {
3616     auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3617     return isInterestingIdentifier(II, MacroOffset);
3618   }
3619 
3620   bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3621     return isInterestingIdentifier(II, 0);
3622   }
3623 
3624   std::pair<unsigned, unsigned>
3625   EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3626     // Record the location of the identifier data. This is used when generating
3627     // the mapping from persistent IDs to strings.
3628     Writer.SetIdentifierOffset(II, Out.tell());
3629 
3630     auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3631 
3632     // Emit the offset of the key/data length information to the interesting
3633     // identifiers table if necessary.
3634     if (InterestingIdentifierOffsets &&
3635         isInterestingIdentifier(II, MacroOffset))
3636       InterestingIdentifierOffsets->push_back(Out.tell());
3637 
3638     unsigned KeyLen = II->getLength() + 1;
3639     unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3640     if (isInterestingIdentifier(II, MacroOffset)) {
3641       DataLen += 2; // 2 bytes for builtin ID
3642       DataLen += 2; // 2 bytes for flags
3643       if (MacroOffset)
3644         DataLen += 4; // MacroDirectives offset.
3645 
3646       if (NeedDecls)
3647         DataLen += std::distance(IdResolver.begin(II), IdResolver.end()) * 4;
3648     }
3649     return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3650   }
3651 
3652   void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3653                unsigned KeyLen) {
3654     Out.write(II->getNameStart(), KeyLen);
3655   }
3656 
3657   void EmitData(raw_ostream& Out, IdentifierInfo* II,
3658                 IdentID ID, unsigned) {
3659     using namespace llvm::support;
3660 
3661     endian::Writer LE(Out, llvm::endianness::little);
3662 
3663     auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3664     if (!isInterestingIdentifier(II, MacroOffset)) {
3665       LE.write<uint32_t>(ID << 1);
3666       return;
3667     }
3668 
3669     LE.write<uint32_t>((ID << 1) | 0x01);
3670     uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3671     assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3672     LE.write<uint16_t>(Bits);
3673     Bits = 0;
3674     bool HadMacroDefinition = MacroOffset != 0;
3675     Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3676     Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3677     Bits = (Bits << 1) | unsigned(II->isPoisoned());
3678     Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3679     Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3680     LE.write<uint16_t>(Bits);
3681 
3682     if (HadMacroDefinition)
3683       LE.write<uint32_t>(MacroOffset);
3684 
3685     if (NeedDecls) {
3686       // Emit the declaration IDs in reverse order, because the
3687       // IdentifierResolver provides the declarations as they would be
3688       // visible (e.g., the function "stat" would come before the struct
3689       // "stat"), but the ASTReader adds declarations to the end of the list
3690       // (so we need to see the struct "stat" before the function "stat").
3691       // Only emit declarations that aren't from a chained PCH, though.
3692       SmallVector<NamedDecl *, 16> Decls(IdResolver.decls(II));
3693       for (NamedDecl *D : llvm::reverse(Decls))
3694         LE.write<uint32_t>(
3695             Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), D)));
3696     }
3697   }
3698 };
3699 
3700 } // namespace
3701 
3702 /// Write the identifier table into the AST file.
3703 ///
3704 /// The identifier table consists of a blob containing string data
3705 /// (the actual identifiers themselves) and a separate "offsets" index
3706 /// that maps identifier IDs to locations within the blob.
3707 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3708                                      IdentifierResolver &IdResolver,
3709                                      bool IsModule) {
3710   using namespace llvm;
3711 
3712   RecordData InterestingIdents;
3713 
3714   // Create and write out the blob that contains the identifier
3715   // strings.
3716   {
3717     llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3718     ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule,
3719                                   IsModule ? &InterestingIdents : nullptr);
3720 
3721     // Look for any identifiers that were named while processing the
3722     // headers, but are otherwise not needed. We add these to the hash
3723     // table to enable checking of the predefines buffer in the case
3724     // where the user adds new macro definitions when building the AST
3725     // file.
3726     SmallVector<const IdentifierInfo *, 128> IIs;
3727     for (const auto &ID : PP.getIdentifierTable())
3728       if (Trait.isInterestingNonMacroIdentifier(ID.second))
3729         IIs.push_back(ID.second);
3730     // Sort the identifiers lexicographically before getting the references so
3731     // that their order is stable.
3732     llvm::sort(IIs, llvm::deref<std::less<>>());
3733     for (const IdentifierInfo *II : IIs)
3734       getIdentifierRef(II);
3735 
3736     // Create the on-disk hash table representation. We only store offsets
3737     // for identifiers that appear here for the first time.
3738     IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3739     for (auto IdentIDPair : IdentifierIDs) {
3740       auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3741       IdentID ID = IdentIDPair.second;
3742       assert(II && "NULL identifier in identifier table");
3743       // Write out identifiers if either the ID is local or the identifier has
3744       // changed since it was loaded.
3745       if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3746           || II->hasChangedSinceDeserialization() ||
3747           (Trait.needDecls() &&
3748            II->hasFETokenInfoChangedSinceDeserialization()))
3749         Generator.insert(II, ID, Trait);
3750     }
3751 
3752     // Create the on-disk hash table in a buffer.
3753     SmallString<4096> IdentifierTable;
3754     uint32_t BucketOffset;
3755     {
3756       using namespace llvm::support;
3757 
3758       llvm::raw_svector_ostream Out(IdentifierTable);
3759       // Make sure that no bucket is at offset 0
3760       endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3761       BucketOffset = Generator.Emit(Out, Trait);
3762     }
3763 
3764     // Create a blob abbreviation
3765     auto Abbrev = std::make_shared<BitCodeAbbrev>();
3766     Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3767     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3768     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3769     unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3770 
3771     // Write the identifier table
3772     RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3773     Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3774   }
3775 
3776   // Write the offsets table for identifier IDs.
3777   auto Abbrev = std::make_shared<BitCodeAbbrev>();
3778   Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3779   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3780   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3781   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3782   unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3783 
3784 #ifndef NDEBUG
3785   for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3786     assert(IdentifierOffsets[I] && "Missing identifier offset?");
3787 #endif
3788 
3789   RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3790                                      IdentifierOffsets.size(),
3791                                      FirstIdentID - NUM_PREDEF_IDENT_IDS};
3792   Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3793                             bytes(IdentifierOffsets));
3794 
3795   // In C++, write the list of interesting identifiers (those that are
3796   // defined as macros, poisoned, or similar unusual things).
3797   if (!InterestingIdents.empty())
3798     Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3799 }
3800 
3801 //===----------------------------------------------------------------------===//
3802 // DeclContext's Name Lookup Table Serialization
3803 //===----------------------------------------------------------------------===//
3804 
3805 namespace {
3806 
3807 // Trait used for the on-disk hash table used in the method pool.
3808 class ASTDeclContextNameLookupTrait {
3809   ASTWriter &Writer;
3810   llvm::SmallVector<DeclID, 64> DeclIDs;
3811 
3812 public:
3813   using key_type = DeclarationNameKey;
3814   using key_type_ref = key_type;
3815 
3816   /// A start and end index into DeclIDs, representing a sequence of decls.
3817   using data_type = std::pair<unsigned, unsigned>;
3818   using data_type_ref = const data_type &;
3819 
3820   using hash_value_type = unsigned;
3821   using offset_type = unsigned;
3822 
3823   explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3824 
3825   template<typename Coll>
3826   data_type getData(const Coll &Decls) {
3827     unsigned Start = DeclIDs.size();
3828     for (NamedDecl *D : Decls) {
3829       DeclIDs.push_back(
3830           Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3831     }
3832     return std::make_pair(Start, DeclIDs.size());
3833   }
3834 
3835   data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3836     unsigned Start = DeclIDs.size();
3837     llvm::append_range(DeclIDs, FromReader);
3838     return std::make_pair(Start, DeclIDs.size());
3839   }
3840 
3841   static bool EqualKey(key_type_ref a, key_type_ref b) {
3842     return a == b;
3843   }
3844 
3845   hash_value_type ComputeHash(DeclarationNameKey Name) {
3846     return Name.getHash();
3847   }
3848 
3849   void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3850     assert(Writer.hasChain() &&
3851            "have reference to loaded module file but no chain?");
3852 
3853     using namespace llvm::support;
3854 
3855     endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F),
3856                             llvm::endianness::little);
3857   }
3858 
3859   std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3860                                                   DeclarationNameKey Name,
3861                                                   data_type_ref Lookup) {
3862     unsigned KeyLen = 1;
3863     switch (Name.getKind()) {
3864     case DeclarationName::Identifier:
3865     case DeclarationName::ObjCZeroArgSelector:
3866     case DeclarationName::ObjCOneArgSelector:
3867     case DeclarationName::ObjCMultiArgSelector:
3868     case DeclarationName::CXXLiteralOperatorName:
3869     case DeclarationName::CXXDeductionGuideName:
3870       KeyLen += 4;
3871       break;
3872     case DeclarationName::CXXOperatorName:
3873       KeyLen += 1;
3874       break;
3875     case DeclarationName::CXXConstructorName:
3876     case DeclarationName::CXXDestructorName:
3877     case DeclarationName::CXXConversionFunctionName:
3878     case DeclarationName::CXXUsingDirective:
3879       break;
3880     }
3881 
3882     // 4 bytes for each DeclID.
3883     unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3884 
3885     return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3886   }
3887 
3888   void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3889     using namespace llvm::support;
3890 
3891     endian::Writer LE(Out, llvm::endianness::little);
3892     LE.write<uint8_t>(Name.getKind());
3893     switch (Name.getKind()) {
3894     case DeclarationName::Identifier:
3895     case DeclarationName::CXXLiteralOperatorName:
3896     case DeclarationName::CXXDeductionGuideName:
3897       LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3898       return;
3899     case DeclarationName::ObjCZeroArgSelector:
3900     case DeclarationName::ObjCOneArgSelector:
3901     case DeclarationName::ObjCMultiArgSelector:
3902       LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3903       return;
3904     case DeclarationName::CXXOperatorName:
3905       assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3906              "Invalid operator?");
3907       LE.write<uint8_t>(Name.getOperatorKind());
3908       return;
3909     case DeclarationName::CXXConstructorName:
3910     case DeclarationName::CXXDestructorName:
3911     case DeclarationName::CXXConversionFunctionName:
3912     case DeclarationName::CXXUsingDirective:
3913       return;
3914     }
3915 
3916     llvm_unreachable("Invalid name kind?");
3917   }
3918 
3919   void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3920                 unsigned DataLen) {
3921     using namespace llvm::support;
3922 
3923     endian::Writer LE(Out, llvm::endianness::little);
3924     uint64_t Start = Out.tell(); (void)Start;
3925     for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3926       LE.write<uint32_t>(DeclIDs[I]);
3927     assert(Out.tell() - Start == DataLen && "Data length is wrong");
3928   }
3929 };
3930 
3931 } // namespace
3932 
3933 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3934                                        DeclContext *DC) {
3935   return Result.hasExternalDecls() &&
3936          DC->hasNeedToReconcileExternalVisibleStorage();
3937 }
3938 
3939 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3940                                                DeclContext *DC) {
3941   for (auto *D : Result.getLookupResult())
3942     if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3943       return false;
3944 
3945   return true;
3946 }
3947 
3948 void
3949 ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3950                                    llvm::SmallVectorImpl<char> &LookupTable) {
3951   assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3952          !ConstDC->hasLazyExternalLexicalLookups() &&
3953          "must call buildLookups first");
3954 
3955   // FIXME: We need to build the lookups table, which is logically const.
3956   auto *DC = const_cast<DeclContext*>(ConstDC);
3957   assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3958 
3959   // Create the on-disk hash table representation.
3960   MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3961                                 ASTDeclContextNameLookupTrait> Generator;
3962   ASTDeclContextNameLookupTrait Trait(*this);
3963 
3964   // The first step is to collect the declaration names which we need to
3965   // serialize into the name lookup table, and to collect them in a stable
3966   // order.
3967   SmallVector<DeclarationName, 16> Names;
3968 
3969   // We also build up small sets of the constructor and conversion function
3970   // names which are visible.
3971   llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
3972 
3973   for (auto &Lookup : *DC->buildLookup()) {
3974     auto &Name = Lookup.first;
3975     auto &Result = Lookup.second;
3976 
3977     // If there are no local declarations in our lookup result, we
3978     // don't need to write an entry for the name at all. If we can't
3979     // write out a lookup set without performing more deserialization,
3980     // just skip this entry.
3981     if (isLookupResultExternal(Result, DC) &&
3982         isLookupResultEntirelyExternal(Result, DC))
3983       continue;
3984 
3985     // We also skip empty results. If any of the results could be external and
3986     // the currently available results are empty, then all of the results are
3987     // external and we skip it above. So the only way we get here with an empty
3988     // results is when no results could have been external *and* we have
3989     // external results.
3990     //
3991     // FIXME: While we might want to start emitting on-disk entries for negative
3992     // lookups into a decl context as an optimization, today we *have* to skip
3993     // them because there are names with empty lookup results in decl contexts
3994     // which we can't emit in any stable ordering: we lookup constructors and
3995     // conversion functions in the enclosing namespace scope creating empty
3996     // results for them. This in almost certainly a bug in Clang's name lookup,
3997     // but that is likely to be hard or impossible to fix and so we tolerate it
3998     // here by omitting lookups with empty results.
3999     if (Lookup.second.getLookupResult().empty())
4000       continue;
4001 
4002     switch (Lookup.first.getNameKind()) {
4003     default:
4004       Names.push_back(Lookup.first);
4005       break;
4006 
4007     case DeclarationName::CXXConstructorName:
4008       assert(isa<CXXRecordDecl>(DC) &&
4009              "Cannot have a constructor name outside of a class!");
4010       ConstructorNameSet.insert(Name);
4011       break;
4012 
4013     case DeclarationName::CXXConversionFunctionName:
4014       assert(isa<CXXRecordDecl>(DC) &&
4015              "Cannot have a conversion function name outside of a class!");
4016       ConversionNameSet.insert(Name);
4017       break;
4018     }
4019   }
4020 
4021   // Sort the names into a stable order.
4022   llvm::sort(Names);
4023 
4024   if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
4025     // We need to establish an ordering of constructor and conversion function
4026     // names, and they don't have an intrinsic ordering.
4027 
4028     // First we try the easy case by forming the current context's constructor
4029     // name and adding that name first. This is a very useful optimization to
4030     // avoid walking the lexical declarations in many cases, and it also
4031     // handles the only case where a constructor name can come from some other
4032     // lexical context -- when that name is an implicit constructor merged from
4033     // another declaration in the redecl chain. Any non-implicit constructor or
4034     // conversion function which doesn't occur in all the lexical contexts
4035     // would be an ODR violation.
4036     auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4037         Context->getCanonicalType(Context->getRecordType(D)));
4038     if (ConstructorNameSet.erase(ImplicitCtorName))
4039       Names.push_back(ImplicitCtorName);
4040 
4041     // If we still have constructors or conversion functions, we walk all the
4042     // names in the decl and add the constructors and conversion functions
4043     // which are visible in the order they lexically occur within the context.
4044     if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4045       for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4046         if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4047           auto Name = ChildND->getDeclName();
4048           switch (Name.getNameKind()) {
4049           default:
4050             continue;
4051 
4052           case DeclarationName::CXXConstructorName:
4053             if (ConstructorNameSet.erase(Name))
4054               Names.push_back(Name);
4055             break;
4056 
4057           case DeclarationName::CXXConversionFunctionName:
4058             if (ConversionNameSet.erase(Name))
4059               Names.push_back(Name);
4060             break;
4061           }
4062 
4063           if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4064             break;
4065         }
4066 
4067     assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4068                                          "constructors by walking all the "
4069                                          "lexical members of the context.");
4070     assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4071                                         "conversion functions by walking all "
4072                                         "the lexical members of the context.");
4073   }
4074 
4075   // Next we need to do a lookup with each name into this decl context to fully
4076   // populate any results from external sources. We don't actually use the
4077   // results of these lookups because we only want to use the results after all
4078   // results have been loaded and the pointers into them will be stable.
4079   for (auto &Name : Names)
4080     DC->lookup(Name);
4081 
4082   // Now we need to insert the results for each name into the hash table. For
4083   // constructor names and conversion function names, we actually need to merge
4084   // all of the results for them into one list of results each and insert
4085   // those.
4086   SmallVector<NamedDecl *, 8> ConstructorDecls;
4087   SmallVector<NamedDecl *, 8> ConversionDecls;
4088 
4089   // Now loop over the names, either inserting them or appending for the two
4090   // special cases.
4091   for (auto &Name : Names) {
4092     DeclContext::lookup_result Result = DC->noload_lookup(Name);
4093 
4094     switch (Name.getNameKind()) {
4095     default:
4096       Generator.insert(Name, Trait.getData(Result), Trait);
4097       break;
4098 
4099     case DeclarationName::CXXConstructorName:
4100       ConstructorDecls.append(Result.begin(), Result.end());
4101       break;
4102 
4103     case DeclarationName::CXXConversionFunctionName:
4104       ConversionDecls.append(Result.begin(), Result.end());
4105       break;
4106     }
4107   }
4108 
4109   // Handle our two special cases if we ended up having any. We arbitrarily use
4110   // the first declaration's name here because the name itself isn't part of
4111   // the key, only the kind of name is used.
4112   if (!ConstructorDecls.empty())
4113     Generator.insert(ConstructorDecls.front()->getDeclName(),
4114                      Trait.getData(ConstructorDecls), Trait);
4115   if (!ConversionDecls.empty())
4116     Generator.insert(ConversionDecls.front()->getDeclName(),
4117                      Trait.getData(ConversionDecls), Trait);
4118 
4119   // Create the on-disk hash table. Also emit the existing imported and
4120   // merged table if there is one.
4121   auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4122   Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
4123 }
4124 
4125 /// Write the block containing all of the declaration IDs
4126 /// visible from the given DeclContext.
4127 ///
4128 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4129 /// bitstream, or 0 if no block was written.
4130 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4131                                                  DeclContext *DC) {
4132   // If we imported a key declaration of this namespace, write the visible
4133   // lookup results as an update record for it rather than including them
4134   // on this declaration. We will only look at key declarations on reload.
4135   if (isa<NamespaceDecl>(DC) && Chain &&
4136       Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4137     // Only do this once, for the first local declaration of the namespace.
4138     for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
4139          Prev = Prev->getPreviousDecl())
4140       if (!Prev->isFromASTFile())
4141         return 0;
4142 
4143     // Note that we need to emit an update record for the primary context.
4144     UpdatedDeclContexts.insert(DC->getPrimaryContext());
4145 
4146     // Make sure all visible decls are written. They will be recorded later. We
4147     // do this using a side data structure so we can sort the names into
4148     // a deterministic order.
4149     StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4150     SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4151         LookupResults;
4152     if (Map) {
4153       LookupResults.reserve(Map->size());
4154       for (auto &Entry : *Map)
4155         LookupResults.push_back(
4156             std::make_pair(Entry.first, Entry.second.getLookupResult()));
4157     }
4158 
4159     llvm::sort(LookupResults, llvm::less_first());
4160     for (auto &NameAndResult : LookupResults) {
4161       DeclarationName Name = NameAndResult.first;
4162       DeclContext::lookup_result Result = NameAndResult.second;
4163       if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4164           Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4165         // We have to work around a name lookup bug here where negative lookup
4166         // results for these names get cached in namespace lookup tables (these
4167         // names should never be looked up in a namespace).
4168         assert(Result.empty() && "Cannot have a constructor or conversion "
4169                                  "function name in a namespace!");
4170         continue;
4171       }
4172 
4173       for (NamedDecl *ND : Result)
4174         if (!ND->isFromASTFile())
4175           GetDeclRef(ND);
4176     }
4177 
4178     return 0;
4179   }
4180 
4181   if (DC->getPrimaryContext() != DC)
4182     return 0;
4183 
4184   // Skip contexts which don't support name lookup.
4185   if (!DC->isLookupContext())
4186     return 0;
4187 
4188   // If not in C++, we perform name lookup for the translation unit via the
4189   // IdentifierInfo chains, don't bother to build a visible-declarations table.
4190   if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4191     return 0;
4192 
4193   // Serialize the contents of the mapping used for lookup. Note that,
4194   // although we have two very different code paths, the serialized
4195   // representation is the same for both cases: a declaration name,
4196   // followed by a size, followed by references to the visible
4197   // declarations that have that name.
4198   uint64_t Offset = Stream.GetCurrentBitNo();
4199   StoredDeclsMap *Map = DC->buildLookup();
4200   if (!Map || Map->empty())
4201     return 0;
4202 
4203   // Create the on-disk hash table in a buffer.
4204   SmallString<4096> LookupTable;
4205   GenerateNameLookupTable(DC, LookupTable);
4206 
4207   // Write the lookup table
4208   RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4209   Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
4210                             LookupTable);
4211   ++NumVisibleDeclContexts;
4212   return Offset;
4213 }
4214 
4215 /// Write an UPDATE_VISIBLE block for the given context.
4216 ///
4217 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4218 /// DeclContext in a dependent AST file. As such, they only exist for the TU
4219 /// (in C++), for namespaces, and for classes with forward-declared unscoped
4220 /// enumeration members (in C++11).
4221 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
4222   StoredDeclsMap *Map = DC->getLookupPtr();
4223   if (!Map || Map->empty())
4224     return;
4225 
4226   // Create the on-disk hash table in a buffer.
4227   SmallString<4096> LookupTable;
4228   GenerateNameLookupTable(DC, LookupTable);
4229 
4230   // If we're updating a namespace, select a key declaration as the key for the
4231   // update record; those are the only ones that will be checked on reload.
4232   if (isa<NamespaceDecl>(DC))
4233     DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4234 
4235   // Write the lookup table
4236   RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
4237   Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
4238 }
4239 
4240 /// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4241 void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4242   RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4243   Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4244 }
4245 
4246 /// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4247 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4248   if (!SemaRef.Context.getLangOpts().OpenCL)
4249     return;
4250 
4251   const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4252   RecordData Record;
4253   for (const auto &I:Opts.OptMap) {
4254     AddString(I.getKey(), Record);
4255     auto V = I.getValue();
4256     Record.push_back(V.Supported ? 1 : 0);
4257     Record.push_back(V.Enabled ? 1 : 0);
4258     Record.push_back(V.WithPragma ? 1 : 0);
4259     Record.push_back(V.Avail);
4260     Record.push_back(V.Core);
4261     Record.push_back(V.Opt);
4262   }
4263   Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4264 }
4265 void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4266   if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4267     RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4268     Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4269   }
4270 }
4271 
4272 void ASTWriter::WriteObjCCategories() {
4273   SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4274   RecordData Categories;
4275 
4276   for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4277     unsigned Size = 0;
4278     unsigned StartIndex = Categories.size();
4279 
4280     ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4281 
4282     // Allocate space for the size.
4283     Categories.push_back(0);
4284 
4285     // Add the categories.
4286     for (ObjCInterfaceDecl::known_categories_iterator
4287            Cat = Class->known_categories_begin(),
4288            CatEnd = Class->known_categories_end();
4289          Cat != CatEnd; ++Cat, ++Size) {
4290       assert(getDeclID(*Cat) != 0 && "Bogus category");
4291       AddDeclRef(*Cat, Categories);
4292     }
4293 
4294     // Update the size.
4295     Categories[StartIndex] = Size;
4296 
4297     // Record this interface -> category map.
4298     ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4299     CategoriesMap.push_back(CatInfo);
4300   }
4301 
4302   // Sort the categories map by the definition ID, since the reader will be
4303   // performing binary searches on this information.
4304   llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4305 
4306   // Emit the categories map.
4307   using namespace llvm;
4308 
4309   auto Abbrev = std::make_shared<BitCodeAbbrev>();
4310   Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4311   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4312   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4313   unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
4314 
4315   RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4316   Stream.EmitRecordWithBlob(AbbrevID, Record,
4317                             reinterpret_cast<char *>(CategoriesMap.data()),
4318                             CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4319 
4320   // Emit the category lists.
4321   Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4322 }
4323 
4324 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4325   Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4326 
4327   if (LPTMap.empty())
4328     return;
4329 
4330   RecordData Record;
4331   for (auto &LPTMapEntry : LPTMap) {
4332     const FunctionDecl *FD = LPTMapEntry.first;
4333     LateParsedTemplate &LPT = *LPTMapEntry.second;
4334     AddDeclRef(FD, Record);
4335     AddDeclRef(LPT.D, Record);
4336     Record.push_back(LPT.FPO.getAsOpaqueInt());
4337     Record.push_back(LPT.Toks.size());
4338 
4339     for (const auto &Tok : LPT.Toks) {
4340       AddToken(Tok, Record);
4341     }
4342   }
4343   Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4344 }
4345 
4346 /// Write the state of 'pragma clang optimize' at the end of the module.
4347 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4348   RecordData Record;
4349   SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4350   AddSourceLocation(PragmaLoc, Record);
4351   Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4352 }
4353 
4354 /// Write the state of 'pragma ms_struct' at the end of the module.
4355 void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4356   RecordData Record;
4357   Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4358   Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4359 }
4360 
4361 /// Write the state of 'pragma pointers_to_members' at the end of the
4362 //module.
4363 void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4364   RecordData Record;
4365   Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4366   AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4367   Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4368 }
4369 
4370 /// Write the state of 'pragma align/pack' at the end of the module.
4371 void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4372   // Don't serialize pragma align/pack state for modules, since it should only
4373   // take effect on a per-submodule basis.
4374   if (WritingModule)
4375     return;
4376 
4377   RecordData Record;
4378   AddAlignPackInfo(SemaRef.AlignPackStack.CurrentValue, Record);
4379   AddSourceLocation(SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
4380   Record.push_back(SemaRef.AlignPackStack.Stack.size());
4381   for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
4382     AddAlignPackInfo(StackEntry.Value, Record);
4383     AddSourceLocation(StackEntry.PragmaLocation, Record);
4384     AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4385     AddString(StackEntry.StackSlotLabel, Record);
4386   }
4387   Stream.EmitRecord(ALIGN_PACK_PRAGMA_OPTIONS, Record);
4388 }
4389 
4390 /// Write the state of 'pragma float_control' at the end of the module.
4391 void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4392   // Don't serialize pragma float_control state for modules,
4393   // since it should only take effect on a per-submodule basis.
4394   if (WritingModule)
4395     return;
4396 
4397   RecordData Record;
4398   Record.push_back(SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4399   AddSourceLocation(SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4400   Record.push_back(SemaRef.FpPragmaStack.Stack.size());
4401   for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4402     Record.push_back(StackEntry.Value.getAsOpaqueInt());
4403     AddSourceLocation(StackEntry.PragmaLocation, Record);
4404     AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4405     AddString(StackEntry.StackSlotLabel, Record);
4406   }
4407   Stream.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS, Record);
4408 }
4409 
4410 void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4411                                          ModuleFileExtensionWriter &Writer) {
4412   // Enter the extension block.
4413   Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4414 
4415   // Emit the metadata record abbreviation.
4416   auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4417   Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4418   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4419   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4420   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4421   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4422   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4423   unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
4424 
4425   // Emit the metadata record.
4426   RecordData Record;
4427   auto Metadata = Writer.getExtension()->getExtensionMetadata();
4428   Record.push_back(EXTENSION_METADATA);
4429   Record.push_back(Metadata.MajorVersion);
4430   Record.push_back(Metadata.MinorVersion);
4431   Record.push_back(Metadata.BlockName.size());
4432   Record.push_back(Metadata.UserInfo.size());
4433   SmallString<64> Buffer;
4434   Buffer += Metadata.BlockName;
4435   Buffer += Metadata.UserInfo;
4436   Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4437 
4438   // Emit the contents of the extension block.
4439   Writer.writeExtensionContents(SemaRef, Stream);
4440 
4441   // Exit the extension block.
4442   Stream.ExitBlock();
4443 }
4444 
4445 //===----------------------------------------------------------------------===//
4446 // General Serialization Routines
4447 //===----------------------------------------------------------------------===//
4448 
4449 void ASTRecordWriter::AddAttr(const Attr *A) {
4450   auto &Record = *this;
4451   // FIXME: Clang can't handle the serialization/deserialization of
4452   // preferred_name properly now. See
4453   // https://github.com/llvm/llvm-project/issues/56490 for example.
4454   if (!A || (isa<PreferredNameAttr>(A) &&
4455              Writer->isWritingStdCXXNamedModules()))
4456     return Record.push_back(0);
4457 
4458   Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4459 
4460   Record.AddIdentifierRef(A->getAttrName());
4461   Record.AddIdentifierRef(A->getScopeName());
4462   Record.AddSourceRange(A->getRange());
4463   Record.AddSourceLocation(A->getScopeLoc());
4464   Record.push_back(A->getParsedKind());
4465   Record.push_back(A->getSyntax());
4466   Record.push_back(A->getAttributeSpellingListIndexRaw());
4467   Record.push_back(A->isRegularKeywordAttribute());
4468 
4469 #include "clang/Serialization/AttrPCHWrite.inc"
4470 }
4471 
4472 /// Emit the list of attributes to the specified record.
4473 void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4474   push_back(Attrs.size());
4475   for (const auto *A : Attrs)
4476     AddAttr(A);
4477 }
4478 
4479 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4480   AddSourceLocation(Tok.getLocation(), Record);
4481   // FIXME: Should translate token kind to a stable encoding.
4482   Record.push_back(Tok.getKind());
4483   // FIXME: Should translate token flags to a stable encoding.
4484   Record.push_back(Tok.getFlags());
4485 
4486   if (Tok.isAnnotation()) {
4487     AddSourceLocation(Tok.getAnnotationEndLoc(), Record);
4488     switch (Tok.getKind()) {
4489     case tok::annot_pragma_loop_hint: {
4490       auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
4491       AddToken(Info->PragmaName, Record);
4492       AddToken(Info->Option, Record);
4493       Record.push_back(Info->Toks.size());
4494       for (const auto &T : Info->Toks)
4495         AddToken(T, Record);
4496       break;
4497     }
4498     case tok::annot_pragma_pack: {
4499       auto *Info =
4500           static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
4501       Record.push_back(static_cast<unsigned>(Info->Action));
4502       AddString(Info->SlotLabel, Record);
4503       AddToken(Info->Alignment, Record);
4504       break;
4505     }
4506     // Some annotation tokens do not use the PtrData field.
4507     case tok::annot_pragma_openmp:
4508     case tok::annot_pragma_openmp_end:
4509     case tok::annot_pragma_unused:
4510     case tok::annot_pragma_openacc:
4511     case tok::annot_pragma_openacc_end:
4512       break;
4513     default:
4514       llvm_unreachable("missing serialization code for annotation token");
4515     }
4516   } else {
4517     Record.push_back(Tok.getLength());
4518     // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4519     // is needed.
4520     AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4521   }
4522 }
4523 
4524 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4525   Record.push_back(Str.size());
4526   Record.insert(Record.end(), Str.begin(), Str.end());
4527 }
4528 
4529 bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4530   assert(Context && "should have context when outputting path");
4531 
4532   // Leave special file names as they are.
4533   StringRef PathStr(Path.data(), Path.size());
4534   if (PathStr == "<built-in>" || PathStr == "<command line>")
4535     return false;
4536 
4537   bool Changed =
4538       cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4539 
4540   // Remove a prefix to make the path relative, if relevant.
4541   const char *PathBegin = Path.data();
4542   const char *PathPtr =
4543       adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4544   if (PathPtr != PathBegin) {
4545     Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4546     Changed = true;
4547   }
4548 
4549   return Changed;
4550 }
4551 
4552 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4553   SmallString<128> FilePath(Path);
4554   PreparePathForOutput(FilePath);
4555   AddString(FilePath, Record);
4556 }
4557 
4558 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4559                                    StringRef Path) {
4560   SmallString<128> FilePath(Path);
4561   PreparePathForOutput(FilePath);
4562   Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4563 }
4564 
4565 void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4566                                 RecordDataImpl &Record) {
4567   Record.push_back(Version.getMajor());
4568   if (std::optional<unsigned> Minor = Version.getMinor())
4569     Record.push_back(*Minor + 1);
4570   else
4571     Record.push_back(0);
4572   if (std::optional<unsigned> Subminor = Version.getSubminor())
4573     Record.push_back(*Subminor + 1);
4574   else
4575     Record.push_back(0);
4576 }
4577 
4578 /// Note that the identifier II occurs at the given offset
4579 /// within the identifier table.
4580 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4581   IdentID ID = IdentifierIDs[II];
4582   // Only store offsets new to this AST file. Other identifier names are looked
4583   // up earlier in the chain and thus don't need an offset.
4584   if (ID >= FirstIdentID)
4585     IdentifierOffsets[ID - FirstIdentID] = Offset;
4586 }
4587 
4588 /// Note that the selector Sel occurs at the given offset
4589 /// within the method pool/selector table.
4590 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4591   unsigned ID = SelectorIDs[Sel];
4592   assert(ID && "Unknown selector");
4593   // Don't record offsets for selectors that are also available in a different
4594   // file.
4595   if (ID < FirstSelectorID)
4596     return;
4597   SelectorOffsets[ID - FirstSelectorID] = Offset;
4598 }
4599 
4600 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4601                      SmallVectorImpl<char> &Buffer,
4602                      InMemoryModuleCache &ModuleCache,
4603                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4604                      bool IncludeTimestamps, bool BuildingImplicitModule)
4605     : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4606       IncludeTimestamps(IncludeTimestamps),
4607       BuildingImplicitModule(BuildingImplicitModule) {
4608   for (const auto &Ext : Extensions) {
4609     if (auto Writer = Ext->createExtensionWriter(*this))
4610       ModuleFileExtensionWriters.push_back(std::move(Writer));
4611   }
4612 }
4613 
4614 ASTWriter::~ASTWriter() = default;
4615 
4616 const LangOptions &ASTWriter::getLangOpts() const {
4617   assert(WritingAST && "can't determine lang opts when not writing AST");
4618   return Context->getLangOpts();
4619 }
4620 
4621 time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4622   return IncludeTimestamps ? E->getModificationTime() : 0;
4623 }
4624 
4625 ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
4626                                      Module *WritingModule, StringRef isysroot,
4627                                      bool ShouldCacheASTInMemory) {
4628   llvm::TimeTraceScope scope("WriteAST", OutputFile);
4629   WritingAST = true;
4630 
4631   ASTHasCompilerErrors =
4632       SemaRef.PP.getDiagnostics().hasUncompilableErrorOccurred();
4633 
4634   // Emit the file header.
4635   Stream.Emit((unsigned)'C', 8);
4636   Stream.Emit((unsigned)'P', 8);
4637   Stream.Emit((unsigned)'C', 8);
4638   Stream.Emit((unsigned)'H', 8);
4639 
4640   WriteBlockInfoBlock();
4641 
4642   Context = &SemaRef.Context;
4643   PP = &SemaRef.PP;
4644   this->WritingModule = WritingModule;
4645   ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
4646   Context = nullptr;
4647   PP = nullptr;
4648   this->WritingModule = nullptr;
4649   this->BaseDirectory.clear();
4650 
4651   WritingAST = false;
4652   if (ShouldCacheASTInMemory) {
4653     // Construct MemoryBuffer and update buffer manager.
4654     ModuleCache.addBuiltPCM(OutputFile,
4655                             llvm::MemoryBuffer::getMemBufferCopy(
4656                                 StringRef(Buffer.begin(), Buffer.size())));
4657   }
4658   return Signature;
4659 }
4660 
4661 template<typename Vector>
4662 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4663                                ASTWriter::RecordData &Record) {
4664   for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4665        I != E; ++I) {
4666     Writer.AddDeclRef(*I, Record);
4667   }
4668 }
4669 
4670 void ASTWriter::collectNonAffectingInputFiles() {
4671   SourceManager &SrcMgr = PP->getSourceManager();
4672   unsigned N = SrcMgr.local_sloc_entry_size();
4673 
4674   IsSLocAffecting.resize(N, true);
4675 
4676   if (!WritingModule)
4677     return;
4678 
4679   auto AffectingModuleMaps = GetAffectingModuleMaps(*PP, WritingModule);
4680 
4681   unsigned FileIDAdjustment = 0;
4682   unsigned OffsetAdjustment = 0;
4683 
4684   NonAffectingFileIDAdjustments.reserve(N);
4685   NonAffectingOffsetAdjustments.reserve(N);
4686 
4687   NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4688   NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4689 
4690   for (unsigned I = 1; I != N; ++I) {
4691     const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(I);
4692     FileID FID = FileID::get(I);
4693     assert(&SrcMgr.getSLocEntry(FID) == SLoc);
4694 
4695     if (!SLoc->isFile())
4696       continue;
4697     const SrcMgr::FileInfo &File = SLoc->getFile();
4698     const SrcMgr::ContentCache *Cache = &File.getContentCache();
4699     if (!Cache->OrigEntry)
4700       continue;
4701 
4702     if (!isModuleMap(File.getFileCharacteristic()) ||
4703         AffectingModuleMaps.empty() ||
4704         llvm::is_contained(AffectingModuleMaps, *Cache->OrigEntry))
4705       continue;
4706 
4707     IsSLocAffecting[I] = false;
4708 
4709     FileIDAdjustment += 1;
4710     // Even empty files take up one element in the offset table.
4711     OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
4712 
4713     // If the previous file was non-affecting as well, just extend its entry
4714     // with our information.
4715     if (!NonAffectingFileIDs.empty() &&
4716         NonAffectingFileIDs.back().ID == FID.ID - 1) {
4717       NonAffectingFileIDs.back() = FID;
4718       NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
4719       NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
4720       NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
4721       continue;
4722     }
4723 
4724     NonAffectingFileIDs.push_back(FID);
4725     NonAffectingRanges.emplace_back(SrcMgr.getLocForStartOfFile(FID),
4726                                     SrcMgr.getLocForEndOfFile(FID));
4727     NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4728     NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4729   }
4730 }
4731 
4732 ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4733                                          Module *WritingModule) {
4734   using namespace llvm;
4735 
4736   bool isModule = WritingModule != nullptr;
4737 
4738   // Make sure that the AST reader knows to finalize itself.
4739   if (Chain)
4740     Chain->finalizeForWriting();
4741 
4742   ASTContext &Context = SemaRef.Context;
4743   Preprocessor &PP = SemaRef.PP;
4744 
4745   // This needs to be done very early, since everything that writes
4746   // SourceLocations or FileIDs depends on it.
4747   collectNonAffectingInputFiles();
4748 
4749   writeUnhashedControlBlock(PP, Context);
4750 
4751   // Set up predefined declaration IDs.
4752   auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4753     if (D) {
4754       assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4755       DeclIDs[D] = ID;
4756     }
4757   };
4758   RegisterPredefDecl(Context.getTranslationUnitDecl(),
4759                      PREDEF_DECL_TRANSLATION_UNIT_ID);
4760   RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4761   RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4762   RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4763   RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4764                      PREDEF_DECL_OBJC_PROTOCOL_ID);
4765   RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4766   RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4767   RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4768                      PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4769   RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
4770   RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
4771   RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4772                      PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
4773   RegisterPredefDecl(Context.MSGuidTagDecl,
4774                      PREDEF_DECL_BUILTIN_MS_GUID_ID);
4775   RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
4776   RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4777                      PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
4778   RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4779                      PREDEF_DECL_CF_CONSTANT_STRING_ID);
4780   RegisterPredefDecl(Context.CFConstantStringTagDecl,
4781                      PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
4782   RegisterPredefDecl(Context.TypePackElementDecl,
4783                      PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
4784 
4785   // Build a record containing all of the tentative definitions in this file, in
4786   // TentativeDefinitions order.  Generally, this record will be empty for
4787   // headers.
4788   RecordData TentativeDefinitions;
4789   AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4790 
4791   // Build a record containing all of the file scoped decls in this file.
4792   RecordData UnusedFileScopedDecls;
4793   if (!isModule)
4794     AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4795                        UnusedFileScopedDecls);
4796 
4797   // Build a record containing all of the delegating constructors we still need
4798   // to resolve.
4799   RecordData DelegatingCtorDecls;
4800   if (!isModule)
4801     AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4802 
4803   // Write the set of weak, undeclared identifiers. We always write the
4804   // entire table, since later PCH files in a PCH chain are only interested in
4805   // the results at the end of the chain.
4806   RecordData WeakUndeclaredIdentifiers;
4807   for (const auto &WeakUndeclaredIdentifierList :
4808        SemaRef.WeakUndeclaredIdentifiers) {
4809     const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
4810     for (const auto &WI : WeakUndeclaredIdentifierList.second) {
4811       AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4812       AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4813       AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4814     }
4815   }
4816 
4817   // Build a record containing all of the ext_vector declarations.
4818   RecordData ExtVectorDecls;
4819   AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4820 
4821   // Build a record containing all of the VTable uses information.
4822   RecordData VTableUses;
4823   if (!SemaRef.VTableUses.empty()) {
4824     for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4825       AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4826       AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4827       VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4828     }
4829   }
4830 
4831   // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4832   RecordData UnusedLocalTypedefNameCandidates;
4833   for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4834     AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4835 
4836   // Build a record containing all of pending implicit instantiations.
4837   RecordData PendingInstantiations;
4838   for (const auto &I : SemaRef.PendingInstantiations) {
4839     AddDeclRef(I.first, PendingInstantiations);
4840     AddSourceLocation(I.second, PendingInstantiations);
4841   }
4842   assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4843          "There are local ones at end of translation unit!");
4844 
4845   // Build a record containing some declaration references.
4846   RecordData SemaDeclRefs;
4847   if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
4848     AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4849     AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4850     AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
4851   }
4852 
4853   RecordData CUDASpecialDeclRefs;
4854   if (Context.getcudaConfigureCallDecl()) {
4855     AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4856   }
4857 
4858   // Build a record containing all of the known namespaces.
4859   RecordData KnownNamespaces;
4860   for (const auto &I : SemaRef.KnownNamespaces) {
4861     if (!I.second)
4862       AddDeclRef(I.first, KnownNamespaces);
4863   }
4864 
4865   // Build a record of all used, undefined objects that require definitions.
4866   RecordData UndefinedButUsed;
4867 
4868   SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4869   SemaRef.getUndefinedButUsed(Undefined);
4870   for (const auto &I : Undefined) {
4871     AddDeclRef(I.first, UndefinedButUsed);
4872     AddSourceLocation(I.second, UndefinedButUsed);
4873   }
4874 
4875   // Build a record containing all delete-expressions that we would like to
4876   // analyze later in AST.
4877   RecordData DeleteExprsToAnalyze;
4878 
4879   if (!isModule) {
4880     for (const auto &DeleteExprsInfo :
4881          SemaRef.getMismatchingDeleteExpressions()) {
4882       AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4883       DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4884       for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4885         AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4886         DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4887       }
4888     }
4889   }
4890 
4891   // Write the control block
4892   WriteControlBlock(PP, Context, isysroot);
4893 
4894   // Write the remaining AST contents.
4895   Stream.FlushToWord();
4896   ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
4897   Stream.EnterSubblock(AST_BLOCK_ID, 5);
4898   ASTBlockStartOffset = Stream.GetCurrentBitNo();
4899 
4900   // This is so that older clang versions, before the introduction
4901   // of the control block, can read and reject the newer PCH format.
4902   {
4903     RecordData Record = {VERSION_MAJOR};
4904     Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4905   }
4906 
4907   // Create a lexical update block containing all of the declarations in the
4908   // translation unit that do not come from other AST files.
4909   const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4910   SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4911   for (const auto *D : TU->noload_decls()) {
4912     if (!D->isFromASTFile()) {
4913       NewGlobalKindDeclPairs.push_back(D->getKind());
4914       NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4915     }
4916   }
4917 
4918   auto Abv = std::make_shared<BitCodeAbbrev>();
4919   Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4920   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4921   unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
4922   {
4923     RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4924     Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4925                               bytes(NewGlobalKindDeclPairs));
4926   }
4927 
4928   // And a visible updates block for the translation unit.
4929   Abv = std::make_shared<BitCodeAbbrev>();
4930   Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4931   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4932   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4933   UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
4934   WriteDeclContextVisibleUpdate(TU);
4935 
4936   // If we have any extern "C" names, write out a visible update for them.
4937   if (Context.ExternCContext)
4938     WriteDeclContextVisibleUpdate(Context.ExternCContext);
4939 
4940   // If the translation unit has an anonymous namespace, and we don't already
4941   // have an update block for it, write it as an update block.
4942   // FIXME: Why do we not do this if there's already an update block?
4943   if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4944     ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4945     if (Record.empty())
4946       Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4947   }
4948 
4949   // Add update records for all mangling numbers and static local numbers.
4950   // These aren't really update records, but this is a convenient way of
4951   // tagging this rare extra data onto the declarations.
4952   for (const auto &Number : Context.MangleNumbers)
4953     if (!Number.first->isFromASTFile())
4954       DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4955                                                      Number.second));
4956   for (const auto &Number : Context.StaticLocalNumbers)
4957     if (!Number.first->isFromASTFile())
4958       DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4959                                                      Number.second));
4960 
4961   // Make sure visible decls, added to DeclContexts previously loaded from
4962   // an AST file, are registered for serialization. Likewise for template
4963   // specializations added to imported templates.
4964   for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
4965     GetDeclRef(I);
4966   }
4967 
4968   // Make sure all decls associated with an identifier are registered for
4969   // serialization, if we're storing decls with identifiers.
4970   if (!WritingModule || !getLangOpts().CPlusPlus) {
4971     llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4972     for (const auto &ID : PP.getIdentifierTable()) {
4973       const IdentifierInfo *II = ID.second;
4974       if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4975         IIs.push_back(II);
4976     }
4977     // Sort the identifiers to visit based on their name.
4978     llvm::sort(IIs, llvm::deref<std::less<>>());
4979     for (const IdentifierInfo *II : IIs)
4980       for (const Decl *D : SemaRef.IdResolver.decls(II))
4981         GetDeclRef(D);
4982   }
4983 
4984   // For method pool in the module, if it contains an entry for a selector,
4985   // the entry should be complete, containing everything introduced by that
4986   // module and all modules it imports. It's possible that the entry is out of
4987   // date, so we need to pull in the new content here.
4988 
4989   // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4990   // safe, we copy all selectors out.
4991   llvm::SmallVector<Selector, 256> AllSelectors;
4992   for (auto &SelectorAndID : SelectorIDs)
4993     AllSelectors.push_back(SelectorAndID.first);
4994   for (auto &Selector : AllSelectors)
4995     SemaRef.updateOutOfDateSelector(Selector);
4996 
4997   // Form the record of special types.
4998   RecordData SpecialTypes;
4999   AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
5000   AddTypeRef(Context.getFILEType(), SpecialTypes);
5001   AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
5002   AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
5003   AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
5004   AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
5005   AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
5006   AddTypeRef(Context.getucontext_tType(), SpecialTypes);
5007 
5008   if (Chain) {
5009     // Write the mapping information describing our module dependencies and how
5010     // each of those modules were mapped into our own offset/ID space, so that
5011     // the reader can build the appropriate mapping to its own offset/ID space.
5012     // The map consists solely of a blob with the following format:
5013     // *(module-kind:i8
5014     //   module-name-len:i16 module-name:len*i8
5015     //   source-location-offset:i32
5016     //   identifier-id:i32
5017     //   preprocessed-entity-id:i32
5018     //   macro-definition-id:i32
5019     //   submodule-id:i32
5020     //   selector-id:i32
5021     //   declaration-id:i32
5022     //   c++-base-specifiers-id:i32
5023     //   type-id:i32)
5024     //
5025     // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
5026     // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
5027     // module name. Otherwise, it is the module file name.
5028     auto Abbrev = std::make_shared<BitCodeAbbrev>();
5029     Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
5030     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5031     unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
5032     SmallString<2048> Buffer;
5033     {
5034       llvm::raw_svector_ostream Out(Buffer);
5035       for (ModuleFile &M : Chain->ModuleMgr) {
5036         using namespace llvm::support;
5037 
5038         endian::Writer LE(Out, llvm::endianness::little);
5039         LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
5040         StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
5041         LE.write<uint16_t>(Name.size());
5042         Out.write(Name.data(), Name.size());
5043 
5044         // Note: if a base ID was uint max, it would not be possible to load
5045         // another module after it or have more than one entity inside it.
5046         uint32_t None = std::numeric_limits<uint32_t>::max();
5047 
5048         auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
5049           assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5050           if (ShouldWrite)
5051             LE.write<uint32_t>(BaseID);
5052           else
5053             LE.write<uint32_t>(None);
5054         };
5055 
5056         // These values should be unique within a chain, since they will be read
5057         // as keys into ContinuousRangeMaps.
5058         writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
5059         writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
5060         writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5061         writeBaseIDOrNone(M.BasePreprocessedEntityID,
5062                           M.NumPreprocessedEntities);
5063         writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5064         writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5065         writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
5066         writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
5067       }
5068     }
5069     RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
5070     Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
5071                               Buffer.data(), Buffer.size());
5072   }
5073 
5074   // Build a record containing all of the DeclsToCheckForDeferredDiags.
5075   SmallVector<serialization::DeclID, 64> DeclsToCheckForDeferredDiags;
5076   for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5077     DeclsToCheckForDeferredDiags.push_back(GetDeclRef(D));
5078 
5079   RecordData DeclUpdatesOffsetsRecord;
5080 
5081   // Keep writing types, declarations, and declaration update records
5082   // until we've emitted all of them.
5083   Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5084   DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5085   WriteTypeAbbrevs();
5086   WriteDeclAbbrevs();
5087   do {
5088     WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5089     while (!DeclTypesToEmit.empty()) {
5090       DeclOrType DOT = DeclTypesToEmit.front();
5091       DeclTypesToEmit.pop();
5092       if (DOT.isType())
5093         WriteType(DOT.getType());
5094       else
5095         WriteDecl(Context, DOT.getDecl());
5096     }
5097   } while (!DeclUpdates.empty());
5098   Stream.ExitBlock();
5099 
5100   DoneWritingDeclsAndTypes = true;
5101 
5102   // These things can only be done once we've written out decls and types.
5103   WriteTypeDeclOffsets();
5104   if (!DeclUpdatesOffsetsRecord.empty())
5105     Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
5106   WriteFileDeclIDsMap();
5107   WriteSourceManagerBlock(Context.getSourceManager(), PP);
5108   WriteComments();
5109   WritePreprocessor(PP, isModule);
5110   WriteHeaderSearch(PP.getHeaderSearchInfo());
5111   WriteSelectors(SemaRef);
5112   WriteReferencedSelectorsPool(SemaRef);
5113   WriteLateParsedTemplates(SemaRef);
5114   WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
5115   WriteFPPragmaOptions(SemaRef.CurFPFeatureOverrides());
5116   WriteOpenCLExtensions(SemaRef);
5117   WriteCUDAPragmas(SemaRef);
5118 
5119   // If we're emitting a module, write out the submodule information.
5120   if (WritingModule)
5121     WriteSubmodules(WritingModule);
5122 
5123   Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5124 
5125   // Write the record containing external, unnamed definitions.
5126   if (!EagerlyDeserializedDecls.empty())
5127     Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
5128 
5129   if (!ModularCodegenDecls.empty())
5130     Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5131 
5132   // Write the record containing tentative definitions.
5133   if (!TentativeDefinitions.empty())
5134     Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
5135 
5136   // Write the record containing unused file scoped decls.
5137   if (!UnusedFileScopedDecls.empty())
5138     Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
5139 
5140   // Write the record containing weak undeclared identifiers.
5141   if (!WeakUndeclaredIdentifiers.empty())
5142     Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
5143                       WeakUndeclaredIdentifiers);
5144 
5145   // Write the record containing ext_vector type names.
5146   if (!ExtVectorDecls.empty())
5147     Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
5148 
5149   // Write the record containing VTable uses information.
5150   if (!VTableUses.empty())
5151     Stream.EmitRecord(VTABLE_USES, VTableUses);
5152 
5153   // Write the record containing potentially unused local typedefs.
5154   if (!UnusedLocalTypedefNameCandidates.empty())
5155     Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5156                       UnusedLocalTypedefNameCandidates);
5157 
5158   // Write the record containing pending implicit instantiations.
5159   if (!PendingInstantiations.empty())
5160     Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
5161 
5162   // Write the record containing declaration references of Sema.
5163   if (!SemaDeclRefs.empty())
5164     Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
5165 
5166   // Write the record containing decls to be checked for deferred diags.
5167   if (!DeclsToCheckForDeferredDiags.empty())
5168     Stream.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5169         DeclsToCheckForDeferredDiags);
5170 
5171   // Write the record containing CUDA-specific declaration references.
5172   if (!CUDASpecialDeclRefs.empty())
5173     Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
5174 
5175   // Write the delegating constructors.
5176   if (!DelegatingCtorDecls.empty())
5177     Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
5178 
5179   // Write the known namespaces.
5180   if (!KnownNamespaces.empty())
5181     Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
5182 
5183   // Write the undefined internal functions and variables, and inline functions.
5184   if (!UndefinedButUsed.empty())
5185     Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
5186 
5187   if (!DeleteExprsToAnalyze.empty())
5188     Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5189 
5190   // Write the visible updates to DeclContexts.
5191   for (auto *DC : UpdatedDeclContexts)
5192     WriteDeclContextVisibleUpdate(DC);
5193 
5194   if (!WritingModule) {
5195     // Write the submodules that were imported, if any.
5196     struct ModuleInfo {
5197       uint64_t ID;
5198       Module *M;
5199       ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5200     };
5201     llvm::SmallVector<ModuleInfo, 64> Imports;
5202     for (const auto *I : Context.local_imports()) {
5203       assert(SubmoduleIDs.contains(I->getImportedModule()));
5204       Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5205                          I->getImportedModule()));
5206     }
5207 
5208     if (!Imports.empty()) {
5209       auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5210         return A.ID < B.ID;
5211       };
5212       auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5213         return A.ID == B.ID;
5214       };
5215 
5216       // Sort and deduplicate module IDs.
5217       llvm::sort(Imports, Cmp);
5218       Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
5219                     Imports.end());
5220 
5221       RecordData ImportedModules;
5222       for (const auto &Import : Imports) {
5223         ImportedModules.push_back(Import.ID);
5224         // FIXME: If the module has macros imported then later has declarations
5225         // imported, this location won't be the right one as a location for the
5226         // declaration imports.
5227         AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
5228       }
5229 
5230       Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5231     }
5232   }
5233 
5234   WriteObjCCategories();
5235   if(!WritingModule) {
5236     WriteOptimizePragmaOptions(SemaRef);
5237     WriteMSStructPragmaOptions(SemaRef);
5238     WriteMSPointersToMembersPragmaOptions(SemaRef);
5239   }
5240   WritePackPragmaOptions(SemaRef);
5241   WriteFloatControlPragmaOptions(SemaRef);
5242 
5243   // Some simple statistics
5244   RecordData::value_type Record[] = {
5245       NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
5246   Stream.EmitRecord(STATISTICS, Record);
5247   Stream.ExitBlock();
5248   Stream.FlushToWord();
5249   ASTBlockRange.second = Stream.GetCurrentBitNo() >> 3;
5250 
5251   // Write the module file extension blocks.
5252   for (const auto &ExtWriter : ModuleFileExtensionWriters)
5253     WriteModuleFileExtension(SemaRef, *ExtWriter);
5254 
5255   return backpatchSignature();
5256 }
5257 
5258 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
5259   if (DeclUpdates.empty())
5260     return;
5261 
5262   DeclUpdateMap LocalUpdates;
5263   LocalUpdates.swap(DeclUpdates);
5264 
5265   for (auto &DeclUpdate : LocalUpdates) {
5266     const Decl *D = DeclUpdate.first;
5267 
5268     bool HasUpdatedBody = false;
5269     bool HasAddedVarDefinition = false;
5270     RecordData RecordData;
5271     ASTRecordWriter Record(*this, RecordData);
5272     for (auto &Update : DeclUpdate.second) {
5273       DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5274 
5275       // An updated body is emitted last, so that the reader doesn't need
5276       // to skip over the lazy body to reach statements for other records.
5277       if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5278         HasUpdatedBody = true;
5279       else if (Kind == UPD_CXX_ADDED_VAR_DEFINITION)
5280         HasAddedVarDefinition = true;
5281       else
5282         Record.push_back(Kind);
5283 
5284       switch (Kind) {
5285       case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5286       case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5287       case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
5288         assert(Update.getDecl() && "no decl to add?");
5289         Record.push_back(GetDeclRef(Update.getDecl()));
5290         break;
5291 
5292       case UPD_CXX_ADDED_FUNCTION_DEFINITION:
5293       case UPD_CXX_ADDED_VAR_DEFINITION:
5294         break;
5295 
5296       case UPD_CXX_POINT_OF_INSTANTIATION:
5297         // FIXME: Do we need to also save the template specialization kind here?
5298         Record.AddSourceLocation(Update.getLoc());
5299         break;
5300 
5301       case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
5302         Record.AddStmt(const_cast<Expr *>(
5303             cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
5304         break;
5305 
5306       case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5307         Record.AddStmt(
5308             cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5309         break;
5310 
5311       case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5312         auto *RD = cast<CXXRecordDecl>(D);
5313         UpdatedDeclContexts.insert(RD->getPrimaryContext());
5314         Record.push_back(RD->isParamDestroyedInCallee());
5315         Record.push_back(llvm::to_underlying(RD->getArgPassingRestrictions()));
5316         Record.AddCXXDefinitionData(RD);
5317         Record.AddOffset(WriteDeclContextLexicalBlock(
5318             *Context, const_cast<CXXRecordDecl *>(RD)));
5319 
5320         // This state is sometimes updated by template instantiation, when we
5321         // switch from the specialization referring to the template declaration
5322         // to it referring to the template definition.
5323         if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5324           Record.push_back(MSInfo->getTemplateSpecializationKind());
5325           Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
5326         } else {
5327           auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5328           Record.push_back(Spec->getTemplateSpecializationKind());
5329           Record.AddSourceLocation(Spec->getPointOfInstantiation());
5330 
5331           // The instantiation might have been resolved to a partial
5332           // specialization. If so, record which one.
5333           auto From = Spec->getInstantiatedFrom();
5334           if (auto PartialSpec =
5335                 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5336             Record.push_back(true);
5337             Record.AddDeclRef(PartialSpec);
5338             Record.AddTemplateArgumentList(
5339                 &Spec->getTemplateInstantiationArgs());
5340           } else {
5341             Record.push_back(false);
5342           }
5343         }
5344         Record.push_back(llvm::to_underlying(RD->getTagKind()));
5345         Record.AddSourceLocation(RD->getLocation());
5346         Record.AddSourceLocation(RD->getBeginLoc());
5347         Record.AddSourceRange(RD->getBraceRange());
5348 
5349         // Instantiation may change attributes; write them all out afresh.
5350         Record.push_back(D->hasAttrs());
5351         if (D->hasAttrs())
5352           Record.AddAttributes(D->getAttrs());
5353 
5354         // FIXME: Ensure we don't get here for explicit instantiations.
5355         break;
5356       }
5357 
5358       case UPD_CXX_RESOLVED_DTOR_DELETE:
5359         Record.AddDeclRef(Update.getDecl());
5360         Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
5361         break;
5362 
5363       case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
5364         auto prototype =
5365           cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
5366         Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
5367         break;
5368       }
5369 
5370       case UPD_CXX_DEDUCED_RETURN_TYPE:
5371         Record.push_back(GetOrCreateTypeID(Update.getType()));
5372         break;
5373 
5374       case UPD_DECL_MARKED_USED:
5375         break;
5376 
5377       case UPD_MANGLING_NUMBER:
5378       case UPD_STATIC_LOCAL_NUMBER:
5379         Record.push_back(Update.getNumber());
5380         break;
5381 
5382       case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
5383         Record.AddSourceRange(
5384             D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
5385         break;
5386 
5387       case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5388         auto *A = D->getAttr<OMPAllocateDeclAttr>();
5389         Record.push_back(A->getAllocatorType());
5390         Record.AddStmt(A->getAllocator());
5391         Record.AddStmt(A->getAlignment());
5392         Record.AddSourceRange(A->getRange());
5393         break;
5394       }
5395 
5396       case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5397         Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
5398         Record.AddSourceRange(
5399             D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5400         break;
5401 
5402       case UPD_DECL_EXPORTED:
5403         Record.push_back(getSubmoduleID(Update.getModule()));
5404         break;
5405 
5406       case UPD_ADDED_ATTR_TO_RECORD:
5407         Record.AddAttributes(llvm::ArrayRef(Update.getAttr()));
5408         break;
5409       }
5410     }
5411 
5412     // Add a trailing update record, if any. These must go last because we
5413     // lazily load their attached statement.
5414     if (HasUpdatedBody) {
5415       const auto *Def = cast<FunctionDecl>(D);
5416       Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
5417       Record.push_back(Def->isInlined());
5418       Record.AddSourceLocation(Def->getInnerLocStart());
5419       Record.AddFunctionDefinition(Def);
5420     } else if (HasAddedVarDefinition) {
5421       const auto *VD = cast<VarDecl>(D);
5422       Record.push_back(UPD_CXX_ADDED_VAR_DEFINITION);
5423       Record.push_back(VD->isInline());
5424       Record.push_back(VD->isInlineSpecified());
5425       Record.AddVarDeclInit(VD);
5426     }
5427 
5428     OffsetsRecord.push_back(GetDeclRef(D));
5429     OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
5430   }
5431 }
5432 
5433 void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
5434                                  RecordDataImpl &Record) {
5435   uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
5436   Record.push_back(Raw);
5437 }
5438 
5439 FileID ASTWriter::getAdjustedFileID(FileID FID) const {
5440   if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
5441       NonAffectingFileIDs.empty())
5442     return FID;
5443   auto It = llvm::lower_bound(NonAffectingFileIDs, FID);
5444   unsigned Idx = std::distance(NonAffectingFileIDs.begin(), It);
5445   unsigned Offset = NonAffectingFileIDAdjustments[Idx];
5446   return FileID::get(FID.getOpaqueValue() - Offset);
5447 }
5448 
5449 unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
5450   unsigned NumCreatedFIDs = PP->getSourceManager()
5451                                 .getLocalSLocEntry(FID.ID)
5452                                 .getFile()
5453                                 .NumCreatedFIDs;
5454 
5455   unsigned AdjustedNumCreatedFIDs = 0;
5456   for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
5457     if (IsSLocAffecting[I])
5458       ++AdjustedNumCreatedFIDs;
5459   return AdjustedNumCreatedFIDs;
5460 }
5461 
5462 SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
5463   if (Loc.isInvalid())
5464     return Loc;
5465   return Loc.getLocWithOffset(-getAdjustment(Loc.getOffset()));
5466 }
5467 
5468 SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
5469   return SourceRange(getAdjustedLocation(Range.getBegin()),
5470                      getAdjustedLocation(Range.getEnd()));
5471 }
5472 
5473 SourceLocation::UIntTy
5474 ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
5475   return Offset - getAdjustment(Offset);
5476 }
5477 
5478 SourceLocation::UIntTy
5479 ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
5480   if (NonAffectingRanges.empty())
5481     return 0;
5482 
5483   if (PP->getSourceManager().isLoadedOffset(Offset))
5484     return 0;
5485 
5486   if (Offset > NonAffectingRanges.back().getEnd().getOffset())
5487     return NonAffectingOffsetAdjustments.back();
5488 
5489   if (Offset < NonAffectingRanges.front().getBegin().getOffset())
5490     return 0;
5491 
5492   auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
5493     return Range.getEnd().getOffset() < Offset;
5494   };
5495 
5496   auto It = llvm::lower_bound(NonAffectingRanges, Offset, Contains);
5497   unsigned Idx = std::distance(NonAffectingRanges.begin(), It);
5498   return NonAffectingOffsetAdjustments[Idx];
5499 }
5500 
5501 void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
5502   Record.push_back(getAdjustedFileID(FID).getOpaqueValue());
5503 }
5504 
5505 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
5506                                   SourceLocationSequence *Seq) {
5507   Loc = getAdjustedLocation(Loc);
5508   Record.push_back(SourceLocationEncoding::encode(Loc, Seq));
5509 }
5510 
5511 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record,
5512                                SourceLocationSequence *Seq) {
5513   AddSourceLocation(Range.getBegin(), Record, Seq);
5514   AddSourceLocation(Range.getEnd(), Record, Seq);
5515 }
5516 
5517 void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5518   AddAPInt(Value.bitcastToAPInt());
5519 }
5520 
5521 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
5522   Record.push_back(getIdentifierRef(II));
5523 }
5524 
5525 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
5526   if (!II)
5527     return 0;
5528 
5529   IdentID &ID = IdentifierIDs[II];
5530   if (ID == 0)
5531     ID = NextIdentID++;
5532   return ID;
5533 }
5534 
5535 MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
5536   // Don't emit builtin macros like __LINE__ to the AST file unless they
5537   // have been redefined by the header (in which case they are not
5538   // isBuiltinMacro).
5539   if (!MI || MI->isBuiltinMacro())
5540     return 0;
5541 
5542   MacroID &ID = MacroIDs[MI];
5543   if (ID == 0) {
5544     ID = NextMacroID++;
5545     MacroInfoToEmitData Info = { Name, MI, ID };
5546     MacroInfosToEmit.push_back(Info);
5547   }
5548   return ID;
5549 }
5550 
5551 MacroID ASTWriter::getMacroID(MacroInfo *MI) {
5552   if (!MI || MI->isBuiltinMacro())
5553     return 0;
5554 
5555   assert(MacroIDs.contains(MI) && "Macro not emitted!");
5556   return MacroIDs[MI];
5557 }
5558 
5559 uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
5560   return IdentMacroDirectivesOffsetMap.lookup(Name);
5561 }
5562 
5563 void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5564   Record->push_back(Writer->getSelectorRef(SelRef));
5565 }
5566 
5567 SelectorID ASTWriter::getSelectorRef(Selector Sel) {
5568   if (Sel.getAsOpaquePtr() == nullptr) {
5569     return 0;
5570   }
5571 
5572   SelectorID SID = SelectorIDs[Sel];
5573   if (SID == 0 && Chain) {
5574     // This might trigger a ReadSelector callback, which will set the ID for
5575     // this selector.
5576     Chain->LoadSelector(Sel);
5577     SID = SelectorIDs[Sel];
5578   }
5579   if (SID == 0) {
5580     SID = NextSelectorID++;
5581     SelectorIDs[Sel] = SID;
5582   }
5583   return SID;
5584 }
5585 
5586 void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5587   AddDeclRef(Temp->getDestructor());
5588 }
5589 
5590 void ASTRecordWriter::AddTemplateArgumentLocInfo(
5591     TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
5592   switch (Kind) {
5593   case TemplateArgument::Expression:
5594     AddStmt(Arg.getAsExpr());
5595     break;
5596   case TemplateArgument::Type:
5597     AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
5598     break;
5599   case TemplateArgument::Template:
5600     AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5601     AddSourceLocation(Arg.getTemplateNameLoc());
5602     break;
5603   case TemplateArgument::TemplateExpansion:
5604     AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5605     AddSourceLocation(Arg.getTemplateNameLoc());
5606     AddSourceLocation(Arg.getTemplateEllipsisLoc());
5607     break;
5608   case TemplateArgument::Null:
5609   case TemplateArgument::Integral:
5610   case TemplateArgument::Declaration:
5611   case TemplateArgument::NullPtr:
5612   case TemplateArgument::StructuralValue:
5613   case TemplateArgument::Pack:
5614     // FIXME: Is this right?
5615     break;
5616   }
5617 }
5618 
5619 void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5620   AddTemplateArgument(Arg.getArgument());
5621 
5622   if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5623     bool InfoHasSameExpr
5624       = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5625     Record->push_back(InfoHasSameExpr);
5626     if (InfoHasSameExpr)
5627       return; // Avoid storing the same expr twice.
5628   }
5629   AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
5630 }
5631 
5632 void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
5633   if (!TInfo) {
5634     AddTypeRef(QualType());
5635     return;
5636   }
5637 
5638   AddTypeRef(TInfo->getType());
5639   AddTypeLoc(TInfo->getTypeLoc());
5640 }
5641 
5642 void ASTRecordWriter::AddTypeLoc(TypeLoc TL, LocSeq *OuterSeq) {
5643   LocSeq::State Seq(OuterSeq);
5644   TypeLocWriter TLW(*this, Seq);
5645   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
5646     TLW.Visit(TL);
5647 }
5648 
5649 void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
5650   Record.push_back(GetOrCreateTypeID(T));
5651 }
5652 
5653 TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
5654   assert(Context);
5655   return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5656     if (T.isNull())
5657       return TypeIdx();
5658     assert(!T.getLocalFastQualifiers());
5659 
5660     TypeIdx &Idx = TypeIdxs[T];
5661     if (Idx.getIndex() == 0) {
5662       if (DoneWritingDeclsAndTypes) {
5663         assert(0 && "New type seen after serializing all the types to emit!");
5664         return TypeIdx();
5665       }
5666 
5667       // We haven't seen this type before. Assign it a new ID and put it
5668       // into the queue of types to emit.
5669       Idx = TypeIdx(NextTypeID++);
5670       DeclTypesToEmit.push(T);
5671     }
5672     return Idx;
5673   });
5674 }
5675 
5676 TypeID ASTWriter::getTypeID(QualType T) const {
5677   assert(Context);
5678   return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5679     if (T.isNull())
5680       return TypeIdx();
5681     assert(!T.getLocalFastQualifiers());
5682 
5683     TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5684     assert(I != TypeIdxs.end() && "Type not emitted!");
5685     return I->second;
5686   });
5687 }
5688 
5689 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
5690   Record.push_back(GetDeclRef(D));
5691 }
5692 
5693 DeclID ASTWriter::GetDeclRef(const Decl *D) {
5694   assert(WritingAST && "Cannot request a declaration ID before AST writing");
5695 
5696   if (!D) {
5697     return 0;
5698   }
5699 
5700   // If D comes from an AST file, its declaration ID is already known and
5701   // fixed.
5702   if (D->isFromASTFile())
5703     return D->getGlobalID();
5704 
5705   assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
5706   DeclID &ID = DeclIDs[D];
5707   if (ID == 0) {
5708     if (DoneWritingDeclsAndTypes) {
5709       assert(0 && "New decl seen after serializing all the decls to emit!");
5710       return 0;
5711     }
5712 
5713     // We haven't seen this declaration before. Give it a new ID and
5714     // enqueue it in the list of declarations to emit.
5715     ID = NextDeclID++;
5716     DeclTypesToEmit.push(const_cast<Decl *>(D));
5717   }
5718 
5719   return ID;
5720 }
5721 
5722 DeclID ASTWriter::getDeclID(const Decl *D) {
5723   if (!D)
5724     return 0;
5725 
5726   // If D comes from an AST file, its declaration ID is already known and
5727   // fixed.
5728   if (D->isFromASTFile())
5729     return D->getGlobalID();
5730 
5731   assert(DeclIDs.contains(D) && "Declaration not emitted!");
5732   return DeclIDs[D];
5733 }
5734 
5735 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5736   assert(ID);
5737   assert(D);
5738 
5739   SourceLocation Loc = D->getLocation();
5740   if (Loc.isInvalid())
5741     return;
5742 
5743   // We only keep track of the file-level declarations of each file.
5744   if (!D->getLexicalDeclContext()->isFileContext())
5745     return;
5746   // FIXME: ParmVarDecls that are part of a function type of a parameter of
5747   // a function/objc method, should not have TU as lexical context.
5748   // TemplateTemplateParmDecls that are part of an alias template, should not
5749   // have TU as lexical context.
5750   if (isa<ParmVarDecl, TemplateTemplateParmDecl>(D))
5751     return;
5752 
5753   SourceManager &SM = Context->getSourceManager();
5754   SourceLocation FileLoc = SM.getFileLoc(Loc);
5755   assert(SM.isLocalSourceLocation(FileLoc));
5756   FileID FID;
5757   unsigned Offset;
5758   std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5759   if (FID.isInvalid())
5760     return;
5761   assert(SM.getSLocEntry(FID).isFile());
5762   assert(IsSLocAffecting[FID.ID]);
5763 
5764   std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
5765   if (!Info)
5766     Info = std::make_unique<DeclIDInFileInfo>();
5767 
5768   std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5769   LocDeclIDsTy &Decls = Info->DeclIDs;
5770   Decls.push_back(LocDecl);
5771 }
5772 
5773 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5774   assert(needsAnonymousDeclarationNumber(D) &&
5775          "expected an anonymous declaration");
5776 
5777   // Number the anonymous declarations within this context, if we've not
5778   // already done so.
5779   auto It = AnonymousDeclarationNumbers.find(D);
5780   if (It == AnonymousDeclarationNumbers.end()) {
5781     auto *DC = D->getLexicalDeclContext();
5782     numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5783       AnonymousDeclarationNumbers[ND] = Number;
5784     });
5785 
5786     It = AnonymousDeclarationNumbers.find(D);
5787     assert(It != AnonymousDeclarationNumbers.end() &&
5788            "declaration not found within its lexical context");
5789   }
5790 
5791   return It->second;
5792 }
5793 
5794 void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5795                                             DeclarationName Name) {
5796   switch (Name.getNameKind()) {
5797   case DeclarationName::CXXConstructorName:
5798   case DeclarationName::CXXDestructorName:
5799   case DeclarationName::CXXConversionFunctionName:
5800     AddTypeSourceInfo(DNLoc.getNamedTypeInfo());
5801     break;
5802 
5803   case DeclarationName::CXXOperatorName:
5804     AddSourceRange(DNLoc.getCXXOperatorNameRange());
5805     break;
5806 
5807   case DeclarationName::CXXLiteralOperatorName:
5808     AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc());
5809     break;
5810 
5811   case DeclarationName::Identifier:
5812   case DeclarationName::ObjCZeroArgSelector:
5813   case DeclarationName::ObjCOneArgSelector:
5814   case DeclarationName::ObjCMultiArgSelector:
5815   case DeclarationName::CXXUsingDirective:
5816   case DeclarationName::CXXDeductionGuideName:
5817     break;
5818   }
5819 }
5820 
5821 void ASTRecordWriter::AddDeclarationNameInfo(
5822     const DeclarationNameInfo &NameInfo) {
5823   AddDeclarationName(NameInfo.getName());
5824   AddSourceLocation(NameInfo.getLoc());
5825   AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
5826 }
5827 
5828 void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5829   AddNestedNameSpecifierLoc(Info.QualifierLoc);
5830   Record->push_back(Info.NumTemplParamLists);
5831   for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
5832     AddTemplateParameterList(Info.TemplParamLists[i]);
5833 }
5834 
5835 void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
5836   // Nested name specifiers usually aren't too long. I think that 8 would
5837   // typically accommodate the vast majority.
5838   SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5839 
5840   // Push each of the nested-name-specifiers's onto a stack for
5841   // serialization in reverse order.
5842   while (NNS) {
5843     NestedNames.push_back(NNS);
5844     NNS = NNS.getPrefix();
5845   }
5846 
5847   Record->push_back(NestedNames.size());
5848   while(!NestedNames.empty()) {
5849     NNS = NestedNames.pop_back_val();
5850     NestedNameSpecifier::SpecifierKind Kind
5851       = NNS.getNestedNameSpecifier()->getKind();
5852     Record->push_back(Kind);
5853     switch (Kind) {
5854     case NestedNameSpecifier::Identifier:
5855       AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5856       AddSourceRange(NNS.getLocalSourceRange());
5857       break;
5858 
5859     case NestedNameSpecifier::Namespace:
5860       AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5861       AddSourceRange(NNS.getLocalSourceRange());
5862       break;
5863 
5864     case NestedNameSpecifier::NamespaceAlias:
5865       AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5866       AddSourceRange(NNS.getLocalSourceRange());
5867       break;
5868 
5869     case NestedNameSpecifier::TypeSpec:
5870     case NestedNameSpecifier::TypeSpecWithTemplate:
5871       Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5872       AddTypeRef(NNS.getTypeLoc().getType());
5873       AddTypeLoc(NNS.getTypeLoc());
5874       AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5875       break;
5876 
5877     case NestedNameSpecifier::Global:
5878       AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5879       break;
5880 
5881     case NestedNameSpecifier::Super:
5882       AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5883       AddSourceRange(NNS.getLocalSourceRange());
5884       break;
5885     }
5886   }
5887 }
5888 
5889 void ASTRecordWriter::AddTemplateParameterList(
5890     const TemplateParameterList *TemplateParams) {
5891   assert(TemplateParams && "No TemplateParams!");
5892   AddSourceLocation(TemplateParams->getTemplateLoc());
5893   AddSourceLocation(TemplateParams->getLAngleLoc());
5894   AddSourceLocation(TemplateParams->getRAngleLoc());
5895 
5896   Record->push_back(TemplateParams->size());
5897   for (const auto &P : *TemplateParams)
5898     AddDeclRef(P);
5899   if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
5900     Record->push_back(true);
5901     AddStmt(const_cast<Expr*>(RequiresClause));
5902   } else {
5903     Record->push_back(false);
5904   }
5905 }
5906 
5907 /// Emit a template argument list.
5908 void ASTRecordWriter::AddTemplateArgumentList(
5909     const TemplateArgumentList *TemplateArgs) {
5910   assert(TemplateArgs && "No TemplateArgs!");
5911   Record->push_back(TemplateArgs->size());
5912   for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
5913     AddTemplateArgument(TemplateArgs->get(i));
5914 }
5915 
5916 void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5917     const ASTTemplateArgumentListInfo *ASTTemplArgList) {
5918   assert(ASTTemplArgList && "No ASTTemplArgList!");
5919   AddSourceLocation(ASTTemplArgList->LAngleLoc);
5920   AddSourceLocation(ASTTemplArgList->RAngleLoc);
5921   Record->push_back(ASTTemplArgList->NumTemplateArgs);
5922   const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5923   for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5924     AddTemplateArgumentLoc(TemplArgs[i]);
5925 }
5926 
5927 void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5928   Record->push_back(Set.size());
5929   for (ASTUnresolvedSet::const_iterator
5930          I = Set.begin(), E = Set.end(); I != E; ++I) {
5931     AddDeclRef(I.getDecl());
5932     Record->push_back(I.getAccess());
5933   }
5934 }
5935 
5936 // FIXME: Move this out of the main ASTRecordWriter interface.
5937 void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5938   Record->push_back(Base.isVirtual());
5939   Record->push_back(Base.isBaseOfClass());
5940   Record->push_back(Base.getAccessSpecifierAsWritten());
5941   Record->push_back(Base.getInheritConstructors());
5942   AddTypeSourceInfo(Base.getTypeSourceInfo());
5943   AddSourceRange(Base.getSourceRange());
5944   AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5945                                           : SourceLocation());
5946 }
5947 
5948 static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5949                                       ArrayRef<CXXBaseSpecifier> Bases) {
5950   ASTWriter::RecordData Record;
5951   ASTRecordWriter Writer(W, Record);
5952   Writer.push_back(Bases.size());
5953 
5954   for (auto &Base : Bases)
5955     Writer.AddCXXBaseSpecifier(Base);
5956 
5957   return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
5958 }
5959 
5960 // FIXME: Move this out of the main ASTRecordWriter interface.
5961 void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5962   AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5963 }
5964 
5965 static uint64_t
5966 EmitCXXCtorInitializers(ASTWriter &W,
5967                         ArrayRef<CXXCtorInitializer *> CtorInits) {
5968   ASTWriter::RecordData Record;
5969   ASTRecordWriter Writer(W, Record);
5970   Writer.push_back(CtorInits.size());
5971 
5972   for (auto *Init : CtorInits) {
5973     if (Init->isBaseInitializer()) {
5974       Writer.push_back(CTOR_INITIALIZER_BASE);
5975       Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5976       Writer.push_back(Init->isBaseVirtual());
5977     } else if (Init->isDelegatingInitializer()) {
5978       Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5979       Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5980     } else if (Init->isMemberInitializer()){
5981       Writer.push_back(CTOR_INITIALIZER_MEMBER);
5982       Writer.AddDeclRef(Init->getMember());
5983     } else {
5984       Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5985       Writer.AddDeclRef(Init->getIndirectMember());
5986     }
5987 
5988     Writer.AddSourceLocation(Init->getMemberLocation());
5989     Writer.AddStmt(Init->getInit());
5990     Writer.AddSourceLocation(Init->getLParenLoc());
5991     Writer.AddSourceLocation(Init->getRParenLoc());
5992     Writer.push_back(Init->isWritten());
5993     if (Init->isWritten())
5994       Writer.push_back(Init->getSourceOrder());
5995   }
5996 
5997   return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
5998 }
5999 
6000 // FIXME: Move this out of the main ASTRecordWriter interface.
6001 void ASTRecordWriter::AddCXXCtorInitializers(
6002     ArrayRef<CXXCtorInitializer *> CtorInits) {
6003   AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
6004 }
6005 
6006 void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
6007   auto &Data = D->data();
6008 
6009   Record->push_back(Data.IsLambda);
6010 
6011   BitsPacker DefinitionBits;
6012 
6013   bool ShouldSkipCheckingODR = D->shouldSkipCheckingODR();
6014   DefinitionBits.addBit(ShouldSkipCheckingODR);
6015 
6016 #define FIELD(Name, Width, Merge)                                              \
6017   if (!DefinitionBits.canWriteNextNBits(Width)) {                              \
6018     Record->push_back(DefinitionBits);                                         \
6019     DefinitionBits.reset(0);                                                   \
6020   }                                                                            \
6021   DefinitionBits.addBits(Data.Name, Width);
6022 
6023 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
6024 #undef FIELD
6025 
6026   Record->push_back(DefinitionBits);
6027 
6028   // We only perform ODR checks for decls not in GMF.
6029   if (!ShouldSkipCheckingODR)
6030     // getODRHash will compute the ODRHash if it has not been previously
6031     // computed.
6032     Record->push_back(D->getODRHash());
6033 
6034   bool ModulesDebugInfo =
6035       Writer->Context->getLangOpts().ModulesDebugInfo && !D->isDependentType();
6036   Record->push_back(ModulesDebugInfo);
6037   if (ModulesDebugInfo)
6038     Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
6039 
6040   // IsLambda bit is already saved.
6041 
6042   AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
6043   Record->push_back(Data.ComputedVisibleConversions);
6044   if (Data.ComputedVisibleConversions)
6045     AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
6046   // Data.Definition is the owning decl, no need to write it.
6047 
6048   if (!Data.IsLambda) {
6049     Record->push_back(Data.NumBases);
6050     if (Data.NumBases > 0)
6051       AddCXXBaseSpecifiers(Data.bases());
6052 
6053     // FIXME: Make VBases lazily computed when needed to avoid storing them.
6054     Record->push_back(Data.NumVBases);
6055     if (Data.NumVBases > 0)
6056       AddCXXBaseSpecifiers(Data.vbases());
6057 
6058     AddDeclRef(D->getFirstFriend());
6059   } else {
6060     auto &Lambda = D->getLambdaData();
6061 
6062     BitsPacker LambdaBits;
6063     LambdaBits.addBits(Lambda.DependencyKind, /*Width=*/2);
6064     LambdaBits.addBit(Lambda.IsGenericLambda);
6065     LambdaBits.addBits(Lambda.CaptureDefault, /*Width=*/2);
6066     LambdaBits.addBits(Lambda.NumCaptures, /*Width=*/15);
6067     LambdaBits.addBit(Lambda.HasKnownInternalLinkage);
6068     Record->push_back(LambdaBits);
6069 
6070     Record->push_back(Lambda.NumExplicitCaptures);
6071     Record->push_back(Lambda.ManglingNumber);
6072     Record->push_back(D->getDeviceLambdaManglingNumber());
6073     // The lambda context declaration and index within the context are provided
6074     // separately, so that they can be used for merging.
6075     AddTypeSourceInfo(Lambda.MethodTyInfo);
6076     for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
6077       const LambdaCapture &Capture = Lambda.Captures.front()[I];
6078       AddSourceLocation(Capture.getLocation());
6079 
6080       BitsPacker CaptureBits;
6081       CaptureBits.addBit(Capture.isImplicit());
6082       CaptureBits.addBits(Capture.getCaptureKind(), /*Width=*/3);
6083       Record->push_back(CaptureBits);
6084 
6085       switch (Capture.getCaptureKind()) {
6086       case LCK_StarThis:
6087       case LCK_This:
6088       case LCK_VLAType:
6089         break;
6090       case LCK_ByCopy:
6091       case LCK_ByRef:
6092         ValueDecl *Var =
6093             Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
6094         AddDeclRef(Var);
6095         AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
6096                                                     : SourceLocation());
6097         break;
6098       }
6099     }
6100   }
6101 }
6102 
6103 void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
6104   const Expr *Init = VD->getInit();
6105   if (!Init) {
6106     push_back(0);
6107     return;
6108   }
6109 
6110   uint64_t Val = 1;
6111   if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
6112     Val |= (ES->HasConstantInitialization ? 2 : 0);
6113     Val |= (ES->HasConstantDestruction ? 4 : 0);
6114     APValue *Evaluated = VD->getEvaluatedValue();
6115     // If the evaluated result is constant, emit it.
6116     if (Evaluated && (Evaluated->isInt() || Evaluated->isFloat()))
6117       Val |= 8;
6118   }
6119   push_back(Val);
6120   if (Val & 8) {
6121     AddAPValue(*VD->getEvaluatedValue());
6122   }
6123 
6124   writeStmtRef(Init);
6125 }
6126 
6127 void ASTWriter::ReaderInitialized(ASTReader *Reader) {
6128   assert(Reader && "Cannot remove chain");
6129   assert((!Chain || Chain == Reader) && "Cannot replace chain");
6130   assert(FirstDeclID == NextDeclID &&
6131          FirstTypeID == NextTypeID &&
6132          FirstIdentID == NextIdentID &&
6133          FirstMacroID == NextMacroID &&
6134          FirstSubmoduleID == NextSubmoduleID &&
6135          FirstSelectorID == NextSelectorID &&
6136          "Setting chain after writing has started.");
6137 
6138   Chain = Reader;
6139 
6140   // Note, this will get called multiple times, once one the reader starts up
6141   // and again each time it's done reading a PCH or module.
6142   FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6143   FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6144   FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
6145   FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
6146   FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
6147   FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
6148   NextDeclID = FirstDeclID;
6149   NextTypeID = FirstTypeID;
6150   NextIdentID = FirstIdentID;
6151   NextMacroID = FirstMacroID;
6152   NextSelectorID = FirstSelectorID;
6153   NextSubmoduleID = FirstSubmoduleID;
6154 }
6155 
6156 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
6157   // Always keep the highest ID. See \p TypeRead() for more information.
6158   IdentID &StoredID = IdentifierIDs[II];
6159   if (ID > StoredID)
6160     StoredID = ID;
6161 }
6162 
6163 void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
6164   // Always keep the highest ID. See \p TypeRead() for more information.
6165   MacroID &StoredID = MacroIDs[MI];
6166   if (ID > StoredID)
6167     StoredID = ID;
6168 }
6169 
6170 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
6171   // Always take the highest-numbered type index. This copes with an interesting
6172   // case for chained AST writing where we schedule writing the type and then,
6173   // later, deserialize the type from another AST. In this case, we want to
6174   // keep the higher-numbered entry so that we can properly write it out to
6175   // the AST file.
6176   TypeIdx &StoredIdx = TypeIdxs[T];
6177   if (Idx.getIndex() >= StoredIdx.getIndex())
6178     StoredIdx = Idx;
6179 }
6180 
6181 void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
6182   // Always keep the highest ID. See \p TypeRead() for more information.
6183   SelectorID &StoredID = SelectorIDs[S];
6184   if (ID > StoredID)
6185     StoredID = ID;
6186 }
6187 
6188 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
6189                                     MacroDefinitionRecord *MD) {
6190   assert(!MacroDefinitions.contains(MD));
6191   MacroDefinitions[MD] = ID;
6192 }
6193 
6194 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6195   assert(!SubmoduleIDs.contains(Mod));
6196   SubmoduleIDs[Mod] = ID;
6197 }
6198 
6199 void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
6200   if (Chain && Chain->isProcessingUpdateRecords()) return;
6201   assert(D->isCompleteDefinition());
6202   assert(!WritingAST && "Already writing the AST!");
6203   if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
6204     // We are interested when a PCH decl is modified.
6205     if (RD->isFromASTFile()) {
6206       // A forward reference was mutated into a definition. Rewrite it.
6207       // FIXME: This happens during template instantiation, should we
6208       // have created a new definition decl instead ?
6209       assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6210              "completed a tag from another module but not by instantiation?");
6211       DeclUpdates[RD].push_back(
6212           DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
6213     }
6214   }
6215 }
6216 
6217 static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6218   if (D->isFromASTFile())
6219     return true;
6220 
6221   // The predefined __va_list_tag struct is imported if we imported any decls.
6222   // FIXME: This is a gross hack.
6223   return D == D->getASTContext().getVaListTagDecl();
6224 }
6225 
6226 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
6227   if (Chain && Chain->isProcessingUpdateRecords()) return;
6228   assert(DC->isLookupContext() &&
6229           "Should not add lookup results to non-lookup contexts!");
6230 
6231   // TU is handled elsewhere.
6232   if (isa<TranslationUnitDecl>(DC))
6233     return;
6234 
6235   // Namespaces are handled elsewhere, except for template instantiations of
6236   // FunctionTemplateDecls in namespaces. We are interested in cases where the
6237   // local instantiations are added to an imported context. Only happens when
6238   // adding ADL lookup candidates, for example templated friends.
6239   if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6240       !isa<FunctionTemplateDecl>(D))
6241     return;
6242 
6243   // We're only interested in cases where a local declaration is added to an
6244   // imported context.
6245   if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6246     return;
6247 
6248   assert(DC == DC->getPrimaryContext() && "added to non-primary context");
6249   assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
6250   assert(!WritingAST && "Already writing the AST!");
6251   if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6252     // We're adding a visible declaration to a predefined decl context. Ensure
6253     // that we write out all of its lookup results so we don't get a nasty
6254     // surprise when we try to emit its lookup table.
6255     llvm::append_range(DeclsToEmitEvenIfUnreferenced, DC->decls());
6256   }
6257   DeclsToEmitEvenIfUnreferenced.push_back(D);
6258 }
6259 
6260 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
6261   if (Chain && Chain->isProcessingUpdateRecords()) return;
6262   assert(D->isImplicit());
6263 
6264   // We're only interested in cases where a local declaration is added to an
6265   // imported context.
6266   if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6267     return;
6268 
6269   if (!isa<CXXMethodDecl>(D))
6270     return;
6271 
6272   // A decl coming from PCH was modified.
6273   assert(RD->isCompleteDefinition());
6274   assert(!WritingAST && "Already writing the AST!");
6275   DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
6276 }
6277 
6278 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
6279   if (Chain && Chain->isProcessingUpdateRecords()) return;
6280   assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6281   if (!Chain) return;
6282   Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6283     // If we don't already know the exception specification for this redecl
6284     // chain, add an update record for it.
6285     if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6286                                       ->getType()
6287                                       ->castAs<FunctionProtoType>()
6288                                       ->getExceptionSpecType()))
6289       DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6290   });
6291 }
6292 
6293 void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
6294   if (Chain && Chain->isProcessingUpdateRecords()) return;
6295   assert(!WritingAST && "Already writing the AST!");
6296   if (!Chain) return;
6297   Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6298     DeclUpdates[D].push_back(
6299         DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6300   });
6301 }
6302 
6303 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6304                                        const FunctionDecl *Delete,
6305                                        Expr *ThisArg) {
6306   if (Chain && Chain->isProcessingUpdateRecords()) return;
6307   assert(!WritingAST && "Already writing the AST!");
6308   assert(Delete && "Not given an operator delete");
6309   if (!Chain) return;
6310   Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
6311     DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6312   });
6313 }
6314 
6315 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
6316   if (Chain && Chain->isProcessingUpdateRecords()) return;
6317   assert(!WritingAST && "Already writing the AST!");
6318   if (!D->isFromASTFile())
6319     return; // Declaration not imported from PCH.
6320 
6321   // Implicit function decl from a PCH was defined.
6322   DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6323 }
6324 
6325 void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6326   if (Chain && Chain->isProcessingUpdateRecords()) return;
6327   assert(!WritingAST && "Already writing the AST!");
6328   if (!D->isFromASTFile())
6329     return;
6330 
6331   DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6332 }
6333 
6334 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
6335   if (Chain && Chain->isProcessingUpdateRecords()) return;
6336   assert(!WritingAST && "Already writing the AST!");
6337   if (!D->isFromASTFile())
6338     return;
6339 
6340   DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6341 }
6342 
6343 void ASTWriter::InstantiationRequested(const ValueDecl *D) {
6344   if (Chain && Chain->isProcessingUpdateRecords()) return;
6345   assert(!WritingAST && "Already writing the AST!");
6346   if (!D->isFromASTFile())
6347     return;
6348 
6349   // Since the actual instantiation is delayed, this really means that we need
6350   // to update the instantiation location.
6351   SourceLocation POI;
6352   if (auto *VD = dyn_cast<VarDecl>(D))
6353     POI = VD->getPointOfInstantiation();
6354   else
6355     POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6356   DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
6357 }
6358 
6359 void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
6360   if (Chain && Chain->isProcessingUpdateRecords()) return;
6361   assert(!WritingAST && "Already writing the AST!");
6362   if (!D->isFromASTFile())
6363     return;
6364 
6365   DeclUpdates[D].push_back(
6366       DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6367 }
6368 
6369 void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6370   assert(!WritingAST && "Already writing the AST!");
6371   if (!D->isFromASTFile())
6372     return;
6373 
6374   DeclUpdates[D].push_back(
6375       DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6376 }
6377 
6378 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6379                                              const ObjCInterfaceDecl *IFD) {
6380   if (Chain && Chain->isProcessingUpdateRecords()) return;
6381   assert(!WritingAST && "Already writing the AST!");
6382   if (!IFD->isFromASTFile())
6383     return; // Declaration not imported from PCH.
6384 
6385   assert(IFD->getDefinition() && "Category on a class without a definition?");
6386   ObjCClassesWithCategories.insert(
6387     const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
6388 }
6389 
6390 void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
6391   if (Chain && Chain->isProcessingUpdateRecords()) return;
6392   assert(!WritingAST && "Already writing the AST!");
6393 
6394   // If there is *any* declaration of the entity that's not from an AST file,
6395   // we can skip writing the update record. We make sure that isUsed() triggers
6396   // completion of the redeclaration chain of the entity.
6397   for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6398     if (IsLocalDecl(Prev))
6399       return;
6400 
6401   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
6402 }
6403 
6404 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
6405   if (Chain && Chain->isProcessingUpdateRecords()) return;
6406   assert(!WritingAST && "Already writing the AST!");
6407   if (!D->isFromASTFile())
6408     return;
6409 
6410   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6411 }
6412 
6413 void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
6414   if (Chain && Chain->isProcessingUpdateRecords()) return;
6415   assert(!WritingAST && "Already writing the AST!");
6416   if (!D->isFromASTFile())
6417     return;
6418 
6419   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
6420 }
6421 
6422 void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6423                                                      const Attr *Attr) {
6424   if (Chain && Chain->isProcessingUpdateRecords()) return;
6425   assert(!WritingAST && "Already writing the AST!");
6426   if (!D->isFromASTFile())
6427     return;
6428 
6429   DeclUpdates[D].push_back(
6430       DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
6431 }
6432 
6433 void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
6434   if (Chain && Chain->isProcessingUpdateRecords()) return;
6435   assert(!WritingAST && "Already writing the AST!");
6436   assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
6437   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
6438 }
6439 
6440 void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6441                                        const RecordDecl *Record) {
6442   if (Chain && Chain->isProcessingUpdateRecords()) return;
6443   assert(!WritingAST && "Already writing the AST!");
6444   if (!Record->isFromASTFile())
6445     return;
6446   DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6447 }
6448 
6449 void ASTWriter::AddedCXXTemplateSpecialization(
6450     const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6451   assert(!WritingAST && "Already writing the AST!");
6452 
6453   if (!TD->getFirstDecl()->isFromASTFile())
6454     return;
6455   if (Chain && Chain->isProcessingUpdateRecords())
6456     return;
6457 
6458   DeclsToEmitEvenIfUnreferenced.push_back(D);
6459 }
6460 
6461 void ASTWriter::AddedCXXTemplateSpecialization(
6462     const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6463   assert(!WritingAST && "Already writing the AST!");
6464 
6465   if (!TD->getFirstDecl()->isFromASTFile())
6466     return;
6467   if (Chain && Chain->isProcessingUpdateRecords())
6468     return;
6469 
6470   DeclsToEmitEvenIfUnreferenced.push_back(D);
6471 }
6472 
6473 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6474                                                const FunctionDecl *D) {
6475   assert(!WritingAST && "Already writing the AST!");
6476 
6477   if (!TD->getFirstDecl()->isFromASTFile())
6478     return;
6479   if (Chain && Chain->isProcessingUpdateRecords())
6480     return;
6481 
6482   DeclsToEmitEvenIfUnreferenced.push_back(D);
6483 }
6484 
6485 //===----------------------------------------------------------------------===//
6486 //// OMPClause Serialization
6487 ////===----------------------------------------------------------------------===//
6488 
6489 namespace {
6490 
6491 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
6492   ASTRecordWriter &Record;
6493 
6494 public:
6495   OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
6496 #define GEN_CLANG_CLAUSE_CLASS
6497 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
6498 #include "llvm/Frontend/OpenMP/OMP.inc"
6499   void writeClause(OMPClause *C);
6500   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
6501   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
6502 };
6503 
6504 }
6505 
6506 void ASTRecordWriter::writeOMPClause(OMPClause *C) {
6507   OMPClauseWriter(*this).writeClause(C);
6508 }
6509 
6510 void OMPClauseWriter::writeClause(OMPClause *C) {
6511   Record.push_back(unsigned(C->getClauseKind()));
6512   Visit(C);
6513   Record.AddSourceLocation(C->getBeginLoc());
6514   Record.AddSourceLocation(C->getEndLoc());
6515 }
6516 
6517 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
6518   Record.push_back(uint64_t(C->getCaptureRegion()));
6519   Record.AddStmt(C->getPreInitStmt());
6520 }
6521 
6522 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6523   VisitOMPClauseWithPreInit(C);
6524   Record.AddStmt(C->getPostUpdateExpr());
6525 }
6526 
6527 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6528   VisitOMPClauseWithPreInit(C);
6529   Record.push_back(uint64_t(C->getNameModifier()));
6530   Record.AddSourceLocation(C->getNameModifierLoc());
6531   Record.AddSourceLocation(C->getColonLoc());
6532   Record.AddStmt(C->getCondition());
6533   Record.AddSourceLocation(C->getLParenLoc());
6534 }
6535 
6536 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
6537   VisitOMPClauseWithPreInit(C);
6538   Record.AddStmt(C->getCondition());
6539   Record.AddSourceLocation(C->getLParenLoc());
6540 }
6541 
6542 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6543   VisitOMPClauseWithPreInit(C);
6544   Record.AddStmt(C->getNumThreads());
6545   Record.AddSourceLocation(C->getLParenLoc());
6546 }
6547 
6548 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6549   Record.AddStmt(C->getSafelen());
6550   Record.AddSourceLocation(C->getLParenLoc());
6551 }
6552 
6553 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6554   Record.AddStmt(C->getSimdlen());
6555   Record.AddSourceLocation(C->getLParenLoc());
6556 }
6557 
6558 void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause *C) {
6559   Record.push_back(C->getNumSizes());
6560   for (Expr *Size : C->getSizesRefs())
6561     Record.AddStmt(Size);
6562   Record.AddSourceLocation(C->getLParenLoc());
6563 }
6564 
6565 void OMPClauseWriter::VisitOMPFullClause(OMPFullClause *C) {}
6566 
6567 void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause *C) {
6568   Record.AddStmt(C->getFactor());
6569   Record.AddSourceLocation(C->getLParenLoc());
6570 }
6571 
6572 void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6573   Record.AddStmt(C->getAllocator());
6574   Record.AddSourceLocation(C->getLParenLoc());
6575 }
6576 
6577 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6578   Record.AddStmt(C->getNumForLoops());
6579   Record.AddSourceLocation(C->getLParenLoc());
6580 }
6581 
6582 void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
6583   Record.AddStmt(C->getEventHandler());
6584   Record.AddSourceLocation(C->getLParenLoc());
6585 }
6586 
6587 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
6588   Record.push_back(unsigned(C->getDefaultKind()));
6589   Record.AddSourceLocation(C->getLParenLoc());
6590   Record.AddSourceLocation(C->getDefaultKindKwLoc());
6591 }
6592 
6593 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
6594   Record.push_back(unsigned(C->getProcBindKind()));
6595   Record.AddSourceLocation(C->getLParenLoc());
6596   Record.AddSourceLocation(C->getProcBindKindKwLoc());
6597 }
6598 
6599 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6600   VisitOMPClauseWithPreInit(C);
6601   Record.push_back(C->getScheduleKind());
6602   Record.push_back(C->getFirstScheduleModifier());
6603   Record.push_back(C->getSecondScheduleModifier());
6604   Record.AddStmt(C->getChunkSize());
6605   Record.AddSourceLocation(C->getLParenLoc());
6606   Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6607   Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6608   Record.AddSourceLocation(C->getScheduleKindLoc());
6609   Record.AddSourceLocation(C->getCommaLoc());
6610 }
6611 
6612 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6613   Record.push_back(C->getLoopNumIterations().size());
6614   Record.AddStmt(C->getNumForLoops());
6615   for (Expr *NumIter : C->getLoopNumIterations())
6616     Record.AddStmt(NumIter);
6617   for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
6618     Record.AddStmt(C->getLoopCounter(I));
6619   Record.AddSourceLocation(C->getLParenLoc());
6620 }
6621 
6622 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6623 
6624 void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6625 
6626 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6627 
6628 void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6629 
6630 void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6631 
6632 void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
6633   Record.push_back(C->isExtended() ? 1 : 0);
6634   if (C->isExtended()) {
6635     Record.AddSourceLocation(C->getLParenLoc());
6636     Record.AddSourceLocation(C->getArgumentLoc());
6637     Record.writeEnum(C->getDependencyKind());
6638   }
6639 }
6640 
6641 void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6642 
6643 void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause *) {}
6644 
6645 // Save the parameter of fail clause.
6646 void OMPClauseWriter::VisitOMPFailClause(OMPFailClause *C) {
6647   Record.AddSourceLocation(C->getLParenLoc());
6648   Record.AddSourceLocation(C->getFailParameterLoc());
6649   Record.writeEnum(C->getFailParameter());
6650 }
6651 
6652 void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6653 
6654 void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
6655 
6656 void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
6657 
6658 void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
6659 
6660 void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
6661 
6662 void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6663 
6664 void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6665 
6666 void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6667 
6668 void OMPClauseWriter::VisitOMPInitClause(OMPInitClause *C) {
6669   Record.push_back(C->varlist_size());
6670   for (Expr *VE : C->varlists())
6671     Record.AddStmt(VE);
6672   Record.writeBool(C->getIsTarget());
6673   Record.writeBool(C->getIsTargetSync());
6674   Record.AddSourceLocation(C->getLParenLoc());
6675   Record.AddSourceLocation(C->getVarLoc());
6676 }
6677 
6678 void OMPClauseWriter::VisitOMPUseClause(OMPUseClause *C) {
6679   Record.AddStmt(C->getInteropVar());
6680   Record.AddSourceLocation(C->getLParenLoc());
6681   Record.AddSourceLocation(C->getVarLoc());
6682 }
6683 
6684 void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *C) {
6685   Record.AddStmt(C->getInteropVar());
6686   Record.AddSourceLocation(C->getLParenLoc());
6687   Record.AddSourceLocation(C->getVarLoc());
6688 }
6689 
6690 void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
6691   VisitOMPClauseWithPreInit(C);
6692   Record.AddStmt(C->getCondition());
6693   Record.AddSourceLocation(C->getLParenLoc());
6694 }
6695 
6696 void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause *C) {
6697   VisitOMPClauseWithPreInit(C);
6698   Record.AddStmt(C->getCondition());
6699   Record.AddSourceLocation(C->getLParenLoc());
6700 }
6701 
6702 void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause *C) {
6703   VisitOMPClauseWithPreInit(C);
6704   Record.AddStmt(C->getThreadID());
6705   Record.AddSourceLocation(C->getLParenLoc());
6706 }
6707 
6708 void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause *C) {
6709   Record.AddStmt(C->getAlignment());
6710   Record.AddSourceLocation(C->getLParenLoc());
6711 }
6712 
6713 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6714   Record.push_back(C->varlist_size());
6715   Record.AddSourceLocation(C->getLParenLoc());
6716   for (auto *VE : C->varlists()) {
6717     Record.AddStmt(VE);
6718   }
6719   for (auto *VE : C->private_copies()) {
6720     Record.AddStmt(VE);
6721   }
6722 }
6723 
6724 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6725   Record.push_back(C->varlist_size());
6726   VisitOMPClauseWithPreInit(C);
6727   Record.AddSourceLocation(C->getLParenLoc());
6728   for (auto *VE : C->varlists()) {
6729     Record.AddStmt(VE);
6730   }
6731   for (auto *VE : C->private_copies()) {
6732     Record.AddStmt(VE);
6733   }
6734   for (auto *VE : C->inits()) {
6735     Record.AddStmt(VE);
6736   }
6737 }
6738 
6739 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6740   Record.push_back(C->varlist_size());
6741   VisitOMPClauseWithPostUpdate(C);
6742   Record.AddSourceLocation(C->getLParenLoc());
6743   Record.writeEnum(C->getKind());
6744   Record.AddSourceLocation(C->getKindLoc());
6745   Record.AddSourceLocation(C->getColonLoc());
6746   for (auto *VE : C->varlists())
6747     Record.AddStmt(VE);
6748   for (auto *E : C->private_copies())
6749     Record.AddStmt(E);
6750   for (auto *E : C->source_exprs())
6751     Record.AddStmt(E);
6752   for (auto *E : C->destination_exprs())
6753     Record.AddStmt(E);
6754   for (auto *E : C->assignment_ops())
6755     Record.AddStmt(E);
6756 }
6757 
6758 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6759   Record.push_back(C->varlist_size());
6760   Record.AddSourceLocation(C->getLParenLoc());
6761   for (auto *VE : C->varlists())
6762     Record.AddStmt(VE);
6763 }
6764 
6765 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6766   Record.push_back(C->varlist_size());
6767   Record.writeEnum(C->getModifier());
6768   VisitOMPClauseWithPostUpdate(C);
6769   Record.AddSourceLocation(C->getLParenLoc());
6770   Record.AddSourceLocation(C->getModifierLoc());
6771   Record.AddSourceLocation(C->getColonLoc());
6772   Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6773   Record.AddDeclarationNameInfo(C->getNameInfo());
6774   for (auto *VE : C->varlists())
6775     Record.AddStmt(VE);
6776   for (auto *VE : C->privates())
6777     Record.AddStmt(VE);
6778   for (auto *E : C->lhs_exprs())
6779     Record.AddStmt(E);
6780   for (auto *E : C->rhs_exprs())
6781     Record.AddStmt(E);
6782   for (auto *E : C->reduction_ops())
6783     Record.AddStmt(E);
6784   if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
6785     for (auto *E : C->copy_ops())
6786       Record.AddStmt(E);
6787     for (auto *E : C->copy_array_temps())
6788       Record.AddStmt(E);
6789     for (auto *E : C->copy_array_elems())
6790       Record.AddStmt(E);
6791   }
6792 }
6793 
6794 void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6795   Record.push_back(C->varlist_size());
6796   VisitOMPClauseWithPostUpdate(C);
6797   Record.AddSourceLocation(C->getLParenLoc());
6798   Record.AddSourceLocation(C->getColonLoc());
6799   Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6800   Record.AddDeclarationNameInfo(C->getNameInfo());
6801   for (auto *VE : C->varlists())
6802     Record.AddStmt(VE);
6803   for (auto *VE : C->privates())
6804     Record.AddStmt(VE);
6805   for (auto *E : C->lhs_exprs())
6806     Record.AddStmt(E);
6807   for (auto *E : C->rhs_exprs())
6808     Record.AddStmt(E);
6809   for (auto *E : C->reduction_ops())
6810     Record.AddStmt(E);
6811 }
6812 
6813 void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6814   Record.push_back(C->varlist_size());
6815   VisitOMPClauseWithPostUpdate(C);
6816   Record.AddSourceLocation(C->getLParenLoc());
6817   Record.AddSourceLocation(C->getColonLoc());
6818   Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6819   Record.AddDeclarationNameInfo(C->getNameInfo());
6820   for (auto *VE : C->varlists())
6821     Record.AddStmt(VE);
6822   for (auto *VE : C->privates())
6823     Record.AddStmt(VE);
6824   for (auto *E : C->lhs_exprs())
6825     Record.AddStmt(E);
6826   for (auto *E : C->rhs_exprs())
6827     Record.AddStmt(E);
6828   for (auto *E : C->reduction_ops())
6829     Record.AddStmt(E);
6830   for (auto *E : C->taskgroup_descriptors())
6831     Record.AddStmt(E);
6832 }
6833 
6834 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6835   Record.push_back(C->varlist_size());
6836   VisitOMPClauseWithPostUpdate(C);
6837   Record.AddSourceLocation(C->getLParenLoc());
6838   Record.AddSourceLocation(C->getColonLoc());
6839   Record.push_back(C->getModifier());
6840   Record.AddSourceLocation(C->getModifierLoc());
6841   for (auto *VE : C->varlists()) {
6842     Record.AddStmt(VE);
6843   }
6844   for (auto *VE : C->privates()) {
6845     Record.AddStmt(VE);
6846   }
6847   for (auto *VE : C->inits()) {
6848     Record.AddStmt(VE);
6849   }
6850   for (auto *VE : C->updates()) {
6851     Record.AddStmt(VE);
6852   }
6853   for (auto *VE : C->finals()) {
6854     Record.AddStmt(VE);
6855   }
6856   Record.AddStmt(C->getStep());
6857   Record.AddStmt(C->getCalcStep());
6858   for (auto *VE : C->used_expressions())
6859     Record.AddStmt(VE);
6860 }
6861 
6862 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6863   Record.push_back(C->varlist_size());
6864   Record.AddSourceLocation(C->getLParenLoc());
6865   Record.AddSourceLocation(C->getColonLoc());
6866   for (auto *VE : C->varlists())
6867     Record.AddStmt(VE);
6868   Record.AddStmt(C->getAlignment());
6869 }
6870 
6871 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6872   Record.push_back(C->varlist_size());
6873   Record.AddSourceLocation(C->getLParenLoc());
6874   for (auto *VE : C->varlists())
6875     Record.AddStmt(VE);
6876   for (auto *E : C->source_exprs())
6877     Record.AddStmt(E);
6878   for (auto *E : C->destination_exprs())
6879     Record.AddStmt(E);
6880   for (auto *E : C->assignment_ops())
6881     Record.AddStmt(E);
6882 }
6883 
6884 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6885   Record.push_back(C->varlist_size());
6886   Record.AddSourceLocation(C->getLParenLoc());
6887   for (auto *VE : C->varlists())
6888     Record.AddStmt(VE);
6889   for (auto *E : C->source_exprs())
6890     Record.AddStmt(E);
6891   for (auto *E : C->destination_exprs())
6892     Record.AddStmt(E);
6893   for (auto *E : C->assignment_ops())
6894     Record.AddStmt(E);
6895 }
6896 
6897 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6898   Record.push_back(C->varlist_size());
6899   Record.AddSourceLocation(C->getLParenLoc());
6900   for (auto *VE : C->varlists())
6901     Record.AddStmt(VE);
6902 }
6903 
6904 void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
6905   Record.AddStmt(C->getDepobj());
6906   Record.AddSourceLocation(C->getLParenLoc());
6907 }
6908 
6909 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6910   Record.push_back(C->varlist_size());
6911   Record.push_back(C->getNumLoops());
6912   Record.AddSourceLocation(C->getLParenLoc());
6913   Record.AddStmt(C->getModifier());
6914   Record.push_back(C->getDependencyKind());
6915   Record.AddSourceLocation(C->getDependencyLoc());
6916   Record.AddSourceLocation(C->getColonLoc());
6917   Record.AddSourceLocation(C->getOmpAllMemoryLoc());
6918   for (auto *VE : C->varlists())
6919     Record.AddStmt(VE);
6920   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6921     Record.AddStmt(C->getLoopData(I));
6922 }
6923 
6924 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6925   VisitOMPClauseWithPreInit(C);
6926   Record.writeEnum(C->getModifier());
6927   Record.AddStmt(C->getDevice());
6928   Record.AddSourceLocation(C->getModifierLoc());
6929   Record.AddSourceLocation(C->getLParenLoc());
6930 }
6931 
6932 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6933   Record.push_back(C->varlist_size());
6934   Record.push_back(C->getUniqueDeclarationsNum());
6935   Record.push_back(C->getTotalComponentListNum());
6936   Record.push_back(C->getTotalComponentsNum());
6937   Record.AddSourceLocation(C->getLParenLoc());
6938   bool HasIteratorModifier = false;
6939   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
6940     Record.push_back(C->getMapTypeModifier(I));
6941     Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6942     if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)
6943       HasIteratorModifier = true;
6944   }
6945   Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6946   Record.AddDeclarationNameInfo(C->getMapperIdInfo());
6947   Record.push_back(C->getMapType());
6948   Record.AddSourceLocation(C->getMapLoc());
6949   Record.AddSourceLocation(C->getColonLoc());
6950   for (auto *E : C->varlists())
6951     Record.AddStmt(E);
6952   for (auto *E : C->mapperlists())
6953     Record.AddStmt(E);
6954   if (HasIteratorModifier)
6955     Record.AddStmt(C->getIteratorModifier());
6956   for (auto *D : C->all_decls())
6957     Record.AddDeclRef(D);
6958   for (auto N : C->all_num_lists())
6959     Record.push_back(N);
6960   for (auto N : C->all_lists_sizes())
6961     Record.push_back(N);
6962   for (auto &M : C->all_components()) {
6963     Record.AddStmt(M.getAssociatedExpression());
6964     Record.AddDeclRef(M.getAssociatedDeclaration());
6965   }
6966 }
6967 
6968 void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6969   Record.push_back(C->varlist_size());
6970   Record.AddSourceLocation(C->getLParenLoc());
6971   Record.AddSourceLocation(C->getColonLoc());
6972   Record.AddStmt(C->getAllocator());
6973   for (auto *VE : C->varlists())
6974     Record.AddStmt(VE);
6975 }
6976 
6977 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6978   VisitOMPClauseWithPreInit(C);
6979   Record.AddStmt(C->getNumTeams());
6980   Record.AddSourceLocation(C->getLParenLoc());
6981 }
6982 
6983 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6984   VisitOMPClauseWithPreInit(C);
6985   Record.AddStmt(C->getThreadLimit());
6986   Record.AddSourceLocation(C->getLParenLoc());
6987 }
6988 
6989 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
6990   VisitOMPClauseWithPreInit(C);
6991   Record.AddStmt(C->getPriority());
6992   Record.AddSourceLocation(C->getLParenLoc());
6993 }
6994 
6995 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
6996   VisitOMPClauseWithPreInit(C);
6997   Record.writeEnum(C->getModifier());
6998   Record.AddStmt(C->getGrainsize());
6999   Record.AddSourceLocation(C->getModifierLoc());
7000   Record.AddSourceLocation(C->getLParenLoc());
7001 }
7002 
7003 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
7004   VisitOMPClauseWithPreInit(C);
7005   Record.writeEnum(C->getModifier());
7006   Record.AddStmt(C->getNumTasks());
7007   Record.AddSourceLocation(C->getModifierLoc());
7008   Record.AddSourceLocation(C->getLParenLoc());
7009 }
7010 
7011 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
7012   Record.AddStmt(C->getHint());
7013   Record.AddSourceLocation(C->getLParenLoc());
7014 }
7015 
7016 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
7017   VisitOMPClauseWithPreInit(C);
7018   Record.push_back(C->getDistScheduleKind());
7019   Record.AddStmt(C->getChunkSize());
7020   Record.AddSourceLocation(C->getLParenLoc());
7021   Record.AddSourceLocation(C->getDistScheduleKindLoc());
7022   Record.AddSourceLocation(C->getCommaLoc());
7023 }
7024 
7025 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
7026   Record.push_back(C->getDefaultmapKind());
7027   Record.push_back(C->getDefaultmapModifier());
7028   Record.AddSourceLocation(C->getLParenLoc());
7029   Record.AddSourceLocation(C->getDefaultmapModifierLoc());
7030   Record.AddSourceLocation(C->getDefaultmapKindLoc());
7031 }
7032 
7033 void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
7034   Record.push_back(C->varlist_size());
7035   Record.push_back(C->getUniqueDeclarationsNum());
7036   Record.push_back(C->getTotalComponentListNum());
7037   Record.push_back(C->getTotalComponentsNum());
7038   Record.AddSourceLocation(C->getLParenLoc());
7039   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7040     Record.push_back(C->getMotionModifier(I));
7041     Record.AddSourceLocation(C->getMotionModifierLoc(I));
7042   }
7043   Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7044   Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7045   Record.AddSourceLocation(C->getColonLoc());
7046   for (auto *E : C->varlists())
7047     Record.AddStmt(E);
7048   for (auto *E : C->mapperlists())
7049     Record.AddStmt(E);
7050   for (auto *D : C->all_decls())
7051     Record.AddDeclRef(D);
7052   for (auto N : C->all_num_lists())
7053     Record.push_back(N);
7054   for (auto N : C->all_lists_sizes())
7055     Record.push_back(N);
7056   for (auto &M : C->all_components()) {
7057     Record.AddStmt(M.getAssociatedExpression());
7058     Record.writeBool(M.isNonContiguous());
7059     Record.AddDeclRef(M.getAssociatedDeclaration());
7060   }
7061 }
7062 
7063 void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
7064   Record.push_back(C->varlist_size());
7065   Record.push_back(C->getUniqueDeclarationsNum());
7066   Record.push_back(C->getTotalComponentListNum());
7067   Record.push_back(C->getTotalComponentsNum());
7068   Record.AddSourceLocation(C->getLParenLoc());
7069   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7070     Record.push_back(C->getMotionModifier(I));
7071     Record.AddSourceLocation(C->getMotionModifierLoc(I));
7072   }
7073   Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7074   Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7075   Record.AddSourceLocation(C->getColonLoc());
7076   for (auto *E : C->varlists())
7077     Record.AddStmt(E);
7078   for (auto *E : C->mapperlists())
7079     Record.AddStmt(E);
7080   for (auto *D : C->all_decls())
7081     Record.AddDeclRef(D);
7082   for (auto N : C->all_num_lists())
7083     Record.push_back(N);
7084   for (auto N : C->all_lists_sizes())
7085     Record.push_back(N);
7086   for (auto &M : C->all_components()) {
7087     Record.AddStmt(M.getAssociatedExpression());
7088     Record.writeBool(M.isNonContiguous());
7089     Record.AddDeclRef(M.getAssociatedDeclaration());
7090   }
7091 }
7092 
7093 void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
7094   Record.push_back(C->varlist_size());
7095   Record.push_back(C->getUniqueDeclarationsNum());
7096   Record.push_back(C->getTotalComponentListNum());
7097   Record.push_back(C->getTotalComponentsNum());
7098   Record.AddSourceLocation(C->getLParenLoc());
7099   for (auto *E : C->varlists())
7100     Record.AddStmt(E);
7101   for (auto *VE : C->private_copies())
7102     Record.AddStmt(VE);
7103   for (auto *VE : C->inits())
7104     Record.AddStmt(VE);
7105   for (auto *D : C->all_decls())
7106     Record.AddDeclRef(D);
7107   for (auto N : C->all_num_lists())
7108     Record.push_back(N);
7109   for (auto N : C->all_lists_sizes())
7110     Record.push_back(N);
7111   for (auto &M : C->all_components()) {
7112     Record.AddStmt(M.getAssociatedExpression());
7113     Record.AddDeclRef(M.getAssociatedDeclaration());
7114   }
7115 }
7116 
7117 void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
7118   Record.push_back(C->varlist_size());
7119   Record.push_back(C->getUniqueDeclarationsNum());
7120   Record.push_back(C->getTotalComponentListNum());
7121   Record.push_back(C->getTotalComponentsNum());
7122   Record.AddSourceLocation(C->getLParenLoc());
7123   for (auto *E : C->varlists())
7124     Record.AddStmt(E);
7125   for (auto *D : C->all_decls())
7126     Record.AddDeclRef(D);
7127   for (auto N : C->all_num_lists())
7128     Record.push_back(N);
7129   for (auto N : C->all_lists_sizes())
7130     Record.push_back(N);
7131   for (auto &M : C->all_components()) {
7132     Record.AddStmt(M.getAssociatedExpression());
7133     Record.AddDeclRef(M.getAssociatedDeclaration());
7134   }
7135 }
7136 
7137 void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
7138   Record.push_back(C->varlist_size());
7139   Record.push_back(C->getUniqueDeclarationsNum());
7140   Record.push_back(C->getTotalComponentListNum());
7141   Record.push_back(C->getTotalComponentsNum());
7142   Record.AddSourceLocation(C->getLParenLoc());
7143   for (auto *E : C->varlists())
7144     Record.AddStmt(E);
7145   for (auto *D : C->all_decls())
7146     Record.AddDeclRef(D);
7147   for (auto N : C->all_num_lists())
7148     Record.push_back(N);
7149   for (auto N : C->all_lists_sizes())
7150     Record.push_back(N);
7151   for (auto &M : C->all_components()) {
7152     Record.AddStmt(M.getAssociatedExpression());
7153     Record.AddDeclRef(M.getAssociatedDeclaration());
7154   }
7155 }
7156 
7157 void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
7158   Record.push_back(C->varlist_size());
7159   Record.push_back(C->getUniqueDeclarationsNum());
7160   Record.push_back(C->getTotalComponentListNum());
7161   Record.push_back(C->getTotalComponentsNum());
7162   Record.AddSourceLocation(C->getLParenLoc());
7163   for (auto *E : C->varlists())
7164     Record.AddStmt(E);
7165   for (auto *D : C->all_decls())
7166     Record.AddDeclRef(D);
7167   for (auto N : C->all_num_lists())
7168     Record.push_back(N);
7169   for (auto N : C->all_lists_sizes())
7170     Record.push_back(N);
7171   for (auto &M : C->all_components()) {
7172     Record.AddStmt(M.getAssociatedExpression());
7173     Record.AddDeclRef(M.getAssociatedDeclaration());
7174   }
7175 }
7176 
7177 void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
7178 
7179 void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7180     OMPUnifiedSharedMemoryClause *) {}
7181 
7182 void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
7183 
7184 void
7185 OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
7186 }
7187 
7188 void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7189     OMPAtomicDefaultMemOrderClause *C) {
7190   Record.push_back(C->getAtomicDefaultMemOrderKind());
7191   Record.AddSourceLocation(C->getLParenLoc());
7192   Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
7193 }
7194 
7195 void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
7196   Record.push_back(C->getAtKind());
7197   Record.AddSourceLocation(C->getLParenLoc());
7198   Record.AddSourceLocation(C->getAtKindKwLoc());
7199 }
7200 
7201 void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
7202   Record.push_back(C->getSeverityKind());
7203   Record.AddSourceLocation(C->getLParenLoc());
7204   Record.AddSourceLocation(C->getSeverityKindKwLoc());
7205 }
7206 
7207 void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
7208   Record.AddStmt(C->getMessageString());
7209   Record.AddSourceLocation(C->getLParenLoc());
7210 }
7211 
7212 void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
7213   Record.push_back(C->varlist_size());
7214   Record.AddSourceLocation(C->getLParenLoc());
7215   for (auto *VE : C->varlists())
7216     Record.AddStmt(VE);
7217   for (auto *E : C->private_refs())
7218     Record.AddStmt(E);
7219 }
7220 
7221 void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
7222   Record.push_back(C->varlist_size());
7223   Record.AddSourceLocation(C->getLParenLoc());
7224   for (auto *VE : C->varlists())
7225     Record.AddStmt(VE);
7226 }
7227 
7228 void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
7229   Record.push_back(C->varlist_size());
7230   Record.AddSourceLocation(C->getLParenLoc());
7231   for (auto *VE : C->varlists())
7232     Record.AddStmt(VE);
7233 }
7234 
7235 void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
7236   Record.writeEnum(C->getKind());
7237   Record.writeEnum(C->getModifier());
7238   Record.AddSourceLocation(C->getLParenLoc());
7239   Record.AddSourceLocation(C->getKindKwLoc());
7240   Record.AddSourceLocation(C->getModifierKwLoc());
7241 }
7242 
7243 void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
7244   Record.push_back(C->getNumberOfAllocators());
7245   Record.AddSourceLocation(C->getLParenLoc());
7246   for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
7247     OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
7248     Record.AddStmt(Data.Allocator);
7249     Record.AddStmt(Data.AllocatorTraits);
7250     Record.AddSourceLocation(Data.LParenLoc);
7251     Record.AddSourceLocation(Data.RParenLoc);
7252   }
7253 }
7254 
7255 void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
7256   Record.push_back(C->varlist_size());
7257   Record.AddSourceLocation(C->getLParenLoc());
7258   Record.AddStmt(C->getModifier());
7259   Record.AddSourceLocation(C->getColonLoc());
7260   for (Expr *E : C->varlists())
7261     Record.AddStmt(E);
7262 }
7263 
7264 void OMPClauseWriter::VisitOMPBindClause(OMPBindClause *C) {
7265   Record.writeEnum(C->getBindKind());
7266   Record.AddSourceLocation(C->getLParenLoc());
7267   Record.AddSourceLocation(C->getBindKindLoc());
7268 }
7269 
7270 void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
7271   VisitOMPClauseWithPreInit(C);
7272   Record.AddStmt(C->getSize());
7273   Record.AddSourceLocation(C->getLParenLoc());
7274 }
7275 
7276 void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
7277   Record.push_back(C->varlist_size());
7278   Record.push_back(C->getNumLoops());
7279   Record.AddSourceLocation(C->getLParenLoc());
7280   Record.push_back(C->getDependenceType());
7281   Record.AddSourceLocation(C->getDependenceLoc());
7282   Record.AddSourceLocation(C->getColonLoc());
7283   for (auto *VE : C->varlists())
7284     Record.AddStmt(VE);
7285   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
7286     Record.AddStmt(C->getLoopData(I));
7287 }
7288 
7289 void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
7290   Record.AddAttributes(C->getAttrs());
7291   Record.AddSourceLocation(C->getBeginLoc());
7292   Record.AddSourceLocation(C->getLParenLoc());
7293   Record.AddSourceLocation(C->getEndLoc());
7294 }
7295 
7296 void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause *C) {}
7297 
7298 void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
7299   writeUInt32(TI->Sets.size());
7300   for (const auto &Set : TI->Sets) {
7301     writeEnum(Set.Kind);
7302     writeUInt32(Set.Selectors.size());
7303     for (const auto &Selector : Set.Selectors) {
7304       writeEnum(Selector.Kind);
7305       writeBool(Selector.ScoreOrCondition);
7306       if (Selector.ScoreOrCondition)
7307         writeExprRef(Selector.ScoreOrCondition);
7308       writeUInt32(Selector.Properties.size());
7309       for (const auto &Property : Selector.Properties)
7310         writeEnum(Property.Kind);
7311     }
7312   }
7313 }
7314 
7315 void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
7316   if (!Data)
7317     return;
7318   writeUInt32(Data->getNumClauses());
7319   writeUInt32(Data->getNumChildren());
7320   writeBool(Data->hasAssociatedStmt());
7321   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
7322     writeOMPClause(Data->getClauses()[I]);
7323   if (Data->hasAssociatedStmt())
7324     AddStmt(Data->getAssociatedStmt());
7325   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
7326     AddStmt(Data->getChildren()[I]);
7327 }
7328