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
122using namespace clang;
123using namespace clang::serialization;
124
125template <typename T, typename Allocator>
126static 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
132template <typename T>
133static StringRef bytes(const SmallVectorImpl<T> &v) {
134 return StringRef(reinterpret_cast<const char*>(v.data()),
135 sizeof(T) * v.size());
136}
137
138static std::string bytes(const std::vector<bool> &V) {
139 std::string Str;
140 Str.reserve(res_arg: 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
154static 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
165namespace {
166
167struct AffectingModuleMaps {
168 llvm::DenseSet<FileID> DefinitionFileIDs;
169 llvm::DenseSet<const FileEntry *> DefinitionFiles;
170};
171
172std::optional<AffectingModuleMaps>
173GetAffectingModuleMaps(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(a: LHS, b: 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(KV: {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(x: 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(Module: 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(M: Mod); UniqF.isValid())
222 AssignMostImportant(ModuleMaps[UniqF], Reason);
223
224 for (auto *SubM : Mod->submodules())
225 Q.push(x: 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(x: 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(x: M);
246 }
247
248 // Handle textually-included headers that belong to other modules.
249
250 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
251 HS.getFileMgr().GetUniqueIDMapping(UIDToFiles&: FilesByUID);
252
253 if (FilesByUID.size() > HS.header_file_size())
254 FilesByUID.resize(N: 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(FE: *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: *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(V: FID);
297 if (auto *FE = SM.getFileEntryForID(FID))
298 ModuleFileEntries.insert(V: 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
307class ASTTypeWriter {
308 ASTWriter &Writer;
309 ASTWriter::RecordData Record;
310 ASTRecordWriter BasicWriter;
311
312public:
313 ASTTypeWriter(ASTContext &Context, ASTWriter &Writer)
314 : Writer(Writer), BasicWriter(Context, Writer, Record) {}
315
316 uint64_t write(QualType T) {
317 if (T.hasLocalNonFastQualifiers()) {
318 Qualifiers Qs = T.getLocalQualifiers();
319 BasicWriter.writeQualType(T: T.getLocalUnqualifiedType());
320 BasicWriter.writeQualifiers(value: Qs);
321 return BasicWriter.Emit(Code: TYPE_EXT_QUAL, Abbrev: Writer.getTypeExtQualAbbrev());
322 }
323
324 const Type *typePtr = T.getTypePtr();
325 serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
326 atw.write(node: typePtr);
327 return BasicWriter.Emit(Code: getTypeCodeForTypeClass(id: typePtr->getTypeClass()),
328 /*abbrev*/ Abbrev: 0);
329 }
330};
331
332class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
333 ASTRecordWriter &Record;
334
335 void addSourceLocation(SourceLocation Loc) { Record.AddSourceLocation(Loc); }
336 void addSourceRange(SourceRange Range) { Record.AddSourceRange(Range); }
337
338public:
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
352void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
353 // nothing to do
354}
355
356void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
357 addSourceLocation(Loc: TL.getBuiltinLoc());
358 if (TL.needsExtraLocalData()) {
359 Record.push_back(N: TL.getWrittenTypeSpec());
360 Record.push_back(N: static_cast<uint64_t>(TL.getWrittenSignSpec()));
361 Record.push_back(N: static_cast<uint64_t>(TL.getWrittenWidthSpec()));
362 Record.push_back(N: TL.hasModeAttr());
363 }
364}
365
366void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
367 addSourceLocation(Loc: TL.getNameLoc());
368}
369
370void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
371 addSourceLocation(Loc: TL.getStarLoc());
372}
373
374void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
375 // nothing to do
376}
377
378void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
379 // nothing to do
380}
381
382void TypeLocWriter::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
383 // nothing to do
384}
385
386void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
387 addSourceLocation(Loc: TL.getCaretLoc());
388}
389
390void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
391 addSourceLocation(Loc: TL.getAmpLoc());
392}
393
394void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
395 addSourceLocation(Loc: TL.getAmpAmpLoc());
396}
397
398void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
399 addSourceLocation(Loc: TL.getStarLoc());
400 Record.AddNestedNameSpecifierLoc(NNS: TL.getQualifierLoc());
401}
402
403void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
404 addSourceLocation(Loc: TL.getLBracketLoc());
405 addSourceLocation(Loc: TL.getRBracketLoc());
406 Record.push_back(N: TL.getSizeExpr() ? 1 : 0);
407 if (TL.getSizeExpr())
408 Record.AddStmt(S: TL.getSizeExpr());
409}
410
411void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
412 VisitArrayTypeLoc(TL);
413}
414
415void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
416 VisitArrayTypeLoc(TL);
417}
418
419void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
420 VisitArrayTypeLoc(TL);
421}
422
423void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
424 DependentSizedArrayTypeLoc TL) {
425 VisitArrayTypeLoc(TL);
426}
427
428void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
429 DependentAddressSpaceTypeLoc TL) {
430 addSourceLocation(Loc: TL.getAttrNameLoc());
431 SourceRange range = TL.getAttrOperandParensRange();
432 addSourceLocation(Loc: range.getBegin());
433 addSourceLocation(Loc: range.getEnd());
434 Record.AddStmt(S: TL.getAttrExprOperand());
435}
436
437void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
438 DependentSizedExtVectorTypeLoc TL) {
439 addSourceLocation(Loc: TL.getNameLoc());
440}
441
442void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
443 addSourceLocation(Loc: TL.getNameLoc());
444}
445
446void TypeLocWriter::VisitDependentVectorTypeLoc(
447 DependentVectorTypeLoc TL) {
448 addSourceLocation(Loc: TL.getNameLoc());
449}
450
451void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
452 addSourceLocation(Loc: TL.getNameLoc());
453}
454
455void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
456 addSourceLocation(Loc: TL.getAttrNameLoc());
457 SourceRange range = TL.getAttrOperandParensRange();
458 addSourceLocation(Loc: range.getBegin());
459 addSourceLocation(Loc: range.getEnd());
460 Record.AddStmt(S: TL.getAttrRowOperand());
461 Record.AddStmt(S: TL.getAttrColumnOperand());
462}
463
464void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
465 DependentSizedMatrixTypeLoc TL) {
466 addSourceLocation(Loc: TL.getAttrNameLoc());
467 SourceRange range = TL.getAttrOperandParensRange();
468 addSourceLocation(Loc: range.getBegin());
469 addSourceLocation(Loc: range.getEnd());
470 Record.AddStmt(S: TL.getAttrRowOperand());
471 Record.AddStmt(S: TL.getAttrColumnOperand());
472}
473
474void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
475 addSourceLocation(Loc: TL.getLocalRangeBegin());
476 addSourceLocation(Loc: TL.getLParenLoc());
477 addSourceLocation(Loc: TL.getRParenLoc());
478 addSourceRange(Range: TL.getExceptionSpecRange());
479 addSourceLocation(Loc: TL.getLocalRangeEnd());
480 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
481 Record.AddDeclRef(D: TL.getParam(i));
482}
483
484void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
485 VisitFunctionTypeLoc(TL);
486}
487
488void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
489 VisitFunctionTypeLoc(TL);
490}
491
492void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
493 addSourceLocation(Loc: TL.getNameLoc());
494}
495
496void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) {
497 addSourceLocation(Loc: TL.getNameLoc());
498}
499
500void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
501 addSourceLocation(Loc: TL.getNameLoc());
502}
503
504void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
505 if (TL.getNumProtocols()) {
506 addSourceLocation(Loc: TL.getProtocolLAngleLoc());
507 addSourceLocation(Loc: TL.getProtocolRAngleLoc());
508 }
509 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
510 addSourceLocation(Loc: TL.getProtocolLoc(i));
511}
512
513void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
514 addSourceLocation(Loc: TL.getTypeofLoc());
515 addSourceLocation(Loc: TL.getLParenLoc());
516 addSourceLocation(Loc: TL.getRParenLoc());
517}
518
519void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
520 addSourceLocation(Loc: TL.getTypeofLoc());
521 addSourceLocation(Loc: TL.getLParenLoc());
522 addSourceLocation(Loc: TL.getRParenLoc());
523 Record.AddTypeSourceInfo(TInfo: TL.getUnmodifiedTInfo());
524}
525
526void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
527 addSourceLocation(Loc: TL.getDecltypeLoc());
528 addSourceLocation(Loc: TL.getRParenLoc());
529}
530
531void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
532 addSourceLocation(Loc: TL.getKWLoc());
533 addSourceLocation(Loc: TL.getLParenLoc());
534 addSourceLocation(Loc: TL.getRParenLoc());
535 Record.AddTypeSourceInfo(TInfo: TL.getUnderlyingTInfo());
536}
537
538void ASTRecordWriter::AddConceptReference(const ConceptReference *CR) {
539 assert(CR);
540 AddNestedNameSpecifierLoc(NNS: CR->getNestedNameSpecifierLoc());
541 AddSourceLocation(Loc: CR->getTemplateKWLoc());
542 AddDeclarationNameInfo(NameInfo: CR->getConceptNameInfo());
543 AddDeclRef(D: CR->getFoundDecl());
544 AddDeclRef(D: CR->getNamedConcept());
545 push_back(N: CR->getTemplateArgsAsWritten() != nullptr);
546 if (CR->getTemplateArgsAsWritten())
547 AddASTTemplateArgumentListInfo(ASTTemplArgList: CR->getTemplateArgsAsWritten());
548}
549
550void TypeLocWriter::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
551 addSourceLocation(Loc: TL.getEllipsisLoc());
552}
553
554void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
555 addSourceLocation(Loc: TL.getNameLoc());
556 auto *CR = TL.getConceptReference();
557 Record.push_back(N: TL.isConstrained() && CR);
558 if (TL.isConstrained() && CR)
559 Record.AddConceptReference(CR);
560 Record.push_back(N: TL.isDecltypeAuto());
561 if (TL.isDecltypeAuto())
562 addSourceLocation(Loc: TL.getRParenLoc());
563}
564
565void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
566 DeducedTemplateSpecializationTypeLoc TL) {
567 addSourceLocation(Loc: TL.getTemplateNameLoc());
568}
569
570void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
571 addSourceLocation(Loc: TL.getNameLoc());
572}
573
574void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
575 addSourceLocation(Loc: TL.getNameLoc());
576}
577
578void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
579 Record.AddAttr(A: TL.getAttr());
580}
581
582void TypeLocWriter::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
583 // Nothing to do
584}
585
586void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
587 // Nothing to do.
588}
589
590void TypeLocWriter::VisitHLSLAttributedResourceTypeLoc(
591 HLSLAttributedResourceTypeLoc TL) {
592 // Nothing to do.
593}
594
595void TypeLocWriter::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
596 // Nothing to do.
597}
598
599void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
600 addSourceLocation(Loc: TL.getNameLoc());
601}
602
603void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
604 SubstTemplateTypeParmTypeLoc TL) {
605 addSourceLocation(Loc: TL.getNameLoc());
606}
607
608void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
609 SubstTemplateTypeParmPackTypeLoc TL) {
610 addSourceLocation(Loc: TL.getNameLoc());
611}
612
613void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
614 TemplateSpecializationTypeLoc TL) {
615 addSourceLocation(Loc: TL.getTemplateKeywordLoc());
616 addSourceLocation(Loc: TL.getTemplateNameLoc());
617 addSourceLocation(Loc: TL.getLAngleLoc());
618 addSourceLocation(Loc: TL.getRAngleLoc());
619 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
620 Record.AddTemplateArgumentLocInfo(Kind: TL.getArgLoc(i).getArgument().getKind(),
621 Arg: TL.getArgLoc(i).getLocInfo());
622}
623
624void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
625 addSourceLocation(Loc: TL.getLParenLoc());
626 addSourceLocation(Loc: TL.getRParenLoc());
627}
628
629void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
630 addSourceLocation(Loc: TL.getExpansionLoc());
631}
632
633void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
634 addSourceLocation(Loc: TL.getElaboratedKeywordLoc());
635 Record.AddNestedNameSpecifierLoc(NNS: TL.getQualifierLoc());
636}
637
638void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
639 addSourceLocation(Loc: TL.getNameLoc());
640}
641
642void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
643 addSourceLocation(Loc: TL.getElaboratedKeywordLoc());
644 Record.AddNestedNameSpecifierLoc(NNS: TL.getQualifierLoc());
645 addSourceLocation(Loc: TL.getNameLoc());
646}
647
648void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
649 DependentTemplateSpecializationTypeLoc TL) {
650 addSourceLocation(Loc: TL.getElaboratedKeywordLoc());
651 Record.AddNestedNameSpecifierLoc(NNS: TL.getQualifierLoc());
652 addSourceLocation(Loc: TL.getTemplateKeywordLoc());
653 addSourceLocation(Loc: TL.getTemplateNameLoc());
654 addSourceLocation(Loc: TL.getLAngleLoc());
655 addSourceLocation(Loc: TL.getRAngleLoc());
656 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
657 Record.AddTemplateArgumentLocInfo(Kind: TL.getArgLoc(i: I).getArgument().getKind(),
658 Arg: TL.getArgLoc(i: I).getLocInfo());
659}
660
661void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
662 addSourceLocation(Loc: TL.getEllipsisLoc());
663}
664
665void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
666 addSourceLocation(Loc: TL.getNameLoc());
667 addSourceLocation(Loc: TL.getNameEndLoc());
668}
669
670void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
671 Record.push_back(N: TL.hasBaseTypeAsWritten());
672 addSourceLocation(Loc: TL.getTypeArgsLAngleLoc());
673 addSourceLocation(Loc: TL.getTypeArgsRAngleLoc());
674 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
675 Record.AddTypeSourceInfo(TInfo: TL.getTypeArgTInfo(i));
676 addSourceLocation(Loc: TL.getProtocolLAngleLoc());
677 addSourceLocation(Loc: TL.getProtocolRAngleLoc());
678 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
679 addSourceLocation(Loc: TL.getProtocolLoc(i));
680}
681
682void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
683 addSourceLocation(Loc: TL.getStarLoc());
684}
685
686void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
687 addSourceLocation(Loc: TL.getKWLoc());
688 addSourceLocation(Loc: TL.getLParenLoc());
689 addSourceLocation(Loc: TL.getRParenLoc());
690}
691
692void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
693 addSourceLocation(Loc: TL.getKWLoc());
694}
695
696void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
697 addSourceLocation(Loc: TL.getNameLoc());
698}
699void TypeLocWriter::VisitDependentBitIntTypeLoc(
700 clang::DependentBitIntTypeLoc TL) {
701 addSourceLocation(Loc: TL.getNameLoc());
702}
703
704void 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(OpInfo: BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
712 Abv->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
713 Abv->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
714 TypeExtQualAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
715}
716
717//===----------------------------------------------------------------------===//
718// ASTWriter Implementation
719//===----------------------------------------------------------------------===//
720
721static void EmitBlockID(unsigned ID, const char *Name,
722 llvm::BitstreamWriter &Stream,
723 ASTWriter::RecordDataImpl &Record) {
724 Record.clear();
725 Record.push_back(Elt: ID);
726 Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_SETBID, Vals: 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(Elt: *Name++);
734 Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Vals: Record);
735}
736
737static void EmitRecordID(unsigned ID, const char *Name,
738 llvm::BitstreamWriter &Stream,
739 ASTWriter::RecordDataImpl &Record) {
740 Record.clear();
741 Record.push_back(Elt: ID);
742 while (*Name)
743 Record.push_back(Elt: *Name++);
744 Stream.EmitRecord(Code: llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Vals: Record);
745}
746
747static 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
881void 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.
1164static bool cleanPathForOutput(FileManager &FileMgr,
1165 SmallVectorImpl<char> &Path) {
1166 bool Changed = FileMgr.makeAbsolutePath(Path);
1167 return Changed | llvm::sys::path::remove_dots(path&: 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).
1180static const char *
1181adjustFilenameForRelocatableAST(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(value: Filename[Pos])) {
1200 if (!llvm::sys::path::is_separator(value: 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
1217std::pair<ASTFileSignature, ASTFileSignature>
1218ASTWriter::createSignature() const {
1219 StringRef AllBytes(Buffer.data(), Buffer.size());
1220
1221 llvm::SHA1 Hasher;
1222 Hasher.update(Str: AllBytes.slice(Start: ASTBlockRange.first, End: ASTBlockRange.second));
1223 ASTFileSignature ASTBlockHash = ASTFileSignature::create(Bytes: Hasher.result());
1224
1225 // Add the remaining bytes:
1226 // 1. Before the unhashed control block.
1227 Hasher.update(Str: AllBytes.slice(Start: 0, End: UnhashedControlBlockRange.first));
1228 // 2. Between the unhashed control block and the AST block.
1229 Hasher.update(
1230 Str: AllBytes.slice(Start: UnhashedControlBlockRange.second, End: ASTBlockRange.first));
1231 // 3. After the AST block.
1232 Hasher.update(Str: AllBytes.substr(Start: ASTBlockRange.second));
1233 ASTFileSignature Signature = ASTFileSignature::create(Bytes: Hasher.result());
1234
1235 return std::make_pair(x&: ASTBlockHash, y&: Signature);
1236}
1237
1238ASTFileSignature ASTWriter::createSignatureForNamedModule() const {
1239 llvm::SHA1 Hasher;
1240 Hasher.update(Str: 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(Data: 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(Data: M->Signature);
1274
1275 return ASTFileSignature::create(Bytes: Hasher.result());
1276}
1277
1278static void BackpatchSignatureAt(llvm::BitstreamWriter &Stream,
1279 const ASTFileSignature &S, uint64_t BitNo) {
1280 for (uint8_t Byte : S) {
1281 Stream.BackpatchByte(BitNo, NewByte: Byte);
1282 BitNo += 8;
1283 }
1284}
1285
1286ASTFileSignature ASTWriter::backpatchSignature() {
1287 if (isWritingStdCXXNamedModules()) {
1288 ASTFileSignature Signature = createSignatureForNamedModule();
1289 BackpatchSignatureAt(Stream, S: Signature, BitNo: 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(args&: ASTBlockHash, args&: Signature) = createSignature();
1301
1302 BackpatchSignatureAt(Stream, S: ASTBlockHash, BitNo: ASTBlockHashOffset);
1303 BackpatchSignatureAt(Stream, S: Signature, BitNo: SignatureOffset);
1304
1305 return Signature;
1306}
1307
1308void 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(BlockID: UNHASHED_CONTROL_BLOCK_ID, CodeLen: 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(OpInfo: BitCodeAbbrevOp(AST_BLOCK_HASH));
1338 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1339 unsigned ASTBlockHashAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1340
1341 Record.push_back(Elt: AST_BLOCK_HASH);
1342 Stream.EmitRecordWithBlob(Abbrev: ASTBlockHashAbbrev, Vals: Record, Blob);
1343 ASTBlockHashOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1344 Record.clear();
1345 }
1346
1347 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1348 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SIGNATURE));
1349 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1350 unsigned SignatureAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1351
1352 Record.push_back(Elt: SIGNATURE);
1353 Stream.EmitRecordWithBlob(Abbrev: SignatureAbbrev, Vals: 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(Elt: DiagOpts.Warnings.size());
1369 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1370 AddString(Str: DiagOpts.Warnings[I], Record);
1371 Record.push_back(Elt: DiagOpts.Remarks.size());
1372 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1373 AddString(Str: 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(Code: DIAGNOSTIC_OPTIONS, Vals: Record);
1377 Record.clear();
1378 }
1379
1380 // Header search paths.
1381 if (!HSOpts.ModulesSkipHeaderSearchPaths) {
1382 // Include entries.
1383 Record.push_back(Elt: 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(Str: Entry.Path, Record);
1387 Record.push_back(Elt: static_cast<unsigned>(Entry.Group));
1388 Record.push_back(Elt: Entry.IsFramework);
1389 Record.push_back(Elt: Entry.IgnoreSysRoot);
1390 }
1391
1392 // System header prefixes.
1393 Record.push_back(Elt: HSOpts.SystemHeaderPrefixes.size());
1394 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1395 AddString(Str: HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1396 Record.push_back(Elt: HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1397 }
1398
1399 // VFS overlay files.
1400 Record.push_back(Elt: HSOpts.VFSOverlayFiles.size());
1401 for (StringRef VFSOverlayFile : HSOpts.VFSOverlayFiles)
1402 AddString(Str: VFSOverlayFile, Record);
1403
1404 Stream.EmitRecord(Code: HEADER_SEARCH_PATHS, Vals: Record);
1405 }
1406
1407 if (!HSOpts.ModulesSkipPragmaDiagnosticMappings)
1408 WritePragmaDiagnosticMappings(Diag: 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(OpInfo: BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1415 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1416 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1417 unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1418 RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1419 HSEntryUsage.size()};
1420 Stream.EmitRecordWithBlob(Abbrev: HSUsageAbbrevCode, Vals: Record, Blob: bytes(V: HSEntryUsage));
1421 }
1422
1423 // VFS usage.
1424 {
1425 auto VFSUsage = PP.getHeaderSearchInfo().collectVFSUsageAndClear();
1426 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1427 Abbrev->Add(OpInfo: BitCodeAbbrevOp(VFS_USAGE));
1428 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1429 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1430 unsigned VFSUsageAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1431 RecordData::value_type Record[] = {VFS_USAGE, VFSUsage.size()};
1432 Stream.EmitRecordWithBlob(Abbrev: VFSUsageAbbrevCode, Vals: Record, Blob: bytes(V: VFSUsage));
1433 }
1434
1435 // Leave the options block.
1436 Stream.ExitBlock();
1437 UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
1438}
1439
1440/// Write the control block.
1441void 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(BlockID: CONTROL_BLOCK_ID, CodeLen: 5);
1448 RecordData Record;
1449
1450 // Metadata
1451 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1452 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(METADATA));
1453 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1454 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1455 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1456 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1457 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1458 // Standard C++ module
1459 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1460 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1461 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1462 MetadataAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1463 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(Abbv: 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(Abbrev: MetadataAbbrevCode, Vals: Record,
1477 Blob: getClangFullRepositoryVersion());
1478 }
1479
1480 if (WritingModule) {
1481 // Module name
1482 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1483 Abbrev->Add(OpInfo: BitCodeAbbrevOp(MODULE_NAME));
1484 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1485 unsigned AbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1486 RecordData::value_type Record[] = {MODULE_NAME};
1487 Stream.EmitRecordWithBlob(Abbrev: AbbrevCode, Vals: Record, Blob: 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(DirName: ".");
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, Path&: *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(OpInfo: BitCodeAbbrevOp(MODULE_DIRECTORY));
1514 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1515 unsigned AbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1516
1517 RecordData::value_type Record[] = {MODULE_DIRECTORY};
1518 Stream.EmitRecordWithBlob(Abbrev: AbbrevCode, Vals: Record, Blob: *BaseDir);
1519 }
1520
1521 // Write out all other paths relative to the base directory if possible.
1522 BaseDirectory.assign(first: BaseDir->begin(), last: 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(Path: WritingModule->PresumedModuleMapFile.empty()
1535 ? Map.getModuleMapFileForUniquing(M: WritingModule)
1536 ->getNameAsRequested()
1537 : StringRef(WritingModule->PresumedModuleMapFile),
1538 Record);
1539
1540 // Additional module map files.
1541 if (auto *AdditionalModMaps =
1542 Map.getAdditionalModuleMapFiles(M: WritingModule)) {
1543 Record.push_back(Elt: AdditionalModMaps->size());
1544 SmallVector<FileEntryRef, 1> ModMaps(AdditionalModMaps->begin(),
1545 AdditionalModMaps->end());
1546 llvm::sort(C&: ModMaps, Comp: [](FileEntryRef A, FileEntryRef B) {
1547 return A.getName() < B.getName();
1548 });
1549 for (FileEntryRef F : ModMaps)
1550 AddPath(Path: F.getName(), Record);
1551 } else {
1552 Record.push_back(Elt: 0);
1553 }
1554
1555 Stream.EmitRecord(Code: MODULE_MAP_FILE, Vals: Record);
1556 }
1557
1558 // Imports
1559 if (Chain) {
1560 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1561 Abbrev->Add(OpInfo: BitCodeAbbrevOp(IMPORT));
1562 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Kind
1563 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ImportLoc
1564 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Module name len
1565 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Standard C++ mod
1566 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File size
1567 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File timestamp
1568 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File name len
1569 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Strings
1570 unsigned AbbrevCode = Stream.EmitAbbrev(Abbv: 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(Elt: IMPORT);
1583 Record.push_back(Elt: (unsigned)M.Kind); // FIXME: Stable encoding
1584 AddSourceLocation(Loc: M.ImportLoc, Record);
1585 AddStringBlob(Str: M.ModuleName, Record, Blob);
1586 Record.push_back(Elt: 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(Elt: 0);
1592 Record.push_back(Elt: 0);
1593 Record.push_back(Elt: 0);
1594 } else {
1595 // If we have calculated signature, there is no need to store
1596 // the size or timestamp.
1597 Record.push_back(Elt: M.Signature ? 0 : M.File.getSize());
1598 Record.push_back(Elt: M.Signature ? 0 : getTimestampForOutput(E: M.File));
1599
1600 llvm::append_range(C&: Blob, R&: M.Signature);
1601
1602 AddPathBlob(Str: M.FileName, Record, Blob);
1603 }
1604
1605 Stream.EmitRecordWithBlob(Abbrev: AbbrevCode, Vals: Record, Blob);
1606 }
1607 }
1608
1609 // Write the options block.
1610 Stream.EnterSubblock(BlockID: OPTIONS_BLOCK_ID, CodeLen: 4);
1611
1612 // Language options.
1613 Record.clear();
1614 const LangOptions &LangOpts = PP.getLangOpts();
1615#define LANGOPT(Name, Bits, Default, Description) \
1616 Record.push_back(LangOpts.Name);
1617#define ENUM_LANGOPT(Name, Type, Bits, Default, 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(Elt: LangOpts.ModuleFeatures.size());
1625 for (StringRef Feature : LangOpts.ModuleFeatures)
1626 AddString(Str: Feature, Record);
1627
1628 Record.push_back(Elt: (unsigned) LangOpts.ObjCRuntime.getKind());
1629 AddVersionTuple(Version: LangOpts.ObjCRuntime.getVersion(), Record);
1630
1631 AddString(Str: LangOpts.CurrentModule, Record);
1632
1633 // Comment options.
1634 Record.push_back(Elt: LangOpts.CommentOpts.BlockCommandNames.size());
1635 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1636 AddString(Str: I, Record);
1637 }
1638 Record.push_back(Elt: LangOpts.CommentOpts.ParseAllComments);
1639
1640 // OpenMP offloading options.
1641 Record.push_back(Elt: LangOpts.OMPTargetTriples.size());
1642 for (auto &T : LangOpts.OMPTargetTriples)
1643 AddString(Str: T.getTriple(), Record);
1644
1645 AddString(Str: LangOpts.OMPHostIRFile, Record);
1646
1647 Stream.EmitRecord(Code: LANGUAGE_OPTIONS, Vals: Record);
1648
1649 // Target options.
1650 Record.clear();
1651 const TargetInfo &Target = PP.getTargetInfo();
1652 const TargetOptions &TargetOpts = Target.getTargetOpts();
1653 AddString(Str: TargetOpts.Triple, Record);
1654 AddString(Str: TargetOpts.CPU, Record);
1655 AddString(Str: TargetOpts.TuneCPU, Record);
1656 AddString(Str: TargetOpts.ABI, Record);
1657 Record.push_back(Elt: TargetOpts.FeaturesAsWritten.size());
1658 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1659 AddString(Str: TargetOpts.FeaturesAsWritten[I], Record);
1660 }
1661 Record.push_back(Elt: TargetOpts.Features.size());
1662 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1663 AddString(Str: TargetOpts.Features[I], Record);
1664 }
1665 Stream.EmitRecord(Code: TARGET_OPTIONS, Vals: Record);
1666
1667 // File system options.
1668 Record.clear();
1669 const FileSystemOptions &FSOpts = FileMgr.getFileSystemOpts();
1670 AddString(Str: FSOpts.WorkingDir, Record);
1671 Stream.EmitRecord(Code: FILE_SYSTEM_OPTIONS, Vals: Record);
1672
1673 // Header search options.
1674 Record.clear();
1675 const HeaderSearchOptions &HSOpts =
1676 PP.getHeaderSearchInfo().getHeaderSearchOpts();
1677
1678 AddString(Str: HSOpts.Sysroot, Record);
1679 AddString(Str: HSOpts.ResourceDir, Record);
1680 AddString(Str: HSOpts.ModuleCachePath, Record);
1681 AddString(Str: HSOpts.ModuleUserBuildPath, Record);
1682 Record.push_back(Elt: HSOpts.DisableModuleHash);
1683 Record.push_back(Elt: HSOpts.ImplicitModuleMaps);
1684 Record.push_back(Elt: HSOpts.ModuleMapFileHomeIsCwd);
1685 Record.push_back(Elt: HSOpts.EnablePrebuiltImplicitModules);
1686 Record.push_back(Elt: HSOpts.UseBuiltinIncludes);
1687 Record.push_back(Elt: HSOpts.UseStandardSystemIncludes);
1688 Record.push_back(Elt: HSOpts.UseStandardCXXIncludes);
1689 Record.push_back(Elt: HSOpts.UseLibcxx);
1690 // Write out the specific module cache path that contains the module files.
1691 AddString(Str: PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1692 Stream.EmitRecord(Code: HEADER_SEARCH_OPTIONS, Vals: 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(Elt: WriteMacros);
1704 if (WriteMacros) {
1705 // Macro definitions.
1706 Record.push_back(Elt: PPOpts.Macros.size());
1707 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1708 AddString(Str: PPOpts.Macros[I].first, Record);
1709 Record.push_back(Elt: PPOpts.Macros[I].second);
1710 }
1711 }
1712
1713 // Includes
1714 Record.push_back(Elt: PPOpts.Includes.size());
1715 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1716 AddString(Str: PPOpts.Includes[I], Record);
1717
1718 // Macro includes
1719 Record.push_back(Elt: PPOpts.MacroIncludes.size());
1720 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1721 AddString(Str: PPOpts.MacroIncludes[I], Record);
1722
1723 Record.push_back(Elt: PPOpts.UsePredefines);
1724 // Detailed record is important since it is used for the module cache hash.
1725 Record.push_back(Elt: PPOpts.DetailedRecord);
1726 AddString(Str: PPOpts.ImplicitPCHInclude, Record);
1727 Record.push_back(Elt: static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1728 Stream.EmitRecord(Code: PREPROCESSOR_OPTIONS, Vals: 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(FID: SourceMgr.getMainFileID())) {
1736 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1737 FileAbbrev->Add(OpInfo: BitCodeAbbrevOp(ORIGINAL_FILE));
1738 FileAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1739 FileAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1740 unsigned FileAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(FileAbbrev));
1741
1742 Record.clear();
1743 Record.push_back(Elt: ORIGINAL_FILE);
1744 AddFileID(FID: SourceMgr.getMainFileID(), Record);
1745 EmitRecordWithPath(Abbrev: FileAbbrevCode, Record, Path: MainFile->getName());
1746 }
1747
1748 Record.clear();
1749 AddFileID(FID: SourceMgr.getMainFileID(), Record);
1750 Stream.EmitRecord(Code: ORIGINAL_FILE_ID, Vals: Record);
1751
1752 WriteInputFiles(SourceMgr);
1753 Stream.ExitBlock();
1754}
1755
1756namespace {
1757
1758/// An input file.
1759struct 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
1768 InputFileEntry(FileEntryRef File) : File(File) {}
1769
1770 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(Loc: SourceLocation(), DiagID: diag::err_module_unable_to_hash_content)
1784 << File.getName();
1785 return;
1786 }
1787
1788 uint64_t Hash = xxh3_64bits(data: MemBuff->getBuffer());
1789 ContentHash[0] = uint32_t(Hash);
1790 ContentHash[1] = uint32_t(Hash >> 32);
1791 }
1792};
1793
1794} // namespace
1795
1796SourceLocation ASTWriter::getAffectingIncludeLoc(const SourceManager &SourceMgr,
1797 const SrcMgr::FileInfo &File) {
1798 SourceLocation IncludeLoc = File.getIncludeLoc();
1799 if (IncludeLoc.isValid()) {
1800 FileID IncludeFID = SourceMgr.getFileID(SpellingLoc: IncludeLoc);
1801 assert(IncludeFID.isValid() && "IncludeLoc in invalid file");
1802 if (!IsSLocAffecting[IncludeFID.ID])
1803 IncludeLoc = SourceLocation();
1804 }
1805 return IncludeLoc;
1806}
1807
1808void ASTWriter::WriteInputFiles(SourceManager &SourceMgr) {
1809 using namespace llvm;
1810
1811 Stream.EnterSubblock(BlockID: INPUT_FILES_BLOCK_ID, CodeLen: 4);
1812
1813 // Create input-file abbreviation.
1814 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1815 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(INPUT_FILE));
1816 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1817 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1818 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1819 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1820 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1821 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1822 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1823 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1824 IFAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
1825 unsigned IFAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(IFAbbrev));
1826
1827 // Create input file hash abbreviation.
1828 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1829 IFHAbbrev->Add(OpInfo: BitCodeAbbrevOp(INPUT_FILE_HASH));
1830 IFHAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1831 IFHAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1832 unsigned IFHAbbrevCode = Stream.EmitAbbrev(Abbv: 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(Index: 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(CK: File.getFileCharacteristic());
1858 Entry.IsTransient = Cache->IsTransient;
1859 Entry.BufferOverridden = Cache->BufferOverridden;
1860
1861 FileID IncludeFileID = SourceMgr.getFileID(SpellingLoc: File.getIncludeLoc());
1862 Entry.IsTopLevel = IncludeFileID.isInvalid() || IncludeFileID.ID < 0 ||
1863 !IsSLocFileEntryAffecting[IncludeFileID.ID];
1864 Entry.IsModuleMap = isModuleMap(CK: File.getFileCharacteristic());
1865
1866 Entry.trySetContentHash(PP&: *PP, GetMemBuff: [&] { return Cache->getBufferIfLoaded(); });
1867
1868 if (Entry.IsSystemFile)
1869 SystemFiles.push_back(x: Entry);
1870 else
1871 UserFiles.push_back(x: 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(path&: SDKSettingsJSON, a: "SDKSettings.json");
1881 FileManager &FM = PP->getFileManager();
1882 if (auto FE = FM.getOptionalFileRef(Filename: 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&: *PP, GetMemBuff: [&]() -> std::optional<MemoryBufferRef> {
1891 if (auto MBOrErr = FM.getBufferForFile(Entry: Entry.File)) {
1892 MB = std::move(*MBOrErr);
1893 return MB->getMemBufferRef();
1894 }
1895 return std::nullopt;
1896 });
1897 SystemFiles.push_back(x: Entry);
1898 }
1899 }
1900
1901 // User files go at the front, system files at the back.
1902 auto SortedFiles = llvm::concat<InputFileEntry>(Ranges: std::move(UserFiles),
1903 Ranges: 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(x: 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(Path&: NameAsRequested);
1928 PreparePathForOutput(Path&: 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(E: Entry.File),
1938 Entry.BufferOverridden,
1939 Entry.IsTransient,
1940 Entry.IsTopLevel,
1941 Entry.IsModuleMap,
1942 NameAsRequested.size()};
1943
1944 Stream.EmitRecordWithBlob(Abbrev: IFAbbrevCode, Vals: Record,
1945 Blob: (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(Abbrev: IFHAbbrevCode, Vals: Record);
1953 }
1954 }
1955
1956 Stream.ExitBlock();
1957
1958 // Create input file offsets abbreviation.
1959 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1960 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1961 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1962 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1963 // input files
1964 OffsetsAbbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1965 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(Abbv: std::move(OffsetsAbbrev));
1966
1967 // Write input file offsets.
1968 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1969 InputFileOffsets.size(), UserFilesNum};
1970 Stream.EmitRecordWithBlob(Abbrev: OffsetsAbbrevCode, Vals: Record, Blob: bytes(v: InputFileOffsets));
1971}
1972
1973//===----------------------------------------------------------------------===//
1974// Source Manager Serialization
1975//===----------------------------------------------------------------------===//
1976
1977/// Create an abbreviation for the SLocEntry that refers to a
1978/// file.
1979static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1980 using namespace llvm;
1981
1982 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1983 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1984 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1985 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1986 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1987 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1988 // FileEntry fields.
1989 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1990 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1991 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1992 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1993 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
1994}
1995
1996/// Create an abbreviation for the SLocEntry that refers to a
1997/// buffer.
1998static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1999 using namespace llvm;
2000
2001 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2002 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
2003 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
2004 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
2005 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
2006 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
2007 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
2008 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2009}
2010
2011/// Create an abbreviation for the SLocEntry that refers to a
2012/// buffer's blob.
2013static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
2014 bool Compressed) {
2015 using namespace llvm;
2016
2017 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2018 Abbrev->Add(OpInfo: BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
2019 : SM_SLOC_BUFFER_BLOB));
2020 if (Compressed)
2021 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
2022 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
2023 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2024}
2025
2026/// Create an abbreviation for the SLocEntry that refers to a macro
2027/// expansion.
2028static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
2029 using namespace llvm;
2030
2031 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2032 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
2033 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
2034 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
2035 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
2036 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
2037 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
2038 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
2039 return Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2040}
2041
2042/// Emit key length and data length as ULEB-encoded data, and return them as a
2043/// pair.
2044static std::pair<unsigned, unsigned>
2045emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
2046 llvm::encodeULEB128(Value: KeyLen, OS&: Out);
2047 llvm::encodeULEB128(Value: DataLen, OS&: Out);
2048 return std::make_pair(x&: KeyLen, y&: DataLen);
2049}
2050
2051namespace {
2052
2053 // Trait used for the on-disk hash table of header search information.
2054 class HeaderFileInfoTrait {
2055 ASTWriter &Writer;
2056
2057 public:
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 {
2071 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
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(dest: buf, src: &key.Size, n: sizeof(key.Size));
2093 memcpy(dest: buf + sizeof(key.Size), src: &key.ModTime, n: sizeof(key.ModTime));
2094 return llvm::xxh3_64bits(data: buf);
2095 }
2096
2097 std::pair<unsigned, unsigned>
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(Mod: ModInfo.getModule()))
2103 DataLen += 4;
2104 if (Data.Unresolved.getPointer())
2105 DataLen += 4;
2106 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
2107 }
2108
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>(Val: key.Size);
2114 KeyLen -= 8;
2115 LE.write<uint64_t>(Val: key.ModTime);
2116 KeyLen -= 8;
2117 Out.write(Ptr: key.Filename.data(), Size: KeyLen);
2118 }
2119
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>(Val: Flags);
2133
2134 if (Data.HFI.LazyControllingMacro.isID())
2135 LE.write<IdentifierID>(Val: Data.HFI.LazyControllingMacro.getID());
2136 else
2137 LE.write<IdentifierID>(
2138 Val: Writer.getIdentifierRef(II: Data.HFI.LazyControllingMacro.getPtr()));
2139
2140 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
2141 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(Mod: M)) {
2142 uint32_t Value = (ModID << 3) | (unsigned)Role;
2143 assert((Value >> 3) == ModID && "overflow in header module info");
2144 LE.write<uint32_t>(Val: 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.
2162void 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(Mod: 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(Loc: U.FileNameLoc, DiagID: 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(path&: Filename, a: U.FileName);
2203 PreparePathForOutput(Path&: Filename);
2204
2205 StringRef FilenameDup = strdup(s: Filename.c_str());
2206 SavedStrings.push_back(Elt: FilenameDup.data());
2207
2208 HeaderFileInfoTrait::key_type Key = {
2209 .Filename: FilenameDup, .Size: *U.Size, .ModTime: IncludeTimestamps ? *U.ModTime : 0};
2210 HeaderFileInfoTrait::data_type Data = {
2211 Empty, false, {}, {M, ModuleMap::headerKindToRole(Kind: 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, InfoObj&: GeneratorTrait);
2215 ++NumHeaderSearchEntries;
2216 }
2217 auto SubmodulesRange = M->submodules();
2218 Worklist.append(in_start: SubmodulesRange.begin(), in_end: SubmodulesRange.end());
2219 }
2220 }
2221
2222 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
2223 HS.getFileMgr().GetUniqueIDMapping(UIDToFiles&: FilesByUID);
2224
2225 if (FilesByUID.size() > HS.header_file_size())
2226 FilesByUID.resize(N: 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(FE: *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(Path&: 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(s: FilenameTmp.c_str()));
2248 SavedStrings.push_back(Elt: Filename.data());
2249 }
2250
2251 bool Included = HFI->IsLocallyIncluded || PP->alreadyIncluded(File: *File);
2252
2253 HeaderFileInfoTrait::key_type Key = {
2254 .Filename: Filename, .Size: File->getSize(), .ModTime: getTimestampForOutput(E: *File)
2255 };
2256 HeaderFileInfoTrait::data_type Data = {
2257 *HFI, Included, HS.getModuleMap().findResolvedModulesForHeader(File: *File), {}
2258 };
2259 Generator.insert(Key, Data, InfoObj&: 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>(os&: Out, value: 0, endian: llvm::endianness::little);
2272 BucketOffset = Generator.Emit(Out, InfoObj&: GeneratorTrait);
2273 }
2274
2275 // Create a blob abbreviation
2276 using namespace llvm;
2277
2278 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2279 Abbrev->Add(OpInfo: BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2280 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2281 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2282 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2283 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2284 unsigned TableAbbrev = Stream.EmitAbbrev(Abbv: 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(Abbrev: TableAbbrev, Vals: Record, Blob: 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(ptr: const_cast<char *>(SavedStrings[I]));
2294}
2295
2296static 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 Input: llvm::arrayRefFromStringRef(Input: Blob.drop_back(N: 1)), CompressedBuffer, Level: 9);
2307 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2308 Stream.EmitRecordWithBlob(Abbrev: SLocBufferBlobCompressedAbbrv, Vals: Record,
2309 Blob: llvm::toStringRef(Input: CompressedBuffer));
2310 return;
2311 }
2312 if (llvm::compression::zlib::isAvailable()) {
2313 llvm::compression::zlib::compress(
2314 Input: llvm::arrayRefFromStringRef(Input: Blob.drop_back(N: 1)), CompressedBuffer);
2315 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2316 Stream.EmitRecordWithBlob(Abbrev: SLocBufferBlobCompressedAbbrv, Vals: Record,
2317 Blob: llvm::toStringRef(Input: CompressedBuffer));
2318 return;
2319 }
2320
2321 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2322 Stream.EmitRecordWithBlob(Abbrev: SLocBufferBlobAbbrv, Vals: 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.
2333void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
2334 RecordData Record;
2335
2336 // Enter the source manager block.
2337 Stream.EnterSubblock(BlockID: SOURCE_MANAGER_BLOCK_ID, CodeLen: 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, Compressed: false);
2344 unsigned SLocBufferBlobCompressedAbbrv =
2345 CreateSLocBufferBlobAbbrev(Stream, Compressed: 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(n: 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(Index: I);
2357 FileID FID = FileID::get(V: 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(Elt: 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(x: Offset);
2384 // Starting offset of this entry within this module, so skip the dummy.
2385 Record.push_back(Elt: getAdjustedOffset(Offset: SLoc->getOffset()) - 2);
2386 AddSourceLocation(Loc: getAffectingIncludeLoc(SourceMgr, File), Record);
2387 Record.push_back(Elt: File.getFileCharacteristic()); // FIXME: stable encoding
2388 Record.push_back(Elt: 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(Elt: InputFileIDs[*Content->OrigEntry]);
2398
2399 Record.push_back(Elt: getAdjustedNumCreatedFIDs(FID));
2400
2401 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(Val: FID);
2402 if (FDI != FileDeclIDs.end()) {
2403 Record.push_back(Elt: FDI->second->FirstDeclIndex);
2404 Record.push_back(Elt: FDI->second->DeclIDs.size());
2405 } else {
2406 Record.push_back(Elt: 0);
2407 Record.push_back(Elt: 0);
2408 }
2409
2410 Stream.EmitRecordWithAbbrev(Abbrev: SLocFileAbbrv, Vals: 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 Diag&: SourceMgr.getDiagnostics(), FM&: SourceMgr.getFileManager());
2423 StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2424 Stream.EmitRecordWithBlob(Abbrev: SLocBufferAbbrv, Vals: Record,
2425 Blob: 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 Diag&: SourceMgr.getDiagnostics(), FM&: 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(x: Offset);
2444 // Starting offset of this entry within this module, so skip the dummy.
2445 Record.push_back(Elt: getAdjustedOffset(Offset: SLoc->getOffset()) - 2);
2446 AddSourceLocation(Loc: Expansion.getSpellingLoc(), Record);
2447 AddSourceLocation(Loc: Expansion.getExpansionLocStart(), Record);
2448 AddSourceLocation(Loc: Expansion.isMacroArgExpansion()
2449 ? SourceLocation()
2450 : Expansion.getExpansionLocEnd(),
2451 Record);
2452 Record.push_back(Elt: 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(Index: I + 1).getOffset();
2458 Record.push_back(Elt: getAdjustedOffset(Offset: NextOffset - SLoc->getOffset()) - 1);
2459 Stream.EmitRecordWithAbbrev(Abbrev: SLocExpansionAbbrv, Vals: 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(OpInfo: BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2474 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2475 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2476 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2477 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2478 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2479 {
2480 RecordData::value_type Record[] = {
2481 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2482 getAdjustedOffset(Offset: SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2483 SLocEntryOffsetsBase - SourceManagerBlockOffset};
2484 Stream.EmitRecordWithBlob(Abbrev: SLocOffsetsAbbrev, Vals: Record,
2485 Blob: bytes(v: 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(KV: std::make_pair(x: LE.FilenameID,
2503 y: FilenameMap.size() - 1)).second)
2504 AddPath(Path: LineTable.getFilename(ID: LE.FilenameID), Record);
2505 }
2506 }
2507 Record.push_back(Elt: 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(FID: L.first, Record);
2516
2517 // Emit the line entries
2518 Record.push_back(Elt: L.second.size());
2519 for (const auto &LE : L.second) {
2520 Record.push_back(Elt: LE.FileOffset);
2521 Record.push_back(Elt: LE.LineNo);
2522 Record.push_back(Elt: FilenameMap[LE.FilenameID]);
2523 Record.push_back(Elt: (unsigned)LE.FileKind);
2524 Record.push_back(Elt: LE.IncludeOffset);
2525 }
2526 }
2527
2528 Stream.EmitRecord(Code: SOURCE_MANAGER_LINE_TABLE, Vals: Record);
2529 }
2530}
2531
2532//===----------------------------------------------------------------------===//
2533// Preprocessor Serialization
2534//===----------------------------------------------------------------------===//
2535
2536static 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(SpellingLoc: 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.
2555void 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&: *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(Code: PP_COUNTER_VALUE, Vals: 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(Loc: AssumeNonNullLoc, Record);
2578 Stream.EmitRecord(Code: PP_ASSUME_NONNULL_LOC, Vals: 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(Elt: true);
2587 AddSourceLocation(Loc: SkipInfo->HashTokenLoc, Record);
2588 AddSourceLocation(Loc: SkipInfo->IfTokenLoc, Record);
2589 Record.push_back(Elt: SkipInfo->FoundNonSkipPortion);
2590 Record.push_back(Elt: SkipInfo->FoundElse);
2591 AddSourceLocation(Loc: SkipInfo->ElseLoc, Record);
2592 } else {
2593 Record.push_back(Elt: false);
2594 }
2595 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2596 AddSourceLocation(Loc: Cond.IfLoc, Record);
2597 Record.push_back(Elt: Cond.WasSkipping);
2598 Record.push_back(Elt: Cond.FoundNonSkip);
2599 Record.push_back(Elt: Cond.FoundElse);
2600 }
2601 Stream.EmitRecord(Code: PP_CONDITIONAL_STACK, Vals: 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(Loc: S, Record);
2608 Stream.EmitRecord(Code: PP_UNSAFE_BUFFER_USAGE, Vals: Record);
2609 Record.clear();
2610
2611 // Enter the preprocessor block.
2612 Stream.EnterSubblock(BlockID: PREPROCESSOR_BLOCK_ID, CodeLen: 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(Loc: SourceLocation(), DiagID: 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(Elt: 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(C&: MacroIdentifiers, Comp: 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(II: 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(Loc: MD->getLocation(), Record);
2661 Record.push_back(Elt: MD->getKind());
2662 if (auto *DefMD = dyn_cast<DefMacroDirective>(Val: MD)) {
2663 Record.push_back(Elt: getMacroRef(MI: DefMD->getInfo(), Name));
2664 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(Val: MD)) {
2665 Record.push_back(Elt: VisMD->isPublic());
2666 }
2667 ModuleMacroRecord.push_back(Elt: getSubmoduleID(Mod: WritingModule));
2668 ModuleMacroRecord.push_back(Elt: getMacroRef(MI: MD->getMacroInfo(), Name));
2669 Stream.EmitRecord(Code: PP_MODULE_MACRO, Vals: 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(Loc: MD->getLocation(), Record);
2680 Record.push_back(Elt: MD->getKind());
2681 if (auto *DefMD = dyn_cast<DefMacroDirective>(Val: MD)) {
2682 Record.push_back(Elt: getMacroRef(MI: DefMD->getInfo(), Name));
2683 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(Val: MD)) {
2684 Record.push_back(Elt: VisMD->isPublic());
2685 }
2686 }
2687
2688 // We write out exported module macros for PCH as well.
2689 auto Leafs = PP.getLeafModuleMacros(II: 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(Elt: getSubmoduleID(Mod: Macro->getOwningModule()));
2697 ModuleMacroRecord.push_back(Elt: getMacroRef(MI: Macro->getMacroInfo(), Name));
2698 for (auto *M : Macro->overrides())
2699 ModuleMacroRecord.push_back(Elt: getSubmoduleID(Mod: M->getOwningModule()));
2700
2701 Stream.EmitRecord(Code: PP_MODULE_MACRO, Vals: 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(Elt: M);
2708
2709 EmittedModuleMacros = true;
2710 }
2711 }
2712 if (Record.empty() && !EmittedModuleMacros)
2713 continue;
2714
2715 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2716 Stream.EmitRecord(Code: PP_MACRO_DIRECTIVE_HISTORY, Vals: 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(new_size: 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(II: Name, Record);
2748 AddSourceLocation(Loc: MI->getDefinitionLoc(), Record);
2749 AddSourceLocation(Loc: MI->getDefinitionEndLoc(), Record);
2750 Record.push_back(Elt: MI->isUsed());
2751 Record.push_back(Elt: MI->isUsedForHeaderGuard());
2752 Record.push_back(Elt: 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(Elt: MI->isC99Varargs());
2760 Record.push_back(Elt: MI->isGNUVarargs());
2761 Record.push_back(Elt: MI->hasCommaPasting());
2762 Record.push_back(Elt: MI->getNumParams());
2763 for (const IdentifierInfo *Param : MI->params())
2764 AddIdentifierRef(II: 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(Elt: MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2771
2772 Stream.EmitRecord(Code, Vals: 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(Tok: TokNo);
2781 AddToken(Tok, Record);
2782 Stream.EmitRecord(Code: PP_TOKEN, Vals: 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(OpInfo: BitCodeAbbrevOp(MACRO_OFFSET));
2795 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2796 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2797 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2798 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2799
2800 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbv: 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(Abbrev: MacroOffsetAbbrev, Vals: Record, Blob: bytes(v: MacroOffsets));
2806 }
2807}
2808
2809void 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(BlockID: PREPROCESSOR_DETAIL_BLOCK_ID, CodeLen: 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(OpInfo: BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2828 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2829 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2830 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2831 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2832 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2833 InclusionAbbrev = Stream.EmitAbbrev(Abbv: 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(Range: (*E)->getSourceRange());
2850 PreprocessedEntityOffsets.emplace_back(
2851 Args: getRawSourceLocationEncoding(Loc: R.getBegin()),
2852 Args: getRawSourceLocationEncoding(Loc: R.getEnd()), Args&: Offset);
2853
2854 if (auto *MD = dyn_cast<MacroDefinitionRecord>(Val: *E)) {
2855 // Record this macro definition's ID.
2856 MacroDefinitions[MD] = NextPreprocessorEntityID;
2857
2858 AddIdentifierRef(II: MD->getName(), Record);
2859 Stream.EmitRecord(Code: PPD_MACRO_DEFINITION, Vals: Record);
2860 continue;
2861 }
2862
2863 if (auto *ME = dyn_cast<MacroExpansion>(Val: *E)) {
2864 Record.push_back(Elt: ME->isBuiltinMacro());
2865 if (ME->isBuiltinMacro())
2866 AddIdentifierRef(II: ME->getName(), Record);
2867 else
2868 Record.push_back(Elt: MacroDefinitions[ME->getDefinition()]);
2869 Stream.EmitRecord(Code: PPD_MACRO_EXPANSION, Vals: Record);
2870 continue;
2871 }
2872
2873 if (auto *ID = dyn_cast<InclusionDirective>(Val: *E)) {
2874 Record.push_back(Elt: PPD_INCLUSION_DIRECTIVE);
2875 Record.push_back(Elt: ID->getFileName().size());
2876 Record.push_back(Elt: ID->wasInQuotes());
2877 Record.push_back(Elt: static_cast<unsigned>(ID->getKind()));
2878 Record.push_back(Elt: 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(Abbrev: InclusionAbbrev, Vals: Record, Blob: 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(OpInfo: BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2902 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2903 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2904 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2905
2906 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2907 FirstPreprocessorEntityID -
2908 NUM_PREDEF_PP_ENTITY_IDS};
2909 Stream.EmitRecordWithBlob(Abbrev: PPEOffsetAbbrev, Vals: Record,
2910 Blob: bytes(v: 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(n: SkippedRanges.size());
2918 for (auto const& Range : SkippedRanges)
2919 SerializedSkippedRanges.emplace_back(
2920 args: getRawSourceLocationEncoding(Loc: Range.getBegin()),
2921 args: getRawSourceLocationEncoding(Loc: Range.getEnd()));
2922
2923 using namespace llvm;
2924 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2925 Abbrev->Add(OpInfo: BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2926 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2927 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2928
2929 Record.clear();
2930 Record.push_back(Elt: PPD_SKIPPED_RANGES);
2931 Stream.EmitRecordWithBlob(Abbrev: PPESkippedRangeAbbrev, Vals: Record,
2932 Blob: bytes(v: SerializedSkippedRanges));
2933 }
2934}
2935
2936unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module *Mod) {
2937 if (!Mod)
2938 return 0;
2939
2940 auto Known = SubmoduleIDs.find(Val: 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(nameParts: StringRef(getLangOpts().CurrentModule))))
2948 return 0;
2949
2950 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2951}
2952
2953unsigned 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).
2965static unsigned getNumberOfModules(Module *Mod) {
2966 unsigned ChildModules = 0;
2967 for (auto *Submodule : Mod->submodules())
2968 ChildModules += getNumberOfModules(Mod: Submodule);
2969
2970 return ChildModules + 1;
2971}
2972
2973void ASTWriter::WriteSubmodules(Module *WritingModule, ASTContext *Context) {
2974 // Enter the submodule description block.
2975 Stream.EnterSubblock(BlockID: SUBMODULE_BLOCK_ID, /*bits for abbreviations*/CodeLen: 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(OpInfo: BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2982 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2983 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2984 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Kind
2985 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
2986 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Inferred allowed by
2987 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2988 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2989 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2990 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2991 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2992 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2993 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2994 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2995 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2996 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NamedModuleHasN...
2997 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2998 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
2999
3000 Abbrev = std::make_shared<BitCodeAbbrev>();
3001 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
3002 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3003 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3004
3005 Abbrev = std::make_shared<BitCodeAbbrev>();
3006 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_HEADER));
3007 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3008 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3009
3010 Abbrev = std::make_shared<BitCodeAbbrev>();
3011 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
3012 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3013 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3014
3015 Abbrev = std::make_shared<BitCodeAbbrev>();
3016 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
3017 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3018 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3019
3020 Abbrev = std::make_shared<BitCodeAbbrev>();
3021 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_REQUIRES));
3022 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
3023 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
3024 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3025
3026 Abbrev = std::make_shared<BitCodeAbbrev>();
3027 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
3028 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3029 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3030
3031 Abbrev = std::make_shared<BitCodeAbbrev>();
3032 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
3033 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3034 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3035
3036 Abbrev = std::make_shared<BitCodeAbbrev>();
3037 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
3038 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3039 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3040
3041 Abbrev = std::make_shared<BitCodeAbbrev>();
3042 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
3043 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3044 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3045
3046 Abbrev = std::make_shared<BitCodeAbbrev>();
3047 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
3048 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
3049 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
3050 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3051
3052 Abbrev = std::make_shared<BitCodeAbbrev>();
3053 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
3054 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
3055 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3056
3057 Abbrev = std::make_shared<BitCodeAbbrev>();
3058 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_CONFLICT));
3059 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
3060 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
3061 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3062
3063 Abbrev = std::make_shared<BitCodeAbbrev>();
3064 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
3065 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
3066 unsigned ExportAsAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3067
3068 // Write the submodule metadata block.
3069 RecordData::value_type Record[] = {
3070 getNumberOfModules(Mod: WritingModule),
3071 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
3072 Stream.EmitRecord(Code: SUBMODULE_METADATA, Vals: Record);
3073
3074 // Write all of the submodules.
3075 std::queue<Module *> Q;
3076 Q.push(x: 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(Loc: getAdjustedLocation(Loc: Mod->DefinitionLoc));
3090
3091 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
3092 FileID UnadjustedInferredFID;
3093 if (Mod->IsInferred)
3094 UnadjustedInferredFID = ModMap.getModuleMapFileIDForUniquing(M: Mod);
3095 int InferredFID = getAdjustedFileID(FID: 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(Abbrev: DefinitionAbbrev, Vals: Record, Blob: 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(Abbrev: RequiresAbbrev, Vals: Record, Blob: 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(Abbrev: UmbrellaAbbrev, Vals: Record,
3129 Blob: UmbrellaHeader->NameAsWritten);
3130 } else if (std::optional<Module::DirectoryName> UmbrellaDir =
3131 Mod->getUmbrellaDirAsWritten()) {
3132 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
3133 Stream.EmitRecordWithBlob(Abbrev: UmbrellaDirAbbrev, Vals: Record,
3134 Blob: UmbrellaDir->NameAsWritten);
3135 }
3136
3137 // Emit the headers.
3138 struct {
3139 unsigned RecordKind;
3140 unsigned Abbrev;
3141 Module::HeaderKind HeaderKind;
3142 } HeaderLists[] = {
3143 {.RecordKind: SUBMODULE_HEADER, .Abbrev: HeaderAbbrev, .HeaderKind: Module::HK_Normal},
3144 {.RecordKind: SUBMODULE_TEXTUAL_HEADER, .Abbrev: TextualHeaderAbbrev, .HeaderKind: Module::HK_Textual},
3145 {.RecordKind: SUBMODULE_PRIVATE_HEADER, .Abbrev: PrivateHeaderAbbrev, .HeaderKind: Module::HK_Private},
3146 {.RecordKind: SUBMODULE_PRIVATE_TEXTUAL_HEADER, .Abbrev: PrivateTextualHeaderAbbrev,
3147 .HeaderKind: Module::HK_PrivateTextual},
3148 {.RecordKind: SUBMODULE_EXCLUDED_HEADER, .Abbrev: ExcludedHeaderAbbrev, .HeaderKind: Module::HK_Excluded}
3149 };
3150 for (const auto &HL : HeaderLists) {
3151 RecordData::value_type Record[] = {HL.RecordKind};
3152 for (const auto &H : Mod->getHeaders(HK: HL.HeaderKind))
3153 Stream.EmitRecordWithBlob(Abbrev: HL.Abbrev, Vals: Record, Blob: H.NameAsWritten);
3154 }
3155
3156 // Emit the top headers.
3157 {
3158 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
3159 for (FileEntryRef H : Mod->getTopHeaders(FileMgr&: PP->getFileManager())) {
3160 SmallString<128> HeaderName(H.getName());
3161 PreparePathForOutput(Path&: HeaderName);
3162 Stream.EmitRecordWithBlob(Abbrev: TopHeaderAbbrev, Vals: Record, Blob: 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(Elt: getSubmoduleID(Mod: I));
3171 Stream.EmitRecord(Code: SUBMODULE_IMPORTS, Vals: 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(Elt: getSubmoduleID(Mod: I));
3179 Stream.EmitRecord(Code: SUBMODULE_AFFECTING_MODULES, Vals: 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(Elt: getSubmoduleID(Mod: E.getPointer()));
3189 Record.push_back(Elt: E.getInt());
3190 }
3191 Stream.EmitRecord(Code: SUBMODULE_EXPORTS, Vals: 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(Abbrev: LinkLibraryAbbrev, Vals: Record, Blob: 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(Mod: C.Other)};
3213 Stream.EmitRecordWithBlob(Abbrev: ConflictAbbrev, Vals: Record, Blob: 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(Abbrev: ConfigMacroAbbrev, Vals: Record, Blob: 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(M: Mod))
3227 if (wasDeclEmitted(D))
3228 AddDeclRef(D, Record&: Inits);
3229 if (!Inits.empty())
3230 Stream.EmitRecord(Code: SUBMODULE_INITIALIZERS, Vals: 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(Abbrev: ExportAsAbbrev, Vals: Record, Blob: Mod->ExportAsModule);
3237 }
3238
3239 // Queue up the submodules of this module.
3240 for (auto *M : Mod->submodules())
3241 Q.push(x: 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
3252void 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(Elt: 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(Elt: 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(DiagID: I.first))
3303 continue;
3304 Mappings.push_back(Elt: I);
3305 }
3306
3307 // Sort by diag::kind for deterministic output.
3308 llvm::sort(C&: Mappings, Comp: llvm::less_first());
3309
3310 for (const auto &I : Mappings) {
3311 Record.push_back(Elt: I.first);
3312 Record.push_back(Elt: 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(FID: FileIDAndFile.first, Record);
3334
3335 Record.push_back(Elt: FileIDAndFile.second.StateTransitions.size());
3336 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3337 Record.push_back(Elt: getAdjustedOffset(Offset: 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(Loc: Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3352 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3353
3354 Stream.EmitRecord(Code: DIAG_PRAGMA_MAPPINGS, Vals: Record);
3355}
3356
3357//===----------------------------------------------------------------------===//
3358// Type Serialization
3359//===----------------------------------------------------------------------===//
3360
3361/// Write the representation of a type to the AST stream.
3362void 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(args&: Offset);
3379 else if (TypeOffsets.size() < Index) {
3380 TypeOffsets.resize(new_size: 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
3391static bool IsInternalDeclFromFileContext(const Decl *D) {
3392 auto *ND = dyn_cast<NamedDecl>(Val: 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.
3407uint64_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(Elt: D->getKind());
3432 KindDeclPairs.push_back(Elt: GetDeclRef(D).getRawValue());
3433 }
3434
3435 ++NumLexicalDeclContexts;
3436 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3437 Stream.EmitRecordWithBlob(Abbrev: DeclContextLexicalAbbrev, Vals: Record,
3438 Blob: bytes(v: KindDeclPairs));
3439 return Offset;
3440}
3441
3442void ASTWriter::WriteTypeDeclOffsets() {
3443 using namespace llvm;
3444
3445 // Write the type offsets array
3446 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3447 Abbrev->Add(OpInfo: BitCodeAbbrevOp(TYPE_OFFSET));
3448 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3449 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3450 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3451 {
3452 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size()};
3453 Stream.EmitRecordWithBlob(Abbrev: TypeOffsetAbbrev, Vals: Record, Blob: bytes(v: TypeOffsets));
3454 }
3455
3456 // Write the declaration offsets array
3457 Abbrev = std::make_shared<BitCodeAbbrev>();
3458 Abbrev->Add(OpInfo: BitCodeAbbrevOp(DECL_OFFSET));
3459 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3460 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3461 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3462 {
3463 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size()};
3464 Stream.EmitRecordWithBlob(Abbrev: DeclOffsetAbbrev, Vals: Record, Blob: bytes(v: DeclOffsets));
3465 }
3466}
3467
3468void ASTWriter::WriteFileDeclIDsMap() {
3469 using namespace llvm;
3470
3471 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
3472 SortedFileDeclIDs.reserve(N: FileDeclIDs.size());
3473 for (const auto &P : FileDeclIDs)
3474 SortedFileDeclIDs.push_back(Elt: std::make_pair(x: P.first, y: P.second.get()));
3475 llvm::sort(C&: SortedFileDeclIDs, Comp: 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(Range&: Info.DeclIDs);
3483 for (auto &LocDeclEntry : Info.DeclIDs)
3484 FileGroupedDeclIDs.push_back(Elt: LocDeclEntry.second.getRawValue());
3485 }
3486
3487 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3488 Abbrev->Add(OpInfo: BitCodeAbbrevOp(FILE_SORTED_DECLS));
3489 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3490 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3491 unsigned AbbrevCode = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3492 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3493 FileGroupedDeclIDs.size()};
3494 Stream.EmitRecordWithBlob(Abbrev: AbbrevCode, Vals: Record, Blob: bytes(v: FileGroupedDeclIDs));
3495}
3496
3497void ASTWriter::WriteComments(ASTContext &Context) {
3498 Stream.EnterSubblock(BlockID: COMMENTS_BLOCK_ID, CodeLen: 3);
3499 auto _ = llvm::make_scope_exit(F: [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(Range: I->getSourceRange(), Record);
3515 Record.push_back(Elt: I->getKind());
3516 Record.push_back(Elt: I->isTrailingComment());
3517 Record.push_back(Elt: I->isAlmostTrailingComment());
3518 Stream.EmitRecord(Code: COMMENTS_RAW_COMMENT, Vals: Record);
3519 }
3520 }
3521}
3522
3523//===----------------------------------------------------------------------===//
3524// Global Method Pool and Selector Serialization
3525//===----------------------------------------------------------------------===//
3526
3527namespace {
3528
3529// Trait used for the on-disk hash table used in the method pool.
3530class ASTMethodPoolTrait {
3531 ASTWriter &Writer;
3532
3533public:
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
3546 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3547
3548 static hash_value_type ComputeHash(Selector Sel) {
3549 return serialization::ComputeHash(Sel);
3550 }
3551
3552 std::pair<unsigned, unsigned>
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(Node: Method))
3562 DataLen += sizeof(DeclID);
3563 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3564 Method = Method->getNext())
3565 if (ShouldWriteMethodListNode(Node: Method))
3566 DataLen += sizeof(DeclID);
3567 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3568 }
3569
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, Offset: Start);
3577 unsigned N = Sel.getNumArgs();
3578 LE.write<uint16_t>(Val: N);
3579 if (N == 0)
3580 N = 1;
3581 for (unsigned I = 0; I != N; ++I)
3582 LE.write<IdentifierID>(
3583 Val: Writer.getIdentifierRef(II: Sel.getIdentifierInfoForSlot(argIndex: I)));
3584 }
3585
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>(Val: Methods.ID);
3593 unsigned NumInstanceMethods = 0;
3594 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3595 Method = Method->getNext())
3596 if (ShouldWriteMethodListNode(Node: Method))
3597 ++NumInstanceMethods;
3598
3599 unsigned NumFactoryMethods = 0;
3600 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3601 Method = Method->getNext())
3602 if (ShouldWriteMethodListNode(Node: 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>(Val: FullInstanceBits);
3620 LE.write<uint16_t>(Val: FullFactoryBits);
3621 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3622 Method = Method->getNext())
3623 if (ShouldWriteMethodListNode(Node: Method))
3624 LE.write<DeclID>(Val: (DeclID)Writer.getDeclID(D: Method->getMethod()));
3625 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3626 Method = Method->getNext())
3627 if (ShouldWriteMethodListNode(Node: Method))
3628 LE.write<DeclID>(Val: (DeclID)Writer.getDeclID(D: Method->getMethod()));
3629
3630 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3631 }
3632
3633private:
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.
3646void 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(new_size: 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(Val: S);
3666 ASTMethodPoolTrait::data_type Data = {
3667 .ID: ID,
3668 .Instance: ObjCMethodList(),
3669 .Factory: 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(Key: S, Data, InfoObj&: 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>(os&: Out, value: 0, endian: llvm::endianness::little);
3715 BucketOffset = Generator.Emit(Out, InfoObj&: Trait);
3716 }
3717
3718 // Create a blob abbreviation
3719 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3720 Abbrev->Add(OpInfo: BitCodeAbbrevOp(METHOD_POOL));
3721 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3722 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3723 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3724 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
3725
3726 // Write the method pool
3727 {
3728 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3729 NumTableEntries};
3730 Stream.EmitRecordWithBlob(Abbrev: MethodPoolAbbrev, Vals: Record, Blob: MethodPool);
3731 }
3732
3733 // Create a blob abbreviation for the selector table offsets.
3734 Abbrev = std::make_shared<BitCodeAbbrev>();
3735 Abbrev->Add(OpInfo: BitCodeAbbrevOp(SELECTOR_OFFSETS));
3736 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3737 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3738 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3739 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbv: 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(Abbrev: SelectorOffsetAbbrev, Vals: Record,
3747 Blob: bytes(v: SelectorOffsets));
3748 }
3749 }
3750}
3751
3752/// Write the selectors referenced in @selector expression into AST file.
3753void 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(S: Sel);
3769 Writer.AddSourceLocation(Loc);
3770 }
3771 Writer.Emit(Code: 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.
3783static 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 DC: D->getDeclContext()->getRedeclContext()))
3798 continue;
3799 return cast<NamedDecl>(Val: 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>(Val: 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
3818namespace {
3819
3820bool 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
3839bool 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
3846class 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.
3858 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3859 return IsInterestingIdentifier(II, MacroOffset, IsModule,
3860 IsCPlusPlus: Writer.getLangOpts().CPlusPlus);
3861 }
3862
3863public:
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
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
3880 bool needDecls() const { return NeedDecls; }
3881
3882 static hash_value_type ComputeHash(const IdentifierInfo* II) {
3883 return llvm::djbHash(Buffer: II->getName());
3884 }
3885
3886 bool isInterestingIdentifier(const IdentifierInfo *II) {
3887 auto MacroOffset = Writer.getMacroDirectivesOffset(Name: II);
3888 return isInterestingIdentifier(II, MacroOffset);
3889 }
3890
3891 std::pair<unsigned, unsigned>
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, Offset: Out.tell());
3896
3897 auto MacroOffset = Writer.getMacroDirectivesOffset(Name: 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(Elt: 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(first: IdResolver->begin(Name: II), last: IdResolver->end()) *
3916 sizeof(DeclID);
3917 }
3918 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3919 }
3920
3921 void EmitKey(raw_ostream &Out, const IdentifierInfo *II, unsigned KeyLen) {
3922 Out.write(Ptr: II->getNameStart(), Size: KeyLen);
3923 }
3924
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(Name: II);
3932 if (!isInterestingIdentifier(II, MacroOffset)) {
3933 LE.write<IdentifierID>(Val: ID << 1);
3934 return;
3935 }
3936
3937 LE.write<IdentifierID>(Val: (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>(Val: 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>(Val: Bits);
3951
3952 if (HasMacroDefinition)
3953 LE.write<uint32_t>(Val: 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(Name: II));
3963 for (NamedDecl *D : llvm::reverse(C&: Decls))
3964 LE.write<DeclID>(Val: (DeclID)Writer.getDeclID(
3965 D: getDeclForLocalLookup(LangOpts: 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.
3974static 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.
3981void 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(new_size: 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(Key: II, Data: ID, InfoObj&: 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>(os&: Out, value: 0, endian: llvm::endianness::little);
4020 BucketOffset = Generator.Emit(Out, InfoObj&: Trait);
4021 }
4022
4023 // Create a blob abbreviation
4024 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4025 Abbrev->Add(OpInfo: BitCodeAbbrevOp(IDENTIFIER_TABLE));
4026 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4027 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4028 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
4029
4030 // Write the identifier table
4031 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
4032 Stream.EmitRecordWithBlob(Abbrev: IDTableAbbrev, Vals: Record, Blob: IdentifierTable);
4033 }
4034
4035 // Write the offsets table for identifier IDs.
4036 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4037 Abbrev->Add(OpInfo: BitCodeAbbrevOp(IDENTIFIER_OFFSET));
4038 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
4039 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4040 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbv: 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(Abbrev: IdentifierOffsetAbbrev, Vals: Record,
4050 Blob: bytes(v: 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(Code: INTERESTING_IDENTIFIERS, Vals: InterestingIdents);
4056}
4057
4058void ASTWriter::handleVTable(CXXRecordDecl *RD) {
4059 if (!RD->isInNamedModule())
4060 return;
4061
4062 PendingEmittingVTables.push_back(Elt: RD);
4063}
4064
4065void ASTWriter::addTouchedModuleFile(serialization::ModuleFile *MF) {
4066 TouchedModuleFiles.insert(X: MF);
4067}
4068
4069//===----------------------------------------------------------------------===//
4070// DeclContext's Name Lookup Table Serialization
4071//===----------------------------------------------------------------------===//
4072
4073namespace {
4074
4075class ASTDeclContextNameLookupTraitBase {
4076protected:
4077 ASTWriter &Writer;
4078 using DeclIDsTy = llvm::SmallVector<LocalDeclID, 64>;
4079 DeclIDsTy DeclIDs;
4080
4081public:
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
4089 explicit ASTDeclContextNameLookupTraitBase(ASTWriter &Writer)
4090 : Writer(Writer) {}
4091
4092 data_type getData(const DeclIDsTy &LocalIDs) {
4093 unsigned Start = DeclIDs.size();
4094 for (auto ID : LocalIDs)
4095 DeclIDs.push_back(Elt: ID);
4096 return std::make_pair(x&: Start, y: DeclIDs.size());
4097 }
4098
4099 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
4100 unsigned Start = DeclIDs.size();
4101 DeclIDs.insert(
4102 I: DeclIDs.end(),
4103 From: DeclIDIterator<GlobalDeclID, LocalDeclID>(FromReader.begin()),
4104 To: DeclIDIterator<GlobalDeclID, LocalDeclID>(FromReader.end()));
4105 return std::make_pair(x&: Start, y: DeclIDs.size());
4106 }
4107
4108 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(MF: F);
4114 endian::write<uint32_t>(os&: Out, value: Writer.getChain()->getModuleFileID(M: F),
4115 endian: llvm::endianness::little);
4116 }
4117
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
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>(Val: Name.getKind());
4154 switch (Name.getKind()) {
4155 case DeclarationName::Identifier:
4156 case DeclarationName::CXXLiteralOperatorName:
4157 case DeclarationName::CXXDeductionGuideName:
4158 LE.write<IdentifierID>(Val: Writer.getIdentifierRef(II: Name.getIdentifier()));
4159 return;
4160 case DeclarationName::ObjCZeroArgSelector:
4161 case DeclarationName::ObjCOneArgSelector:
4162 case DeclarationName::ObjCMultiArgSelector:
4163 LE.write<uint32_t>(Val: Writer.getSelectorRef(Sel: Name.getSelector()));
4164 return;
4165 case DeclarationName::CXXOperatorName:
4166 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
4167 "Invalid operator?");
4168 LE.write<uint8_t>(Val: 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
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>(Val: (DeclID)DeclIDs[I]);
4187 assert(Out.tell() - Start == DataLen && "Data length is wrong");
4188 }
4189};
4190
4191class ModuleLevelNameLookupTrait : public ASTDeclContextNameLookupTraitBase {
4192public:
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
4198 explicit ModuleLevelNameLookupTrait(ASTWriter &Writer)
4199 : ASTDeclContextNameLookupTraitBase(Writer) {}
4200
4201 static bool EqualKey(key_type_ref a, key_type_ref b) { return a == b; }
4202
4203 hash_value_type ComputeHash(key_type Key) {
4204 llvm::FoldingSetNodeID ID;
4205 ID.AddInteger(I: Key.first.getHash());
4206 ID.AddInteger(I: Key.second);
4207 return ID.computeStableHash();
4208 }
4209
4210 std::pair<unsigned, unsigned>
4211 EmitKeyDataLength(raw_ostream &Out, key_type Key, data_type_ref Lookup) {
4212 auto [KeyLen, DataLen] = EmitKeyDataLengthBase(Out, Name: Key.first, Lookup);
4213 KeyLen += sizeof(Key.second);
4214 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
4215 }
4216
4217 void EmitKey(raw_ostream &Out, key_type Key, unsigned) {
4218 EmitKeyBase(Out, Name: Key.first);
4219 llvm::support::endian::Writer LE(Out, llvm::endianness::little);
4220 LE.write<primary_module_hash_type>(Val: Key.second);
4221 }
4222
4223 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
4224 unsigned DataLen) {
4225 EmitDataBase(Out, Lookup, DataLen);
4226 }
4227};
4228
4229class ASTDeclContextNameTrivialLookupTrait
4230 : public ASTDeclContextNameLookupTraitBase {
4231public:
4232 using key_type = DeclarationNameKey;
4233 using key_type_ref = key_type;
4234
4235public:
4236 using ASTDeclContextNameLookupTraitBase::ASTDeclContextNameLookupTraitBase;
4237
4238 using ASTDeclContextNameLookupTraitBase::getData;
4239
4240 static bool EqualKey(key_type_ref a, key_type_ref b) { return a == b; }
4241
4242 hash_value_type ComputeHash(key_type Name) { return Name.getHash(); }
4243
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
4251 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
4252 return EmitKeyBase(Out, Name);
4253 }
4254
4255 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
4256 unsigned DataLen) {
4257 EmitDataBase(Out, Lookup, DataLen);
4258 }
4259};
4260
4261static 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>(Val: D->getNonTransparentDeclContext());
4265 Parent && !D->getNonTransparentDeclContext()->isFileContext())
4266 return isModuleLocalDecl(D: 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>(Val: D))
4271 if (auto *CDGD = dyn_cast<CXXDeductionGuideDecl>(Val: FTD->getTemplatedDecl()))
4272 return isModuleLocalDecl(D: CDGD->getDeducedTemplate());
4273
4274 if (D->getFormalLinkage() == Linkage::Module)
4275 return true;
4276
4277 return false;
4278}
4279
4280static 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
4303class ASTDeclContextNameLookupTrait
4304 : public ASTDeclContextNameTrivialLookupTrait {
4305public:
4306 using TULocalDeclsMapTy = llvm::DenseMap<key_type, DeclIDsTy>;
4307
4308 using ModuleLevelDeclsMapTy =
4309 llvm::DenseMap<ModuleLevelNameLookupTrait::key_type, DeclIDsTy>;
4310
4311private:
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
4322 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>(Val: D);
4343 ECD && DC.isFileContext() && ECD->getOwningModule() &&
4344 ECD->getTopLevelOwningNamedModule()->isNamedModule()) {
4345 if (llvm::all_of(
4346 Range: DC.noload_lookup(
4347 Name: cast<EnumDecl>(Val: ECD->getDeclContext())->getDeclName()),
4348 P: [](auto *Found) {
4349 return Found->isInvisibleOutsideTheOwningModule();
4350 }))
4351 return ECD->isFromExplicitGlobalModule() ||
4352 ECD->isInAnonymousNamespace()
4353 ? LookupVisibility::TULocal
4354 : LookupVisibility::ModuleLocalVisible;
4355 }
4356
4357 return LookupVisibility::GenerallyVisibile;
4358 }
4359
4360 DeclContext &DC;
4361 ModuleLevelDeclsMapTy ModuleLocalDeclsMap;
4362 TULocalDeclsMapTy TULocalDeclsMap;
4363
4364public:
4365 using ASTDeclContextNameTrivialLookupTrait::
4366 ASTDeclContextNameTrivialLookupTrait;
4367
4368 ASTDeclContextNameLookupTrait(ASTWriter &Writer, DeclContext &DC)
4369 : ASTDeclContextNameTrivialLookupTrait(Writer), DC(DC) {}
4370
4371 template <typename Coll> data_type getData(const Coll &Decls) {
4372 unsigned Start = DeclIDs.size();
4373 for (NamedDecl *D : Decls) {
4374 NamedDecl *DeclForLocalLookup =
4375 getDeclForLocalLookup(LangOpts: Writer.getLangOpts(), D);
4376
4377 if (Writer.getDoneWritingDeclsAndTypes() &&
4378 !Writer.wasDeclEmitted(D: DeclForLocalLookup))
4379 continue;
4380
4381 // Try to avoid writing internal decls to reduced BMI.
4382 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4383 if (Writer.isGeneratingReducedBMI() &&
4384 !DeclForLocalLookup->isFromExplicitGlobalModule() &&
4385 IsInternalDeclFromFileContext(D: DeclForLocalLookup))
4386 continue;
4387
4388 auto ID = Writer.GetDeclRef(D: DeclForLocalLookup);
4389
4390 switch (getLookupVisibility(D: DeclForLocalLookup)) {
4391 case LookupVisibility::ModuleLocalVisible:
4392 if (UnsignedOrNone PrimaryModuleHash =
4393 getPrimaryModuleHash(M: D->getOwningModule())) {
4394 auto Key = std::make_pair(x: D->getDeclName(), y: *PrimaryModuleHash);
4395 auto Iter = ModuleLocalDeclsMap.find(Val: Key);
4396 if (Iter == ModuleLocalDeclsMap.end())
4397 ModuleLocalDeclsMap.insert(KV: {Key, DeclIDsTy{ID}});
4398 else
4399 Iter->second.push_back(Elt: ID);
4400 continue;
4401 }
4402 break;
4403 case LookupVisibility::TULocal: {
4404 auto Iter = TULocalDeclsMap.find(Val: D->getDeclName());
4405 if (Iter == TULocalDeclsMap.end())
4406 TULocalDeclsMap.insert(KV: {D->getDeclName(), DeclIDsTy{ID}});
4407 else
4408 Iter->second.push_back(Elt: ID);
4409 continue;
4410 }
4411 case LookupVisibility::GenerallyVisibile:
4412 // Generally visible decls go into the general lookup table.
4413 break;
4414 }
4415
4416 DeclIDs.push_back(Elt: ID);
4417 }
4418 return std::make_pair(x&: Start, y: DeclIDs.size());
4419 }
4420
4421 const ModuleLevelDeclsMapTy &getModuleLocalDecls() {
4422 return ModuleLocalDeclsMap;
4423 }
4424
4425 const TULocalDeclsMapTy &getTULocalDecls() { return TULocalDeclsMap; }
4426};
4427
4428} // namespace
4429
4430namespace {
4431class LazySpecializationInfoLookupTrait {
4432 ASTWriter &Writer;
4433 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 64> Specs;
4434
4435public:
4436 using key_type = unsigned;
4437 using key_type_ref = key_type;
4438
4439 /// A start and end index into Specs, representing a sequence of decls.
4440 using data_type = std::pair<unsigned, unsigned>;
4441 using data_type_ref = const data_type &;
4442
4443 using hash_value_type = unsigned;
4444 using offset_type = unsigned;
4445
4446 explicit LazySpecializationInfoLookupTrait(ASTWriter &Writer)
4447 : Writer(Writer) {}
4448
4449 template <typename Col, typename Col2>
4450 data_type getData(Col &&C, Col2 &ExistingInfo) {
4451 unsigned Start = Specs.size();
4452 for (auto *D : C) {
4453 NamedDecl *ND = getDeclForLocalLookup(LangOpts: Writer.getLangOpts(),
4454 D: const_cast<NamedDecl *>(D));
4455 Specs.push_back(Elt: GlobalDeclID(Writer.GetDeclRef(D: ND).getRawValue()));
4456 }
4457 for (const serialization::reader::LazySpecializationInfo &Info :
4458 ExistingInfo)
4459 Specs.push_back(Elt: Info);
4460 return std::make_pair(x&: Start, y: Specs.size());
4461 }
4462
4463 data_type ImportData(
4464 const reader::LazySpecializationInfoLookupTrait::data_type &FromReader) {
4465 unsigned Start = Specs.size();
4466 for (auto ID : FromReader)
4467 Specs.push_back(Elt: ID);
4468 return std::make_pair(x&: Start, y: Specs.size());
4469 }
4470
4471 static bool EqualKey(key_type_ref a, key_type_ref b) { return a == b; }
4472
4473 hash_value_type ComputeHash(key_type Name) { return Name; }
4474
4475 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
4476 assert(Writer.hasChain() &&
4477 "have reference to loaded module file but no chain?");
4478
4479 using namespace llvm::support;
4480 Writer.addTouchedModuleFile(MF: F);
4481 endian::write<uint32_t>(os&: Out, value: Writer.getChain()->getModuleFileID(M: F),
4482 endian: llvm::endianness::little);
4483 }
4484
4485 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
4486 key_type HashValue,
4487 data_type_ref Lookup) {
4488 // 4 bytes for each slot.
4489 unsigned KeyLen = 4;
4490 unsigned DataLen = sizeof(serialization::reader::LazySpecializationInfo) *
4491 (Lookup.second - Lookup.first);
4492
4493 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
4494 }
4495
4496 void EmitKey(raw_ostream &Out, key_type HashValue, unsigned) {
4497 using namespace llvm::support;
4498
4499 endian::Writer LE(Out, llvm::endianness::little);
4500 LE.write<uint32_t>(Val: HashValue);
4501 }
4502
4503 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
4504 unsigned DataLen) {
4505 using namespace llvm::support;
4506
4507 endian::Writer LE(Out, llvm::endianness::little);
4508 uint64_t Start = Out.tell();
4509 (void)Start;
4510 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I) {
4511 LE.write<DeclID>(Val: Specs[I].getRawValue());
4512 }
4513 assert(Out.tell() - Start == DataLen && "Data length is wrong");
4514 }
4515};
4516
4517unsigned CalculateODRHashForSpecs(const Decl *Spec) {
4518 ArrayRef<TemplateArgument> Args;
4519 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: Spec))
4520 Args = CTSD->getTemplateArgs().asArray();
4521 else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: Spec))
4522 Args = VTSD->getTemplateArgs().asArray();
4523 else if (auto *FD = dyn_cast<FunctionDecl>(Val: Spec))
4524 Args = FD->getTemplateSpecializationArgs()->asArray();
4525 else
4526 llvm_unreachable("New Specialization Kind?");
4527
4528 return StableHashForTemplateArguments(Args);
4529}
4530} // namespace
4531
4532void ASTWriter::GenerateSpecializationInfoLookupTable(
4533 const NamedDecl *D, llvm::SmallVectorImpl<const Decl *> &Specializations,
4534 llvm::SmallVectorImpl<char> &LookupTable, bool IsPartial) {
4535 assert(D->isFirstDecl());
4536
4537 // Create the on-disk hash table representation.
4538 MultiOnDiskHashTableGenerator<reader::LazySpecializationInfoLookupTrait,
4539 LazySpecializationInfoLookupTrait>
4540 Generator;
4541 LazySpecializationInfoLookupTrait Trait(*this);
4542
4543 llvm::MapVector<unsigned, llvm::SmallVector<const NamedDecl *, 4>>
4544 SpecializationMaps;
4545
4546 for (auto *Specialization : Specializations) {
4547 unsigned HashedValue = CalculateODRHashForSpecs(Spec: Specialization);
4548
4549 auto Iter = SpecializationMaps.find(Key: HashedValue);
4550 if (Iter == SpecializationMaps.end())
4551 Iter = SpecializationMaps
4552 .try_emplace(Key: HashedValue,
4553 Args: llvm::SmallVector<const NamedDecl *, 4>())
4554 .first;
4555
4556 Iter->second.push_back(Elt: cast<NamedDecl>(Val: Specialization));
4557 }
4558
4559 auto *Lookups =
4560 Chain ? Chain->getLoadedSpecializationsLookupTables(D, IsPartial)
4561 : nullptr;
4562
4563 for (auto &[HashValue, Specs] : SpecializationMaps) {
4564 SmallVector<serialization::reader::LazySpecializationInfo, 16>
4565 ExisitingSpecs;
4566 // We have to merge the lookup table manually here. We can't depend on the
4567 // merge mechanism offered by
4568 // clang::serialization::MultiOnDiskHashTableGenerator since that generator
4569 // assumes the we'll get the same value with the same key.
4570 // And also underlying llvm::OnDiskChainedHashTableGenerator assumes that we
4571 // won't insert the values with the same key twice. So we have to merge the
4572 // lookup table here manually.
4573 if (Lookups)
4574 ExisitingSpecs = Lookups->Table.find(EKey: HashValue);
4575
4576 Generator.insert(Key: HashValue, Data: Trait.getData(C&: Specs, ExistingInfo&: ExisitingSpecs), Info&: Trait);
4577 }
4578
4579 Generator.emit(Out&: LookupTable, Info&: Trait, Base: Lookups ? &Lookups->Table : nullptr);
4580}
4581
4582uint64_t ASTWriter::WriteSpecializationInfoLookupTable(
4583 const NamedDecl *D, llvm::SmallVectorImpl<const Decl *> &Specializations,
4584 bool IsPartial) {
4585
4586 llvm::SmallString<4096> LookupTable;
4587 GenerateSpecializationInfoLookupTable(D, Specializations, LookupTable,
4588 IsPartial);
4589
4590 uint64_t Offset = Stream.GetCurrentBitNo();
4591 RecordData::value_type Record[] = {static_cast<RecordData::value_type>(
4592 IsPartial ? DECL_PARTIAL_SPECIALIZATIONS : DECL_SPECIALIZATIONS)};
4593 Stream.EmitRecordWithBlob(Abbrev: IsPartial ? DeclPartialSpecializationsAbbrev
4594 : DeclSpecializationsAbbrev,
4595 Vals: Record, Blob: LookupTable);
4596
4597 return Offset;
4598}
4599
4600/// Returns ture if all of the lookup result are either external, not emitted or
4601/// predefined. In such cases, the lookup result is not interesting and we don't
4602/// need to record the result in the current being written module. Return false
4603/// otherwise.
4604static bool isLookupResultNotInteresting(ASTWriter &Writer,
4605 StoredDeclsList &Result) {
4606 for (auto *D : Result.getLookupResult()) {
4607 auto *LocalD = getDeclForLocalLookup(LangOpts: Writer.getLangOpts(), D);
4608 if (LocalD->isFromASTFile())
4609 continue;
4610
4611 // We can only be sure whether the local declaration is reachable
4612 // after we done writing the declarations and types.
4613 if (Writer.getDoneWritingDeclsAndTypes() && !Writer.wasDeclEmitted(D: LocalD))
4614 continue;
4615
4616 // We don't need to emit the predefined decls.
4617 if (Writer.isDeclPredefined(D: LocalD))
4618 continue;
4619
4620 return false;
4621 }
4622
4623 return true;
4624}
4625
4626void ASTWriter::GenerateNameLookupTable(
4627 ASTContext &Context, const DeclContext *ConstDC,
4628 llvm::SmallVectorImpl<char> &LookupTable,
4629 llvm::SmallVectorImpl<char> &ModuleLocalLookupTable,
4630 llvm::SmallVectorImpl<char> &TULookupTable) {
4631 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
4632 !ConstDC->hasLazyExternalLexicalLookups() &&
4633 "must call buildLookups first");
4634
4635 // FIXME: We need to build the lookups table, which is logically const.
4636 auto *DC = const_cast<DeclContext*>(ConstDC);
4637 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
4638
4639 // Create the on-disk hash table representation.
4640 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
4641 ASTDeclContextNameLookupTrait>
4642 Generator;
4643 ASTDeclContextNameLookupTrait Trait(*this, *DC);
4644
4645 // The first step is to collect the declaration names which we need to
4646 // serialize into the name lookup table, and to collect them in a stable
4647 // order.
4648 SmallVector<DeclarationName, 16> Names;
4649
4650 // We also track whether we're writing out the DeclarationNameKey for
4651 // constructors or conversion functions.
4652 bool IncludeConstructorNames = false;
4653 bool IncludeConversionNames = false;
4654
4655 for (auto &[Name, Result] : *DC->buildLookup()) {
4656 // If there are no local declarations in our lookup result, we
4657 // don't need to write an entry for the name at all. If we can't
4658 // write out a lookup set without performing more deserialization,
4659 // just skip this entry.
4660 //
4661 // Also in reduced BMI, we'd like to avoid writing unreachable
4662 // declarations in GMF, so we need to avoid writing declarations
4663 // that entirely external or unreachable.
4664 if (GeneratingReducedBMI && isLookupResultNotInteresting(Writer&: *this, Result))
4665 continue;
4666 // We also skip empty results. If any of the results could be external and
4667 // the currently available results are empty, then all of the results are
4668 // external and we skip it above. So the only way we get here with an empty
4669 // results is when no results could have been external *and* we have
4670 // external results.
4671 //
4672 // FIXME: While we might want to start emitting on-disk entries for negative
4673 // lookups into a decl context as an optimization, today we *have* to skip
4674 // them because there are names with empty lookup results in decl contexts
4675 // which we can't emit in any stable ordering: we lookup constructors and
4676 // conversion functions in the enclosing namespace scope creating empty
4677 // results for them. This in almost certainly a bug in Clang's name lookup,
4678 // but that is likely to be hard or impossible to fix and so we tolerate it
4679 // here by omitting lookups with empty results.
4680 if (Result.getLookupResult().empty())
4681 continue;
4682
4683 switch (Name.getNameKind()) {
4684 default:
4685 Names.push_back(Elt: Name);
4686 break;
4687
4688 case DeclarationName::CXXConstructorName:
4689 IncludeConstructorNames = true;
4690 break;
4691
4692 case DeclarationName::CXXConversionFunctionName:
4693 IncludeConversionNames = true;
4694 break;
4695 }
4696 }
4697
4698 // Sort the names into a stable order.
4699 llvm::sort(C&: Names);
4700
4701 if (IncludeConstructorNames || IncludeConversionNames) {
4702 // We need to establish an ordering of constructor and conversion function
4703 // names, and they don't have an intrinsic ordering. We also need to write
4704 // out all constructor and conversion function results if we write out any
4705 // of them, because they're all tracked under the same lookup key.
4706 llvm::SmallPtrSet<DeclarationName, 8> AddedNames;
4707 for (Decl *ChildD : cast<CXXRecordDecl>(Val: DC)->decls()) {
4708 if (auto *ChildND = dyn_cast<NamedDecl>(Val: ChildD)) {
4709 auto Name = ChildND->getDeclName();
4710 switch (Name.getNameKind()) {
4711 default:
4712 continue;
4713
4714 case DeclarationName::CXXConstructorName:
4715 if (!IncludeConstructorNames)
4716 continue;
4717 break;
4718
4719 case DeclarationName::CXXConversionFunctionName:
4720 if (!IncludeConversionNames)
4721 continue;
4722 break;
4723 }
4724 if (AddedNames.insert(Ptr: Name).second)
4725 Names.push_back(Elt: Name);
4726 }
4727 }
4728 }
4729 // Next we need to do a lookup with each name into this decl context to fully
4730 // populate any results from external sources. We don't actually use the
4731 // results of these lookups because we only want to use the results after all
4732 // results have been loaded and the pointers into them will be stable.
4733 for (auto &Name : Names)
4734 DC->lookup(Name);
4735
4736 // Now we need to insert the results for each name into the hash table. For
4737 // constructor names and conversion function names, we actually need to merge
4738 // all of the results for them into one list of results each and insert
4739 // those.
4740 SmallVector<NamedDecl *, 8> ConstructorDecls;
4741 SmallVector<NamedDecl *, 8> ConversionDecls;
4742
4743 // Now loop over the names, either inserting them or appending for the two
4744 // special cases.
4745 for (auto &Name : Names) {
4746 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4747
4748 switch (Name.getNameKind()) {
4749 default:
4750 Generator.insert(Key: Name, Data: Trait.getData(Decls: Result), Info&: Trait);
4751 break;
4752
4753 case DeclarationName::CXXConstructorName:
4754 ConstructorDecls.append(in_start: Result.begin(), in_end: Result.end());
4755 break;
4756
4757 case DeclarationName::CXXConversionFunctionName:
4758 ConversionDecls.append(in_start: Result.begin(), in_end: Result.end());
4759 break;
4760 }
4761 }
4762
4763 // Handle our two special cases if we ended up having any. We arbitrarily use
4764 // the first declaration's name here because the name itself isn't part of
4765 // the key, only the kind of name is used.
4766 if (!ConstructorDecls.empty())
4767 Generator.insert(Key: ConstructorDecls.front()->getDeclName(),
4768 Data: Trait.getData(Decls: ConstructorDecls), Info&: Trait);
4769 if (!ConversionDecls.empty())
4770 Generator.insert(Key: ConversionDecls.front()->getDeclName(),
4771 Data: Trait.getData(Decls: ConversionDecls), Info&: Trait);
4772
4773 // Create the on-disk hash table. Also emit the existing imported and
4774 // merged table if there is one.
4775 auto *Lookups = Chain ? Chain->getLoadedLookupTables(Primary: DC) : nullptr;
4776 Generator.emit(Out&: LookupTable, Info&: Trait, Base: Lookups ? &Lookups->Table : nullptr);
4777
4778 const auto &ModuleLocalDecls = Trait.getModuleLocalDecls();
4779 if (!ModuleLocalDecls.empty()) {
4780 MultiOnDiskHashTableGenerator<reader::ModuleLocalNameLookupTrait,
4781 ModuleLevelNameLookupTrait>
4782 ModuleLocalLookupGenerator;
4783 ModuleLevelNameLookupTrait ModuleLocalTrait(*this);
4784
4785 for (const auto &ModuleLocalIter : ModuleLocalDecls) {
4786 const auto &Key = ModuleLocalIter.first;
4787 const auto &IDs = ModuleLocalIter.second;
4788 ModuleLocalLookupGenerator.insert(Key, Data: ModuleLocalTrait.getData(LocalIDs: IDs),
4789 Info&: ModuleLocalTrait);
4790 }
4791
4792 auto *ModuleLocalLookups =
4793 Chain ? Chain->getModuleLocalLookupTables(Primary: DC) : nullptr;
4794 ModuleLocalLookupGenerator.emit(
4795 Out&: ModuleLocalLookupTable, Info&: ModuleLocalTrait,
4796 Base: ModuleLocalLookups ? &ModuleLocalLookups->Table : nullptr);
4797 }
4798
4799 const auto &TULocalDecls = Trait.getTULocalDecls();
4800 if (!TULocalDecls.empty() && !isGeneratingReducedBMI()) {
4801 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
4802 ASTDeclContextNameTrivialLookupTrait>
4803 TULookupGenerator;
4804 ASTDeclContextNameTrivialLookupTrait TULocalTrait(*this);
4805
4806 for (const auto &TULocalIter : TULocalDecls) {
4807 const auto &Key = TULocalIter.first;
4808 const auto &IDs = TULocalIter.second;
4809 TULookupGenerator.insert(Key, Data: TULocalTrait.getData(LocalIDs: IDs), Info&: TULocalTrait);
4810 }
4811
4812 auto *TULocalLookups = Chain ? Chain->getTULocalLookupTables(Primary: DC) : nullptr;
4813 TULookupGenerator.emit(Out&: TULookupTable, Info&: TULocalTrait,
4814 Base: TULocalLookups ? &TULocalLookups->Table : nullptr);
4815 }
4816}
4817
4818/// Write the block containing all of the declaration IDs
4819/// visible from the given DeclContext.
4820///
4821/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4822/// bitstream, or 0 if no block was written.
4823void ASTWriter::WriteDeclContextVisibleBlock(
4824 ASTContext &Context, DeclContext *DC, VisibleLookupBlockOffsets &Offsets) {
4825 assert(!Offsets);
4826
4827 // If we imported a key declaration of this namespace, write the visible
4828 // lookup results as an update record for it rather than including them
4829 // on this declaration. We will only look at key declarations on reload.
4830 if (isa<NamespaceDecl>(Val: DC) && Chain &&
4831 Chain->getKeyDeclaration(D: cast<Decl>(Val: DC))->isFromASTFile()) {
4832 // Only do this once, for the first local declaration of the namespace.
4833 for (auto *Prev = cast<NamespaceDecl>(Val: DC)->getPreviousDecl(); Prev;
4834 Prev = Prev->getPreviousDecl())
4835 if (!Prev->isFromASTFile())
4836 return;
4837
4838 // Note that we need to emit an update record for the primary context.
4839 UpdatedDeclContexts.insert(X: DC->getPrimaryContext());
4840
4841 // Make sure all visible decls are written. They will be recorded later. We
4842 // do this using a side data structure so we can sort the names into
4843 // a deterministic order.
4844 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4845 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4846 LookupResults;
4847 if (Map) {
4848 LookupResults.reserve(N: Map->size());
4849 for (auto &Entry : *Map)
4850 LookupResults.push_back(
4851 Elt: std::make_pair(x&: Entry.first, y: Entry.second.getLookupResult()));
4852 }
4853
4854 llvm::sort(C&: LookupResults, Comp: llvm::less_first());
4855 for (auto &NameAndResult : LookupResults) {
4856 DeclarationName Name = NameAndResult.first;
4857 DeclContext::lookup_result Result = NameAndResult.second;
4858 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4859 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4860 // We have to work around a name lookup bug here where negative lookup
4861 // results for these names get cached in namespace lookup tables (these
4862 // names should never be looked up in a namespace).
4863 assert(Result.empty() && "Cannot have a constructor or conversion "
4864 "function name in a namespace!");
4865 continue;
4866 }
4867
4868 for (NamedDecl *ND : Result) {
4869 if (ND->isFromASTFile())
4870 continue;
4871
4872 if (DoneWritingDeclsAndTypes && !wasDeclEmitted(D: ND))
4873 continue;
4874
4875 // We don't need to force emitting internal decls into reduced BMI.
4876 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4877 if (GeneratingReducedBMI && !ND->isFromExplicitGlobalModule() &&
4878 IsInternalDeclFromFileContext(D: ND))
4879 continue;
4880
4881 GetDeclRef(D: ND);
4882 }
4883 }
4884
4885 return;
4886 }
4887
4888 if (DC->getPrimaryContext() != DC)
4889 return;
4890
4891 // Skip contexts which don't support name lookup.
4892 if (!DC->isLookupContext())
4893 return;
4894
4895 // If not in C++, we perform name lookup for the translation unit via the
4896 // IdentifierInfo chains, don't bother to build a visible-declarations table.
4897 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4898 return;
4899
4900 // Serialize the contents of the mapping used for lookup. Note that,
4901 // although we have two very different code paths, the serialized
4902 // representation is the same for both cases: a declaration name,
4903 // followed by a size, followed by references to the visible
4904 // declarations that have that name.
4905 StoredDeclsMap *Map = DC->buildLookup();
4906 if (!Map || Map->empty())
4907 return;
4908
4909 Offsets.VisibleOffset = Stream.GetCurrentBitNo();
4910 // Create the on-disk hash table in a buffer.
4911 SmallString<4096> LookupTable;
4912 SmallString<4096> ModuleLocalLookupTable;
4913 SmallString<4096> TULookupTable;
4914 GenerateNameLookupTable(Context, ConstDC: DC, LookupTable, ModuleLocalLookupTable,
4915 TULookupTable);
4916
4917 // Write the lookup table
4918 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4919 Stream.EmitRecordWithBlob(Abbrev: DeclContextVisibleLookupAbbrev, Vals: Record,
4920 Blob: LookupTable);
4921 ++NumVisibleDeclContexts;
4922
4923 if (!ModuleLocalLookupTable.empty()) {
4924 Offsets.ModuleLocalOffset = Stream.GetCurrentBitNo();
4925 assert(Offsets.ModuleLocalOffset > Offsets.VisibleOffset);
4926 // Write the lookup table
4927 RecordData::value_type ModuleLocalRecord[] = {
4928 DECL_CONTEXT_MODULE_LOCAL_VISIBLE};
4929 Stream.EmitRecordWithBlob(Abbrev: DeclModuleLocalVisibleLookupAbbrev,
4930 Vals: ModuleLocalRecord, Blob: ModuleLocalLookupTable);
4931 ++NumModuleLocalDeclContexts;
4932 }
4933
4934 if (!TULookupTable.empty()) {
4935 Offsets.TULocalOffset = Stream.GetCurrentBitNo();
4936 // Write the lookup table
4937 RecordData::value_type TULocalDeclsRecord[] = {
4938 DECL_CONTEXT_TU_LOCAL_VISIBLE};
4939 Stream.EmitRecordWithBlob(Abbrev: DeclTULocalLookupAbbrev, Vals: TULocalDeclsRecord,
4940 Blob: TULookupTable);
4941 ++NumTULocalDeclContexts;
4942 }
4943}
4944
4945/// Write an UPDATE_VISIBLE block for the given context.
4946///
4947/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4948/// DeclContext in a dependent AST file. As such, they only exist for the TU
4949/// (in C++), for namespaces, and for classes with forward-declared unscoped
4950/// enumeration members (in C++11).
4951void ASTWriter::WriteDeclContextVisibleUpdate(ASTContext &Context,
4952 const DeclContext *DC) {
4953 StoredDeclsMap *Map = DC->getLookupPtr();
4954 if (!Map || Map->empty())
4955 return;
4956
4957 // Create the on-disk hash table in a buffer.
4958 SmallString<4096> LookupTable;
4959 SmallString<4096> ModuleLocalLookupTable;
4960 SmallString<4096> TULookupTable;
4961 GenerateNameLookupTable(Context, ConstDC: DC, LookupTable, ModuleLocalLookupTable,
4962 TULookupTable);
4963
4964 // If we're updating a namespace, select a key declaration as the key for the
4965 // update record; those are the only ones that will be checked on reload.
4966 if (isa<NamespaceDecl>(Val: DC))
4967 DC = cast<DeclContext>(Val: Chain->getKeyDeclaration(D: cast<Decl>(Val: DC)));
4968
4969 // Write the lookup table
4970 RecordData::value_type Record[] = {UPDATE_VISIBLE,
4971 getDeclID(D: cast<Decl>(Val: DC)).getRawValue()};
4972 Stream.EmitRecordWithBlob(Abbrev: UpdateVisibleAbbrev, Vals: Record, Blob: LookupTable);
4973
4974 if (!ModuleLocalLookupTable.empty()) {
4975 // Write the module local lookup table
4976 RecordData::value_type ModuleLocalRecord[] = {
4977 UPDATE_MODULE_LOCAL_VISIBLE, getDeclID(D: cast<Decl>(Val: DC)).getRawValue()};
4978 Stream.EmitRecordWithBlob(Abbrev: ModuleLocalUpdateVisibleAbbrev, Vals: ModuleLocalRecord,
4979 Blob: ModuleLocalLookupTable);
4980 }
4981
4982 if (!TULookupTable.empty()) {
4983 RecordData::value_type GMFRecord[] = {
4984 UPDATE_TU_LOCAL_VISIBLE, getDeclID(D: cast<Decl>(Val: DC)).getRawValue()};
4985 Stream.EmitRecordWithBlob(Abbrev: TULocalUpdateVisibleAbbrev, Vals: GMFRecord,
4986 Blob: TULookupTable);
4987 }
4988}
4989
4990/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4991void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4992 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4993 Stream.EmitRecord(Code: FP_PRAGMA_OPTIONS, Vals: Record);
4994}
4995
4996/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4997void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4998 if (!SemaRef.Context.getLangOpts().OpenCL)
4999 return;
5000
5001 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
5002 RecordData Record;
5003 for (const auto &I:Opts.OptMap) {
5004 AddString(Str: I.getKey(), Record);
5005 auto V = I.getValue();
5006 Record.push_back(Elt: V.Supported ? 1 : 0);
5007 Record.push_back(Elt: V.Enabled ? 1 : 0);
5008 Record.push_back(Elt: V.WithPragma ? 1 : 0);
5009 Record.push_back(Elt: V.Avail);
5010 Record.push_back(Elt: V.Core);
5011 Record.push_back(Elt: V.Opt);
5012 }
5013 Stream.EmitRecord(Code: OPENCL_EXTENSIONS, Vals: Record);
5014}
5015void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
5016 if (SemaRef.CUDA().ForceHostDeviceDepth > 0) {
5017 RecordData::value_type Record[] = {SemaRef.CUDA().ForceHostDeviceDepth};
5018 Stream.EmitRecord(Code: CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Vals: Record);
5019 }
5020}
5021
5022void ASTWriter::WriteObjCCategories() {
5023 if (ObjCClassesWithCategories.empty())
5024 return;
5025
5026 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
5027 RecordData Categories;
5028
5029 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
5030 unsigned Size = 0;
5031 unsigned StartIndex = Categories.size();
5032
5033 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
5034
5035 // Allocate space for the size.
5036 Categories.push_back(Elt: 0);
5037
5038 // Add the categories.
5039 for (ObjCInterfaceDecl::known_categories_iterator
5040 Cat = Class->known_categories_begin(),
5041 CatEnd = Class->known_categories_end();
5042 Cat != CatEnd; ++Cat, ++Size) {
5043 assert(getDeclID(*Cat).isValid() && "Bogus category");
5044 AddDeclRef(D: *Cat, Record&: Categories);
5045 }
5046
5047 // Update the size.
5048 Categories[StartIndex] = Size;
5049
5050 // Record this interface -> category map.
5051 ObjCCategoriesInfo CatInfo = { getDeclID(D: Class), StartIndex };
5052 CategoriesMap.push_back(Elt: CatInfo);
5053 }
5054
5055 // Sort the categories map by the definition ID, since the reader will be
5056 // performing binary searches on this information.
5057 llvm::array_pod_sort(Start: CategoriesMap.begin(), End: CategoriesMap.end());
5058
5059 // Emit the categories map.
5060 using namespace llvm;
5061
5062 auto Abbrev = std::make_shared<BitCodeAbbrev>();
5063 Abbrev->Add(OpInfo: BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
5064 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
5065 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5066 unsigned AbbrevID = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
5067
5068 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
5069 Stream.EmitRecordWithBlob(Abbrev: AbbrevID, Vals: Record,
5070 BlobData: reinterpret_cast<char *>(CategoriesMap.data()),
5071 BlobLen: CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
5072
5073 // Emit the category lists.
5074 Stream.EmitRecord(Code: OBJC_CATEGORIES, Vals: Categories);
5075}
5076
5077void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
5078 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
5079
5080 if (LPTMap.empty())
5081 return;
5082
5083 RecordData Record;
5084 for (auto &LPTMapEntry : LPTMap) {
5085 const FunctionDecl *FD = LPTMapEntry.first;
5086 LateParsedTemplate &LPT = *LPTMapEntry.second;
5087 AddDeclRef(D: FD, Record);
5088 AddDeclRef(D: LPT.D, Record);
5089 Record.push_back(Elt: LPT.FPO.getAsOpaqueInt());
5090 Record.push_back(Elt: LPT.Toks.size());
5091
5092 for (const auto &Tok : LPT.Toks) {
5093 AddToken(Tok, Record);
5094 }
5095 }
5096 Stream.EmitRecord(Code: LATE_PARSED_TEMPLATE, Vals: Record);
5097}
5098
5099/// Write the state of 'pragma clang optimize' at the end of the module.
5100void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
5101 RecordData Record;
5102 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
5103 AddSourceLocation(Loc: PragmaLoc, Record);
5104 Stream.EmitRecord(Code: OPTIMIZE_PRAGMA_OPTIONS, Vals: Record);
5105}
5106
5107/// Write the state of 'pragma ms_struct' at the end of the module.
5108void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
5109 RecordData Record;
5110 Record.push_back(Elt: SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
5111 Stream.EmitRecord(Code: MSSTRUCT_PRAGMA_OPTIONS, Vals: Record);
5112}
5113
5114/// Write the state of 'pragma pointers_to_members' at the end of the
5115//module.
5116void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
5117 RecordData Record;
5118 Record.push_back(Elt: SemaRef.MSPointerToMemberRepresentationMethod);
5119 AddSourceLocation(Loc: SemaRef.ImplicitMSInheritanceAttrLoc, Record);
5120 Stream.EmitRecord(Code: POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Vals: Record);
5121}
5122
5123/// Write the state of 'pragma align/pack' at the end of the module.
5124void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
5125 // Don't serialize pragma align/pack state for modules, since it should only
5126 // take effect on a per-submodule basis.
5127 if (WritingModule)
5128 return;
5129
5130 RecordData Record;
5131 AddAlignPackInfo(Info: SemaRef.AlignPackStack.CurrentValue, Record);
5132 AddSourceLocation(Loc: SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
5133 Record.push_back(Elt: SemaRef.AlignPackStack.Stack.size());
5134 for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
5135 AddAlignPackInfo(Info: StackEntry.Value, Record);
5136 AddSourceLocation(Loc: StackEntry.PragmaLocation, Record);
5137 AddSourceLocation(Loc: StackEntry.PragmaPushLocation, Record);
5138 AddString(Str: StackEntry.StackSlotLabel, Record);
5139 }
5140 Stream.EmitRecord(Code: ALIGN_PACK_PRAGMA_OPTIONS, Vals: Record);
5141}
5142
5143/// Write the state of 'pragma float_control' at the end of the module.
5144void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
5145 // Don't serialize pragma float_control state for modules,
5146 // since it should only take effect on a per-submodule basis.
5147 if (WritingModule)
5148 return;
5149
5150 RecordData Record;
5151 Record.push_back(Elt: SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
5152 AddSourceLocation(Loc: SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
5153 Record.push_back(Elt: SemaRef.FpPragmaStack.Stack.size());
5154 for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
5155 Record.push_back(Elt: StackEntry.Value.getAsOpaqueInt());
5156 AddSourceLocation(Loc: StackEntry.PragmaLocation, Record);
5157 AddSourceLocation(Loc: StackEntry.PragmaPushLocation, Record);
5158 AddString(Str: StackEntry.StackSlotLabel, Record);
5159 }
5160 Stream.EmitRecord(Code: FLOAT_CONTROL_PRAGMA_OPTIONS, Vals: Record);
5161}
5162
5163/// Write Sema's collected list of declarations with unverified effects.
5164void ASTWriter::WriteDeclsWithEffectsToVerify(Sema &SemaRef) {
5165 if (SemaRef.DeclsWithEffectsToVerify.empty())
5166 return;
5167 RecordData Record;
5168 for (const auto *D : SemaRef.DeclsWithEffectsToVerify) {
5169 AddDeclRef(D, Record);
5170 }
5171 Stream.EmitRecord(Code: DECLS_WITH_EFFECTS_TO_VERIFY, Vals: Record);
5172}
5173
5174void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
5175 ModuleFileExtensionWriter &Writer) {
5176 // Enter the extension block.
5177 Stream.EnterSubblock(BlockID: EXTENSION_BLOCK_ID, CodeLen: 4);
5178
5179 // Emit the metadata record abbreviation.
5180 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
5181 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
5182 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5183 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5184 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5185 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5186 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5187 unsigned Abbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
5188
5189 // Emit the metadata record.
5190 RecordData Record;
5191 auto Metadata = Writer.getExtension()->getExtensionMetadata();
5192 Record.push_back(Elt: EXTENSION_METADATA);
5193 Record.push_back(Elt: Metadata.MajorVersion);
5194 Record.push_back(Elt: Metadata.MinorVersion);
5195 Record.push_back(Elt: Metadata.BlockName.size());
5196 Record.push_back(Elt: Metadata.UserInfo.size());
5197 SmallString<64> Buffer;
5198 Buffer += Metadata.BlockName;
5199 Buffer += Metadata.UserInfo;
5200 Stream.EmitRecordWithBlob(Abbrev, Vals: Record, Blob: Buffer);
5201
5202 // Emit the contents of the extension block.
5203 Writer.writeExtensionContents(SemaRef, Stream);
5204
5205 // Exit the extension block.
5206 Stream.ExitBlock();
5207}
5208
5209//===----------------------------------------------------------------------===//
5210// General Serialization Routines
5211//===----------------------------------------------------------------------===//
5212
5213void ASTRecordWriter::AddAttr(const Attr *A) {
5214 auto &Record = *this;
5215 // FIXME: Clang can't handle the serialization/deserialization of
5216 // preferred_name properly now. See
5217 // https://github.com/llvm/llvm-project/issues/56490 for example.
5218 if (!A ||
5219 (isa<PreferredNameAttr>(Val: A) && (Writer->isWritingStdCXXNamedModules() ||
5220 Writer->isWritingStdCXXHeaderUnit())))
5221 return Record.push_back(N: 0);
5222
5223 Record.push_back(N: A->getKind() + 1); // FIXME: stable encoding, target attrs
5224
5225 Record.AddIdentifierRef(II: A->getAttrName());
5226 Record.AddIdentifierRef(II: A->getScopeName());
5227 Record.AddSourceRange(Range: A->getRange());
5228 Record.AddSourceLocation(Loc: A->getScopeLoc());
5229 Record.push_back(N: A->getParsedKind());
5230 Record.push_back(N: A->getSyntax());
5231 Record.push_back(N: A->getAttributeSpellingListIndexRaw());
5232 Record.push_back(N: A->isRegularKeywordAttribute());
5233
5234#include "clang/Serialization/AttrPCHWrite.inc"
5235}
5236
5237/// Emit the list of attributes to the specified record.
5238void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
5239 push_back(N: Attrs.size());
5240 for (const auto *A : Attrs)
5241 AddAttr(A);
5242}
5243
5244void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
5245 AddSourceLocation(Loc: Tok.getLocation(), Record);
5246 // FIXME: Should translate token kind to a stable encoding.
5247 Record.push_back(Elt: Tok.getKind());
5248 // FIXME: Should translate token flags to a stable encoding.
5249 Record.push_back(Elt: Tok.getFlags());
5250
5251 if (Tok.isAnnotation()) {
5252 AddSourceLocation(Loc: Tok.getAnnotationEndLoc(), Record);
5253 switch (Tok.getKind()) {
5254 case tok::annot_pragma_loop_hint: {
5255 auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
5256 AddToken(Tok: Info->PragmaName, Record);
5257 AddToken(Tok: Info->Option, Record);
5258 Record.push_back(Elt: Info->Toks.size());
5259 for (const auto &T : Info->Toks)
5260 AddToken(Tok: T, Record);
5261 break;
5262 }
5263 case tok::annot_pragma_pack: {
5264 auto *Info =
5265 static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
5266 Record.push_back(Elt: static_cast<unsigned>(Info->Action));
5267 AddString(Str: Info->SlotLabel, Record);
5268 AddToken(Tok: Info->Alignment, Record);
5269 break;
5270 }
5271 // Some annotation tokens do not use the PtrData field.
5272 case tok::annot_pragma_openmp:
5273 case tok::annot_pragma_openmp_end:
5274 case tok::annot_pragma_unused:
5275 case tok::annot_pragma_openacc:
5276 case tok::annot_pragma_openacc_end:
5277 case tok::annot_repl_input_end:
5278 break;
5279 default:
5280 llvm_unreachable("missing serialization code for annotation token");
5281 }
5282 } else {
5283 Record.push_back(Elt: Tok.getLength());
5284 // FIXME: When reading literal tokens, reconstruct the literal pointer if it
5285 // is needed.
5286 AddIdentifierRef(II: Tok.getIdentifierInfo(), Record);
5287 }
5288}
5289
5290void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
5291 Record.push_back(Elt: Str.size());
5292 llvm::append_range(C&: Record, R&: Str);
5293}
5294
5295void ASTWriter::AddStringBlob(StringRef Str, RecordDataImpl &Record,
5296 SmallVectorImpl<char> &Blob) {
5297 Record.push_back(Elt: Str.size());
5298 llvm::append_range(C&: Blob, R&: Str);
5299}
5300
5301bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
5302 assert(WritingAST && "can't prepare path for output when not writing AST");
5303
5304 // Leave special file names as they are.
5305 StringRef PathStr(Path.data(), Path.size());
5306 if (PathStr == "<built-in>" || PathStr == "<command line>")
5307 return false;
5308
5309 bool Changed = cleanPathForOutput(FileMgr&: PP->getFileManager(), Path);
5310
5311 // Remove a prefix to make the path relative, if relevant.
5312 const char *PathBegin = Path.data();
5313 const char *PathPtr =
5314 adjustFilenameForRelocatableAST(Filename: PathBegin, BaseDir: BaseDirectory);
5315 if (PathPtr != PathBegin) {
5316 Path.erase(CS: Path.begin(), CE: Path.begin() + (PathPtr - PathBegin));
5317 Changed = true;
5318 }
5319
5320 return Changed;
5321}
5322
5323void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
5324 SmallString<128> FilePath(Path);
5325 PreparePathForOutput(Path&: FilePath);
5326 AddString(Str: FilePath, Record);
5327}
5328
5329void ASTWriter::AddPathBlob(StringRef Path, RecordDataImpl &Record,
5330 SmallVectorImpl<char> &Blob) {
5331 SmallString<128> FilePath(Path);
5332 PreparePathForOutput(Path&: FilePath);
5333 AddStringBlob(Str: FilePath, Record, Blob);
5334}
5335
5336void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
5337 StringRef Path) {
5338 SmallString<128> FilePath(Path);
5339 PreparePathForOutput(Path&: FilePath);
5340 Stream.EmitRecordWithBlob(Abbrev, Vals: Record, Blob: FilePath);
5341}
5342
5343void ASTWriter::AddVersionTuple(const VersionTuple &Version,
5344 RecordDataImpl &Record) {
5345 Record.push_back(Elt: Version.getMajor());
5346 if (std::optional<unsigned> Minor = Version.getMinor())
5347 Record.push_back(Elt: *Minor + 1);
5348 else
5349 Record.push_back(Elt: 0);
5350 if (std::optional<unsigned> Subminor = Version.getSubminor())
5351 Record.push_back(Elt: *Subminor + 1);
5352 else
5353 Record.push_back(Elt: 0);
5354}
5355
5356/// Note that the identifier II occurs at the given offset
5357/// within the identifier table.
5358void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
5359 IdentifierID ID = IdentifierIDs[II];
5360 // Only store offsets new to this AST file. Other identifier names are looked
5361 // up earlier in the chain and thus don't need an offset.
5362 if (!isLocalIdentifierID(ID))
5363 return;
5364
5365 // For local identifiers, the module file index must be 0.
5366
5367 assert(ID != 0);
5368 ID -= NUM_PREDEF_IDENT_IDS;
5369 assert(ID < IdentifierOffsets.size());
5370 IdentifierOffsets[ID] = Offset;
5371}
5372
5373/// Note that the selector Sel occurs at the given offset
5374/// within the method pool/selector table.
5375void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
5376 unsigned ID = SelectorIDs[Sel];
5377 assert(ID && "Unknown selector");
5378 // Don't record offsets for selectors that are also available in a different
5379 // file.
5380 if (ID < FirstSelectorID)
5381 return;
5382 SelectorOffsets[ID - FirstSelectorID] = Offset;
5383}
5384
5385ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
5386 SmallVectorImpl<char> &Buffer, ModuleCache &ModCache,
5387 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
5388 bool IncludeTimestamps, bool BuildingImplicitModule,
5389 bool GeneratingReducedBMI)
5390 : Stream(Stream), Buffer(Buffer), ModCache(ModCache),
5391 IncludeTimestamps(IncludeTimestamps),
5392 BuildingImplicitModule(BuildingImplicitModule),
5393 GeneratingReducedBMI(GeneratingReducedBMI) {
5394 for (const auto &Ext : Extensions) {
5395 if (auto Writer = Ext->createExtensionWriter(Writer&: *this))
5396 ModuleFileExtensionWriters.push_back(x: std::move(Writer));
5397 }
5398}
5399
5400ASTWriter::~ASTWriter() = default;
5401
5402const LangOptions &ASTWriter::getLangOpts() const {
5403 assert(WritingAST && "can't determine lang opts when not writing AST");
5404 return PP->getLangOpts();
5405}
5406
5407time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
5408 return IncludeTimestamps ? E->getModificationTime() : 0;
5409}
5410
5411ASTFileSignature
5412ASTWriter::WriteAST(llvm::PointerUnion<Sema *, Preprocessor *> Subject,
5413 StringRef OutputFile, Module *WritingModule,
5414 StringRef isysroot, bool ShouldCacheASTInMemory) {
5415 llvm::TimeTraceScope scope("WriteAST", OutputFile);
5416 WritingAST = true;
5417
5418 Sema *SemaPtr = dyn_cast<Sema *>(Val&: Subject);
5419 Preprocessor &PPRef =
5420 SemaPtr ? SemaPtr->getPreprocessor() : *cast<Preprocessor *>(Val&: Subject);
5421
5422 ASTHasCompilerErrors = PPRef.getDiagnostics().hasUncompilableErrorOccurred();
5423
5424 // Emit the file header.
5425 Stream.Emit(Val: (unsigned)'C', NumBits: 8);
5426 Stream.Emit(Val: (unsigned)'P', NumBits: 8);
5427 Stream.Emit(Val: (unsigned)'C', NumBits: 8);
5428 Stream.Emit(Val: (unsigned)'H', NumBits: 8);
5429
5430 WriteBlockInfoBlock();
5431
5432 PP = &PPRef;
5433 this->WritingModule = WritingModule;
5434 ASTFileSignature Signature = WriteASTCore(SemaPtr, isysroot, WritingModule);
5435 PP = nullptr;
5436 this->WritingModule = nullptr;
5437 this->BaseDirectory.clear();
5438
5439 WritingAST = false;
5440
5441 if (WritingModule && PPRef.getHeaderSearchInfo()
5442 .getHeaderSearchOpts()
5443 .ModulesValidateOncePerBuildSession)
5444 ModCache.updateModuleTimestamp(ModuleFilename: OutputFile);
5445
5446 if (ShouldCacheASTInMemory) {
5447 // Construct MemoryBuffer and update buffer manager.
5448 ModCache.getInMemoryModuleCache().addBuiltPCM(
5449 Filename: OutputFile, Buffer: llvm::MemoryBuffer::getMemBufferCopy(
5450 InputData: StringRef(Buffer.begin(), Buffer.size())));
5451 }
5452 return Signature;
5453}
5454
5455template<typename Vector>
5456static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec) {
5457 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
5458 I != E; ++I) {
5459 Writer.GetDeclRef(D: *I);
5460 }
5461}
5462
5463template <typename Vector>
5464static void AddLazyVectorEmiitedDecls(ASTWriter &Writer, Vector &Vec,
5465 ASTWriter::RecordData &Record) {
5466 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
5467 I != E; ++I) {
5468 Writer.AddEmittedDeclRef(D: *I, Record);
5469 }
5470}
5471
5472void ASTWriter::computeNonAffectingInputFiles() {
5473 SourceManager &SrcMgr = PP->getSourceManager();
5474 unsigned N = SrcMgr.local_sloc_entry_size();
5475
5476 IsSLocAffecting.resize(N, t: true);
5477 IsSLocFileEntryAffecting.resize(N, t: true);
5478
5479 if (!WritingModule)
5480 return;
5481
5482 auto AffectingModuleMaps = GetAffectingModuleMaps(PP: *PP, RootModule: WritingModule);
5483
5484 unsigned FileIDAdjustment = 0;
5485 unsigned OffsetAdjustment = 0;
5486
5487 NonAffectingFileIDAdjustments.reserve(n: N);
5488 NonAffectingOffsetAdjustments.reserve(n: N);
5489
5490 NonAffectingFileIDAdjustments.push_back(x: FileIDAdjustment);
5491 NonAffectingOffsetAdjustments.push_back(x: OffsetAdjustment);
5492
5493 for (unsigned I = 1; I != N; ++I) {
5494 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(Index: I);
5495 FileID FID = FileID::get(V: I);
5496 assert(&SrcMgr.getSLocEntry(FID) == SLoc);
5497
5498 if (!SLoc->isFile())
5499 continue;
5500 const SrcMgr::FileInfo &File = SLoc->getFile();
5501 const SrcMgr::ContentCache *Cache = &File.getContentCache();
5502 if (!Cache->OrigEntry)
5503 continue;
5504
5505 // Don't prune anything other than module maps.
5506 if (!isModuleMap(CK: File.getFileCharacteristic()))
5507 continue;
5508
5509 // Don't prune module maps if all are guaranteed to be affecting.
5510 if (!AffectingModuleMaps)
5511 continue;
5512
5513 // Don't prune module maps that are affecting.
5514 if (AffectingModuleMaps->DefinitionFileIDs.contains(V: FID))
5515 continue;
5516
5517 IsSLocAffecting[I] = false;
5518 IsSLocFileEntryAffecting[I] =
5519 AffectingModuleMaps->DefinitionFiles.contains(V: *Cache->OrigEntry);
5520
5521 FileIDAdjustment += 1;
5522 // Even empty files take up one element in the offset table.
5523 OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
5524
5525 // If the previous file was non-affecting as well, just extend its entry
5526 // with our information.
5527 if (!NonAffectingFileIDs.empty() &&
5528 NonAffectingFileIDs.back().ID == FID.ID - 1) {
5529 NonAffectingFileIDs.back() = FID;
5530 NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
5531 NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
5532 NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
5533 continue;
5534 }
5535
5536 NonAffectingFileIDs.push_back(x: FID);
5537 NonAffectingRanges.emplace_back(args: SrcMgr.getLocForStartOfFile(FID),
5538 args: SrcMgr.getLocForEndOfFile(FID));
5539 NonAffectingFileIDAdjustments.push_back(x: FileIDAdjustment);
5540 NonAffectingOffsetAdjustments.push_back(x: OffsetAdjustment);
5541 }
5542
5543 if (!PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesIncludeVFSUsage)
5544 return;
5545
5546 FileManager &FileMgr = PP->getFileManager();
5547 FileMgr.trackVFSUsage(Active: true);
5548 // Lookup the paths in the VFS to trigger `-ivfsoverlay` usage tracking.
5549 for (StringRef Path :
5550 PP->getHeaderSearchInfo().getHeaderSearchOpts().VFSOverlayFiles)
5551 FileMgr.getVirtualFileSystem().exists(Path);
5552 for (unsigned I = 1; I != N; ++I) {
5553 if (IsSLocAffecting[I]) {
5554 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(Index: I);
5555 if (!SLoc->isFile())
5556 continue;
5557 const SrcMgr::FileInfo &File = SLoc->getFile();
5558 const SrcMgr::ContentCache *Cache = &File.getContentCache();
5559 if (!Cache->OrigEntry)
5560 continue;
5561 FileMgr.getVirtualFileSystem().exists(
5562 Path: Cache->OrigEntry->getNameAsRequested());
5563 }
5564 }
5565 FileMgr.trackVFSUsage(Active: false);
5566}
5567
5568void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
5569 ASTContext &Context = SemaRef.Context;
5570
5571 bool isModule = WritingModule != nullptr;
5572
5573 // Set up predefined declaration IDs.
5574 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
5575 if (D) {
5576 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
5577 DeclIDs[D] = ID;
5578 PredefinedDecls.insert(Ptr: D);
5579 }
5580 };
5581 RegisterPredefDecl(Context.getTranslationUnitDecl(),
5582 PREDEF_DECL_TRANSLATION_UNIT_ID);
5583 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
5584 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
5585 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
5586 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
5587 PREDEF_DECL_OBJC_PROTOCOL_ID);
5588 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
5589 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
5590 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
5591 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
5592 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
5593 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
5594 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
5595 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
5596 RegisterPredefDecl(Context.MSGuidTagDecl,
5597 PREDEF_DECL_BUILTIN_MS_GUID_ID);
5598 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
5599 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
5600 PREDEF_DECL_CF_CONSTANT_STRING_ID);
5601 RegisterPredefDecl(Context.CFConstantStringTagDecl,
5602 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
5603#define BuiltinTemplate(BTName) \
5604 RegisterPredefDecl(Context.Decl##BTName, PREDEF_DECL##BTName##_ID);
5605#include "clang/Basic/BuiltinTemplates.inc"
5606
5607 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5608
5609 // Force all top level declarations to be emitted.
5610 //
5611 // We start emitting top level declarations from the module purview to
5612 // implement the eliding unreachable declaration feature.
5613 for (const auto *D : TU->noload_decls()) {
5614 if (D->isFromASTFile())
5615 continue;
5616
5617 if (GeneratingReducedBMI) {
5618 if (D->isFromExplicitGlobalModule())
5619 continue;
5620
5621 // Don't force emitting static entities.
5622 //
5623 // Technically, all static entities shouldn't be in reduced BMI. The
5624 // language also specifies that the program exposes TU-local entities
5625 // is ill-formed. However, in practice, there are a lot of projects
5626 // uses `static inline` in the headers. So we can't get rid of all
5627 // static entities in reduced BMI now.
5628 if (IsInternalDeclFromFileContext(D))
5629 continue;
5630 }
5631
5632 // If we're writing C++ named modules, don't emit declarations which are
5633 // not from modules by default. They may be built in declarations (be
5634 // handled above) or implcit declarations (see the implementation of
5635 // `Sema::Initialize()` for example).
5636 if (isWritingStdCXXNamedModules() && !D->getOwningModule() &&
5637 D->isImplicit())
5638 continue;
5639
5640 GetDeclRef(D);
5641 }
5642
5643 if (GeneratingReducedBMI)
5644 return;
5645
5646 // Writing all of the tentative definitions in this file, in
5647 // TentativeDefinitions order. Generally, this record will be empty for
5648 // headers.
5649 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.TentativeDefinitions);
5650
5651 // Writing all of the file scoped decls in this file.
5652 if (!isModule)
5653 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.UnusedFileScopedDecls);
5654
5655 // Writing all of the delegating constructors we still need
5656 // to resolve.
5657 if (!isModule)
5658 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.DelegatingCtorDecls);
5659
5660 // Writing all of the ext_vector declarations.
5661 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.ExtVectorDecls);
5662
5663 // Writing all of the VTable uses information.
5664 if (!SemaRef.VTableUses.empty())
5665 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I)
5666 GetDeclRef(D: SemaRef.VTableUses[I].first);
5667
5668 // Writing all of the UnusedLocalTypedefNameCandidates.
5669 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5670 GetDeclRef(D: TD);
5671
5672 // Writing all of pending implicit instantiations.
5673 for (const auto &I : SemaRef.PendingInstantiations)
5674 GetDeclRef(D: I.first);
5675 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
5676 "There are local ones at end of translation unit!");
5677
5678 // Writing some declaration references.
5679 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5680 GetDeclRef(D: SemaRef.getStdNamespace());
5681 GetDeclRef(D: SemaRef.getStdBadAlloc());
5682 GetDeclRef(D: SemaRef.getStdAlignValT());
5683 }
5684
5685 if (Context.getcudaConfigureCallDecl())
5686 GetDeclRef(D: Context.getcudaConfigureCallDecl());
5687
5688 // Writing all of the known namespaces.
5689 for (const auto &I : SemaRef.KnownNamespaces)
5690 if (!I.second)
5691 GetDeclRef(D: I.first);
5692
5693 // Writing all used, undefined objects that require definitions.
5694 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5695 SemaRef.getUndefinedButUsed(Undefined);
5696 for (const auto &I : Undefined)
5697 GetDeclRef(D: I.first);
5698
5699 // Writing all delete-expressions that we would like to
5700 // analyze later in AST.
5701 if (!isModule)
5702 for (const auto &DeleteExprsInfo :
5703 SemaRef.getMismatchingDeleteExpressions())
5704 GetDeclRef(D: DeleteExprsInfo.first);
5705
5706 // Make sure visible decls, added to DeclContexts previously loaded from
5707 // an AST file, are registered for serialization. Likewise for template
5708 // specializations added to imported templates.
5709 for (const auto *I : DeclsToEmitEvenIfUnreferenced)
5710 GetDeclRef(D: I);
5711 DeclsToEmitEvenIfUnreferenced.clear();
5712
5713 // Make sure all decls associated with an identifier are registered for
5714 // serialization, if we're storing decls with identifiers.
5715 if (!WritingModule || !getLangOpts().CPlusPlus) {
5716 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
5717 for (const auto &ID : SemaRef.PP.getIdentifierTable()) {
5718 const IdentifierInfo *II = ID.second;
5719 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization() ||
5720 II->hasFETokenInfoChangedSinceDeserialization())
5721 IIs.push_back(Elt: II);
5722 }
5723 // Sort the identifiers to visit based on their name.
5724 llvm::sort(C&: IIs, Comp: llvm::deref<std::less<>>());
5725 const LangOptions &LangOpts = getLangOpts();
5726 for (const IdentifierInfo *II : IIs)
5727 for (NamedDecl *D : SemaRef.IdResolver.decls(Name: II))
5728 GetDeclRef(D: getDeclForLocalLookup(LangOpts, D));
5729 }
5730
5731 // Write all of the DeclsToCheckForDeferredDiags.
5732 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5733 GetDeclRef(D);
5734
5735 // Write all classes that need to emit the vtable definitions if required.
5736 if (isWritingStdCXXNamedModules())
5737 for (CXXRecordDecl *RD : PendingEmittingVTables)
5738 GetDeclRef(D: RD);
5739 else
5740 PendingEmittingVTables.clear();
5741}
5742
5743void ASTWriter::WriteSpecialDeclRecords(Sema &SemaRef) {
5744 ASTContext &Context = SemaRef.Context;
5745
5746 bool isModule = WritingModule != nullptr;
5747
5748 // Write the record containing external, unnamed definitions.
5749 if (!EagerlyDeserializedDecls.empty())
5750 Stream.EmitRecord(Code: EAGERLY_DESERIALIZED_DECLS, Vals: EagerlyDeserializedDecls);
5751
5752 if (!ModularCodegenDecls.empty())
5753 Stream.EmitRecord(Code: MODULAR_CODEGEN_DECLS, Vals: ModularCodegenDecls);
5754
5755 // Write the record containing tentative definitions.
5756 RecordData TentativeDefinitions;
5757 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.TentativeDefinitions,
5758 Record&: TentativeDefinitions);
5759 if (!TentativeDefinitions.empty())
5760 Stream.EmitRecord(Code: TENTATIVE_DEFINITIONS, Vals: TentativeDefinitions);
5761
5762 // Write the record containing unused file scoped decls.
5763 RecordData UnusedFileScopedDecls;
5764 if (!isModule)
5765 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.UnusedFileScopedDecls,
5766 Record&: UnusedFileScopedDecls);
5767 if (!UnusedFileScopedDecls.empty())
5768 Stream.EmitRecord(Code: UNUSED_FILESCOPED_DECLS, Vals: UnusedFileScopedDecls);
5769
5770 // Write the record containing ext_vector type names.
5771 RecordData ExtVectorDecls;
5772 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.ExtVectorDecls, Record&: ExtVectorDecls);
5773 if (!ExtVectorDecls.empty())
5774 Stream.EmitRecord(Code: EXT_VECTOR_DECLS, Vals: ExtVectorDecls);
5775
5776 // Write the record containing VTable uses information.
5777 RecordData VTableUses;
5778 if (!SemaRef.VTableUses.empty()) {
5779 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
5780 CXXRecordDecl *D = SemaRef.VTableUses[I].first;
5781 if (!wasDeclEmitted(D))
5782 continue;
5783
5784 AddDeclRef(D, Record&: VTableUses);
5785 AddSourceLocation(Loc: SemaRef.VTableUses[I].second, Record&: VTableUses);
5786 VTableUses.push_back(Elt: SemaRef.VTablesUsed[D]);
5787 }
5788 Stream.EmitRecord(Code: VTABLE_USES, Vals: VTableUses);
5789 }
5790
5791 // Write the record containing potentially unused local typedefs.
5792 RecordData UnusedLocalTypedefNameCandidates;
5793 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5794 AddEmittedDeclRef(D: TD, Record&: UnusedLocalTypedefNameCandidates);
5795 if (!UnusedLocalTypedefNameCandidates.empty())
5796 Stream.EmitRecord(Code: UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5797 Vals: UnusedLocalTypedefNameCandidates);
5798
5799 // Write the record containing pending implicit instantiations.
5800 RecordData PendingInstantiations;
5801 for (const auto &I : SemaRef.PendingInstantiations) {
5802 if (!wasDeclEmitted(D: I.first))
5803 continue;
5804
5805 AddDeclRef(D: I.first, Record&: PendingInstantiations);
5806 AddSourceLocation(Loc: I.second, Record&: PendingInstantiations);
5807 }
5808 if (!PendingInstantiations.empty())
5809 Stream.EmitRecord(Code: PENDING_IMPLICIT_INSTANTIATIONS, Vals: PendingInstantiations);
5810
5811 // Write the record containing declaration references of Sema.
5812 RecordData SemaDeclRefs;
5813 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5814 auto AddEmittedDeclRefOrZero = [this, &SemaDeclRefs](Decl *D) {
5815 if (!D || !wasDeclEmitted(D))
5816 SemaDeclRefs.push_back(Elt: 0);
5817 else
5818 AddDeclRef(D, Record&: SemaDeclRefs);
5819 };
5820
5821 AddEmittedDeclRefOrZero(SemaRef.getStdNamespace());
5822 AddEmittedDeclRefOrZero(SemaRef.getStdBadAlloc());
5823 AddEmittedDeclRefOrZero(SemaRef.getStdAlignValT());
5824 }
5825 if (!SemaDeclRefs.empty())
5826 Stream.EmitRecord(Code: SEMA_DECL_REFS, Vals: SemaDeclRefs);
5827
5828 // Write the record containing decls to be checked for deferred diags.
5829 RecordData DeclsToCheckForDeferredDiags;
5830 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5831 if (wasDeclEmitted(D))
5832 AddDeclRef(D, Record&: DeclsToCheckForDeferredDiags);
5833 if (!DeclsToCheckForDeferredDiags.empty())
5834 Stream.EmitRecord(Code: DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5835 Vals: DeclsToCheckForDeferredDiags);
5836
5837 // Write the record containing CUDA-specific declaration references.
5838 RecordData CUDASpecialDeclRefs;
5839 if (auto *CudaCallDecl = Context.getcudaConfigureCallDecl();
5840 CudaCallDecl && wasDeclEmitted(D: CudaCallDecl)) {
5841 AddDeclRef(D: CudaCallDecl, Record&: CUDASpecialDeclRefs);
5842 Stream.EmitRecord(Code: CUDA_SPECIAL_DECL_REFS, Vals: CUDASpecialDeclRefs);
5843 }
5844
5845 // Write the delegating constructors.
5846 RecordData DelegatingCtorDecls;
5847 if (!isModule)
5848 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.DelegatingCtorDecls,
5849 Record&: DelegatingCtorDecls);
5850 if (!DelegatingCtorDecls.empty())
5851 Stream.EmitRecord(Code: DELEGATING_CTORS, Vals: DelegatingCtorDecls);
5852
5853 // Write the known namespaces.
5854 RecordData KnownNamespaces;
5855 for (const auto &I : SemaRef.KnownNamespaces) {
5856 if (!I.second && wasDeclEmitted(D: I.first))
5857 AddDeclRef(D: I.first, Record&: KnownNamespaces);
5858 }
5859 if (!KnownNamespaces.empty())
5860 Stream.EmitRecord(Code: KNOWN_NAMESPACES, Vals: KnownNamespaces);
5861
5862 // Write the undefined internal functions and variables, and inline functions.
5863 RecordData UndefinedButUsed;
5864 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5865 SemaRef.getUndefinedButUsed(Undefined);
5866 for (const auto &I : Undefined) {
5867 if (!wasDeclEmitted(D: I.first))
5868 continue;
5869
5870 AddDeclRef(D: I.first, Record&: UndefinedButUsed);
5871 AddSourceLocation(Loc: I.second, Record&: UndefinedButUsed);
5872 }
5873 if (!UndefinedButUsed.empty())
5874 Stream.EmitRecord(Code: UNDEFINED_BUT_USED, Vals: UndefinedButUsed);
5875
5876 // Write all delete-expressions that we would like to
5877 // analyze later in AST.
5878 RecordData DeleteExprsToAnalyze;
5879 if (!isModule) {
5880 for (const auto &DeleteExprsInfo :
5881 SemaRef.getMismatchingDeleteExpressions()) {
5882 if (!wasDeclEmitted(D: DeleteExprsInfo.first))
5883 continue;
5884
5885 AddDeclRef(D: DeleteExprsInfo.first, Record&: DeleteExprsToAnalyze);
5886 DeleteExprsToAnalyze.push_back(Elt: DeleteExprsInfo.second.size());
5887 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
5888 AddSourceLocation(Loc: DeleteLoc.first, Record&: DeleteExprsToAnalyze);
5889 DeleteExprsToAnalyze.push_back(Elt: DeleteLoc.second);
5890 }
5891 }
5892 }
5893 if (!DeleteExprsToAnalyze.empty())
5894 Stream.EmitRecord(Code: DELETE_EXPRS_TO_ANALYZE, Vals: DeleteExprsToAnalyze);
5895
5896 RecordData VTablesToEmit;
5897 for (CXXRecordDecl *RD : PendingEmittingVTables) {
5898 if (!wasDeclEmitted(D: RD))
5899 continue;
5900
5901 AddDeclRef(D: RD, Record&: VTablesToEmit);
5902 }
5903
5904 if (!VTablesToEmit.empty())
5905 Stream.EmitRecord(Code: VTABLES_TO_EMIT, Vals: VTablesToEmit);
5906}
5907
5908ASTFileSignature ASTWriter::WriteASTCore(Sema *SemaPtr, StringRef isysroot,
5909 Module *WritingModule) {
5910 using namespace llvm;
5911
5912 bool isModule = WritingModule != nullptr;
5913
5914 // Make sure that the AST reader knows to finalize itself.
5915 if (Chain)
5916 Chain->finalizeForWriting();
5917
5918 // This needs to be done very early, since everything that writes
5919 // SourceLocations or FileIDs depends on it.
5920 computeNonAffectingInputFiles();
5921
5922 writeUnhashedControlBlock(PP&: *PP);
5923
5924 // Don't reuse type ID and Identifier ID from readers for C++ standard named
5925 // modules since we want to support no-transitive-change model for named
5926 // modules. The theory for no-transitive-change model is,
5927 // for a user of a named module, the user can only access the indirectly
5928 // imported decls via the directly imported module. So that it is possible to
5929 // control what matters to the users when writing the module. It would be
5930 // problematic if the users can reuse the type IDs and identifier IDs from
5931 // indirectly imported modules arbitrarily. So we choose to clear these ID
5932 // here.
5933 if (isWritingStdCXXNamedModules()) {
5934 TypeIdxs.clear();
5935 IdentifierIDs.clear();
5936 }
5937
5938 // Look for any identifiers that were named while processing the
5939 // headers, but are otherwise not needed. We add these to the hash
5940 // table to enable checking of the predefines buffer in the case
5941 // where the user adds new macro definitions when building the AST
5942 // file.
5943 //
5944 // We do this before emitting any Decl and Types to make sure the
5945 // Identifier ID is stable.
5946 SmallVector<const IdentifierInfo *, 128> IIs;
5947 for (const auto &ID : PP->getIdentifierTable())
5948 if (IsInterestingNonMacroIdentifier(II: ID.second, Writer&: *this))
5949 IIs.push_back(Elt: ID.second);
5950 // Sort the identifiers lexicographically before getting the references so
5951 // that their order is stable.
5952 llvm::sort(C&: IIs, Comp: llvm::deref<std::less<>>());
5953 for (const IdentifierInfo *II : IIs)
5954 getIdentifierRef(II);
5955
5956 // Write the set of weak, undeclared identifiers. We always write the
5957 // entire table, since later PCH files in a PCH chain are only interested in
5958 // the results at the end of the chain.
5959 RecordData WeakUndeclaredIdentifiers;
5960 if (SemaPtr) {
5961 for (const auto &WeakUndeclaredIdentifierList :
5962 SemaPtr->WeakUndeclaredIdentifiers) {
5963 const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
5964 for (const auto &WI : WeakUndeclaredIdentifierList.second) {
5965 AddIdentifierRef(II, Record&: WeakUndeclaredIdentifiers);
5966 AddIdentifierRef(II: WI.getAlias(), Record&: WeakUndeclaredIdentifiers);
5967 AddSourceLocation(Loc: WI.getLocation(), Record&: WeakUndeclaredIdentifiers);
5968 }
5969 }
5970 }
5971
5972 // Form the record of special types.
5973 RecordData SpecialTypes;
5974 if (SemaPtr) {
5975 ASTContext &Context = SemaPtr->Context;
5976 AddTypeRef(Context, T: Context.getRawCFConstantStringType(), Record&: SpecialTypes);
5977 AddTypeRef(Context, T: Context.getFILEType(), Record&: SpecialTypes);
5978 AddTypeRef(Context, T: Context.getjmp_bufType(), Record&: SpecialTypes);
5979 AddTypeRef(Context, T: Context.getsigjmp_bufType(), Record&: SpecialTypes);
5980 AddTypeRef(Context, T: Context.ObjCIdRedefinitionType, Record&: SpecialTypes);
5981 AddTypeRef(Context, T: Context.ObjCClassRedefinitionType, Record&: SpecialTypes);
5982 AddTypeRef(Context, T: Context.ObjCSelRedefinitionType, Record&: SpecialTypes);
5983 AddTypeRef(Context, T: Context.getucontext_tType(), Record&: SpecialTypes);
5984 }
5985
5986 if (SemaPtr)
5987 PrepareWritingSpecialDecls(SemaRef&: *SemaPtr);
5988
5989 // Write the control block
5990 WriteControlBlock(PP&: *PP, isysroot);
5991
5992 // Write the remaining AST contents.
5993 Stream.FlushToWord();
5994 ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
5995 Stream.EnterSubblock(BlockID: AST_BLOCK_ID, CodeLen: 5);
5996 ASTBlockStartOffset = Stream.GetCurrentBitNo();
5997
5998 // This is so that older clang versions, before the introduction
5999 // of the control block, can read and reject the newer PCH format.
6000 {
6001 RecordData Record = {VERSION_MAJOR};
6002 Stream.EmitRecord(Code: METADATA_OLD_FORMAT, Vals: Record);
6003 }
6004
6005 // For method pool in the module, if it contains an entry for a selector,
6006 // the entry should be complete, containing everything introduced by that
6007 // module and all modules it imports. It's possible that the entry is out of
6008 // date, so we need to pull in the new content here.
6009
6010 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
6011 // safe, we copy all selectors out.
6012 if (SemaPtr) {
6013 llvm::SmallVector<Selector, 256> AllSelectors;
6014 for (auto &SelectorAndID : SelectorIDs)
6015 AllSelectors.push_back(Elt: SelectorAndID.first);
6016 for (auto &Selector : AllSelectors)
6017 SemaPtr->ObjC().updateOutOfDateSelector(Sel: Selector);
6018 }
6019
6020 if (Chain) {
6021 // Write the mapping information describing our module dependencies and how
6022 // each of those modules were mapped into our own offset/ID space, so that
6023 // the reader can build the appropriate mapping to its own offset/ID space.
6024 // The map consists solely of a blob with the following format:
6025 // *(module-kind:i8
6026 // module-name-len:i16 module-name:len*i8
6027 // source-location-offset:i32
6028 // identifier-id:i32
6029 // preprocessed-entity-id:i32
6030 // macro-definition-id:i32
6031 // submodule-id:i32
6032 // selector-id:i32
6033 // declaration-id:i32
6034 // c++-base-specifiers-id:i32
6035 // type-id:i32)
6036 //
6037 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
6038 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
6039 // module name. Otherwise, it is the module file name.
6040 auto Abbrev = std::make_shared<BitCodeAbbrev>();
6041 Abbrev->Add(OpInfo: BitCodeAbbrevOp(MODULE_OFFSET_MAP));
6042 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
6043 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
6044 SmallString<2048> Buffer;
6045 {
6046 llvm::raw_svector_ostream Out(Buffer);
6047 for (ModuleFile &M : Chain->ModuleMgr) {
6048 using namespace llvm::support;
6049
6050 endian::Writer LE(Out, llvm::endianness::little);
6051 LE.write<uint8_t>(Val: static_cast<uint8_t>(M.Kind));
6052 StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
6053 LE.write<uint16_t>(Val: Name.size());
6054 Out.write(Ptr: Name.data(), Size: Name.size());
6055
6056 // Note: if a base ID was uint max, it would not be possible to load
6057 // another module after it or have more than one entity inside it.
6058 uint32_t None = std::numeric_limits<uint32_t>::max();
6059
6060 auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
6061 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
6062 if (ShouldWrite)
6063 LE.write<uint32_t>(BaseID);
6064 else
6065 LE.write<uint32_t>(Val: None);
6066 };
6067
6068 // These values should be unique within a chain, since they will be read
6069 // as keys into ContinuousRangeMaps.
6070 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
6071 writeBaseIDOrNone(M.BasePreprocessedEntityID,
6072 M.NumPreprocessedEntities);
6073 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
6074 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
6075 }
6076 }
6077 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
6078 Stream.EmitRecordWithBlob(Abbrev: ModuleOffsetMapAbbrev, Vals: Record,
6079 BlobData: Buffer.data(), BlobLen: Buffer.size());
6080 }
6081
6082 if (SemaPtr)
6083 WriteDeclAndTypes(Context&: SemaPtr->Context);
6084
6085 WriteFileDeclIDsMap();
6086 WriteSourceManagerBlock(SourceMgr&: PP->getSourceManager());
6087 if (SemaPtr)
6088 WriteComments(Context&: SemaPtr->Context);
6089 WritePreprocessor(PP: *PP, IsModule: isModule);
6090 WriteHeaderSearch(HS: PP->getHeaderSearchInfo());
6091 if (SemaPtr) {
6092 WriteSelectors(SemaRef&: *SemaPtr);
6093 WriteReferencedSelectorsPool(SemaRef&: *SemaPtr);
6094 WriteLateParsedTemplates(SemaRef&: *SemaPtr);
6095 }
6096 WriteIdentifierTable(PP&: *PP, IdResolver: SemaPtr ? &SemaPtr->IdResolver : nullptr, IsModule: isModule);
6097 if (SemaPtr) {
6098 WriteFPPragmaOptions(Opts: SemaPtr->CurFPFeatureOverrides());
6099 WriteOpenCLExtensions(SemaRef&: *SemaPtr);
6100 WriteCUDAPragmas(SemaRef&: *SemaPtr);
6101 }
6102
6103 // If we're emitting a module, write out the submodule information.
6104 if (WritingModule)
6105 WriteSubmodules(WritingModule, Context: SemaPtr ? &SemaPtr->Context : nullptr);
6106
6107 Stream.EmitRecord(Code: SPECIAL_TYPES, Vals: SpecialTypes);
6108
6109 if (SemaPtr)
6110 WriteSpecialDeclRecords(SemaRef&: *SemaPtr);
6111
6112 // Write the record containing weak undeclared identifiers.
6113 if (!WeakUndeclaredIdentifiers.empty())
6114 Stream.EmitRecord(Code: WEAK_UNDECLARED_IDENTIFIERS,
6115 Vals: WeakUndeclaredIdentifiers);
6116
6117 if (!WritingModule) {
6118 // Write the submodules that were imported, if any.
6119 struct ModuleInfo {
6120 uint64_t ID;
6121 Module *M;
6122 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
6123 };
6124 llvm::SmallVector<ModuleInfo, 64> Imports;
6125 if (SemaPtr) {
6126 for (const auto *I : SemaPtr->Context.local_imports()) {
6127 assert(SubmoduleIDs.contains(I->getImportedModule()));
6128 Imports.push_back(Elt: ModuleInfo(SubmoduleIDs[I->getImportedModule()],
6129 I->getImportedModule()));
6130 }
6131 }
6132
6133 if (!Imports.empty()) {
6134 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
6135 return A.ID < B.ID;
6136 };
6137 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
6138 return A.ID == B.ID;
6139 };
6140
6141 // Sort and deduplicate module IDs.
6142 llvm::sort(C&: Imports, Comp: Cmp);
6143 Imports.erase(CS: llvm::unique(R&: Imports, P: Eq), CE: Imports.end());
6144
6145 RecordData ImportedModules;
6146 for (const auto &Import : Imports) {
6147 ImportedModules.push_back(Elt: Import.ID);
6148 // FIXME: If the module has macros imported then later has declarations
6149 // imported, this location won't be the right one as a location for the
6150 // declaration imports.
6151 AddSourceLocation(Loc: PP->getModuleImportLoc(M: Import.M), Record&: ImportedModules);
6152 }
6153
6154 Stream.EmitRecord(Code: IMPORTED_MODULES, Vals: ImportedModules);
6155 }
6156 }
6157
6158 WriteObjCCategories();
6159 if (SemaPtr) {
6160 if (!WritingModule) {
6161 WriteOptimizePragmaOptions(SemaRef&: *SemaPtr);
6162 WriteMSStructPragmaOptions(SemaRef&: *SemaPtr);
6163 WriteMSPointersToMembersPragmaOptions(SemaRef&: *SemaPtr);
6164 }
6165 WritePackPragmaOptions(SemaRef&: *SemaPtr);
6166 WriteFloatControlPragmaOptions(SemaRef&: *SemaPtr);
6167 WriteDeclsWithEffectsToVerify(SemaRef&: *SemaPtr);
6168 }
6169
6170 // Some simple statistics
6171 RecordData::value_type Record[] = {NumStatements,
6172 NumMacros,
6173 NumLexicalDeclContexts,
6174 NumVisibleDeclContexts,
6175 NumModuleLocalDeclContexts,
6176 NumTULocalDeclContexts};
6177 Stream.EmitRecord(Code: STATISTICS, Vals: Record);
6178 Stream.ExitBlock();
6179 Stream.FlushToWord();
6180 ASTBlockRange.second = Stream.GetCurrentBitNo() >> 3;
6181
6182 // Write the module file extension blocks.
6183 if (SemaPtr)
6184 for (const auto &ExtWriter : ModuleFileExtensionWriters)
6185 WriteModuleFileExtension(SemaRef&: *SemaPtr, Writer&: *ExtWriter);
6186
6187 return backpatchSignature();
6188}
6189
6190void ASTWriter::EnteringModulePurview() {
6191 // In C++20 named modules, all entities before entering the module purview
6192 // lives in the GMF.
6193 if (GeneratingReducedBMI)
6194 DeclUpdatesFromGMF.swap(RHS&: DeclUpdates);
6195}
6196
6197// Add update records for all mangling numbers and static local numbers.
6198// These aren't really update records, but this is a convenient way of
6199// tagging this rare extra data onto the declarations.
6200void ASTWriter::AddedManglingNumber(const Decl *D, unsigned Number) {
6201 if (D->isFromASTFile())
6202 return;
6203
6204 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::ManglingNumber, Number));
6205}
6206void ASTWriter::AddedStaticLocalNumbers(const Decl *D, unsigned Number) {
6207 if (D->isFromASTFile())
6208 return;
6209
6210 DeclUpdates[D].push_back(
6211 Elt: DeclUpdate(DeclUpdateKind::StaticLocalNumber, Number));
6212}
6213
6214void ASTWriter::AddedAnonymousNamespace(const TranslationUnitDecl *TU,
6215 NamespaceDecl *AnonNamespace) {
6216 // If the translation unit has an anonymous namespace, and we don't already
6217 // have an update block for it, write it as an update block.
6218 // FIXME: Why do we not do this if there's already an update block?
6219 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
6220 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
6221 if (Record.empty())
6222 Record.push_back(
6223 Elt: DeclUpdate(DeclUpdateKind::CXXAddedAnonymousNamespace, NS));
6224 }
6225}
6226
6227void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
6228 // Keep writing types, declarations, and declaration update records
6229 // until we've emitted all of them.
6230 RecordData DeclUpdatesOffsetsRecord;
6231 Stream.EnterSubblock(BlockID: DECLTYPES_BLOCK_ID, /*bits for abbreviations*/ CodeLen: 6);
6232 DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
6233 WriteTypeAbbrevs();
6234 WriteDeclAbbrevs();
6235 do {
6236 WriteDeclUpdatesBlocks(Context, OffsetsRecord&: DeclUpdatesOffsetsRecord);
6237 while (!DeclTypesToEmit.empty()) {
6238 DeclOrType DOT = DeclTypesToEmit.front();
6239 DeclTypesToEmit.pop();
6240 if (DOT.isType())
6241 WriteType(Context, T: DOT.getType());
6242 else
6243 WriteDecl(Context, D: DOT.getDecl());
6244 }
6245 } while (!DeclUpdates.empty());
6246
6247 DoneWritingDeclsAndTypes = true;
6248
6249 // DelayedNamespace is only meaningful in reduced BMI.
6250 // See the comments of DelayedNamespace for details.
6251 assert(DelayedNamespace.empty() || GeneratingReducedBMI);
6252 RecordData DelayedNamespaceRecord;
6253 for (NamespaceDecl *NS : DelayedNamespace) {
6254 LookupBlockOffsets Offsets;
6255
6256 Offsets.LexicalOffset = WriteDeclContextLexicalBlock(Context, DC: NS);
6257 WriteDeclContextVisibleBlock(Context, DC: NS, Offsets);
6258
6259 if (Offsets.LexicalOffset)
6260 Offsets.LexicalOffset -= DeclTypesBlockStartOffset;
6261
6262 // Write the offset relative to current block.
6263 if (Offsets.VisibleOffset)
6264 Offsets.VisibleOffset -= DeclTypesBlockStartOffset;
6265
6266 if (Offsets.ModuleLocalOffset)
6267 Offsets.ModuleLocalOffset -= DeclTypesBlockStartOffset;
6268
6269 if (Offsets.TULocalOffset)
6270 Offsets.TULocalOffset -= DeclTypesBlockStartOffset;
6271
6272 AddDeclRef(D: NS, Record&: DelayedNamespaceRecord);
6273 AddLookupOffsets(Offsets, Record&: DelayedNamespaceRecord);
6274 }
6275
6276 // The process of writing lexical and visible block for delayed namespace
6277 // shouldn't introduce any new decls, types or update to emit.
6278 assert(DeclTypesToEmit.empty());
6279 assert(DeclUpdates.empty());
6280
6281 Stream.ExitBlock();
6282
6283 // These things can only be done once we've written out decls and types.
6284 WriteTypeDeclOffsets();
6285 if (!DeclUpdatesOffsetsRecord.empty())
6286 Stream.EmitRecord(Code: DECL_UPDATE_OFFSETS, Vals: DeclUpdatesOffsetsRecord);
6287
6288 if (!DelayedNamespaceRecord.empty())
6289 Stream.EmitRecord(Code: DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD,
6290 Vals: DelayedNamespaceRecord);
6291
6292 if (!RelatedDeclsMap.empty()) {
6293 // TODO: on disk hash table for related decls mapping might be more
6294 // efficent becuase it allows lazy deserialization.
6295 RecordData RelatedDeclsMapRecord;
6296 for (const auto &Pair : RelatedDeclsMap) {
6297 RelatedDeclsMapRecord.push_back(Elt: Pair.first.getRawValue());
6298 RelatedDeclsMapRecord.push_back(Elt: Pair.second.size());
6299 for (const auto &Lambda : Pair.second)
6300 RelatedDeclsMapRecord.push_back(Elt: Lambda.getRawValue());
6301 }
6302
6303 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
6304 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(RELATED_DECLS_MAP));
6305 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Array));
6306 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6307 unsigned FunctionToLambdaMapAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6308 Stream.EmitRecord(Code: RELATED_DECLS_MAP, Vals: RelatedDeclsMapRecord,
6309 Abbrev: FunctionToLambdaMapAbbrev);
6310 }
6311
6312 if (!SpecializationsUpdates.empty()) {
6313 WriteSpecializationsUpdates(/*IsPartial=*/false);
6314 SpecializationsUpdates.clear();
6315 }
6316
6317 if (!PartialSpecializationsUpdates.empty()) {
6318 WriteSpecializationsUpdates(/*IsPartial=*/true);
6319 PartialSpecializationsUpdates.clear();
6320 }
6321
6322 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
6323 // Create a lexical update block containing all of the declarations in the
6324 // translation unit that do not come from other AST files.
6325 SmallVector<DeclID, 128> NewGlobalKindDeclPairs;
6326 for (const auto *D : TU->noload_decls()) {
6327 if (D->isFromASTFile())
6328 continue;
6329
6330 // In reduced BMI, skip unreached declarations.
6331 if (!wasDeclEmitted(D))
6332 continue;
6333
6334 NewGlobalKindDeclPairs.push_back(Elt: D->getKind());
6335 NewGlobalKindDeclPairs.push_back(Elt: GetDeclRef(D).getRawValue());
6336 }
6337
6338 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
6339 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
6340 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6341 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6342
6343 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
6344 Stream.EmitRecordWithBlob(Abbrev: TuUpdateLexicalAbbrev, Vals: Record,
6345 Blob: bytes(v: NewGlobalKindDeclPairs));
6346
6347 Abv = std::make_shared<llvm::BitCodeAbbrev>();
6348 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
6349 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6350 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6351 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6352
6353 Abv = std::make_shared<llvm::BitCodeAbbrev>();
6354 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(UPDATE_MODULE_LOCAL_VISIBLE));
6355 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6356 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6357 ModuleLocalUpdateVisibleAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6358
6359 Abv = std::make_shared<llvm::BitCodeAbbrev>();
6360 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(UPDATE_TU_LOCAL_VISIBLE));
6361 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6362 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6363 TULocalUpdateVisibleAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6364
6365 // And a visible updates block for the translation unit.
6366 WriteDeclContextVisibleUpdate(Context, DC: TU);
6367
6368 // If we have any extern "C" names, write out a visible update for them.
6369 if (Context.ExternCContext)
6370 WriteDeclContextVisibleUpdate(Context, DC: Context.ExternCContext);
6371
6372 // Write the visible updates to DeclContexts.
6373 for (auto *DC : UpdatedDeclContexts)
6374 WriteDeclContextVisibleUpdate(Context, DC);
6375}
6376
6377void ASTWriter::WriteSpecializationsUpdates(bool IsPartial) {
6378 auto RecordType = IsPartial ? CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION
6379 : CXX_ADDED_TEMPLATE_SPECIALIZATION;
6380
6381 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
6382 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(RecordType));
6383 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6384 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6385 auto UpdateSpecializationAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6386
6387 auto &SpecUpdates =
6388 IsPartial ? PartialSpecializationsUpdates : SpecializationsUpdates;
6389 for (auto &SpecializationUpdate : SpecUpdates) {
6390 const NamedDecl *D = SpecializationUpdate.first;
6391
6392 llvm::SmallString<4096> LookupTable;
6393 GenerateSpecializationInfoLookupTable(D, Specializations&: SpecializationUpdate.second,
6394 LookupTable, IsPartial);
6395
6396 // Write the lookup table
6397 RecordData::value_type Record[] = {
6398 static_cast<RecordData::value_type>(RecordType),
6399 getDeclID(D).getRawValue()};
6400 Stream.EmitRecordWithBlob(Abbrev: UpdateSpecializationAbbrev, Vals: Record, Blob: LookupTable);
6401 }
6402}
6403
6404void ASTWriter::WriteDeclUpdatesBlocks(ASTContext &Context,
6405 RecordDataImpl &OffsetsRecord) {
6406 if (DeclUpdates.empty())
6407 return;
6408
6409 DeclUpdateMap LocalUpdates;
6410 LocalUpdates.swap(RHS&: DeclUpdates);
6411
6412 for (auto &DeclUpdate : LocalUpdates) {
6413 const Decl *D = DeclUpdate.first;
6414
6415 bool HasUpdatedBody = false;
6416 bool HasAddedVarDefinition = false;
6417 RecordData RecordData;
6418 ASTRecordWriter Record(Context, *this, RecordData);
6419 for (auto &Update : DeclUpdate.second) {
6420 DeclUpdateKind Kind = Update.getKind();
6421
6422 // An updated body is emitted last, so that the reader doesn't need
6423 // to skip over the lazy body to reach statements for other records.
6424 if (Kind == DeclUpdateKind::CXXAddedFunctionDefinition)
6425 HasUpdatedBody = true;
6426 else if (Kind == DeclUpdateKind::CXXAddedVarDefinition)
6427 HasAddedVarDefinition = true;
6428 else
6429 Record.push_back(N: llvm::to_underlying(E: Kind));
6430
6431 switch (Kind) {
6432 case DeclUpdateKind::CXXAddedImplicitMember:
6433 case DeclUpdateKind::CXXAddedAnonymousNamespace:
6434 assert(Update.getDecl() && "no decl to add?");
6435 Record.AddDeclRef(D: Update.getDecl());
6436 break;
6437 case DeclUpdateKind::CXXAddedFunctionDefinition:
6438 case DeclUpdateKind::CXXAddedVarDefinition:
6439 break;
6440
6441 case DeclUpdateKind::CXXPointOfInstantiation:
6442 // FIXME: Do we need to also save the template specialization kind here?
6443 Record.AddSourceLocation(Loc: Update.getLoc());
6444 break;
6445
6446 case DeclUpdateKind::CXXInstantiatedDefaultArgument:
6447 Record.writeStmtRef(
6448 S: cast<ParmVarDecl>(Val: Update.getDecl())->getDefaultArg());
6449 break;
6450
6451 case DeclUpdateKind::CXXInstantiatedDefaultMemberInitializer:
6452 Record.AddStmt(
6453 S: cast<FieldDecl>(Val: Update.getDecl())->getInClassInitializer());
6454 break;
6455
6456 case DeclUpdateKind::CXXInstantiatedClassDefinition: {
6457 auto *RD = cast<CXXRecordDecl>(Val: D);
6458 UpdatedDeclContexts.insert(X: RD->getPrimaryContext());
6459 Record.push_back(N: RD->isParamDestroyedInCallee());
6460 Record.push_back(N: llvm::to_underlying(E: RD->getArgPassingRestrictions()));
6461 Record.AddCXXDefinitionData(D: RD);
6462 Record.AddOffset(BitOffset: WriteDeclContextLexicalBlock(Context, DC: RD));
6463
6464 // This state is sometimes updated by template instantiation, when we
6465 // switch from the specialization referring to the template declaration
6466 // to it referring to the template definition.
6467 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
6468 Record.push_back(N: MSInfo->getTemplateSpecializationKind());
6469 Record.AddSourceLocation(Loc: MSInfo->getPointOfInstantiation());
6470 } else {
6471 auto *Spec = cast<ClassTemplateSpecializationDecl>(Val: RD);
6472 Record.push_back(N: Spec->getTemplateSpecializationKind());
6473 Record.AddSourceLocation(Loc: Spec->getPointOfInstantiation());
6474
6475 // The instantiation might have been resolved to a partial
6476 // specialization. If so, record which one.
6477 auto From = Spec->getInstantiatedFrom();
6478 if (auto PartialSpec =
6479 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
6480 Record.push_back(N: true);
6481 Record.AddDeclRef(D: PartialSpec);
6482 Record.AddTemplateArgumentList(
6483 TemplateArgs: &Spec->getTemplateInstantiationArgs());
6484 } else {
6485 Record.push_back(N: false);
6486 }
6487 }
6488 Record.push_back(N: llvm::to_underlying(E: RD->getTagKind()));
6489 Record.AddSourceLocation(Loc: RD->getLocation());
6490 Record.AddSourceLocation(Loc: RD->getBeginLoc());
6491 Record.AddSourceRange(Range: RD->getBraceRange());
6492
6493 // Instantiation may change attributes; write them all out afresh.
6494 Record.push_back(N: D->hasAttrs());
6495 if (D->hasAttrs())
6496 Record.AddAttributes(Attrs: D->getAttrs());
6497
6498 // FIXME: Ensure we don't get here for explicit instantiations.
6499 break;
6500 }
6501
6502 case DeclUpdateKind::CXXResolvedDtorDelete:
6503 Record.AddDeclRef(D: Update.getDecl());
6504 Record.AddStmt(S: cast<CXXDestructorDecl>(Val: D)->getOperatorDeleteThisArg());
6505 break;
6506
6507 case DeclUpdateKind::CXXResolvedExceptionSpec: {
6508 auto prototype =
6509 cast<FunctionDecl>(Val: D)->getType()->castAs<FunctionProtoType>();
6510 Record.writeExceptionSpecInfo(esi: prototype->getExceptionSpecInfo());
6511 break;
6512 }
6513
6514 case DeclUpdateKind::CXXDeducedReturnType:
6515 Record.push_back(N: GetOrCreateTypeID(Context, T: Update.getType()));
6516 break;
6517
6518 case DeclUpdateKind::DeclMarkedUsed:
6519 break;
6520
6521 case DeclUpdateKind::ManglingNumber:
6522 case DeclUpdateKind::StaticLocalNumber:
6523 Record.push_back(N: Update.getNumber());
6524 break;
6525
6526 case DeclUpdateKind::DeclMarkedOpenMPThreadPrivate:
6527 Record.AddSourceRange(
6528 Range: D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
6529 break;
6530
6531 case DeclUpdateKind::DeclMarkedOpenMPAllocate: {
6532 auto *A = D->getAttr<OMPAllocateDeclAttr>();
6533 Record.push_back(N: A->getAllocatorType());
6534 Record.AddStmt(S: A->getAllocator());
6535 Record.AddStmt(S: A->getAlignment());
6536 Record.AddSourceRange(Range: A->getRange());
6537 break;
6538 }
6539
6540 case DeclUpdateKind::DeclMarkedOpenMPDeclareTarget:
6541 Record.push_back(N: D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
6542 Record.AddSourceRange(
6543 Range: D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
6544 break;
6545
6546 case DeclUpdateKind::DeclExported:
6547 Record.push_back(N: getSubmoduleID(Mod: Update.getModule()));
6548 break;
6549
6550 case DeclUpdateKind::AddedAttrToRecord:
6551 Record.AddAttributes(Attrs: llvm::ArrayRef(Update.getAttr()));
6552 break;
6553 }
6554 }
6555
6556 // Add a trailing update record, if any. These must go last because we
6557 // lazily load their attached statement.
6558 if (!GeneratingReducedBMI || !CanElideDeclDef(D)) {
6559 if (HasUpdatedBody) {
6560 const auto *Def = cast<FunctionDecl>(Val: D);
6561 Record.push_back(
6562 N: llvm::to_underlying(E: DeclUpdateKind::CXXAddedFunctionDefinition));
6563 Record.push_back(N: Def->isInlined());
6564 Record.AddSourceLocation(Loc: Def->getInnerLocStart());
6565 Record.AddFunctionDefinition(FD: Def);
6566 } else if (HasAddedVarDefinition) {
6567 const auto *VD = cast<VarDecl>(Val: D);
6568 Record.push_back(
6569 N: llvm::to_underlying(E: DeclUpdateKind::CXXAddedVarDefinition));
6570 Record.push_back(N: VD->isInline());
6571 Record.push_back(N: VD->isInlineSpecified());
6572 Record.AddVarDeclInit(VD);
6573 }
6574 }
6575
6576 AddDeclRef(D, Record&: OffsetsRecord);
6577 OffsetsRecord.push_back(Elt: Record.Emit(Code: DECL_UPDATES));
6578 }
6579}
6580
6581void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
6582 RecordDataImpl &Record) {
6583 uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
6584 Record.push_back(Elt: Raw);
6585}
6586
6587FileID ASTWriter::getAdjustedFileID(FileID FID) const {
6588 if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
6589 NonAffectingFileIDs.empty())
6590 return FID;
6591 auto It = llvm::lower_bound(Range: NonAffectingFileIDs, Value&: FID);
6592 unsigned Idx = std::distance(first: NonAffectingFileIDs.begin(), last: It);
6593 unsigned Offset = NonAffectingFileIDAdjustments[Idx];
6594 return FileID::get(V: FID.getOpaqueValue() - Offset);
6595}
6596
6597unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
6598 unsigned NumCreatedFIDs = PP->getSourceManager()
6599 .getLocalSLocEntry(Index: FID.ID)
6600 .getFile()
6601 .NumCreatedFIDs;
6602
6603 unsigned AdjustedNumCreatedFIDs = 0;
6604 for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
6605 if (IsSLocAffecting[I])
6606 ++AdjustedNumCreatedFIDs;
6607 return AdjustedNumCreatedFIDs;
6608}
6609
6610SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
6611 if (Loc.isInvalid())
6612 return Loc;
6613 return Loc.getLocWithOffset(Offset: -getAdjustment(Offset: Loc.getOffset()));
6614}
6615
6616SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
6617 return SourceRange(getAdjustedLocation(Loc: Range.getBegin()),
6618 getAdjustedLocation(Loc: Range.getEnd()));
6619}
6620
6621SourceLocation::UIntTy
6622ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
6623 return Offset - getAdjustment(Offset);
6624}
6625
6626SourceLocation::UIntTy
6627ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
6628 if (NonAffectingRanges.empty())
6629 return 0;
6630
6631 if (PP->getSourceManager().isLoadedOffset(SLocOffset: Offset))
6632 return 0;
6633
6634 if (Offset > NonAffectingRanges.back().getEnd().getOffset())
6635 return NonAffectingOffsetAdjustments.back();
6636
6637 if (Offset < NonAffectingRanges.front().getBegin().getOffset())
6638 return 0;
6639
6640 auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
6641 return Range.getEnd().getOffset() < Offset;
6642 };
6643
6644 auto It = llvm::lower_bound(Range: NonAffectingRanges, Value&: Offset, C: Contains);
6645 unsigned Idx = std::distance(first: NonAffectingRanges.begin(), last: It);
6646 return NonAffectingOffsetAdjustments[Idx];
6647}
6648
6649void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
6650 Record.push_back(Elt: getAdjustedFileID(FID).getOpaqueValue());
6651}
6652
6653SourceLocationEncoding::RawLocEncoding
6654ASTWriter::getRawSourceLocationEncoding(SourceLocation Loc) {
6655 SourceLocation::UIntTy BaseOffset = 0;
6656 unsigned ModuleFileIndex = 0;
6657
6658 // See SourceLocationEncoding.h for the encoding details.
6659 if (PP->getSourceManager().isLoadedSourceLocation(Loc) && Loc.isValid()) {
6660 assert(getChain());
6661 auto SLocMapI = getChain()->GlobalSLocOffsetMap.find(
6662 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6663 assert(SLocMapI != getChain()->GlobalSLocOffsetMap.end() &&
6664 "Corrupted global sloc offset map");
6665 ModuleFile *F = SLocMapI->second;
6666 BaseOffset = F->SLocEntryBaseOffset - 2;
6667 // 0 means the location is not loaded. So we need to add 1 to the index to
6668 // make it clear.
6669 ModuleFileIndex = F->Index + 1;
6670 assert(&getChain()->getModuleManager()[F->Index] == F);
6671 }
6672
6673 return SourceLocationEncoding::encode(Loc, BaseOffset, BaseModuleFileIndex: ModuleFileIndex);
6674}
6675
6676void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
6677 Loc = getAdjustedLocation(Loc);
6678 Record.push_back(Elt: getRawSourceLocationEncoding(Loc));
6679}
6680
6681void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
6682 AddSourceLocation(Loc: Range.getBegin(), Record);
6683 AddSourceLocation(Loc: Range.getEnd(), Record);
6684}
6685
6686void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
6687 AddAPInt(Value: Value.bitcastToAPInt());
6688}
6689
6690void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
6691 Record.push_back(Elt: getIdentifierRef(II));
6692}
6693
6694IdentifierID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
6695 if (!II)
6696 return 0;
6697
6698 IdentifierID &ID = IdentifierIDs[II];
6699 if (ID == 0)
6700 ID = NextIdentID++;
6701 return ID;
6702}
6703
6704MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
6705 // Don't emit builtin macros like __LINE__ to the AST file unless they
6706 // have been redefined by the header (in which case they are not
6707 // isBuiltinMacro).
6708 if (!MI || MI->isBuiltinMacro())
6709 return 0;
6710
6711 MacroID &ID = MacroIDs[MI];
6712 if (ID == 0) {
6713 ID = NextMacroID++;
6714 MacroInfoToEmitData Info = { .Name: Name, .MI: MI, .ID: ID };
6715 MacroInfosToEmit.push_back(x: Info);
6716 }
6717 return ID;
6718}
6719
6720uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
6721 return IdentMacroDirectivesOffsetMap.lookup(Val: Name);
6722}
6723
6724void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
6725 Record->push_back(Elt: Writer->getSelectorRef(Sel: SelRef));
6726}
6727
6728SelectorID ASTWriter::getSelectorRef(Selector Sel) {
6729 if (Sel.getAsOpaquePtr() == nullptr) {
6730 return 0;
6731 }
6732
6733 SelectorID SID = SelectorIDs[Sel];
6734 if (SID == 0 && Chain) {
6735 // This might trigger a ReadSelector callback, which will set the ID for
6736 // this selector.
6737 Chain->LoadSelector(Sel);
6738 SID = SelectorIDs[Sel];
6739 }
6740 if (SID == 0) {
6741 SID = NextSelectorID++;
6742 SelectorIDs[Sel] = SID;
6743 }
6744 return SID;
6745}
6746
6747void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
6748 AddDeclRef(D: Temp->getDestructor());
6749}
6750
6751void ASTRecordWriter::AddTemplateArgumentLocInfo(
6752 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
6753 switch (Kind) {
6754 case TemplateArgument::Expression:
6755 AddStmt(S: Arg.getAsExpr());
6756 break;
6757 case TemplateArgument::Type:
6758 AddTypeSourceInfo(TInfo: Arg.getAsTypeSourceInfo());
6759 break;
6760 case TemplateArgument::Template:
6761 AddNestedNameSpecifierLoc(NNS: Arg.getTemplateQualifierLoc());
6762 AddSourceLocation(Loc: Arg.getTemplateNameLoc());
6763 break;
6764 case TemplateArgument::TemplateExpansion:
6765 AddNestedNameSpecifierLoc(NNS: Arg.getTemplateQualifierLoc());
6766 AddSourceLocation(Loc: Arg.getTemplateNameLoc());
6767 AddSourceLocation(Loc: Arg.getTemplateEllipsisLoc());
6768 break;
6769 case TemplateArgument::Null:
6770 case TemplateArgument::Integral:
6771 case TemplateArgument::Declaration:
6772 case TemplateArgument::NullPtr:
6773 case TemplateArgument::StructuralValue:
6774 case TemplateArgument::Pack:
6775 // FIXME: Is this right?
6776 break;
6777 }
6778}
6779
6780void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
6781 AddTemplateArgument(Arg: Arg.getArgument());
6782
6783 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
6784 bool InfoHasSameExpr
6785 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
6786 Record->push_back(Elt: InfoHasSameExpr);
6787 if (InfoHasSameExpr)
6788 return; // Avoid storing the same expr twice.
6789 }
6790 AddTemplateArgumentLocInfo(Kind: Arg.getArgument().getKind(), Arg: Arg.getLocInfo());
6791}
6792
6793void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
6794 if (!TInfo) {
6795 AddTypeRef(T: QualType());
6796 return;
6797 }
6798
6799 AddTypeRef(T: TInfo->getType());
6800 AddTypeLoc(TL: TInfo->getTypeLoc());
6801}
6802
6803void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
6804 TypeLocWriter TLW(*this);
6805 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6806 TLW.Visit(TyLoc: TL);
6807}
6808
6809void ASTWriter::AddTypeRef(ASTContext &Context, QualType T,
6810 RecordDataImpl &Record) {
6811 Record.push_back(Elt: GetOrCreateTypeID(Context, T));
6812}
6813
6814template <typename IdxForTypeTy>
6815static TypeID MakeTypeID(ASTContext &Context, QualType T,
6816 IdxForTypeTy IdxForType) {
6817 if (T.isNull())
6818 return PREDEF_TYPE_NULL_ID;
6819
6820 unsigned FastQuals = T.getLocalFastQualifiers();
6821 T.removeLocalFastQualifiers();
6822
6823 if (T.hasLocalNonFastQualifiers())
6824 return IdxForType(T).asTypeID(FastQuals);
6825
6826 assert(!T.hasLocalQualifiers());
6827
6828 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Val: T.getTypePtr()))
6829 return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
6830
6831 if (T == Context.AutoDeductTy)
6832 return TypeIdx(0, PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
6833 if (T == Context.AutoRRefDeductTy)
6834 return TypeIdx(0, PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
6835
6836 return IdxForType(T).asTypeID(FastQuals);
6837}
6838
6839TypeID ASTWriter::GetOrCreateTypeID(ASTContext &Context, QualType T) {
6840 return MakeTypeID(Context, T, IdxForType: [&](QualType T) -> TypeIdx {
6841 if (T.isNull())
6842 return TypeIdx();
6843 assert(!T.getLocalFastQualifiers());
6844
6845 TypeIdx &Idx = TypeIdxs[T];
6846 if (Idx.getValue() == 0) {
6847 if (DoneWritingDeclsAndTypes) {
6848 assert(0 && "New type seen after serializing all the types to emit!");
6849 return TypeIdx();
6850 }
6851
6852 // We haven't seen this type before. Assign it a new ID and put it
6853 // into the queue of types to emit.
6854 Idx = TypeIdx(0, NextTypeID++);
6855 DeclTypesToEmit.push(x: T);
6856 }
6857 return Idx;
6858 });
6859}
6860
6861void ASTWriter::AddLookupOffsets(const LookupBlockOffsets &Offsets,
6862 RecordDataImpl &Record) {
6863 Record.push_back(Elt: Offsets.LexicalOffset);
6864 Record.push_back(Elt: Offsets.VisibleOffset);
6865 Record.push_back(Elt: Offsets.ModuleLocalOffset);
6866 Record.push_back(Elt: Offsets.TULocalOffset);
6867}
6868
6869void ASTWriter::AddEmittedDeclRef(const Decl *D, RecordDataImpl &Record) {
6870 if (!wasDeclEmitted(D))
6871 return;
6872
6873 AddDeclRef(D, Record);
6874}
6875
6876void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
6877 Record.push_back(Elt: GetDeclRef(D).getRawValue());
6878}
6879
6880LocalDeclID ASTWriter::GetDeclRef(const Decl *D) {
6881 assert(WritingAST && "Cannot request a declaration ID before AST writing");
6882
6883 if (!D) {
6884 return LocalDeclID();
6885 }
6886
6887 // If the DeclUpdate from the GMF gets touched, emit it.
6888 if (auto *Iter = DeclUpdatesFromGMF.find(Key: D);
6889 Iter != DeclUpdatesFromGMF.end()) {
6890 for (DeclUpdate &Update : Iter->second)
6891 DeclUpdates[D].push_back(Elt: Update);
6892 DeclUpdatesFromGMF.erase(Iterator: Iter);
6893 }
6894
6895 // If D comes from an AST file, its declaration ID is already known and
6896 // fixed.
6897 if (D->isFromASTFile()) {
6898 if (isWritingStdCXXNamedModules() && D->getOwningModule())
6899 TouchedTopLevelModules.insert(X: D->getOwningModule()->getTopLevelModule());
6900
6901 return LocalDeclID(D->getGlobalID());
6902 }
6903
6904 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
6905 LocalDeclID &ID = DeclIDs[D];
6906 if (ID.isInvalid()) {
6907 if (DoneWritingDeclsAndTypes) {
6908 assert(0 && "New decl seen after serializing all the decls to emit!");
6909 return LocalDeclID();
6910 }
6911
6912 // We haven't seen this declaration before. Give it a new ID and
6913 // enqueue it in the list of declarations to emit.
6914 ID = NextDeclID++;
6915 DeclTypesToEmit.push(x: const_cast<Decl *>(D));
6916 }
6917
6918 return ID;
6919}
6920
6921LocalDeclID ASTWriter::getDeclID(const Decl *D) {
6922 if (!D)
6923 return LocalDeclID();
6924
6925 // If D comes from an AST file, its declaration ID is already known and
6926 // fixed.
6927 if (D->isFromASTFile())
6928 return LocalDeclID(D->getGlobalID());
6929
6930 assert(DeclIDs.contains(D) && "Declaration not emitted!");
6931 return DeclIDs[D];
6932}
6933
6934bool ASTWriter::wasDeclEmitted(const Decl *D) const {
6935 assert(D);
6936
6937 assert(DoneWritingDeclsAndTypes &&
6938 "wasDeclEmitted should only be called after writing declarations");
6939
6940 if (D->isFromASTFile())
6941 return true;
6942
6943 bool Emitted = DeclIDs.contains(Val: D);
6944 assert((Emitted || (!D->getOwningModule() && isWritingStdCXXNamedModules()) ||
6945 GeneratingReducedBMI) &&
6946 "The declaration within modules can only be omitted in reduced BMI.");
6947 return Emitted;
6948}
6949
6950void ASTWriter::associateDeclWithFile(const Decl *D, LocalDeclID ID) {
6951 assert(ID.isValid());
6952 assert(D);
6953
6954 SourceLocation Loc = D->getLocation();
6955 if (Loc.isInvalid())
6956 return;
6957
6958 // We only keep track of the file-level declarations of each file.
6959 if (!D->getLexicalDeclContext()->isFileContext())
6960 return;
6961 // FIXME: ParmVarDecls that are part of a function type of a parameter of
6962 // a function/objc method, should not have TU as lexical context.
6963 // TemplateTemplateParmDecls that are part of an alias template, should not
6964 // have TU as lexical context.
6965 if (isa<ParmVarDecl, TemplateTemplateParmDecl>(Val: D))
6966 return;
6967
6968 SourceManager &SM = PP->getSourceManager();
6969 SourceLocation FileLoc = SM.getFileLoc(Loc);
6970 assert(SM.isLocalSourceLocation(FileLoc));
6971 FileID FID;
6972 unsigned Offset;
6973 std::tie(args&: FID, args&: Offset) = SM.getDecomposedLoc(Loc: FileLoc);
6974 if (FID.isInvalid())
6975 return;
6976 assert(SM.getSLocEntry(FID).isFile());
6977 assert(IsSLocAffecting[FID.ID]);
6978
6979 std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
6980 if (!Info)
6981 Info = std::make_unique<DeclIDInFileInfo>();
6982
6983 std::pair<unsigned, LocalDeclID> LocDecl(Offset, ID);
6984 LocDeclIDsTy &Decls = Info->DeclIDs;
6985 Decls.push_back(Elt: LocDecl);
6986}
6987
6988unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
6989 assert(needsAnonymousDeclarationNumber(D) &&
6990 "expected an anonymous declaration");
6991
6992 // Number the anonymous declarations within this context, if we've not
6993 // already done so.
6994 auto It = AnonymousDeclarationNumbers.find(Val: D);
6995 if (It == AnonymousDeclarationNumbers.end()) {
6996 auto *DC = D->getLexicalDeclContext();
6997 numberAnonymousDeclsWithin(DC, Visit: [&](const NamedDecl *ND, unsigned Number) {
6998 AnonymousDeclarationNumbers[ND] = Number;
6999 });
7000
7001 It = AnonymousDeclarationNumbers.find(Val: D);
7002 assert(It != AnonymousDeclarationNumbers.end() &&
7003 "declaration not found within its lexical context");
7004 }
7005
7006 return It->second;
7007}
7008
7009void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
7010 DeclarationName Name) {
7011 switch (Name.getNameKind()) {
7012 case DeclarationName::CXXConstructorName:
7013 case DeclarationName::CXXDestructorName:
7014 case DeclarationName::CXXConversionFunctionName:
7015 AddTypeSourceInfo(TInfo: DNLoc.getNamedTypeInfo());
7016 break;
7017
7018 case DeclarationName::CXXOperatorName:
7019 AddSourceRange(Range: DNLoc.getCXXOperatorNameRange());
7020 break;
7021
7022 case DeclarationName::CXXLiteralOperatorName:
7023 AddSourceLocation(Loc: DNLoc.getCXXLiteralOperatorNameLoc());
7024 break;
7025
7026 case DeclarationName::Identifier:
7027 case DeclarationName::ObjCZeroArgSelector:
7028 case DeclarationName::ObjCOneArgSelector:
7029 case DeclarationName::ObjCMultiArgSelector:
7030 case DeclarationName::CXXUsingDirective:
7031 case DeclarationName::CXXDeductionGuideName:
7032 break;
7033 }
7034}
7035
7036void ASTRecordWriter::AddDeclarationNameInfo(
7037 const DeclarationNameInfo &NameInfo) {
7038 AddDeclarationName(Name: NameInfo.getName());
7039 AddSourceLocation(Loc: NameInfo.getLoc());
7040 AddDeclarationNameLoc(DNLoc: NameInfo.getInfo(), Name: NameInfo.getName());
7041}
7042
7043void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
7044 AddNestedNameSpecifierLoc(NNS: Info.QualifierLoc);
7045 Record->push_back(Elt: Info.NumTemplParamLists);
7046 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
7047 AddTemplateParameterList(TemplateParams: Info.TemplParamLists[i]);
7048}
7049
7050void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
7051 // Nested name specifiers usually aren't too long. I think that 8 would
7052 // typically accommodate the vast majority.
7053 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7054
7055 // Push each of the nested-name-specifiers's onto a stack for
7056 // serialization in reverse order.
7057 while (NNS) {
7058 NestedNames.push_back(Elt: NNS);
7059 NNS = NNS.getPrefix();
7060 }
7061
7062 Record->push_back(Elt: NestedNames.size());
7063 while(!NestedNames.empty()) {
7064 NNS = NestedNames.pop_back_val();
7065 NestedNameSpecifier::SpecifierKind Kind
7066 = NNS.getNestedNameSpecifier()->getKind();
7067 Record->push_back(Elt: Kind);
7068 switch (Kind) {
7069 case NestedNameSpecifier::Identifier:
7070 AddIdentifierRef(II: NNS.getNestedNameSpecifier()->getAsIdentifier());
7071 AddSourceRange(Range: NNS.getLocalSourceRange());
7072 break;
7073
7074 case NestedNameSpecifier::Namespace:
7075 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsNamespace());
7076 AddSourceRange(Range: NNS.getLocalSourceRange());
7077 break;
7078
7079 case NestedNameSpecifier::NamespaceAlias:
7080 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
7081 AddSourceRange(Range: NNS.getLocalSourceRange());
7082 break;
7083
7084 case NestedNameSpecifier::TypeSpec:
7085 AddTypeRef(T: NNS.getTypeLoc().getType());
7086 AddTypeLoc(TL: NNS.getTypeLoc());
7087 AddSourceLocation(Loc: NNS.getLocalSourceRange().getEnd());
7088 break;
7089
7090 case NestedNameSpecifier::Global:
7091 AddSourceLocation(Loc: NNS.getLocalSourceRange().getEnd());
7092 break;
7093
7094 case NestedNameSpecifier::Super:
7095 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsRecordDecl());
7096 AddSourceRange(Range: NNS.getLocalSourceRange());
7097 break;
7098 }
7099 }
7100}
7101
7102void ASTRecordWriter::AddTemplateParameterList(
7103 const TemplateParameterList *TemplateParams) {
7104 assert(TemplateParams && "No TemplateParams!");
7105 AddSourceLocation(Loc: TemplateParams->getTemplateLoc());
7106 AddSourceLocation(Loc: TemplateParams->getLAngleLoc());
7107 AddSourceLocation(Loc: TemplateParams->getRAngleLoc());
7108
7109 Record->push_back(Elt: TemplateParams->size());
7110 for (const auto &P : *TemplateParams)
7111 AddDeclRef(D: P);
7112 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
7113 Record->push_back(Elt: true);
7114 writeStmtRef(S: RequiresClause);
7115 } else {
7116 Record->push_back(Elt: false);
7117 }
7118}
7119
7120/// Emit a template argument list.
7121void ASTRecordWriter::AddTemplateArgumentList(
7122 const TemplateArgumentList *TemplateArgs) {
7123 assert(TemplateArgs && "No TemplateArgs!");
7124 Record->push_back(Elt: TemplateArgs->size());
7125 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
7126 AddTemplateArgument(Arg: TemplateArgs->get(Idx: i));
7127}
7128
7129void ASTRecordWriter::AddASTTemplateArgumentListInfo(
7130 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
7131 assert(ASTTemplArgList && "No ASTTemplArgList!");
7132 AddSourceLocation(Loc: ASTTemplArgList->LAngleLoc);
7133 AddSourceLocation(Loc: ASTTemplArgList->RAngleLoc);
7134 Record->push_back(Elt: ASTTemplArgList->NumTemplateArgs);
7135 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
7136 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
7137 AddTemplateArgumentLoc(Arg: TemplArgs[i]);
7138}
7139
7140void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
7141 Record->push_back(Elt: Set.size());
7142 for (ASTUnresolvedSet::const_iterator
7143 I = Set.begin(), E = Set.end(); I != E; ++I) {
7144 AddDeclRef(D: I.getDecl());
7145 Record->push_back(Elt: I.getAccess());
7146 }
7147}
7148
7149// FIXME: Move this out of the main ASTRecordWriter interface.
7150void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
7151 Record->push_back(Elt: Base.isVirtual());
7152 Record->push_back(Elt: Base.isBaseOfClass());
7153 Record->push_back(Elt: Base.getAccessSpecifierAsWritten());
7154 Record->push_back(Elt: Base.getInheritConstructors());
7155 AddTypeSourceInfo(TInfo: Base.getTypeSourceInfo());
7156 AddSourceRange(Range: Base.getSourceRange());
7157 AddSourceLocation(Loc: Base.isPackExpansion()? Base.getEllipsisLoc()
7158 : SourceLocation());
7159}
7160
7161static uint64_t EmitCXXBaseSpecifiers(ASTContext &Context, ASTWriter &W,
7162 ArrayRef<CXXBaseSpecifier> Bases) {
7163 ASTWriter::RecordData Record;
7164 ASTRecordWriter Writer(Context, W, Record);
7165 Writer.push_back(N: Bases.size());
7166
7167 for (auto &Base : Bases)
7168 Writer.AddCXXBaseSpecifier(Base);
7169
7170 return Writer.Emit(Code: serialization::DECL_CXX_BASE_SPECIFIERS);
7171}
7172
7173// FIXME: Move this out of the main ASTRecordWriter interface.
7174void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
7175 AddOffset(BitOffset: EmitCXXBaseSpecifiers(Context&: getASTContext(), W&: *Writer, Bases));
7176}
7177
7178static uint64_t
7179EmitCXXCtorInitializers(ASTContext &Context, ASTWriter &W,
7180 ArrayRef<CXXCtorInitializer *> CtorInits) {
7181 ASTWriter::RecordData Record;
7182 ASTRecordWriter Writer(Context, W, Record);
7183 Writer.push_back(N: CtorInits.size());
7184
7185 for (auto *Init : CtorInits) {
7186 if (Init->isBaseInitializer()) {
7187 Writer.push_back(N: CTOR_INITIALIZER_BASE);
7188 Writer.AddTypeSourceInfo(TInfo: Init->getTypeSourceInfo());
7189 Writer.push_back(N: Init->isBaseVirtual());
7190 } else if (Init->isDelegatingInitializer()) {
7191 Writer.push_back(N: CTOR_INITIALIZER_DELEGATING);
7192 Writer.AddTypeSourceInfo(TInfo: Init->getTypeSourceInfo());
7193 } else if (Init->isMemberInitializer()){
7194 Writer.push_back(N: CTOR_INITIALIZER_MEMBER);
7195 Writer.AddDeclRef(D: Init->getMember());
7196 } else {
7197 Writer.push_back(N: CTOR_INITIALIZER_INDIRECT_MEMBER);
7198 Writer.AddDeclRef(D: Init->getIndirectMember());
7199 }
7200
7201 Writer.AddSourceLocation(Loc: Init->getMemberLocation());
7202 Writer.AddStmt(S: Init->getInit());
7203 Writer.AddSourceLocation(Loc: Init->getLParenLoc());
7204 Writer.AddSourceLocation(Loc: Init->getRParenLoc());
7205 Writer.push_back(N: Init->isWritten());
7206 if (Init->isWritten())
7207 Writer.push_back(N: Init->getSourceOrder());
7208 }
7209
7210 return Writer.Emit(Code: serialization::DECL_CXX_CTOR_INITIALIZERS);
7211}
7212
7213// FIXME: Move this out of the main ASTRecordWriter interface.
7214void ASTRecordWriter::AddCXXCtorInitializers(
7215 ArrayRef<CXXCtorInitializer *> CtorInits) {
7216 AddOffset(BitOffset: EmitCXXCtorInitializers(Context&: getASTContext(), W&: *Writer, CtorInits));
7217}
7218
7219void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
7220 auto &Data = D->data();
7221
7222 Record->push_back(Elt: Data.IsLambda);
7223
7224 BitsPacker DefinitionBits;
7225
7226#define FIELD(Name, Width, Merge) \
7227 if (!DefinitionBits.canWriteNextNBits(Width)) { \
7228 Record->push_back(DefinitionBits); \
7229 DefinitionBits.reset(0); \
7230 } \
7231 DefinitionBits.addBits(Data.Name, Width);
7232
7233#include "clang/AST/CXXRecordDeclDefinitionBits.def"
7234#undef FIELD
7235
7236 Record->push_back(Elt: DefinitionBits);
7237
7238 // getODRHash will compute the ODRHash if it has not been previously
7239 // computed.
7240 Record->push_back(Elt: D->getODRHash());
7241
7242 bool ModulesCodegen =
7243 !D->isDependentType() &&
7244 D->getTemplateSpecializationKind() !=
7245 TSK_ExplicitInstantiationDeclaration &&
7246 (Writer->getLangOpts().ModulesDebugInfo || D->isInNamedModule());
7247 Record->push_back(Elt: ModulesCodegen);
7248 if (ModulesCodegen)
7249 Writer->AddDeclRef(D, Record&: Writer->ModularCodegenDecls);
7250
7251 // IsLambda bit is already saved.
7252
7253 AddUnresolvedSet(Set: Data.Conversions.get(C&: getASTContext()));
7254 Record->push_back(Elt: Data.ComputedVisibleConversions);
7255 if (Data.ComputedVisibleConversions)
7256 AddUnresolvedSet(Set: Data.VisibleConversions.get(C&: getASTContext()));
7257 // Data.Definition is the owning decl, no need to write it.
7258
7259 if (!Data.IsLambda) {
7260 Record->push_back(Elt: Data.NumBases);
7261 if (Data.NumBases > 0)
7262 AddCXXBaseSpecifiers(Bases: Data.bases());
7263
7264 // FIXME: Make VBases lazily computed when needed to avoid storing them.
7265 Record->push_back(Elt: Data.NumVBases);
7266 if (Data.NumVBases > 0)
7267 AddCXXBaseSpecifiers(Bases: Data.vbases());
7268
7269 AddDeclRef(D: D->getFirstFriend());
7270 } else {
7271 auto &Lambda = D->getLambdaData();
7272
7273 BitsPacker LambdaBits;
7274 LambdaBits.addBits(Value: Lambda.DependencyKind, /*Width=*/BitsWidth: 2);
7275 LambdaBits.addBit(Value: Lambda.IsGenericLambda);
7276 LambdaBits.addBits(Value: Lambda.CaptureDefault, /*Width=*/BitsWidth: 2);
7277 LambdaBits.addBits(Value: Lambda.NumCaptures, /*Width=*/BitsWidth: 15);
7278 LambdaBits.addBit(Value: Lambda.HasKnownInternalLinkage);
7279 Record->push_back(Elt: LambdaBits);
7280
7281 Record->push_back(Elt: Lambda.NumExplicitCaptures);
7282 Record->push_back(Elt: Lambda.ManglingNumber);
7283 Record->push_back(Elt: D->getDeviceLambdaManglingNumber());
7284 // The lambda context declaration and index within the context are provided
7285 // separately, so that they can be used for merging.
7286 AddTypeSourceInfo(TInfo: Lambda.MethodTyInfo);
7287 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
7288 const LambdaCapture &Capture = Lambda.Captures.front()[I];
7289 AddSourceLocation(Loc: Capture.getLocation());
7290
7291 BitsPacker CaptureBits;
7292 CaptureBits.addBit(Value: Capture.isImplicit());
7293 CaptureBits.addBits(Value: Capture.getCaptureKind(), /*Width=*/BitsWidth: 3);
7294 Record->push_back(Elt: CaptureBits);
7295
7296 switch (Capture.getCaptureKind()) {
7297 case LCK_StarThis:
7298 case LCK_This:
7299 case LCK_VLAType:
7300 break;
7301 case LCK_ByCopy:
7302 case LCK_ByRef:
7303 ValueDecl *Var =
7304 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
7305 AddDeclRef(D: Var);
7306 AddSourceLocation(Loc: Capture.isPackExpansion() ? Capture.getEllipsisLoc()
7307 : SourceLocation());
7308 break;
7309 }
7310 }
7311 }
7312}
7313
7314void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
7315 const Expr *Init = VD->getInit();
7316 if (!Init) {
7317 push_back(N: 0);
7318 return;
7319 }
7320
7321 uint64_t Val = 1;
7322 if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
7323 // This may trigger evaluation, so run it first
7324 if (VD->hasInitWithSideEffects())
7325 Val |= 16;
7326 assert(ES->CheckedForSideEffects);
7327 Val |= (ES->HasConstantInitialization ? 2 : 0);
7328 Val |= (ES->HasConstantDestruction ? 4 : 0);
7329 APValue *Evaluated = VD->getEvaluatedValue();
7330 // If the evaluated result is constant, emit it.
7331 if (Evaluated && (Evaluated->isInt() || Evaluated->isFloat()))
7332 Val |= 8;
7333 }
7334 push_back(N: Val);
7335 if (Val & 8) {
7336 AddAPValue(Value: *VD->getEvaluatedValue());
7337 }
7338
7339 writeStmtRef(S: Init);
7340}
7341
7342void ASTWriter::ReaderInitialized(ASTReader *Reader) {
7343 assert(Reader && "Cannot remove chain");
7344 assert((!Chain || Chain == Reader) && "Cannot replace chain");
7345 assert(FirstDeclID == NextDeclID &&
7346 FirstTypeID == NextTypeID &&
7347 FirstIdentID == NextIdentID &&
7348 FirstMacroID == NextMacroID &&
7349 FirstSubmoduleID == NextSubmoduleID &&
7350 FirstSelectorID == NextSelectorID &&
7351 "Setting chain after writing has started.");
7352
7353 Chain = Reader;
7354
7355 // Note, this will get called multiple times, once one the reader starts up
7356 // and again each time it's done reading a PCH or module.
7357 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
7358 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
7359 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
7360 NextMacroID = FirstMacroID;
7361 NextSelectorID = FirstSelectorID;
7362 NextSubmoduleID = FirstSubmoduleID;
7363}
7364
7365void ASTWriter::IdentifierRead(IdentifierID ID, IdentifierInfo *II) {
7366 // Don't reuse Type ID from external modules for named modules. See the
7367 // comments in WriteASTCore for details.
7368 if (isWritingStdCXXNamedModules())
7369 return;
7370
7371 IdentifierID &StoredID = IdentifierIDs[II];
7372 unsigned OriginalModuleFileIndex = StoredID >> 32;
7373
7374 // Always keep the local identifier ID. See \p TypeRead() for more
7375 // information.
7376 if (OriginalModuleFileIndex == 0 && StoredID)
7377 return;
7378
7379 // Otherwise, keep the highest ID since the module file comes later has
7380 // higher module file indexes.
7381 if (ID > StoredID)
7382 StoredID = ID;
7383}
7384
7385void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
7386 // Always keep the highest ID. See \p TypeRead() for more information.
7387 MacroID &StoredID = MacroIDs[MI];
7388 if (ID > StoredID)
7389 StoredID = ID;
7390}
7391
7392void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
7393 // Don't reuse Type ID from external modules for named modules. See the
7394 // comments in WriteASTCore for details.
7395 if (isWritingStdCXXNamedModules())
7396 return;
7397
7398 // Always take the type index that comes in later module files.
7399 // This copes with an interesting
7400 // case for chained AST writing where we schedule writing the type and then,
7401 // later, deserialize the type from another AST. In this case, we want to
7402 // keep the entry from a later module so that we can properly write it out to
7403 // the AST file.
7404 TypeIdx &StoredIdx = TypeIdxs[T];
7405
7406 // Ignore it if the type comes from the current being written module file.
7407 // Since the current module file being written logically has the highest
7408 // index.
7409 unsigned ModuleFileIndex = StoredIdx.getModuleFileIndex();
7410 if (ModuleFileIndex == 0 && StoredIdx.getValue())
7411 return;
7412
7413 // Otherwise, keep the highest ID since the module file comes later has
7414 // higher module file indexes.
7415 if (Idx.getModuleFileIndex() >= StoredIdx.getModuleFileIndex())
7416 StoredIdx = Idx;
7417}
7418
7419void ASTWriter::PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {
7420 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
7421 DeclIDs[D] = LocalDeclID(ID);
7422 PredefinedDecls.insert(Ptr: D);
7423}
7424
7425void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
7426 // Always keep the highest ID. See \p TypeRead() for more information.
7427 SelectorID &StoredID = SelectorIDs[S];
7428 if (ID > StoredID)
7429 StoredID = ID;
7430}
7431
7432void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
7433 MacroDefinitionRecord *MD) {
7434 assert(!MacroDefinitions.contains(MD));
7435 MacroDefinitions[MD] = ID;
7436}
7437
7438void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
7439 assert(!SubmoduleIDs.contains(Mod));
7440 SubmoduleIDs[Mod] = ID;
7441}
7442
7443void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
7444 if (Chain && Chain->isProcessingUpdateRecords()) return;
7445 assert(D->isCompleteDefinition());
7446 assert(!WritingAST && "Already writing the AST!");
7447 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) {
7448 // We are interested when a PCH decl is modified.
7449 if (RD->isFromASTFile()) {
7450 // A forward reference was mutated into a definition. Rewrite it.
7451 // FIXME: This happens during template instantiation, should we
7452 // have created a new definition decl instead ?
7453 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
7454 "completed a tag from another module but not by instantiation?");
7455 DeclUpdates[RD].push_back(
7456 Elt: DeclUpdate(DeclUpdateKind::CXXInstantiatedClassDefinition));
7457 }
7458 }
7459}
7460
7461static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
7462 if (D->isFromASTFile())
7463 return true;
7464
7465 // The predefined __va_list_tag struct is imported if we imported any decls.
7466 // FIXME: This is a gross hack.
7467 return D == D->getASTContext().getVaListTagDecl();
7468}
7469
7470void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
7471 if (Chain && Chain->isProcessingUpdateRecords()) return;
7472 assert(DC->isLookupContext() &&
7473 "Should not add lookup results to non-lookup contexts!");
7474
7475 // TU is handled elsewhere.
7476 if (isa<TranslationUnitDecl>(Val: DC))
7477 return;
7478
7479 // Namespaces are handled elsewhere, except for template instantiations of
7480 // FunctionTemplateDecls in namespaces. We are interested in cases where the
7481 // local instantiations are added to an imported context. Only happens when
7482 // adding ADL lookup candidates, for example templated friends.
7483 if (isa<NamespaceDecl>(Val: DC) && D->getFriendObjectKind() == Decl::FOK_None &&
7484 !isa<FunctionTemplateDecl>(Val: D))
7485 return;
7486
7487 // We're only interested in cases where a local declaration is added to an
7488 // imported context.
7489 if (D->isFromASTFile() || !isImportedDeclContext(Chain, D: cast<Decl>(Val: DC)))
7490 return;
7491
7492 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
7493 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
7494 assert(!WritingAST && "Already writing the AST!");
7495 if (UpdatedDeclContexts.insert(X: DC) && !cast<Decl>(Val: DC)->isFromASTFile()) {
7496 // We're adding a visible declaration to a predefined decl context. Ensure
7497 // that we write out all of its lookup results so we don't get a nasty
7498 // surprise when we try to emit its lookup table.
7499 llvm::append_range(C&: DeclsToEmitEvenIfUnreferenced, R: DC->decls());
7500 }
7501 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7502}
7503
7504void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
7505 if (Chain && Chain->isProcessingUpdateRecords()) return;
7506 assert(D->isImplicit());
7507
7508 // We're only interested in cases where a local declaration is added to an
7509 // imported context.
7510 if (D->isFromASTFile() || !isImportedDeclContext(Chain, D: RD))
7511 return;
7512
7513 if (!isa<CXXMethodDecl>(Val: D))
7514 return;
7515
7516 // A decl coming from PCH was modified.
7517 assert(RD->isCompleteDefinition());
7518 assert(!WritingAST && "Already writing the AST!");
7519 DeclUpdates[RD].push_back(
7520 Elt: DeclUpdate(DeclUpdateKind::CXXAddedImplicitMember, D));
7521}
7522
7523void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
7524 if (Chain && Chain->isProcessingUpdateRecords()) return;
7525 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
7526 if (!Chain) return;
7527 Chain->forEachImportedKeyDecl(D: FD, Visit: [&](const Decl *D) {
7528 // If we don't already know the exception specification for this redecl
7529 // chain, add an update record for it.
7530 if (isUnresolvedExceptionSpec(ESpecType: cast<FunctionDecl>(Val: D)
7531 ->getType()
7532 ->castAs<FunctionProtoType>()
7533 ->getExceptionSpecType()))
7534 DeclUpdates[D].push_back(Elt: DeclUpdateKind::CXXResolvedExceptionSpec);
7535 });
7536}
7537
7538void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
7539 if (Chain && Chain->isProcessingUpdateRecords()) return;
7540 assert(!WritingAST && "Already writing the AST!");
7541 if (!Chain) return;
7542 Chain->forEachImportedKeyDecl(D: FD, Visit: [&](const Decl *D) {
7543 DeclUpdates[D].push_back(
7544 Elt: DeclUpdate(DeclUpdateKind::CXXDeducedReturnType, ReturnType));
7545 });
7546}
7547
7548void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
7549 const FunctionDecl *Delete,
7550 Expr *ThisArg) {
7551 if (Chain && Chain->isProcessingUpdateRecords()) return;
7552 assert(!WritingAST && "Already writing the AST!");
7553 assert(Delete && "Not given an operator delete");
7554 if (!Chain) return;
7555 Chain->forEachImportedKeyDecl(D: DD, Visit: [&](const Decl *D) {
7556 DeclUpdates[D].push_back(
7557 Elt: DeclUpdate(DeclUpdateKind::CXXResolvedDtorDelete, Delete));
7558 });
7559}
7560
7561void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
7562 if (Chain && Chain->isProcessingUpdateRecords()) return;
7563 assert(!WritingAST && "Already writing the AST!");
7564 if (!D->isFromASTFile())
7565 return; // Declaration not imported from PCH.
7566
7567 // The function definition may not have a body due to parsing errors.
7568 if (!D->doesThisDeclarationHaveABody())
7569 return;
7570
7571 // Implicit function decl from a PCH was defined.
7572 DeclUpdates[D].push_back(
7573 Elt: DeclUpdate(DeclUpdateKind::CXXAddedFunctionDefinition));
7574}
7575
7576void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
7577 if (Chain && Chain->isProcessingUpdateRecords()) return;
7578 assert(!WritingAST && "Already writing the AST!");
7579 if (!D->isFromASTFile())
7580 return;
7581
7582 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::CXXAddedVarDefinition));
7583}
7584
7585void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
7586 if (Chain && Chain->isProcessingUpdateRecords()) return;
7587 assert(!WritingAST && "Already writing the AST!");
7588 if (!D->isFromASTFile())
7589 return;
7590
7591 // The function definition may not have a body due to parsing errors.
7592 if (!D->doesThisDeclarationHaveABody())
7593 return;
7594
7595 DeclUpdates[D].push_back(
7596 Elt: DeclUpdate(DeclUpdateKind::CXXAddedFunctionDefinition));
7597}
7598
7599void ASTWriter::InstantiationRequested(const ValueDecl *D) {
7600 if (Chain && Chain->isProcessingUpdateRecords()) return;
7601 assert(!WritingAST && "Already writing the AST!");
7602 if (!D->isFromASTFile())
7603 return;
7604
7605 // Since the actual instantiation is delayed, this really means that we need
7606 // to update the instantiation location.
7607 SourceLocation POI;
7608 if (auto *VD = dyn_cast<VarDecl>(Val: D))
7609 POI = VD->getPointOfInstantiation();
7610 else
7611 POI = cast<FunctionDecl>(Val: D)->getPointOfInstantiation();
7612 DeclUpdates[D].push_back(
7613 Elt: DeclUpdate(DeclUpdateKind::CXXPointOfInstantiation, POI));
7614}
7615
7616void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
7617 if (Chain && Chain->isProcessingUpdateRecords()) return;
7618 assert(!WritingAST && "Already writing the AST!");
7619 if (!D->isFromASTFile())
7620 return;
7621
7622 DeclUpdates[D].push_back(
7623 Elt: DeclUpdate(DeclUpdateKind::CXXInstantiatedDefaultArgument, D));
7624}
7625
7626void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
7627 assert(!WritingAST && "Already writing the AST!");
7628 if (!D->isFromASTFile())
7629 return;
7630
7631 DeclUpdates[D].push_back(
7632 Elt: DeclUpdate(DeclUpdateKind::CXXInstantiatedDefaultMemberInitializer, D));
7633}
7634
7635void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
7636 const ObjCInterfaceDecl *IFD) {
7637 if (Chain && Chain->isProcessingUpdateRecords()) return;
7638 assert(!WritingAST && "Already writing the AST!");
7639 if (!IFD->isFromASTFile())
7640 return; // Declaration not imported from PCH.
7641
7642 assert(IFD->getDefinition() && "Category on a class without a definition?");
7643 ObjCClassesWithCategories.insert(
7644 X: const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
7645}
7646
7647void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
7648 if (Chain && Chain->isProcessingUpdateRecords()) return;
7649 assert(!WritingAST && "Already writing the AST!");
7650
7651 // If there is *any* declaration of the entity that's not from an AST file,
7652 // we can skip writing the update record. We make sure that isUsed() triggers
7653 // completion of the redeclaration chain of the entity.
7654 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
7655 if (IsLocalDecl(D: Prev))
7656 return;
7657
7658 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::DeclMarkedUsed));
7659}
7660
7661void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
7662 if (Chain && Chain->isProcessingUpdateRecords()) return;
7663 assert(!WritingAST && "Already writing the AST!");
7664 if (!D->isFromASTFile())
7665 return;
7666
7667 DeclUpdates[D].push_back(
7668 Elt: DeclUpdate(DeclUpdateKind::DeclMarkedOpenMPThreadPrivate));
7669}
7670
7671void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
7672 if (Chain && Chain->isProcessingUpdateRecords()) return;
7673 assert(!WritingAST && "Already writing the AST!");
7674 if (!D->isFromASTFile())
7675 return;
7676
7677 DeclUpdates[D].push_back(
7678 Elt: DeclUpdate(DeclUpdateKind::DeclMarkedOpenMPAllocate, A));
7679}
7680
7681void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
7682 const Attr *Attr) {
7683 if (Chain && Chain->isProcessingUpdateRecords()) return;
7684 assert(!WritingAST && "Already writing the AST!");
7685 if (!D->isFromASTFile())
7686 return;
7687
7688 DeclUpdates[D].push_back(
7689 Elt: DeclUpdate(DeclUpdateKind::DeclMarkedOpenMPDeclareTarget, Attr));
7690}
7691
7692void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
7693 if (Chain && Chain->isProcessingUpdateRecords()) return;
7694 assert(!WritingAST && "Already writing the AST!");
7695 assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
7696 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::DeclExported, M));
7697}
7698
7699void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
7700 const RecordDecl *Record) {
7701 if (Chain && Chain->isProcessingUpdateRecords()) return;
7702 assert(!WritingAST && "Already writing the AST!");
7703 if (!Record->isFromASTFile())
7704 return;
7705 DeclUpdates[Record].push_back(
7706 Elt: DeclUpdate(DeclUpdateKind::AddedAttrToRecord, Attr));
7707}
7708
7709void ASTWriter::AddedCXXTemplateSpecialization(
7710 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
7711 assert(!WritingAST && "Already writing the AST!");
7712
7713 if (!TD->getFirstDecl()->isFromASTFile())
7714 return;
7715 if (Chain && Chain->isProcessingUpdateRecords())
7716 return;
7717
7718 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7719}
7720
7721void ASTWriter::AddedCXXTemplateSpecialization(
7722 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
7723 assert(!WritingAST && "Already writing the AST!");
7724
7725 if (!TD->getFirstDecl()->isFromASTFile())
7726 return;
7727 if (Chain && Chain->isProcessingUpdateRecords())
7728 return;
7729
7730 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7731}
7732
7733void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
7734 const FunctionDecl *D) {
7735 assert(!WritingAST && "Already writing the AST!");
7736
7737 if (!TD->getFirstDecl()->isFromASTFile())
7738 return;
7739 if (Chain && Chain->isProcessingUpdateRecords())
7740 return;
7741
7742 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7743}
7744
7745//===----------------------------------------------------------------------===//
7746//// OMPClause Serialization
7747////===----------------------------------------------------------------------===//
7748
7749namespace {
7750
7751class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
7752 ASTRecordWriter &Record;
7753
7754public:
7755 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
7756#define GEN_CLANG_CLAUSE_CLASS
7757#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
7758#include "llvm/Frontend/OpenMP/OMP.inc"
7759 void writeClause(OMPClause *C);
7760 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
7761 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
7762};
7763
7764}
7765
7766void ASTRecordWriter::writeOMPClause(OMPClause *C) {
7767 OMPClauseWriter(*this).writeClause(C);
7768}
7769
7770void OMPClauseWriter::writeClause(OMPClause *C) {
7771 Record.push_back(N: unsigned(C->getClauseKind()));
7772 Visit(S: C);
7773 Record.AddSourceLocation(Loc: C->getBeginLoc());
7774 Record.AddSourceLocation(Loc: C->getEndLoc());
7775}
7776
7777void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
7778 Record.push_back(N: uint64_t(C->getCaptureRegion()));
7779 Record.AddStmt(S: C->getPreInitStmt());
7780}
7781
7782void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
7783 VisitOMPClauseWithPreInit(C);
7784 Record.AddStmt(S: C->getPostUpdateExpr());
7785}
7786
7787void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
7788 VisitOMPClauseWithPreInit(C);
7789 Record.push_back(N: uint64_t(C->getNameModifier()));
7790 Record.AddSourceLocation(Loc: C->getNameModifierLoc());
7791 Record.AddSourceLocation(Loc: C->getColonLoc());
7792 Record.AddStmt(S: C->getCondition());
7793 Record.AddSourceLocation(Loc: C->getLParenLoc());
7794}
7795
7796void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
7797 VisitOMPClauseWithPreInit(C);
7798 Record.AddStmt(S: C->getCondition());
7799 Record.AddSourceLocation(Loc: C->getLParenLoc());
7800}
7801
7802void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
7803 VisitOMPClauseWithPreInit(C);
7804 Record.writeEnum(value: C->getModifier());
7805 Record.AddStmt(S: C->getNumThreads());
7806 Record.AddSourceLocation(Loc: C->getModifierLoc());
7807 Record.AddSourceLocation(Loc: C->getLParenLoc());
7808}
7809
7810void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
7811 Record.AddStmt(S: C->getSafelen());
7812 Record.AddSourceLocation(Loc: C->getLParenLoc());
7813}
7814
7815void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
7816 Record.AddStmt(S: C->getSimdlen());
7817 Record.AddSourceLocation(Loc: C->getLParenLoc());
7818}
7819
7820void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause *C) {
7821 Record.push_back(N: C->getNumSizes());
7822 for (Expr *Size : C->getSizesRefs())
7823 Record.AddStmt(S: Size);
7824 Record.AddSourceLocation(Loc: C->getLParenLoc());
7825}
7826
7827void OMPClauseWriter::VisitOMPPermutationClause(OMPPermutationClause *C) {
7828 Record.push_back(N: C->getNumLoops());
7829 for (Expr *Size : C->getArgsRefs())
7830 Record.AddStmt(S: Size);
7831 Record.AddSourceLocation(Loc: C->getLParenLoc());
7832}
7833
7834void OMPClauseWriter::VisitOMPFullClause(OMPFullClause *C) {}
7835
7836void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause *C) {
7837 Record.AddStmt(S: C->getFactor());
7838 Record.AddSourceLocation(Loc: C->getLParenLoc());
7839}
7840
7841void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
7842 Record.AddStmt(S: C->getAllocator());
7843 Record.AddSourceLocation(Loc: C->getLParenLoc());
7844}
7845
7846void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
7847 Record.AddStmt(S: C->getNumForLoops());
7848 Record.AddSourceLocation(Loc: C->getLParenLoc());
7849}
7850
7851void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
7852 Record.AddStmt(S: C->getEventHandler());
7853 Record.AddSourceLocation(Loc: C->getLParenLoc());
7854}
7855
7856void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
7857 Record.push_back(N: unsigned(C->getDefaultKind()));
7858 Record.AddSourceLocation(Loc: C->getLParenLoc());
7859 Record.AddSourceLocation(Loc: C->getDefaultKindKwLoc());
7860}
7861
7862void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
7863 Record.push_back(N: unsigned(C->getProcBindKind()));
7864 Record.AddSourceLocation(Loc: C->getLParenLoc());
7865 Record.AddSourceLocation(Loc: C->getProcBindKindKwLoc());
7866}
7867
7868void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
7869 VisitOMPClauseWithPreInit(C);
7870 Record.push_back(N: C->getScheduleKind());
7871 Record.push_back(N: C->getFirstScheduleModifier());
7872 Record.push_back(N: C->getSecondScheduleModifier());
7873 Record.AddStmt(S: C->getChunkSize());
7874 Record.AddSourceLocation(Loc: C->getLParenLoc());
7875 Record.AddSourceLocation(Loc: C->getFirstScheduleModifierLoc());
7876 Record.AddSourceLocation(Loc: C->getSecondScheduleModifierLoc());
7877 Record.AddSourceLocation(Loc: C->getScheduleKindLoc());
7878 Record.AddSourceLocation(Loc: C->getCommaLoc());
7879}
7880
7881void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
7882 Record.push_back(N: C->getLoopNumIterations().size());
7883 Record.AddStmt(S: C->getNumForLoops());
7884 for (Expr *NumIter : C->getLoopNumIterations())
7885 Record.AddStmt(S: NumIter);
7886 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
7887 Record.AddStmt(S: C->getLoopCounter(NumLoop: I));
7888 Record.AddSourceLocation(Loc: C->getLParenLoc());
7889}
7890
7891void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
7892
7893void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
7894
7895void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
7896
7897void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
7898
7899void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
7900
7901void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
7902 Record.push_back(N: C->isExtended() ? 1 : 0);
7903 if (C->isExtended()) {
7904 Record.AddSourceLocation(Loc: C->getLParenLoc());
7905 Record.AddSourceLocation(Loc: C->getArgumentLoc());
7906 Record.writeEnum(value: C->getDependencyKind());
7907 }
7908}
7909
7910void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
7911
7912void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause *) {}
7913
7914// Save the parameter of fail clause.
7915void OMPClauseWriter::VisitOMPFailClause(OMPFailClause *C) {
7916 Record.AddSourceLocation(Loc: C->getLParenLoc());
7917 Record.AddSourceLocation(Loc: C->getFailParameterLoc());
7918 Record.writeEnum(value: C->getFailParameter());
7919}
7920
7921void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
7922
7923void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
7924
7925void OMPClauseWriter::VisitOMPAbsentClause(OMPAbsentClause *C) {
7926 Record.push_back(N: static_cast<uint64_t>(C->getDirectiveKinds().size()));
7927 Record.AddSourceLocation(Loc: C->getLParenLoc());
7928 for (auto K : C->getDirectiveKinds()) {
7929 Record.writeEnum(value: K);
7930 }
7931}
7932
7933void OMPClauseWriter::VisitOMPHoldsClause(OMPHoldsClause *C) {
7934 Record.AddStmt(S: C->getExpr());
7935 Record.AddSourceLocation(Loc: C->getLParenLoc());
7936}
7937
7938void OMPClauseWriter::VisitOMPContainsClause(OMPContainsClause *C) {
7939 Record.push_back(N: static_cast<uint64_t>(C->getDirectiveKinds().size()));
7940 Record.AddSourceLocation(Loc: C->getLParenLoc());
7941 for (auto K : C->getDirectiveKinds()) {
7942 Record.writeEnum(value: K);
7943 }
7944}
7945
7946void OMPClauseWriter::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
7947
7948void OMPClauseWriter::VisitOMPNoOpenMPRoutinesClause(
7949 OMPNoOpenMPRoutinesClause *) {}
7950
7951void OMPClauseWriter::VisitOMPNoOpenMPConstructsClause(
7952 OMPNoOpenMPConstructsClause *) {}
7953
7954void OMPClauseWriter::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
7955
7956void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
7957
7958void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
7959
7960void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
7961
7962void OMPClauseWriter::VisitOMPWeakClause(OMPWeakClause *) {}
7963
7964void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
7965
7966void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
7967
7968void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
7969
7970void OMPClauseWriter::VisitOMPInitClause(OMPInitClause *C) {
7971 Record.push_back(N: C->varlist_size());
7972 for (Expr *VE : C->varlist())
7973 Record.AddStmt(S: VE);
7974 Record.writeBool(Value: C->getIsTarget());
7975 Record.writeBool(Value: C->getIsTargetSync());
7976 Record.AddSourceLocation(Loc: C->getLParenLoc());
7977 Record.AddSourceLocation(Loc: C->getVarLoc());
7978}
7979
7980void OMPClauseWriter::VisitOMPUseClause(OMPUseClause *C) {
7981 Record.AddStmt(S: C->getInteropVar());
7982 Record.AddSourceLocation(Loc: C->getLParenLoc());
7983 Record.AddSourceLocation(Loc: C->getVarLoc());
7984}
7985
7986void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *C) {
7987 Record.AddStmt(S: C->getInteropVar());
7988 Record.AddSourceLocation(Loc: C->getLParenLoc());
7989 Record.AddSourceLocation(Loc: C->getVarLoc());
7990}
7991
7992void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
7993 VisitOMPClauseWithPreInit(C);
7994 Record.AddStmt(S: C->getCondition());
7995 Record.AddSourceLocation(Loc: C->getLParenLoc());
7996}
7997
7998void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause *C) {
7999 VisitOMPClauseWithPreInit(C);
8000 Record.AddStmt(S: C->getCondition());
8001 Record.AddSourceLocation(Loc: C->getLParenLoc());
8002}
8003
8004void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause *C) {
8005 VisitOMPClauseWithPreInit(C);
8006 Record.AddStmt(S: C->getThreadID());
8007 Record.AddSourceLocation(Loc: C->getLParenLoc());
8008}
8009
8010void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause *C) {
8011 Record.AddStmt(S: C->getAlignment());
8012 Record.AddSourceLocation(Loc: C->getLParenLoc());
8013}
8014
8015void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
8016 Record.push_back(N: C->varlist_size());
8017 Record.AddSourceLocation(Loc: C->getLParenLoc());
8018 for (auto *VE : C->varlist()) {
8019 Record.AddStmt(S: VE);
8020 }
8021 for (auto *VE : C->private_copies()) {
8022 Record.AddStmt(S: VE);
8023 }
8024}
8025
8026void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
8027 Record.push_back(N: C->varlist_size());
8028 VisitOMPClauseWithPreInit(C);
8029 Record.AddSourceLocation(Loc: C->getLParenLoc());
8030 for (auto *VE : C->varlist()) {
8031 Record.AddStmt(S: VE);
8032 }
8033 for (auto *VE : C->private_copies()) {
8034 Record.AddStmt(S: VE);
8035 }
8036 for (auto *VE : C->inits()) {
8037 Record.AddStmt(S: VE);
8038 }
8039}
8040
8041void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
8042 Record.push_back(N: C->varlist_size());
8043 VisitOMPClauseWithPostUpdate(C);
8044 Record.AddSourceLocation(Loc: C->getLParenLoc());
8045 Record.writeEnum(value: C->getKind());
8046 Record.AddSourceLocation(Loc: C->getKindLoc());
8047 Record.AddSourceLocation(Loc: C->getColonLoc());
8048 for (auto *VE : C->varlist())
8049 Record.AddStmt(S: VE);
8050 for (auto *E : C->private_copies())
8051 Record.AddStmt(S: E);
8052 for (auto *E : C->source_exprs())
8053 Record.AddStmt(S: E);
8054 for (auto *E : C->destination_exprs())
8055 Record.AddStmt(S: E);
8056 for (auto *E : C->assignment_ops())
8057 Record.AddStmt(S: E);
8058}
8059
8060void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
8061 Record.push_back(N: C->varlist_size());
8062 Record.AddSourceLocation(Loc: C->getLParenLoc());
8063 for (auto *VE : C->varlist())
8064 Record.AddStmt(S: VE);
8065}
8066
8067void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
8068 Record.push_back(N: C->varlist_size());
8069 Record.writeEnum(value: C->getModifier());
8070 VisitOMPClauseWithPostUpdate(C);
8071 Record.AddSourceLocation(Loc: C->getLParenLoc());
8072 Record.AddSourceLocation(Loc: C->getModifierLoc());
8073 Record.AddSourceLocation(Loc: C->getColonLoc());
8074 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
8075 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
8076 for (auto *VE : C->varlist())
8077 Record.AddStmt(S: VE);
8078 for (auto *VE : C->privates())
8079 Record.AddStmt(S: VE);
8080 for (auto *E : C->lhs_exprs())
8081 Record.AddStmt(S: E);
8082 for (auto *E : C->rhs_exprs())
8083 Record.AddStmt(S: E);
8084 for (auto *E : C->reduction_ops())
8085 Record.AddStmt(S: E);
8086 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
8087 for (auto *E : C->copy_ops())
8088 Record.AddStmt(S: E);
8089 for (auto *E : C->copy_array_temps())
8090 Record.AddStmt(S: E);
8091 for (auto *E : C->copy_array_elems())
8092 Record.AddStmt(S: E);
8093 }
8094 auto PrivateFlags = C->private_var_reduction_flags();
8095 Record.push_back(N: std::distance(first: PrivateFlags.begin(), last: PrivateFlags.end()));
8096 for (bool Flag : PrivateFlags)
8097 Record.push_back(N: Flag);
8098}
8099
8100void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
8101 Record.push_back(N: C->varlist_size());
8102 VisitOMPClauseWithPostUpdate(C);
8103 Record.AddSourceLocation(Loc: C->getLParenLoc());
8104 Record.AddSourceLocation(Loc: C->getColonLoc());
8105 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
8106 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
8107 for (auto *VE : C->varlist())
8108 Record.AddStmt(S: VE);
8109 for (auto *VE : C->privates())
8110 Record.AddStmt(S: VE);
8111 for (auto *E : C->lhs_exprs())
8112 Record.AddStmt(S: E);
8113 for (auto *E : C->rhs_exprs())
8114 Record.AddStmt(S: E);
8115 for (auto *E : C->reduction_ops())
8116 Record.AddStmt(S: E);
8117}
8118
8119void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
8120 Record.push_back(N: C->varlist_size());
8121 VisitOMPClauseWithPostUpdate(C);
8122 Record.AddSourceLocation(Loc: C->getLParenLoc());
8123 Record.AddSourceLocation(Loc: C->getColonLoc());
8124 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
8125 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
8126 for (auto *VE : C->varlist())
8127 Record.AddStmt(S: VE);
8128 for (auto *VE : C->privates())
8129 Record.AddStmt(S: VE);
8130 for (auto *E : C->lhs_exprs())
8131 Record.AddStmt(S: E);
8132 for (auto *E : C->rhs_exprs())
8133 Record.AddStmt(S: E);
8134 for (auto *E : C->reduction_ops())
8135 Record.AddStmt(S: E);
8136 for (auto *E : C->taskgroup_descriptors())
8137 Record.AddStmt(S: E);
8138}
8139
8140void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
8141 Record.push_back(N: C->varlist_size());
8142 VisitOMPClauseWithPostUpdate(C);
8143 Record.AddSourceLocation(Loc: C->getLParenLoc());
8144 Record.AddSourceLocation(Loc: C->getColonLoc());
8145 Record.push_back(N: C->getModifier());
8146 Record.AddSourceLocation(Loc: C->getModifierLoc());
8147 for (auto *VE : C->varlist()) {
8148 Record.AddStmt(S: VE);
8149 }
8150 for (auto *VE : C->privates()) {
8151 Record.AddStmt(S: VE);
8152 }
8153 for (auto *VE : C->inits()) {
8154 Record.AddStmt(S: VE);
8155 }
8156 for (auto *VE : C->updates()) {
8157 Record.AddStmt(S: VE);
8158 }
8159 for (auto *VE : C->finals()) {
8160 Record.AddStmt(S: VE);
8161 }
8162 Record.AddStmt(S: C->getStep());
8163 Record.AddStmt(S: C->getCalcStep());
8164 for (auto *VE : C->used_expressions())
8165 Record.AddStmt(S: VE);
8166}
8167
8168void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
8169 Record.push_back(N: C->varlist_size());
8170 Record.AddSourceLocation(Loc: C->getLParenLoc());
8171 Record.AddSourceLocation(Loc: C->getColonLoc());
8172 for (auto *VE : C->varlist())
8173 Record.AddStmt(S: VE);
8174 Record.AddStmt(S: C->getAlignment());
8175}
8176
8177void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
8178 Record.push_back(N: C->varlist_size());
8179 Record.AddSourceLocation(Loc: C->getLParenLoc());
8180 for (auto *VE : C->varlist())
8181 Record.AddStmt(S: VE);
8182 for (auto *E : C->source_exprs())
8183 Record.AddStmt(S: E);
8184 for (auto *E : C->destination_exprs())
8185 Record.AddStmt(S: E);
8186 for (auto *E : C->assignment_ops())
8187 Record.AddStmt(S: E);
8188}
8189
8190void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
8191 Record.push_back(N: C->varlist_size());
8192 Record.AddSourceLocation(Loc: C->getLParenLoc());
8193 for (auto *VE : C->varlist())
8194 Record.AddStmt(S: VE);
8195 for (auto *E : C->source_exprs())
8196 Record.AddStmt(S: E);
8197 for (auto *E : C->destination_exprs())
8198 Record.AddStmt(S: E);
8199 for (auto *E : C->assignment_ops())
8200 Record.AddStmt(S: E);
8201}
8202
8203void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
8204 Record.push_back(N: C->varlist_size());
8205 Record.AddSourceLocation(Loc: C->getLParenLoc());
8206 for (auto *VE : C->varlist())
8207 Record.AddStmt(S: VE);
8208}
8209
8210void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
8211 Record.AddStmt(S: C->getDepobj());
8212 Record.AddSourceLocation(Loc: C->getLParenLoc());
8213}
8214
8215void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
8216 Record.push_back(N: C->varlist_size());
8217 Record.push_back(N: C->getNumLoops());
8218 Record.AddSourceLocation(Loc: C->getLParenLoc());
8219 Record.AddStmt(S: C->getModifier());
8220 Record.push_back(N: C->getDependencyKind());
8221 Record.AddSourceLocation(Loc: C->getDependencyLoc());
8222 Record.AddSourceLocation(Loc: C->getColonLoc());
8223 Record.AddSourceLocation(Loc: C->getOmpAllMemoryLoc());
8224 for (auto *VE : C->varlist())
8225 Record.AddStmt(S: VE);
8226 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
8227 Record.AddStmt(S: C->getLoopData(NumLoop: I));
8228}
8229
8230void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
8231 VisitOMPClauseWithPreInit(C);
8232 Record.writeEnum(value: C->getModifier());
8233 Record.AddStmt(S: C->getDevice());
8234 Record.AddSourceLocation(Loc: C->getModifierLoc());
8235 Record.AddSourceLocation(Loc: C->getLParenLoc());
8236}
8237
8238void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
8239 Record.push_back(N: C->varlist_size());
8240 Record.push_back(N: C->getUniqueDeclarationsNum());
8241 Record.push_back(N: C->getTotalComponentListNum());
8242 Record.push_back(N: C->getTotalComponentsNum());
8243 Record.AddSourceLocation(Loc: C->getLParenLoc());
8244 bool HasIteratorModifier = false;
8245 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
8246 Record.push_back(N: C->getMapTypeModifier(Cnt: I));
8247 Record.AddSourceLocation(Loc: C->getMapTypeModifierLoc(Cnt: I));
8248 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
8249 HasIteratorModifier = true;
8250 }
8251 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
8252 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
8253 Record.push_back(N: C->getMapType());
8254 Record.AddSourceLocation(Loc: C->getMapLoc());
8255 Record.AddSourceLocation(Loc: C->getColonLoc());
8256 for (auto *E : C->varlist())
8257 Record.AddStmt(S: E);
8258 for (auto *E : C->mapperlists())
8259 Record.AddStmt(S: E);
8260 if (HasIteratorModifier)
8261 Record.AddStmt(S: C->getIteratorModifier());
8262 for (auto *D : C->all_decls())
8263 Record.AddDeclRef(D);
8264 for (auto N : C->all_num_lists())
8265 Record.push_back(N);
8266 for (auto N : C->all_lists_sizes())
8267 Record.push_back(N);
8268 for (auto &M : C->all_components()) {
8269 Record.AddStmt(S: M.getAssociatedExpression());
8270 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8271 }
8272}
8273
8274void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
8275 Record.push_back(N: C->varlist_size());
8276 Record.writeEnum(value: C->getFirstAllocateModifier());
8277 Record.writeEnum(value: C->getSecondAllocateModifier());
8278 Record.AddSourceLocation(Loc: C->getLParenLoc());
8279 Record.AddSourceLocation(Loc: C->getColonLoc());
8280 Record.AddStmt(S: C->getAllocator());
8281 Record.AddStmt(S: C->getAlignment());
8282 for (auto *VE : C->varlist())
8283 Record.AddStmt(S: VE);
8284}
8285
8286void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
8287 Record.push_back(N: C->varlist_size());
8288 VisitOMPClauseWithPreInit(C);
8289 Record.AddSourceLocation(Loc: C->getLParenLoc());
8290 for (auto *VE : C->varlist())
8291 Record.AddStmt(S: VE);
8292}
8293
8294void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
8295 Record.push_back(N: C->varlist_size());
8296 VisitOMPClauseWithPreInit(C);
8297 Record.AddSourceLocation(Loc: C->getLParenLoc());
8298 for (auto *VE : C->varlist())
8299 Record.AddStmt(S: VE);
8300}
8301
8302void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
8303 VisitOMPClauseWithPreInit(C);
8304 Record.AddStmt(S: C->getPriority());
8305 Record.AddSourceLocation(Loc: C->getLParenLoc());
8306}
8307
8308void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
8309 VisitOMPClauseWithPreInit(C);
8310 Record.writeEnum(value: C->getModifier());
8311 Record.AddStmt(S: C->getGrainsize());
8312 Record.AddSourceLocation(Loc: C->getModifierLoc());
8313 Record.AddSourceLocation(Loc: C->getLParenLoc());
8314}
8315
8316void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
8317 VisitOMPClauseWithPreInit(C);
8318 Record.writeEnum(value: C->getModifier());
8319 Record.AddStmt(S: C->getNumTasks());
8320 Record.AddSourceLocation(Loc: C->getModifierLoc());
8321 Record.AddSourceLocation(Loc: C->getLParenLoc());
8322}
8323
8324void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
8325 Record.AddStmt(S: C->getHint());
8326 Record.AddSourceLocation(Loc: C->getLParenLoc());
8327}
8328
8329void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
8330 VisitOMPClauseWithPreInit(C);
8331 Record.push_back(N: C->getDistScheduleKind());
8332 Record.AddStmt(S: C->getChunkSize());
8333 Record.AddSourceLocation(Loc: C->getLParenLoc());
8334 Record.AddSourceLocation(Loc: C->getDistScheduleKindLoc());
8335 Record.AddSourceLocation(Loc: C->getCommaLoc());
8336}
8337
8338void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
8339 Record.push_back(N: C->getDefaultmapKind());
8340 Record.push_back(N: C->getDefaultmapModifier());
8341 Record.AddSourceLocation(Loc: C->getLParenLoc());
8342 Record.AddSourceLocation(Loc: C->getDefaultmapModifierLoc());
8343 Record.AddSourceLocation(Loc: C->getDefaultmapKindLoc());
8344}
8345
8346void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
8347 Record.push_back(N: C->varlist_size());
8348 Record.push_back(N: C->getUniqueDeclarationsNum());
8349 Record.push_back(N: C->getTotalComponentListNum());
8350 Record.push_back(N: C->getTotalComponentsNum());
8351 Record.AddSourceLocation(Loc: C->getLParenLoc());
8352 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
8353 Record.push_back(N: C->getMotionModifier(Cnt: I));
8354 Record.AddSourceLocation(Loc: C->getMotionModifierLoc(Cnt: I));
8355 }
8356 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
8357 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
8358 Record.AddSourceLocation(Loc: C->getColonLoc());
8359 for (auto *E : C->varlist())
8360 Record.AddStmt(S: E);
8361 for (auto *E : C->mapperlists())
8362 Record.AddStmt(S: E);
8363 for (auto *D : C->all_decls())
8364 Record.AddDeclRef(D);
8365 for (auto N : C->all_num_lists())
8366 Record.push_back(N);
8367 for (auto N : C->all_lists_sizes())
8368 Record.push_back(N);
8369 for (auto &M : C->all_components()) {
8370 Record.AddStmt(S: M.getAssociatedExpression());
8371 Record.writeBool(Value: M.isNonContiguous());
8372 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8373 }
8374}
8375
8376void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
8377 Record.push_back(N: C->varlist_size());
8378 Record.push_back(N: C->getUniqueDeclarationsNum());
8379 Record.push_back(N: C->getTotalComponentListNum());
8380 Record.push_back(N: C->getTotalComponentsNum());
8381 Record.AddSourceLocation(Loc: C->getLParenLoc());
8382 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
8383 Record.push_back(N: C->getMotionModifier(Cnt: I));
8384 Record.AddSourceLocation(Loc: C->getMotionModifierLoc(Cnt: I));
8385 }
8386 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
8387 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
8388 Record.AddSourceLocation(Loc: C->getColonLoc());
8389 for (auto *E : C->varlist())
8390 Record.AddStmt(S: E);
8391 for (auto *E : C->mapperlists())
8392 Record.AddStmt(S: E);
8393 for (auto *D : C->all_decls())
8394 Record.AddDeclRef(D);
8395 for (auto N : C->all_num_lists())
8396 Record.push_back(N);
8397 for (auto N : C->all_lists_sizes())
8398 Record.push_back(N);
8399 for (auto &M : C->all_components()) {
8400 Record.AddStmt(S: M.getAssociatedExpression());
8401 Record.writeBool(Value: M.isNonContiguous());
8402 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8403 }
8404}
8405
8406void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
8407 Record.push_back(N: C->varlist_size());
8408 Record.push_back(N: C->getUniqueDeclarationsNum());
8409 Record.push_back(N: C->getTotalComponentListNum());
8410 Record.push_back(N: C->getTotalComponentsNum());
8411 Record.AddSourceLocation(Loc: C->getLParenLoc());
8412 for (auto *E : C->varlist())
8413 Record.AddStmt(S: E);
8414 for (auto *VE : C->private_copies())
8415 Record.AddStmt(S: VE);
8416 for (auto *VE : C->inits())
8417 Record.AddStmt(S: VE);
8418 for (auto *D : C->all_decls())
8419 Record.AddDeclRef(D);
8420 for (auto N : C->all_num_lists())
8421 Record.push_back(N);
8422 for (auto N : C->all_lists_sizes())
8423 Record.push_back(N);
8424 for (auto &M : C->all_components()) {
8425 Record.AddStmt(S: M.getAssociatedExpression());
8426 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8427 }
8428}
8429
8430void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
8431 Record.push_back(N: C->varlist_size());
8432 Record.push_back(N: C->getUniqueDeclarationsNum());
8433 Record.push_back(N: C->getTotalComponentListNum());
8434 Record.push_back(N: C->getTotalComponentsNum());
8435 Record.AddSourceLocation(Loc: C->getLParenLoc());
8436 for (auto *E : C->varlist())
8437 Record.AddStmt(S: E);
8438 for (auto *D : C->all_decls())
8439 Record.AddDeclRef(D);
8440 for (auto N : C->all_num_lists())
8441 Record.push_back(N);
8442 for (auto N : C->all_lists_sizes())
8443 Record.push_back(N);
8444 for (auto &M : C->all_components()) {
8445 Record.AddStmt(S: M.getAssociatedExpression());
8446 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8447 }
8448}
8449
8450void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
8451 Record.push_back(N: C->varlist_size());
8452 Record.push_back(N: C->getUniqueDeclarationsNum());
8453 Record.push_back(N: C->getTotalComponentListNum());
8454 Record.push_back(N: C->getTotalComponentsNum());
8455 Record.AddSourceLocation(Loc: C->getLParenLoc());
8456 for (auto *E : C->varlist())
8457 Record.AddStmt(S: E);
8458 for (auto *D : C->all_decls())
8459 Record.AddDeclRef(D);
8460 for (auto N : C->all_num_lists())
8461 Record.push_back(N);
8462 for (auto N : C->all_lists_sizes())
8463 Record.push_back(N);
8464 for (auto &M : C->all_components()) {
8465 Record.AddStmt(S: M.getAssociatedExpression());
8466 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8467 }
8468}
8469
8470void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
8471 Record.push_back(N: C->varlist_size());
8472 Record.push_back(N: C->getUniqueDeclarationsNum());
8473 Record.push_back(N: C->getTotalComponentListNum());
8474 Record.push_back(N: C->getTotalComponentsNum());
8475 Record.AddSourceLocation(Loc: C->getLParenLoc());
8476 for (auto *E : C->varlist())
8477 Record.AddStmt(S: E);
8478 for (auto *D : C->all_decls())
8479 Record.AddDeclRef(D);
8480 for (auto N : C->all_num_lists())
8481 Record.push_back(N);
8482 for (auto N : C->all_lists_sizes())
8483 Record.push_back(N);
8484 for (auto &M : C->all_components()) {
8485 Record.AddStmt(S: M.getAssociatedExpression());
8486 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8487 }
8488}
8489
8490void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
8491
8492void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
8493 OMPUnifiedSharedMemoryClause *) {}
8494
8495void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
8496
8497void
8498OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
8499}
8500
8501void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
8502 OMPAtomicDefaultMemOrderClause *C) {
8503 Record.push_back(N: C->getAtomicDefaultMemOrderKind());
8504 Record.AddSourceLocation(Loc: C->getLParenLoc());
8505 Record.AddSourceLocation(Loc: C->getAtomicDefaultMemOrderKindKwLoc());
8506}
8507
8508void OMPClauseWriter::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
8509
8510void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
8511 Record.push_back(N: C->getAtKind());
8512 Record.AddSourceLocation(Loc: C->getLParenLoc());
8513 Record.AddSourceLocation(Loc: C->getAtKindKwLoc());
8514}
8515
8516void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
8517 Record.push_back(N: C->getSeverityKind());
8518 Record.AddSourceLocation(Loc: C->getLParenLoc());
8519 Record.AddSourceLocation(Loc: C->getSeverityKindKwLoc());
8520}
8521
8522void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
8523 Record.AddStmt(S: C->getMessageString());
8524 Record.AddSourceLocation(Loc: C->getLParenLoc());
8525}
8526
8527void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
8528 Record.push_back(N: C->varlist_size());
8529 Record.AddSourceLocation(Loc: C->getLParenLoc());
8530 for (auto *VE : C->varlist())
8531 Record.AddStmt(S: VE);
8532 for (auto *E : C->private_refs())
8533 Record.AddStmt(S: E);
8534}
8535
8536void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
8537 Record.push_back(N: C->varlist_size());
8538 Record.AddSourceLocation(Loc: C->getLParenLoc());
8539 for (auto *VE : C->varlist())
8540 Record.AddStmt(S: VE);
8541}
8542
8543void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
8544 Record.push_back(N: C->varlist_size());
8545 Record.AddSourceLocation(Loc: C->getLParenLoc());
8546 for (auto *VE : C->varlist())
8547 Record.AddStmt(S: VE);
8548}
8549
8550void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
8551 Record.writeEnum(value: C->getKind());
8552 Record.writeEnum(value: C->getModifier());
8553 Record.AddSourceLocation(Loc: C->getLParenLoc());
8554 Record.AddSourceLocation(Loc: C->getKindKwLoc());
8555 Record.AddSourceLocation(Loc: C->getModifierKwLoc());
8556}
8557
8558void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
8559 Record.push_back(N: C->getNumberOfAllocators());
8560 Record.AddSourceLocation(Loc: C->getLParenLoc());
8561 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
8562 OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
8563 Record.AddStmt(S: Data.Allocator);
8564 Record.AddStmt(S: Data.AllocatorTraits);
8565 Record.AddSourceLocation(Loc: Data.LParenLoc);
8566 Record.AddSourceLocation(Loc: Data.RParenLoc);
8567 }
8568}
8569
8570void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
8571 Record.push_back(N: C->varlist_size());
8572 Record.AddSourceLocation(Loc: C->getLParenLoc());
8573 Record.AddStmt(S: C->getModifier());
8574 Record.AddSourceLocation(Loc: C->getColonLoc());
8575 for (Expr *E : C->varlist())
8576 Record.AddStmt(S: E);
8577}
8578
8579void OMPClauseWriter::VisitOMPBindClause(OMPBindClause *C) {
8580 Record.writeEnum(value: C->getBindKind());
8581 Record.AddSourceLocation(Loc: C->getLParenLoc());
8582 Record.AddSourceLocation(Loc: C->getBindKindLoc());
8583}
8584
8585void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
8586 VisitOMPClauseWithPreInit(C);
8587 Record.AddStmt(S: C->getSize());
8588 Record.AddSourceLocation(Loc: C->getLParenLoc());
8589}
8590
8591void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
8592 Record.push_back(N: C->varlist_size());
8593 Record.push_back(N: C->getNumLoops());
8594 Record.AddSourceLocation(Loc: C->getLParenLoc());
8595 Record.push_back(N: C->getDependenceType());
8596 Record.AddSourceLocation(Loc: C->getDependenceLoc());
8597 Record.AddSourceLocation(Loc: C->getColonLoc());
8598 for (auto *VE : C->varlist())
8599 Record.AddStmt(S: VE);
8600 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
8601 Record.AddStmt(S: C->getLoopData(NumLoop: I));
8602}
8603
8604void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
8605 Record.AddAttributes(Attrs: C->getAttrs());
8606 Record.AddSourceLocation(Loc: C->getBeginLoc());
8607 Record.AddSourceLocation(Loc: C->getLParenLoc());
8608 Record.AddSourceLocation(Loc: C->getEndLoc());
8609}
8610
8611void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause *C) {}
8612
8613void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
8614 writeUInt32(Value: TI->Sets.size());
8615 for (const auto &Set : TI->Sets) {
8616 writeEnum(value: Set.Kind);
8617 writeUInt32(Value: Set.Selectors.size());
8618 for (const auto &Selector : Set.Selectors) {
8619 writeEnum(value: Selector.Kind);
8620 writeBool(Value: Selector.ScoreOrCondition);
8621 if (Selector.ScoreOrCondition)
8622 writeExprRef(value: Selector.ScoreOrCondition);
8623 writeUInt32(Value: Selector.Properties.size());
8624 for (const auto &Property : Selector.Properties)
8625 writeEnum(value: Property.Kind);
8626 }
8627 }
8628}
8629
8630void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
8631 if (!Data)
8632 return;
8633 writeUInt32(Value: Data->getNumClauses());
8634 writeUInt32(Value: Data->getNumChildren());
8635 writeBool(Value: Data->hasAssociatedStmt());
8636 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
8637 writeOMPClause(C: Data->getClauses()[I]);
8638 if (Data->hasAssociatedStmt())
8639 AddStmt(S: Data->getAssociatedStmt());
8640 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
8641 AddStmt(S: Data->getChildren()[I]);
8642}
8643
8644void ASTRecordWriter::writeOpenACCVarList(const OpenACCClauseWithVarList *C) {
8645 writeUInt32(Value: C->getVarList().size());
8646 for (Expr *E : C->getVarList())
8647 AddStmt(S: E);
8648}
8649
8650void ASTRecordWriter::writeOpenACCIntExprList(ArrayRef<Expr *> Exprs) {
8651 writeUInt32(Value: Exprs.size());
8652 for (Expr *E : Exprs)
8653 AddStmt(S: E);
8654}
8655
8656void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
8657 writeEnum(value: C->getClauseKind());
8658 writeSourceLocation(Loc: C->getBeginLoc());
8659 writeSourceLocation(Loc: C->getEndLoc());
8660
8661 switch (C->getClauseKind()) {
8662 case OpenACCClauseKind::Default: {
8663 const auto *DC = cast<OpenACCDefaultClause>(Val: C);
8664 writeSourceLocation(Loc: DC->getLParenLoc());
8665 writeEnum(value: DC->getDefaultClauseKind());
8666 return;
8667 }
8668 case OpenACCClauseKind::If: {
8669 const auto *IC = cast<OpenACCIfClause>(Val: C);
8670 writeSourceLocation(Loc: IC->getLParenLoc());
8671 AddStmt(S: const_cast<Expr*>(IC->getConditionExpr()));
8672 return;
8673 }
8674 case OpenACCClauseKind::Self: {
8675 const auto *SC = cast<OpenACCSelfClause>(Val: C);
8676 writeSourceLocation(Loc: SC->getLParenLoc());
8677 writeBool(Value: SC->isConditionExprClause());
8678 if (SC->isConditionExprClause()) {
8679 writeBool(Value: SC->hasConditionExpr());
8680 if (SC->hasConditionExpr())
8681 AddStmt(S: const_cast<Expr *>(SC->getConditionExpr()));
8682 } else {
8683 writeUInt32(Value: SC->getVarList().size());
8684 for (Expr *E : SC->getVarList())
8685 AddStmt(S: E);
8686 }
8687 return;
8688 }
8689 case OpenACCClauseKind::NumGangs: {
8690 const auto *NGC = cast<OpenACCNumGangsClause>(Val: C);
8691 writeSourceLocation(Loc: NGC->getLParenLoc());
8692 writeUInt32(Value: NGC->getIntExprs().size());
8693 for (Expr *E : NGC->getIntExprs())
8694 AddStmt(S: E);
8695 return;
8696 }
8697 case OpenACCClauseKind::DeviceNum: {
8698 const auto *DNC = cast<OpenACCDeviceNumClause>(Val: C);
8699 writeSourceLocation(Loc: DNC->getLParenLoc());
8700 AddStmt(S: const_cast<Expr*>(DNC->getIntExpr()));
8701 return;
8702 }
8703 case OpenACCClauseKind::DefaultAsync: {
8704 const auto *DAC = cast<OpenACCDefaultAsyncClause>(Val: C);
8705 writeSourceLocation(Loc: DAC->getLParenLoc());
8706 AddStmt(S: const_cast<Expr *>(DAC->getIntExpr()));
8707 return;
8708 }
8709 case OpenACCClauseKind::NumWorkers: {
8710 const auto *NWC = cast<OpenACCNumWorkersClause>(Val: C);
8711 writeSourceLocation(Loc: NWC->getLParenLoc());
8712 AddStmt(S: const_cast<Expr*>(NWC->getIntExpr()));
8713 return;
8714 }
8715 case OpenACCClauseKind::VectorLength: {
8716 const auto *NWC = cast<OpenACCVectorLengthClause>(Val: C);
8717 writeSourceLocation(Loc: NWC->getLParenLoc());
8718 AddStmt(S: const_cast<Expr*>(NWC->getIntExpr()));
8719 return;
8720 }
8721 case OpenACCClauseKind::Private: {
8722 const auto *PC = cast<OpenACCPrivateClause>(Val: C);
8723 writeSourceLocation(Loc: PC->getLParenLoc());
8724 writeOpenACCVarList(C: PC);
8725 return;
8726 }
8727 case OpenACCClauseKind::Host: {
8728 const auto *HC = cast<OpenACCHostClause>(Val: C);
8729 writeSourceLocation(Loc: HC->getLParenLoc());
8730 writeOpenACCVarList(C: HC);
8731 return;
8732 }
8733 case OpenACCClauseKind::Device: {
8734 const auto *DC = cast<OpenACCDeviceClause>(Val: C);
8735 writeSourceLocation(Loc: DC->getLParenLoc());
8736 writeOpenACCVarList(C: DC);
8737 return;
8738 }
8739 case OpenACCClauseKind::FirstPrivate: {
8740 const auto *FPC = cast<OpenACCFirstPrivateClause>(Val: C);
8741 writeSourceLocation(Loc: FPC->getLParenLoc());
8742 writeOpenACCVarList(C: FPC);
8743 return;
8744 }
8745 case OpenACCClauseKind::Attach: {
8746 const auto *AC = cast<OpenACCAttachClause>(Val: C);
8747 writeSourceLocation(Loc: AC->getLParenLoc());
8748 writeOpenACCVarList(C: AC);
8749 return;
8750 }
8751 case OpenACCClauseKind::Detach: {
8752 const auto *DC = cast<OpenACCDetachClause>(Val: C);
8753 writeSourceLocation(Loc: DC->getLParenLoc());
8754 writeOpenACCVarList(C: DC);
8755 return;
8756 }
8757 case OpenACCClauseKind::Delete: {
8758 const auto *DC = cast<OpenACCDeleteClause>(Val: C);
8759 writeSourceLocation(Loc: DC->getLParenLoc());
8760 writeOpenACCVarList(C: DC);
8761 return;
8762 }
8763 case OpenACCClauseKind::UseDevice: {
8764 const auto *UDC = cast<OpenACCUseDeviceClause>(Val: C);
8765 writeSourceLocation(Loc: UDC->getLParenLoc());
8766 writeOpenACCVarList(C: UDC);
8767 return;
8768 }
8769 case OpenACCClauseKind::DevicePtr: {
8770 const auto *DPC = cast<OpenACCDevicePtrClause>(Val: C);
8771 writeSourceLocation(Loc: DPC->getLParenLoc());
8772 writeOpenACCVarList(C: DPC);
8773 return;
8774 }
8775 case OpenACCClauseKind::NoCreate: {
8776 const auto *NCC = cast<OpenACCNoCreateClause>(Val: C);
8777 writeSourceLocation(Loc: NCC->getLParenLoc());
8778 writeOpenACCVarList(C: NCC);
8779 return;
8780 }
8781 case OpenACCClauseKind::Present: {
8782 const auto *PC = cast<OpenACCPresentClause>(Val: C);
8783 writeSourceLocation(Loc: PC->getLParenLoc());
8784 writeOpenACCVarList(C: PC);
8785 return;
8786 }
8787 case OpenACCClauseKind::Copy:
8788 case OpenACCClauseKind::PCopy:
8789 case OpenACCClauseKind::PresentOrCopy: {
8790 const auto *CC = cast<OpenACCCopyClause>(Val: C);
8791 writeSourceLocation(Loc: CC->getLParenLoc());
8792 writeEnum(value: CC->getModifierList());
8793 writeOpenACCVarList(C: CC);
8794 return;
8795 }
8796 case OpenACCClauseKind::CopyIn:
8797 case OpenACCClauseKind::PCopyIn:
8798 case OpenACCClauseKind::PresentOrCopyIn: {
8799 const auto *CIC = cast<OpenACCCopyInClause>(Val: C);
8800 writeSourceLocation(Loc: CIC->getLParenLoc());
8801 writeEnum(value: CIC->getModifierList());
8802 writeOpenACCVarList(C: CIC);
8803 return;
8804 }
8805 case OpenACCClauseKind::CopyOut:
8806 case OpenACCClauseKind::PCopyOut:
8807 case OpenACCClauseKind::PresentOrCopyOut: {
8808 const auto *COC = cast<OpenACCCopyOutClause>(Val: C);
8809 writeSourceLocation(Loc: COC->getLParenLoc());
8810 writeEnum(value: COC->getModifierList());
8811 writeOpenACCVarList(C: COC);
8812 return;
8813 }
8814 case OpenACCClauseKind::Create:
8815 case OpenACCClauseKind::PCreate:
8816 case OpenACCClauseKind::PresentOrCreate: {
8817 const auto *CC = cast<OpenACCCreateClause>(Val: C);
8818 writeSourceLocation(Loc: CC->getLParenLoc());
8819 writeEnum(value: CC->getModifierList());
8820 writeOpenACCVarList(C: CC);
8821 return;
8822 }
8823 case OpenACCClauseKind::Async: {
8824 const auto *AC = cast<OpenACCAsyncClause>(Val: C);
8825 writeSourceLocation(Loc: AC->getLParenLoc());
8826 writeBool(Value: AC->hasIntExpr());
8827 if (AC->hasIntExpr())
8828 AddStmt(S: const_cast<Expr*>(AC->getIntExpr()));
8829 return;
8830 }
8831 case OpenACCClauseKind::Wait: {
8832 const auto *WC = cast<OpenACCWaitClause>(Val: C);
8833 writeSourceLocation(Loc: WC->getLParenLoc());
8834 writeBool(Value: WC->getDevNumExpr());
8835 if (Expr *DNE = WC->getDevNumExpr())
8836 AddStmt(S: DNE);
8837 writeSourceLocation(Loc: WC->getQueuesLoc());
8838
8839 writeOpenACCIntExprList(Exprs: WC->getQueueIdExprs());
8840 return;
8841 }
8842 case OpenACCClauseKind::DeviceType:
8843 case OpenACCClauseKind::DType: {
8844 const auto *DTC = cast<OpenACCDeviceTypeClause>(Val: C);
8845 writeSourceLocation(Loc: DTC->getLParenLoc());
8846 writeUInt32(Value: DTC->getArchitectures().size());
8847 for (const DeviceTypeArgument &Arg : DTC->getArchitectures()) {
8848 writeBool(Value: Arg.getIdentifierInfo());
8849 if (Arg.getIdentifierInfo())
8850 AddIdentifierRef(II: Arg.getIdentifierInfo());
8851 writeSourceLocation(Loc: Arg.getLoc());
8852 }
8853 return;
8854 }
8855 case OpenACCClauseKind::Reduction: {
8856 const auto *RC = cast<OpenACCReductionClause>(Val: C);
8857 writeSourceLocation(Loc: RC->getLParenLoc());
8858 writeEnum(value: RC->getReductionOp());
8859 writeOpenACCVarList(C: RC);
8860 return;
8861 }
8862 case OpenACCClauseKind::Seq:
8863 case OpenACCClauseKind::Independent:
8864 case OpenACCClauseKind::NoHost:
8865 case OpenACCClauseKind::Auto:
8866 case OpenACCClauseKind::Finalize:
8867 case OpenACCClauseKind::IfPresent:
8868 // Nothing to do here, there is no additional information beyond the
8869 // begin/end loc and clause kind.
8870 return;
8871 case OpenACCClauseKind::Collapse: {
8872 const auto *CC = cast<OpenACCCollapseClause>(Val: C);
8873 writeSourceLocation(Loc: CC->getLParenLoc());
8874 writeBool(Value: CC->hasForce());
8875 AddStmt(S: const_cast<Expr *>(CC->getLoopCount()));
8876 return;
8877 }
8878 case OpenACCClauseKind::Tile: {
8879 const auto *TC = cast<OpenACCTileClause>(Val: C);
8880 writeSourceLocation(Loc: TC->getLParenLoc());
8881 writeUInt32(Value: TC->getSizeExprs().size());
8882 for (Expr *E : TC->getSizeExprs())
8883 AddStmt(S: E);
8884 return;
8885 }
8886 case OpenACCClauseKind::Gang: {
8887 const auto *GC = cast<OpenACCGangClause>(Val: C);
8888 writeSourceLocation(Loc: GC->getLParenLoc());
8889 writeUInt32(Value: GC->getNumExprs());
8890 for (unsigned I = 0; I < GC->getNumExprs(); ++I) {
8891 writeEnum(value: GC->getExpr(I).first);
8892 AddStmt(S: const_cast<Expr *>(GC->getExpr(I).second));
8893 }
8894 return;
8895 }
8896 case OpenACCClauseKind::Worker: {
8897 const auto *WC = cast<OpenACCWorkerClause>(Val: C);
8898 writeSourceLocation(Loc: WC->getLParenLoc());
8899 writeBool(Value: WC->hasIntExpr());
8900 if (WC->hasIntExpr())
8901 AddStmt(S: const_cast<Expr *>(WC->getIntExpr()));
8902 return;
8903 }
8904 case OpenACCClauseKind::Vector: {
8905 const auto *VC = cast<OpenACCVectorClause>(Val: C);
8906 writeSourceLocation(Loc: VC->getLParenLoc());
8907 writeBool(Value: VC->hasIntExpr());
8908 if (VC->hasIntExpr())
8909 AddStmt(S: const_cast<Expr *>(VC->getIntExpr()));
8910 return;
8911 }
8912 case OpenACCClauseKind::Link: {
8913 const auto *LC = cast<OpenACCLinkClause>(Val: C);
8914 writeSourceLocation(Loc: LC->getLParenLoc());
8915 writeOpenACCVarList(C: LC);
8916 return;
8917 }
8918 case OpenACCClauseKind::DeviceResident: {
8919 const auto *DRC = cast<OpenACCDeviceResidentClause>(Val: C);
8920 writeSourceLocation(Loc: DRC->getLParenLoc());
8921 writeOpenACCVarList(C: DRC);
8922 return;
8923 }
8924
8925 case OpenACCClauseKind::Bind: {
8926 const auto *BC = cast<OpenACCBindClause>(Val: C);
8927 writeSourceLocation(Loc: BC->getLParenLoc());
8928 writeBool(Value: BC->isStringArgument());
8929 if (BC->isStringArgument())
8930 AddStmt(S: const_cast<StringLiteral *>(BC->getStringArgument()));
8931 else
8932 AddIdentifierRef(II: BC->getIdentifierArgument());
8933
8934 return;
8935 }
8936 case OpenACCClauseKind::Invalid:
8937 case OpenACCClauseKind::Shortloop:
8938 llvm_unreachable("Clause serialization not yet implemented");
8939 }
8940 llvm_unreachable("Invalid Clause Kind");
8941}
8942
8943void ASTRecordWriter::writeOpenACCClauseList(
8944 ArrayRef<const OpenACCClause *> Clauses) {
8945 for (const OpenACCClause *Clause : Clauses)
8946 writeOpenACCClause(C: Clause);
8947}
8948void ASTRecordWriter::AddOpenACCRoutineDeclAttr(
8949 const OpenACCRoutineDeclAttr *A) {
8950 // We have to write the size so that the reader can do a resize. Unlike the
8951 // Decl version of this, we can't count on trailing storage to get this right.
8952 writeUInt32(Value: A->Clauses.size());
8953 writeOpenACCClauseList(Clauses: A->Clauses);
8954}
8955