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