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