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, Compatibility, Description) \
1616 Record.push_back(LangOpts.Name);
1617#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
1618 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1619#include "clang/Basic/LangOptions.def"
1620#define SANITIZER(NAME, ID) \
1621 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1622#include "clang/Basic/Sanitizers.def"
1623
1624 Record.push_back(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->getTopLevelOwningNamedModule()) {
4344 if (llvm::all_of(
4345 Range: DC.noload_lookup(
4346 Name: cast<EnumDecl>(Val: ECD->getDeclContext())->getDeclName()),
4347 P: [](auto *Found) {
4348 return Found->isInvisibleOutsideTheOwningModule();
4349 }))
4350 return ECD->isFromExplicitGlobalModule() ||
4351 ECD->isInAnonymousNamespace()
4352 ? LookupVisibility::TULocal
4353 : LookupVisibility::ModuleLocalVisible;
4354 }
4355
4356 return LookupVisibility::GenerallyVisibile;
4357 }
4358
4359 DeclContext &DC;
4360 ModuleLevelDeclsMapTy ModuleLocalDeclsMap;
4361 TULocalDeclsMapTy TULocalDeclsMap;
4362
4363public:
4364 using ASTDeclContextNameTrivialLookupTrait::
4365 ASTDeclContextNameTrivialLookupTrait;
4366
4367 ASTDeclContextNameLookupTrait(ASTWriter &Writer, DeclContext &DC)
4368 : ASTDeclContextNameTrivialLookupTrait(Writer), DC(DC) {}
4369
4370 template <typename Coll> data_type getData(const Coll &Decls) {
4371 unsigned Start = DeclIDs.size();
4372 for (NamedDecl *D : Decls) {
4373 NamedDecl *DeclForLocalLookup =
4374 getDeclForLocalLookup(LangOpts: Writer.getLangOpts(), D);
4375
4376 if (Writer.getDoneWritingDeclsAndTypes() &&
4377 !Writer.wasDeclEmitted(D: DeclForLocalLookup))
4378 continue;
4379
4380 // Try to avoid writing internal decls to reduced BMI.
4381 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4382 if (Writer.isGeneratingReducedBMI() &&
4383 !DeclForLocalLookup->isFromExplicitGlobalModule() &&
4384 IsInternalDeclFromFileContext(D: DeclForLocalLookup))
4385 continue;
4386
4387 auto ID = Writer.GetDeclRef(D: DeclForLocalLookup);
4388
4389 switch (getLookupVisibility(D: DeclForLocalLookup)) {
4390 case LookupVisibility::ModuleLocalVisible:
4391 if (UnsignedOrNone PrimaryModuleHash =
4392 getPrimaryModuleHash(M: D->getOwningModule())) {
4393 auto Key = std::make_pair(x: D->getDeclName(), y: *PrimaryModuleHash);
4394 auto Iter = ModuleLocalDeclsMap.find(Val: Key);
4395 if (Iter == ModuleLocalDeclsMap.end())
4396 ModuleLocalDeclsMap.insert(KV: {Key, DeclIDsTy{ID}});
4397 else
4398 Iter->second.push_back(Elt: ID);
4399 continue;
4400 }
4401 break;
4402 case LookupVisibility::TULocal: {
4403 auto Iter = TULocalDeclsMap.find(Val: D->getDeclName());
4404 if (Iter == TULocalDeclsMap.end())
4405 TULocalDeclsMap.insert(KV: {D->getDeclName(), DeclIDsTy{ID}});
4406 else
4407 Iter->second.push_back(Elt: ID);
4408 continue;
4409 }
4410 case LookupVisibility::GenerallyVisibile:
4411 // Generally visible decls go into the general lookup table.
4412 break;
4413 }
4414
4415 DeclIDs.push_back(Elt: ID);
4416 }
4417 return std::make_pair(x&: Start, y: DeclIDs.size());
4418 }
4419
4420 const ModuleLevelDeclsMapTy &getModuleLocalDecls() {
4421 return ModuleLocalDeclsMap;
4422 }
4423
4424 const TULocalDeclsMapTy &getTULocalDecls() { return TULocalDeclsMap; }
4425};
4426
4427} // namespace
4428
4429namespace {
4430class LazySpecializationInfoLookupTrait {
4431 ASTWriter &Writer;
4432 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 64> Specs;
4433
4434public:
4435 using key_type = unsigned;
4436 using key_type_ref = key_type;
4437
4438 /// A start and end index into Specs, representing a sequence of decls.
4439 using data_type = std::pair<unsigned, unsigned>;
4440 using data_type_ref = const data_type &;
4441
4442 using hash_value_type = unsigned;
4443 using offset_type = unsigned;
4444
4445 explicit LazySpecializationInfoLookupTrait(ASTWriter &Writer)
4446 : Writer(Writer) {}
4447
4448 template <typename Col, typename Col2>
4449 data_type getData(Col &&C, Col2 &ExistingInfo) {
4450 unsigned Start = Specs.size();
4451 for (auto *D : C) {
4452 NamedDecl *ND = getDeclForLocalLookup(LangOpts: Writer.getLangOpts(),
4453 D: const_cast<NamedDecl *>(D));
4454 Specs.push_back(Elt: GlobalDeclID(Writer.GetDeclRef(D: ND).getRawValue()));
4455 }
4456 for (const serialization::reader::LazySpecializationInfo &Info :
4457 ExistingInfo)
4458 Specs.push_back(Elt: Info);
4459 return std::make_pair(x&: Start, y: Specs.size());
4460 }
4461
4462 data_type ImportData(
4463 const reader::LazySpecializationInfoLookupTrait::data_type &FromReader) {
4464 unsigned Start = Specs.size();
4465 for (auto ID : FromReader)
4466 Specs.push_back(Elt: ID);
4467 return std::make_pair(x&: Start, y: Specs.size());
4468 }
4469
4470 static bool EqualKey(key_type_ref a, key_type_ref b) { return a == b; }
4471
4472 hash_value_type ComputeHash(key_type Name) { return Name; }
4473
4474 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
4475 assert(Writer.hasChain() &&
4476 "have reference to loaded module file but no chain?");
4477
4478 using namespace llvm::support;
4479 Writer.addTouchedModuleFile(MF: F);
4480 endian::write<uint32_t>(os&: Out, value: Writer.getChain()->getModuleFileID(M: F),
4481 endian: llvm::endianness::little);
4482 }
4483
4484 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
4485 key_type HashValue,
4486 data_type_ref Lookup) {
4487 // 4 bytes for each slot.
4488 unsigned KeyLen = 4;
4489 unsigned DataLen = sizeof(serialization::reader::LazySpecializationInfo) *
4490 (Lookup.second - Lookup.first);
4491
4492 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
4493 }
4494
4495 void EmitKey(raw_ostream &Out, key_type HashValue, unsigned) {
4496 using namespace llvm::support;
4497
4498 endian::Writer LE(Out, llvm::endianness::little);
4499 LE.write<uint32_t>(Val: HashValue);
4500 }
4501
4502 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
4503 unsigned DataLen) {
4504 using namespace llvm::support;
4505
4506 endian::Writer LE(Out, llvm::endianness::little);
4507 uint64_t Start = Out.tell();
4508 (void)Start;
4509 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I) {
4510 LE.write<DeclID>(Val: Specs[I].getRawValue());
4511 }
4512 assert(Out.tell() - Start == DataLen && "Data length is wrong");
4513 }
4514};
4515
4516unsigned CalculateODRHashForSpecs(const Decl *Spec) {
4517 ArrayRef<TemplateArgument> Args;
4518 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: Spec))
4519 Args = CTSD->getTemplateArgs().asArray();
4520 else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: Spec))
4521 Args = VTSD->getTemplateArgs().asArray();
4522 else if (auto *FD = dyn_cast<FunctionDecl>(Val: Spec))
4523 Args = FD->getTemplateSpecializationArgs()->asArray();
4524 else
4525 llvm_unreachable("New Specialization Kind?");
4526
4527 return StableHashForTemplateArguments(Args);
4528}
4529} // namespace
4530
4531void ASTWriter::GenerateSpecializationInfoLookupTable(
4532 const NamedDecl *D, llvm::SmallVectorImpl<const Decl *> &Specializations,
4533 llvm::SmallVectorImpl<char> &LookupTable, bool IsPartial) {
4534 assert(D->isFirstDecl());
4535
4536 // Create the on-disk hash table representation.
4537 MultiOnDiskHashTableGenerator<reader::LazySpecializationInfoLookupTrait,
4538 LazySpecializationInfoLookupTrait>
4539 Generator;
4540 LazySpecializationInfoLookupTrait Trait(*this);
4541
4542 llvm::MapVector<unsigned, llvm::SmallVector<const NamedDecl *, 4>>
4543 SpecializationMaps;
4544
4545 for (auto *Specialization : Specializations) {
4546 unsigned HashedValue = CalculateODRHashForSpecs(Spec: Specialization);
4547
4548 auto Iter = SpecializationMaps.find(Key: HashedValue);
4549 if (Iter == SpecializationMaps.end())
4550 Iter = SpecializationMaps
4551 .try_emplace(Key: HashedValue,
4552 Args: llvm::SmallVector<const NamedDecl *, 4>())
4553 .first;
4554
4555 Iter->second.push_back(Elt: cast<NamedDecl>(Val: Specialization));
4556 }
4557
4558 auto *Lookups =
4559 Chain ? Chain->getLoadedSpecializationsLookupTables(D, IsPartial)
4560 : nullptr;
4561
4562 for (auto &[HashValue, Specs] : SpecializationMaps) {
4563 SmallVector<serialization::reader::LazySpecializationInfo, 16>
4564 ExisitingSpecs;
4565 // We have to merge the lookup table manually here. We can't depend on the
4566 // merge mechanism offered by
4567 // clang::serialization::MultiOnDiskHashTableGenerator since that generator
4568 // assumes the we'll get the same value with the same key.
4569 // And also underlying llvm::OnDiskChainedHashTableGenerator assumes that we
4570 // won't insert the values with the same key twice. So we have to merge the
4571 // lookup table here manually.
4572 if (Lookups)
4573 ExisitingSpecs = Lookups->Table.find(EKey: HashValue);
4574
4575 Generator.insert(Key: HashValue, Data: Trait.getData(C&: Specs, ExistingInfo&: ExisitingSpecs), Info&: Trait);
4576 }
4577
4578 Generator.emit(Out&: LookupTable, Info&: Trait, Base: Lookups ? &Lookups->Table : nullptr);
4579}
4580
4581uint64_t ASTWriter::WriteSpecializationInfoLookupTable(
4582 const NamedDecl *D, llvm::SmallVectorImpl<const Decl *> &Specializations,
4583 bool IsPartial) {
4584
4585 llvm::SmallString<4096> LookupTable;
4586 GenerateSpecializationInfoLookupTable(D, Specializations, LookupTable,
4587 IsPartial);
4588
4589 uint64_t Offset = Stream.GetCurrentBitNo();
4590 RecordData::value_type Record[] = {static_cast<RecordData::value_type>(
4591 IsPartial ? DECL_PARTIAL_SPECIALIZATIONS : DECL_SPECIALIZATIONS)};
4592 Stream.EmitRecordWithBlob(Abbrev: IsPartial ? DeclPartialSpecializationsAbbrev
4593 : DeclSpecializationsAbbrev,
4594 Vals: Record, Blob: LookupTable);
4595
4596 return Offset;
4597}
4598
4599/// Returns ture if all of the lookup result are either external, not emitted or
4600/// predefined. In such cases, the lookup result is not interesting and we don't
4601/// need to record the result in the current being written module. Return false
4602/// otherwise.
4603static bool isLookupResultNotInteresting(ASTWriter &Writer,
4604 StoredDeclsList &Result) {
4605 for (auto *D : Result.getLookupResult()) {
4606 auto *LocalD = getDeclForLocalLookup(LangOpts: Writer.getLangOpts(), D);
4607 if (LocalD->isFromASTFile())
4608 continue;
4609
4610 // We can only be sure whether the local declaration is reachable
4611 // after we done writing the declarations and types.
4612 if (Writer.getDoneWritingDeclsAndTypes() && !Writer.wasDeclEmitted(D: LocalD))
4613 continue;
4614
4615 // We don't need to emit the predefined decls.
4616 if (Writer.isDeclPredefined(D: LocalD))
4617 continue;
4618
4619 return false;
4620 }
4621
4622 return true;
4623}
4624
4625void ASTWriter::GenerateNameLookupTable(
4626 ASTContext &Context, const DeclContext *ConstDC,
4627 llvm::SmallVectorImpl<char> &LookupTable,
4628 llvm::SmallVectorImpl<char> &ModuleLocalLookupTable,
4629 llvm::SmallVectorImpl<char> &TULookupTable) {
4630 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
4631 !ConstDC->hasLazyExternalLexicalLookups() &&
4632 "must call buildLookups first");
4633
4634 // FIXME: We need to build the lookups table, which is logically const.
4635 auto *DC = const_cast<DeclContext*>(ConstDC);
4636 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
4637
4638 // Create the on-disk hash table representation.
4639 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
4640 ASTDeclContextNameLookupTrait>
4641 Generator;
4642 ASTDeclContextNameLookupTrait Trait(*this, *DC);
4643
4644 // The first step is to collect the declaration names which we need to
4645 // serialize into the name lookup table, and to collect them in a stable
4646 // order.
4647 SmallVector<DeclarationName, 16> Names;
4648
4649 // We also track whether we're writing out the DeclarationNameKey for
4650 // constructors or conversion functions.
4651 bool IncludeConstructorNames = false;
4652 bool IncludeConversionNames = false;
4653
4654 for (auto &[Name, Result] : *DC->buildLookup()) {
4655 // If there are no local declarations in our lookup result, we
4656 // don't need to write an entry for the name at all. If we can't
4657 // write out a lookup set without performing more deserialization,
4658 // just skip this entry.
4659 //
4660 // Also in reduced BMI, we'd like to avoid writing unreachable
4661 // declarations in GMF, so we need to avoid writing declarations
4662 // that entirely external or unreachable.
4663 if (GeneratingReducedBMI && isLookupResultNotInteresting(Writer&: *this, Result))
4664 continue;
4665 // We also skip empty results. If any of the results could be external and
4666 // the currently available results are empty, then all of the results are
4667 // external and we skip it above. So the only way we get here with an empty
4668 // results is when no results could have been external *and* we have
4669 // external results.
4670 //
4671 // FIXME: While we might want to start emitting on-disk entries for negative
4672 // lookups into a decl context as an optimization, today we *have* to skip
4673 // them because there are names with empty lookup results in decl contexts
4674 // which we can't emit in any stable ordering: we lookup constructors and
4675 // conversion functions in the enclosing namespace scope creating empty
4676 // results for them. This in almost certainly a bug in Clang's name lookup,
4677 // but that is likely to be hard or impossible to fix and so we tolerate it
4678 // here by omitting lookups with empty results.
4679 if (Result.getLookupResult().empty())
4680 continue;
4681
4682 switch (Name.getNameKind()) {
4683 default:
4684 Names.push_back(Elt: Name);
4685 break;
4686
4687 case DeclarationName::CXXConstructorName:
4688 IncludeConstructorNames = true;
4689 break;
4690
4691 case DeclarationName::CXXConversionFunctionName:
4692 IncludeConversionNames = true;
4693 break;
4694 }
4695 }
4696
4697 // Sort the names into a stable order.
4698 llvm::sort(C&: Names);
4699
4700 if (IncludeConstructorNames || IncludeConversionNames) {
4701 // We need to establish an ordering of constructor and conversion function
4702 // names, and they don't have an intrinsic ordering. We also need to write
4703 // out all constructor and conversion function results if we write out any
4704 // of them, because they're all tracked under the same lookup key.
4705 llvm::SmallPtrSet<DeclarationName, 8> AddedNames;
4706 for (Decl *ChildD : cast<CXXRecordDecl>(Val: DC)->decls()) {
4707 if (auto *ChildND = dyn_cast<NamedDecl>(Val: ChildD)) {
4708 auto Name = ChildND->getDeclName();
4709 switch (Name.getNameKind()) {
4710 default:
4711 continue;
4712
4713 case DeclarationName::CXXConstructorName:
4714 if (!IncludeConstructorNames)
4715 continue;
4716 break;
4717
4718 case DeclarationName::CXXConversionFunctionName:
4719 if (!IncludeConversionNames)
4720 continue;
4721 break;
4722 }
4723 if (AddedNames.insert(Ptr: Name).second)
4724 Names.push_back(Elt: Name);
4725 }
4726 }
4727 }
4728 // Next we need to do a lookup with each name into this decl context to fully
4729 // populate any results from external sources. We don't actually use the
4730 // results of these lookups because we only want to use the results after all
4731 // results have been loaded and the pointers into them will be stable.
4732 for (auto &Name : Names)
4733 DC->lookup(Name);
4734
4735 // Now we need to insert the results for each name into the hash table. For
4736 // constructor names and conversion function names, we actually need to merge
4737 // all of the results for them into one list of results each and insert
4738 // those.
4739 SmallVector<NamedDecl *, 8> ConstructorDecls;
4740 SmallVector<NamedDecl *, 8> ConversionDecls;
4741
4742 // Now loop over the names, either inserting them or appending for the two
4743 // special cases.
4744 for (auto &Name : Names) {
4745 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4746
4747 switch (Name.getNameKind()) {
4748 default:
4749 Generator.insert(Key: Name, Data: Trait.getData(Decls: Result), Info&: Trait);
4750 break;
4751
4752 case DeclarationName::CXXConstructorName:
4753 ConstructorDecls.append(in_start: Result.begin(), in_end: Result.end());
4754 break;
4755
4756 case DeclarationName::CXXConversionFunctionName:
4757 ConversionDecls.append(in_start: Result.begin(), in_end: Result.end());
4758 break;
4759 }
4760 }
4761
4762 // Handle our two special cases if we ended up having any. We arbitrarily use
4763 // the first declaration's name here because the name itself isn't part of
4764 // the key, only the kind of name is used.
4765 if (!ConstructorDecls.empty())
4766 Generator.insert(Key: ConstructorDecls.front()->getDeclName(),
4767 Data: Trait.getData(Decls: ConstructorDecls), Info&: Trait);
4768 if (!ConversionDecls.empty())
4769 Generator.insert(Key: ConversionDecls.front()->getDeclName(),
4770 Data: Trait.getData(Decls: ConversionDecls), Info&: Trait);
4771
4772 // Create the on-disk hash table. Also emit the existing imported and
4773 // merged table if there is one.
4774 auto *Lookups = Chain ? Chain->getLoadedLookupTables(Primary: DC) : nullptr;
4775 Generator.emit(Out&: LookupTable, Info&: Trait, Base: Lookups ? &Lookups->Table : nullptr);
4776
4777 const auto &ModuleLocalDecls = Trait.getModuleLocalDecls();
4778 if (!ModuleLocalDecls.empty()) {
4779 MultiOnDiskHashTableGenerator<reader::ModuleLocalNameLookupTrait,
4780 ModuleLevelNameLookupTrait>
4781 ModuleLocalLookupGenerator;
4782 ModuleLevelNameLookupTrait ModuleLocalTrait(*this);
4783
4784 for (const auto &ModuleLocalIter : ModuleLocalDecls) {
4785 const auto &Key = ModuleLocalIter.first;
4786 const auto &IDs = ModuleLocalIter.second;
4787 ModuleLocalLookupGenerator.insert(Key, Data: ModuleLocalTrait.getData(LocalIDs: IDs),
4788 Info&: ModuleLocalTrait);
4789 }
4790
4791 auto *ModuleLocalLookups =
4792 Chain ? Chain->getModuleLocalLookupTables(Primary: DC) : nullptr;
4793 ModuleLocalLookupGenerator.emit(
4794 Out&: ModuleLocalLookupTable, Info&: ModuleLocalTrait,
4795 Base: ModuleLocalLookups ? &ModuleLocalLookups->Table : nullptr);
4796 }
4797
4798 const auto &TULocalDecls = Trait.getTULocalDecls();
4799 if (!TULocalDecls.empty() && !isGeneratingReducedBMI()) {
4800 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
4801 ASTDeclContextNameTrivialLookupTrait>
4802 TULookupGenerator;
4803 ASTDeclContextNameTrivialLookupTrait TULocalTrait(*this);
4804
4805 for (const auto &TULocalIter : TULocalDecls) {
4806 const auto &Key = TULocalIter.first;
4807 const auto &IDs = TULocalIter.second;
4808 TULookupGenerator.insert(Key, Data: TULocalTrait.getData(LocalIDs: IDs), Info&: TULocalTrait);
4809 }
4810
4811 auto *TULocalLookups = Chain ? Chain->getTULocalLookupTables(Primary: DC) : nullptr;
4812 TULookupGenerator.emit(Out&: TULookupTable, Info&: TULocalTrait,
4813 Base: TULocalLookups ? &TULocalLookups->Table : nullptr);
4814 }
4815}
4816
4817/// Write the block containing all of the declaration IDs
4818/// visible from the given DeclContext.
4819///
4820/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4821/// bitstream, or 0 if no block was written.
4822void ASTWriter::WriteDeclContextVisibleBlock(
4823 ASTContext &Context, DeclContext *DC, VisibleLookupBlockOffsets &Offsets) {
4824 assert(!Offsets);
4825
4826 // If we imported a key declaration of this namespace, write the visible
4827 // lookup results as an update record for it rather than including them
4828 // on this declaration. We will only look at key declarations on reload.
4829 if (isa<NamespaceDecl>(Val: DC) && Chain &&
4830 Chain->getKeyDeclaration(D: cast<Decl>(Val: DC))->isFromASTFile()) {
4831 // Only do this once, for the first local declaration of the namespace.
4832 for (auto *Prev = cast<NamespaceDecl>(Val: DC)->getPreviousDecl(); Prev;
4833 Prev = Prev->getPreviousDecl())
4834 if (!Prev->isFromASTFile())
4835 return;
4836
4837 // Note that we need to emit an update record for the primary context.
4838 UpdatedDeclContexts.insert(X: DC->getPrimaryContext());
4839
4840 // Make sure all visible decls are written. They will be recorded later. We
4841 // do this using a side data structure so we can sort the names into
4842 // a deterministic order.
4843 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4844 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4845 LookupResults;
4846 if (Map) {
4847 LookupResults.reserve(N: Map->size());
4848 for (auto &Entry : *Map)
4849 LookupResults.push_back(
4850 Elt: std::make_pair(x&: Entry.first, y: Entry.second.getLookupResult()));
4851 }
4852
4853 llvm::sort(C&: LookupResults, Comp: llvm::less_first());
4854 for (auto &NameAndResult : LookupResults) {
4855 DeclarationName Name = NameAndResult.first;
4856 DeclContext::lookup_result Result = NameAndResult.second;
4857 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4858 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4859 // We have to work around a name lookup bug here where negative lookup
4860 // results for these names get cached in namespace lookup tables (these
4861 // names should never be looked up in a namespace).
4862 assert(Result.empty() && "Cannot have a constructor or conversion "
4863 "function name in a namespace!");
4864 continue;
4865 }
4866
4867 for (NamedDecl *ND : Result) {
4868 if (ND->isFromASTFile())
4869 continue;
4870
4871 if (DoneWritingDeclsAndTypes && !wasDeclEmitted(D: ND))
4872 continue;
4873
4874 // We don't need to force emitting internal decls into reduced BMI.
4875 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4876 if (GeneratingReducedBMI && !ND->isFromExplicitGlobalModule() &&
4877 IsInternalDeclFromFileContext(D: ND))
4878 continue;
4879
4880 GetDeclRef(D: ND);
4881 }
4882 }
4883
4884 return;
4885 }
4886
4887 if (DC->getPrimaryContext() != DC)
4888 return;
4889
4890 // Skip contexts which don't support name lookup.
4891 if (!DC->isLookupContext())
4892 return;
4893
4894 // If not in C++, we perform name lookup for the translation unit via the
4895 // IdentifierInfo chains, don't bother to build a visible-declarations table.
4896 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4897 return;
4898
4899 // Serialize the contents of the mapping used for lookup. Note that,
4900 // although we have two very different code paths, the serialized
4901 // representation is the same for both cases: a declaration name,
4902 // followed by a size, followed by references to the visible
4903 // declarations that have that name.
4904 StoredDeclsMap *Map = DC->buildLookup();
4905 if (!Map || Map->empty())
4906 return;
4907
4908 Offsets.VisibleOffset = Stream.GetCurrentBitNo();
4909 // Create the on-disk hash table in a buffer.
4910 SmallString<4096> LookupTable;
4911 SmallString<4096> ModuleLocalLookupTable;
4912 SmallString<4096> TULookupTable;
4913 GenerateNameLookupTable(Context, ConstDC: DC, LookupTable, ModuleLocalLookupTable,
4914 TULookupTable);
4915
4916 // Write the lookup table
4917 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4918 Stream.EmitRecordWithBlob(Abbrev: DeclContextVisibleLookupAbbrev, Vals: Record,
4919 Blob: LookupTable);
4920 ++NumVisibleDeclContexts;
4921
4922 if (!ModuleLocalLookupTable.empty()) {
4923 Offsets.ModuleLocalOffset = Stream.GetCurrentBitNo();
4924 assert(Offsets.ModuleLocalOffset > Offsets.VisibleOffset);
4925 // Write the lookup table
4926 RecordData::value_type ModuleLocalRecord[] = {
4927 DECL_CONTEXT_MODULE_LOCAL_VISIBLE};
4928 Stream.EmitRecordWithBlob(Abbrev: DeclModuleLocalVisibleLookupAbbrev,
4929 Vals: ModuleLocalRecord, Blob: ModuleLocalLookupTable);
4930 ++NumModuleLocalDeclContexts;
4931 }
4932
4933 if (!TULookupTable.empty()) {
4934 Offsets.TULocalOffset = Stream.GetCurrentBitNo();
4935 // Write the lookup table
4936 RecordData::value_type TULocalDeclsRecord[] = {
4937 DECL_CONTEXT_TU_LOCAL_VISIBLE};
4938 Stream.EmitRecordWithBlob(Abbrev: DeclTULocalLookupAbbrev, Vals: TULocalDeclsRecord,
4939 Blob: TULookupTable);
4940 ++NumTULocalDeclContexts;
4941 }
4942}
4943
4944/// Write an UPDATE_VISIBLE block for the given context.
4945///
4946/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4947/// DeclContext in a dependent AST file. As such, they only exist for the TU
4948/// (in C++), for namespaces, and for classes with forward-declared unscoped
4949/// enumeration members (in C++11).
4950void ASTWriter::WriteDeclContextVisibleUpdate(ASTContext &Context,
4951 const DeclContext *DC) {
4952 StoredDeclsMap *Map = DC->getLookupPtr();
4953 if (!Map || Map->empty())
4954 return;
4955
4956 // Create the on-disk hash table in a buffer.
4957 SmallString<4096> LookupTable;
4958 SmallString<4096> ModuleLocalLookupTable;
4959 SmallString<4096> TULookupTable;
4960 GenerateNameLookupTable(Context, ConstDC: DC, LookupTable, ModuleLocalLookupTable,
4961 TULookupTable);
4962
4963 // If we're updating a namespace, select a key declaration as the key for the
4964 // update record; those are the only ones that will be checked on reload.
4965 if (isa<NamespaceDecl>(Val: DC))
4966 DC = cast<DeclContext>(Val: Chain->getKeyDeclaration(D: cast<Decl>(Val: DC)));
4967
4968 // Write the lookup table
4969 RecordData::value_type Record[] = {UPDATE_VISIBLE,
4970 getDeclID(D: cast<Decl>(Val: DC)).getRawValue()};
4971 Stream.EmitRecordWithBlob(Abbrev: UpdateVisibleAbbrev, Vals: Record, Blob: LookupTable);
4972
4973 if (!ModuleLocalLookupTable.empty()) {
4974 // Write the module local lookup table
4975 RecordData::value_type ModuleLocalRecord[] = {
4976 UPDATE_MODULE_LOCAL_VISIBLE, getDeclID(D: cast<Decl>(Val: DC)).getRawValue()};
4977 Stream.EmitRecordWithBlob(Abbrev: ModuleLocalUpdateVisibleAbbrev, Vals: ModuleLocalRecord,
4978 Blob: ModuleLocalLookupTable);
4979 }
4980
4981 if (!TULookupTable.empty()) {
4982 RecordData::value_type GMFRecord[] = {
4983 UPDATE_TU_LOCAL_VISIBLE, getDeclID(D: cast<Decl>(Val: DC)).getRawValue()};
4984 Stream.EmitRecordWithBlob(Abbrev: TULocalUpdateVisibleAbbrev, Vals: GMFRecord,
4985 Blob: TULookupTable);
4986 }
4987}
4988
4989/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4990void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4991 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4992 Stream.EmitRecord(Code: FP_PRAGMA_OPTIONS, Vals: Record);
4993}
4994
4995/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4996void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4997 if (!SemaRef.Context.getLangOpts().OpenCL)
4998 return;
4999
5000 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
5001 RecordData Record;
5002 for (const auto &I:Opts.OptMap) {
5003 AddString(Str: I.getKey(), Record);
5004 auto V = I.getValue();
5005 Record.push_back(Elt: V.Supported ? 1 : 0);
5006 Record.push_back(Elt: V.Enabled ? 1 : 0);
5007 Record.push_back(Elt: V.WithPragma ? 1 : 0);
5008 Record.push_back(Elt: V.Avail);
5009 Record.push_back(Elt: V.Core);
5010 Record.push_back(Elt: V.Opt);
5011 }
5012 Stream.EmitRecord(Code: OPENCL_EXTENSIONS, Vals: Record);
5013}
5014void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
5015 if (SemaRef.CUDA().ForceHostDeviceDepth > 0) {
5016 RecordData::value_type Record[] = {SemaRef.CUDA().ForceHostDeviceDepth};
5017 Stream.EmitRecord(Code: CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Vals: Record);
5018 }
5019}
5020
5021void ASTWriter::WriteObjCCategories() {
5022 if (ObjCClassesWithCategories.empty())
5023 return;
5024
5025 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
5026 RecordData Categories;
5027
5028 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
5029 unsigned Size = 0;
5030 unsigned StartIndex = Categories.size();
5031
5032 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
5033
5034 // Allocate space for the size.
5035 Categories.push_back(Elt: 0);
5036
5037 // Add the categories.
5038 for (ObjCInterfaceDecl::known_categories_iterator
5039 Cat = Class->known_categories_begin(),
5040 CatEnd = Class->known_categories_end();
5041 Cat != CatEnd; ++Cat, ++Size) {
5042 assert(getDeclID(*Cat).isValid() && "Bogus category");
5043 AddDeclRef(D: *Cat, Record&: Categories);
5044 }
5045
5046 // Update the size.
5047 Categories[StartIndex] = Size;
5048
5049 // Record this interface -> category map.
5050 ObjCCategoriesInfo CatInfo = { getDeclID(D: Class), StartIndex };
5051 CategoriesMap.push_back(Elt: CatInfo);
5052 }
5053
5054 // Sort the categories map by the definition ID, since the reader will be
5055 // performing binary searches on this information.
5056 llvm::array_pod_sort(Start: CategoriesMap.begin(), End: CategoriesMap.end());
5057
5058 // Emit the categories map.
5059 using namespace llvm;
5060
5061 auto Abbrev = std::make_shared<BitCodeAbbrev>();
5062 Abbrev->Add(OpInfo: BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
5063 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
5064 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5065 unsigned AbbrevID = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
5066
5067 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
5068 Stream.EmitRecordWithBlob(Abbrev: AbbrevID, Vals: Record,
5069 BlobData: reinterpret_cast<char *>(CategoriesMap.data()),
5070 BlobLen: CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
5071
5072 // Emit the category lists.
5073 Stream.EmitRecord(Code: OBJC_CATEGORIES, Vals: Categories);
5074}
5075
5076void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
5077 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
5078
5079 if (LPTMap.empty())
5080 return;
5081
5082 RecordData Record;
5083 for (auto &LPTMapEntry : LPTMap) {
5084 const FunctionDecl *FD = LPTMapEntry.first;
5085 LateParsedTemplate &LPT = *LPTMapEntry.second;
5086 AddDeclRef(D: FD, Record);
5087 AddDeclRef(D: LPT.D, Record);
5088 Record.push_back(Elt: LPT.FPO.getAsOpaqueInt());
5089 Record.push_back(Elt: LPT.Toks.size());
5090
5091 for (const auto &Tok : LPT.Toks) {
5092 AddToken(Tok, Record);
5093 }
5094 }
5095 Stream.EmitRecord(Code: LATE_PARSED_TEMPLATE, Vals: Record);
5096}
5097
5098/// Write the state of 'pragma clang optimize' at the end of the module.
5099void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
5100 RecordData Record;
5101 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
5102 AddSourceLocation(Loc: PragmaLoc, Record);
5103 Stream.EmitRecord(Code: OPTIMIZE_PRAGMA_OPTIONS, Vals: Record);
5104}
5105
5106/// Write the state of 'pragma ms_struct' at the end of the module.
5107void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
5108 RecordData Record;
5109 Record.push_back(Elt: SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
5110 Stream.EmitRecord(Code: MSSTRUCT_PRAGMA_OPTIONS, Vals: Record);
5111}
5112
5113/// Write the state of 'pragma pointers_to_members' at the end of the
5114//module.
5115void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
5116 RecordData Record;
5117 Record.push_back(Elt: SemaRef.MSPointerToMemberRepresentationMethod);
5118 AddSourceLocation(Loc: SemaRef.ImplicitMSInheritanceAttrLoc, Record);
5119 Stream.EmitRecord(Code: POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Vals: Record);
5120}
5121
5122/// Write the state of 'pragma align/pack' at the end of the module.
5123void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
5124 // Don't serialize pragma align/pack state for modules, since it should only
5125 // take effect on a per-submodule basis.
5126 if (WritingModule)
5127 return;
5128
5129 RecordData Record;
5130 AddAlignPackInfo(Info: SemaRef.AlignPackStack.CurrentValue, Record);
5131 AddSourceLocation(Loc: SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
5132 Record.push_back(Elt: SemaRef.AlignPackStack.Stack.size());
5133 for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
5134 AddAlignPackInfo(Info: StackEntry.Value, Record);
5135 AddSourceLocation(Loc: StackEntry.PragmaLocation, Record);
5136 AddSourceLocation(Loc: StackEntry.PragmaPushLocation, Record);
5137 AddString(Str: StackEntry.StackSlotLabel, Record);
5138 }
5139 Stream.EmitRecord(Code: ALIGN_PACK_PRAGMA_OPTIONS, Vals: Record);
5140}
5141
5142/// Write the state of 'pragma float_control' at the end of the module.
5143void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
5144 // Don't serialize pragma float_control state for modules,
5145 // since it should only take effect on a per-submodule basis.
5146 if (WritingModule)
5147 return;
5148
5149 RecordData Record;
5150 Record.push_back(Elt: SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
5151 AddSourceLocation(Loc: SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
5152 Record.push_back(Elt: SemaRef.FpPragmaStack.Stack.size());
5153 for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
5154 Record.push_back(Elt: StackEntry.Value.getAsOpaqueInt());
5155 AddSourceLocation(Loc: StackEntry.PragmaLocation, Record);
5156 AddSourceLocation(Loc: StackEntry.PragmaPushLocation, Record);
5157 AddString(Str: StackEntry.StackSlotLabel, Record);
5158 }
5159 Stream.EmitRecord(Code: FLOAT_CONTROL_PRAGMA_OPTIONS, Vals: Record);
5160}
5161
5162/// Write Sema's collected list of declarations with unverified effects.
5163void ASTWriter::WriteDeclsWithEffectsToVerify(Sema &SemaRef) {
5164 if (SemaRef.DeclsWithEffectsToVerify.empty())
5165 return;
5166 RecordData Record;
5167 for (const auto *D : SemaRef.DeclsWithEffectsToVerify) {
5168 AddDeclRef(D, Record);
5169 }
5170 Stream.EmitRecord(Code: DECLS_WITH_EFFECTS_TO_VERIFY, Vals: Record);
5171}
5172
5173void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
5174 ModuleFileExtensionWriter &Writer) {
5175 // Enter the extension block.
5176 Stream.EnterSubblock(BlockID: EXTENSION_BLOCK_ID, CodeLen: 4);
5177
5178 // Emit the metadata record abbreviation.
5179 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
5180 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
5181 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
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::Blob));
5186 unsigned Abbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
5187
5188 // Emit the metadata record.
5189 RecordData Record;
5190 auto Metadata = Writer.getExtension()->getExtensionMetadata();
5191 Record.push_back(Elt: EXTENSION_METADATA);
5192 Record.push_back(Elt: Metadata.MajorVersion);
5193 Record.push_back(Elt: Metadata.MinorVersion);
5194 Record.push_back(Elt: Metadata.BlockName.size());
5195 Record.push_back(Elt: Metadata.UserInfo.size());
5196 SmallString<64> Buffer;
5197 Buffer += Metadata.BlockName;
5198 Buffer += Metadata.UserInfo;
5199 Stream.EmitRecordWithBlob(Abbrev, Vals: Record, Blob: Buffer);
5200
5201 // Emit the contents of the extension block.
5202 Writer.writeExtensionContents(SemaRef, Stream);
5203
5204 // Exit the extension block.
5205 Stream.ExitBlock();
5206}
5207
5208//===----------------------------------------------------------------------===//
5209// General Serialization Routines
5210//===----------------------------------------------------------------------===//
5211
5212void ASTRecordWriter::AddAttr(const Attr *A) {
5213 auto &Record = *this;
5214 // FIXME: Clang can't handle the serialization/deserialization of
5215 // preferred_name properly now. See
5216 // https://github.com/llvm/llvm-project/issues/56490 for example.
5217 if (!A ||
5218 (isa<PreferredNameAttr>(Val: A) && (Writer->isWritingStdCXXNamedModules() ||
5219 Writer->isWritingStdCXXHeaderUnit())))
5220 return Record.push_back(N: 0);
5221
5222 Record.push_back(N: A->getKind() + 1); // FIXME: stable encoding, target attrs
5223
5224 Record.AddIdentifierRef(II: A->getAttrName());
5225 Record.AddIdentifierRef(II: A->getScopeName());
5226 Record.AddSourceRange(Range: A->getRange());
5227 Record.AddSourceLocation(Loc: A->getScopeLoc());
5228 Record.push_back(N: A->getParsedKind());
5229 Record.push_back(N: A->getSyntax());
5230 Record.push_back(N: A->getAttributeSpellingListIndexRaw());
5231 Record.push_back(N: A->isRegularKeywordAttribute());
5232
5233#include "clang/Serialization/AttrPCHWrite.inc"
5234}
5235
5236/// Emit the list of attributes to the specified record.
5237void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
5238 push_back(N: Attrs.size());
5239 for (const auto *A : Attrs)
5240 AddAttr(A);
5241}
5242
5243void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
5244 AddSourceLocation(Loc: Tok.getLocation(), Record);
5245 // FIXME: Should translate token kind to a stable encoding.
5246 Record.push_back(Elt: Tok.getKind());
5247 // FIXME: Should translate token flags to a stable encoding.
5248 Record.push_back(Elt: Tok.getFlags());
5249
5250 if (Tok.isAnnotation()) {
5251 AddSourceLocation(Loc: Tok.getAnnotationEndLoc(), Record);
5252 switch (Tok.getKind()) {
5253 case tok::annot_pragma_loop_hint: {
5254 auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
5255 AddToken(Tok: Info->PragmaName, Record);
5256 AddToken(Tok: Info->Option, Record);
5257 Record.push_back(Elt: Info->Toks.size());
5258 for (const auto &T : Info->Toks)
5259 AddToken(Tok: T, Record);
5260 break;
5261 }
5262 case tok::annot_pragma_pack: {
5263 auto *Info =
5264 static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
5265 Record.push_back(Elt: static_cast<unsigned>(Info->Action));
5266 AddString(Str: Info->SlotLabel, Record);
5267 AddToken(Tok: Info->Alignment, Record);
5268 break;
5269 }
5270 // Some annotation tokens do not use the PtrData field.
5271 case tok::annot_pragma_openmp:
5272 case tok::annot_pragma_openmp_end:
5273 case tok::annot_pragma_unused:
5274 case tok::annot_pragma_openacc:
5275 case tok::annot_pragma_openacc_end:
5276 case tok::annot_repl_input_end:
5277 break;
5278 default:
5279 llvm_unreachable("missing serialization code for annotation token");
5280 }
5281 } else {
5282 Record.push_back(Elt: Tok.getLength());
5283 // FIXME: When reading literal tokens, reconstruct the literal pointer if it
5284 // is needed.
5285 AddIdentifierRef(II: Tok.getIdentifierInfo(), Record);
5286 }
5287}
5288
5289void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
5290 Record.push_back(Elt: Str.size());
5291 llvm::append_range(C&: Record, R&: Str);
5292}
5293
5294void ASTWriter::AddStringBlob(StringRef Str, RecordDataImpl &Record,
5295 SmallVectorImpl<char> &Blob) {
5296 Record.push_back(Elt: Str.size());
5297 llvm::append_range(C&: Blob, R&: Str);
5298}
5299
5300bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
5301 assert(WritingAST && "can't prepare path for output when not writing AST");
5302
5303 // Leave special file names as they are.
5304 StringRef PathStr(Path.data(), Path.size());
5305 if (PathStr == "<built-in>" || PathStr == "<command line>")
5306 return false;
5307
5308 bool Changed = cleanPathForOutput(FileMgr&: PP->getFileManager(), Path);
5309
5310 // Remove a prefix to make the path relative, if relevant.
5311 const char *PathBegin = Path.data();
5312 const char *PathPtr =
5313 adjustFilenameForRelocatableAST(Filename: PathBegin, BaseDir: BaseDirectory);
5314 if (PathPtr != PathBegin) {
5315 Path.erase(CS: Path.begin(), CE: Path.begin() + (PathPtr - PathBegin));
5316 Changed = true;
5317 }
5318
5319 return Changed;
5320}
5321
5322void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
5323 SmallString<128> FilePath(Path);
5324 PreparePathForOutput(Path&: FilePath);
5325 AddString(Str: FilePath, Record);
5326}
5327
5328void ASTWriter::AddPathBlob(StringRef Path, RecordDataImpl &Record,
5329 SmallVectorImpl<char> &Blob) {
5330 SmallString<128> FilePath(Path);
5331 PreparePathForOutput(Path&: FilePath);
5332 AddStringBlob(Str: FilePath, Record, Blob);
5333}
5334
5335void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
5336 StringRef Path) {
5337 SmallString<128> FilePath(Path);
5338 PreparePathForOutput(Path&: FilePath);
5339 Stream.EmitRecordWithBlob(Abbrev, Vals: Record, Blob: FilePath);
5340}
5341
5342void ASTWriter::AddVersionTuple(const VersionTuple &Version,
5343 RecordDataImpl &Record) {
5344 Record.push_back(Elt: Version.getMajor());
5345 if (std::optional<unsigned> Minor = Version.getMinor())
5346 Record.push_back(Elt: *Minor + 1);
5347 else
5348 Record.push_back(Elt: 0);
5349 if (std::optional<unsigned> Subminor = Version.getSubminor())
5350 Record.push_back(Elt: *Subminor + 1);
5351 else
5352 Record.push_back(Elt: 0);
5353}
5354
5355/// Note that the identifier II occurs at the given offset
5356/// within the identifier table.
5357void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
5358 IdentifierID ID = IdentifierIDs[II];
5359 // Only store offsets new to this AST file. Other identifier names are looked
5360 // up earlier in the chain and thus don't need an offset.
5361 if (!isLocalIdentifierID(ID))
5362 return;
5363
5364 // For local identifiers, the module file index must be 0.
5365
5366 assert(ID != 0);
5367 ID -= NUM_PREDEF_IDENT_IDS;
5368 assert(ID < IdentifierOffsets.size());
5369 IdentifierOffsets[ID] = Offset;
5370}
5371
5372/// Note that the selector Sel occurs at the given offset
5373/// within the method pool/selector table.
5374void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
5375 unsigned ID = SelectorIDs[Sel];
5376 assert(ID && "Unknown selector");
5377 // Don't record offsets for selectors that are also available in a different
5378 // file.
5379 if (ID < FirstSelectorID)
5380 return;
5381 SelectorOffsets[ID - FirstSelectorID] = Offset;
5382}
5383
5384ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
5385 SmallVectorImpl<char> &Buffer, ModuleCache &ModCache,
5386 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
5387 bool IncludeTimestamps, bool BuildingImplicitModule,
5388 bool GeneratingReducedBMI)
5389 : Stream(Stream), Buffer(Buffer), ModCache(ModCache),
5390 IncludeTimestamps(IncludeTimestamps),
5391 BuildingImplicitModule(BuildingImplicitModule),
5392 GeneratingReducedBMI(GeneratingReducedBMI) {
5393 for (const auto &Ext : Extensions) {
5394 if (auto Writer = Ext->createExtensionWriter(Writer&: *this))
5395 ModuleFileExtensionWriters.push_back(x: std::move(Writer));
5396 }
5397}
5398
5399ASTWriter::~ASTWriter() = default;
5400
5401const LangOptions &ASTWriter::getLangOpts() const {
5402 assert(WritingAST && "can't determine lang opts when not writing AST");
5403 return PP->getLangOpts();
5404}
5405
5406time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
5407 return IncludeTimestamps ? E->getModificationTime() : 0;
5408}
5409
5410ASTFileSignature
5411ASTWriter::WriteAST(llvm::PointerUnion<Sema *, Preprocessor *> Subject,
5412 StringRef OutputFile, Module *WritingModule,
5413 StringRef isysroot, bool ShouldCacheASTInMemory) {
5414 llvm::TimeTraceScope scope("WriteAST", OutputFile);
5415 WritingAST = true;
5416
5417 Sema *SemaPtr = dyn_cast<Sema *>(Val&: Subject);
5418 Preprocessor &PPRef =
5419 SemaPtr ? SemaPtr->getPreprocessor() : *cast<Preprocessor *>(Val&: Subject);
5420
5421 ASTHasCompilerErrors = PPRef.getDiagnostics().hasUncompilableErrorOccurred();
5422
5423 // Emit the file header.
5424 Stream.Emit(Val: (unsigned)'C', NumBits: 8);
5425 Stream.Emit(Val: (unsigned)'P', NumBits: 8);
5426 Stream.Emit(Val: (unsigned)'C', NumBits: 8);
5427 Stream.Emit(Val: (unsigned)'H', NumBits: 8);
5428
5429 WriteBlockInfoBlock();
5430
5431 PP = &PPRef;
5432 this->WritingModule = WritingModule;
5433 ASTFileSignature Signature = WriteASTCore(SemaPtr, isysroot, WritingModule);
5434 PP = nullptr;
5435 this->WritingModule = nullptr;
5436 this->BaseDirectory.clear();
5437
5438 WritingAST = false;
5439
5440 if (WritingModule && PPRef.getHeaderSearchInfo()
5441 .getHeaderSearchOpts()
5442 .ModulesValidateOncePerBuildSession)
5443 ModCache.updateModuleTimestamp(ModuleFilename: OutputFile);
5444
5445 if (ShouldCacheASTInMemory) {
5446 // Construct MemoryBuffer and update buffer manager.
5447 ModCache.getInMemoryModuleCache().addBuiltPCM(
5448 Filename: OutputFile, Buffer: llvm::MemoryBuffer::getMemBufferCopy(
5449 InputData: StringRef(Buffer.begin(), Buffer.size())));
5450 }
5451 return Signature;
5452}
5453
5454template<typename Vector>
5455static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec) {
5456 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
5457 I != E; ++I) {
5458 Writer.GetDeclRef(D: *I);
5459 }
5460}
5461
5462template <typename Vector>
5463static void AddLazyVectorEmiitedDecls(ASTWriter &Writer, Vector &Vec,
5464 ASTWriter::RecordData &Record) {
5465 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
5466 I != E; ++I) {
5467 Writer.AddEmittedDeclRef(D: *I, Record);
5468 }
5469}
5470
5471void ASTWriter::computeNonAffectingInputFiles() {
5472 SourceManager &SrcMgr = PP->getSourceManager();
5473 unsigned N = SrcMgr.local_sloc_entry_size();
5474
5475 IsSLocAffecting.resize(N, t: true);
5476 IsSLocFileEntryAffecting.resize(N, t: true);
5477
5478 if (!WritingModule)
5479 return;
5480
5481 auto AffectingModuleMaps = GetAffectingModuleMaps(PP: *PP, RootModule: WritingModule);
5482
5483 unsigned FileIDAdjustment = 0;
5484 unsigned OffsetAdjustment = 0;
5485
5486 NonAffectingFileIDAdjustments.reserve(n: N);
5487 NonAffectingOffsetAdjustments.reserve(n: N);
5488
5489 NonAffectingFileIDAdjustments.push_back(x: FileIDAdjustment);
5490 NonAffectingOffsetAdjustments.push_back(x: OffsetAdjustment);
5491
5492 for (unsigned I = 1; I != N; ++I) {
5493 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(Index: I);
5494 FileID FID = FileID::get(V: I);
5495 assert(&SrcMgr.getSLocEntry(FID) == SLoc);
5496
5497 if (!SLoc->isFile())
5498 continue;
5499 const SrcMgr::FileInfo &File = SLoc->getFile();
5500 const SrcMgr::ContentCache *Cache = &File.getContentCache();
5501 if (!Cache->OrigEntry)
5502 continue;
5503
5504 // Don't prune anything other than module maps.
5505 if (!isModuleMap(CK: File.getFileCharacteristic()))
5506 continue;
5507
5508 // Don't prune module maps if all are guaranteed to be affecting.
5509 if (!AffectingModuleMaps)
5510 continue;
5511
5512 // Don't prune module maps that are affecting.
5513 if (AffectingModuleMaps->DefinitionFileIDs.contains(V: FID))
5514 continue;
5515
5516 IsSLocAffecting[I] = false;
5517 IsSLocFileEntryAffecting[I] =
5518 AffectingModuleMaps->DefinitionFiles.contains(V: *Cache->OrigEntry);
5519
5520 FileIDAdjustment += 1;
5521 // Even empty files take up one element in the offset table.
5522 OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
5523
5524 // If the previous file was non-affecting as well, just extend its entry
5525 // with our information.
5526 if (!NonAffectingFileIDs.empty() &&
5527 NonAffectingFileIDs.back().ID == FID.ID - 1) {
5528 NonAffectingFileIDs.back() = FID;
5529 NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
5530 NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
5531 NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
5532 continue;
5533 }
5534
5535 NonAffectingFileIDs.push_back(x: FID);
5536 NonAffectingRanges.emplace_back(args: SrcMgr.getLocForStartOfFile(FID),
5537 args: SrcMgr.getLocForEndOfFile(FID));
5538 NonAffectingFileIDAdjustments.push_back(x: FileIDAdjustment);
5539 NonAffectingOffsetAdjustments.push_back(x: OffsetAdjustment);
5540 }
5541
5542 if (!PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesIncludeVFSUsage)
5543 return;
5544
5545 FileManager &FileMgr = PP->getFileManager();
5546 FileMgr.trackVFSUsage(Active: true);
5547 // Lookup the paths in the VFS to trigger `-ivfsoverlay` usage tracking.
5548 for (StringRef Path :
5549 PP->getHeaderSearchInfo().getHeaderSearchOpts().VFSOverlayFiles)
5550 FileMgr.getVirtualFileSystem().exists(Path);
5551 for (unsigned I = 1; I != N; ++I) {
5552 if (IsSLocAffecting[I]) {
5553 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(Index: I);
5554 if (!SLoc->isFile())
5555 continue;
5556 const SrcMgr::FileInfo &File = SLoc->getFile();
5557 const SrcMgr::ContentCache *Cache = &File.getContentCache();
5558 if (!Cache->OrigEntry)
5559 continue;
5560 FileMgr.getVirtualFileSystem().exists(
5561 Path: Cache->OrigEntry->getNameAsRequested());
5562 }
5563 }
5564 FileMgr.trackVFSUsage(Active: false);
5565}
5566
5567void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
5568 ASTContext &Context = SemaRef.Context;
5569
5570 bool isModule = WritingModule != nullptr;
5571
5572 // Set up predefined declaration IDs.
5573 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
5574 if (D) {
5575 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
5576 DeclIDs[D] = ID;
5577 PredefinedDecls.insert(Ptr: D);
5578 }
5579 };
5580 RegisterPredefDecl(Context.getTranslationUnitDecl(),
5581 PREDEF_DECL_TRANSLATION_UNIT_ID);
5582 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
5583 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
5584 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
5585 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
5586 PREDEF_DECL_OBJC_PROTOCOL_ID);
5587 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
5588 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
5589 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
5590 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
5591 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
5592 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
5593 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
5594 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
5595 RegisterPredefDecl(Context.MSGuidTagDecl,
5596 PREDEF_DECL_BUILTIN_MS_GUID_ID);
5597 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
5598 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
5599 PREDEF_DECL_CF_CONSTANT_STRING_ID);
5600 RegisterPredefDecl(Context.CFConstantStringTagDecl,
5601 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
5602#define BuiltinTemplate(BTName) \
5603 RegisterPredefDecl(Context.Decl##BTName, PREDEF_DECL##BTName##_ID);
5604#include "clang/Basic/BuiltinTemplates.inc"
5605
5606 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5607
5608 // Force all top level declarations to be emitted.
5609 //
5610 // We start emitting top level declarations from the module purview to
5611 // implement the eliding unreachable declaration feature.
5612 for (const auto *D : TU->noload_decls()) {
5613 if (D->isFromASTFile())
5614 continue;
5615
5616 if (GeneratingReducedBMI) {
5617 if (D->isFromExplicitGlobalModule())
5618 continue;
5619
5620 // Don't force emitting static entities.
5621 //
5622 // Technically, all static entities shouldn't be in reduced BMI. The
5623 // language also specifies that the program exposes TU-local entities
5624 // is ill-formed. However, in practice, there are a lot of projects
5625 // uses `static inline` in the headers. So we can't get rid of all
5626 // static entities in reduced BMI now.
5627 if (IsInternalDeclFromFileContext(D))
5628 continue;
5629 }
5630
5631 // If we're writing C++ named modules, don't emit declarations which are
5632 // not from modules by default. They may be built in declarations (be
5633 // handled above) or implcit declarations (see the implementation of
5634 // `Sema::Initialize()` for example).
5635 if (isWritingStdCXXNamedModules() && !D->getOwningModule() &&
5636 D->isImplicit())
5637 continue;
5638
5639 GetDeclRef(D);
5640 }
5641
5642 if (GeneratingReducedBMI)
5643 return;
5644
5645 // Writing all of the tentative definitions in this file, in
5646 // TentativeDefinitions order. Generally, this record will be empty for
5647 // headers.
5648 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.TentativeDefinitions);
5649
5650 // Writing all of the file scoped decls in this file.
5651 if (!isModule)
5652 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.UnusedFileScopedDecls);
5653
5654 // Writing all of the delegating constructors we still need
5655 // to resolve.
5656 if (!isModule)
5657 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.DelegatingCtorDecls);
5658
5659 // Writing all of the ext_vector declarations.
5660 AddLazyVectorDecls(Writer&: *this, Vec&: SemaRef.ExtVectorDecls);
5661
5662 // Writing all of the VTable uses information.
5663 if (!SemaRef.VTableUses.empty())
5664 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I)
5665 GetDeclRef(D: SemaRef.VTableUses[I].first);
5666
5667 // Writing all of the UnusedLocalTypedefNameCandidates.
5668 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5669 GetDeclRef(D: TD);
5670
5671 // Writing all of pending implicit instantiations.
5672 for (const auto &I : SemaRef.PendingInstantiations)
5673 GetDeclRef(D: I.first);
5674 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
5675 "There are local ones at end of translation unit!");
5676
5677 // Writing some declaration references.
5678 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5679 GetDeclRef(D: SemaRef.getStdNamespace());
5680 GetDeclRef(D: SemaRef.getStdBadAlloc());
5681 GetDeclRef(D: SemaRef.getStdAlignValT());
5682 }
5683
5684 if (Context.getcudaConfigureCallDecl())
5685 GetDeclRef(D: Context.getcudaConfigureCallDecl());
5686
5687 // Writing all of the known namespaces.
5688 for (const auto &I : SemaRef.KnownNamespaces)
5689 if (!I.second)
5690 GetDeclRef(D: I.first);
5691
5692 // Writing all used, undefined objects that require definitions.
5693 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5694 SemaRef.getUndefinedButUsed(Undefined);
5695 for (const auto &I : Undefined)
5696 GetDeclRef(D: I.first);
5697
5698 // Writing all delete-expressions that we would like to
5699 // analyze later in AST.
5700 if (!isModule)
5701 for (const auto &DeleteExprsInfo :
5702 SemaRef.getMismatchingDeleteExpressions())
5703 GetDeclRef(D: DeleteExprsInfo.first);
5704
5705 // Make sure visible decls, added to DeclContexts previously loaded from
5706 // an AST file, are registered for serialization. Likewise for template
5707 // specializations added to imported templates.
5708 for (const auto *I : DeclsToEmitEvenIfUnreferenced)
5709 GetDeclRef(D: I);
5710 DeclsToEmitEvenIfUnreferenced.clear();
5711
5712 // Make sure all decls associated with an identifier are registered for
5713 // serialization, if we're storing decls with identifiers.
5714 if (!WritingModule || !getLangOpts().CPlusPlus) {
5715 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
5716 for (const auto &ID : SemaRef.PP.getIdentifierTable()) {
5717 const IdentifierInfo *II = ID.second;
5718 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization() ||
5719 II->hasFETokenInfoChangedSinceDeserialization())
5720 IIs.push_back(Elt: II);
5721 }
5722 // Sort the identifiers to visit based on their name.
5723 llvm::sort(C&: IIs, Comp: llvm::deref<std::less<>>());
5724 const LangOptions &LangOpts = getLangOpts();
5725 for (const IdentifierInfo *II : IIs)
5726 for (NamedDecl *D : SemaRef.IdResolver.decls(Name: II))
5727 GetDeclRef(D: getDeclForLocalLookup(LangOpts, D));
5728 }
5729
5730 // Write all of the DeclsToCheckForDeferredDiags.
5731 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5732 GetDeclRef(D);
5733
5734 // Write all classes that need to emit the vtable definitions if required.
5735 if (isWritingStdCXXNamedModules())
5736 for (CXXRecordDecl *RD : PendingEmittingVTables)
5737 GetDeclRef(D: RD);
5738 else
5739 PendingEmittingVTables.clear();
5740}
5741
5742void ASTWriter::WriteSpecialDeclRecords(Sema &SemaRef) {
5743 ASTContext &Context = SemaRef.Context;
5744
5745 bool isModule = WritingModule != nullptr;
5746
5747 // Write the record containing external, unnamed definitions.
5748 if (!EagerlyDeserializedDecls.empty())
5749 Stream.EmitRecord(Code: EAGERLY_DESERIALIZED_DECLS, Vals: EagerlyDeserializedDecls);
5750
5751 if (!ModularCodegenDecls.empty())
5752 Stream.EmitRecord(Code: MODULAR_CODEGEN_DECLS, Vals: ModularCodegenDecls);
5753
5754 // Write the record containing tentative definitions.
5755 RecordData TentativeDefinitions;
5756 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.TentativeDefinitions,
5757 Record&: TentativeDefinitions);
5758 if (!TentativeDefinitions.empty())
5759 Stream.EmitRecord(Code: TENTATIVE_DEFINITIONS, Vals: TentativeDefinitions);
5760
5761 // Write the record containing unused file scoped decls.
5762 RecordData UnusedFileScopedDecls;
5763 if (!isModule)
5764 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.UnusedFileScopedDecls,
5765 Record&: UnusedFileScopedDecls);
5766 if (!UnusedFileScopedDecls.empty())
5767 Stream.EmitRecord(Code: UNUSED_FILESCOPED_DECLS, Vals: UnusedFileScopedDecls);
5768
5769 // Write the record containing ext_vector type names.
5770 RecordData ExtVectorDecls;
5771 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.ExtVectorDecls, Record&: ExtVectorDecls);
5772 if (!ExtVectorDecls.empty())
5773 Stream.EmitRecord(Code: EXT_VECTOR_DECLS, Vals: ExtVectorDecls);
5774
5775 // Write the record containing VTable uses information.
5776 RecordData VTableUses;
5777 if (!SemaRef.VTableUses.empty()) {
5778 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
5779 CXXRecordDecl *D = SemaRef.VTableUses[I].first;
5780 if (!wasDeclEmitted(D))
5781 continue;
5782
5783 AddDeclRef(D, Record&: VTableUses);
5784 AddSourceLocation(Loc: SemaRef.VTableUses[I].second, Record&: VTableUses);
5785 VTableUses.push_back(Elt: SemaRef.VTablesUsed[D]);
5786 }
5787 Stream.EmitRecord(Code: VTABLE_USES, Vals: VTableUses);
5788 }
5789
5790 // Write the record containing potentially unused local typedefs.
5791 RecordData UnusedLocalTypedefNameCandidates;
5792 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5793 AddEmittedDeclRef(D: TD, Record&: UnusedLocalTypedefNameCandidates);
5794 if (!UnusedLocalTypedefNameCandidates.empty())
5795 Stream.EmitRecord(Code: UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5796 Vals: UnusedLocalTypedefNameCandidates);
5797
5798 // Write the record containing pending implicit instantiations.
5799 RecordData PendingInstantiations;
5800 for (const auto &I : SemaRef.PendingInstantiations) {
5801 if (!wasDeclEmitted(D: I.first))
5802 continue;
5803
5804 AddDeclRef(D: I.first, Record&: PendingInstantiations);
5805 AddSourceLocation(Loc: I.second, Record&: PendingInstantiations);
5806 }
5807 if (!PendingInstantiations.empty())
5808 Stream.EmitRecord(Code: PENDING_IMPLICIT_INSTANTIATIONS, Vals: PendingInstantiations);
5809
5810 // Write the record containing declaration references of Sema.
5811 RecordData SemaDeclRefs;
5812 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5813 auto AddEmittedDeclRefOrZero = [this, &SemaDeclRefs](Decl *D) {
5814 if (!D || !wasDeclEmitted(D))
5815 SemaDeclRefs.push_back(Elt: 0);
5816 else
5817 AddDeclRef(D, Record&: SemaDeclRefs);
5818 };
5819
5820 AddEmittedDeclRefOrZero(SemaRef.getStdNamespace());
5821 AddEmittedDeclRefOrZero(SemaRef.getStdBadAlloc());
5822 AddEmittedDeclRefOrZero(SemaRef.getStdAlignValT());
5823 }
5824 if (!SemaDeclRefs.empty())
5825 Stream.EmitRecord(Code: SEMA_DECL_REFS, Vals: SemaDeclRefs);
5826
5827 // Write the record containing decls to be checked for deferred diags.
5828 RecordData DeclsToCheckForDeferredDiags;
5829 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5830 if (wasDeclEmitted(D))
5831 AddDeclRef(D, Record&: DeclsToCheckForDeferredDiags);
5832 if (!DeclsToCheckForDeferredDiags.empty())
5833 Stream.EmitRecord(Code: DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5834 Vals: DeclsToCheckForDeferredDiags);
5835
5836 // Write the record containing CUDA-specific declaration references.
5837 RecordData CUDASpecialDeclRefs;
5838 if (auto *CudaCallDecl = Context.getcudaConfigureCallDecl();
5839 CudaCallDecl && wasDeclEmitted(D: CudaCallDecl)) {
5840 AddDeclRef(D: CudaCallDecl, Record&: CUDASpecialDeclRefs);
5841 Stream.EmitRecord(Code: CUDA_SPECIAL_DECL_REFS, Vals: CUDASpecialDeclRefs);
5842 }
5843
5844 // Write the delegating constructors.
5845 RecordData DelegatingCtorDecls;
5846 if (!isModule)
5847 AddLazyVectorEmiitedDecls(Writer&: *this, Vec&: SemaRef.DelegatingCtorDecls,
5848 Record&: DelegatingCtorDecls);
5849 if (!DelegatingCtorDecls.empty())
5850 Stream.EmitRecord(Code: DELEGATING_CTORS, Vals: DelegatingCtorDecls);
5851
5852 // Write the known namespaces.
5853 RecordData KnownNamespaces;
5854 for (const auto &I : SemaRef.KnownNamespaces) {
5855 if (!I.second && wasDeclEmitted(D: I.first))
5856 AddDeclRef(D: I.first, Record&: KnownNamespaces);
5857 }
5858 if (!KnownNamespaces.empty())
5859 Stream.EmitRecord(Code: KNOWN_NAMESPACES, Vals: KnownNamespaces);
5860
5861 // Write the undefined internal functions and variables, and inline functions.
5862 RecordData UndefinedButUsed;
5863 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5864 SemaRef.getUndefinedButUsed(Undefined);
5865 for (const auto &I : Undefined) {
5866 if (!wasDeclEmitted(D: I.first))
5867 continue;
5868
5869 AddDeclRef(D: I.first, Record&: UndefinedButUsed);
5870 AddSourceLocation(Loc: I.second, Record&: UndefinedButUsed);
5871 }
5872 if (!UndefinedButUsed.empty())
5873 Stream.EmitRecord(Code: UNDEFINED_BUT_USED, Vals: UndefinedButUsed);
5874
5875 // Write all delete-expressions that we would like to
5876 // analyze later in AST.
5877 RecordData DeleteExprsToAnalyze;
5878 if (!isModule) {
5879 for (const auto &DeleteExprsInfo :
5880 SemaRef.getMismatchingDeleteExpressions()) {
5881 if (!wasDeclEmitted(D: DeleteExprsInfo.first))
5882 continue;
5883
5884 AddDeclRef(D: DeleteExprsInfo.first, Record&: DeleteExprsToAnalyze);
5885 DeleteExprsToAnalyze.push_back(Elt: DeleteExprsInfo.second.size());
5886 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
5887 AddSourceLocation(Loc: DeleteLoc.first, Record&: DeleteExprsToAnalyze);
5888 DeleteExprsToAnalyze.push_back(Elt: DeleteLoc.second);
5889 }
5890 }
5891 }
5892 if (!DeleteExprsToAnalyze.empty())
5893 Stream.EmitRecord(Code: DELETE_EXPRS_TO_ANALYZE, Vals: DeleteExprsToAnalyze);
5894
5895 RecordData VTablesToEmit;
5896 for (CXXRecordDecl *RD : PendingEmittingVTables) {
5897 if (!wasDeclEmitted(D: RD))
5898 continue;
5899
5900 AddDeclRef(D: RD, Record&: VTablesToEmit);
5901 }
5902
5903 if (!VTablesToEmit.empty())
5904 Stream.EmitRecord(Code: VTABLES_TO_EMIT, Vals: VTablesToEmit);
5905}
5906
5907ASTFileSignature ASTWriter::WriteASTCore(Sema *SemaPtr, StringRef isysroot,
5908 Module *WritingModule) {
5909 using namespace llvm;
5910
5911 bool isModule = WritingModule != nullptr;
5912
5913 // Make sure that the AST reader knows to finalize itself.
5914 if (Chain)
5915 Chain->finalizeForWriting();
5916
5917 // This needs to be done very early, since everything that writes
5918 // SourceLocations or FileIDs depends on it.
5919 computeNonAffectingInputFiles();
5920
5921 writeUnhashedControlBlock(PP&: *PP);
5922
5923 // Don't reuse type ID and Identifier ID from readers for C++ standard named
5924 // modules since we want to support no-transitive-change model for named
5925 // modules. The theory for no-transitive-change model is,
5926 // for a user of a named module, the user can only access the indirectly
5927 // imported decls via the directly imported module. So that it is possible to
5928 // control what matters to the users when writing the module. It would be
5929 // problematic if the users can reuse the type IDs and identifier IDs from
5930 // indirectly imported modules arbitrarily. So we choose to clear these ID
5931 // here.
5932 if (isWritingStdCXXNamedModules()) {
5933 TypeIdxs.clear();
5934 IdentifierIDs.clear();
5935 }
5936
5937 // Look for any identifiers that were named while processing the
5938 // headers, but are otherwise not needed. We add these to the hash
5939 // table to enable checking of the predefines buffer in the case
5940 // where the user adds new macro definitions when building the AST
5941 // file.
5942 //
5943 // We do this before emitting any Decl and Types to make sure the
5944 // Identifier ID is stable.
5945 SmallVector<const IdentifierInfo *, 128> IIs;
5946 for (const auto &ID : PP->getIdentifierTable())
5947 if (IsInterestingNonMacroIdentifier(II: ID.second, Writer&: *this))
5948 IIs.push_back(Elt: ID.second);
5949 // Sort the identifiers lexicographically before getting the references so
5950 // that their order is stable.
5951 llvm::sort(C&: IIs, Comp: llvm::deref<std::less<>>());
5952 for (const IdentifierInfo *II : IIs)
5953 getIdentifierRef(II);
5954
5955 // Write the set of weak, undeclared identifiers. We always write the
5956 // entire table, since later PCH files in a PCH chain are only interested in
5957 // the results at the end of the chain.
5958 RecordData WeakUndeclaredIdentifiers;
5959 if (SemaPtr) {
5960 for (const auto &WeakUndeclaredIdentifierList :
5961 SemaPtr->WeakUndeclaredIdentifiers) {
5962 const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
5963 for (const auto &WI : WeakUndeclaredIdentifierList.second) {
5964 AddIdentifierRef(II, Record&: WeakUndeclaredIdentifiers);
5965 AddIdentifierRef(II: WI.getAlias(), Record&: WeakUndeclaredIdentifiers);
5966 AddSourceLocation(Loc: WI.getLocation(), Record&: WeakUndeclaredIdentifiers);
5967 }
5968 }
5969 }
5970
5971 // Form the record of special types.
5972 RecordData SpecialTypes;
5973 if (SemaPtr) {
5974 ASTContext &Context = SemaPtr->Context;
5975 AddTypeRef(Context, T: Context.getRawCFConstantStringType(), Record&: SpecialTypes);
5976 AddTypeRef(Context, T: Context.getFILEType(), Record&: SpecialTypes);
5977 AddTypeRef(Context, T: Context.getjmp_bufType(), Record&: SpecialTypes);
5978 AddTypeRef(Context, T: Context.getsigjmp_bufType(), Record&: SpecialTypes);
5979 AddTypeRef(Context, T: Context.ObjCIdRedefinitionType, Record&: SpecialTypes);
5980 AddTypeRef(Context, T: Context.ObjCClassRedefinitionType, Record&: SpecialTypes);
5981 AddTypeRef(Context, T: Context.ObjCSelRedefinitionType, Record&: SpecialTypes);
5982 AddTypeRef(Context, T: Context.getucontext_tType(), Record&: SpecialTypes);
5983 }
5984
5985 if (SemaPtr)
5986 PrepareWritingSpecialDecls(SemaRef&: *SemaPtr);
5987
5988 // Write the control block
5989 WriteControlBlock(PP&: *PP, isysroot);
5990
5991 // Write the remaining AST contents.
5992 Stream.FlushToWord();
5993 ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
5994 Stream.EnterSubblock(BlockID: AST_BLOCK_ID, CodeLen: 5);
5995 ASTBlockStartOffset = Stream.GetCurrentBitNo();
5996
5997 // This is so that older clang versions, before the introduction
5998 // of the control block, can read and reject the newer PCH format.
5999 {
6000 RecordData Record = {VERSION_MAJOR};
6001 Stream.EmitRecord(Code: METADATA_OLD_FORMAT, Vals: Record);
6002 }
6003
6004 // For method pool in the module, if it contains an entry for a selector,
6005 // the entry should be complete, containing everything introduced by that
6006 // module and all modules it imports. It's possible that the entry is out of
6007 // date, so we need to pull in the new content here.
6008
6009 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
6010 // safe, we copy all selectors out.
6011 if (SemaPtr) {
6012 llvm::SmallVector<Selector, 256> AllSelectors;
6013 for (auto &SelectorAndID : SelectorIDs)
6014 AllSelectors.push_back(Elt: SelectorAndID.first);
6015 for (auto &Selector : AllSelectors)
6016 SemaPtr->ObjC().updateOutOfDateSelector(Sel: Selector);
6017 }
6018
6019 if (Chain) {
6020 // Write the mapping information describing our module dependencies and how
6021 // each of those modules were mapped into our own offset/ID space, so that
6022 // the reader can build the appropriate mapping to its own offset/ID space.
6023 // The map consists solely of a blob with the following format:
6024 // *(module-kind:i8
6025 // module-name-len:i16 module-name:len*i8
6026 // source-location-offset:i32
6027 // identifier-id:i32
6028 // preprocessed-entity-id:i32
6029 // macro-definition-id:i32
6030 // submodule-id:i32
6031 // selector-id:i32
6032 // declaration-id:i32
6033 // c++-base-specifiers-id:i32
6034 // type-id:i32)
6035 //
6036 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
6037 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
6038 // module name. Otherwise, it is the module file name.
6039 auto Abbrev = std::make_shared<BitCodeAbbrev>();
6040 Abbrev->Add(OpInfo: BitCodeAbbrevOp(MODULE_OFFSET_MAP));
6041 Abbrev->Add(OpInfo: BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
6042 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abbrev));
6043 SmallString<2048> Buffer;
6044 {
6045 llvm::raw_svector_ostream Out(Buffer);
6046 for (ModuleFile &M : Chain->ModuleMgr) {
6047 using namespace llvm::support;
6048
6049 endian::Writer LE(Out, llvm::endianness::little);
6050 LE.write<uint8_t>(Val: static_cast<uint8_t>(M.Kind));
6051 StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
6052 LE.write<uint16_t>(Val: Name.size());
6053 Out.write(Ptr: Name.data(), Size: Name.size());
6054
6055 // Note: if a base ID was uint max, it would not be possible to load
6056 // another module after it or have more than one entity inside it.
6057 uint32_t None = std::numeric_limits<uint32_t>::max();
6058
6059 auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
6060 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
6061 if (ShouldWrite)
6062 LE.write<uint32_t>(BaseID);
6063 else
6064 LE.write<uint32_t>(Val: None);
6065 };
6066
6067 // These values should be unique within a chain, since they will be read
6068 // as keys into ContinuousRangeMaps.
6069 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
6070 writeBaseIDOrNone(M.BasePreprocessedEntityID,
6071 M.NumPreprocessedEntities);
6072 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
6073 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
6074 }
6075 }
6076 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
6077 Stream.EmitRecordWithBlob(Abbrev: ModuleOffsetMapAbbrev, Vals: Record,
6078 BlobData: Buffer.data(), BlobLen: Buffer.size());
6079 }
6080
6081 if (SemaPtr)
6082 WriteDeclAndTypes(Context&: SemaPtr->Context);
6083
6084 WriteFileDeclIDsMap();
6085 WriteSourceManagerBlock(SourceMgr&: PP->getSourceManager());
6086 if (SemaPtr)
6087 WriteComments(Context&: SemaPtr->Context);
6088 WritePreprocessor(PP: *PP, IsModule: isModule);
6089 WriteHeaderSearch(HS: PP->getHeaderSearchInfo());
6090 if (SemaPtr) {
6091 WriteSelectors(SemaRef&: *SemaPtr);
6092 WriteReferencedSelectorsPool(SemaRef&: *SemaPtr);
6093 WriteLateParsedTemplates(SemaRef&: *SemaPtr);
6094 }
6095 WriteIdentifierTable(PP&: *PP, IdResolver: SemaPtr ? &SemaPtr->IdResolver : nullptr, IsModule: isModule);
6096 if (SemaPtr) {
6097 WriteFPPragmaOptions(Opts: SemaPtr->CurFPFeatureOverrides());
6098 WriteOpenCLExtensions(SemaRef&: *SemaPtr);
6099 WriteCUDAPragmas(SemaRef&: *SemaPtr);
6100 }
6101
6102 // If we're emitting a module, write out the submodule information.
6103 if (WritingModule)
6104 WriteSubmodules(WritingModule, Context: SemaPtr ? &SemaPtr->Context : nullptr);
6105
6106 Stream.EmitRecord(Code: SPECIAL_TYPES, Vals: SpecialTypes);
6107
6108 if (SemaPtr)
6109 WriteSpecialDeclRecords(SemaRef&: *SemaPtr);
6110
6111 // Write the record containing weak undeclared identifiers.
6112 if (!WeakUndeclaredIdentifiers.empty())
6113 Stream.EmitRecord(Code: WEAK_UNDECLARED_IDENTIFIERS,
6114 Vals: WeakUndeclaredIdentifiers);
6115
6116 if (!WritingModule) {
6117 // Write the submodules that were imported, if any.
6118 struct ModuleInfo {
6119 uint64_t ID;
6120 Module *M;
6121 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
6122 };
6123 llvm::SmallVector<ModuleInfo, 64> Imports;
6124 if (SemaPtr) {
6125 for (const auto *I : SemaPtr->Context.local_imports()) {
6126 assert(SubmoduleIDs.contains(I->getImportedModule()));
6127 Imports.push_back(Elt: ModuleInfo(SubmoduleIDs[I->getImportedModule()],
6128 I->getImportedModule()));
6129 }
6130 }
6131
6132 if (!Imports.empty()) {
6133 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
6134 return A.ID < B.ID;
6135 };
6136 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
6137 return A.ID == B.ID;
6138 };
6139
6140 // Sort and deduplicate module IDs.
6141 llvm::sort(C&: Imports, Comp: Cmp);
6142 Imports.erase(CS: llvm::unique(R&: Imports, P: Eq), CE: Imports.end());
6143
6144 RecordData ImportedModules;
6145 for (const auto &Import : Imports) {
6146 ImportedModules.push_back(Elt: Import.ID);
6147 // FIXME: If the module has macros imported then later has declarations
6148 // imported, this location won't be the right one as a location for the
6149 // declaration imports.
6150 AddSourceLocation(Loc: PP->getModuleImportLoc(M: Import.M), Record&: ImportedModules);
6151 }
6152
6153 Stream.EmitRecord(Code: IMPORTED_MODULES, Vals: ImportedModules);
6154 }
6155 }
6156
6157 WriteObjCCategories();
6158 if (SemaPtr) {
6159 if (!WritingModule) {
6160 WriteOptimizePragmaOptions(SemaRef&: *SemaPtr);
6161 WriteMSStructPragmaOptions(SemaRef&: *SemaPtr);
6162 WriteMSPointersToMembersPragmaOptions(SemaRef&: *SemaPtr);
6163 }
6164 WritePackPragmaOptions(SemaRef&: *SemaPtr);
6165 WriteFloatControlPragmaOptions(SemaRef&: *SemaPtr);
6166 WriteDeclsWithEffectsToVerify(SemaRef&: *SemaPtr);
6167 }
6168
6169 // Some simple statistics
6170 RecordData::value_type Record[] = {NumStatements,
6171 NumMacros,
6172 NumLexicalDeclContexts,
6173 NumVisibleDeclContexts,
6174 NumModuleLocalDeclContexts,
6175 NumTULocalDeclContexts};
6176 Stream.EmitRecord(Code: STATISTICS, Vals: Record);
6177 Stream.ExitBlock();
6178 Stream.FlushToWord();
6179 ASTBlockRange.second = Stream.GetCurrentBitNo() >> 3;
6180
6181 // Write the module file extension blocks.
6182 if (SemaPtr)
6183 for (const auto &ExtWriter : ModuleFileExtensionWriters)
6184 WriteModuleFileExtension(SemaRef&: *SemaPtr, Writer&: *ExtWriter);
6185
6186 return backpatchSignature();
6187}
6188
6189void ASTWriter::EnteringModulePurview() {
6190 // In C++20 named modules, all entities before entering the module purview
6191 // lives in the GMF.
6192 if (GeneratingReducedBMI)
6193 DeclUpdatesFromGMF.swap(RHS&: DeclUpdates);
6194}
6195
6196// Add update records for all mangling numbers and static local numbers.
6197// These aren't really update records, but this is a convenient way of
6198// tagging this rare extra data onto the declarations.
6199void ASTWriter::AddedManglingNumber(const Decl *D, unsigned Number) {
6200 if (D->isFromASTFile())
6201 return;
6202
6203 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::ManglingNumber, Number));
6204}
6205void ASTWriter::AddedStaticLocalNumbers(const Decl *D, unsigned Number) {
6206 if (D->isFromASTFile())
6207 return;
6208
6209 DeclUpdates[D].push_back(
6210 Elt: DeclUpdate(DeclUpdateKind::StaticLocalNumber, Number));
6211}
6212
6213void ASTWriter::AddedAnonymousNamespace(const TranslationUnitDecl *TU,
6214 NamespaceDecl *AnonNamespace) {
6215 // If the translation unit has an anonymous namespace, and we don't already
6216 // have an update block for it, write it as an update block.
6217 // FIXME: Why do we not do this if there's already an update block?
6218 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
6219 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
6220 if (Record.empty())
6221 Record.push_back(
6222 Elt: DeclUpdate(DeclUpdateKind::CXXAddedAnonymousNamespace, NS));
6223 }
6224}
6225
6226void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
6227 // Keep writing types, declarations, and declaration update records
6228 // until we've emitted all of them.
6229 RecordData DeclUpdatesOffsetsRecord;
6230 Stream.EnterSubblock(BlockID: DECLTYPES_BLOCK_ID, /*bits for abbreviations*/ CodeLen: 6);
6231 DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
6232 WriteTypeAbbrevs();
6233 WriteDeclAbbrevs();
6234 do {
6235 WriteDeclUpdatesBlocks(Context, OffsetsRecord&: DeclUpdatesOffsetsRecord);
6236 while (!DeclTypesToEmit.empty()) {
6237 DeclOrType DOT = DeclTypesToEmit.front();
6238 DeclTypesToEmit.pop();
6239 if (DOT.isType())
6240 WriteType(Context, T: DOT.getType());
6241 else
6242 WriteDecl(Context, D: DOT.getDecl());
6243 }
6244 } while (!DeclUpdates.empty());
6245
6246 DoneWritingDeclsAndTypes = true;
6247
6248 // DelayedNamespace is only meaningful in reduced BMI.
6249 // See the comments of DelayedNamespace for details.
6250 assert(DelayedNamespace.empty() || GeneratingReducedBMI);
6251 RecordData DelayedNamespaceRecord;
6252 for (NamespaceDecl *NS : DelayedNamespace) {
6253 LookupBlockOffsets Offsets;
6254
6255 Offsets.LexicalOffset = WriteDeclContextLexicalBlock(Context, DC: NS);
6256 WriteDeclContextVisibleBlock(Context, DC: NS, Offsets);
6257
6258 if (Offsets.LexicalOffset)
6259 Offsets.LexicalOffset -= DeclTypesBlockStartOffset;
6260
6261 // Write the offset relative to current block.
6262 if (Offsets.VisibleOffset)
6263 Offsets.VisibleOffset -= DeclTypesBlockStartOffset;
6264
6265 if (Offsets.ModuleLocalOffset)
6266 Offsets.ModuleLocalOffset -= DeclTypesBlockStartOffset;
6267
6268 if (Offsets.TULocalOffset)
6269 Offsets.TULocalOffset -= DeclTypesBlockStartOffset;
6270
6271 AddDeclRef(D: NS, Record&: DelayedNamespaceRecord);
6272 AddLookupOffsets(Offsets, Record&: DelayedNamespaceRecord);
6273 }
6274
6275 // The process of writing lexical and visible block for delayed namespace
6276 // shouldn't introduce any new decls, types or update to emit.
6277 assert(DeclTypesToEmit.empty());
6278 assert(DeclUpdates.empty());
6279
6280 Stream.ExitBlock();
6281
6282 // These things can only be done once we've written out decls and types.
6283 WriteTypeDeclOffsets();
6284 if (!DeclUpdatesOffsetsRecord.empty())
6285 Stream.EmitRecord(Code: DECL_UPDATE_OFFSETS, Vals: DeclUpdatesOffsetsRecord);
6286
6287 if (!DelayedNamespaceRecord.empty())
6288 Stream.EmitRecord(Code: DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD,
6289 Vals: DelayedNamespaceRecord);
6290
6291 if (!RelatedDeclsMap.empty()) {
6292 // TODO: on disk hash table for related decls mapping might be more
6293 // efficent becuase it allows lazy deserialization.
6294 RecordData RelatedDeclsMapRecord;
6295 for (const auto &Pair : RelatedDeclsMap) {
6296 RelatedDeclsMapRecord.push_back(Elt: Pair.first.getRawValue());
6297 RelatedDeclsMapRecord.push_back(Elt: Pair.second.size());
6298 for (const auto &Lambda : Pair.second)
6299 RelatedDeclsMapRecord.push_back(Elt: Lambda.getRawValue());
6300 }
6301
6302 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
6303 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(RELATED_DECLS_MAP));
6304 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Array));
6305 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6306 unsigned FunctionToLambdaMapAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6307 Stream.EmitRecord(Code: RELATED_DECLS_MAP, Vals: RelatedDeclsMapRecord,
6308 Abbrev: FunctionToLambdaMapAbbrev);
6309 }
6310
6311 if (!SpecializationsUpdates.empty()) {
6312 WriteSpecializationsUpdates(/*IsPartial=*/false);
6313 SpecializationsUpdates.clear();
6314 }
6315
6316 if (!PartialSpecializationsUpdates.empty()) {
6317 WriteSpecializationsUpdates(/*IsPartial=*/true);
6318 PartialSpecializationsUpdates.clear();
6319 }
6320
6321 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
6322 // Create a lexical update block containing all of the declarations in the
6323 // translation unit that do not come from other AST files.
6324 SmallVector<DeclID, 128> NewGlobalKindDeclPairs;
6325 for (const auto *D : TU->noload_decls()) {
6326 if (D->isFromASTFile())
6327 continue;
6328
6329 // In reduced BMI, skip unreached declarations.
6330 if (!wasDeclEmitted(D))
6331 continue;
6332
6333 NewGlobalKindDeclPairs.push_back(Elt: D->getKind());
6334 NewGlobalKindDeclPairs.push_back(Elt: GetDeclRef(D).getRawValue());
6335 }
6336
6337 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
6338 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
6339 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6340 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6341
6342 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
6343 Stream.EmitRecordWithBlob(Abbrev: TuUpdateLexicalAbbrev, Vals: Record,
6344 Blob: bytes(v: NewGlobalKindDeclPairs));
6345
6346 Abv = std::make_shared<llvm::BitCodeAbbrev>();
6347 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
6348 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6349 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6350 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6351
6352 Abv = std::make_shared<llvm::BitCodeAbbrev>();
6353 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(UPDATE_MODULE_LOCAL_VISIBLE));
6354 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6355 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6356 ModuleLocalUpdateVisibleAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6357
6358 Abv = std::make_shared<llvm::BitCodeAbbrev>();
6359 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(UPDATE_TU_LOCAL_VISIBLE));
6360 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6361 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6362 TULocalUpdateVisibleAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6363
6364 // And a visible updates block for the translation unit.
6365 WriteDeclContextVisibleUpdate(Context, DC: TU);
6366
6367 // If we have any extern "C" names, write out a visible update for them.
6368 if (Context.ExternCContext)
6369 WriteDeclContextVisibleUpdate(Context, DC: Context.ExternCContext);
6370
6371 // Write the visible updates to DeclContexts.
6372 for (auto *DC : UpdatedDeclContexts)
6373 WriteDeclContextVisibleUpdate(Context, DC);
6374}
6375
6376void ASTWriter::WriteSpecializationsUpdates(bool IsPartial) {
6377 auto RecordType = IsPartial ? CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION
6378 : CXX_ADDED_TEMPLATE_SPECIALIZATION;
6379
6380 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
6381 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(RecordType));
6382 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
6383 Abv->Add(OpInfo: llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
6384 auto UpdateSpecializationAbbrev = Stream.EmitAbbrev(Abbv: std::move(Abv));
6385
6386 auto &SpecUpdates =
6387 IsPartial ? PartialSpecializationsUpdates : SpecializationsUpdates;
6388 for (auto &SpecializationUpdate : SpecUpdates) {
6389 const NamedDecl *D = SpecializationUpdate.first;
6390
6391 llvm::SmallString<4096> LookupTable;
6392 GenerateSpecializationInfoLookupTable(D, Specializations&: SpecializationUpdate.second,
6393 LookupTable, IsPartial);
6394
6395 // Write the lookup table
6396 RecordData::value_type Record[] = {
6397 static_cast<RecordData::value_type>(RecordType),
6398 getDeclID(D).getRawValue()};
6399 Stream.EmitRecordWithBlob(Abbrev: UpdateSpecializationAbbrev, Vals: Record, Blob: LookupTable);
6400 }
6401}
6402
6403void ASTWriter::WriteDeclUpdatesBlocks(ASTContext &Context,
6404 RecordDataImpl &OffsetsRecord) {
6405 if (DeclUpdates.empty())
6406 return;
6407
6408 DeclUpdateMap LocalUpdates;
6409 LocalUpdates.swap(RHS&: DeclUpdates);
6410
6411 for (auto &DeclUpdate : LocalUpdates) {
6412 const Decl *D = DeclUpdate.first;
6413
6414 bool HasUpdatedBody = false;
6415 bool HasAddedVarDefinition = false;
6416 RecordData RecordData;
6417 ASTRecordWriter Record(Context, *this, RecordData);
6418 for (auto &Update : DeclUpdate.second) {
6419 DeclUpdateKind Kind = Update.getKind();
6420
6421 // An updated body is emitted last, so that the reader doesn't need
6422 // to skip over the lazy body to reach statements for other records.
6423 if (Kind == DeclUpdateKind::CXXAddedFunctionDefinition)
6424 HasUpdatedBody = true;
6425 else if (Kind == DeclUpdateKind::CXXAddedVarDefinition)
6426 HasAddedVarDefinition = true;
6427 else
6428 Record.push_back(N: llvm::to_underlying(E: Kind));
6429
6430 switch (Kind) {
6431 case DeclUpdateKind::CXXAddedImplicitMember:
6432 case DeclUpdateKind::CXXAddedAnonymousNamespace:
6433 assert(Update.getDecl() && "no decl to add?");
6434 Record.AddDeclRef(D: Update.getDecl());
6435 break;
6436 case DeclUpdateKind::CXXAddedFunctionDefinition:
6437 case DeclUpdateKind::CXXAddedVarDefinition:
6438 break;
6439
6440 case DeclUpdateKind::CXXPointOfInstantiation:
6441 // FIXME: Do we need to also save the template specialization kind here?
6442 Record.AddSourceLocation(Loc: Update.getLoc());
6443 break;
6444
6445 case DeclUpdateKind::CXXInstantiatedDefaultArgument:
6446 Record.writeStmtRef(
6447 S: cast<ParmVarDecl>(Val: Update.getDecl())->getDefaultArg());
6448 break;
6449
6450 case DeclUpdateKind::CXXInstantiatedDefaultMemberInitializer:
6451 Record.AddStmt(
6452 S: cast<FieldDecl>(Val: Update.getDecl())->getInClassInitializer());
6453 break;
6454
6455 case DeclUpdateKind::CXXInstantiatedClassDefinition: {
6456 auto *RD = cast<CXXRecordDecl>(Val: D);
6457 UpdatedDeclContexts.insert(X: RD->getPrimaryContext());
6458 Record.push_back(N: RD->isParamDestroyedInCallee());
6459 Record.push_back(N: llvm::to_underlying(E: RD->getArgPassingRestrictions()));
6460 Record.AddCXXDefinitionData(D: RD);
6461 Record.AddOffset(BitOffset: WriteDeclContextLexicalBlock(Context, DC: RD));
6462
6463 // This state is sometimes updated by template instantiation, when we
6464 // switch from the specialization referring to the template declaration
6465 // to it referring to the template definition.
6466 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
6467 Record.push_back(N: MSInfo->getTemplateSpecializationKind());
6468 Record.AddSourceLocation(Loc: MSInfo->getPointOfInstantiation());
6469 } else {
6470 auto *Spec = cast<ClassTemplateSpecializationDecl>(Val: RD);
6471 Record.push_back(N: Spec->getTemplateSpecializationKind());
6472 Record.AddSourceLocation(Loc: Spec->getPointOfInstantiation());
6473
6474 // The instantiation might have been resolved to a partial
6475 // specialization. If so, record which one.
6476 auto From = Spec->getInstantiatedFrom();
6477 if (auto PartialSpec =
6478 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
6479 Record.push_back(N: true);
6480 Record.AddDeclRef(D: PartialSpec);
6481 Record.AddTemplateArgumentList(
6482 TemplateArgs: &Spec->getTemplateInstantiationArgs());
6483 } else {
6484 Record.push_back(N: false);
6485 }
6486 }
6487 Record.push_back(N: llvm::to_underlying(E: RD->getTagKind()));
6488 Record.AddSourceLocation(Loc: RD->getLocation());
6489 Record.AddSourceLocation(Loc: RD->getBeginLoc());
6490 Record.AddSourceRange(Range: RD->getBraceRange());
6491
6492 // Instantiation may change attributes; write them all out afresh.
6493 Record.push_back(N: D->hasAttrs());
6494 if (D->hasAttrs())
6495 Record.AddAttributes(Attrs: D->getAttrs());
6496
6497 // FIXME: Ensure we don't get here for explicit instantiations.
6498 break;
6499 }
6500
6501 case DeclUpdateKind::CXXResolvedDtorDelete:
6502 Record.AddDeclRef(D: Update.getDecl());
6503 Record.AddStmt(S: cast<CXXDestructorDecl>(Val: D)->getOperatorDeleteThisArg());
6504 break;
6505
6506 case DeclUpdateKind::CXXResolvedExceptionSpec: {
6507 auto prototype =
6508 cast<FunctionDecl>(Val: D)->getType()->castAs<FunctionProtoType>();
6509 Record.writeExceptionSpecInfo(esi: prototype->getExceptionSpecInfo());
6510 break;
6511 }
6512
6513 case DeclUpdateKind::CXXDeducedReturnType:
6514 Record.push_back(N: GetOrCreateTypeID(Context, T: Update.getType()));
6515 break;
6516
6517 case DeclUpdateKind::DeclMarkedUsed:
6518 break;
6519
6520 case DeclUpdateKind::ManglingNumber:
6521 case DeclUpdateKind::StaticLocalNumber:
6522 Record.push_back(N: Update.getNumber());
6523 break;
6524
6525 case DeclUpdateKind::DeclMarkedOpenMPThreadPrivate:
6526 Record.AddSourceRange(
6527 Range: D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
6528 break;
6529
6530 case DeclUpdateKind::DeclMarkedOpenMPAllocate: {
6531 auto *A = D->getAttr<OMPAllocateDeclAttr>();
6532 Record.push_back(N: A->getAllocatorType());
6533 Record.AddStmt(S: A->getAllocator());
6534 Record.AddStmt(S: A->getAlignment());
6535 Record.AddSourceRange(Range: A->getRange());
6536 break;
6537 }
6538
6539 case DeclUpdateKind::DeclMarkedOpenMPDeclareTarget:
6540 Record.push_back(N: D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
6541 Record.AddSourceRange(
6542 Range: D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
6543 break;
6544
6545 case DeclUpdateKind::DeclExported:
6546 Record.push_back(N: getSubmoduleID(Mod: Update.getModule()));
6547 break;
6548
6549 case DeclUpdateKind::AddedAttrToRecord:
6550 Record.AddAttributes(Attrs: llvm::ArrayRef(Update.getAttr()));
6551 break;
6552 }
6553 }
6554
6555 // Add a trailing update record, if any. These must go last because we
6556 // lazily load their attached statement.
6557 if (!GeneratingReducedBMI || !CanElideDeclDef(D)) {
6558 if (HasUpdatedBody) {
6559 const auto *Def = cast<FunctionDecl>(Val: D);
6560 Record.push_back(
6561 N: llvm::to_underlying(E: DeclUpdateKind::CXXAddedFunctionDefinition));
6562 Record.push_back(N: Def->isInlined());
6563 Record.AddSourceLocation(Loc: Def->getInnerLocStart());
6564 Record.AddFunctionDefinition(FD: Def);
6565 } else if (HasAddedVarDefinition) {
6566 const auto *VD = cast<VarDecl>(Val: D);
6567 Record.push_back(
6568 N: llvm::to_underlying(E: DeclUpdateKind::CXXAddedVarDefinition));
6569 Record.push_back(N: VD->isInline());
6570 Record.push_back(N: VD->isInlineSpecified());
6571 Record.AddVarDeclInit(VD);
6572 }
6573 }
6574
6575 AddDeclRef(D, Record&: OffsetsRecord);
6576 OffsetsRecord.push_back(Elt: Record.Emit(Code: DECL_UPDATES));
6577 }
6578}
6579
6580void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
6581 RecordDataImpl &Record) {
6582 uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
6583 Record.push_back(Elt: Raw);
6584}
6585
6586FileID ASTWriter::getAdjustedFileID(FileID FID) const {
6587 if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
6588 NonAffectingFileIDs.empty())
6589 return FID;
6590 auto It = llvm::lower_bound(Range: NonAffectingFileIDs, Value&: FID);
6591 unsigned Idx = std::distance(first: NonAffectingFileIDs.begin(), last: It);
6592 unsigned Offset = NonAffectingFileIDAdjustments[Idx];
6593 return FileID::get(V: FID.getOpaqueValue() - Offset);
6594}
6595
6596unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
6597 unsigned NumCreatedFIDs = PP->getSourceManager()
6598 .getLocalSLocEntry(Index: FID.ID)
6599 .getFile()
6600 .NumCreatedFIDs;
6601
6602 unsigned AdjustedNumCreatedFIDs = 0;
6603 for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
6604 if (IsSLocAffecting[I])
6605 ++AdjustedNumCreatedFIDs;
6606 return AdjustedNumCreatedFIDs;
6607}
6608
6609SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
6610 if (Loc.isInvalid())
6611 return Loc;
6612 return Loc.getLocWithOffset(Offset: -getAdjustment(Offset: Loc.getOffset()));
6613}
6614
6615SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
6616 return SourceRange(getAdjustedLocation(Loc: Range.getBegin()),
6617 getAdjustedLocation(Loc: Range.getEnd()));
6618}
6619
6620SourceLocation::UIntTy
6621ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
6622 return Offset - getAdjustment(Offset);
6623}
6624
6625SourceLocation::UIntTy
6626ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
6627 if (NonAffectingRanges.empty())
6628 return 0;
6629
6630 if (PP->getSourceManager().isLoadedOffset(SLocOffset: Offset))
6631 return 0;
6632
6633 if (Offset > NonAffectingRanges.back().getEnd().getOffset())
6634 return NonAffectingOffsetAdjustments.back();
6635
6636 if (Offset < NonAffectingRanges.front().getBegin().getOffset())
6637 return 0;
6638
6639 auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
6640 return Range.getEnd().getOffset() < Offset;
6641 };
6642
6643 auto It = llvm::lower_bound(Range: NonAffectingRanges, Value&: Offset, C: Contains);
6644 unsigned Idx = std::distance(first: NonAffectingRanges.begin(), last: It);
6645 return NonAffectingOffsetAdjustments[Idx];
6646}
6647
6648void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
6649 Record.push_back(Elt: getAdjustedFileID(FID).getOpaqueValue());
6650}
6651
6652SourceLocationEncoding::RawLocEncoding
6653ASTWriter::getRawSourceLocationEncoding(SourceLocation Loc) {
6654 SourceLocation::UIntTy BaseOffset = 0;
6655 unsigned ModuleFileIndex = 0;
6656
6657 // See SourceLocationEncoding.h for the encoding details.
6658 if (PP->getSourceManager().isLoadedSourceLocation(Loc) && Loc.isValid()) {
6659 assert(getChain());
6660 auto SLocMapI = getChain()->GlobalSLocOffsetMap.find(
6661 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6662 assert(SLocMapI != getChain()->GlobalSLocOffsetMap.end() &&
6663 "Corrupted global sloc offset map");
6664 ModuleFile *F = SLocMapI->second;
6665 BaseOffset = F->SLocEntryBaseOffset - 2;
6666 // 0 means the location is not loaded. So we need to add 1 to the index to
6667 // make it clear.
6668 ModuleFileIndex = F->Index + 1;
6669 assert(&getChain()->getModuleManager()[F->Index] == F);
6670 }
6671
6672 return SourceLocationEncoding::encode(Loc, BaseOffset, BaseModuleFileIndex: ModuleFileIndex);
6673}
6674
6675void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
6676 Loc = getAdjustedLocation(Loc);
6677 Record.push_back(Elt: getRawSourceLocationEncoding(Loc));
6678}
6679
6680void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
6681 AddSourceLocation(Loc: Range.getBegin(), Record);
6682 AddSourceLocation(Loc: Range.getEnd(), Record);
6683}
6684
6685void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
6686 AddAPInt(Value: Value.bitcastToAPInt());
6687}
6688
6689void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
6690 Record.push_back(Elt: getIdentifierRef(II));
6691}
6692
6693IdentifierID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
6694 if (!II)
6695 return 0;
6696
6697 IdentifierID &ID = IdentifierIDs[II];
6698 if (ID == 0)
6699 ID = NextIdentID++;
6700 return ID;
6701}
6702
6703MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
6704 // Don't emit builtin macros like __LINE__ to the AST file unless they
6705 // have been redefined by the header (in which case they are not
6706 // isBuiltinMacro).
6707 if (!MI || MI->isBuiltinMacro())
6708 return 0;
6709
6710 MacroID &ID = MacroIDs[MI];
6711 if (ID == 0) {
6712 ID = NextMacroID++;
6713 MacroInfoToEmitData Info = { .Name: Name, .MI: MI, .ID: ID };
6714 MacroInfosToEmit.push_back(x: Info);
6715 }
6716 return ID;
6717}
6718
6719uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
6720 return IdentMacroDirectivesOffsetMap.lookup(Val: Name);
6721}
6722
6723void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
6724 Record->push_back(Elt: Writer->getSelectorRef(Sel: SelRef));
6725}
6726
6727SelectorID ASTWriter::getSelectorRef(Selector Sel) {
6728 if (Sel.getAsOpaquePtr() == nullptr) {
6729 return 0;
6730 }
6731
6732 SelectorID SID = SelectorIDs[Sel];
6733 if (SID == 0 && Chain) {
6734 // This might trigger a ReadSelector callback, which will set the ID for
6735 // this selector.
6736 Chain->LoadSelector(Sel);
6737 SID = SelectorIDs[Sel];
6738 }
6739 if (SID == 0) {
6740 SID = NextSelectorID++;
6741 SelectorIDs[Sel] = SID;
6742 }
6743 return SID;
6744}
6745
6746void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
6747 AddDeclRef(D: Temp->getDestructor());
6748}
6749
6750void ASTRecordWriter::AddTemplateArgumentLocInfo(
6751 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
6752 switch (Kind) {
6753 case TemplateArgument::Expression:
6754 AddStmt(S: Arg.getAsExpr());
6755 break;
6756 case TemplateArgument::Type:
6757 AddTypeSourceInfo(TInfo: Arg.getAsTypeSourceInfo());
6758 break;
6759 case TemplateArgument::Template:
6760 AddNestedNameSpecifierLoc(NNS: Arg.getTemplateQualifierLoc());
6761 AddSourceLocation(Loc: Arg.getTemplateNameLoc());
6762 break;
6763 case TemplateArgument::TemplateExpansion:
6764 AddNestedNameSpecifierLoc(NNS: Arg.getTemplateQualifierLoc());
6765 AddSourceLocation(Loc: Arg.getTemplateNameLoc());
6766 AddSourceLocation(Loc: Arg.getTemplateEllipsisLoc());
6767 break;
6768 case TemplateArgument::Null:
6769 case TemplateArgument::Integral:
6770 case TemplateArgument::Declaration:
6771 case TemplateArgument::NullPtr:
6772 case TemplateArgument::StructuralValue:
6773 case TemplateArgument::Pack:
6774 // FIXME: Is this right?
6775 break;
6776 }
6777}
6778
6779void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
6780 AddTemplateArgument(Arg: Arg.getArgument());
6781
6782 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
6783 bool InfoHasSameExpr
6784 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
6785 Record->push_back(Elt: InfoHasSameExpr);
6786 if (InfoHasSameExpr)
6787 return; // Avoid storing the same expr twice.
6788 }
6789 AddTemplateArgumentLocInfo(Kind: Arg.getArgument().getKind(), Arg: Arg.getLocInfo());
6790}
6791
6792void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
6793 if (!TInfo) {
6794 AddTypeRef(T: QualType());
6795 return;
6796 }
6797
6798 AddTypeRef(T: TInfo->getType());
6799 AddTypeLoc(TL: TInfo->getTypeLoc());
6800}
6801
6802void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
6803 TypeLocWriter TLW(*this);
6804 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6805 TLW.Visit(TyLoc: TL);
6806}
6807
6808void ASTWriter::AddTypeRef(ASTContext &Context, QualType T,
6809 RecordDataImpl &Record) {
6810 Record.push_back(Elt: GetOrCreateTypeID(Context, T));
6811}
6812
6813template <typename IdxForTypeTy>
6814static TypeID MakeTypeID(ASTContext &Context, QualType T,
6815 IdxForTypeTy IdxForType) {
6816 if (T.isNull())
6817 return PREDEF_TYPE_NULL_ID;
6818
6819 unsigned FastQuals = T.getLocalFastQualifiers();
6820 T.removeLocalFastQualifiers();
6821
6822 if (T.hasLocalNonFastQualifiers())
6823 return IdxForType(T).asTypeID(FastQuals);
6824
6825 assert(!T.hasLocalQualifiers());
6826
6827 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Val: T.getTypePtr()))
6828 return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
6829
6830 if (T == Context.AutoDeductTy)
6831 return TypeIdx(0, PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
6832 if (T == Context.AutoRRefDeductTy)
6833 return TypeIdx(0, PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
6834
6835 return IdxForType(T).asTypeID(FastQuals);
6836}
6837
6838TypeID ASTWriter::GetOrCreateTypeID(ASTContext &Context, QualType T) {
6839 return MakeTypeID(Context, T, IdxForType: [&](QualType T) -> TypeIdx {
6840 if (T.isNull())
6841 return TypeIdx();
6842 assert(!T.getLocalFastQualifiers());
6843
6844 TypeIdx &Idx = TypeIdxs[T];
6845 if (Idx.getValue() == 0) {
6846 if (DoneWritingDeclsAndTypes) {
6847 assert(0 && "New type seen after serializing all the types to emit!");
6848 return TypeIdx();
6849 }
6850
6851 // We haven't seen this type before. Assign it a new ID and put it
6852 // into the queue of types to emit.
6853 Idx = TypeIdx(0, NextTypeID++);
6854 DeclTypesToEmit.push(x: T);
6855 }
6856 return Idx;
6857 });
6858}
6859
6860void ASTWriter::AddLookupOffsets(const LookupBlockOffsets &Offsets,
6861 RecordDataImpl &Record) {
6862 Record.push_back(Elt: Offsets.LexicalOffset);
6863 Record.push_back(Elt: Offsets.VisibleOffset);
6864 Record.push_back(Elt: Offsets.ModuleLocalOffset);
6865 Record.push_back(Elt: Offsets.TULocalOffset);
6866}
6867
6868void ASTWriter::AddEmittedDeclRef(const Decl *D, RecordDataImpl &Record) {
6869 if (!wasDeclEmitted(D))
6870 return;
6871
6872 AddDeclRef(D, Record);
6873}
6874
6875void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
6876 Record.push_back(Elt: GetDeclRef(D).getRawValue());
6877}
6878
6879LocalDeclID ASTWriter::GetDeclRef(const Decl *D) {
6880 assert(WritingAST && "Cannot request a declaration ID before AST writing");
6881
6882 if (!D) {
6883 return LocalDeclID();
6884 }
6885
6886 // If the DeclUpdate from the GMF gets touched, emit it.
6887 if (auto *Iter = DeclUpdatesFromGMF.find(Key: D);
6888 Iter != DeclUpdatesFromGMF.end()) {
6889 for (DeclUpdate &Update : Iter->second)
6890 DeclUpdates[D].push_back(Elt: Update);
6891 DeclUpdatesFromGMF.erase(Iterator: Iter);
6892 }
6893
6894 // If D comes from an AST file, its declaration ID is already known and
6895 // fixed.
6896 if (D->isFromASTFile()) {
6897 if (isWritingStdCXXNamedModules() && D->getOwningModule())
6898 TouchedTopLevelModules.insert(X: D->getOwningModule()->getTopLevelModule());
6899
6900 return LocalDeclID(D->getGlobalID());
6901 }
6902
6903 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
6904 LocalDeclID &ID = DeclIDs[D];
6905 if (ID.isInvalid()) {
6906 if (DoneWritingDeclsAndTypes) {
6907 assert(0 && "New decl seen after serializing all the decls to emit!");
6908 return LocalDeclID();
6909 }
6910
6911 // We haven't seen this declaration before. Give it a new ID and
6912 // enqueue it in the list of declarations to emit.
6913 ID = NextDeclID++;
6914 DeclTypesToEmit.push(x: const_cast<Decl *>(D));
6915 }
6916
6917 return ID;
6918}
6919
6920LocalDeclID ASTWriter::getDeclID(const Decl *D) {
6921 if (!D)
6922 return LocalDeclID();
6923
6924 // If D comes from an AST file, its declaration ID is already known and
6925 // fixed.
6926 if (D->isFromASTFile())
6927 return LocalDeclID(D->getGlobalID());
6928
6929 assert(DeclIDs.contains(D) && "Declaration not emitted!");
6930 return DeclIDs[D];
6931}
6932
6933bool ASTWriter::wasDeclEmitted(const Decl *D) const {
6934 assert(D);
6935
6936 assert(DoneWritingDeclsAndTypes &&
6937 "wasDeclEmitted should only be called after writing declarations");
6938
6939 if (D->isFromASTFile())
6940 return true;
6941
6942 bool Emitted = DeclIDs.contains(Val: D);
6943 assert((Emitted || (!D->getOwningModule() && isWritingStdCXXNamedModules()) ||
6944 GeneratingReducedBMI) &&
6945 "The declaration within modules can only be omitted in reduced BMI.");
6946 return Emitted;
6947}
6948
6949void ASTWriter::associateDeclWithFile(const Decl *D, LocalDeclID ID) {
6950 assert(ID.isValid());
6951 assert(D);
6952
6953 SourceLocation Loc = D->getLocation();
6954 if (Loc.isInvalid())
6955 return;
6956
6957 // We only keep track of the file-level declarations of each file.
6958 if (!D->getLexicalDeclContext()->isFileContext())
6959 return;
6960 // FIXME: ParmVarDecls that are part of a function type of a parameter of
6961 // a function/objc method, should not have TU as lexical context.
6962 // TemplateTemplateParmDecls that are part of an alias template, should not
6963 // have TU as lexical context.
6964 if (isa<ParmVarDecl, TemplateTemplateParmDecl>(Val: D))
6965 return;
6966
6967 SourceManager &SM = PP->getSourceManager();
6968 SourceLocation FileLoc = SM.getFileLoc(Loc);
6969 assert(SM.isLocalSourceLocation(FileLoc));
6970 auto [FID, Offset] = SM.getDecomposedLoc(Loc: FileLoc);
6971 if (FID.isInvalid())
6972 return;
6973 assert(SM.getSLocEntry(FID).isFile());
6974 assert(IsSLocAffecting[FID.ID]);
6975
6976 std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
6977 if (!Info)
6978 Info = std::make_unique<DeclIDInFileInfo>();
6979
6980 std::pair<unsigned, LocalDeclID> LocDecl(Offset, ID);
6981 LocDeclIDsTy &Decls = Info->DeclIDs;
6982 Decls.push_back(Elt: LocDecl);
6983}
6984
6985unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
6986 assert(needsAnonymousDeclarationNumber(D) &&
6987 "expected an anonymous declaration");
6988
6989 // Number the anonymous declarations within this context, if we've not
6990 // already done so.
6991 auto It = AnonymousDeclarationNumbers.find(Val: D);
6992 if (It == AnonymousDeclarationNumbers.end()) {
6993 auto *DC = D->getLexicalDeclContext();
6994 numberAnonymousDeclsWithin(DC, Visit: [&](const NamedDecl *ND, unsigned Number) {
6995 AnonymousDeclarationNumbers[ND] = Number;
6996 });
6997
6998 It = AnonymousDeclarationNumbers.find(Val: D);
6999 assert(It != AnonymousDeclarationNumbers.end() &&
7000 "declaration not found within its lexical context");
7001 }
7002
7003 return It->second;
7004}
7005
7006void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
7007 DeclarationName Name) {
7008 switch (Name.getNameKind()) {
7009 case DeclarationName::CXXConstructorName:
7010 case DeclarationName::CXXDestructorName:
7011 case DeclarationName::CXXConversionFunctionName:
7012 AddTypeSourceInfo(TInfo: DNLoc.getNamedTypeInfo());
7013 break;
7014
7015 case DeclarationName::CXXOperatorName:
7016 AddSourceRange(Range: DNLoc.getCXXOperatorNameRange());
7017 break;
7018
7019 case DeclarationName::CXXLiteralOperatorName:
7020 AddSourceLocation(Loc: DNLoc.getCXXLiteralOperatorNameLoc());
7021 break;
7022
7023 case DeclarationName::Identifier:
7024 case DeclarationName::ObjCZeroArgSelector:
7025 case DeclarationName::ObjCOneArgSelector:
7026 case DeclarationName::ObjCMultiArgSelector:
7027 case DeclarationName::CXXUsingDirective:
7028 case DeclarationName::CXXDeductionGuideName:
7029 break;
7030 }
7031}
7032
7033void ASTRecordWriter::AddDeclarationNameInfo(
7034 const DeclarationNameInfo &NameInfo) {
7035 AddDeclarationName(Name: NameInfo.getName());
7036 AddSourceLocation(Loc: NameInfo.getLoc());
7037 AddDeclarationNameLoc(DNLoc: NameInfo.getInfo(), Name: NameInfo.getName());
7038}
7039
7040void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
7041 AddNestedNameSpecifierLoc(NNS: Info.QualifierLoc);
7042 Record->push_back(Elt: Info.NumTemplParamLists);
7043 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
7044 AddTemplateParameterList(TemplateParams: Info.TemplParamLists[i]);
7045}
7046
7047void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
7048 // Nested name specifiers usually aren't too long. I think that 8 would
7049 // typically accommodate the vast majority.
7050 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7051
7052 // Push each of the nested-name-specifiers's onto a stack for
7053 // serialization in reverse order.
7054 while (NNS) {
7055 NestedNames.push_back(Elt: NNS);
7056 NNS = NNS.getPrefix();
7057 }
7058
7059 Record->push_back(Elt: NestedNames.size());
7060 while(!NestedNames.empty()) {
7061 NNS = NestedNames.pop_back_val();
7062 NestedNameSpecifier::SpecifierKind Kind
7063 = NNS.getNestedNameSpecifier()->getKind();
7064 Record->push_back(Elt: Kind);
7065 switch (Kind) {
7066 case NestedNameSpecifier::Identifier:
7067 AddIdentifierRef(II: NNS.getNestedNameSpecifier()->getAsIdentifier());
7068 AddSourceRange(Range: NNS.getLocalSourceRange());
7069 break;
7070
7071 case NestedNameSpecifier::Namespace:
7072 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsNamespace());
7073 AddSourceRange(Range: NNS.getLocalSourceRange());
7074 break;
7075
7076 case NestedNameSpecifier::NamespaceAlias:
7077 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
7078 AddSourceRange(Range: NNS.getLocalSourceRange());
7079 break;
7080
7081 case NestedNameSpecifier::TypeSpec:
7082 AddTypeRef(T: NNS.getTypeLoc().getType());
7083 AddTypeLoc(TL: NNS.getTypeLoc());
7084 AddSourceLocation(Loc: NNS.getLocalSourceRange().getEnd());
7085 break;
7086
7087 case NestedNameSpecifier::Global:
7088 AddSourceLocation(Loc: NNS.getLocalSourceRange().getEnd());
7089 break;
7090
7091 case NestedNameSpecifier::Super:
7092 AddDeclRef(D: NNS.getNestedNameSpecifier()->getAsRecordDecl());
7093 AddSourceRange(Range: NNS.getLocalSourceRange());
7094 break;
7095 }
7096 }
7097}
7098
7099void ASTRecordWriter::AddTemplateParameterList(
7100 const TemplateParameterList *TemplateParams) {
7101 assert(TemplateParams && "No TemplateParams!");
7102 AddSourceLocation(Loc: TemplateParams->getTemplateLoc());
7103 AddSourceLocation(Loc: TemplateParams->getLAngleLoc());
7104 AddSourceLocation(Loc: TemplateParams->getRAngleLoc());
7105
7106 Record->push_back(Elt: TemplateParams->size());
7107 for (const auto &P : *TemplateParams)
7108 AddDeclRef(D: P);
7109 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
7110 Record->push_back(Elt: true);
7111 writeStmtRef(S: RequiresClause);
7112 } else {
7113 Record->push_back(Elt: false);
7114 }
7115}
7116
7117/// Emit a template argument list.
7118void ASTRecordWriter::AddTemplateArgumentList(
7119 const TemplateArgumentList *TemplateArgs) {
7120 assert(TemplateArgs && "No TemplateArgs!");
7121 Record->push_back(Elt: TemplateArgs->size());
7122 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
7123 AddTemplateArgument(Arg: TemplateArgs->get(Idx: i));
7124}
7125
7126void ASTRecordWriter::AddASTTemplateArgumentListInfo(
7127 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
7128 assert(ASTTemplArgList && "No ASTTemplArgList!");
7129 AddSourceLocation(Loc: ASTTemplArgList->LAngleLoc);
7130 AddSourceLocation(Loc: ASTTemplArgList->RAngleLoc);
7131 Record->push_back(Elt: ASTTemplArgList->NumTemplateArgs);
7132 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
7133 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
7134 AddTemplateArgumentLoc(Arg: TemplArgs[i]);
7135}
7136
7137void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
7138 Record->push_back(Elt: Set.size());
7139 for (ASTUnresolvedSet::const_iterator
7140 I = Set.begin(), E = Set.end(); I != E; ++I) {
7141 AddDeclRef(D: I.getDecl());
7142 Record->push_back(Elt: I.getAccess());
7143 }
7144}
7145
7146// FIXME: Move this out of the main ASTRecordWriter interface.
7147void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
7148 Record->push_back(Elt: Base.isVirtual());
7149 Record->push_back(Elt: Base.isBaseOfClass());
7150 Record->push_back(Elt: Base.getAccessSpecifierAsWritten());
7151 Record->push_back(Elt: Base.getInheritConstructors());
7152 AddTypeSourceInfo(TInfo: Base.getTypeSourceInfo());
7153 AddSourceRange(Range: Base.getSourceRange());
7154 AddSourceLocation(Loc: Base.isPackExpansion()? Base.getEllipsisLoc()
7155 : SourceLocation());
7156}
7157
7158static uint64_t EmitCXXBaseSpecifiers(ASTContext &Context, ASTWriter &W,
7159 ArrayRef<CXXBaseSpecifier> Bases) {
7160 ASTWriter::RecordData Record;
7161 ASTRecordWriter Writer(Context, W, Record);
7162 Writer.push_back(N: Bases.size());
7163
7164 for (auto &Base : Bases)
7165 Writer.AddCXXBaseSpecifier(Base);
7166
7167 return Writer.Emit(Code: serialization::DECL_CXX_BASE_SPECIFIERS);
7168}
7169
7170// FIXME: Move this out of the main ASTRecordWriter interface.
7171void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
7172 AddOffset(BitOffset: EmitCXXBaseSpecifiers(Context&: getASTContext(), W&: *Writer, Bases));
7173}
7174
7175static uint64_t
7176EmitCXXCtorInitializers(ASTContext &Context, ASTWriter &W,
7177 ArrayRef<CXXCtorInitializer *> CtorInits) {
7178 ASTWriter::RecordData Record;
7179 ASTRecordWriter Writer(Context, W, Record);
7180 Writer.push_back(N: CtorInits.size());
7181
7182 for (auto *Init : CtorInits) {
7183 if (Init->isBaseInitializer()) {
7184 Writer.push_back(N: CTOR_INITIALIZER_BASE);
7185 Writer.AddTypeSourceInfo(TInfo: Init->getTypeSourceInfo());
7186 Writer.push_back(N: Init->isBaseVirtual());
7187 } else if (Init->isDelegatingInitializer()) {
7188 Writer.push_back(N: CTOR_INITIALIZER_DELEGATING);
7189 Writer.AddTypeSourceInfo(TInfo: Init->getTypeSourceInfo());
7190 } else if (Init->isMemberInitializer()){
7191 Writer.push_back(N: CTOR_INITIALIZER_MEMBER);
7192 Writer.AddDeclRef(D: Init->getMember());
7193 } else {
7194 Writer.push_back(N: CTOR_INITIALIZER_INDIRECT_MEMBER);
7195 Writer.AddDeclRef(D: Init->getIndirectMember());
7196 }
7197
7198 Writer.AddSourceLocation(Loc: Init->getMemberLocation());
7199 Writer.AddStmt(S: Init->getInit());
7200 Writer.AddSourceLocation(Loc: Init->getLParenLoc());
7201 Writer.AddSourceLocation(Loc: Init->getRParenLoc());
7202 Writer.push_back(N: Init->isWritten());
7203 if (Init->isWritten())
7204 Writer.push_back(N: Init->getSourceOrder());
7205 }
7206
7207 return Writer.Emit(Code: serialization::DECL_CXX_CTOR_INITIALIZERS);
7208}
7209
7210// FIXME: Move this out of the main ASTRecordWriter interface.
7211void ASTRecordWriter::AddCXXCtorInitializers(
7212 ArrayRef<CXXCtorInitializer *> CtorInits) {
7213 AddOffset(BitOffset: EmitCXXCtorInitializers(Context&: getASTContext(), W&: *Writer, CtorInits));
7214}
7215
7216void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
7217 auto &Data = D->data();
7218
7219 Record->push_back(Elt: Data.IsLambda);
7220
7221 BitsPacker DefinitionBits;
7222
7223#define FIELD(Name, Width, Merge) \
7224 if (!DefinitionBits.canWriteNextNBits(Width)) { \
7225 Record->push_back(DefinitionBits); \
7226 DefinitionBits.reset(0); \
7227 } \
7228 DefinitionBits.addBits(Data.Name, Width);
7229
7230#include "clang/AST/CXXRecordDeclDefinitionBits.def"
7231#undef FIELD
7232
7233 Record->push_back(Elt: DefinitionBits);
7234
7235 // getODRHash will compute the ODRHash if it has not been previously
7236 // computed.
7237 Record->push_back(Elt: D->getODRHash());
7238
7239 bool ModulesCodegen =
7240 !D->isDependentType() &&
7241 D->getTemplateSpecializationKind() !=
7242 TSK_ExplicitInstantiationDeclaration &&
7243 (Writer->getLangOpts().ModulesDebugInfo || D->isInNamedModule());
7244 Record->push_back(Elt: ModulesCodegen);
7245 if (ModulesCodegen)
7246 Writer->AddDeclRef(D, Record&: Writer->ModularCodegenDecls);
7247
7248 // IsLambda bit is already saved.
7249
7250 AddUnresolvedSet(Set: Data.Conversions.get(C&: getASTContext()));
7251 Record->push_back(Elt: Data.ComputedVisibleConversions);
7252 if (Data.ComputedVisibleConversions)
7253 AddUnresolvedSet(Set: Data.VisibleConversions.get(C&: getASTContext()));
7254 // Data.Definition is the owning decl, no need to write it.
7255
7256 if (!Data.IsLambda) {
7257 Record->push_back(Elt: Data.NumBases);
7258 if (Data.NumBases > 0)
7259 AddCXXBaseSpecifiers(Bases: Data.bases());
7260
7261 // FIXME: Make VBases lazily computed when needed to avoid storing them.
7262 Record->push_back(Elt: Data.NumVBases);
7263 if (Data.NumVBases > 0)
7264 AddCXXBaseSpecifiers(Bases: Data.vbases());
7265
7266 AddDeclRef(D: D->getFirstFriend());
7267 } else {
7268 auto &Lambda = D->getLambdaData();
7269
7270 BitsPacker LambdaBits;
7271 LambdaBits.addBits(Value: Lambda.DependencyKind, /*Width=*/BitsWidth: 2);
7272 LambdaBits.addBit(Value: Lambda.IsGenericLambda);
7273 LambdaBits.addBits(Value: Lambda.CaptureDefault, /*Width=*/BitsWidth: 2);
7274 LambdaBits.addBits(Value: Lambda.NumCaptures, /*Width=*/BitsWidth: 15);
7275 LambdaBits.addBit(Value: Lambda.HasKnownInternalLinkage);
7276 Record->push_back(Elt: LambdaBits);
7277
7278 Record->push_back(Elt: Lambda.NumExplicitCaptures);
7279 Record->push_back(Elt: Lambda.ManglingNumber);
7280 Record->push_back(Elt: D->getDeviceLambdaManglingNumber());
7281 // The lambda context declaration and index within the context are provided
7282 // separately, so that they can be used for merging.
7283 AddTypeSourceInfo(TInfo: Lambda.MethodTyInfo);
7284 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
7285 const LambdaCapture &Capture = Lambda.Captures.front()[I];
7286 AddSourceLocation(Loc: Capture.getLocation());
7287
7288 BitsPacker CaptureBits;
7289 CaptureBits.addBit(Value: Capture.isImplicit());
7290 CaptureBits.addBits(Value: Capture.getCaptureKind(), /*Width=*/BitsWidth: 3);
7291 Record->push_back(Elt: CaptureBits);
7292
7293 switch (Capture.getCaptureKind()) {
7294 case LCK_StarThis:
7295 case LCK_This:
7296 case LCK_VLAType:
7297 break;
7298 case LCK_ByCopy:
7299 case LCK_ByRef:
7300 ValueDecl *Var =
7301 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
7302 AddDeclRef(D: Var);
7303 AddSourceLocation(Loc: Capture.isPackExpansion() ? Capture.getEllipsisLoc()
7304 : SourceLocation());
7305 break;
7306 }
7307 }
7308 }
7309}
7310
7311void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
7312 const Expr *Init = VD->getInit();
7313 if (!Init) {
7314 push_back(N: 0);
7315 return;
7316 }
7317
7318 uint64_t Val = 1;
7319 if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
7320 // This may trigger evaluation, so run it first
7321 if (VD->hasInitWithSideEffects())
7322 Val |= 16;
7323 assert(ES->CheckedForSideEffects);
7324 Val |= (ES->HasConstantInitialization ? 2 : 0);
7325 Val |= (ES->HasConstantDestruction ? 4 : 0);
7326 APValue *Evaluated = VD->getEvaluatedValue();
7327 // If the evaluated result is constant, emit it.
7328 if (Evaluated && (Evaluated->isInt() || Evaluated->isFloat()))
7329 Val |= 8;
7330 }
7331 push_back(N: Val);
7332 if (Val & 8) {
7333 AddAPValue(Value: *VD->getEvaluatedValue());
7334 }
7335
7336 writeStmtRef(S: Init);
7337}
7338
7339void ASTWriter::ReaderInitialized(ASTReader *Reader) {
7340 assert(Reader && "Cannot remove chain");
7341 assert((!Chain || Chain == Reader) && "Cannot replace chain");
7342 assert(FirstDeclID == NextDeclID &&
7343 FirstTypeID == NextTypeID &&
7344 FirstIdentID == NextIdentID &&
7345 FirstMacroID == NextMacroID &&
7346 FirstSubmoduleID == NextSubmoduleID &&
7347 FirstSelectorID == NextSelectorID &&
7348 "Setting chain after writing has started.");
7349
7350 Chain = Reader;
7351
7352 // Note, this will get called multiple times, once one the reader starts up
7353 // and again each time it's done reading a PCH or module.
7354 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
7355 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
7356 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
7357 NextMacroID = FirstMacroID;
7358 NextSelectorID = FirstSelectorID;
7359 NextSubmoduleID = FirstSubmoduleID;
7360}
7361
7362void ASTWriter::IdentifierRead(IdentifierID ID, IdentifierInfo *II) {
7363 // Don't reuse Type ID from external modules for named modules. See the
7364 // comments in WriteASTCore for details.
7365 if (isWritingStdCXXNamedModules())
7366 return;
7367
7368 IdentifierID &StoredID = IdentifierIDs[II];
7369 unsigned OriginalModuleFileIndex = StoredID >> 32;
7370
7371 // Always keep the local identifier ID. See \p TypeRead() for more
7372 // information.
7373 if (OriginalModuleFileIndex == 0 && StoredID)
7374 return;
7375
7376 // Otherwise, keep the highest ID since the module file comes later has
7377 // higher module file indexes.
7378 if (ID > StoredID)
7379 StoredID = ID;
7380}
7381
7382void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
7383 // Always keep the highest ID. See \p TypeRead() for more information.
7384 MacroID &StoredID = MacroIDs[MI];
7385 if (ID > StoredID)
7386 StoredID = ID;
7387}
7388
7389void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
7390 // Don't reuse Type ID from external modules for named modules. See the
7391 // comments in WriteASTCore for details.
7392 if (isWritingStdCXXNamedModules())
7393 return;
7394
7395 // Always take the type index that comes in later module files.
7396 // This copes with an interesting
7397 // case for chained AST writing where we schedule writing the type and then,
7398 // later, deserialize the type from another AST. In this case, we want to
7399 // keep the entry from a later module so that we can properly write it out to
7400 // the AST file.
7401 TypeIdx &StoredIdx = TypeIdxs[T];
7402
7403 // Ignore it if the type comes from the current being written module file.
7404 // Since the current module file being written logically has the highest
7405 // index.
7406 unsigned ModuleFileIndex = StoredIdx.getModuleFileIndex();
7407 if (ModuleFileIndex == 0 && StoredIdx.getValue())
7408 return;
7409
7410 // Otherwise, keep the highest ID since the module file comes later has
7411 // higher module file indexes.
7412 if (Idx.getModuleFileIndex() >= StoredIdx.getModuleFileIndex())
7413 StoredIdx = Idx;
7414}
7415
7416void ASTWriter::PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {
7417 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
7418 DeclIDs[D] = LocalDeclID(ID);
7419 PredefinedDecls.insert(Ptr: D);
7420}
7421
7422void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
7423 // Always keep the highest ID. See \p TypeRead() for more information.
7424 SelectorID &StoredID = SelectorIDs[S];
7425 if (ID > StoredID)
7426 StoredID = ID;
7427}
7428
7429void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
7430 MacroDefinitionRecord *MD) {
7431 assert(!MacroDefinitions.contains(MD));
7432 MacroDefinitions[MD] = ID;
7433}
7434
7435void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
7436 assert(!SubmoduleIDs.contains(Mod));
7437 SubmoduleIDs[Mod] = ID;
7438}
7439
7440void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
7441 if (Chain && Chain->isProcessingUpdateRecords()) return;
7442 assert(D->isCompleteDefinition());
7443 assert(!WritingAST && "Already writing the AST!");
7444 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) {
7445 // We are interested when a PCH decl is modified.
7446 if (RD->isFromASTFile()) {
7447 // A forward reference was mutated into a definition. Rewrite it.
7448 // FIXME: This happens during template instantiation, should we
7449 // have created a new definition decl instead ?
7450 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
7451 "completed a tag from another module but not by instantiation?");
7452 DeclUpdates[RD].push_back(
7453 Elt: DeclUpdate(DeclUpdateKind::CXXInstantiatedClassDefinition));
7454 }
7455 }
7456}
7457
7458static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
7459 if (D->isFromASTFile())
7460 return true;
7461
7462 // The predefined __va_list_tag struct is imported if we imported any decls.
7463 // FIXME: This is a gross hack.
7464 return D == D->getASTContext().getVaListTagDecl();
7465}
7466
7467void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
7468 if (Chain && Chain->isProcessingUpdateRecords()) return;
7469 assert(DC->isLookupContext() &&
7470 "Should not add lookup results to non-lookup contexts!");
7471
7472 // TU is handled elsewhere.
7473 if (isa<TranslationUnitDecl>(Val: DC))
7474 return;
7475
7476 // Namespaces are handled elsewhere, except for template instantiations of
7477 // FunctionTemplateDecls in namespaces. We are interested in cases where the
7478 // local instantiations are added to an imported context. Only happens when
7479 // adding ADL lookup candidates, for example templated friends.
7480 if (isa<NamespaceDecl>(Val: DC) && D->getFriendObjectKind() == Decl::FOK_None &&
7481 !isa<FunctionTemplateDecl>(Val: D))
7482 return;
7483
7484 // We're only interested in cases where a local declaration is added to an
7485 // imported context.
7486 if (D->isFromASTFile() || !isImportedDeclContext(Chain, D: cast<Decl>(Val: DC)))
7487 return;
7488
7489 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
7490 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
7491 assert(!WritingAST && "Already writing the AST!");
7492 if (UpdatedDeclContexts.insert(X: DC) && !cast<Decl>(Val: DC)->isFromASTFile()) {
7493 // We're adding a visible declaration to a predefined decl context. Ensure
7494 // that we write out all of its lookup results so we don't get a nasty
7495 // surprise when we try to emit its lookup table.
7496 llvm::append_range(C&: DeclsToEmitEvenIfUnreferenced, R: DC->decls());
7497 }
7498 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7499}
7500
7501void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
7502 if (Chain && Chain->isProcessingUpdateRecords()) return;
7503 assert(D->isImplicit());
7504
7505 // We're only interested in cases where a local declaration is added to an
7506 // imported context.
7507 if (D->isFromASTFile() || !isImportedDeclContext(Chain, D: RD))
7508 return;
7509
7510 if (!isa<CXXMethodDecl>(Val: D))
7511 return;
7512
7513 // A decl coming from PCH was modified.
7514 assert(RD->isCompleteDefinition());
7515 assert(!WritingAST && "Already writing the AST!");
7516 DeclUpdates[RD].push_back(
7517 Elt: DeclUpdate(DeclUpdateKind::CXXAddedImplicitMember, D));
7518}
7519
7520void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
7521 if (Chain && Chain->isProcessingUpdateRecords()) return;
7522 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
7523 if (!Chain) return;
7524 Chain->forEachImportedKeyDecl(D: FD, Visit: [&](const Decl *D) {
7525 // If we don't already know the exception specification for this redecl
7526 // chain, add an update record for it.
7527 if (isUnresolvedExceptionSpec(ESpecType: cast<FunctionDecl>(Val: D)
7528 ->getType()
7529 ->castAs<FunctionProtoType>()
7530 ->getExceptionSpecType()))
7531 DeclUpdates[D].push_back(Elt: DeclUpdateKind::CXXResolvedExceptionSpec);
7532 });
7533}
7534
7535void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
7536 if (Chain && Chain->isProcessingUpdateRecords()) return;
7537 assert(!WritingAST && "Already writing the AST!");
7538 if (!Chain) return;
7539 Chain->forEachImportedKeyDecl(D: FD, Visit: [&](const Decl *D) {
7540 DeclUpdates[D].push_back(
7541 Elt: DeclUpdate(DeclUpdateKind::CXXDeducedReturnType, ReturnType));
7542 });
7543}
7544
7545void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
7546 const FunctionDecl *Delete,
7547 Expr *ThisArg) {
7548 if (Chain && Chain->isProcessingUpdateRecords()) return;
7549 assert(!WritingAST && "Already writing the AST!");
7550 assert(Delete && "Not given an operator delete");
7551 if (!Chain) return;
7552 Chain->forEachImportedKeyDecl(D: DD, Visit: [&](const Decl *D) {
7553 DeclUpdates[D].push_back(
7554 Elt: DeclUpdate(DeclUpdateKind::CXXResolvedDtorDelete, Delete));
7555 });
7556}
7557
7558void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
7559 if (Chain && Chain->isProcessingUpdateRecords()) return;
7560 assert(!WritingAST && "Already writing the AST!");
7561 if (!D->isFromASTFile())
7562 return; // Declaration not imported from PCH.
7563
7564 // The function definition may not have a body due to parsing errors.
7565 if (!D->doesThisDeclarationHaveABody())
7566 return;
7567
7568 // Implicit function decl from a PCH was defined.
7569 DeclUpdates[D].push_back(
7570 Elt: DeclUpdate(DeclUpdateKind::CXXAddedFunctionDefinition));
7571}
7572
7573void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
7574 if (Chain && Chain->isProcessingUpdateRecords()) return;
7575 assert(!WritingAST && "Already writing the AST!");
7576 if (!D->isFromASTFile())
7577 return;
7578
7579 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::CXXAddedVarDefinition));
7580}
7581
7582void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
7583 if (Chain && Chain->isProcessingUpdateRecords()) return;
7584 assert(!WritingAST && "Already writing the AST!");
7585 if (!D->isFromASTFile())
7586 return;
7587
7588 // The function definition may not have a body due to parsing errors.
7589 if (!D->doesThisDeclarationHaveABody())
7590 return;
7591
7592 DeclUpdates[D].push_back(
7593 Elt: DeclUpdate(DeclUpdateKind::CXXAddedFunctionDefinition));
7594}
7595
7596void ASTWriter::InstantiationRequested(const ValueDecl *D) {
7597 if (Chain && Chain->isProcessingUpdateRecords()) return;
7598 assert(!WritingAST && "Already writing the AST!");
7599 if (!D->isFromASTFile())
7600 return;
7601
7602 // Since the actual instantiation is delayed, this really means that we need
7603 // to update the instantiation location.
7604 SourceLocation POI;
7605 if (auto *VD = dyn_cast<VarDecl>(Val: D))
7606 POI = VD->getPointOfInstantiation();
7607 else
7608 POI = cast<FunctionDecl>(Val: D)->getPointOfInstantiation();
7609 DeclUpdates[D].push_back(
7610 Elt: DeclUpdate(DeclUpdateKind::CXXPointOfInstantiation, POI));
7611}
7612
7613void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
7614 if (Chain && Chain->isProcessingUpdateRecords()) return;
7615 assert(!WritingAST && "Already writing the AST!");
7616 if (!D->isFromASTFile())
7617 return;
7618
7619 DeclUpdates[D].push_back(
7620 Elt: DeclUpdate(DeclUpdateKind::CXXInstantiatedDefaultArgument, D));
7621}
7622
7623void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
7624 assert(!WritingAST && "Already writing the AST!");
7625 if (!D->isFromASTFile())
7626 return;
7627
7628 DeclUpdates[D].push_back(
7629 Elt: DeclUpdate(DeclUpdateKind::CXXInstantiatedDefaultMemberInitializer, D));
7630}
7631
7632void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
7633 const ObjCInterfaceDecl *IFD) {
7634 if (Chain && Chain->isProcessingUpdateRecords()) return;
7635 assert(!WritingAST && "Already writing the AST!");
7636 if (!IFD->isFromASTFile())
7637 return; // Declaration not imported from PCH.
7638
7639 assert(IFD->getDefinition() && "Category on a class without a definition?");
7640 ObjCClassesWithCategories.insert(
7641 X: const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
7642}
7643
7644void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
7645 if (Chain && Chain->isProcessingUpdateRecords()) return;
7646 assert(!WritingAST && "Already writing the AST!");
7647
7648 // If there is *any* declaration of the entity that's not from an AST file,
7649 // we can skip writing the update record. We make sure that isUsed() triggers
7650 // completion of the redeclaration chain of the entity.
7651 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
7652 if (IsLocalDecl(D: Prev))
7653 return;
7654
7655 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::DeclMarkedUsed));
7656}
7657
7658void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
7659 if (Chain && Chain->isProcessingUpdateRecords()) return;
7660 assert(!WritingAST && "Already writing the AST!");
7661 if (!D->isFromASTFile())
7662 return;
7663
7664 DeclUpdates[D].push_back(
7665 Elt: DeclUpdate(DeclUpdateKind::DeclMarkedOpenMPThreadPrivate));
7666}
7667
7668void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
7669 if (Chain && Chain->isProcessingUpdateRecords()) return;
7670 assert(!WritingAST && "Already writing the AST!");
7671 if (!D->isFromASTFile())
7672 return;
7673
7674 DeclUpdates[D].push_back(
7675 Elt: DeclUpdate(DeclUpdateKind::DeclMarkedOpenMPAllocate, A));
7676}
7677
7678void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
7679 const Attr *Attr) {
7680 if (Chain && Chain->isProcessingUpdateRecords()) return;
7681 assert(!WritingAST && "Already writing the AST!");
7682 if (!D->isFromASTFile())
7683 return;
7684
7685 DeclUpdates[D].push_back(
7686 Elt: DeclUpdate(DeclUpdateKind::DeclMarkedOpenMPDeclareTarget, Attr));
7687}
7688
7689void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
7690 if (Chain && Chain->isProcessingUpdateRecords()) return;
7691 assert(!WritingAST && "Already writing the AST!");
7692 assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
7693 DeclUpdates[D].push_back(Elt: DeclUpdate(DeclUpdateKind::DeclExported, M));
7694}
7695
7696void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
7697 const RecordDecl *Record) {
7698 if (Chain && Chain->isProcessingUpdateRecords()) return;
7699 assert(!WritingAST && "Already writing the AST!");
7700 if (!Record->isFromASTFile())
7701 return;
7702 DeclUpdates[Record].push_back(
7703 Elt: DeclUpdate(DeclUpdateKind::AddedAttrToRecord, Attr));
7704}
7705
7706void ASTWriter::AddedCXXTemplateSpecialization(
7707 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
7708 assert(!WritingAST && "Already writing the AST!");
7709
7710 if (!TD->getFirstDecl()->isFromASTFile())
7711 return;
7712 if (Chain && Chain->isProcessingUpdateRecords())
7713 return;
7714
7715 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7716}
7717
7718void ASTWriter::AddedCXXTemplateSpecialization(
7719 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
7720 assert(!WritingAST && "Already writing the AST!");
7721
7722 if (!TD->getFirstDecl()->isFromASTFile())
7723 return;
7724 if (Chain && Chain->isProcessingUpdateRecords())
7725 return;
7726
7727 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7728}
7729
7730void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
7731 const FunctionDecl *D) {
7732 assert(!WritingAST && "Already writing the AST!");
7733
7734 if (!TD->getFirstDecl()->isFromASTFile())
7735 return;
7736 if (Chain && Chain->isProcessingUpdateRecords())
7737 return;
7738
7739 DeclsToEmitEvenIfUnreferenced.push_back(Elt: D);
7740}
7741
7742//===----------------------------------------------------------------------===//
7743//// OMPClause Serialization
7744////===----------------------------------------------------------------------===//
7745
7746namespace {
7747
7748class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
7749 ASTRecordWriter &Record;
7750
7751public:
7752 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
7753#define GEN_CLANG_CLAUSE_CLASS
7754#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
7755#include "llvm/Frontend/OpenMP/OMP.inc"
7756 void writeClause(OMPClause *C);
7757 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
7758 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
7759};
7760
7761}
7762
7763void ASTRecordWriter::writeOMPClause(OMPClause *C) {
7764 OMPClauseWriter(*this).writeClause(C);
7765}
7766
7767void OMPClauseWriter::writeClause(OMPClause *C) {
7768 Record.push_back(N: unsigned(C->getClauseKind()));
7769 Visit(S: C);
7770 Record.AddSourceLocation(Loc: C->getBeginLoc());
7771 Record.AddSourceLocation(Loc: C->getEndLoc());
7772}
7773
7774void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
7775 Record.push_back(N: uint64_t(C->getCaptureRegion()));
7776 Record.AddStmt(S: C->getPreInitStmt());
7777}
7778
7779void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
7780 VisitOMPClauseWithPreInit(C);
7781 Record.AddStmt(S: C->getPostUpdateExpr());
7782}
7783
7784void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
7785 VisitOMPClauseWithPreInit(C);
7786 Record.push_back(N: uint64_t(C->getNameModifier()));
7787 Record.AddSourceLocation(Loc: C->getNameModifierLoc());
7788 Record.AddSourceLocation(Loc: C->getColonLoc());
7789 Record.AddStmt(S: C->getCondition());
7790 Record.AddSourceLocation(Loc: C->getLParenLoc());
7791}
7792
7793void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
7794 VisitOMPClauseWithPreInit(C);
7795 Record.AddStmt(S: C->getCondition());
7796 Record.AddSourceLocation(Loc: C->getLParenLoc());
7797}
7798
7799void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
7800 VisitOMPClauseWithPreInit(C);
7801 Record.writeEnum(value: C->getModifier());
7802 Record.AddStmt(S: C->getNumThreads());
7803 Record.AddSourceLocation(Loc: C->getModifierLoc());
7804 Record.AddSourceLocation(Loc: C->getLParenLoc());
7805}
7806
7807void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
7808 Record.AddStmt(S: C->getSafelen());
7809 Record.AddSourceLocation(Loc: C->getLParenLoc());
7810}
7811
7812void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
7813 Record.AddStmt(S: C->getSimdlen());
7814 Record.AddSourceLocation(Loc: C->getLParenLoc());
7815}
7816
7817void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause *C) {
7818 Record.push_back(N: C->getNumSizes());
7819 for (Expr *Size : C->getSizesRefs())
7820 Record.AddStmt(S: Size);
7821 Record.AddSourceLocation(Loc: C->getLParenLoc());
7822}
7823
7824void OMPClauseWriter::VisitOMPPermutationClause(OMPPermutationClause *C) {
7825 Record.push_back(N: C->getNumLoops());
7826 for (Expr *Size : C->getArgsRefs())
7827 Record.AddStmt(S: Size);
7828 Record.AddSourceLocation(Loc: C->getLParenLoc());
7829}
7830
7831void OMPClauseWriter::VisitOMPFullClause(OMPFullClause *C) {}
7832
7833void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause *C) {
7834 Record.AddStmt(S: C->getFactor());
7835 Record.AddSourceLocation(Loc: C->getLParenLoc());
7836}
7837
7838void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
7839 Record.AddStmt(S: C->getAllocator());
7840 Record.AddSourceLocation(Loc: C->getLParenLoc());
7841}
7842
7843void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
7844 Record.AddStmt(S: C->getNumForLoops());
7845 Record.AddSourceLocation(Loc: C->getLParenLoc());
7846}
7847
7848void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
7849 Record.AddStmt(S: C->getEventHandler());
7850 Record.AddSourceLocation(Loc: C->getLParenLoc());
7851}
7852
7853void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
7854 Record.push_back(N: unsigned(C->getDefaultKind()));
7855 Record.AddSourceLocation(Loc: C->getLParenLoc());
7856 Record.AddSourceLocation(Loc: C->getDefaultKindKwLoc());
7857}
7858
7859void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
7860 Record.push_back(N: unsigned(C->getProcBindKind()));
7861 Record.AddSourceLocation(Loc: C->getLParenLoc());
7862 Record.AddSourceLocation(Loc: C->getProcBindKindKwLoc());
7863}
7864
7865void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
7866 VisitOMPClauseWithPreInit(C);
7867 Record.push_back(N: C->getScheduleKind());
7868 Record.push_back(N: C->getFirstScheduleModifier());
7869 Record.push_back(N: C->getSecondScheduleModifier());
7870 Record.AddStmt(S: C->getChunkSize());
7871 Record.AddSourceLocation(Loc: C->getLParenLoc());
7872 Record.AddSourceLocation(Loc: C->getFirstScheduleModifierLoc());
7873 Record.AddSourceLocation(Loc: C->getSecondScheduleModifierLoc());
7874 Record.AddSourceLocation(Loc: C->getScheduleKindLoc());
7875 Record.AddSourceLocation(Loc: C->getCommaLoc());
7876}
7877
7878void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
7879 Record.push_back(N: C->getLoopNumIterations().size());
7880 Record.AddStmt(S: C->getNumForLoops());
7881 for (Expr *NumIter : C->getLoopNumIterations())
7882 Record.AddStmt(S: NumIter);
7883 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
7884 Record.AddStmt(S: C->getLoopCounter(NumLoop: I));
7885 Record.AddSourceLocation(Loc: C->getLParenLoc());
7886}
7887
7888void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
7889
7890void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
7891
7892void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
7893
7894void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
7895
7896void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
7897
7898void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
7899 Record.push_back(N: C->isExtended() ? 1 : 0);
7900 if (C->isExtended()) {
7901 Record.AddSourceLocation(Loc: C->getLParenLoc());
7902 Record.AddSourceLocation(Loc: C->getArgumentLoc());
7903 Record.writeEnum(value: C->getDependencyKind());
7904 }
7905}
7906
7907void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
7908
7909void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause *) {}
7910
7911// Save the parameter of fail clause.
7912void OMPClauseWriter::VisitOMPFailClause(OMPFailClause *C) {
7913 Record.AddSourceLocation(Loc: C->getLParenLoc());
7914 Record.AddSourceLocation(Loc: C->getFailParameterLoc());
7915 Record.writeEnum(value: C->getFailParameter());
7916}
7917
7918void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
7919
7920void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
7921
7922void OMPClauseWriter::VisitOMPAbsentClause(OMPAbsentClause *C) {
7923 Record.push_back(N: static_cast<uint64_t>(C->getDirectiveKinds().size()));
7924 Record.AddSourceLocation(Loc: C->getLParenLoc());
7925 for (auto K : C->getDirectiveKinds()) {
7926 Record.writeEnum(value: K);
7927 }
7928}
7929
7930void OMPClauseWriter::VisitOMPHoldsClause(OMPHoldsClause *C) {
7931 Record.AddStmt(S: C->getExpr());
7932 Record.AddSourceLocation(Loc: C->getLParenLoc());
7933}
7934
7935void OMPClauseWriter::VisitOMPContainsClause(OMPContainsClause *C) {
7936 Record.push_back(N: static_cast<uint64_t>(C->getDirectiveKinds().size()));
7937 Record.AddSourceLocation(Loc: C->getLParenLoc());
7938 for (auto K : C->getDirectiveKinds()) {
7939 Record.writeEnum(value: K);
7940 }
7941}
7942
7943void OMPClauseWriter::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
7944
7945void OMPClauseWriter::VisitOMPNoOpenMPRoutinesClause(
7946 OMPNoOpenMPRoutinesClause *) {}
7947
7948void OMPClauseWriter::VisitOMPNoOpenMPConstructsClause(
7949 OMPNoOpenMPConstructsClause *) {}
7950
7951void OMPClauseWriter::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
7952
7953void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
7954
7955void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
7956
7957void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
7958
7959void OMPClauseWriter::VisitOMPWeakClause(OMPWeakClause *) {}
7960
7961void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
7962
7963void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
7964
7965void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
7966
7967void OMPClauseWriter::VisitOMPInitClause(OMPInitClause *C) {
7968 Record.push_back(N: C->varlist_size());
7969 for (Expr *VE : C->varlist())
7970 Record.AddStmt(S: VE);
7971 Record.writeBool(Value: C->getIsTarget());
7972 Record.writeBool(Value: C->getIsTargetSync());
7973 Record.AddSourceLocation(Loc: C->getLParenLoc());
7974 Record.AddSourceLocation(Loc: C->getVarLoc());
7975}
7976
7977void OMPClauseWriter::VisitOMPUseClause(OMPUseClause *C) {
7978 Record.AddStmt(S: C->getInteropVar());
7979 Record.AddSourceLocation(Loc: C->getLParenLoc());
7980 Record.AddSourceLocation(Loc: C->getVarLoc());
7981}
7982
7983void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *C) {
7984 Record.AddStmt(S: C->getInteropVar());
7985 Record.AddSourceLocation(Loc: C->getLParenLoc());
7986 Record.AddSourceLocation(Loc: C->getVarLoc());
7987}
7988
7989void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
7990 VisitOMPClauseWithPreInit(C);
7991 Record.AddStmt(S: C->getCondition());
7992 Record.AddSourceLocation(Loc: C->getLParenLoc());
7993}
7994
7995void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause *C) {
7996 VisitOMPClauseWithPreInit(C);
7997 Record.AddStmt(S: C->getCondition());
7998 Record.AddSourceLocation(Loc: C->getLParenLoc());
7999}
8000
8001void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause *C) {
8002 VisitOMPClauseWithPreInit(C);
8003 Record.AddStmt(S: C->getThreadID());
8004 Record.AddSourceLocation(Loc: C->getLParenLoc());
8005}
8006
8007void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause *C) {
8008 Record.AddStmt(S: C->getAlignment());
8009 Record.AddSourceLocation(Loc: C->getLParenLoc());
8010}
8011
8012void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
8013 Record.push_back(N: C->varlist_size());
8014 Record.AddSourceLocation(Loc: C->getLParenLoc());
8015 for (auto *VE : C->varlist()) {
8016 Record.AddStmt(S: VE);
8017 }
8018 for (auto *VE : C->private_copies()) {
8019 Record.AddStmt(S: VE);
8020 }
8021}
8022
8023void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
8024 Record.push_back(N: C->varlist_size());
8025 VisitOMPClauseWithPreInit(C);
8026 Record.AddSourceLocation(Loc: C->getLParenLoc());
8027 for (auto *VE : C->varlist()) {
8028 Record.AddStmt(S: VE);
8029 }
8030 for (auto *VE : C->private_copies()) {
8031 Record.AddStmt(S: VE);
8032 }
8033 for (auto *VE : C->inits()) {
8034 Record.AddStmt(S: VE);
8035 }
8036}
8037
8038void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
8039 Record.push_back(N: C->varlist_size());
8040 VisitOMPClauseWithPostUpdate(C);
8041 Record.AddSourceLocation(Loc: C->getLParenLoc());
8042 Record.writeEnum(value: C->getKind());
8043 Record.AddSourceLocation(Loc: C->getKindLoc());
8044 Record.AddSourceLocation(Loc: C->getColonLoc());
8045 for (auto *VE : C->varlist())
8046 Record.AddStmt(S: VE);
8047 for (auto *E : C->private_copies())
8048 Record.AddStmt(S: E);
8049 for (auto *E : C->source_exprs())
8050 Record.AddStmt(S: E);
8051 for (auto *E : C->destination_exprs())
8052 Record.AddStmt(S: E);
8053 for (auto *E : C->assignment_ops())
8054 Record.AddStmt(S: E);
8055}
8056
8057void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
8058 Record.push_back(N: C->varlist_size());
8059 Record.AddSourceLocation(Loc: C->getLParenLoc());
8060 for (auto *VE : C->varlist())
8061 Record.AddStmt(S: VE);
8062}
8063
8064void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
8065 Record.push_back(N: C->varlist_size());
8066 Record.writeEnum(value: C->getModifier());
8067 VisitOMPClauseWithPostUpdate(C);
8068 Record.AddSourceLocation(Loc: C->getLParenLoc());
8069 Record.AddSourceLocation(Loc: C->getModifierLoc());
8070 Record.AddSourceLocation(Loc: C->getColonLoc());
8071 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
8072 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
8073 for (auto *VE : C->varlist())
8074 Record.AddStmt(S: VE);
8075 for (auto *VE : C->privates())
8076 Record.AddStmt(S: VE);
8077 for (auto *E : C->lhs_exprs())
8078 Record.AddStmt(S: E);
8079 for (auto *E : C->rhs_exprs())
8080 Record.AddStmt(S: E);
8081 for (auto *E : C->reduction_ops())
8082 Record.AddStmt(S: E);
8083 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
8084 for (auto *E : C->copy_ops())
8085 Record.AddStmt(S: E);
8086 for (auto *E : C->copy_array_temps())
8087 Record.AddStmt(S: E);
8088 for (auto *E : C->copy_array_elems())
8089 Record.AddStmt(S: E);
8090 }
8091 auto PrivateFlags = C->private_var_reduction_flags();
8092 Record.push_back(N: std::distance(first: PrivateFlags.begin(), last: PrivateFlags.end()));
8093 for (bool Flag : PrivateFlags)
8094 Record.push_back(N: Flag);
8095}
8096
8097void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
8098 Record.push_back(N: C->varlist_size());
8099 VisitOMPClauseWithPostUpdate(C);
8100 Record.AddSourceLocation(Loc: C->getLParenLoc());
8101 Record.AddSourceLocation(Loc: C->getColonLoc());
8102 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
8103 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
8104 for (auto *VE : C->varlist())
8105 Record.AddStmt(S: VE);
8106 for (auto *VE : C->privates())
8107 Record.AddStmt(S: VE);
8108 for (auto *E : C->lhs_exprs())
8109 Record.AddStmt(S: E);
8110 for (auto *E : C->rhs_exprs())
8111 Record.AddStmt(S: E);
8112 for (auto *E : C->reduction_ops())
8113 Record.AddStmt(S: E);
8114}
8115
8116void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
8117 Record.push_back(N: C->varlist_size());
8118 VisitOMPClauseWithPostUpdate(C);
8119 Record.AddSourceLocation(Loc: C->getLParenLoc());
8120 Record.AddSourceLocation(Loc: C->getColonLoc());
8121 Record.AddNestedNameSpecifierLoc(NNS: C->getQualifierLoc());
8122 Record.AddDeclarationNameInfo(NameInfo: C->getNameInfo());
8123 for (auto *VE : C->varlist())
8124 Record.AddStmt(S: VE);
8125 for (auto *VE : C->privates())
8126 Record.AddStmt(S: VE);
8127 for (auto *E : C->lhs_exprs())
8128 Record.AddStmt(S: E);
8129 for (auto *E : C->rhs_exprs())
8130 Record.AddStmt(S: E);
8131 for (auto *E : C->reduction_ops())
8132 Record.AddStmt(S: E);
8133 for (auto *E : C->taskgroup_descriptors())
8134 Record.AddStmt(S: E);
8135}
8136
8137void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
8138 Record.push_back(N: C->varlist_size());
8139 VisitOMPClauseWithPostUpdate(C);
8140 Record.AddSourceLocation(Loc: C->getLParenLoc());
8141 Record.AddSourceLocation(Loc: C->getColonLoc());
8142 Record.push_back(N: C->getModifier());
8143 Record.AddSourceLocation(Loc: C->getModifierLoc());
8144 for (auto *VE : C->varlist()) {
8145 Record.AddStmt(S: VE);
8146 }
8147 for (auto *VE : C->privates()) {
8148 Record.AddStmt(S: VE);
8149 }
8150 for (auto *VE : C->inits()) {
8151 Record.AddStmt(S: VE);
8152 }
8153 for (auto *VE : C->updates()) {
8154 Record.AddStmt(S: VE);
8155 }
8156 for (auto *VE : C->finals()) {
8157 Record.AddStmt(S: VE);
8158 }
8159 Record.AddStmt(S: C->getStep());
8160 Record.AddStmt(S: C->getCalcStep());
8161 for (auto *VE : C->used_expressions())
8162 Record.AddStmt(S: VE);
8163}
8164
8165void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
8166 Record.push_back(N: C->varlist_size());
8167 Record.AddSourceLocation(Loc: C->getLParenLoc());
8168 Record.AddSourceLocation(Loc: C->getColonLoc());
8169 for (auto *VE : C->varlist())
8170 Record.AddStmt(S: VE);
8171 Record.AddStmt(S: C->getAlignment());
8172}
8173
8174void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
8175 Record.push_back(N: C->varlist_size());
8176 Record.AddSourceLocation(Loc: C->getLParenLoc());
8177 for (auto *VE : C->varlist())
8178 Record.AddStmt(S: VE);
8179 for (auto *E : C->source_exprs())
8180 Record.AddStmt(S: E);
8181 for (auto *E : C->destination_exprs())
8182 Record.AddStmt(S: E);
8183 for (auto *E : C->assignment_ops())
8184 Record.AddStmt(S: E);
8185}
8186
8187void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
8188 Record.push_back(N: C->varlist_size());
8189 Record.AddSourceLocation(Loc: C->getLParenLoc());
8190 for (auto *VE : C->varlist())
8191 Record.AddStmt(S: VE);
8192 for (auto *E : C->source_exprs())
8193 Record.AddStmt(S: E);
8194 for (auto *E : C->destination_exprs())
8195 Record.AddStmt(S: E);
8196 for (auto *E : C->assignment_ops())
8197 Record.AddStmt(S: E);
8198}
8199
8200void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
8201 Record.push_back(N: C->varlist_size());
8202 Record.AddSourceLocation(Loc: C->getLParenLoc());
8203 for (auto *VE : C->varlist())
8204 Record.AddStmt(S: VE);
8205}
8206
8207void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
8208 Record.AddStmt(S: C->getDepobj());
8209 Record.AddSourceLocation(Loc: C->getLParenLoc());
8210}
8211
8212void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
8213 Record.push_back(N: C->varlist_size());
8214 Record.push_back(N: C->getNumLoops());
8215 Record.AddSourceLocation(Loc: C->getLParenLoc());
8216 Record.AddStmt(S: C->getModifier());
8217 Record.push_back(N: C->getDependencyKind());
8218 Record.AddSourceLocation(Loc: C->getDependencyLoc());
8219 Record.AddSourceLocation(Loc: C->getColonLoc());
8220 Record.AddSourceLocation(Loc: C->getOmpAllMemoryLoc());
8221 for (auto *VE : C->varlist())
8222 Record.AddStmt(S: VE);
8223 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
8224 Record.AddStmt(S: C->getLoopData(NumLoop: I));
8225}
8226
8227void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
8228 VisitOMPClauseWithPreInit(C);
8229 Record.writeEnum(value: C->getModifier());
8230 Record.AddStmt(S: C->getDevice());
8231 Record.AddSourceLocation(Loc: C->getModifierLoc());
8232 Record.AddSourceLocation(Loc: C->getLParenLoc());
8233}
8234
8235void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
8236 Record.push_back(N: C->varlist_size());
8237 Record.push_back(N: C->getUniqueDeclarationsNum());
8238 Record.push_back(N: C->getTotalComponentListNum());
8239 Record.push_back(N: C->getTotalComponentsNum());
8240 Record.AddSourceLocation(Loc: C->getLParenLoc());
8241 bool HasIteratorModifier = false;
8242 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
8243 Record.push_back(N: C->getMapTypeModifier(Cnt: I));
8244 Record.AddSourceLocation(Loc: C->getMapTypeModifierLoc(Cnt: I));
8245 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
8246 HasIteratorModifier = true;
8247 }
8248 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
8249 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
8250 Record.push_back(N: C->getMapType());
8251 Record.AddSourceLocation(Loc: C->getMapLoc());
8252 Record.AddSourceLocation(Loc: C->getColonLoc());
8253 for (auto *E : C->varlist())
8254 Record.AddStmt(S: E);
8255 for (auto *E : C->mapperlists())
8256 Record.AddStmt(S: E);
8257 if (HasIteratorModifier)
8258 Record.AddStmt(S: C->getIteratorModifier());
8259 for (auto *D : C->all_decls())
8260 Record.AddDeclRef(D);
8261 for (auto N : C->all_num_lists())
8262 Record.push_back(N);
8263 for (auto N : C->all_lists_sizes())
8264 Record.push_back(N);
8265 for (auto &M : C->all_components()) {
8266 Record.AddStmt(S: M.getAssociatedExpression());
8267 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8268 }
8269}
8270
8271void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
8272 Record.push_back(N: C->varlist_size());
8273 Record.writeEnum(value: C->getFirstAllocateModifier());
8274 Record.writeEnum(value: C->getSecondAllocateModifier());
8275 Record.AddSourceLocation(Loc: C->getLParenLoc());
8276 Record.AddSourceLocation(Loc: C->getColonLoc());
8277 Record.AddStmt(S: C->getAllocator());
8278 Record.AddStmt(S: C->getAlignment());
8279 for (auto *VE : C->varlist())
8280 Record.AddStmt(S: VE);
8281}
8282
8283void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
8284 Record.push_back(N: C->varlist_size());
8285 VisitOMPClauseWithPreInit(C);
8286 Record.AddSourceLocation(Loc: C->getLParenLoc());
8287 for (auto *VE : C->varlist())
8288 Record.AddStmt(S: VE);
8289}
8290
8291void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
8292 Record.push_back(N: C->varlist_size());
8293 VisitOMPClauseWithPreInit(C);
8294 Record.AddSourceLocation(Loc: C->getLParenLoc());
8295 for (auto *VE : C->varlist())
8296 Record.AddStmt(S: VE);
8297}
8298
8299void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
8300 VisitOMPClauseWithPreInit(C);
8301 Record.AddStmt(S: C->getPriority());
8302 Record.AddSourceLocation(Loc: C->getLParenLoc());
8303}
8304
8305void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
8306 VisitOMPClauseWithPreInit(C);
8307 Record.writeEnum(value: C->getModifier());
8308 Record.AddStmt(S: C->getGrainsize());
8309 Record.AddSourceLocation(Loc: C->getModifierLoc());
8310 Record.AddSourceLocation(Loc: C->getLParenLoc());
8311}
8312
8313void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
8314 VisitOMPClauseWithPreInit(C);
8315 Record.writeEnum(value: C->getModifier());
8316 Record.AddStmt(S: C->getNumTasks());
8317 Record.AddSourceLocation(Loc: C->getModifierLoc());
8318 Record.AddSourceLocation(Loc: C->getLParenLoc());
8319}
8320
8321void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
8322 Record.AddStmt(S: C->getHint());
8323 Record.AddSourceLocation(Loc: C->getLParenLoc());
8324}
8325
8326void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
8327 VisitOMPClauseWithPreInit(C);
8328 Record.push_back(N: C->getDistScheduleKind());
8329 Record.AddStmt(S: C->getChunkSize());
8330 Record.AddSourceLocation(Loc: C->getLParenLoc());
8331 Record.AddSourceLocation(Loc: C->getDistScheduleKindLoc());
8332 Record.AddSourceLocation(Loc: C->getCommaLoc());
8333}
8334
8335void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
8336 Record.push_back(N: C->getDefaultmapKind());
8337 Record.push_back(N: C->getDefaultmapModifier());
8338 Record.AddSourceLocation(Loc: C->getLParenLoc());
8339 Record.AddSourceLocation(Loc: C->getDefaultmapModifierLoc());
8340 Record.AddSourceLocation(Loc: C->getDefaultmapKindLoc());
8341}
8342
8343void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
8344 Record.push_back(N: C->varlist_size());
8345 Record.push_back(N: C->getUniqueDeclarationsNum());
8346 Record.push_back(N: C->getTotalComponentListNum());
8347 Record.push_back(N: C->getTotalComponentsNum());
8348 Record.AddSourceLocation(Loc: C->getLParenLoc());
8349 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
8350 Record.push_back(N: C->getMotionModifier(Cnt: I));
8351 Record.AddSourceLocation(Loc: C->getMotionModifierLoc(Cnt: I));
8352 }
8353 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
8354 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
8355 Record.AddSourceLocation(Loc: C->getColonLoc());
8356 for (auto *E : C->varlist())
8357 Record.AddStmt(S: E);
8358 for (auto *E : C->mapperlists())
8359 Record.AddStmt(S: E);
8360 for (auto *D : C->all_decls())
8361 Record.AddDeclRef(D);
8362 for (auto N : C->all_num_lists())
8363 Record.push_back(N);
8364 for (auto N : C->all_lists_sizes())
8365 Record.push_back(N);
8366 for (auto &M : C->all_components()) {
8367 Record.AddStmt(S: M.getAssociatedExpression());
8368 Record.writeBool(Value: M.isNonContiguous());
8369 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8370 }
8371}
8372
8373void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
8374 Record.push_back(N: C->varlist_size());
8375 Record.push_back(N: C->getUniqueDeclarationsNum());
8376 Record.push_back(N: C->getTotalComponentListNum());
8377 Record.push_back(N: C->getTotalComponentsNum());
8378 Record.AddSourceLocation(Loc: C->getLParenLoc());
8379 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
8380 Record.push_back(N: C->getMotionModifier(Cnt: I));
8381 Record.AddSourceLocation(Loc: C->getMotionModifierLoc(Cnt: I));
8382 }
8383 Record.AddNestedNameSpecifierLoc(NNS: C->getMapperQualifierLoc());
8384 Record.AddDeclarationNameInfo(NameInfo: C->getMapperIdInfo());
8385 Record.AddSourceLocation(Loc: C->getColonLoc());
8386 for (auto *E : C->varlist())
8387 Record.AddStmt(S: E);
8388 for (auto *E : C->mapperlists())
8389 Record.AddStmt(S: E);
8390 for (auto *D : C->all_decls())
8391 Record.AddDeclRef(D);
8392 for (auto N : C->all_num_lists())
8393 Record.push_back(N);
8394 for (auto N : C->all_lists_sizes())
8395 Record.push_back(N);
8396 for (auto &M : C->all_components()) {
8397 Record.AddStmt(S: M.getAssociatedExpression());
8398 Record.writeBool(Value: M.isNonContiguous());
8399 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8400 }
8401}
8402
8403void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
8404 Record.push_back(N: C->varlist_size());
8405 Record.push_back(N: C->getUniqueDeclarationsNum());
8406 Record.push_back(N: C->getTotalComponentListNum());
8407 Record.push_back(N: C->getTotalComponentsNum());
8408 Record.AddSourceLocation(Loc: C->getLParenLoc());
8409 for (auto *E : C->varlist())
8410 Record.AddStmt(S: E);
8411 for (auto *VE : C->private_copies())
8412 Record.AddStmt(S: VE);
8413 for (auto *VE : C->inits())
8414 Record.AddStmt(S: VE);
8415 for (auto *D : C->all_decls())
8416 Record.AddDeclRef(D);
8417 for (auto N : C->all_num_lists())
8418 Record.push_back(N);
8419 for (auto N : C->all_lists_sizes())
8420 Record.push_back(N);
8421 for (auto &M : C->all_components()) {
8422 Record.AddStmt(S: M.getAssociatedExpression());
8423 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8424 }
8425}
8426
8427void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
8428 Record.push_back(N: C->varlist_size());
8429 Record.push_back(N: C->getUniqueDeclarationsNum());
8430 Record.push_back(N: C->getTotalComponentListNum());
8431 Record.push_back(N: C->getTotalComponentsNum());
8432 Record.AddSourceLocation(Loc: C->getLParenLoc());
8433 for (auto *E : C->varlist())
8434 Record.AddStmt(S: E);
8435 for (auto *D : C->all_decls())
8436 Record.AddDeclRef(D);
8437 for (auto N : C->all_num_lists())
8438 Record.push_back(N);
8439 for (auto N : C->all_lists_sizes())
8440 Record.push_back(N);
8441 for (auto &M : C->all_components()) {
8442 Record.AddStmt(S: M.getAssociatedExpression());
8443 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8444 }
8445}
8446
8447void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
8448 Record.push_back(N: C->varlist_size());
8449 Record.push_back(N: C->getUniqueDeclarationsNum());
8450 Record.push_back(N: C->getTotalComponentListNum());
8451 Record.push_back(N: C->getTotalComponentsNum());
8452 Record.AddSourceLocation(Loc: C->getLParenLoc());
8453 for (auto *E : C->varlist())
8454 Record.AddStmt(S: E);
8455 for (auto *D : C->all_decls())
8456 Record.AddDeclRef(D);
8457 for (auto N : C->all_num_lists())
8458 Record.push_back(N);
8459 for (auto N : C->all_lists_sizes())
8460 Record.push_back(N);
8461 for (auto &M : C->all_components()) {
8462 Record.AddStmt(S: M.getAssociatedExpression());
8463 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8464 }
8465}
8466
8467void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
8468 Record.push_back(N: C->varlist_size());
8469 Record.push_back(N: C->getUniqueDeclarationsNum());
8470 Record.push_back(N: C->getTotalComponentListNum());
8471 Record.push_back(N: C->getTotalComponentsNum());
8472 Record.AddSourceLocation(Loc: C->getLParenLoc());
8473 for (auto *E : C->varlist())
8474 Record.AddStmt(S: E);
8475 for (auto *D : C->all_decls())
8476 Record.AddDeclRef(D);
8477 for (auto N : C->all_num_lists())
8478 Record.push_back(N);
8479 for (auto N : C->all_lists_sizes())
8480 Record.push_back(N);
8481 for (auto &M : C->all_components()) {
8482 Record.AddStmt(S: M.getAssociatedExpression());
8483 Record.AddDeclRef(D: M.getAssociatedDeclaration());
8484 }
8485}
8486
8487void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
8488
8489void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
8490 OMPUnifiedSharedMemoryClause *) {}
8491
8492void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
8493
8494void
8495OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
8496}
8497
8498void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
8499 OMPAtomicDefaultMemOrderClause *C) {
8500 Record.push_back(N: C->getAtomicDefaultMemOrderKind());
8501 Record.AddSourceLocation(Loc: C->getLParenLoc());
8502 Record.AddSourceLocation(Loc: C->getAtomicDefaultMemOrderKindKwLoc());
8503}
8504
8505void OMPClauseWriter::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
8506
8507void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
8508 Record.push_back(N: C->getAtKind());
8509 Record.AddSourceLocation(Loc: C->getLParenLoc());
8510 Record.AddSourceLocation(Loc: C->getAtKindKwLoc());
8511}
8512
8513void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
8514 Record.push_back(N: C->getSeverityKind());
8515 Record.AddSourceLocation(Loc: C->getLParenLoc());
8516 Record.AddSourceLocation(Loc: C->getSeverityKindKwLoc());
8517}
8518
8519void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
8520 Record.AddStmt(S: C->getMessageString());
8521 Record.AddSourceLocation(Loc: C->getLParenLoc());
8522}
8523
8524void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
8525 Record.push_back(N: C->varlist_size());
8526 Record.AddSourceLocation(Loc: C->getLParenLoc());
8527 for (auto *VE : C->varlist())
8528 Record.AddStmt(S: VE);
8529 for (auto *E : C->private_refs())
8530 Record.AddStmt(S: E);
8531}
8532
8533void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
8534 Record.push_back(N: C->varlist_size());
8535 Record.AddSourceLocation(Loc: C->getLParenLoc());
8536 for (auto *VE : C->varlist())
8537 Record.AddStmt(S: VE);
8538}
8539
8540void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
8541 Record.push_back(N: C->varlist_size());
8542 Record.AddSourceLocation(Loc: C->getLParenLoc());
8543 for (auto *VE : C->varlist())
8544 Record.AddStmt(S: VE);
8545}
8546
8547void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
8548 Record.writeEnum(value: C->getKind());
8549 Record.writeEnum(value: C->getModifier());
8550 Record.AddSourceLocation(Loc: C->getLParenLoc());
8551 Record.AddSourceLocation(Loc: C->getKindKwLoc());
8552 Record.AddSourceLocation(Loc: C->getModifierKwLoc());
8553}
8554
8555void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
8556 Record.push_back(N: C->getNumberOfAllocators());
8557 Record.AddSourceLocation(Loc: C->getLParenLoc());
8558 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
8559 OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
8560 Record.AddStmt(S: Data.Allocator);
8561 Record.AddStmt(S: Data.AllocatorTraits);
8562 Record.AddSourceLocation(Loc: Data.LParenLoc);
8563 Record.AddSourceLocation(Loc: Data.RParenLoc);
8564 }
8565}
8566
8567void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
8568 Record.push_back(N: C->varlist_size());
8569 Record.AddSourceLocation(Loc: C->getLParenLoc());
8570 Record.AddStmt(S: C->getModifier());
8571 Record.AddSourceLocation(Loc: C->getColonLoc());
8572 for (Expr *E : C->varlist())
8573 Record.AddStmt(S: E);
8574}
8575
8576void OMPClauseWriter::VisitOMPBindClause(OMPBindClause *C) {
8577 Record.writeEnum(value: C->getBindKind());
8578 Record.AddSourceLocation(Loc: C->getLParenLoc());
8579 Record.AddSourceLocation(Loc: C->getBindKindLoc());
8580}
8581
8582void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
8583 VisitOMPClauseWithPreInit(C);
8584 Record.AddStmt(S: C->getSize());
8585 Record.AddSourceLocation(Loc: C->getLParenLoc());
8586}
8587
8588void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
8589 Record.push_back(N: C->varlist_size());
8590 Record.push_back(N: C->getNumLoops());
8591 Record.AddSourceLocation(Loc: C->getLParenLoc());
8592 Record.push_back(N: C->getDependenceType());
8593 Record.AddSourceLocation(Loc: C->getDependenceLoc());
8594 Record.AddSourceLocation(Loc: C->getColonLoc());
8595 for (auto *VE : C->varlist())
8596 Record.AddStmt(S: VE);
8597 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
8598 Record.AddStmt(S: C->getLoopData(NumLoop: I));
8599}
8600
8601void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
8602 Record.AddAttributes(Attrs: C->getAttrs());
8603 Record.AddSourceLocation(Loc: C->getBeginLoc());
8604 Record.AddSourceLocation(Loc: C->getLParenLoc());
8605 Record.AddSourceLocation(Loc: C->getEndLoc());
8606}
8607
8608void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause *C) {}
8609
8610void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
8611 writeUInt32(Value: TI->Sets.size());
8612 for (const auto &Set : TI->Sets) {
8613 writeEnum(value: Set.Kind);
8614 writeUInt32(Value: Set.Selectors.size());
8615 for (const auto &Selector : Set.Selectors) {
8616 writeEnum(value: Selector.Kind);
8617 writeBool(Value: Selector.ScoreOrCondition);
8618 if (Selector.ScoreOrCondition)
8619 writeExprRef(value: Selector.ScoreOrCondition);
8620 writeUInt32(Value: Selector.Properties.size());
8621 for (const auto &Property : Selector.Properties)
8622 writeEnum(value: Property.Kind);
8623 }
8624 }
8625}
8626
8627void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
8628 if (!Data)
8629 return;
8630 writeUInt32(Value: Data->getNumClauses());
8631 writeUInt32(Value: Data->getNumChildren());
8632 writeBool(Value: Data->hasAssociatedStmt());
8633 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
8634 writeOMPClause(C: Data->getClauses()[I]);
8635 if (Data->hasAssociatedStmt())
8636 AddStmt(S: Data->getAssociatedStmt());
8637 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
8638 AddStmt(S: Data->getChildren()[I]);
8639}
8640
8641void ASTRecordWriter::writeOpenACCVarList(const OpenACCClauseWithVarList *C) {
8642 writeUInt32(Value: C->getVarList().size());
8643 for (Expr *E : C->getVarList())
8644 AddStmt(S: E);
8645}
8646
8647void ASTRecordWriter::writeOpenACCIntExprList(ArrayRef<Expr *> Exprs) {
8648 writeUInt32(Value: Exprs.size());
8649 for (Expr *E : Exprs)
8650 AddStmt(S: E);
8651}
8652
8653void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
8654 writeEnum(value: C->getClauseKind());
8655 writeSourceLocation(Loc: C->getBeginLoc());
8656 writeSourceLocation(Loc: C->getEndLoc());
8657
8658 switch (C->getClauseKind()) {
8659 case OpenACCClauseKind::Default: {
8660 const auto *DC = cast<OpenACCDefaultClause>(Val: C);
8661 writeSourceLocation(Loc: DC->getLParenLoc());
8662 writeEnum(value: DC->getDefaultClauseKind());
8663 return;
8664 }
8665 case OpenACCClauseKind::If: {
8666 const auto *IC = cast<OpenACCIfClause>(Val: C);
8667 writeSourceLocation(Loc: IC->getLParenLoc());
8668 AddStmt(S: const_cast<Expr*>(IC->getConditionExpr()));
8669 return;
8670 }
8671 case OpenACCClauseKind::Self: {
8672 const auto *SC = cast<OpenACCSelfClause>(Val: C);
8673 writeSourceLocation(Loc: SC->getLParenLoc());
8674 writeBool(Value: SC->isConditionExprClause());
8675 if (SC->isConditionExprClause()) {
8676 writeBool(Value: SC->hasConditionExpr());
8677 if (SC->hasConditionExpr())
8678 AddStmt(S: const_cast<Expr *>(SC->getConditionExpr()));
8679 } else {
8680 writeUInt32(Value: SC->getVarList().size());
8681 for (Expr *E : SC->getVarList())
8682 AddStmt(S: E);
8683 }
8684 return;
8685 }
8686 case OpenACCClauseKind::NumGangs: {
8687 const auto *NGC = cast<OpenACCNumGangsClause>(Val: C);
8688 writeSourceLocation(Loc: NGC->getLParenLoc());
8689 writeUInt32(Value: NGC->getIntExprs().size());
8690 for (Expr *E : NGC->getIntExprs())
8691 AddStmt(S: E);
8692 return;
8693 }
8694 case OpenACCClauseKind::DeviceNum: {
8695 const auto *DNC = cast<OpenACCDeviceNumClause>(Val: C);
8696 writeSourceLocation(Loc: DNC->getLParenLoc());
8697 AddStmt(S: const_cast<Expr*>(DNC->getIntExpr()));
8698 return;
8699 }
8700 case OpenACCClauseKind::DefaultAsync: {
8701 const auto *DAC = cast<OpenACCDefaultAsyncClause>(Val: C);
8702 writeSourceLocation(Loc: DAC->getLParenLoc());
8703 AddStmt(S: const_cast<Expr *>(DAC->getIntExpr()));
8704 return;
8705 }
8706 case OpenACCClauseKind::NumWorkers: {
8707 const auto *NWC = cast<OpenACCNumWorkersClause>(Val: C);
8708 writeSourceLocation(Loc: NWC->getLParenLoc());
8709 AddStmt(S: const_cast<Expr*>(NWC->getIntExpr()));
8710 return;
8711 }
8712 case OpenACCClauseKind::VectorLength: {
8713 const auto *NWC = cast<OpenACCVectorLengthClause>(Val: C);
8714 writeSourceLocation(Loc: NWC->getLParenLoc());
8715 AddStmt(S: const_cast<Expr*>(NWC->getIntExpr()));
8716 return;
8717 }
8718 case OpenACCClauseKind::Private: {
8719 const auto *PC = cast<OpenACCPrivateClause>(Val: C);
8720 writeSourceLocation(Loc: PC->getLParenLoc());
8721 writeOpenACCVarList(C: PC);
8722 return;
8723 }
8724 case OpenACCClauseKind::Host: {
8725 const auto *HC = cast<OpenACCHostClause>(Val: C);
8726 writeSourceLocation(Loc: HC->getLParenLoc());
8727 writeOpenACCVarList(C: HC);
8728 return;
8729 }
8730 case OpenACCClauseKind::Device: {
8731 const auto *DC = cast<OpenACCDeviceClause>(Val: C);
8732 writeSourceLocation(Loc: DC->getLParenLoc());
8733 writeOpenACCVarList(C: DC);
8734 return;
8735 }
8736 case OpenACCClauseKind::FirstPrivate: {
8737 const auto *FPC = cast<OpenACCFirstPrivateClause>(Val: C);
8738 writeSourceLocation(Loc: FPC->getLParenLoc());
8739 writeOpenACCVarList(C: FPC);
8740 return;
8741 }
8742 case OpenACCClauseKind::Attach: {
8743 const auto *AC = cast<OpenACCAttachClause>(Val: C);
8744 writeSourceLocation(Loc: AC->getLParenLoc());
8745 writeOpenACCVarList(C: AC);
8746 return;
8747 }
8748 case OpenACCClauseKind::Detach: {
8749 const auto *DC = cast<OpenACCDetachClause>(Val: C);
8750 writeSourceLocation(Loc: DC->getLParenLoc());
8751 writeOpenACCVarList(C: DC);
8752 return;
8753 }
8754 case OpenACCClauseKind::Delete: {
8755 const auto *DC = cast<OpenACCDeleteClause>(Val: C);
8756 writeSourceLocation(Loc: DC->getLParenLoc());
8757 writeOpenACCVarList(C: DC);
8758 return;
8759 }
8760 case OpenACCClauseKind::UseDevice: {
8761 const auto *UDC = cast<OpenACCUseDeviceClause>(Val: C);
8762 writeSourceLocation(Loc: UDC->getLParenLoc());
8763 writeOpenACCVarList(C: UDC);
8764 return;
8765 }
8766 case OpenACCClauseKind::DevicePtr: {
8767 const auto *DPC = cast<OpenACCDevicePtrClause>(Val: C);
8768 writeSourceLocation(Loc: DPC->getLParenLoc());
8769 writeOpenACCVarList(C: DPC);
8770 return;
8771 }
8772 case OpenACCClauseKind::NoCreate: {
8773 const auto *NCC = cast<OpenACCNoCreateClause>(Val: C);
8774 writeSourceLocation(Loc: NCC->getLParenLoc());
8775 writeOpenACCVarList(C: NCC);
8776 return;
8777 }
8778 case OpenACCClauseKind::Present: {
8779 const auto *PC = cast<OpenACCPresentClause>(Val: C);
8780 writeSourceLocation(Loc: PC->getLParenLoc());
8781 writeOpenACCVarList(C: PC);
8782 return;
8783 }
8784 case OpenACCClauseKind::Copy:
8785 case OpenACCClauseKind::PCopy:
8786 case OpenACCClauseKind::PresentOrCopy: {
8787 const auto *CC = cast<OpenACCCopyClause>(Val: C);
8788 writeSourceLocation(Loc: CC->getLParenLoc());
8789 writeEnum(value: CC->getModifierList());
8790 writeOpenACCVarList(C: CC);
8791 return;
8792 }
8793 case OpenACCClauseKind::CopyIn:
8794 case OpenACCClauseKind::PCopyIn:
8795 case OpenACCClauseKind::PresentOrCopyIn: {
8796 const auto *CIC = cast<OpenACCCopyInClause>(Val: C);
8797 writeSourceLocation(Loc: CIC->getLParenLoc());
8798 writeEnum(value: CIC->getModifierList());
8799 writeOpenACCVarList(C: CIC);
8800 return;
8801 }
8802 case OpenACCClauseKind::CopyOut:
8803 case OpenACCClauseKind::PCopyOut:
8804 case OpenACCClauseKind::PresentOrCopyOut: {
8805 const auto *COC = cast<OpenACCCopyOutClause>(Val: C);
8806 writeSourceLocation(Loc: COC->getLParenLoc());
8807 writeEnum(value: COC->getModifierList());
8808 writeOpenACCVarList(C: COC);
8809 return;
8810 }
8811 case OpenACCClauseKind::Create:
8812 case OpenACCClauseKind::PCreate:
8813 case OpenACCClauseKind::PresentOrCreate: {
8814 const auto *CC = cast<OpenACCCreateClause>(Val: C);
8815 writeSourceLocation(Loc: CC->getLParenLoc());
8816 writeEnum(value: CC->getModifierList());
8817 writeOpenACCVarList(C: CC);
8818 return;
8819 }
8820 case OpenACCClauseKind::Async: {
8821 const auto *AC = cast<OpenACCAsyncClause>(Val: C);
8822 writeSourceLocation(Loc: AC->getLParenLoc());
8823 writeBool(Value: AC->hasIntExpr());
8824 if (AC->hasIntExpr())
8825 AddStmt(S: const_cast<Expr*>(AC->getIntExpr()));
8826 return;
8827 }
8828 case OpenACCClauseKind::Wait: {
8829 const auto *WC = cast<OpenACCWaitClause>(Val: C);
8830 writeSourceLocation(Loc: WC->getLParenLoc());
8831 writeBool(Value: WC->getDevNumExpr());
8832 if (Expr *DNE = WC->getDevNumExpr())
8833 AddStmt(S: DNE);
8834 writeSourceLocation(Loc: WC->getQueuesLoc());
8835
8836 writeOpenACCIntExprList(Exprs: WC->getQueueIdExprs());
8837 return;
8838 }
8839 case OpenACCClauseKind::DeviceType:
8840 case OpenACCClauseKind::DType: {
8841 const auto *DTC = cast<OpenACCDeviceTypeClause>(Val: C);
8842 writeSourceLocation(Loc: DTC->getLParenLoc());
8843 writeUInt32(Value: DTC->getArchitectures().size());
8844 for (const DeviceTypeArgument &Arg : DTC->getArchitectures()) {
8845 writeBool(Value: Arg.getIdentifierInfo());
8846 if (Arg.getIdentifierInfo())
8847 AddIdentifierRef(II: Arg.getIdentifierInfo());
8848 writeSourceLocation(Loc: Arg.getLoc());
8849 }
8850 return;
8851 }
8852 case OpenACCClauseKind::Reduction: {
8853 const auto *RC = cast<OpenACCReductionClause>(Val: C);
8854 writeSourceLocation(Loc: RC->getLParenLoc());
8855 writeEnum(value: RC->getReductionOp());
8856 writeOpenACCVarList(C: RC);
8857 return;
8858 }
8859 case OpenACCClauseKind::Seq:
8860 case OpenACCClauseKind::Independent:
8861 case OpenACCClauseKind::NoHost:
8862 case OpenACCClauseKind::Auto:
8863 case OpenACCClauseKind::Finalize:
8864 case OpenACCClauseKind::IfPresent:
8865 // Nothing to do here, there is no additional information beyond the
8866 // begin/end loc and clause kind.
8867 return;
8868 case OpenACCClauseKind::Collapse: {
8869 const auto *CC = cast<OpenACCCollapseClause>(Val: C);
8870 writeSourceLocation(Loc: CC->getLParenLoc());
8871 writeBool(Value: CC->hasForce());
8872 AddStmt(S: const_cast<Expr *>(CC->getLoopCount()));
8873 return;
8874 }
8875 case OpenACCClauseKind::Tile: {
8876 const auto *TC = cast<OpenACCTileClause>(Val: C);
8877 writeSourceLocation(Loc: TC->getLParenLoc());
8878 writeUInt32(Value: TC->getSizeExprs().size());
8879 for (Expr *E : TC->getSizeExprs())
8880 AddStmt(S: E);
8881 return;
8882 }
8883 case OpenACCClauseKind::Gang: {
8884 const auto *GC = cast<OpenACCGangClause>(Val: C);
8885 writeSourceLocation(Loc: GC->getLParenLoc());
8886 writeUInt32(Value: GC->getNumExprs());
8887 for (unsigned I = 0; I < GC->getNumExprs(); ++I) {
8888 writeEnum(value: GC->getExpr(I).first);
8889 AddStmt(S: const_cast<Expr *>(GC->getExpr(I).second));
8890 }
8891 return;
8892 }
8893 case OpenACCClauseKind::Worker: {
8894 const auto *WC = cast<OpenACCWorkerClause>(Val: C);
8895 writeSourceLocation(Loc: WC->getLParenLoc());
8896 writeBool(Value: WC->hasIntExpr());
8897 if (WC->hasIntExpr())
8898 AddStmt(S: const_cast<Expr *>(WC->getIntExpr()));
8899 return;
8900 }
8901 case OpenACCClauseKind::Vector: {
8902 const auto *VC = cast<OpenACCVectorClause>(Val: C);
8903 writeSourceLocation(Loc: VC->getLParenLoc());
8904 writeBool(Value: VC->hasIntExpr());
8905 if (VC->hasIntExpr())
8906 AddStmt(S: const_cast<Expr *>(VC->getIntExpr()));
8907 return;
8908 }
8909 case OpenACCClauseKind::Link: {
8910 const auto *LC = cast<OpenACCLinkClause>(Val: C);
8911 writeSourceLocation(Loc: LC->getLParenLoc());
8912 writeOpenACCVarList(C: LC);
8913 return;
8914 }
8915 case OpenACCClauseKind::DeviceResident: {
8916 const auto *DRC = cast<OpenACCDeviceResidentClause>(Val: C);
8917 writeSourceLocation(Loc: DRC->getLParenLoc());
8918 writeOpenACCVarList(C: DRC);
8919 return;
8920 }
8921
8922 case OpenACCClauseKind::Bind: {
8923 const auto *BC = cast<OpenACCBindClause>(Val: C);
8924 writeSourceLocation(Loc: BC->getLParenLoc());
8925 writeBool(Value: BC->isStringArgument());
8926 if (BC->isStringArgument())
8927 AddStmt(S: const_cast<StringLiteral *>(BC->getStringArgument()));
8928 else
8929 AddIdentifierRef(II: BC->getIdentifierArgument());
8930
8931 return;
8932 }
8933 case OpenACCClauseKind::Invalid:
8934 case OpenACCClauseKind::Shortloop:
8935 llvm_unreachable("Clause serialization not yet implemented");
8936 }
8937 llvm_unreachable("Invalid Clause Kind");
8938}
8939
8940void ASTRecordWriter::writeOpenACCClauseList(
8941 ArrayRef<const OpenACCClause *> Clauses) {
8942 for (const OpenACCClause *Clause : Clauses)
8943 writeOpenACCClause(C: Clause);
8944}
8945void ASTRecordWriter::AddOpenACCRoutineDeclAttr(
8946 const OpenACCRoutineDeclAttr *A) {
8947 // We have to write the size so that the reader can do a resize. Unlike the
8948 // Decl version of this, we can't count on trailing storage to get this right.
8949 writeUInt32(Value: A->Clauses.size());
8950 writeOpenACCClauseList(Clauses: A->Clauses);
8951}
8952

source code of clang/lib/Serialization/ASTWriter.cpp