Revert "Reland - [Offload] Introduce offload-tblgen and initial new API implementatio...
[llvm-project.git] / clang / lib / Serialization / ASTWriter.cpp
bloba52d59c61c4ce616989179bf929b44d3a7ddf958
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.
11 //===----------------------------------------------------------------------===//
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "MultiOnDiskHashTable.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTUnresolvedSet.h"
18 #include "clang/AST/AbstractTypeWriter.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclContextInternals.h"
24 #include "clang/AST/DeclFriend.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/LambdaCapture.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/OpenACCClause.h"
33 #include "clang/AST/OpenMPClause.h"
34 #include "clang/AST/RawCommentList.h"
35 #include "clang/AST/TemplateName.h"
36 #include "clang/AST/Type.h"
37 #include "clang/AST/TypeLoc.h"
38 #include "clang/AST/TypeLocVisitor.h"
39 #include "clang/Basic/Diagnostic.h"
40 #include "clang/Basic/DiagnosticOptions.h"
41 #include "clang/Basic/FileEntry.h"
42 #include "clang/Basic/FileManager.h"
43 #include "clang/Basic/FileSystemOptions.h"
44 #include "clang/Basic/IdentifierTable.h"
45 #include "clang/Basic/LLVM.h"
46 #include "clang/Basic/Lambda.h"
47 #include "clang/Basic/LangOptions.h"
48 #include "clang/Basic/Module.h"
49 #include "clang/Basic/ObjCRuntime.h"
50 #include "clang/Basic/OpenACCKinds.h"
51 #include "clang/Basic/OpenCLOptions.h"
52 #include "clang/Basic/SourceLocation.h"
53 #include "clang/Basic/SourceManager.h"
54 #include "clang/Basic/SourceManagerInternals.h"
55 #include "clang/Basic/Specifiers.h"
56 #include "clang/Basic/TargetInfo.h"
57 #include "clang/Basic/TargetOptions.h"
58 #include "clang/Basic/Version.h"
59 #include "clang/Lex/HeaderSearch.h"
60 #include "clang/Lex/HeaderSearchOptions.h"
61 #include "clang/Lex/MacroInfo.h"
62 #include "clang/Lex/ModuleMap.h"
63 #include "clang/Lex/PreprocessingRecord.h"
64 #include "clang/Lex/Preprocessor.h"
65 #include "clang/Lex/PreprocessorOptions.h"
66 #include "clang/Lex/Token.h"
67 #include "clang/Sema/IdentifierResolver.h"
68 #include "clang/Sema/ObjCMethodList.h"
69 #include "clang/Sema/Sema.h"
70 #include "clang/Sema/SemaCUDA.h"
71 #include "clang/Sema/SemaObjC.h"
72 #include "clang/Sema/Weak.h"
73 #include "clang/Serialization/ASTBitCodes.h"
74 #include "clang/Serialization/ASTReader.h"
75 #include "clang/Serialization/ASTRecordWriter.h"
76 #include "clang/Serialization/InMemoryModuleCache.h"
77 #include "clang/Serialization/ModuleFile.h"
78 #include "clang/Serialization/ModuleFileExtension.h"
79 #include "clang/Serialization/SerializationDiagnostic.h"
80 #include "llvm/ADT/APFloat.h"
81 #include "llvm/ADT/APInt.h"
82 #include "llvm/ADT/APSInt.h"
83 #include "llvm/ADT/ArrayRef.h"
84 #include "llvm/ADT/DenseMap.h"
85 #include "llvm/ADT/DenseSet.h"
86 #include "llvm/ADT/Hashing.h"
87 #include "llvm/ADT/PointerIntPair.h"
88 #include "llvm/ADT/STLExtras.h"
89 #include "llvm/ADT/ScopeExit.h"
90 #include "llvm/ADT/SmallPtrSet.h"
91 #include "llvm/ADT/SmallString.h"
92 #include "llvm/ADT/SmallVector.h"
93 #include "llvm/ADT/StringMap.h"
94 #include "llvm/ADT/StringRef.h"
95 #include "llvm/Bitstream/BitCodes.h"
96 #include "llvm/Bitstream/BitstreamWriter.h"
97 #include "llvm/Support/Casting.h"
98 #include "llvm/Support/Compression.h"
99 #include "llvm/Support/DJB.h"
100 #include "llvm/Support/Endian.h"
101 #include "llvm/Support/EndianStream.h"
102 #include "llvm/Support/Error.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/raw_ostream.h"
112 #include <algorithm>
113 #include <cassert>
114 #include <cstdint>
115 #include <cstdlib>
116 #include <cstring>
117 #include <ctime>
118 #include <limits>
119 #include <memory>
120 #include <optional>
121 #include <queue>
122 #include <tuple>
123 #include <utility>
124 #include <vector>
126 using namespace clang;
127 using namespace clang::serialization;
129 template <typename T, typename Allocator>
130 static StringRef bytes(const std::vector<T, Allocator> &v) {
131 if (v.empty()) return StringRef();
132 return StringRef(reinterpret_cast<const char*>(&v[0]),
133 sizeof(T) * v.size());
136 template <typename T>
137 static StringRef bytes(const SmallVectorImpl<T> &v) {
138 return StringRef(reinterpret_cast<const char*>(v.data()),
139 sizeof(T) * v.size());
142 static std::string bytes(const std::vector<bool> &V) {
143 std::string Str;
144 Str.reserve(V.size() / 8);
145 for (unsigned I = 0, E = V.size(); I < E;) {
146 char Byte = 0;
147 for (unsigned Bit = 0; Bit < 8 && I < E; ++Bit, ++I)
148 Byte |= V[I] << Bit;
149 Str += Byte;
151 return Str;
154 //===----------------------------------------------------------------------===//
155 // Type serialization
156 //===----------------------------------------------------------------------===//
158 static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
159 switch (id) {
160 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
161 case Type::CLASS_ID: return TYPE_##CODE_ID;
162 #include "clang/Serialization/TypeBitCodes.def"
163 case Type::Builtin:
164 llvm_unreachable("shouldn't be serializing a builtin type this way");
166 llvm_unreachable("bad type kind");
169 namespace {
171 struct AffectingModuleMaps {
172 llvm::DenseSet<FileID> DefinitionFileIDs;
173 llvm::DenseSet<const FileEntry *> DefinitionFiles;
176 std::optional<AffectingModuleMaps>
177 GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
178 if (!PP.getHeaderSearchInfo()
179 .getHeaderSearchOpts()
180 .ModulesPruneNonAffectingModuleMaps)
181 return std::nullopt;
183 const HeaderSearch &HS = PP.getHeaderSearchInfo();
184 const SourceManager &SM = PP.getSourceManager();
185 const ModuleMap &MM = HS.getModuleMap();
187 llvm::DenseSet<FileID> ModuleMaps;
189 llvm::DenseSet<const Module *> ProcessedModules;
190 auto CollectModuleMapsForHierarchy = [&](const Module *M) {
191 M = M->getTopLevelModule();
193 if (!ProcessedModules.insert(M).second)
194 return;
196 std::queue<const Module *> Q;
197 Q.push(M);
198 while (!Q.empty()) {
199 const Module *Mod = Q.front();
200 Q.pop();
202 // The containing module map is affecting, because it's being pointed
203 // into by Module::DefinitionLoc.
204 if (auto F = MM.getContainingModuleMapFileID(Mod); F.isValid())
205 ModuleMaps.insert(F);
206 // For inferred modules, the module map that allowed inferring is not
207 // related to the virtual containing module map file. It did affect the
208 // compilation, though.
209 if (auto UniqF = MM.getModuleMapFileIDForUniquing(Mod); UniqF.isValid())
210 ModuleMaps.insert(UniqF);
212 for (auto *SubM : Mod->submodules())
213 Q.push(SubM);
217 // Handle all the affecting modules referenced from the root module.
219 CollectModuleMapsForHierarchy(RootModule);
221 std::queue<const Module *> Q;
222 Q.push(RootModule);
223 while (!Q.empty()) {
224 const Module *CurrentModule = Q.front();
225 Q.pop();
227 for (const Module *ImportedModule : CurrentModule->Imports)
228 CollectModuleMapsForHierarchy(ImportedModule);
229 for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses)
230 CollectModuleMapsForHierarchy(UndeclaredModule);
232 for (auto *M : CurrentModule->submodules())
233 Q.push(M);
236 // Handle textually-included headers that belong to other modules.
238 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
239 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
241 if (FilesByUID.size() > HS.header_file_size())
242 FilesByUID.resize(HS.header_file_size());
244 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
245 OptionalFileEntryRef File = FilesByUID[UID];
246 if (!File)
247 continue;
249 const HeaderFileInfo *HFI = HS.getExistingLocalFileInfo(*File);
250 if (!HFI)
251 continue; // We have no information on this being a header file.
252 if (!HFI->isCompilingModuleHeader && HFI->isModuleHeader)
253 continue; // Modular header, handled in the above module-based loop.
254 if (!HFI->isCompilingModuleHeader && !HFI->IsLocallyIncluded)
255 continue; // Non-modular header not included locally is not affecting.
257 for (const auto &KH : HS.findResolvedModulesForHeader(*File))
258 if (const Module *M = KH.getModule())
259 CollectModuleMapsForHierarchy(M);
262 // FIXME: This algorithm is not correct for module map hierarchies where
263 // module map file defining a (sub)module of a top-level module X includes
264 // a module map file that defines a (sub)module of another top-level module Y.
265 // Whenever X is affecting and Y is not, "replaying" this PCM file will fail
266 // when parsing module map files for X due to not knowing about the `extern`
267 // module map for Y.
269 // We don't have a good way to fix it here. We could mark all children of
270 // affecting module map files as being affecting as well, but that's
271 // expensive. SourceManager does not model the edge from parent to child
272 // SLocEntries, so instead, we would need to iterate over leaf module map
273 // files, walk up their include hierarchy and check whether we arrive at an
274 // affecting module map.
276 // Instead of complicating and slowing down this function, we should probably
277 // just ban module map hierarchies where module map defining a (sub)module X
278 // includes a module map defining a module that's not a submodule of X.
280 llvm::DenseSet<const FileEntry *> ModuleFileEntries;
281 for (FileID MM : ModuleMaps) {
282 if (auto *FE = SM.getFileEntryForID(MM))
283 ModuleFileEntries.insert(FE);
286 AffectingModuleMaps R;
287 R.DefinitionFileIDs = std::move(ModuleMaps);
288 R.DefinitionFiles = std::move(ModuleFileEntries);
289 return std::move(R);
292 class ASTTypeWriter {
293 ASTWriter &Writer;
294 ASTWriter::RecordData Record;
295 ASTRecordWriter BasicWriter;
297 public:
298 ASTTypeWriter(ASTContext &Context, ASTWriter &Writer)
299 : Writer(Writer), BasicWriter(Context, Writer, Record) {}
301 uint64_t write(QualType T) {
302 if (T.hasLocalNonFastQualifiers()) {
303 Qualifiers Qs = T.getLocalQualifiers();
304 BasicWriter.writeQualType(T.getLocalUnqualifiedType());
305 BasicWriter.writeQualifiers(Qs);
306 return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
309 const Type *typePtr = T.getTypePtr();
310 serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
311 atw.write(typePtr);
312 return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
313 /*abbrev*/ 0);
317 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
318 using LocSeq = SourceLocationSequence;
320 ASTRecordWriter &Record;
321 LocSeq *Seq;
323 void addSourceLocation(SourceLocation Loc) {
324 Record.AddSourceLocation(Loc, Seq);
326 void addSourceRange(SourceRange Range) { Record.AddSourceRange(Range, Seq); }
328 public:
329 TypeLocWriter(ASTRecordWriter &Record, LocSeq *Seq)
330 : Record(Record), Seq(Seq) {}
332 #define ABSTRACT_TYPELOC(CLASS, PARENT)
333 #define TYPELOC(CLASS, PARENT) \
334 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
335 #include "clang/AST/TypeLocNodes.def"
337 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
338 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
341 } // namespace
343 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
344 // nothing to do
347 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
348 addSourceLocation(TL.getBuiltinLoc());
349 if (TL.needsExtraLocalData()) {
350 Record.push_back(TL.getWrittenTypeSpec());
351 Record.push_back(static_cast<uint64_t>(TL.getWrittenSignSpec()));
352 Record.push_back(static_cast<uint64_t>(TL.getWrittenWidthSpec()));
353 Record.push_back(TL.hasModeAttr());
357 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
358 addSourceLocation(TL.getNameLoc());
361 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
362 addSourceLocation(TL.getStarLoc());
365 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
366 // nothing to do
369 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
370 // nothing to do
373 void TypeLocWriter::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
374 // nothing to do
377 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
378 addSourceLocation(TL.getCaretLoc());
381 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
382 addSourceLocation(TL.getAmpLoc());
385 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
386 addSourceLocation(TL.getAmpAmpLoc());
389 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
390 addSourceLocation(TL.getStarLoc());
391 Record.AddTypeSourceInfo(TL.getClassTInfo());
394 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
395 addSourceLocation(TL.getLBracketLoc());
396 addSourceLocation(TL.getRBracketLoc());
397 Record.push_back(TL.getSizeExpr() ? 1 : 0);
398 if (TL.getSizeExpr())
399 Record.AddStmt(TL.getSizeExpr());
402 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
403 VisitArrayTypeLoc(TL);
406 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
407 VisitArrayTypeLoc(TL);
410 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
411 VisitArrayTypeLoc(TL);
414 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
415 DependentSizedArrayTypeLoc TL) {
416 VisitArrayTypeLoc(TL);
419 void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
420 DependentAddressSpaceTypeLoc TL) {
421 addSourceLocation(TL.getAttrNameLoc());
422 SourceRange range = TL.getAttrOperandParensRange();
423 addSourceLocation(range.getBegin());
424 addSourceLocation(range.getEnd());
425 Record.AddStmt(TL.getAttrExprOperand());
428 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
429 DependentSizedExtVectorTypeLoc TL) {
430 addSourceLocation(TL.getNameLoc());
433 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
434 addSourceLocation(TL.getNameLoc());
437 void TypeLocWriter::VisitDependentVectorTypeLoc(
438 DependentVectorTypeLoc TL) {
439 addSourceLocation(TL.getNameLoc());
442 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
443 addSourceLocation(TL.getNameLoc());
446 void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
447 addSourceLocation(TL.getAttrNameLoc());
448 SourceRange range = TL.getAttrOperandParensRange();
449 addSourceLocation(range.getBegin());
450 addSourceLocation(range.getEnd());
451 Record.AddStmt(TL.getAttrRowOperand());
452 Record.AddStmt(TL.getAttrColumnOperand());
455 void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
456 DependentSizedMatrixTypeLoc TL) {
457 addSourceLocation(TL.getAttrNameLoc());
458 SourceRange range = TL.getAttrOperandParensRange();
459 addSourceLocation(range.getBegin());
460 addSourceLocation(range.getEnd());
461 Record.AddStmt(TL.getAttrRowOperand());
462 Record.AddStmt(TL.getAttrColumnOperand());
465 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
466 addSourceLocation(TL.getLocalRangeBegin());
467 addSourceLocation(TL.getLParenLoc());
468 addSourceLocation(TL.getRParenLoc());
469 addSourceRange(TL.getExceptionSpecRange());
470 addSourceLocation(TL.getLocalRangeEnd());
471 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
472 Record.AddDeclRef(TL.getParam(i));
475 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
476 VisitFunctionTypeLoc(TL);
479 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
480 VisitFunctionTypeLoc(TL);
483 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
484 addSourceLocation(TL.getNameLoc());
487 void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) {
488 addSourceLocation(TL.getNameLoc());
491 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
492 addSourceLocation(TL.getNameLoc());
495 void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
496 if (TL.getNumProtocols()) {
497 addSourceLocation(TL.getProtocolLAngleLoc());
498 addSourceLocation(TL.getProtocolRAngleLoc());
500 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
501 addSourceLocation(TL.getProtocolLoc(i));
504 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
505 addSourceLocation(TL.getTypeofLoc());
506 addSourceLocation(TL.getLParenLoc());
507 addSourceLocation(TL.getRParenLoc());
510 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
511 addSourceLocation(TL.getTypeofLoc());
512 addSourceLocation(TL.getLParenLoc());
513 addSourceLocation(TL.getRParenLoc());
514 Record.AddTypeSourceInfo(TL.getUnmodifiedTInfo());
517 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
518 addSourceLocation(TL.getDecltypeLoc());
519 addSourceLocation(TL.getRParenLoc());
522 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
523 addSourceLocation(TL.getKWLoc());
524 addSourceLocation(TL.getLParenLoc());
525 addSourceLocation(TL.getRParenLoc());
526 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
529 void ASTRecordWriter::AddConceptReference(const ConceptReference *CR) {
530 assert(CR);
531 AddNestedNameSpecifierLoc(CR->getNestedNameSpecifierLoc());
532 AddSourceLocation(CR->getTemplateKWLoc());
533 AddDeclarationNameInfo(CR->getConceptNameInfo());
534 AddDeclRef(CR->getFoundDecl());
535 AddDeclRef(CR->getNamedConcept());
536 push_back(CR->getTemplateArgsAsWritten() != nullptr);
537 if (CR->getTemplateArgsAsWritten())
538 AddASTTemplateArgumentListInfo(CR->getTemplateArgsAsWritten());
541 void TypeLocWriter::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
542 addSourceLocation(TL.getEllipsisLoc());
545 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
546 addSourceLocation(TL.getNameLoc());
547 auto *CR = TL.getConceptReference();
548 Record.push_back(TL.isConstrained() && CR);
549 if (TL.isConstrained() && CR)
550 Record.AddConceptReference(CR);
551 Record.push_back(TL.isDecltypeAuto());
552 if (TL.isDecltypeAuto())
553 addSourceLocation(TL.getRParenLoc());
556 void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
557 DeducedTemplateSpecializationTypeLoc TL) {
558 addSourceLocation(TL.getTemplateNameLoc());
561 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
562 addSourceLocation(TL.getNameLoc());
565 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
566 addSourceLocation(TL.getNameLoc());
569 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
570 Record.AddAttr(TL.getAttr());
573 void TypeLocWriter::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
574 // Nothing to do
577 void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
578 // Nothing to do.
581 void TypeLocWriter::VisitHLSLAttributedResourceTypeLoc(
582 HLSLAttributedResourceTypeLoc TL) {
583 // Nothing to do.
586 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
587 addSourceLocation(TL.getNameLoc());
590 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
591 SubstTemplateTypeParmTypeLoc TL) {
592 addSourceLocation(TL.getNameLoc());
595 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
596 SubstTemplateTypeParmPackTypeLoc TL) {
597 addSourceLocation(TL.getNameLoc());
600 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
601 TemplateSpecializationTypeLoc TL) {
602 addSourceLocation(TL.getTemplateKeywordLoc());
603 addSourceLocation(TL.getTemplateNameLoc());
604 addSourceLocation(TL.getLAngleLoc());
605 addSourceLocation(TL.getRAngleLoc());
606 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
607 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
608 TL.getArgLoc(i).getLocInfo());
611 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
612 addSourceLocation(TL.getLParenLoc());
613 addSourceLocation(TL.getRParenLoc());
616 void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
617 addSourceLocation(TL.getExpansionLoc());
620 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
621 addSourceLocation(TL.getElaboratedKeywordLoc());
622 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
625 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
626 addSourceLocation(TL.getNameLoc());
629 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
630 addSourceLocation(TL.getElaboratedKeywordLoc());
631 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
632 addSourceLocation(TL.getNameLoc());
635 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
636 DependentTemplateSpecializationTypeLoc TL) {
637 addSourceLocation(TL.getElaboratedKeywordLoc());
638 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
639 addSourceLocation(TL.getTemplateKeywordLoc());
640 addSourceLocation(TL.getTemplateNameLoc());
641 addSourceLocation(TL.getLAngleLoc());
642 addSourceLocation(TL.getRAngleLoc());
643 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
644 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
645 TL.getArgLoc(I).getLocInfo());
648 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
649 addSourceLocation(TL.getEllipsisLoc());
652 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
653 addSourceLocation(TL.getNameLoc());
654 addSourceLocation(TL.getNameEndLoc());
657 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
658 Record.push_back(TL.hasBaseTypeAsWritten());
659 addSourceLocation(TL.getTypeArgsLAngleLoc());
660 addSourceLocation(TL.getTypeArgsRAngleLoc());
661 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
662 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
663 addSourceLocation(TL.getProtocolLAngleLoc());
664 addSourceLocation(TL.getProtocolRAngleLoc());
665 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
666 addSourceLocation(TL.getProtocolLoc(i));
669 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
670 addSourceLocation(TL.getStarLoc());
673 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
674 addSourceLocation(TL.getKWLoc());
675 addSourceLocation(TL.getLParenLoc());
676 addSourceLocation(TL.getRParenLoc());
679 void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
680 addSourceLocation(TL.getKWLoc());
683 void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
684 addSourceLocation(TL.getNameLoc());
686 void TypeLocWriter::VisitDependentBitIntTypeLoc(
687 clang::DependentBitIntTypeLoc TL) {
688 addSourceLocation(TL.getNameLoc());
691 void ASTWriter::WriteTypeAbbrevs() {
692 using namespace llvm;
694 std::shared_ptr<BitCodeAbbrev> Abv;
696 // Abbreviation for TYPE_EXT_QUAL
697 Abv = std::make_shared<BitCodeAbbrev>();
698 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
699 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
700 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
701 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
704 //===----------------------------------------------------------------------===//
705 // ASTWriter Implementation
706 //===----------------------------------------------------------------------===//
708 static void EmitBlockID(unsigned ID, const char *Name,
709 llvm::BitstreamWriter &Stream,
710 ASTWriter::RecordDataImpl &Record) {
711 Record.clear();
712 Record.push_back(ID);
713 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
715 // Emit the block name if present.
716 if (!Name || Name[0] == 0)
717 return;
718 Record.clear();
719 while (*Name)
720 Record.push_back(*Name++);
721 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
724 static void EmitRecordID(unsigned ID, const char *Name,
725 llvm::BitstreamWriter &Stream,
726 ASTWriter::RecordDataImpl &Record) {
727 Record.clear();
728 Record.push_back(ID);
729 while (*Name)
730 Record.push_back(*Name++);
731 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
734 static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
735 ASTWriter::RecordDataImpl &Record) {
736 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
737 RECORD(STMT_STOP);
738 RECORD(STMT_NULL_PTR);
739 RECORD(STMT_REF_PTR);
740 RECORD(STMT_NULL);
741 RECORD(STMT_COMPOUND);
742 RECORD(STMT_CASE);
743 RECORD(STMT_DEFAULT);
744 RECORD(STMT_LABEL);
745 RECORD(STMT_ATTRIBUTED);
746 RECORD(STMT_IF);
747 RECORD(STMT_SWITCH);
748 RECORD(STMT_WHILE);
749 RECORD(STMT_DO);
750 RECORD(STMT_FOR);
751 RECORD(STMT_GOTO);
752 RECORD(STMT_INDIRECT_GOTO);
753 RECORD(STMT_CONTINUE);
754 RECORD(STMT_BREAK);
755 RECORD(STMT_RETURN);
756 RECORD(STMT_DECL);
757 RECORD(STMT_GCCASM);
758 RECORD(STMT_MSASM);
759 RECORD(EXPR_PREDEFINED);
760 RECORD(EXPR_DECL_REF);
761 RECORD(EXPR_INTEGER_LITERAL);
762 RECORD(EXPR_FIXEDPOINT_LITERAL);
763 RECORD(EXPR_FLOATING_LITERAL);
764 RECORD(EXPR_IMAGINARY_LITERAL);
765 RECORD(EXPR_STRING_LITERAL);
766 RECORD(EXPR_CHARACTER_LITERAL);
767 RECORD(EXPR_PAREN);
768 RECORD(EXPR_PAREN_LIST);
769 RECORD(EXPR_UNARY_OPERATOR);
770 RECORD(EXPR_SIZEOF_ALIGN_OF);
771 RECORD(EXPR_ARRAY_SUBSCRIPT);
772 RECORD(EXPR_CALL);
773 RECORD(EXPR_MEMBER);
774 RECORD(EXPR_BINARY_OPERATOR);
775 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
776 RECORD(EXPR_CONDITIONAL_OPERATOR);
777 RECORD(EXPR_IMPLICIT_CAST);
778 RECORD(EXPR_CSTYLE_CAST);
779 RECORD(EXPR_COMPOUND_LITERAL);
780 RECORD(EXPR_EXT_VECTOR_ELEMENT);
781 RECORD(EXPR_INIT_LIST);
782 RECORD(EXPR_DESIGNATED_INIT);
783 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
784 RECORD(EXPR_IMPLICIT_VALUE_INIT);
785 RECORD(EXPR_NO_INIT);
786 RECORD(EXPR_VA_ARG);
787 RECORD(EXPR_ADDR_LABEL);
788 RECORD(EXPR_STMT);
789 RECORD(EXPR_CHOOSE);
790 RECORD(EXPR_GNU_NULL);
791 RECORD(EXPR_SHUFFLE_VECTOR);
792 RECORD(EXPR_BLOCK);
793 RECORD(EXPR_GENERIC_SELECTION);
794 RECORD(EXPR_OBJC_STRING_LITERAL);
795 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
796 RECORD(EXPR_OBJC_ARRAY_LITERAL);
797 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
798 RECORD(EXPR_OBJC_ENCODE);
799 RECORD(EXPR_OBJC_SELECTOR_EXPR);
800 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
801 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
802 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
803 RECORD(EXPR_OBJC_KVC_REF_EXPR);
804 RECORD(EXPR_OBJC_MESSAGE_EXPR);
805 RECORD(STMT_OBJC_FOR_COLLECTION);
806 RECORD(STMT_OBJC_CATCH);
807 RECORD(STMT_OBJC_FINALLY);
808 RECORD(STMT_OBJC_AT_TRY);
809 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
810 RECORD(STMT_OBJC_AT_THROW);
811 RECORD(EXPR_OBJC_BOOL_LITERAL);
812 RECORD(STMT_CXX_CATCH);
813 RECORD(STMT_CXX_TRY);
814 RECORD(STMT_CXX_FOR_RANGE);
815 RECORD(EXPR_CXX_OPERATOR_CALL);
816 RECORD(EXPR_CXX_MEMBER_CALL);
817 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
818 RECORD(EXPR_CXX_CONSTRUCT);
819 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
820 RECORD(EXPR_CXX_STATIC_CAST);
821 RECORD(EXPR_CXX_DYNAMIC_CAST);
822 RECORD(EXPR_CXX_REINTERPRET_CAST);
823 RECORD(EXPR_CXX_CONST_CAST);
824 RECORD(EXPR_CXX_ADDRSPACE_CAST);
825 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
826 RECORD(EXPR_USER_DEFINED_LITERAL);
827 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
828 RECORD(EXPR_CXX_BOOL_LITERAL);
829 RECORD(EXPR_CXX_PAREN_LIST_INIT);
830 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
831 RECORD(EXPR_CXX_TYPEID_EXPR);
832 RECORD(EXPR_CXX_TYPEID_TYPE);
833 RECORD(EXPR_CXX_THIS);
834 RECORD(EXPR_CXX_THROW);
835 RECORD(EXPR_CXX_DEFAULT_ARG);
836 RECORD(EXPR_CXX_DEFAULT_INIT);
837 RECORD(EXPR_CXX_BIND_TEMPORARY);
838 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
839 RECORD(EXPR_CXX_NEW);
840 RECORD(EXPR_CXX_DELETE);
841 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
842 RECORD(EXPR_EXPR_WITH_CLEANUPS);
843 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
844 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
845 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
846 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
847 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
848 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
849 RECORD(EXPR_CXX_NOEXCEPT);
850 RECORD(EXPR_OPAQUE_VALUE);
851 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
852 RECORD(EXPR_TYPE_TRAIT);
853 RECORD(EXPR_ARRAY_TYPE_TRAIT);
854 RECORD(EXPR_PACK_EXPANSION);
855 RECORD(EXPR_SIZEOF_PACK);
856 RECORD(EXPR_PACK_INDEXING);
857 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
858 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
859 RECORD(EXPR_FUNCTION_PARM_PACK);
860 RECORD(EXPR_MATERIALIZE_TEMPORARY);
861 RECORD(EXPR_CUDA_KERNEL_CALL);
862 RECORD(EXPR_CXX_UUIDOF_EXPR);
863 RECORD(EXPR_CXX_UUIDOF_TYPE);
864 RECORD(EXPR_LAMBDA);
865 #undef RECORD
868 void ASTWriter::WriteBlockInfoBlock() {
869 RecordData Record;
870 Stream.EnterBlockInfoBlock();
872 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
873 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
875 // Control Block.
876 BLOCK(CONTROL_BLOCK);
877 RECORD(METADATA);
878 RECORD(MODULE_NAME);
879 RECORD(MODULE_DIRECTORY);
880 RECORD(MODULE_MAP_FILE);
881 RECORD(IMPORT);
882 RECORD(ORIGINAL_FILE);
883 RECORD(ORIGINAL_FILE_ID);
884 RECORD(INPUT_FILE_OFFSETS);
886 BLOCK(OPTIONS_BLOCK);
887 RECORD(LANGUAGE_OPTIONS);
888 RECORD(TARGET_OPTIONS);
889 RECORD(FILE_SYSTEM_OPTIONS);
890 RECORD(HEADER_SEARCH_OPTIONS);
891 RECORD(PREPROCESSOR_OPTIONS);
893 BLOCK(INPUT_FILES_BLOCK);
894 RECORD(INPUT_FILE);
895 RECORD(INPUT_FILE_HASH);
897 // AST Top-Level Block.
898 BLOCK(AST_BLOCK);
899 RECORD(TYPE_OFFSET);
900 RECORD(DECL_OFFSET);
901 RECORD(IDENTIFIER_OFFSET);
902 RECORD(IDENTIFIER_TABLE);
903 RECORD(EAGERLY_DESERIALIZED_DECLS);
904 RECORD(MODULAR_CODEGEN_DECLS);
905 RECORD(SPECIAL_TYPES);
906 RECORD(STATISTICS);
907 RECORD(TENTATIVE_DEFINITIONS);
908 RECORD(SELECTOR_OFFSETS);
909 RECORD(METHOD_POOL);
910 RECORD(PP_COUNTER_VALUE);
911 RECORD(SOURCE_LOCATION_OFFSETS);
912 RECORD(EXT_VECTOR_DECLS);
913 RECORD(UNUSED_FILESCOPED_DECLS);
914 RECORD(PPD_ENTITIES_OFFSETS);
915 RECORD(VTABLE_USES);
916 RECORD(PPD_SKIPPED_RANGES);
917 RECORD(REFERENCED_SELECTOR_POOL);
918 RECORD(TU_UPDATE_LEXICAL);
919 RECORD(SEMA_DECL_REFS);
920 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
921 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
922 RECORD(UPDATE_VISIBLE);
923 RECORD(DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD);
924 RECORD(FUNCTION_DECL_TO_LAMBDAS_MAP);
925 RECORD(DECL_UPDATE_OFFSETS);
926 RECORD(DECL_UPDATES);
927 RECORD(CUDA_SPECIAL_DECL_REFS);
928 RECORD(HEADER_SEARCH_TABLE);
929 RECORD(FP_PRAGMA_OPTIONS);
930 RECORD(OPENCL_EXTENSIONS);
931 RECORD(OPENCL_EXTENSION_TYPES);
932 RECORD(OPENCL_EXTENSION_DECLS);
933 RECORD(DELEGATING_CTORS);
934 RECORD(KNOWN_NAMESPACES);
935 RECORD(MODULE_OFFSET_MAP);
936 RECORD(SOURCE_MANAGER_LINE_TABLE);
937 RECORD(OBJC_CATEGORIES_MAP);
938 RECORD(FILE_SORTED_DECLS);
939 RECORD(IMPORTED_MODULES);
940 RECORD(OBJC_CATEGORIES);
941 RECORD(MACRO_OFFSET);
942 RECORD(INTERESTING_IDENTIFIERS);
943 RECORD(UNDEFINED_BUT_USED);
944 RECORD(LATE_PARSED_TEMPLATE);
945 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
946 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
947 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
948 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
949 RECORD(DELETE_EXPRS_TO_ANALYZE);
950 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
951 RECORD(PP_CONDITIONAL_STACK);
952 RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
953 RECORD(PP_ASSUME_NONNULL_LOC);
954 RECORD(PP_UNSAFE_BUFFER_USAGE);
955 RECORD(VTABLES_TO_EMIT);
957 // SourceManager Block.
958 BLOCK(SOURCE_MANAGER_BLOCK);
959 RECORD(SM_SLOC_FILE_ENTRY);
960 RECORD(SM_SLOC_BUFFER_ENTRY);
961 RECORD(SM_SLOC_BUFFER_BLOB);
962 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
963 RECORD(SM_SLOC_EXPANSION_ENTRY);
965 // Preprocessor Block.
966 BLOCK(PREPROCESSOR_BLOCK);
967 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
968 RECORD(PP_MACRO_FUNCTION_LIKE);
969 RECORD(PP_MACRO_OBJECT_LIKE);
970 RECORD(PP_MODULE_MACRO);
971 RECORD(PP_TOKEN);
973 // Submodule Block.
974 BLOCK(SUBMODULE_BLOCK);
975 RECORD(SUBMODULE_METADATA);
976 RECORD(SUBMODULE_DEFINITION);
977 RECORD(SUBMODULE_UMBRELLA_HEADER);
978 RECORD(SUBMODULE_HEADER);
979 RECORD(SUBMODULE_TOPHEADER);
980 RECORD(SUBMODULE_UMBRELLA_DIR);
981 RECORD(SUBMODULE_IMPORTS);
982 RECORD(SUBMODULE_AFFECTING_MODULES);
983 RECORD(SUBMODULE_EXPORTS);
984 RECORD(SUBMODULE_REQUIRES);
985 RECORD(SUBMODULE_EXCLUDED_HEADER);
986 RECORD(SUBMODULE_LINK_LIBRARY);
987 RECORD(SUBMODULE_CONFIG_MACRO);
988 RECORD(SUBMODULE_CONFLICT);
989 RECORD(SUBMODULE_PRIVATE_HEADER);
990 RECORD(SUBMODULE_TEXTUAL_HEADER);
991 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
992 RECORD(SUBMODULE_INITIALIZERS);
993 RECORD(SUBMODULE_EXPORT_AS);
995 // Comments Block.
996 BLOCK(COMMENTS_BLOCK);
997 RECORD(COMMENTS_RAW_COMMENT);
999 // Decls and Types block.
1000 BLOCK(DECLTYPES_BLOCK);
1001 RECORD(TYPE_EXT_QUAL);
1002 RECORD(TYPE_COMPLEX);
1003 RECORD(TYPE_POINTER);
1004 RECORD(TYPE_BLOCK_POINTER);
1005 RECORD(TYPE_LVALUE_REFERENCE);
1006 RECORD(TYPE_RVALUE_REFERENCE);
1007 RECORD(TYPE_MEMBER_POINTER);
1008 RECORD(TYPE_CONSTANT_ARRAY);
1009 RECORD(TYPE_INCOMPLETE_ARRAY);
1010 RECORD(TYPE_VARIABLE_ARRAY);
1011 RECORD(TYPE_VECTOR);
1012 RECORD(TYPE_EXT_VECTOR);
1013 RECORD(TYPE_FUNCTION_NO_PROTO);
1014 RECORD(TYPE_FUNCTION_PROTO);
1015 RECORD(TYPE_TYPEDEF);
1016 RECORD(TYPE_TYPEOF_EXPR);
1017 RECORD(TYPE_TYPEOF);
1018 RECORD(TYPE_RECORD);
1019 RECORD(TYPE_ENUM);
1020 RECORD(TYPE_OBJC_INTERFACE);
1021 RECORD(TYPE_OBJC_OBJECT_POINTER);
1022 RECORD(TYPE_DECLTYPE);
1023 RECORD(TYPE_ELABORATED);
1024 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1025 RECORD(TYPE_UNRESOLVED_USING);
1026 RECORD(TYPE_INJECTED_CLASS_NAME);
1027 RECORD(TYPE_OBJC_OBJECT);
1028 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1029 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1030 RECORD(TYPE_DEPENDENT_NAME);
1031 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1032 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1033 RECORD(TYPE_PAREN);
1034 RECORD(TYPE_MACRO_QUALIFIED);
1035 RECORD(TYPE_PACK_EXPANSION);
1036 RECORD(TYPE_ATTRIBUTED);
1037 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
1038 RECORD(TYPE_AUTO);
1039 RECORD(TYPE_UNARY_TRANSFORM);
1040 RECORD(TYPE_ATOMIC);
1041 RECORD(TYPE_DECAYED);
1042 RECORD(TYPE_ADJUSTED);
1043 RECORD(TYPE_OBJC_TYPE_PARAM);
1044 RECORD(LOCAL_REDECLARATIONS);
1045 RECORD(DECL_TYPEDEF);
1046 RECORD(DECL_TYPEALIAS);
1047 RECORD(DECL_ENUM);
1048 RECORD(DECL_RECORD);
1049 RECORD(DECL_ENUM_CONSTANT);
1050 RECORD(DECL_FUNCTION);
1051 RECORD(DECL_OBJC_METHOD);
1052 RECORD(DECL_OBJC_INTERFACE);
1053 RECORD(DECL_OBJC_PROTOCOL);
1054 RECORD(DECL_OBJC_IVAR);
1055 RECORD(DECL_OBJC_AT_DEFS_FIELD);
1056 RECORD(DECL_OBJC_CATEGORY);
1057 RECORD(DECL_OBJC_CATEGORY_IMPL);
1058 RECORD(DECL_OBJC_IMPLEMENTATION);
1059 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1060 RECORD(DECL_OBJC_PROPERTY);
1061 RECORD(DECL_OBJC_PROPERTY_IMPL);
1062 RECORD(DECL_FIELD);
1063 RECORD(DECL_MS_PROPERTY);
1064 RECORD(DECL_VAR);
1065 RECORD(DECL_IMPLICIT_PARAM);
1066 RECORD(DECL_PARM_VAR);
1067 RECORD(DECL_FILE_SCOPE_ASM);
1068 RECORD(DECL_BLOCK);
1069 RECORD(DECL_CONTEXT_LEXICAL);
1070 RECORD(DECL_CONTEXT_VISIBLE);
1071 RECORD(DECL_NAMESPACE);
1072 RECORD(DECL_NAMESPACE_ALIAS);
1073 RECORD(DECL_USING);
1074 RECORD(DECL_USING_SHADOW);
1075 RECORD(DECL_USING_DIRECTIVE);
1076 RECORD(DECL_UNRESOLVED_USING_VALUE);
1077 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1078 RECORD(DECL_LINKAGE_SPEC);
1079 RECORD(DECL_EXPORT);
1080 RECORD(DECL_CXX_RECORD);
1081 RECORD(DECL_CXX_METHOD);
1082 RECORD(DECL_CXX_CONSTRUCTOR);
1083 RECORD(DECL_CXX_DESTRUCTOR);
1084 RECORD(DECL_CXX_CONVERSION);
1085 RECORD(DECL_ACCESS_SPEC);
1086 RECORD(DECL_FRIEND);
1087 RECORD(DECL_FRIEND_TEMPLATE);
1088 RECORD(DECL_CLASS_TEMPLATE);
1089 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1090 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
1091 RECORD(DECL_VAR_TEMPLATE);
1092 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1093 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
1094 RECORD(DECL_FUNCTION_TEMPLATE);
1095 RECORD(DECL_TEMPLATE_TYPE_PARM);
1096 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1097 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1098 RECORD(DECL_CONCEPT);
1099 RECORD(DECL_REQUIRES_EXPR_BODY);
1100 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
1101 RECORD(DECL_STATIC_ASSERT);
1102 RECORD(DECL_CXX_BASE_SPECIFIERS);
1103 RECORD(DECL_CXX_CTOR_INITIALIZERS);
1104 RECORD(DECL_INDIRECTFIELD);
1105 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1106 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1107 RECORD(DECL_IMPORT);
1108 RECORD(DECL_OMP_THREADPRIVATE);
1109 RECORD(DECL_EMPTY);
1110 RECORD(DECL_OBJC_TYPE_PARAM);
1111 RECORD(DECL_OMP_CAPTUREDEXPR);
1112 RECORD(DECL_PRAGMA_COMMENT);
1113 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1114 RECORD(DECL_OMP_DECLARE_REDUCTION);
1115 RECORD(DECL_OMP_ALLOCATE);
1116 RECORD(DECL_HLSL_BUFFER);
1118 // Statements and Exprs can occur in the Decls and Types block.
1119 AddStmtsExprs(Stream, Record);
1121 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1122 RECORD(PPD_MACRO_EXPANSION);
1123 RECORD(PPD_MACRO_DEFINITION);
1124 RECORD(PPD_INCLUSION_DIRECTIVE);
1126 // Decls and Types block.
1127 BLOCK(EXTENSION_BLOCK);
1128 RECORD(EXTENSION_METADATA);
1130 BLOCK(UNHASHED_CONTROL_BLOCK);
1131 RECORD(SIGNATURE);
1132 RECORD(AST_BLOCK_HASH);
1133 RECORD(DIAGNOSTIC_OPTIONS);
1134 RECORD(HEADER_SEARCH_PATHS);
1135 RECORD(DIAG_PRAGMA_MAPPINGS);
1137 #undef RECORD
1138 #undef BLOCK
1139 Stream.ExitBlock();
1142 /// Prepares a path for being written to an AST file by converting it
1143 /// to an absolute path and removing nested './'s.
1145 /// \return \c true if the path was changed.
1146 static bool cleanPathForOutput(FileManager &FileMgr,
1147 SmallVectorImpl<char> &Path) {
1148 bool Changed = FileMgr.makeAbsolutePath(Path);
1149 return Changed | llvm::sys::path::remove_dots(Path);
1152 /// Adjusts the given filename to only write out the portion of the
1153 /// filename that is not part of the system root directory.
1155 /// \param Filename the file name to adjust.
1157 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1158 /// the returned filename will be adjusted by this root directory.
1160 /// \returns either the original filename (if it needs no adjustment) or the
1161 /// adjusted filename (which points into the @p Filename parameter).
1162 static const char *
1163 adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1164 assert(Filename && "No file name to adjust?");
1166 if (BaseDir.empty())
1167 return Filename;
1169 // Verify that the filename and the system root have the same prefix.
1170 unsigned Pos = 0;
1171 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1172 if (Filename[Pos] != BaseDir[Pos])
1173 return Filename; // Prefixes don't match.
1175 // We hit the end of the filename before we hit the end of the system root.
1176 if (!Filename[Pos])
1177 return Filename;
1179 // If there's not a path separator at the end of the base directory nor
1180 // immediately after it, then this isn't within the base directory.
1181 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1182 if (!llvm::sys::path::is_separator(BaseDir.back()))
1183 return Filename;
1184 } else {
1185 // If the file name has a '/' at the current position, skip over the '/'.
1186 // We distinguish relative paths from absolute paths by the
1187 // absence of '/' at the beginning of relative paths.
1189 // FIXME: This is wrong. We distinguish them by asking if the path is
1190 // absolute, which isn't the same thing. And there might be multiple '/'s
1191 // in a row. Use a better mechanism to indicate whether we have emitted an
1192 // absolute or relative path.
1193 ++Pos;
1196 return Filename + Pos;
1199 std::pair<ASTFileSignature, ASTFileSignature>
1200 ASTWriter::createSignature() const {
1201 StringRef AllBytes(Buffer.data(), Buffer.size());
1203 llvm::SHA1 Hasher;
1204 Hasher.update(AllBytes.slice(ASTBlockRange.first, ASTBlockRange.second));
1205 ASTFileSignature ASTBlockHash = ASTFileSignature::create(Hasher.result());
1207 // Add the remaining bytes:
1208 // 1. Before the unhashed control block.
1209 Hasher.update(AllBytes.slice(0, UnhashedControlBlockRange.first));
1210 // 2. Between the unhashed control block and the AST block.
1211 Hasher.update(
1212 AllBytes.slice(UnhashedControlBlockRange.second, ASTBlockRange.first));
1213 // 3. After the AST block.
1214 Hasher.update(AllBytes.slice(ASTBlockRange.second, StringRef::npos));
1215 ASTFileSignature Signature = ASTFileSignature::create(Hasher.result());
1217 return std::make_pair(ASTBlockHash, Signature);
1220 ASTFileSignature ASTWriter::createSignatureForNamedModule() const {
1221 llvm::SHA1 Hasher;
1222 Hasher.update(StringRef(Buffer.data(), Buffer.size()));
1224 assert(WritingModule);
1225 assert(WritingModule->isNamedModule());
1227 // We need to combine all the export imported modules no matter
1228 // we used it or not.
1229 for (auto [ExportImported, _] : WritingModule->Exports)
1230 Hasher.update(ExportImported->Signature);
1232 // We combine all the used modules to make sure the signature is precise.
1233 // Consider the case like:
1235 // // a.cppm
1236 // export module a;
1237 // export inline int a() { ... }
1239 // // b.cppm
1240 // export module b;
1241 // import a;
1242 // export inline int b() { return a(); }
1244 // Since both `a()` and `b()` are inline, we need to make sure the BMI of
1245 // `b.pcm` will change after the implementation of `a()` changes. We can't
1246 // get that naturally since we won't record the body of `a()` during the
1247 // writing process. We can't reuse ODRHash here since ODRHash won't calculate
1248 // the called function recursively. So ODRHash will be problematic if `a()`
1249 // calls other inline functions.
1251 // Probably we can solve this by a new hash mechanism. But the safety and
1252 // efficiency may a problem too. Here we just combine the hash value of the
1253 // used modules conservatively.
1254 for (Module *M : TouchedTopLevelModules)
1255 Hasher.update(M->Signature);
1257 return ASTFileSignature::create(Hasher.result());
1260 static void BackpatchSignatureAt(llvm::BitstreamWriter &Stream,
1261 const ASTFileSignature &S, uint64_t BitNo) {
1262 for (uint8_t Byte : S) {
1263 Stream.BackpatchByte(BitNo, Byte);
1264 BitNo += 8;
1268 ASTFileSignature ASTWriter::backpatchSignature() {
1269 if (isWritingStdCXXNamedModules()) {
1270 ASTFileSignature Signature = createSignatureForNamedModule();
1271 BackpatchSignatureAt(Stream, Signature, SignatureOffset);
1272 return Signature;
1275 if (!WritingModule ||
1276 !PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent)
1277 return {};
1279 // For implicit modules, write the hash of the PCM as its signature.
1280 ASTFileSignature ASTBlockHash;
1281 ASTFileSignature Signature;
1282 std::tie(ASTBlockHash, Signature) = createSignature();
1284 BackpatchSignatureAt(Stream, ASTBlockHash, ASTBlockHashOffset);
1285 BackpatchSignatureAt(Stream, Signature, SignatureOffset);
1287 return Signature;
1290 void ASTWriter::writeUnhashedControlBlock(Preprocessor &PP) {
1291 using namespace llvm;
1293 // Flush first to prepare the PCM hash (signature).
1294 Stream.FlushToWord();
1295 UnhashedControlBlockRange.first = Stream.GetCurrentBitNo() >> 3;
1297 // Enter the block and prepare to write records.
1298 RecordData Record;
1299 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1301 // For implicit modules and C++20 named modules, write the hash of the PCM as
1302 // its signature.
1303 if (isWritingStdCXXNamedModules() ||
1304 (WritingModule &&
1305 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent)) {
1306 // At this point, we don't know the actual signature of the file or the AST
1307 // block - we're only able to compute those at the end of the serialization
1308 // process. Let's store dummy signatures for now, and replace them with the
1309 // real ones later on.
1310 // The bitstream VBR-encodes record elements, which makes backpatching them
1311 // really difficult. Let's store the signatures as blobs instead - they are
1312 // guaranteed to be word-aligned, and we control their format/encoding.
1313 auto Dummy = ASTFileSignature::createDummy();
1314 SmallString<128> Blob{Dummy.begin(), Dummy.end()};
1316 // We don't need AST Block hash in named modules.
1317 if (!isWritingStdCXXNamedModules()) {
1318 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1319 Abbrev->Add(BitCodeAbbrevOp(AST_BLOCK_HASH));
1320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1321 unsigned ASTBlockHashAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1323 Record.push_back(AST_BLOCK_HASH);
1324 Stream.EmitRecordWithBlob(ASTBlockHashAbbrev, Record, Blob);
1325 ASTBlockHashOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1326 Record.clear();
1329 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1330 Abbrev->Add(BitCodeAbbrevOp(SIGNATURE));
1331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1332 unsigned SignatureAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1334 Record.push_back(SIGNATURE);
1335 Stream.EmitRecordWithBlob(SignatureAbbrev, Record, Blob);
1336 SignatureOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1337 Record.clear();
1340 const auto &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1342 // Diagnostic options.
1343 const auto &Diags = PP.getDiagnostics();
1344 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1345 if (!HSOpts.ModulesSkipDiagnosticOptions) {
1346 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1347 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1348 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1349 #include "clang/Basic/DiagnosticOptions.def"
1350 Record.push_back(DiagOpts.Warnings.size());
1351 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1352 AddString(DiagOpts.Warnings[I], Record);
1353 Record.push_back(DiagOpts.Remarks.size());
1354 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1355 AddString(DiagOpts.Remarks[I], Record);
1356 // Note: we don't serialize the log or serialization file names, because
1357 // they are generally transient files and will almost always be overridden.
1358 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1359 Record.clear();
1362 // Header search paths.
1363 if (!HSOpts.ModulesSkipHeaderSearchPaths) {
1364 // Include entries.
1365 Record.push_back(HSOpts.UserEntries.size());
1366 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1367 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1368 AddString(Entry.Path, Record);
1369 Record.push_back(static_cast<unsigned>(Entry.Group));
1370 Record.push_back(Entry.IsFramework);
1371 Record.push_back(Entry.IgnoreSysRoot);
1374 // System header prefixes.
1375 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1376 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1377 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1378 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1381 // VFS overlay files.
1382 Record.push_back(HSOpts.VFSOverlayFiles.size());
1383 for (StringRef VFSOverlayFile : HSOpts.VFSOverlayFiles)
1384 AddString(VFSOverlayFile, Record);
1386 Stream.EmitRecord(HEADER_SEARCH_PATHS, Record);
1389 if (!HSOpts.ModulesSkipPragmaDiagnosticMappings)
1390 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1392 // Header search entry usage.
1394 auto HSEntryUsage = PP.getHeaderSearchInfo().computeUserEntryUsage();
1395 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1396 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1399 unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1400 RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1401 HSEntryUsage.size()};
1402 Stream.EmitRecordWithBlob(HSUsageAbbrevCode, Record, bytes(HSEntryUsage));
1405 // VFS usage.
1407 auto VFSUsage = PP.getHeaderSearchInfo().collectVFSUsageAndClear();
1408 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1409 Abbrev->Add(BitCodeAbbrevOp(VFS_USAGE));
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1412 unsigned VFSUsageAbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1413 RecordData::value_type Record[] = {VFS_USAGE, VFSUsage.size()};
1414 Stream.EmitRecordWithBlob(VFSUsageAbbrevCode, Record, bytes(VFSUsage));
1417 // Leave the options block.
1418 Stream.ExitBlock();
1419 UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
1422 /// Write the control block.
1423 void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) {
1424 using namespace llvm;
1426 SourceManager &SourceMgr = PP.getSourceManager();
1427 FileManager &FileMgr = PP.getFileManager();
1429 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1430 RecordData Record;
1432 // Metadata
1433 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1434 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1435 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1436 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1437 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1438 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1439 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1440 // Standard C++ module
1441 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1442 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1443 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1444 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1445 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1446 assert((!WritingModule || isysroot.empty()) &&
1447 "writing module as a relocatable PCH?");
1449 RecordData::value_type Record[] = {METADATA,
1450 VERSION_MAJOR,
1451 VERSION_MINOR,
1452 CLANG_VERSION_MAJOR,
1453 CLANG_VERSION_MINOR,
1454 !isysroot.empty(),
1455 isWritingStdCXXNamedModules(),
1456 IncludeTimestamps,
1457 ASTHasCompilerErrors};
1458 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1459 getClangFullRepositoryVersion());
1462 if (WritingModule) {
1463 // Module name
1464 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1465 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1466 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1467 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1468 RecordData::value_type Record[] = {MODULE_NAME};
1469 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1472 if (WritingModule && WritingModule->Directory) {
1473 SmallString<128> BaseDir;
1474 if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1475 // Use the current working directory as the base path for all inputs.
1476 auto CWD = FileMgr.getOptionalDirectoryRef(".");
1477 BaseDir.assign(CWD->getName());
1478 } else {
1479 BaseDir.assign(WritingModule->Directory->getName());
1481 cleanPathForOutput(FileMgr, BaseDir);
1483 // If the home of the module is the current working directory, then we
1484 // want to pick up the cwd of the build process loading the module, not
1485 // our cwd, when we load this module.
1486 if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1487 (!PP.getHeaderSearchInfo()
1488 .getHeaderSearchOpts()
1489 .ModuleMapFileHomeIsCwd ||
1490 WritingModule->Directory->getName() != ".")) {
1491 // Module directory.
1492 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1493 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1495 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1497 RecordData::value_type Record[] = {MODULE_DIRECTORY};
1498 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1501 // Write out all other paths relative to the base directory if possible.
1502 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1503 } else if (!isysroot.empty()) {
1504 // Write out paths relative to the sysroot if possible.
1505 BaseDirectory = std::string(isysroot);
1508 // Module map file
1509 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1510 Record.clear();
1512 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1513 AddPath(WritingModule->PresumedModuleMapFile.empty()
1514 ? Map.getModuleMapFileForUniquing(WritingModule)
1515 ->getNameAsRequested()
1516 : StringRef(WritingModule->PresumedModuleMapFile),
1517 Record);
1519 // Additional module map files.
1520 if (auto *AdditionalModMaps =
1521 Map.getAdditionalModuleMapFiles(WritingModule)) {
1522 Record.push_back(AdditionalModMaps->size());
1523 SmallVector<FileEntryRef, 1> ModMaps(AdditionalModMaps->begin(),
1524 AdditionalModMaps->end());
1525 llvm::sort(ModMaps, [](FileEntryRef A, FileEntryRef B) {
1526 return A.getName() < B.getName();
1528 for (FileEntryRef F : ModMaps)
1529 AddPath(F.getName(), Record);
1530 } else {
1531 Record.push_back(0);
1534 Stream.EmitRecord(MODULE_MAP_FILE, Record);
1537 // Imports
1538 if (Chain) {
1539 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1540 Abbrev->Add(BitCodeAbbrevOp(IMPORT));
1541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Kind
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ImportLoc
1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Module name len
1544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Standard C++ mod
1545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File size
1546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File timestamp
1547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File name len
1548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Strings
1549 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1551 SmallString<128> Blob;
1553 for (ModuleFile &M : Chain->getModuleManager()) {
1554 // Skip modules that weren't directly imported.
1555 if (!M.isDirectlyImported())
1556 continue;
1558 Record.clear();
1559 Blob.clear();
1561 Record.push_back(IMPORT);
1562 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1563 AddSourceLocation(M.ImportLoc, Record);
1564 AddStringBlob(M.ModuleName, Record, Blob);
1565 Record.push_back(M.StandardCXXModule);
1567 // We don't want to hard code the information about imported modules
1568 // in the C++20 named modules.
1569 if (M.StandardCXXModule) {
1570 Record.push_back(0);
1571 Record.push_back(0);
1572 Record.push_back(0);
1573 } else {
1574 // If we have calculated signature, there is no need to store
1575 // the size or timestamp.
1576 Record.push_back(M.Signature ? 0 : M.File.getSize());
1577 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1579 llvm::append_range(Blob, M.Signature);
1581 AddPathBlob(M.FileName, Record, Blob);
1584 Stream.EmitRecordWithBlob(AbbrevCode, Record, Blob);
1588 // Write the options block.
1589 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1591 // Language options.
1592 Record.clear();
1593 const LangOptions &LangOpts = PP.getLangOpts();
1594 #define LANGOPT(Name, Bits, Default, Description) \
1595 Record.push_back(LangOpts.Name);
1596 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1597 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1598 #include "clang/Basic/LangOptions.def"
1599 #define SANITIZER(NAME, ID) \
1600 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1601 #include "clang/Basic/Sanitizers.def"
1603 Record.push_back(LangOpts.ModuleFeatures.size());
1604 for (StringRef Feature : LangOpts.ModuleFeatures)
1605 AddString(Feature, Record);
1607 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1608 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1610 AddString(LangOpts.CurrentModule, Record);
1612 // Comment options.
1613 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1614 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1615 AddString(I, Record);
1617 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1619 // OpenMP offloading options.
1620 Record.push_back(LangOpts.OMPTargetTriples.size());
1621 for (auto &T : LangOpts.OMPTargetTriples)
1622 AddString(T.getTriple(), Record);
1624 AddString(LangOpts.OMPHostIRFile, Record);
1626 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1628 // Target options.
1629 Record.clear();
1630 const TargetInfo &Target = PP.getTargetInfo();
1631 const TargetOptions &TargetOpts = Target.getTargetOpts();
1632 AddString(TargetOpts.Triple, Record);
1633 AddString(TargetOpts.CPU, Record);
1634 AddString(TargetOpts.TuneCPU, Record);
1635 AddString(TargetOpts.ABI, Record);
1636 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1637 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1638 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1640 Record.push_back(TargetOpts.Features.size());
1641 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1642 AddString(TargetOpts.Features[I], Record);
1644 Stream.EmitRecord(TARGET_OPTIONS, Record);
1646 // File system options.
1647 Record.clear();
1648 const FileSystemOptions &FSOpts = FileMgr.getFileSystemOpts();
1649 AddString(FSOpts.WorkingDir, Record);
1650 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1652 // Header search options.
1653 Record.clear();
1654 const HeaderSearchOptions &HSOpts =
1655 PP.getHeaderSearchInfo().getHeaderSearchOpts();
1657 AddString(HSOpts.Sysroot, Record);
1658 AddString(HSOpts.ResourceDir, Record);
1659 AddString(HSOpts.ModuleCachePath, Record);
1660 AddString(HSOpts.ModuleUserBuildPath, Record);
1661 Record.push_back(HSOpts.DisableModuleHash);
1662 Record.push_back(HSOpts.ImplicitModuleMaps);
1663 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1664 Record.push_back(HSOpts.EnablePrebuiltImplicitModules);
1665 Record.push_back(HSOpts.UseBuiltinIncludes);
1666 Record.push_back(HSOpts.UseStandardSystemIncludes);
1667 Record.push_back(HSOpts.UseStandardCXXIncludes);
1668 Record.push_back(HSOpts.UseLibcxx);
1669 // Write out the specific module cache path that contains the module files.
1670 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1671 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1673 // Preprocessor options.
1674 Record.clear();
1675 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1677 // If we're building an implicit module with a context hash, the importer is
1678 // guaranteed to have the same macros defined on the command line. Skip
1679 // writing them.
1680 bool SkipMacros = BuildingImplicitModule && !HSOpts.DisableModuleHash;
1681 bool WriteMacros = !SkipMacros;
1682 Record.push_back(WriteMacros);
1683 if (WriteMacros) {
1684 // Macro definitions.
1685 Record.push_back(PPOpts.Macros.size());
1686 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1687 AddString(PPOpts.Macros[I].first, Record);
1688 Record.push_back(PPOpts.Macros[I].second);
1692 // Includes
1693 Record.push_back(PPOpts.Includes.size());
1694 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1695 AddString(PPOpts.Includes[I], Record);
1697 // Macro includes
1698 Record.push_back(PPOpts.MacroIncludes.size());
1699 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1700 AddString(PPOpts.MacroIncludes[I], Record);
1702 Record.push_back(PPOpts.UsePredefines);
1703 // Detailed record is important since it is used for the module cache hash.
1704 Record.push_back(PPOpts.DetailedRecord);
1705 AddString(PPOpts.ImplicitPCHInclude, Record);
1706 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1707 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1709 // Leave the options block.
1710 Stream.ExitBlock();
1712 // Original file name and file ID
1713 if (auto MainFile =
1714 SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())) {
1715 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1716 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1717 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1718 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1719 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1721 Record.clear();
1722 Record.push_back(ORIGINAL_FILE);
1723 AddFileID(SourceMgr.getMainFileID(), Record);
1724 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1727 Record.clear();
1728 AddFileID(SourceMgr.getMainFileID(), Record);
1729 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1731 WriteInputFiles(SourceMgr, PP.getHeaderSearchInfo().getHeaderSearchOpts());
1732 Stream.ExitBlock();
1735 namespace {
1737 /// An input file.
1738 struct InputFileEntry {
1739 FileEntryRef File;
1740 bool IsSystemFile;
1741 bool IsTransient;
1742 bool BufferOverridden;
1743 bool IsTopLevel;
1744 bool IsModuleMap;
1745 uint32_t ContentHash[2];
1747 InputFileEntry(FileEntryRef File) : File(File) {}
1750 } // namespace
1752 SourceLocation ASTWriter::getAffectingIncludeLoc(const SourceManager &SourceMgr,
1753 const SrcMgr::FileInfo &File) {
1754 SourceLocation IncludeLoc = File.getIncludeLoc();
1755 if (IncludeLoc.isValid()) {
1756 FileID IncludeFID = SourceMgr.getFileID(IncludeLoc);
1757 assert(IncludeFID.isValid() && "IncludeLoc in invalid file");
1758 if (!IsSLocAffecting[IncludeFID.ID])
1759 IncludeLoc = SourceLocation();
1761 return IncludeLoc;
1764 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1765 HeaderSearchOptions &HSOpts) {
1766 using namespace llvm;
1768 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1770 // Create input-file abbreviation.
1771 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1772 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1773 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1774 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1775 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1776 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1777 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1778 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1779 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1780 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1781 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
1782 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1784 // Create input file hash abbreviation.
1785 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1786 IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1787 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1788 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1789 unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1791 uint64_t InputFilesOffsetBase = Stream.GetCurrentBitNo();
1793 // Get all ContentCache objects for files.
1794 std::vector<InputFileEntry> UserFiles;
1795 std::vector<InputFileEntry> SystemFiles;
1796 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1797 // Get this source location entry.
1798 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1799 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1801 // We only care about file entries that were not overridden.
1802 if (!SLoc->isFile())
1803 continue;
1804 const SrcMgr::FileInfo &File = SLoc->getFile();
1805 const SrcMgr::ContentCache *Cache = &File.getContentCache();
1806 if (!Cache->OrigEntry)
1807 continue;
1809 // Do not emit input files that do not affect current module.
1810 if (!IsSLocFileEntryAffecting[I])
1811 continue;
1813 InputFileEntry Entry(*Cache->OrigEntry);
1814 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1815 Entry.IsTransient = Cache->IsTransient;
1816 Entry.BufferOverridden = Cache->BufferOverridden;
1818 FileID IncludeFileID = SourceMgr.getFileID(File.getIncludeLoc());
1819 Entry.IsTopLevel = IncludeFileID.isInvalid() || IncludeFileID.ID < 0 ||
1820 !IsSLocFileEntryAffecting[IncludeFileID.ID];
1821 Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
1823 uint64_t ContentHash = 0;
1824 if (PP->getHeaderSearchInfo()
1825 .getHeaderSearchOpts()
1826 .ValidateASTInputFilesContent) {
1827 auto MemBuff = Cache->getBufferIfLoaded();
1828 if (MemBuff)
1829 ContentHash = xxh3_64bits(MemBuff->getBuffer());
1830 else
1831 PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1832 << Entry.File.getName();
1834 Entry.ContentHash[0] = uint32_t(ContentHash);
1835 Entry.ContentHash[1] = uint32_t(ContentHash >> 32);
1836 if (Entry.IsSystemFile)
1837 SystemFiles.push_back(Entry);
1838 else
1839 UserFiles.push_back(Entry);
1842 // User files go at the front, system files at the back.
1843 auto SortedFiles = llvm::concat<InputFileEntry>(std::move(UserFiles),
1844 std::move(SystemFiles));
1846 unsigned UserFilesNum = 0;
1847 // Write out all of the input files.
1848 std::vector<uint64_t> InputFileOffsets;
1849 for (const auto &Entry : SortedFiles) {
1850 uint32_t &InputFileID = InputFileIDs[Entry.File];
1851 if (InputFileID != 0)
1852 continue; // already recorded this file.
1854 // Record this entry's offset.
1855 InputFileOffsets.push_back(Stream.GetCurrentBitNo() - InputFilesOffsetBase);
1857 InputFileID = InputFileOffsets.size();
1859 if (!Entry.IsSystemFile)
1860 ++UserFilesNum;
1862 // Emit size/modification time for this file.
1863 // And whether this file was overridden.
1865 SmallString<128> NameAsRequested = Entry.File.getNameAsRequested();
1866 SmallString<128> Name = Entry.File.getName();
1868 PreparePathForOutput(NameAsRequested);
1869 PreparePathForOutput(Name);
1871 if (Name == NameAsRequested)
1872 Name.clear();
1874 RecordData::value_type Record[] = {
1875 INPUT_FILE,
1876 InputFileOffsets.size(),
1877 (uint64_t)Entry.File.getSize(),
1878 (uint64_t)getTimestampForOutput(Entry.File),
1879 Entry.BufferOverridden,
1880 Entry.IsTransient,
1881 Entry.IsTopLevel,
1882 Entry.IsModuleMap,
1883 NameAsRequested.size()};
1885 Stream.EmitRecordWithBlob(IFAbbrevCode, Record,
1886 (NameAsRequested + Name).str());
1889 // Emit content hash for this file.
1891 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1892 Entry.ContentHash[1]};
1893 Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1897 Stream.ExitBlock();
1899 // Create input file offsets abbreviation.
1900 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1901 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1902 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1903 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1904 // input files
1905 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1906 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1908 // Write input file offsets.
1909 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1910 InputFileOffsets.size(), UserFilesNum};
1911 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1914 //===----------------------------------------------------------------------===//
1915 // Source Manager Serialization
1916 //===----------------------------------------------------------------------===//
1918 /// Create an abbreviation for the SLocEntry that refers to a
1919 /// file.
1920 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1921 using namespace llvm;
1923 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1924 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1929 // FileEntry fields.
1930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1934 return Stream.EmitAbbrev(std::move(Abbrev));
1937 /// Create an abbreviation for the SLocEntry that refers to a
1938 /// buffer.
1939 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1940 using namespace llvm;
1942 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1943 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1949 return Stream.EmitAbbrev(std::move(Abbrev));
1952 /// Create an abbreviation for the SLocEntry that refers to a
1953 /// buffer's blob.
1954 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1955 bool Compressed) {
1956 using namespace llvm;
1958 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1959 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1960 : SM_SLOC_BUFFER_BLOB));
1961 if (Compressed)
1962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1964 return Stream.EmitAbbrev(std::move(Abbrev));
1967 /// Create an abbreviation for the SLocEntry that refers to a macro
1968 /// expansion.
1969 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1970 using namespace llvm;
1972 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1973 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
1977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
1978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1980 return Stream.EmitAbbrev(std::move(Abbrev));
1983 /// Emit key length and data length as ULEB-encoded data, and return them as a
1984 /// pair.
1985 static std::pair<unsigned, unsigned>
1986 emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
1987 llvm::encodeULEB128(KeyLen, Out);
1988 llvm::encodeULEB128(DataLen, Out);
1989 return std::make_pair(KeyLen, DataLen);
1992 namespace {
1994 // Trait used for the on-disk hash table of header search information.
1995 class HeaderFileInfoTrait {
1996 ASTWriter &Writer;
1998 public:
1999 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
2001 struct key_type {
2002 StringRef Filename;
2003 off_t Size;
2004 time_t ModTime;
2006 using key_type_ref = const key_type &;
2008 using UnresolvedModule =
2009 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
2011 struct data_type {
2012 data_type(const HeaderFileInfo &HFI, bool AlreadyIncluded,
2013 ArrayRef<ModuleMap::KnownHeader> KnownHeaders,
2014 UnresolvedModule Unresolved)
2015 : HFI(HFI), AlreadyIncluded(AlreadyIncluded),
2016 KnownHeaders(KnownHeaders), Unresolved(Unresolved) {}
2018 HeaderFileInfo HFI;
2019 bool AlreadyIncluded;
2020 SmallVector<ModuleMap::KnownHeader, 1> KnownHeaders;
2021 UnresolvedModule Unresolved;
2023 using data_type_ref = const data_type &;
2025 using hash_value_type = unsigned;
2026 using offset_type = unsigned;
2028 hash_value_type ComputeHash(key_type_ref key) {
2029 // The hash is based only on size/time of the file, so that the reader can
2030 // match even when symlinking or excess path elements ("foo/../", "../")
2031 // change the form of the name. However, complete path is still the key.
2032 uint8_t buf[sizeof(key.Size) + sizeof(key.ModTime)];
2033 memcpy(buf, &key.Size, sizeof(key.Size));
2034 memcpy(buf + sizeof(key.Size), &key.ModTime, sizeof(key.ModTime));
2035 return llvm::xxh3_64bits(buf);
2038 std::pair<unsigned, unsigned>
2039 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
2040 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
2041 unsigned DataLen = 1 + sizeof(IdentifierID);
2042 for (auto ModInfo : Data.KnownHeaders)
2043 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
2044 DataLen += 4;
2045 if (Data.Unresolved.getPointer())
2046 DataLen += 4;
2047 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
2050 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
2051 using namespace llvm::support;
2053 endian::Writer LE(Out, llvm::endianness::little);
2054 LE.write<uint64_t>(key.Size);
2055 KeyLen -= 8;
2056 LE.write<uint64_t>(key.ModTime);
2057 KeyLen -= 8;
2058 Out.write(key.Filename.data(), KeyLen);
2061 void EmitData(raw_ostream &Out, key_type_ref key,
2062 data_type_ref Data, unsigned DataLen) {
2063 using namespace llvm::support;
2065 endian::Writer LE(Out, llvm::endianness::little);
2066 uint64_t Start = Out.tell(); (void)Start;
2068 unsigned char Flags = (Data.AlreadyIncluded << 6)
2069 | (Data.HFI.isImport << 5)
2070 | (Writer.isWritingStdCXXNamedModules() ? 0 :
2071 Data.HFI.isPragmaOnce << 4)
2072 | (Data.HFI.DirInfo << 1);
2073 LE.write<uint8_t>(Flags);
2075 if (Data.HFI.LazyControllingMacro.isID())
2076 LE.write<IdentifierID>(Data.HFI.LazyControllingMacro.getID());
2077 else
2078 LE.write<IdentifierID>(
2079 Writer.getIdentifierRef(Data.HFI.LazyControllingMacro.getPtr()));
2081 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
2082 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
2083 uint32_t Value = (ModID << 3) | (unsigned)Role;
2084 assert((Value >> 3) == ModID && "overflow in header module info");
2085 LE.write<uint32_t>(Value);
2089 for (auto ModInfo : Data.KnownHeaders)
2090 EmitModule(ModInfo.getModule(), ModInfo.getRole());
2091 if (Data.Unresolved.getPointer())
2092 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
2094 assert(Out.tell() - Start == DataLen && "Wrong data length");
2098 } // namespace
2100 /// Write the header search block for the list of files that
2102 /// \param HS The header search structure to save.
2103 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
2104 HeaderFileInfoTrait GeneratorTrait(*this);
2105 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
2106 SmallVector<const char *, 4> SavedStrings;
2107 unsigned NumHeaderSearchEntries = 0;
2109 // Find all unresolved headers for the current module. We generally will
2110 // have resolved them before we get here, but not necessarily: we might be
2111 // compiling a preprocessed module, where there is no requirement for the
2112 // original files to exist any more.
2113 const HeaderFileInfo Empty; // So we can take a reference.
2114 if (WritingModule) {
2115 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
2116 while (!Worklist.empty()) {
2117 Module *M = Worklist.pop_back_val();
2118 // We don't care about headers in unimportable submodules.
2119 if (M->isUnimportable())
2120 continue;
2122 // Map to disk files where possible, to pick up any missing stat
2123 // information. This also means we don't need to check the unresolved
2124 // headers list when emitting resolved headers in the first loop below.
2125 // FIXME: It'd be preferable to avoid doing this if we were given
2126 // sufficient stat information in the module map.
2127 HS.getModuleMap().resolveHeaderDirectives(M, /*File=*/std::nullopt);
2129 // If the file didn't exist, we can still create a module if we were given
2130 // enough information in the module map.
2131 for (const auto &U : M->MissingHeaders) {
2132 // Check that we were given enough information to build a module
2133 // without this file existing on disk.
2134 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2135 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
2136 << WritingModule->getFullModuleName() << U.Size.has_value()
2137 << U.FileName;
2138 continue;
2141 // Form the effective relative pathname for the file.
2142 SmallString<128> Filename(M->Directory->getName());
2143 llvm::sys::path::append(Filename, U.FileName);
2144 PreparePathForOutput(Filename);
2146 StringRef FilenameDup = strdup(Filename.c_str());
2147 SavedStrings.push_back(FilenameDup.data());
2149 HeaderFileInfoTrait::key_type Key = {
2150 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0};
2151 HeaderFileInfoTrait::data_type Data = {
2152 Empty, false, {}, {M, ModuleMap::headerKindToRole(U.Kind)}};
2153 // FIXME: Deal with cases where there are multiple unresolved header
2154 // directives in different submodules for the same header.
2155 Generator.insert(Key, Data, GeneratorTrait);
2156 ++NumHeaderSearchEntries;
2158 auto SubmodulesRange = M->submodules();
2159 Worklist.append(SubmodulesRange.begin(), SubmodulesRange.end());
2163 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
2164 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
2166 if (FilesByUID.size() > HS.header_file_size())
2167 FilesByUID.resize(HS.header_file_size());
2169 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2170 OptionalFileEntryRef File = FilesByUID[UID];
2171 if (!File)
2172 continue;
2174 const HeaderFileInfo *HFI = HS.getExistingLocalFileInfo(*File);
2175 if (!HFI)
2176 continue; // We have no information on this being a header file.
2177 if (!HFI->isCompilingModuleHeader && HFI->isModuleHeader)
2178 continue; // Header file info is tracked by the owning module file.
2179 if (!HFI->isCompilingModuleHeader && !HFI->IsLocallyIncluded)
2180 continue; // Header file info is tracked by the including module file.
2182 // Massage the file path into an appropriate form.
2183 StringRef Filename = File->getName();
2184 SmallString<128> FilenameTmp(Filename);
2185 if (PreparePathForOutput(FilenameTmp)) {
2186 // If we performed any translation on the file name at all, we need to
2187 // save this string, since the generator will refer to it later.
2188 Filename = StringRef(strdup(FilenameTmp.c_str()));
2189 SavedStrings.push_back(Filename.data());
2192 bool Included = HFI->IsLocallyIncluded || PP->alreadyIncluded(*File);
2194 HeaderFileInfoTrait::key_type Key = {
2195 Filename, File->getSize(), getTimestampForOutput(*File)
2197 HeaderFileInfoTrait::data_type Data = {
2198 *HFI, Included, HS.getModuleMap().findResolvedModulesForHeader(*File), {}
2200 Generator.insert(Key, Data, GeneratorTrait);
2201 ++NumHeaderSearchEntries;
2204 // Create the on-disk hash table in a buffer.
2205 SmallString<4096> TableData;
2206 uint32_t BucketOffset;
2208 using namespace llvm::support;
2210 llvm::raw_svector_ostream Out(TableData);
2211 // Make sure that no bucket is at offset 0
2212 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
2213 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2216 // Create a blob abbreviation
2217 using namespace llvm;
2219 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2220 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2225 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2227 // Write the header search table
2228 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2229 NumHeaderSearchEntries, TableData.size()};
2230 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
2232 // Free all of the strings we had to duplicate.
2233 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
2234 free(const_cast<char *>(SavedStrings[I]));
2237 static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2238 unsigned SLocBufferBlobCompressedAbbrv,
2239 unsigned SLocBufferBlobAbbrv) {
2240 using RecordDataType = ASTWriter::RecordData::value_type;
2242 // Compress the buffer if possible. We expect that almost all PCM
2243 // consumers will not want its contents.
2244 SmallVector<uint8_t, 0> CompressedBuffer;
2245 if (llvm::compression::zstd::isAvailable()) {
2246 llvm::compression::zstd::compress(
2247 llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer, 9);
2248 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2249 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2250 llvm::toStringRef(CompressedBuffer));
2251 return;
2253 if (llvm::compression::zlib::isAvailable()) {
2254 llvm::compression::zlib::compress(
2255 llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer);
2256 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2257 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2258 llvm::toStringRef(CompressedBuffer));
2259 return;
2262 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2263 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2266 /// Writes the block containing the serialized form of the
2267 /// source manager.
2269 /// TODO: We should probably use an on-disk hash table (stored in a
2270 /// blob), indexed based on the file name, so that we only create
2271 /// entries for files that we actually need. In the common case (no
2272 /// errors), we probably won't have to create file entries for any of
2273 /// the files in the AST.
2274 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
2275 RecordData Record;
2277 // Enter the source manager block.
2278 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
2279 const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
2281 // Abbreviations for the various kinds of source-location entries.
2282 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2283 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
2284 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2285 unsigned SLocBufferBlobCompressedAbbrv =
2286 CreateSLocBufferBlobAbbrev(Stream, true);
2287 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
2289 // Write out the source location entry table. We skip the first
2290 // entry, which is always the same dummy entry.
2291 std::vector<uint32_t> SLocEntryOffsets;
2292 uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
2293 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2294 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
2295 I != N; ++I) {
2296 // Get this source location entry.
2297 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
2298 FileID FID = FileID::get(I);
2299 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
2301 // Record the offset of this source-location entry.
2302 uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
2303 assert((Offset >> 32) == 0 && "SLocEntry offset too large");
2305 // Figure out which record code to use.
2306 unsigned Code;
2307 if (SLoc->isFile()) {
2308 const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
2309 if (Cache->OrigEntry) {
2310 Code = SM_SLOC_FILE_ENTRY;
2311 } else
2312 Code = SM_SLOC_BUFFER_ENTRY;
2313 } else
2314 Code = SM_SLOC_EXPANSION_ENTRY;
2315 Record.clear();
2316 Record.push_back(Code);
2318 if (SLoc->isFile()) {
2319 const SrcMgr::FileInfo &File = SLoc->getFile();
2320 const SrcMgr::ContentCache *Content = &File.getContentCache();
2321 // Do not emit files that were not listed as inputs.
2322 if (!IsSLocAffecting[I])
2323 continue;
2324 SLocEntryOffsets.push_back(Offset);
2325 // Starting offset of this entry within this module, so skip the dummy.
2326 Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2327 AddSourceLocation(getAffectingIncludeLoc(SourceMgr, File), Record);
2328 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2329 Record.push_back(File.hasLineDirectives());
2331 bool EmitBlob = false;
2332 if (Content->OrigEntry) {
2333 assert(Content->OrigEntry == Content->ContentsEntry &&
2334 "Writing to AST an overridden file is not supported");
2336 // The source location entry is a file. Emit input file ID.
2337 assert(InputFileIDs[*Content->OrigEntry] != 0 && "Missed file entry");
2338 Record.push_back(InputFileIDs[*Content->OrigEntry]);
2340 Record.push_back(getAdjustedNumCreatedFIDs(FID));
2342 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
2343 if (FDI != FileDeclIDs.end()) {
2344 Record.push_back(FDI->second->FirstDeclIndex);
2345 Record.push_back(FDI->second->DeclIDs.size());
2346 } else {
2347 Record.push_back(0);
2348 Record.push_back(0);
2351 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2353 if (Content->BufferOverridden || Content->IsTransient)
2354 EmitBlob = true;
2355 } else {
2356 // The source location entry is a buffer. The blob associated
2357 // with this entry contains the contents of the buffer.
2359 // We add one to the size so that we capture the trailing NULL
2360 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2361 // the reader side).
2362 std::optional<llvm::MemoryBufferRef> Buffer = Content->getBufferOrNone(
2363 SourceMgr.getDiagnostics(), SourceMgr.getFileManager());
2364 StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2365 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
2366 StringRef(Name.data(), Name.size() + 1));
2367 EmitBlob = true;
2370 if (EmitBlob) {
2371 // Include the implicit terminating null character in the on-disk buffer
2372 // if we're writing it uncompressed.
2373 std::optional<llvm::MemoryBufferRef> Buffer = Content->getBufferOrNone(
2374 SourceMgr.getDiagnostics(), SourceMgr.getFileManager());
2375 if (!Buffer)
2376 Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2377 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2378 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2379 SLocBufferBlobAbbrv);
2381 } else {
2382 // The source location entry is a macro expansion.
2383 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2384 SLocEntryOffsets.push_back(Offset);
2385 // Starting offset of this entry within this module, so skip the dummy.
2386 Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2387 LocSeq::State Seq;
2388 AddSourceLocation(Expansion.getSpellingLoc(), Record, Seq);
2389 AddSourceLocation(Expansion.getExpansionLocStart(), Record, Seq);
2390 AddSourceLocation(Expansion.isMacroArgExpansion()
2391 ? SourceLocation()
2392 : Expansion.getExpansionLocEnd(),
2393 Record, Seq);
2394 Record.push_back(Expansion.isExpansionTokenRange());
2396 // Compute the token length for this macro expansion.
2397 SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
2398 if (I + 1 != N)
2399 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2400 Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
2401 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
2405 Stream.ExitBlock();
2407 if (SLocEntryOffsets.empty())
2408 return;
2410 // Write the source-location offsets table into the AST block. This
2411 // table is used for lazily loading source-location information.
2412 using namespace llvm;
2414 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2415 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2420 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2422 RecordData::value_type Record[] = {
2423 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2424 getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2425 SLocEntryOffsetsBase - SourceManagerBlockOffset};
2426 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2427 bytes(SLocEntryOffsets));
2430 // Write the line table. It depends on remapping working, so it must come
2431 // after the source location offsets.
2432 if (SourceMgr.hasLineTable()) {
2433 LineTableInfo &LineTable = SourceMgr.getLineTable();
2435 Record.clear();
2437 // Emit the needed file names.
2438 llvm::DenseMap<int, int> FilenameMap;
2439 FilenameMap[-1] = -1; // For unspecified filenames.
2440 for (const auto &L : LineTable) {
2441 if (L.first.ID < 0)
2442 continue;
2443 for (auto &LE : L.second) {
2444 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2445 FilenameMap.size() - 1)).second)
2446 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2449 Record.push_back(0);
2451 // Emit the line entries
2452 for (const auto &L : LineTable) {
2453 // Only emit entries for local files.
2454 if (L.first.ID < 0)
2455 continue;
2457 AddFileID(L.first, Record);
2459 // Emit the line entries
2460 Record.push_back(L.second.size());
2461 for (const auto &LE : L.second) {
2462 Record.push_back(LE.FileOffset);
2463 Record.push_back(LE.LineNo);
2464 Record.push_back(FilenameMap[LE.FilenameID]);
2465 Record.push_back((unsigned)LE.FileKind);
2466 Record.push_back(LE.IncludeOffset);
2470 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2474 //===----------------------------------------------------------------------===//
2475 // Preprocessor Serialization
2476 //===----------------------------------------------------------------------===//
2478 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2479 const Preprocessor &PP) {
2480 if (MacroInfo *MI = MD->getMacroInfo())
2481 if (MI->isBuiltinMacro())
2482 return true;
2484 if (IsModule) {
2485 SourceLocation Loc = MD->getLocation();
2486 if (Loc.isInvalid())
2487 return true;
2488 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2489 return true;
2492 return false;
2495 /// Writes the block containing the serialized form of the
2496 /// preprocessor.
2497 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2498 uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2500 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2501 if (PPRec)
2502 WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
2504 RecordData Record;
2505 RecordData ModuleMacroRecord;
2507 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2508 if (PP.getCounterValue() != 0) {
2509 RecordData::value_type Record[] = {PP.getCounterValue()};
2510 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2513 // If we have a recorded #pragma assume_nonnull, remember it so it can be
2514 // replayed when the preamble terminates into the main file.
2515 SourceLocation AssumeNonNullLoc =
2516 PP.getPreambleRecordedPragmaAssumeNonNullLoc();
2517 if (AssumeNonNullLoc.isValid()) {
2518 assert(PP.isRecordingPreamble());
2519 AddSourceLocation(AssumeNonNullLoc, Record);
2520 Stream.EmitRecord(PP_ASSUME_NONNULL_LOC, Record);
2521 Record.clear();
2524 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2525 assert(!IsModule);
2526 auto SkipInfo = PP.getPreambleSkipInfo();
2527 if (SkipInfo) {
2528 Record.push_back(true);
2529 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2530 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2531 Record.push_back(SkipInfo->FoundNonSkipPortion);
2532 Record.push_back(SkipInfo->FoundElse);
2533 AddSourceLocation(SkipInfo->ElseLoc, Record);
2534 } else {
2535 Record.push_back(false);
2537 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2538 AddSourceLocation(Cond.IfLoc, Record);
2539 Record.push_back(Cond.WasSkipping);
2540 Record.push_back(Cond.FoundNonSkip);
2541 Record.push_back(Cond.FoundElse);
2543 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2544 Record.clear();
2547 // Write the safe buffer opt-out region map in PP
2548 for (SourceLocation &S : PP.serializeSafeBufferOptOutMap())
2549 AddSourceLocation(S, Record);
2550 Stream.EmitRecord(PP_UNSAFE_BUFFER_USAGE, Record);
2551 Record.clear();
2553 // Enter the preprocessor block.
2554 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2556 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2557 // FIXME: Include a location for the use, and say which one was used.
2558 if (PP.SawDateOrTime())
2559 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2561 // Loop over all the macro directives that are live at the end of the file,
2562 // emitting each to the PP section.
2564 // Construct the list of identifiers with macro directives that need to be
2565 // serialized.
2566 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2567 // It is meaningless to emit macros for named modules. It only wastes times
2568 // and spaces.
2569 if (!isWritingStdCXXNamedModules())
2570 for (auto &Id : PP.getIdentifierTable())
2571 if (Id.second->hadMacroDefinition() &&
2572 (!Id.second->isFromAST() ||
2573 Id.second->hasChangedSinceDeserialization()))
2574 MacroIdentifiers.push_back(Id.second);
2575 // Sort the set of macro definitions that need to be serialized by the
2576 // name of the macro, to provide a stable ordering.
2577 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2579 // Emit the macro directives as a list and associate the offset with the
2580 // identifier they belong to.
2581 for (const IdentifierInfo *Name : MacroIdentifiers) {
2582 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2583 uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2584 assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2586 // Write out any exported module macros.
2587 bool EmittedModuleMacros = false;
2588 // C+=20 Header Units are compiled module interfaces, but they preserve
2589 // macros that are live (i.e. have a defined value) at the end of the
2590 // compilation. So when writing a header unit, we preserve only the final
2591 // value of each macro (and discard any that are undefined). Header units
2592 // do not have sub-modules (although they might import other header units).
2593 // PCH files, conversely, retain the history of each macro's define/undef
2594 // and of leaf macros in sub modules.
2595 if (IsModule && WritingModule->isHeaderUnit()) {
2596 // This is for the main TU when it is a C++20 header unit.
2597 // We preserve the final state of defined macros, and we do not emit ones
2598 // that are undefined.
2599 if (!MD || shouldIgnoreMacro(MD, IsModule, PP) ||
2600 MD->getKind() == MacroDirective::MD_Undefine)
2601 continue;
2602 AddSourceLocation(MD->getLocation(), Record);
2603 Record.push_back(MD->getKind());
2604 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2605 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2606 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2607 Record.push_back(VisMD->isPublic());
2609 ModuleMacroRecord.push_back(getSubmoduleID(WritingModule));
2610 ModuleMacroRecord.push_back(getMacroRef(MD->getMacroInfo(), Name));
2611 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2612 ModuleMacroRecord.clear();
2613 EmittedModuleMacros = true;
2614 } else {
2615 // Emit the macro directives in reverse source order.
2616 for (; MD; MD = MD->getPrevious()) {
2617 // Once we hit an ignored macro, we're done: the rest of the chain
2618 // will all be ignored macros.
2619 if (shouldIgnoreMacro(MD, IsModule, PP))
2620 break;
2621 AddSourceLocation(MD->getLocation(), Record);
2622 Record.push_back(MD->getKind());
2623 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2624 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2625 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2626 Record.push_back(VisMD->isPublic());
2630 // We write out exported module macros for PCH as well.
2631 auto Leafs = PP.getLeafModuleMacros(Name);
2632 SmallVector<ModuleMacro *, 8> Worklist(Leafs);
2633 llvm::DenseMap<ModuleMacro *, unsigned> Visits;
2634 while (!Worklist.empty()) {
2635 auto *Macro = Worklist.pop_back_val();
2637 // Emit a record indicating this submodule exports this macro.
2638 ModuleMacroRecord.push_back(getSubmoduleID(Macro->getOwningModule()));
2639 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2640 for (auto *M : Macro->overrides())
2641 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2643 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2644 ModuleMacroRecord.clear();
2646 // Enqueue overridden macros once we've visited all their ancestors.
2647 for (auto *M : Macro->overrides())
2648 if (++Visits[M] == M->getNumOverridingMacros())
2649 Worklist.push_back(M);
2651 EmittedModuleMacros = true;
2654 if (Record.empty() && !EmittedModuleMacros)
2655 continue;
2657 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2658 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2659 Record.clear();
2662 /// Offsets of each of the macros into the bitstream, indexed by
2663 /// the local macro ID
2665 /// For each identifier that is associated with a macro, this map
2666 /// provides the offset into the bitstream where that macro is
2667 /// defined.
2668 std::vector<uint32_t> MacroOffsets;
2670 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2671 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2672 MacroInfo *MI = MacroInfosToEmit[I].MI;
2673 MacroID ID = MacroInfosToEmit[I].ID;
2675 if (ID < FirstMacroID) {
2676 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2677 continue;
2680 // Record the local offset of this macro.
2681 unsigned Index = ID - FirstMacroID;
2682 if (Index >= MacroOffsets.size())
2683 MacroOffsets.resize(Index + 1);
2685 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2686 assert((Offset >> 32) == 0 && "Macro offset too large");
2687 MacroOffsets[Index] = Offset;
2689 AddIdentifierRef(Name, Record);
2690 AddSourceLocation(MI->getDefinitionLoc(), Record);
2691 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2692 Record.push_back(MI->isUsed());
2693 Record.push_back(MI->isUsedForHeaderGuard());
2694 Record.push_back(MI->getNumTokens());
2695 unsigned Code;
2696 if (MI->isObjectLike()) {
2697 Code = PP_MACRO_OBJECT_LIKE;
2698 } else {
2699 Code = PP_MACRO_FUNCTION_LIKE;
2701 Record.push_back(MI->isC99Varargs());
2702 Record.push_back(MI->isGNUVarargs());
2703 Record.push_back(MI->hasCommaPasting());
2704 Record.push_back(MI->getNumParams());
2705 for (const IdentifierInfo *Param : MI->params())
2706 AddIdentifierRef(Param, Record);
2709 // If we have a detailed preprocessing record, record the macro definition
2710 // ID that corresponds to this macro.
2711 if (PPRec)
2712 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2714 Stream.EmitRecord(Code, Record);
2715 Record.clear();
2717 // Emit the tokens array.
2718 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2719 // Note that we know that the preprocessor does not have any annotation
2720 // tokens in it because they are created by the parser, and thus can't
2721 // be in a macro definition.
2722 const Token &Tok = MI->getReplacementToken(TokNo);
2723 AddToken(Tok, Record);
2724 Stream.EmitRecord(PP_TOKEN, Record);
2725 Record.clear();
2727 ++NumMacros;
2730 Stream.ExitBlock();
2732 // Write the offsets table for macro IDs.
2733 using namespace llvm;
2735 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2736 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2742 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2744 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2745 FirstMacroID - NUM_PREDEF_MACRO_IDS,
2746 MacroOffsetsBase - ASTBlockStartOffset};
2747 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2751 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2752 uint64_t MacroOffsetsBase) {
2753 if (PPRec.local_begin() == PPRec.local_end())
2754 return;
2756 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2758 // Enter the preprocessor block.
2759 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2761 // If the preprocessor has a preprocessing record, emit it.
2762 unsigned NumPreprocessingRecords = 0;
2763 using namespace llvm;
2765 // Set up the abbreviation for
2766 unsigned InclusionAbbrev = 0;
2768 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2769 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2775 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2778 unsigned FirstPreprocessorEntityID
2779 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2780 + NUM_PREDEF_PP_ENTITY_IDS;
2781 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2782 RecordData Record;
2783 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2784 EEnd = PPRec.local_end();
2785 E != EEnd;
2786 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2787 Record.clear();
2789 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2790 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2791 SourceRange R = getAdjustedRange((*E)->getSourceRange());
2792 PreprocessedEntityOffsets.emplace_back(
2793 getRawSourceLocationEncoding(R.getBegin()),
2794 getRawSourceLocationEncoding(R.getEnd()), Offset);
2796 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2797 // Record this macro definition's ID.
2798 MacroDefinitions[MD] = NextPreprocessorEntityID;
2800 AddIdentifierRef(MD->getName(), Record);
2801 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2802 continue;
2805 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2806 Record.push_back(ME->isBuiltinMacro());
2807 if (ME->isBuiltinMacro())
2808 AddIdentifierRef(ME->getName(), Record);
2809 else
2810 Record.push_back(MacroDefinitions[ME->getDefinition()]);
2811 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2812 continue;
2815 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2816 Record.push_back(PPD_INCLUSION_DIRECTIVE);
2817 Record.push_back(ID->getFileName().size());
2818 Record.push_back(ID->wasInQuotes());
2819 Record.push_back(static_cast<unsigned>(ID->getKind()));
2820 Record.push_back(ID->importedModule());
2821 SmallString<64> Buffer;
2822 Buffer += ID->getFileName();
2823 // Check that the FileEntry is not null because it was not resolved and
2824 // we create a PCH even with compiler errors.
2825 if (ID->getFile())
2826 Buffer += ID->getFile()->getName();
2827 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2828 continue;
2831 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2833 Stream.ExitBlock();
2835 // Write the offsets table for the preprocessing record.
2836 if (NumPreprocessingRecords > 0) {
2837 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2839 // Write the offsets table for identifier IDs.
2840 using namespace llvm;
2842 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2843 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2846 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2848 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2849 FirstPreprocessorEntityID -
2850 NUM_PREDEF_PP_ENTITY_IDS};
2851 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2852 bytes(PreprocessedEntityOffsets));
2855 // Write the skipped region table for the preprocessing record.
2856 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2857 if (SkippedRanges.size() > 0) {
2858 std::vector<PPSkippedRange> SerializedSkippedRanges;
2859 SerializedSkippedRanges.reserve(SkippedRanges.size());
2860 for (auto const& Range : SkippedRanges)
2861 SerializedSkippedRanges.emplace_back(
2862 getRawSourceLocationEncoding(Range.getBegin()),
2863 getRawSourceLocationEncoding(Range.getEnd()));
2865 using namespace llvm;
2866 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2867 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2869 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2871 Record.clear();
2872 Record.push_back(PPD_SKIPPED_RANGES);
2873 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2874 bytes(SerializedSkippedRanges));
2878 unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module *Mod) {
2879 if (!Mod)
2880 return 0;
2882 auto Known = SubmoduleIDs.find(Mod);
2883 if (Known != SubmoduleIDs.end())
2884 return Known->second;
2886 auto *Top = Mod->getTopLevelModule();
2887 if (Top != WritingModule &&
2888 (getLangOpts().CompilingPCH ||
2889 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
2890 return 0;
2892 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2895 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2896 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2897 // FIXME: This can easily happen, if we have a reference to a submodule that
2898 // did not result in us loading a module file for that submodule. For
2899 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2900 // assert((ID || !Mod) &&
2901 // "asked for module ID for non-local, non-imported module");
2902 return ID;
2905 /// Compute the number of modules within the given tree (including the
2906 /// given module).
2907 static unsigned getNumberOfModules(Module *Mod) {
2908 unsigned ChildModules = 0;
2909 for (auto *Submodule : Mod->submodules())
2910 ChildModules += getNumberOfModules(Submodule);
2912 return ChildModules + 1;
2915 void ASTWriter::WriteSubmodules(Module *WritingModule, ASTContext *Context) {
2916 // Enter the submodule description block.
2917 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2919 // Write the abbreviations needed for the submodules block.
2920 using namespace llvm;
2922 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2923 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Kind
2927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
2928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Inferred allowed by
2929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NamedModuleHasN...
2939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2940 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2942 Abbrev = std::make_shared<BitCodeAbbrev>();
2943 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2945 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2947 Abbrev = std::make_shared<BitCodeAbbrev>();
2948 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2950 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2952 Abbrev = std::make_shared<BitCodeAbbrev>();
2953 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2955 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2957 Abbrev = std::make_shared<BitCodeAbbrev>();
2958 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2960 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2962 Abbrev = std::make_shared<BitCodeAbbrev>();
2963 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2966 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2968 Abbrev = std::make_shared<BitCodeAbbrev>();
2969 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2971 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2973 Abbrev = std::make_shared<BitCodeAbbrev>();
2974 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2976 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2978 Abbrev = std::make_shared<BitCodeAbbrev>();
2979 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2981 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2983 Abbrev = std::make_shared<BitCodeAbbrev>();
2984 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2986 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2988 Abbrev = std::make_shared<BitCodeAbbrev>();
2989 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2992 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2994 Abbrev = std::make_shared<BitCodeAbbrev>();
2995 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2997 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2999 Abbrev = std::make_shared<BitCodeAbbrev>();
3000 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
3001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
3002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
3003 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3005 Abbrev = std::make_shared<BitCodeAbbrev>();
3006 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
3007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
3008 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3010 // Write the submodule metadata block.
3011 RecordData::value_type Record[] = {
3012 getNumberOfModules(WritingModule),
3013 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
3014 Stream.EmitRecord(SUBMODULE_METADATA, Record);
3016 // Write all of the submodules.
3017 std::queue<Module *> Q;
3018 Q.push(WritingModule);
3019 while (!Q.empty()) {
3020 Module *Mod = Q.front();
3021 Q.pop();
3022 unsigned ID = getSubmoduleID(Mod);
3024 uint64_t ParentID = 0;
3025 if (Mod->Parent) {
3026 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
3027 ParentID = SubmoduleIDs[Mod->Parent];
3030 SourceLocationEncoding::RawLocEncoding DefinitionLoc =
3031 getRawSourceLocationEncoding(getAdjustedLocation(Mod->DefinitionLoc));
3033 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
3034 FileID UnadjustedInferredFID;
3035 if (Mod->IsInferred)
3036 UnadjustedInferredFID = ModMap.getModuleMapFileIDForUniquing(Mod);
3037 int InferredFID = getAdjustedFileID(UnadjustedInferredFID).getOpaqueValue();
3039 // Emit the definition of the block.
3041 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
3043 ParentID,
3044 (RecordData::value_type)Mod->Kind,
3045 DefinitionLoc,
3046 (RecordData::value_type)InferredFID,
3047 Mod->IsFramework,
3048 Mod->IsExplicit,
3049 Mod->IsSystem,
3050 Mod->IsExternC,
3051 Mod->InferSubmodules,
3052 Mod->InferExplicitSubmodules,
3053 Mod->InferExportWildcard,
3054 Mod->ConfigMacrosExhaustive,
3055 Mod->ModuleMapIsPrivate,
3056 Mod->NamedModuleHasInit};
3057 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
3060 // Emit the requirements.
3061 for (const auto &R : Mod->Requirements) {
3062 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.RequiredState};
3063 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.FeatureName);
3066 // Emit the umbrella header, if there is one.
3067 if (std::optional<Module::Header> UmbrellaHeader =
3068 Mod->getUmbrellaHeaderAsWritten()) {
3069 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
3070 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
3071 UmbrellaHeader->NameAsWritten);
3072 } else if (std::optional<Module::DirectoryName> UmbrellaDir =
3073 Mod->getUmbrellaDirAsWritten()) {
3074 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
3075 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
3076 UmbrellaDir->NameAsWritten);
3079 // Emit the headers.
3080 struct {
3081 unsigned RecordKind;
3082 unsigned Abbrev;
3083 Module::HeaderKind HeaderKind;
3084 } HeaderLists[] = {
3085 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
3086 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
3087 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
3088 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
3089 Module::HK_PrivateTextual},
3090 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
3092 for (const auto &HL : HeaderLists) {
3093 RecordData::value_type Record[] = {HL.RecordKind};
3094 for (const auto &H : Mod->getHeaders(HL.HeaderKind))
3095 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
3098 // Emit the top headers.
3100 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
3101 for (FileEntryRef H : Mod->getTopHeaders(PP->getFileManager())) {
3102 SmallString<128> HeaderName(H.getName());
3103 PreparePathForOutput(HeaderName);
3104 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
3108 // Emit the imports.
3109 if (!Mod->Imports.empty()) {
3110 RecordData Record;
3111 for (auto *I : Mod->Imports)
3112 Record.push_back(getSubmoduleID(I));
3113 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
3116 // Emit the modules affecting compilation that were not imported.
3117 if (!Mod->AffectingClangModules.empty()) {
3118 RecordData Record;
3119 for (auto *I : Mod->AffectingClangModules)
3120 Record.push_back(getSubmoduleID(I));
3121 Stream.EmitRecord(SUBMODULE_AFFECTING_MODULES, Record);
3124 // Emit the exports.
3125 if (!Mod->Exports.empty()) {
3126 RecordData Record;
3127 for (const auto &E : Mod->Exports) {
3128 // FIXME: This may fail; we don't require that all exported modules
3129 // are local or imported.
3130 Record.push_back(getSubmoduleID(E.getPointer()));
3131 Record.push_back(E.getInt());
3133 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
3136 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
3137 // Might be unnecessary as use declarations are only used to build the
3138 // module itself.
3140 // TODO: Consider serializing undeclared uses of modules.
3142 // Emit the link libraries.
3143 for (const auto &LL : Mod->LinkLibraries) {
3144 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
3145 LL.IsFramework};
3146 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
3149 // Emit the conflicts.
3150 for (const auto &C : Mod->Conflicts) {
3151 // FIXME: This may fail; we don't require that all conflicting modules
3152 // are local or imported.
3153 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
3154 getSubmoduleID(C.Other)};
3155 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
3158 // Emit the configuration macros.
3159 for (const auto &CM : Mod->ConfigMacros) {
3160 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
3161 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
3164 // Emit the reachable initializers.
3165 // The initializer may only be unreachable in reduced BMI.
3166 if (Context) {
3167 RecordData Inits;
3168 for (Decl *D : Context->getModuleInitializers(Mod))
3169 if (wasDeclEmitted(D))
3170 AddDeclRef(D, Inits);
3171 if (!Inits.empty())
3172 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3175 // Emit the name of the re-exported module, if any.
3176 if (!Mod->ExportAsModule.empty()) {
3177 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3178 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3181 // Queue up the submodules of this module.
3182 for (auto *M : Mod->submodules())
3183 Q.push(M);
3186 Stream.ExitBlock();
3188 assert((NextSubmoduleID - FirstSubmoduleID ==
3189 getNumberOfModules(WritingModule)) &&
3190 "Wrong # of submodules; found a reference to a non-local, "
3191 "non-imported submodule?");
3194 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3195 bool isModule) {
3196 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3197 DiagStateIDMap;
3198 unsigned CurrID = 0;
3199 RecordData Record;
3201 auto EncodeDiagStateFlags =
3202 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3203 unsigned Result = (unsigned)DS->ExtBehavior;
3204 for (unsigned Val :
3205 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3206 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3207 (unsigned)DS->SuppressSystemWarnings})
3208 Result = (Result << 1) | Val;
3209 return Result;
3212 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3213 Record.push_back(Flags);
3215 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3216 bool IncludeNonPragmaStates) {
3217 // Ensure that the diagnostic state wasn't modified since it was created.
3218 // We will not correctly round-trip this information otherwise.
3219 assert(Flags == EncodeDiagStateFlags(State) &&
3220 "diag state flags vary in single AST file");
3222 // If we ever serialize non-pragma mappings outside the initial state, the
3223 // code below will need to consider more than getDefaultMapping.
3224 assert(!IncludeNonPragmaStates ||
3225 State == Diag.DiagStatesByLoc.FirstDiagState);
3227 unsigned &DiagStateID = DiagStateIDMap[State];
3228 Record.push_back(DiagStateID);
3230 if (DiagStateID == 0) {
3231 DiagStateID = ++CurrID;
3232 SmallVector<std::pair<unsigned, DiagnosticMapping>> Mappings;
3234 // Add a placeholder for the number of mappings.
3235 auto SizeIdx = Record.size();
3236 Record.emplace_back();
3237 for (const auto &I : *State) {
3238 // Maybe skip non-pragmas.
3239 if (!I.second.isPragma() && !IncludeNonPragmaStates)
3240 continue;
3241 // Skip default mappings. We have a mapping for every diagnostic ever
3242 // emitted, regardless of whether it was customized.
3243 if (!I.second.isPragma() &&
3244 I.second == DiagnosticIDs::getDefaultMapping(I.first))
3245 continue;
3246 Mappings.push_back(I);
3249 // Sort by diag::kind for deterministic output.
3250 llvm::sort(Mappings, llvm::less_first());
3252 for (const auto &I : Mappings) {
3253 Record.push_back(I.first);
3254 Record.push_back(I.second.serialize());
3256 // Update the placeholder.
3257 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
3261 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
3263 // Reserve a spot for the number of locations with state transitions.
3264 auto NumLocationsIdx = Record.size();
3265 Record.emplace_back();
3267 // Emit the state transitions.
3268 unsigned NumLocations = 0;
3269 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3270 if (!FileIDAndFile.first.isValid() ||
3271 !FileIDAndFile.second.HasLocalTransitions)
3272 continue;
3273 ++NumLocations;
3275 AddFileID(FileIDAndFile.first, Record);
3277 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3278 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3279 Record.push_back(getAdjustedOffset(StatePoint.Offset));
3280 AddDiagState(StatePoint.State, false);
3284 // Backpatch the number of locations.
3285 Record[NumLocationsIdx] = NumLocations;
3287 // Emit CurDiagStateLoc. Do it last in order to match source order.
3289 // This also protects against a hypothetical corner case with simulating
3290 // -Werror settings for implicit modules in the ASTReader, where reading
3291 // CurDiagState out of context could change whether warning pragmas are
3292 // treated as errors.
3293 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3294 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3296 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
3299 //===----------------------------------------------------------------------===//
3300 // Type Serialization
3301 //===----------------------------------------------------------------------===//
3303 /// Write the representation of a type to the AST stream.
3304 void ASTWriter::WriteType(ASTContext &Context, QualType T) {
3305 TypeIdx &IdxRef = TypeIdxs[T];
3306 if (IdxRef.getValue() == 0) // we haven't seen this type before.
3307 IdxRef = TypeIdx(0, NextTypeID++);
3308 TypeIdx Idx = IdxRef;
3310 assert(Idx.getModuleFileIndex() == 0 && "Re-writing a type from a prior AST");
3311 assert(Idx.getValue() >= FirstTypeID && "Writing predefined type");
3313 // Emit the type's representation.
3314 uint64_t Offset =
3315 ASTTypeWriter(Context, *this).write(T) - DeclTypesBlockStartOffset;
3317 // Record the offset for this type.
3318 uint64_t Index = Idx.getValue() - FirstTypeID;
3319 if (TypeOffsets.size() == Index)
3320 TypeOffsets.emplace_back(Offset);
3321 else if (TypeOffsets.size() < Index) {
3322 TypeOffsets.resize(Index + 1);
3323 TypeOffsets[Index].set(Offset);
3324 } else {
3325 llvm_unreachable("Types emitted in wrong order");
3329 //===----------------------------------------------------------------------===//
3330 // Declaration Serialization
3331 //===----------------------------------------------------------------------===//
3333 static bool IsInternalDeclFromFileContext(const Decl *D) {
3334 auto *ND = dyn_cast<NamedDecl>(D);
3335 if (!ND)
3336 return false;
3338 if (!D->getDeclContext()->getRedeclContext()->isFileContext())
3339 return false;
3341 return ND->getFormalLinkage() == Linkage::Internal;
3344 /// Write the block containing all of the declaration IDs
3345 /// lexically declared within the given DeclContext.
3347 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3348 /// bitstream, or 0 if no block was written.
3349 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3350 const DeclContext *DC) {
3351 if (DC->decls_empty())
3352 return 0;
3354 // In reduced BMI, we don't care the declarations in functions.
3355 if (GeneratingReducedBMI && DC->isFunctionOrMethod())
3356 return 0;
3358 uint64_t Offset = Stream.GetCurrentBitNo();
3359 SmallVector<DeclID, 128> KindDeclPairs;
3360 for (const auto *D : DC->decls()) {
3361 if (DoneWritingDeclsAndTypes && !wasDeclEmitted(D))
3362 continue;
3364 // We don't need to write decls with internal linkage into reduced BMI.
3365 // If such decls gets emitted due to it get used from inline functions,
3366 // the program illegal. However, there are too many use of static inline
3367 // functions in the global module fragment and it will be breaking change
3368 // to forbid that. So we have to allow to emit such declarations from GMF.
3369 if (GeneratingReducedBMI && !D->isFromExplicitGlobalModule() &&
3370 IsInternalDeclFromFileContext(D))
3371 continue;
3373 KindDeclPairs.push_back(D->getKind());
3374 KindDeclPairs.push_back(GetDeclRef(D).getRawValue());
3377 ++NumLexicalDeclContexts;
3378 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3379 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3380 bytes(KindDeclPairs));
3381 return Offset;
3384 void ASTWriter::WriteTypeDeclOffsets() {
3385 using namespace llvm;
3387 // Write the type offsets array
3388 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3389 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
3390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3392 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3394 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size()};
3395 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3398 // Write the declaration offsets array
3399 Abbrev = std::make_shared<BitCodeAbbrev>();
3400 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
3401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3403 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3405 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size()};
3406 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3410 void ASTWriter::WriteFileDeclIDsMap() {
3411 using namespace llvm;
3413 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
3414 SortedFileDeclIDs.reserve(FileDeclIDs.size());
3415 for (const auto &P : FileDeclIDs)
3416 SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
3417 llvm::sort(SortedFileDeclIDs, llvm::less_first());
3419 // Join the vectors of DeclIDs from all files.
3420 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3421 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3422 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3423 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3424 llvm::stable_sort(Info.DeclIDs);
3425 for (auto &LocDeclEntry : Info.DeclIDs)
3426 FileGroupedDeclIDs.push_back(LocDeclEntry.second.getRawValue());
3429 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3430 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
3431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3433 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
3434 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3435 FileGroupedDeclIDs.size()};
3436 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
3439 void ASTWriter::WriteComments(ASTContext &Context) {
3440 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
3441 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3442 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3443 return;
3445 // Don't write comments to BMI to reduce the size of BMI.
3446 // If language services (e.g., clangd) want such abilities,
3447 // we can offer a special option then.
3448 if (isWritingStdCXXNamedModules())
3449 return;
3451 RecordData Record;
3452 for (const auto &FO : Context.Comments.OrderedComments) {
3453 for (const auto &OC : FO.second) {
3454 const RawComment *I = OC.second;
3455 Record.clear();
3456 AddSourceRange(I->getSourceRange(), Record);
3457 Record.push_back(I->getKind());
3458 Record.push_back(I->isTrailingComment());
3459 Record.push_back(I->isAlmostTrailingComment());
3460 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3465 //===----------------------------------------------------------------------===//
3466 // Global Method Pool and Selector Serialization
3467 //===----------------------------------------------------------------------===//
3469 namespace {
3471 // Trait used for the on-disk hash table used in the method pool.
3472 class ASTMethodPoolTrait {
3473 ASTWriter &Writer;
3475 public:
3476 using key_type = Selector;
3477 using key_type_ref = key_type;
3479 struct data_type {
3480 SelectorID ID;
3481 ObjCMethodList Instance, Factory;
3483 using data_type_ref = const data_type &;
3485 using hash_value_type = unsigned;
3486 using offset_type = unsigned;
3488 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3490 static hash_value_type ComputeHash(Selector Sel) {
3491 return serialization::ComputeHash(Sel);
3494 std::pair<unsigned, unsigned>
3495 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3496 data_type_ref Methods) {
3497 unsigned KeyLen =
3498 2 + (Sel.getNumArgs() ? Sel.getNumArgs() * sizeof(IdentifierID)
3499 : sizeof(IdentifierID));
3500 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3501 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3502 Method = Method->getNext())
3503 if (ShouldWriteMethodListNode(Method))
3504 DataLen += sizeof(DeclID);
3505 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3506 Method = Method->getNext())
3507 if (ShouldWriteMethodListNode(Method))
3508 DataLen += sizeof(DeclID);
3509 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3512 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3513 using namespace llvm::support;
3515 endian::Writer LE(Out, llvm::endianness::little);
3516 uint64_t Start = Out.tell();
3517 assert((Start >> 32) == 0 && "Selector key offset too large");
3518 Writer.SetSelectorOffset(Sel, Start);
3519 unsigned N = Sel.getNumArgs();
3520 LE.write<uint16_t>(N);
3521 if (N == 0)
3522 N = 1;
3523 for (unsigned I = 0; I != N; ++I)
3524 LE.write<IdentifierID>(
3525 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
3528 void EmitData(raw_ostream& Out, key_type_ref,
3529 data_type_ref Methods, unsigned DataLen) {
3530 using namespace llvm::support;
3532 endian::Writer LE(Out, llvm::endianness::little);
3533 uint64_t Start = Out.tell(); (void)Start;
3534 LE.write<uint32_t>(Methods.ID);
3535 unsigned NumInstanceMethods = 0;
3536 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3537 Method = Method->getNext())
3538 if (ShouldWriteMethodListNode(Method))
3539 ++NumInstanceMethods;
3541 unsigned NumFactoryMethods = 0;
3542 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3543 Method = Method->getNext())
3544 if (ShouldWriteMethodListNode(Method))
3545 ++NumFactoryMethods;
3547 unsigned InstanceBits = Methods.Instance.getBits();
3548 assert(InstanceBits < 4);
3549 unsigned InstanceHasMoreThanOneDeclBit =
3550 Methods.Instance.hasMoreThanOneDecl();
3551 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3552 (InstanceHasMoreThanOneDeclBit << 2) |
3553 InstanceBits;
3554 unsigned FactoryBits = Methods.Factory.getBits();
3555 assert(FactoryBits < 4);
3556 unsigned FactoryHasMoreThanOneDeclBit =
3557 Methods.Factory.hasMoreThanOneDecl();
3558 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3559 (FactoryHasMoreThanOneDeclBit << 2) |
3560 FactoryBits;
3561 LE.write<uint16_t>(FullInstanceBits);
3562 LE.write<uint16_t>(FullFactoryBits);
3563 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3564 Method = Method->getNext())
3565 if (ShouldWriteMethodListNode(Method))
3566 LE.write<DeclID>((DeclID)Writer.getDeclID(Method->getMethod()));
3567 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3568 Method = Method->getNext())
3569 if (ShouldWriteMethodListNode(Method))
3570 LE.write<DeclID>((DeclID)Writer.getDeclID(Method->getMethod()));
3572 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3575 private:
3576 static bool ShouldWriteMethodListNode(const ObjCMethodList *Node) {
3577 return (Node->getMethod() && !Node->getMethod()->isFromASTFile());
3581 } // namespace
3583 /// Write ObjC data: selectors and the method pool.
3585 /// The method pool contains both instance and factory methods, stored
3586 /// in an on-disk hash table indexed by the selector. The hash table also
3587 /// contains an empty entry for every other selector known to Sema.
3588 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3589 using namespace llvm;
3591 // Do we have to do anything at all?
3592 if (SemaRef.ObjC().MethodPool.empty() && SelectorIDs.empty())
3593 return;
3594 unsigned NumTableEntries = 0;
3595 // Create and write out the blob that contains selectors and the method pool.
3597 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3598 ASTMethodPoolTrait Trait(*this);
3600 // Create the on-disk hash table representation. We walk through every
3601 // selector we've seen and look it up in the method pool.
3602 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3603 for (auto &SelectorAndID : SelectorIDs) {
3604 Selector S = SelectorAndID.first;
3605 SelectorID ID = SelectorAndID.second;
3606 SemaObjC::GlobalMethodPool::iterator F =
3607 SemaRef.ObjC().MethodPool.find(S);
3608 ASTMethodPoolTrait::data_type Data = {
3610 ObjCMethodList(),
3611 ObjCMethodList()
3613 if (F != SemaRef.ObjC().MethodPool.end()) {
3614 Data.Instance = F->second.first;
3615 Data.Factory = F->second.second;
3617 // Only write this selector if it's not in an existing AST or something
3618 // changed.
3619 if (Chain && ID < FirstSelectorID) {
3620 // Selector already exists. Did it change?
3621 bool changed = false;
3622 for (ObjCMethodList *M = &Data.Instance; M && M->getMethod();
3623 M = M->getNext()) {
3624 if (!M->getMethod()->isFromASTFile()) {
3625 changed = true;
3626 Data.Instance = *M;
3627 break;
3630 for (ObjCMethodList *M = &Data.Factory; M && M->getMethod();
3631 M = M->getNext()) {
3632 if (!M->getMethod()->isFromASTFile()) {
3633 changed = true;
3634 Data.Factory = *M;
3635 break;
3638 if (!changed)
3639 continue;
3640 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3641 // A new method pool entry.
3642 ++NumTableEntries;
3644 Generator.insert(S, Data, Trait);
3647 // Create the on-disk hash table in a buffer.
3648 SmallString<4096> MethodPool;
3649 uint32_t BucketOffset;
3651 using namespace llvm::support;
3653 ASTMethodPoolTrait Trait(*this);
3654 llvm::raw_svector_ostream Out(MethodPool);
3655 // Make sure that no bucket is at offset 0
3656 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3657 BucketOffset = Generator.Emit(Out, Trait);
3660 // Create a blob abbreviation
3661 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3662 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3663 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3666 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3668 // Write the method pool
3670 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3671 NumTableEntries};
3672 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3675 // Create a blob abbreviation for the selector table offsets.
3676 Abbrev = std::make_shared<BitCodeAbbrev>();
3677 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3678 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3679 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3681 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3683 // Write the selector offsets table.
3685 RecordData::value_type Record[] = {
3686 SELECTOR_OFFSETS, SelectorOffsets.size(),
3687 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3688 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3689 bytes(SelectorOffsets));
3694 /// Write the selectors referenced in @selector expression into AST file.
3695 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3696 using namespace llvm;
3698 if (SemaRef.ObjC().ReferencedSelectors.empty())
3699 return;
3701 RecordData Record;
3702 ASTRecordWriter Writer(SemaRef.Context, *this, Record);
3704 // Note: this writes out all references even for a dependent AST. But it is
3705 // very tricky to fix, and given that @selector shouldn't really appear in
3706 // headers, probably not worth it. It's not a correctness issue.
3707 for (auto &SelectorAndLocation : SemaRef.ObjC().ReferencedSelectors) {
3708 Selector Sel = SelectorAndLocation.first;
3709 SourceLocation Loc = SelectorAndLocation.second;
3710 Writer.AddSelectorRef(Sel);
3711 Writer.AddSourceLocation(Loc);
3713 Writer.Emit(REFERENCED_SELECTOR_POOL);
3716 //===----------------------------------------------------------------------===//
3717 // Identifier Table Serialization
3718 //===----------------------------------------------------------------------===//
3720 /// Determine the declaration that should be put into the name lookup table to
3721 /// represent the given declaration in this module. This is usually D itself,
3722 /// but if D was imported and merged into a local declaration, we want the most
3723 /// recent local declaration instead. The chosen declaration will be the most
3724 /// recent declaration in any module that imports this one.
3725 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3726 NamedDecl *D) {
3727 if (!LangOpts.Modules || !D->isFromASTFile())
3728 return D;
3730 if (Decl *Redecl = D->getPreviousDecl()) {
3731 // For Redeclarable decls, a prior declaration might be local.
3732 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3733 // If we find a local decl, we're done.
3734 if (!Redecl->isFromASTFile()) {
3735 // Exception: in very rare cases (for injected-class-names), not all
3736 // redeclarations are in the same semantic context. Skip ones in a
3737 // different context. They don't go in this lookup table at all.
3738 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3739 D->getDeclContext()->getRedeclContext()))
3740 continue;
3741 return cast<NamedDecl>(Redecl);
3744 // If we find a decl from a (chained-)PCH stop since we won't find a
3745 // local one.
3746 if (Redecl->getOwningModuleID() == 0)
3747 break;
3749 } else if (Decl *First = D->getCanonicalDecl()) {
3750 // For Mergeable decls, the first decl might be local.
3751 if (!First->isFromASTFile())
3752 return cast<NamedDecl>(First);
3755 // All declarations are imported. Our most recent declaration will also be
3756 // the most recent one in anyone who imports us.
3757 return D;
3760 namespace {
3762 bool IsInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset,
3763 bool IsModule, bool IsCPlusPlus) {
3764 bool NeedDecls = !IsModule || !IsCPlusPlus;
3766 bool IsInteresting =
3767 II->getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
3768 II->getBuiltinID() != Builtin::ID::NotBuiltin ||
3769 II->getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
3770 if (MacroOffset || II->isPoisoned() || (!IsModule && IsInteresting) ||
3771 II->hasRevertedTokenIDToIdentifier() ||
3772 (NeedDecls && II->getFETokenInfo()))
3773 return true;
3775 return false;
3778 bool IsInterestingNonMacroIdentifier(const IdentifierInfo *II,
3779 ASTWriter &Writer) {
3780 bool IsModule = Writer.isWritingModule();
3781 bool IsCPlusPlus = Writer.getLangOpts().CPlusPlus;
3782 return IsInterestingIdentifier(II, /*MacroOffset=*/0, IsModule, IsCPlusPlus);
3785 class ASTIdentifierTableTrait {
3786 ASTWriter &Writer;
3787 Preprocessor &PP;
3788 IdentifierResolver *IdResolver;
3789 bool IsModule;
3790 bool NeedDecls;
3791 ASTWriter::RecordData *InterestingIdentifierOffsets;
3793 /// Determines whether this is an "interesting" identifier that needs a
3794 /// full IdentifierInfo structure written into the hash table. Notably, this
3795 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3796 /// to check that.
3797 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3798 return IsInterestingIdentifier(II, MacroOffset, IsModule,
3799 Writer.getLangOpts().CPlusPlus);
3802 public:
3803 using key_type = const IdentifierInfo *;
3804 using key_type_ref = key_type;
3806 using data_type = IdentifierID;
3807 using data_type_ref = data_type;
3809 using hash_value_type = unsigned;
3810 using offset_type = unsigned;
3812 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3813 IdentifierResolver *IdResolver, bool IsModule,
3814 ASTWriter::RecordData *InterestingIdentifierOffsets)
3815 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3816 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3817 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3819 bool needDecls() const { return NeedDecls; }
3821 static hash_value_type ComputeHash(const IdentifierInfo* II) {
3822 return llvm::djbHash(II->getName());
3825 bool isInterestingIdentifier(const IdentifierInfo *II) {
3826 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3827 return isInterestingIdentifier(II, MacroOffset);
3830 std::pair<unsigned, unsigned>
3831 EmitKeyDataLength(raw_ostream &Out, const IdentifierInfo *II, IdentifierID ID) {
3832 // Record the location of the identifier data. This is used when generating
3833 // the mapping from persistent IDs to strings.
3834 Writer.SetIdentifierOffset(II, Out.tell());
3836 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3838 // Emit the offset of the key/data length information to the interesting
3839 // identifiers table if necessary.
3840 if (InterestingIdentifierOffsets &&
3841 isInterestingIdentifier(II, MacroOffset))
3842 InterestingIdentifierOffsets->push_back(Out.tell());
3844 unsigned KeyLen = II->getLength() + 1;
3845 unsigned DataLen = sizeof(IdentifierID); // bytes for the persistent ID << 1
3846 if (isInterestingIdentifier(II, MacroOffset)) {
3847 DataLen += 2; // 2 bytes for builtin ID
3848 DataLen += 2; // 2 bytes for flags
3849 if (MacroOffset)
3850 DataLen += 4; // MacroDirectives offset.
3852 if (NeedDecls && IdResolver)
3853 DataLen += std::distance(IdResolver->begin(II), IdResolver->end()) *
3854 sizeof(DeclID);
3856 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3859 void EmitKey(raw_ostream &Out, const IdentifierInfo *II, unsigned KeyLen) {
3860 Out.write(II->getNameStart(), KeyLen);
3863 void EmitData(raw_ostream &Out, const IdentifierInfo *II, IdentifierID ID,
3864 unsigned) {
3865 using namespace llvm::support;
3867 endian::Writer LE(Out, llvm::endianness::little);
3869 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3870 if (!isInterestingIdentifier(II, MacroOffset)) {
3871 LE.write<IdentifierID>(ID << 1);
3872 return;
3875 LE.write<IdentifierID>((ID << 1) | 0x01);
3876 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3877 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3878 LE.write<uint16_t>(Bits);
3879 Bits = 0;
3880 bool HadMacroDefinition = MacroOffset != 0;
3881 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3882 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3883 Bits = (Bits << 1) | unsigned(II->isPoisoned());
3884 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3885 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3886 LE.write<uint16_t>(Bits);
3888 if (HadMacroDefinition)
3889 LE.write<uint32_t>(MacroOffset);
3891 if (NeedDecls && IdResolver) {
3892 // Emit the declaration IDs in reverse order, because the
3893 // IdentifierResolver provides the declarations as they would be
3894 // visible (e.g., the function "stat" would come before the struct
3895 // "stat"), but the ASTReader adds declarations to the end of the list
3896 // (so we need to see the struct "stat" before the function "stat").
3897 // Only emit declarations that aren't from a chained PCH, though.
3898 SmallVector<NamedDecl *, 16> Decls(IdResolver->decls(II));
3899 for (NamedDecl *D : llvm::reverse(Decls))
3900 LE.write<DeclID>((DeclID)Writer.getDeclID(
3901 getDeclForLocalLookup(PP.getLangOpts(), D)));
3906 } // namespace
3908 /// If the \param IdentifierID ID is a local Identifier ID. If the higher
3909 /// bits of ID is 0, it implies that the ID doesn't come from AST files.
3910 static bool isLocalIdentifierID(IdentifierID ID) { return !(ID >> 32); }
3912 /// Write the identifier table into the AST file.
3914 /// The identifier table consists of a blob containing string data
3915 /// (the actual identifiers themselves) and a separate "offsets" index
3916 /// that maps identifier IDs to locations within the blob.
3917 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3918 IdentifierResolver *IdResolver,
3919 bool IsModule) {
3920 using namespace llvm;
3922 RecordData InterestingIdents;
3924 // Create and write out the blob that contains the identifier
3925 // strings.
3927 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3928 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule,
3929 IsModule ? &InterestingIdents : nullptr);
3931 // Create the on-disk hash table representation. We only store offsets
3932 // for identifiers that appear here for the first time.
3933 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3934 for (auto IdentIDPair : IdentifierIDs) {
3935 const IdentifierInfo *II = IdentIDPair.first;
3936 IdentifierID ID = IdentIDPair.second;
3937 assert(II && "NULL identifier in identifier table");
3939 // Write out identifiers if either the ID is local or the identifier has
3940 // changed since it was loaded.
3941 if (isLocalIdentifierID(ID) || II->hasChangedSinceDeserialization() ||
3942 (Trait.needDecls() &&
3943 II->hasFETokenInfoChangedSinceDeserialization()))
3944 Generator.insert(II, ID, Trait);
3947 // Create the on-disk hash table in a buffer.
3948 SmallString<4096> IdentifierTable;
3949 uint32_t BucketOffset;
3951 using namespace llvm::support;
3953 llvm::raw_svector_ostream Out(IdentifierTable);
3954 // Make sure that no bucket is at offset 0
3955 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3956 BucketOffset = Generator.Emit(Out, Trait);
3959 // Create a blob abbreviation
3960 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3961 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3964 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3966 // Write the identifier table
3967 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3968 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3971 // Write the offsets table for identifier IDs.
3972 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3973 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3976 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3978 #ifndef NDEBUG
3979 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3980 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3981 #endif
3983 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3984 IdentifierOffsets.size()};
3985 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3986 bytes(IdentifierOffsets));
3988 // In C++, write the list of interesting identifiers (those that are
3989 // defined as macros, poisoned, or similar unusual things).
3990 if (!InterestingIdents.empty())
3991 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3994 void ASTWriter::handleVTable(CXXRecordDecl *RD) {
3995 if (!RD->isInNamedModule())
3996 return;
3998 PendingEmittingVTables.push_back(RD);
4001 //===----------------------------------------------------------------------===//
4002 // DeclContext's Name Lookup Table Serialization
4003 //===----------------------------------------------------------------------===//
4005 namespace {
4007 // Trait used for the on-disk hash table used in the method pool.
4008 class ASTDeclContextNameLookupTrait {
4009 ASTWriter &Writer;
4010 llvm::SmallVector<LocalDeclID, 64> DeclIDs;
4012 public:
4013 using key_type = DeclarationNameKey;
4014 using key_type_ref = key_type;
4016 /// A start and end index into DeclIDs, representing a sequence of decls.
4017 using data_type = std::pair<unsigned, unsigned>;
4018 using data_type_ref = const data_type &;
4020 using hash_value_type = unsigned;
4021 using offset_type = unsigned;
4023 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
4025 template<typename Coll>
4026 data_type getData(const Coll &Decls) {
4027 unsigned Start = DeclIDs.size();
4028 for (NamedDecl *D : Decls) {
4029 NamedDecl *DeclForLocalLookup =
4030 getDeclForLocalLookup(Writer.getLangOpts(), D);
4032 if (Writer.getDoneWritingDeclsAndTypes() &&
4033 !Writer.wasDeclEmitted(DeclForLocalLookup))
4034 continue;
4036 // Try to avoid writing internal decls to reduced BMI.
4037 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4038 if (Writer.isGeneratingReducedBMI() &&
4039 !DeclForLocalLookup->isFromExplicitGlobalModule() &&
4040 IsInternalDeclFromFileContext(DeclForLocalLookup))
4041 continue;
4043 DeclIDs.push_back(Writer.GetDeclRef(DeclForLocalLookup));
4045 return std::make_pair(Start, DeclIDs.size());
4048 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
4049 unsigned Start = DeclIDs.size();
4050 DeclIDs.insert(
4051 DeclIDs.end(),
4052 DeclIDIterator<GlobalDeclID, LocalDeclID>(FromReader.begin()),
4053 DeclIDIterator<GlobalDeclID, LocalDeclID>(FromReader.end()));
4054 return std::make_pair(Start, DeclIDs.size());
4057 static bool EqualKey(key_type_ref a, key_type_ref b) {
4058 return a == b;
4061 hash_value_type ComputeHash(DeclarationNameKey Name) {
4062 return Name.getHash();
4065 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
4066 assert(Writer.hasChain() &&
4067 "have reference to loaded module file but no chain?");
4069 using namespace llvm::support;
4071 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F),
4072 llvm::endianness::little);
4075 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
4076 DeclarationNameKey Name,
4077 data_type_ref Lookup) {
4078 unsigned KeyLen = 1;
4079 switch (Name.getKind()) {
4080 case DeclarationName::Identifier:
4081 case DeclarationName::CXXLiteralOperatorName:
4082 case DeclarationName::CXXDeductionGuideName:
4083 KeyLen += sizeof(IdentifierID);
4084 break;
4085 case DeclarationName::ObjCZeroArgSelector:
4086 case DeclarationName::ObjCOneArgSelector:
4087 case DeclarationName::ObjCMultiArgSelector:
4088 KeyLen += 4;
4089 break;
4090 case DeclarationName::CXXOperatorName:
4091 KeyLen += 1;
4092 break;
4093 case DeclarationName::CXXConstructorName:
4094 case DeclarationName::CXXDestructorName:
4095 case DeclarationName::CXXConversionFunctionName:
4096 case DeclarationName::CXXUsingDirective:
4097 break;
4100 // length of DeclIDs.
4101 unsigned DataLen = sizeof(DeclID) * (Lookup.second - Lookup.first);
4103 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
4106 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
4107 using namespace llvm::support;
4109 endian::Writer LE(Out, llvm::endianness::little);
4110 LE.write<uint8_t>(Name.getKind());
4111 switch (Name.getKind()) {
4112 case DeclarationName::Identifier:
4113 case DeclarationName::CXXLiteralOperatorName:
4114 case DeclarationName::CXXDeductionGuideName:
4115 LE.write<IdentifierID>(Writer.getIdentifierRef(Name.getIdentifier()));
4116 return;
4117 case DeclarationName::ObjCZeroArgSelector:
4118 case DeclarationName::ObjCOneArgSelector:
4119 case DeclarationName::ObjCMultiArgSelector:
4120 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
4121 return;
4122 case DeclarationName::CXXOperatorName:
4123 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
4124 "Invalid operator?");
4125 LE.write<uint8_t>(Name.getOperatorKind());
4126 return;
4127 case DeclarationName::CXXConstructorName:
4128 case DeclarationName::CXXDestructorName:
4129 case DeclarationName::CXXConversionFunctionName:
4130 case DeclarationName::CXXUsingDirective:
4131 return;
4134 llvm_unreachable("Invalid name kind?");
4137 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
4138 unsigned DataLen) {
4139 using namespace llvm::support;
4141 endian::Writer LE(Out, llvm::endianness::little);
4142 uint64_t Start = Out.tell(); (void)Start;
4143 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
4144 LE.write<DeclID>((DeclID)DeclIDs[I]);
4145 assert(Out.tell() - Start == DataLen && "Data length is wrong");
4149 } // namespace
4151 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
4152 DeclContext *DC) {
4153 return Result.hasExternalDecls() &&
4154 DC->hasNeedToReconcileExternalVisibleStorage();
4157 /// Returns ture if all of the lookup result are either external, not emitted or
4158 /// predefined. In such cases, the lookup result is not interesting and we don't
4159 /// need to record the result in the current being written module. Return false
4160 /// otherwise.
4161 static bool isLookupResultNotInteresting(ASTWriter &Writer,
4162 StoredDeclsList &Result) {
4163 for (auto *D : Result.getLookupResult()) {
4164 auto *LocalD = getDeclForLocalLookup(Writer.getLangOpts(), D);
4165 if (LocalD->isFromASTFile())
4166 continue;
4168 // We can only be sure whether the local declaration is reachable
4169 // after we done writing the declarations and types.
4170 if (Writer.getDoneWritingDeclsAndTypes() && !Writer.wasDeclEmitted(LocalD))
4171 continue;
4173 // We don't need to emit the predefined decls.
4174 if (Writer.isDeclPredefined(LocalD))
4175 continue;
4177 return false;
4180 return true;
4183 void ASTWriter::GenerateNameLookupTable(
4184 ASTContext &Context, const DeclContext *ConstDC,
4185 llvm::SmallVectorImpl<char> &LookupTable) {
4186 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
4187 !ConstDC->hasLazyExternalLexicalLookups() &&
4188 "must call buildLookups first");
4190 // FIXME: We need to build the lookups table, which is logically const.
4191 auto *DC = const_cast<DeclContext*>(ConstDC);
4192 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
4194 // Create the on-disk hash table representation.
4195 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
4196 ASTDeclContextNameLookupTrait> Generator;
4197 ASTDeclContextNameLookupTrait Trait(*this);
4199 // The first step is to collect the declaration names which we need to
4200 // serialize into the name lookup table, and to collect them in a stable
4201 // order.
4202 SmallVector<DeclarationName, 16> Names;
4204 // We also build up small sets of the constructor and conversion function
4205 // names which are visible.
4206 llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
4208 for (auto &Lookup : *DC->buildLookup()) {
4209 auto &Name = Lookup.first;
4210 auto &Result = Lookup.second;
4212 // If there are no local declarations in our lookup result, we
4213 // don't need to write an entry for the name at all. If we can't
4214 // write out a lookup set without performing more deserialization,
4215 // just skip this entry.
4217 // Also in reduced BMI, we'd like to avoid writing unreachable
4218 // declarations in GMF, so we need to avoid writing declarations
4219 // that entirely external or unreachable.
4221 // FIMXE: It looks sufficient to test
4222 // isLookupResultNotInteresting here. But due to bug we have
4223 // to test isLookupResultExternal here. See
4224 // https://github.com/llvm/llvm-project/issues/61065 for details.
4225 if ((GeneratingReducedBMI || isLookupResultExternal(Result, DC)) &&
4226 isLookupResultNotInteresting(*this, Result))
4227 continue;
4229 // We also skip empty results. If any of the results could be external and
4230 // the currently available results are empty, then all of the results are
4231 // external and we skip it above. So the only way we get here with an empty
4232 // results is when no results could have been external *and* we have
4233 // external results.
4235 // FIXME: While we might want to start emitting on-disk entries for negative
4236 // lookups into a decl context as an optimization, today we *have* to skip
4237 // them because there are names with empty lookup results in decl contexts
4238 // which we can't emit in any stable ordering: we lookup constructors and
4239 // conversion functions in the enclosing namespace scope creating empty
4240 // results for them. This in almost certainly a bug in Clang's name lookup,
4241 // but that is likely to be hard or impossible to fix and so we tolerate it
4242 // here by omitting lookups with empty results.
4243 if (Lookup.second.getLookupResult().empty())
4244 continue;
4246 switch (Lookup.first.getNameKind()) {
4247 default:
4248 Names.push_back(Lookup.first);
4249 break;
4251 case DeclarationName::CXXConstructorName:
4252 assert(isa<CXXRecordDecl>(DC) &&
4253 "Cannot have a constructor name outside of a class!");
4254 ConstructorNameSet.insert(Name);
4255 break;
4257 case DeclarationName::CXXConversionFunctionName:
4258 assert(isa<CXXRecordDecl>(DC) &&
4259 "Cannot have a conversion function name outside of a class!");
4260 ConversionNameSet.insert(Name);
4261 break;
4265 // Sort the names into a stable order.
4266 llvm::sort(Names);
4268 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
4269 // We need to establish an ordering of constructor and conversion function
4270 // names, and they don't have an intrinsic ordering.
4272 // First we try the easy case by forming the current context's constructor
4273 // name and adding that name first. This is a very useful optimization to
4274 // avoid walking the lexical declarations in many cases, and it also
4275 // handles the only case where a constructor name can come from some other
4276 // lexical context -- when that name is an implicit constructor merged from
4277 // another declaration in the redecl chain. Any non-implicit constructor or
4278 // conversion function which doesn't occur in all the lexical contexts
4279 // would be an ODR violation.
4280 auto ImplicitCtorName = Context.DeclarationNames.getCXXConstructorName(
4281 Context.getCanonicalType(Context.getRecordType(D)));
4282 if (ConstructorNameSet.erase(ImplicitCtorName))
4283 Names.push_back(ImplicitCtorName);
4285 // If we still have constructors or conversion functions, we walk all the
4286 // names in the decl and add the constructors and conversion functions
4287 // which are visible in the order they lexically occur within the context.
4288 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4289 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4290 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4291 auto Name = ChildND->getDeclName();
4292 switch (Name.getNameKind()) {
4293 default:
4294 continue;
4296 case DeclarationName::CXXConstructorName:
4297 if (ConstructorNameSet.erase(Name))
4298 Names.push_back(Name);
4299 break;
4301 case DeclarationName::CXXConversionFunctionName:
4302 if (ConversionNameSet.erase(Name))
4303 Names.push_back(Name);
4304 break;
4307 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4308 break;
4311 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4312 "constructors by walking all the "
4313 "lexical members of the context.");
4314 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4315 "conversion functions by walking all "
4316 "the lexical members of the context.");
4319 // Next we need to do a lookup with each name into this decl context to fully
4320 // populate any results from external sources. We don't actually use the
4321 // results of these lookups because we only want to use the results after all
4322 // results have been loaded and the pointers into them will be stable.
4323 for (auto &Name : Names)
4324 DC->lookup(Name);
4326 // Now we need to insert the results for each name into the hash table. For
4327 // constructor names and conversion function names, we actually need to merge
4328 // all of the results for them into one list of results each and insert
4329 // those.
4330 SmallVector<NamedDecl *, 8> ConstructorDecls;
4331 SmallVector<NamedDecl *, 8> ConversionDecls;
4333 // Now loop over the names, either inserting them or appending for the two
4334 // special cases.
4335 for (auto &Name : Names) {
4336 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4338 switch (Name.getNameKind()) {
4339 default:
4340 Generator.insert(Name, Trait.getData(Result), Trait);
4341 break;
4343 case DeclarationName::CXXConstructorName:
4344 ConstructorDecls.append(Result.begin(), Result.end());
4345 break;
4347 case DeclarationName::CXXConversionFunctionName:
4348 ConversionDecls.append(Result.begin(), Result.end());
4349 break;
4353 // Handle our two special cases if we ended up having any. We arbitrarily use
4354 // the first declaration's name here because the name itself isn't part of
4355 // the key, only the kind of name is used.
4356 if (!ConstructorDecls.empty())
4357 Generator.insert(ConstructorDecls.front()->getDeclName(),
4358 Trait.getData(ConstructorDecls), Trait);
4359 if (!ConversionDecls.empty())
4360 Generator.insert(ConversionDecls.front()->getDeclName(),
4361 Trait.getData(ConversionDecls), Trait);
4363 // Create the on-disk hash table. Also emit the existing imported and
4364 // merged table if there is one.
4365 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4366 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
4369 /// Write the block containing all of the declaration IDs
4370 /// visible from the given DeclContext.
4372 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4373 /// bitstream, or 0 if no block was written.
4374 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4375 DeclContext *DC) {
4376 // If we imported a key declaration of this namespace, write the visible
4377 // lookup results as an update record for it rather than including them
4378 // on this declaration. We will only look at key declarations on reload.
4379 if (isa<NamespaceDecl>(DC) && Chain &&
4380 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4381 // Only do this once, for the first local declaration of the namespace.
4382 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
4383 Prev = Prev->getPreviousDecl())
4384 if (!Prev->isFromASTFile())
4385 return 0;
4387 // Note that we need to emit an update record for the primary context.
4388 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4390 // Make sure all visible decls are written. They will be recorded later. We
4391 // do this using a side data structure so we can sort the names into
4392 // a deterministic order.
4393 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4394 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4395 LookupResults;
4396 if (Map) {
4397 LookupResults.reserve(Map->size());
4398 for (auto &Entry : *Map)
4399 LookupResults.push_back(
4400 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4403 llvm::sort(LookupResults, llvm::less_first());
4404 for (auto &NameAndResult : LookupResults) {
4405 DeclarationName Name = NameAndResult.first;
4406 DeclContext::lookup_result Result = NameAndResult.second;
4407 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4408 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4409 // We have to work around a name lookup bug here where negative lookup
4410 // results for these names get cached in namespace lookup tables (these
4411 // names should never be looked up in a namespace).
4412 assert(Result.empty() && "Cannot have a constructor or conversion "
4413 "function name in a namespace!");
4414 continue;
4417 for (NamedDecl *ND : Result) {
4418 if (ND->isFromASTFile())
4419 continue;
4421 if (DoneWritingDeclsAndTypes && !wasDeclEmitted(ND))
4422 continue;
4424 // We don't need to force emitting internal decls into reduced BMI.
4425 // See comments in ASTWriter::WriteDeclContextLexicalBlock for details.
4426 if (GeneratingReducedBMI && !ND->isFromExplicitGlobalModule() &&
4427 IsInternalDeclFromFileContext(ND))
4428 continue;
4430 GetDeclRef(ND);
4434 return 0;
4437 if (DC->getPrimaryContext() != DC)
4438 return 0;
4440 // Skip contexts which don't support name lookup.
4441 if (!DC->isLookupContext())
4442 return 0;
4444 // If not in C++, we perform name lookup for the translation unit via the
4445 // IdentifierInfo chains, don't bother to build a visible-declarations table.
4446 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4447 return 0;
4449 // Serialize the contents of the mapping used for lookup. Note that,
4450 // although we have two very different code paths, the serialized
4451 // representation is the same for both cases: a declaration name,
4452 // followed by a size, followed by references to the visible
4453 // declarations that have that name.
4454 uint64_t Offset = Stream.GetCurrentBitNo();
4455 StoredDeclsMap *Map = DC->buildLookup();
4456 if (!Map || Map->empty())
4457 return 0;
4459 // Create the on-disk hash table in a buffer.
4460 SmallString<4096> LookupTable;
4461 GenerateNameLookupTable(Context, DC, LookupTable);
4463 // Write the lookup table
4464 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4465 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
4466 LookupTable);
4467 ++NumVisibleDeclContexts;
4468 return Offset;
4471 /// Write an UPDATE_VISIBLE block for the given context.
4473 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4474 /// DeclContext in a dependent AST file. As such, they only exist for the TU
4475 /// (in C++), for namespaces, and for classes with forward-declared unscoped
4476 /// enumeration members (in C++11).
4477 void ASTWriter::WriteDeclContextVisibleUpdate(ASTContext &Context,
4478 const DeclContext *DC) {
4479 StoredDeclsMap *Map = DC->getLookupPtr();
4480 if (!Map || Map->empty())
4481 return;
4483 // Create the on-disk hash table in a buffer.
4484 SmallString<4096> LookupTable;
4485 GenerateNameLookupTable(Context, DC, LookupTable);
4487 // If we're updating a namespace, select a key declaration as the key for the
4488 // update record; those are the only ones that will be checked on reload.
4489 if (isa<NamespaceDecl>(DC))
4490 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4492 // Write the lookup table
4493 RecordData::value_type Record[] = {UPDATE_VISIBLE,
4494 getDeclID(cast<Decl>(DC)).getRawValue()};
4495 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
4498 /// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4499 void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4500 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4501 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4504 /// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4505 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4506 if (!SemaRef.Context.getLangOpts().OpenCL)
4507 return;
4509 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4510 RecordData Record;
4511 for (const auto &I:Opts.OptMap) {
4512 AddString(I.getKey(), Record);
4513 auto V = I.getValue();
4514 Record.push_back(V.Supported ? 1 : 0);
4515 Record.push_back(V.Enabled ? 1 : 0);
4516 Record.push_back(V.WithPragma ? 1 : 0);
4517 Record.push_back(V.Avail);
4518 Record.push_back(V.Core);
4519 Record.push_back(V.Opt);
4521 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4523 void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4524 if (SemaRef.CUDA().ForceHostDeviceDepth > 0) {
4525 RecordData::value_type Record[] = {SemaRef.CUDA().ForceHostDeviceDepth};
4526 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4530 void ASTWriter::WriteObjCCategories() {
4531 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4532 RecordData Categories;
4534 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4535 unsigned Size = 0;
4536 unsigned StartIndex = Categories.size();
4538 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4540 // Allocate space for the size.
4541 Categories.push_back(0);
4543 // Add the categories.
4544 for (ObjCInterfaceDecl::known_categories_iterator
4545 Cat = Class->known_categories_begin(),
4546 CatEnd = Class->known_categories_end();
4547 Cat != CatEnd; ++Cat, ++Size) {
4548 assert(getDeclID(*Cat).isValid() && "Bogus category");
4549 AddDeclRef(*Cat, Categories);
4552 // Update the size.
4553 Categories[StartIndex] = Size;
4555 // Record this interface -> category map.
4556 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4557 CategoriesMap.push_back(CatInfo);
4560 // Sort the categories map by the definition ID, since the reader will be
4561 // performing binary searches on this information.
4562 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4564 // Emit the categories map.
4565 using namespace llvm;
4567 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4568 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4571 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
4573 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4574 Stream.EmitRecordWithBlob(AbbrevID, Record,
4575 reinterpret_cast<char *>(CategoriesMap.data()),
4576 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4578 // Emit the category lists.
4579 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4582 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4583 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4585 if (LPTMap.empty())
4586 return;
4588 RecordData Record;
4589 for (auto &LPTMapEntry : LPTMap) {
4590 const FunctionDecl *FD = LPTMapEntry.first;
4591 LateParsedTemplate &LPT = *LPTMapEntry.second;
4592 AddDeclRef(FD, Record);
4593 AddDeclRef(LPT.D, Record);
4594 Record.push_back(LPT.FPO.getAsOpaqueInt());
4595 Record.push_back(LPT.Toks.size());
4597 for (const auto &Tok : LPT.Toks) {
4598 AddToken(Tok, Record);
4601 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4604 /// Write the state of 'pragma clang optimize' at the end of the module.
4605 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4606 RecordData Record;
4607 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4608 AddSourceLocation(PragmaLoc, Record);
4609 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4612 /// Write the state of 'pragma ms_struct' at the end of the module.
4613 void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4614 RecordData Record;
4615 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4616 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4619 /// Write the state of 'pragma pointers_to_members' at the end of the
4620 //module.
4621 void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4622 RecordData Record;
4623 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4624 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4625 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4628 /// Write the state of 'pragma align/pack' at the end of the module.
4629 void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4630 // Don't serialize pragma align/pack state for modules, since it should only
4631 // take effect on a per-submodule basis.
4632 if (WritingModule)
4633 return;
4635 RecordData Record;
4636 AddAlignPackInfo(SemaRef.AlignPackStack.CurrentValue, Record);
4637 AddSourceLocation(SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
4638 Record.push_back(SemaRef.AlignPackStack.Stack.size());
4639 for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
4640 AddAlignPackInfo(StackEntry.Value, Record);
4641 AddSourceLocation(StackEntry.PragmaLocation, Record);
4642 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4643 AddString(StackEntry.StackSlotLabel, Record);
4645 Stream.EmitRecord(ALIGN_PACK_PRAGMA_OPTIONS, Record);
4648 /// Write the state of 'pragma float_control' at the end of the module.
4649 void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4650 // Don't serialize pragma float_control state for modules,
4651 // since it should only take effect on a per-submodule basis.
4652 if (WritingModule)
4653 return;
4655 RecordData Record;
4656 Record.push_back(SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4657 AddSourceLocation(SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4658 Record.push_back(SemaRef.FpPragmaStack.Stack.size());
4659 for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4660 Record.push_back(StackEntry.Value.getAsOpaqueInt());
4661 AddSourceLocation(StackEntry.PragmaLocation, Record);
4662 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4663 AddString(StackEntry.StackSlotLabel, Record);
4665 Stream.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS, Record);
4668 /// Write Sema's collected list of declarations with unverified effects.
4669 void ASTWriter::WriteDeclsWithEffectsToVerify(Sema &SemaRef) {
4670 if (SemaRef.DeclsWithEffectsToVerify.empty())
4671 return;
4672 RecordData Record;
4673 for (const auto *D : SemaRef.DeclsWithEffectsToVerify) {
4674 AddDeclRef(D, Record);
4676 Stream.EmitRecord(DECLS_WITH_EFFECTS_TO_VERIFY, Record);
4679 void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4680 ModuleFileExtensionWriter &Writer) {
4681 // Enter the extension block.
4682 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4684 // Emit the metadata record abbreviation.
4685 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4686 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4687 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4688 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4689 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4690 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4691 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4692 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
4694 // Emit the metadata record.
4695 RecordData Record;
4696 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4697 Record.push_back(EXTENSION_METADATA);
4698 Record.push_back(Metadata.MajorVersion);
4699 Record.push_back(Metadata.MinorVersion);
4700 Record.push_back(Metadata.BlockName.size());
4701 Record.push_back(Metadata.UserInfo.size());
4702 SmallString<64> Buffer;
4703 Buffer += Metadata.BlockName;
4704 Buffer += Metadata.UserInfo;
4705 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4707 // Emit the contents of the extension block.
4708 Writer.writeExtensionContents(SemaRef, Stream);
4710 // Exit the extension block.
4711 Stream.ExitBlock();
4714 //===----------------------------------------------------------------------===//
4715 // General Serialization Routines
4716 //===----------------------------------------------------------------------===//
4718 void ASTRecordWriter::AddAttr(const Attr *A) {
4719 auto &Record = *this;
4720 // FIXME: Clang can't handle the serialization/deserialization of
4721 // preferred_name properly now. See
4722 // https://github.com/llvm/llvm-project/issues/56490 for example.
4723 if (!A || (isa<PreferredNameAttr>(A) &&
4724 Writer->isWritingStdCXXNamedModules()))
4725 return Record.push_back(0);
4727 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4729 Record.AddIdentifierRef(A->getAttrName());
4730 Record.AddIdentifierRef(A->getScopeName());
4731 Record.AddSourceRange(A->getRange());
4732 Record.AddSourceLocation(A->getScopeLoc());
4733 Record.push_back(A->getParsedKind());
4734 Record.push_back(A->getSyntax());
4735 Record.push_back(A->getAttributeSpellingListIndexRaw());
4736 Record.push_back(A->isRegularKeywordAttribute());
4738 #include "clang/Serialization/AttrPCHWrite.inc"
4741 /// Emit the list of attributes to the specified record.
4742 void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4743 push_back(Attrs.size());
4744 for (const auto *A : Attrs)
4745 AddAttr(A);
4748 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4749 AddSourceLocation(Tok.getLocation(), Record);
4750 // FIXME: Should translate token kind to a stable encoding.
4751 Record.push_back(Tok.getKind());
4752 // FIXME: Should translate token flags to a stable encoding.
4753 Record.push_back(Tok.getFlags());
4755 if (Tok.isAnnotation()) {
4756 AddSourceLocation(Tok.getAnnotationEndLoc(), Record);
4757 switch (Tok.getKind()) {
4758 case tok::annot_pragma_loop_hint: {
4759 auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
4760 AddToken(Info->PragmaName, Record);
4761 AddToken(Info->Option, Record);
4762 Record.push_back(Info->Toks.size());
4763 for (const auto &T : Info->Toks)
4764 AddToken(T, Record);
4765 break;
4767 case tok::annot_pragma_pack: {
4768 auto *Info =
4769 static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
4770 Record.push_back(static_cast<unsigned>(Info->Action));
4771 AddString(Info->SlotLabel, Record);
4772 AddToken(Info->Alignment, Record);
4773 break;
4775 // Some annotation tokens do not use the PtrData field.
4776 case tok::annot_pragma_openmp:
4777 case tok::annot_pragma_openmp_end:
4778 case tok::annot_pragma_unused:
4779 case tok::annot_pragma_openacc:
4780 case tok::annot_pragma_openacc_end:
4781 case tok::annot_repl_input_end:
4782 break;
4783 default:
4784 llvm_unreachable("missing serialization code for annotation token");
4786 } else {
4787 Record.push_back(Tok.getLength());
4788 // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4789 // is needed.
4790 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4794 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4795 Record.push_back(Str.size());
4796 Record.insert(Record.end(), Str.begin(), Str.end());
4799 void ASTWriter::AddStringBlob(StringRef Str, RecordDataImpl &Record,
4800 SmallVectorImpl<char> &Blob) {
4801 Record.push_back(Str.size());
4802 Blob.insert(Blob.end(), Str.begin(), Str.end());
4805 bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4806 assert(WritingAST && "can't prepare path for output when not writing AST");
4808 // Leave special file names as they are.
4809 StringRef PathStr(Path.data(), Path.size());
4810 if (PathStr == "<built-in>" || PathStr == "<command line>")
4811 return false;
4813 bool Changed = cleanPathForOutput(PP->getFileManager(), Path);
4815 // Remove a prefix to make the path relative, if relevant.
4816 const char *PathBegin = Path.data();
4817 const char *PathPtr =
4818 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4819 if (PathPtr != PathBegin) {
4820 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4821 Changed = true;
4824 return Changed;
4827 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4828 SmallString<128> FilePath(Path);
4829 PreparePathForOutput(FilePath);
4830 AddString(FilePath, Record);
4833 void ASTWriter::AddPathBlob(StringRef Path, RecordDataImpl &Record,
4834 SmallVectorImpl<char> &Blob) {
4835 SmallString<128> FilePath(Path);
4836 PreparePathForOutput(FilePath);
4837 AddStringBlob(FilePath, Record, Blob);
4840 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4841 StringRef Path) {
4842 SmallString<128> FilePath(Path);
4843 PreparePathForOutput(FilePath);
4844 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4847 void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4848 RecordDataImpl &Record) {
4849 Record.push_back(Version.getMajor());
4850 if (std::optional<unsigned> Minor = Version.getMinor())
4851 Record.push_back(*Minor + 1);
4852 else
4853 Record.push_back(0);
4854 if (std::optional<unsigned> Subminor = Version.getSubminor())
4855 Record.push_back(*Subminor + 1);
4856 else
4857 Record.push_back(0);
4860 /// Note that the identifier II occurs at the given offset
4861 /// within the identifier table.
4862 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4863 IdentifierID ID = IdentifierIDs[II];
4864 // Only store offsets new to this AST file. Other identifier names are looked
4865 // up earlier in the chain and thus don't need an offset.
4866 if (!isLocalIdentifierID(ID))
4867 return;
4869 // For local identifiers, the module file index must be 0.
4871 assert(ID != 0);
4872 ID -= NUM_PREDEF_IDENT_IDS;
4873 assert(ID < IdentifierOffsets.size());
4874 IdentifierOffsets[ID] = Offset;
4877 /// Note that the selector Sel occurs at the given offset
4878 /// within the method pool/selector table.
4879 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4880 unsigned ID = SelectorIDs[Sel];
4881 assert(ID && "Unknown selector");
4882 // Don't record offsets for selectors that are also available in a different
4883 // file.
4884 if (ID < FirstSelectorID)
4885 return;
4886 SelectorOffsets[ID - FirstSelectorID] = Offset;
4889 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4890 SmallVectorImpl<char> &Buffer,
4891 InMemoryModuleCache &ModuleCache,
4892 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4893 bool IncludeTimestamps, bool BuildingImplicitModule,
4894 bool GeneratingReducedBMI)
4895 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4896 IncludeTimestamps(IncludeTimestamps),
4897 BuildingImplicitModule(BuildingImplicitModule),
4898 GeneratingReducedBMI(GeneratingReducedBMI) {
4899 for (const auto &Ext : Extensions) {
4900 if (auto Writer = Ext->createExtensionWriter(*this))
4901 ModuleFileExtensionWriters.push_back(std::move(Writer));
4905 ASTWriter::~ASTWriter() = default;
4907 const LangOptions &ASTWriter::getLangOpts() const {
4908 assert(WritingAST && "can't determine lang opts when not writing AST");
4909 return PP->getLangOpts();
4912 time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4913 return IncludeTimestamps ? E->getModificationTime() : 0;
4916 ASTFileSignature
4917 ASTWriter::WriteAST(llvm::PointerUnion<Sema *, Preprocessor *> Subject,
4918 StringRef OutputFile, Module *WritingModule,
4919 StringRef isysroot, bool ShouldCacheASTInMemory) {
4920 llvm::TimeTraceScope scope("WriteAST", OutputFile);
4921 WritingAST = true;
4923 Sema *SemaPtr = Subject.dyn_cast<Sema *>();
4924 Preprocessor &PPRef =
4925 SemaPtr ? SemaPtr->getPreprocessor() : *Subject.get<Preprocessor *>();
4927 ASTHasCompilerErrors = PPRef.getDiagnostics().hasUncompilableErrorOccurred();
4929 // Emit the file header.
4930 Stream.Emit((unsigned)'C', 8);
4931 Stream.Emit((unsigned)'P', 8);
4932 Stream.Emit((unsigned)'C', 8);
4933 Stream.Emit((unsigned)'H', 8);
4935 WriteBlockInfoBlock();
4937 PP = &PPRef;
4938 this->WritingModule = WritingModule;
4939 ASTFileSignature Signature = WriteASTCore(SemaPtr, isysroot, WritingModule);
4940 PP = nullptr;
4941 this->WritingModule = nullptr;
4942 this->BaseDirectory.clear();
4944 WritingAST = false;
4946 if (WritingModule && PPRef.getHeaderSearchInfo()
4947 .getHeaderSearchOpts()
4948 .ModulesValidateOncePerBuildSession)
4949 updateModuleTimestamp(OutputFile);
4951 if (ShouldCacheASTInMemory) {
4952 // Construct MemoryBuffer and update buffer manager.
4953 ModuleCache.addBuiltPCM(OutputFile,
4954 llvm::MemoryBuffer::getMemBufferCopy(
4955 StringRef(Buffer.begin(), Buffer.size())));
4957 return Signature;
4960 template<typename Vector>
4961 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec) {
4962 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4963 I != E; ++I) {
4964 Writer.GetDeclRef(*I);
4968 template <typename Vector>
4969 static void AddLazyVectorEmiitedDecls(ASTWriter &Writer, Vector &Vec,
4970 ASTWriter::RecordData &Record) {
4971 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4972 I != E; ++I) {
4973 Writer.AddEmittedDeclRef(*I, Record);
4977 void ASTWriter::computeNonAffectingInputFiles() {
4978 SourceManager &SrcMgr = PP->getSourceManager();
4979 unsigned N = SrcMgr.local_sloc_entry_size();
4981 IsSLocAffecting.resize(N, true);
4982 IsSLocFileEntryAffecting.resize(N, true);
4984 if (!WritingModule)
4985 return;
4987 auto AffectingModuleMaps = GetAffectingModuleMaps(*PP, WritingModule);
4989 unsigned FileIDAdjustment = 0;
4990 unsigned OffsetAdjustment = 0;
4992 NonAffectingFileIDAdjustments.reserve(N);
4993 NonAffectingOffsetAdjustments.reserve(N);
4995 NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4996 NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4998 for (unsigned I = 1; I != N; ++I) {
4999 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(I);
5000 FileID FID = FileID::get(I);
5001 assert(&SrcMgr.getSLocEntry(FID) == SLoc);
5003 if (!SLoc->isFile())
5004 continue;
5005 const SrcMgr::FileInfo &File = SLoc->getFile();
5006 const SrcMgr::ContentCache *Cache = &File.getContentCache();
5007 if (!Cache->OrigEntry)
5008 continue;
5010 // Don't prune anything other than module maps.
5011 if (!isModuleMap(File.getFileCharacteristic()))
5012 continue;
5014 // Don't prune module maps if all are guaranteed to be affecting.
5015 if (!AffectingModuleMaps)
5016 continue;
5018 // Don't prune module maps that are affecting.
5019 if (AffectingModuleMaps->DefinitionFileIDs.contains(FID))
5020 continue;
5022 IsSLocAffecting[I] = false;
5023 IsSLocFileEntryAffecting[I] =
5024 AffectingModuleMaps->DefinitionFiles.contains(*Cache->OrigEntry);
5026 FileIDAdjustment += 1;
5027 // Even empty files take up one element in the offset table.
5028 OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
5030 // If the previous file was non-affecting as well, just extend its entry
5031 // with our information.
5032 if (!NonAffectingFileIDs.empty() &&
5033 NonAffectingFileIDs.back().ID == FID.ID - 1) {
5034 NonAffectingFileIDs.back() = FID;
5035 NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
5036 NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
5037 NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
5038 continue;
5041 NonAffectingFileIDs.push_back(FID);
5042 NonAffectingRanges.emplace_back(SrcMgr.getLocForStartOfFile(FID),
5043 SrcMgr.getLocForEndOfFile(FID));
5044 NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
5045 NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
5048 if (!PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesIncludeVFSUsage)
5049 return;
5051 FileManager &FileMgr = PP->getFileManager();
5052 FileMgr.trackVFSUsage(true);
5053 // Lookup the paths in the VFS to trigger `-ivfsoverlay` usage tracking.
5054 for (StringRef Path :
5055 PP->getHeaderSearchInfo().getHeaderSearchOpts().VFSOverlayFiles)
5056 FileMgr.getVirtualFileSystem().exists(Path);
5057 for (unsigned I = 1; I != N; ++I) {
5058 if (IsSLocAffecting[I]) {
5059 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(I);
5060 if (!SLoc->isFile())
5061 continue;
5062 const SrcMgr::FileInfo &File = SLoc->getFile();
5063 const SrcMgr::ContentCache *Cache = &File.getContentCache();
5064 if (!Cache->OrigEntry)
5065 continue;
5066 FileMgr.getVirtualFileSystem().exists(
5067 Cache->OrigEntry->getNameAsRequested());
5070 FileMgr.trackVFSUsage(false);
5073 void ASTWriter::PrepareWritingSpecialDecls(Sema &SemaRef) {
5074 ASTContext &Context = SemaRef.Context;
5076 bool isModule = WritingModule != nullptr;
5078 // Set up predefined declaration IDs.
5079 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
5080 if (D) {
5081 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
5082 DeclIDs[D] = ID;
5083 PredefinedDecls.insert(D);
5086 RegisterPredefDecl(Context.getTranslationUnitDecl(),
5087 PREDEF_DECL_TRANSLATION_UNIT_ID);
5088 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
5089 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
5090 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
5091 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
5092 PREDEF_DECL_OBJC_PROTOCOL_ID);
5093 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
5094 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
5095 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
5096 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
5097 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
5098 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
5099 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
5100 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
5101 RegisterPredefDecl(Context.MSGuidTagDecl,
5102 PREDEF_DECL_BUILTIN_MS_GUID_ID);
5103 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
5104 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
5105 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
5106 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
5107 PREDEF_DECL_CF_CONSTANT_STRING_ID);
5108 RegisterPredefDecl(Context.CFConstantStringTagDecl,
5109 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
5110 RegisterPredefDecl(Context.TypePackElementDecl,
5111 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
5112 RegisterPredefDecl(Context.BuiltinCommonTypeDecl, PREDEF_DECL_COMMON_TYPE_ID);
5114 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5116 // Force all top level declarations to be emitted.
5118 // We start emitting top level declarations from the module purview to
5119 // implement the eliding unreachable declaration feature.
5120 for (const auto *D : TU->noload_decls()) {
5121 if (D->isFromASTFile())
5122 continue;
5124 if (GeneratingReducedBMI) {
5125 if (D->isFromExplicitGlobalModule())
5126 continue;
5128 // Don't force emitting static entities.
5130 // Technically, all static entities shouldn't be in reduced BMI. The
5131 // language also specifies that the program exposes TU-local entities
5132 // is ill-formed. However, in practice, there are a lot of projects
5133 // uses `static inline` in the headers. So we can't get rid of all
5134 // static entities in reduced BMI now.
5135 if (IsInternalDeclFromFileContext(D))
5136 continue;
5139 // If we're writing C++ named modules, don't emit declarations which are
5140 // not from modules by default. They may be built in declarations (be
5141 // handled above) or implcit declarations (see the implementation of
5142 // `Sema::Initialize()` for example).
5143 if (isWritingStdCXXNamedModules() && !D->getOwningModule() &&
5144 D->isImplicit())
5145 continue;
5147 GetDeclRef(D);
5150 if (GeneratingReducedBMI)
5151 return;
5153 // Writing all of the tentative definitions in this file, in
5154 // TentativeDefinitions order. Generally, this record will be empty for
5155 // headers.
5156 RecordData TentativeDefinitions;
5157 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions);
5159 // Writing all of the file scoped decls in this file.
5160 if (!isModule)
5161 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls);
5163 // Writing all of the delegating constructors we still need
5164 // to resolve.
5165 if (!isModule)
5166 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls);
5168 // Writing all of the ext_vector declarations.
5169 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls);
5171 // Writing all of the VTable uses information.
5172 if (!SemaRef.VTableUses.empty())
5173 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I)
5174 GetDeclRef(SemaRef.VTableUses[I].first);
5176 // Writing all of the UnusedLocalTypedefNameCandidates.
5177 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5178 GetDeclRef(TD);
5180 // Writing all of pending implicit instantiations.
5181 for (const auto &I : SemaRef.PendingInstantiations)
5182 GetDeclRef(I.first);
5183 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
5184 "There are local ones at end of translation unit!");
5186 // Writing some declaration references.
5187 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5188 GetDeclRef(SemaRef.getStdNamespace());
5189 GetDeclRef(SemaRef.getStdBadAlloc());
5190 GetDeclRef(SemaRef.getStdAlignValT());
5193 if (Context.getcudaConfigureCallDecl())
5194 GetDeclRef(Context.getcudaConfigureCallDecl());
5196 // Writing all of the known namespaces.
5197 for (const auto &I : SemaRef.KnownNamespaces)
5198 if (!I.second)
5199 GetDeclRef(I.first);
5201 // Writing all used, undefined objects that require definitions.
5202 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5203 SemaRef.getUndefinedButUsed(Undefined);
5204 for (const auto &I : Undefined)
5205 GetDeclRef(I.first);
5207 // Writing all delete-expressions that we would like to
5208 // analyze later in AST.
5209 if (!isModule)
5210 for (const auto &DeleteExprsInfo :
5211 SemaRef.getMismatchingDeleteExpressions())
5212 GetDeclRef(DeleteExprsInfo.first);
5214 // Make sure visible decls, added to DeclContexts previously loaded from
5215 // an AST file, are registered for serialization. Likewise for template
5216 // specializations added to imported templates.
5217 for (const auto *I : DeclsToEmitEvenIfUnreferenced)
5218 GetDeclRef(I);
5219 DeclsToEmitEvenIfUnreferenced.clear();
5221 // Make sure all decls associated with an identifier are registered for
5222 // serialization, if we're storing decls with identifiers.
5223 if (!WritingModule || !getLangOpts().CPlusPlus) {
5224 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
5225 for (const auto &ID : SemaRef.PP.getIdentifierTable()) {
5226 const IdentifierInfo *II = ID.second;
5227 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
5228 IIs.push_back(II);
5230 // Sort the identifiers to visit based on their name.
5231 llvm::sort(IIs, llvm::deref<std::less<>>());
5232 for (const IdentifierInfo *II : IIs)
5233 for (const Decl *D : SemaRef.IdResolver.decls(II))
5234 GetDeclRef(D);
5237 // Write all of the DeclsToCheckForDeferredDiags.
5238 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5239 GetDeclRef(D);
5241 // Write all classes that need to emit the vtable definitions if required.
5242 if (isWritingStdCXXNamedModules())
5243 for (CXXRecordDecl *RD : PendingEmittingVTables)
5244 GetDeclRef(RD);
5245 else
5246 PendingEmittingVTables.clear();
5249 void ASTWriter::WriteSpecialDeclRecords(Sema &SemaRef) {
5250 ASTContext &Context = SemaRef.Context;
5252 bool isModule = WritingModule != nullptr;
5254 // Write the record containing external, unnamed definitions.
5255 if (!EagerlyDeserializedDecls.empty())
5256 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
5258 if (!ModularCodegenDecls.empty())
5259 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5261 // Write the record containing tentative definitions.
5262 RecordData TentativeDefinitions;
5263 AddLazyVectorEmiitedDecls(*this, SemaRef.TentativeDefinitions,
5264 TentativeDefinitions);
5265 if (!TentativeDefinitions.empty())
5266 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
5268 // Write the record containing unused file scoped decls.
5269 RecordData UnusedFileScopedDecls;
5270 if (!isModule)
5271 AddLazyVectorEmiitedDecls(*this, SemaRef.UnusedFileScopedDecls,
5272 UnusedFileScopedDecls);
5273 if (!UnusedFileScopedDecls.empty())
5274 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
5276 // Write the record containing ext_vector type names.
5277 RecordData ExtVectorDecls;
5278 AddLazyVectorEmiitedDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
5279 if (!ExtVectorDecls.empty())
5280 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
5282 // Write the record containing VTable uses information.
5283 RecordData VTableUses;
5284 if (!SemaRef.VTableUses.empty()) {
5285 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
5286 CXXRecordDecl *D = SemaRef.VTableUses[I].first;
5287 if (!wasDeclEmitted(D))
5288 continue;
5290 AddDeclRef(D, VTableUses);
5291 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
5292 VTableUses.push_back(SemaRef.VTablesUsed[D]);
5294 Stream.EmitRecord(VTABLE_USES, VTableUses);
5297 // Write the record containing potentially unused local typedefs.
5298 RecordData UnusedLocalTypedefNameCandidates;
5299 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
5300 AddEmittedDeclRef(TD, UnusedLocalTypedefNameCandidates);
5301 if (!UnusedLocalTypedefNameCandidates.empty())
5302 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5303 UnusedLocalTypedefNameCandidates);
5305 // Write the record containing pending implicit instantiations.
5306 RecordData PendingInstantiations;
5307 for (const auto &I : SemaRef.PendingInstantiations) {
5308 if (!wasDeclEmitted(I.first))
5309 continue;
5311 AddDeclRef(I.first, PendingInstantiations);
5312 AddSourceLocation(I.second, PendingInstantiations);
5314 if (!PendingInstantiations.empty())
5315 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
5317 // Write the record containing declaration references of Sema.
5318 RecordData SemaDeclRefs;
5319 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
5320 auto AddEmittedDeclRefOrZero = [this, &SemaDeclRefs](Decl *D) {
5321 if (!D || !wasDeclEmitted(D))
5322 SemaDeclRefs.push_back(0);
5323 else
5324 AddDeclRef(D, SemaDeclRefs);
5327 AddEmittedDeclRefOrZero(SemaRef.getStdNamespace());
5328 AddEmittedDeclRefOrZero(SemaRef.getStdBadAlloc());
5329 AddEmittedDeclRefOrZero(SemaRef.getStdAlignValT());
5331 if (!SemaDeclRefs.empty())
5332 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
5334 // Write the record containing decls to be checked for deferred diags.
5335 RecordData DeclsToCheckForDeferredDiags;
5336 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5337 if (wasDeclEmitted(D))
5338 AddDeclRef(D, DeclsToCheckForDeferredDiags);
5339 if (!DeclsToCheckForDeferredDiags.empty())
5340 Stream.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5341 DeclsToCheckForDeferredDiags);
5343 // Write the record containing CUDA-specific declaration references.
5344 RecordData CUDASpecialDeclRefs;
5345 if (auto *CudaCallDecl = Context.getcudaConfigureCallDecl();
5346 CudaCallDecl && wasDeclEmitted(CudaCallDecl)) {
5347 AddDeclRef(CudaCallDecl, CUDASpecialDeclRefs);
5348 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
5351 // Write the delegating constructors.
5352 RecordData DelegatingCtorDecls;
5353 if (!isModule)
5354 AddLazyVectorEmiitedDecls(*this, SemaRef.DelegatingCtorDecls,
5355 DelegatingCtorDecls);
5356 if (!DelegatingCtorDecls.empty())
5357 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
5359 // Write the known namespaces.
5360 RecordData KnownNamespaces;
5361 for (const auto &I : SemaRef.KnownNamespaces) {
5362 if (!I.second && wasDeclEmitted(I.first))
5363 AddDeclRef(I.first, KnownNamespaces);
5365 if (!KnownNamespaces.empty())
5366 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
5368 // Write the undefined internal functions and variables, and inline functions.
5369 RecordData UndefinedButUsed;
5370 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
5371 SemaRef.getUndefinedButUsed(Undefined);
5372 for (const auto &I : Undefined) {
5373 if (!wasDeclEmitted(I.first))
5374 continue;
5376 AddDeclRef(I.first, UndefinedButUsed);
5377 AddSourceLocation(I.second, UndefinedButUsed);
5379 if (!UndefinedButUsed.empty())
5380 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
5382 // Write all delete-expressions that we would like to
5383 // analyze later in AST.
5384 RecordData DeleteExprsToAnalyze;
5385 if (!isModule) {
5386 for (const auto &DeleteExprsInfo :
5387 SemaRef.getMismatchingDeleteExpressions()) {
5388 if (!wasDeclEmitted(DeleteExprsInfo.first))
5389 continue;
5391 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
5392 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
5393 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
5394 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
5395 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
5399 if (!DeleteExprsToAnalyze.empty())
5400 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5402 RecordData VTablesToEmit;
5403 for (CXXRecordDecl *RD : PendingEmittingVTables) {
5404 if (!wasDeclEmitted(RD))
5405 continue;
5407 AddDeclRef(RD, VTablesToEmit);
5410 if (!VTablesToEmit.empty())
5411 Stream.EmitRecord(VTABLES_TO_EMIT, VTablesToEmit);
5414 ASTFileSignature ASTWriter::WriteASTCore(Sema *SemaPtr, StringRef isysroot,
5415 Module *WritingModule) {
5416 using namespace llvm;
5418 bool isModule = WritingModule != nullptr;
5420 // Make sure that the AST reader knows to finalize itself.
5421 if (Chain)
5422 Chain->finalizeForWriting();
5424 // This needs to be done very early, since everything that writes
5425 // SourceLocations or FileIDs depends on it.
5426 computeNonAffectingInputFiles();
5428 writeUnhashedControlBlock(*PP);
5430 // Don't reuse type ID and Identifier ID from readers for C++ standard named
5431 // modules since we want to support no-transitive-change model for named
5432 // modules. The theory for no-transitive-change model is,
5433 // for a user of a named module, the user can only access the indirectly
5434 // imported decls via the directly imported module. So that it is possible to
5435 // control what matters to the users when writing the module. It would be
5436 // problematic if the users can reuse the type IDs and identifier IDs from
5437 // indirectly imported modules arbitrarily. So we choose to clear these ID
5438 // here.
5439 if (isWritingStdCXXNamedModules()) {
5440 TypeIdxs.clear();
5441 IdentifierIDs.clear();
5444 // Look for any identifiers that were named while processing the
5445 // headers, but are otherwise not needed. We add these to the hash
5446 // table to enable checking of the predefines buffer in the case
5447 // where the user adds new macro definitions when building the AST
5448 // file.
5450 // We do this before emitting any Decl and Types to make sure the
5451 // Identifier ID is stable.
5452 SmallVector<const IdentifierInfo *, 128> IIs;
5453 for (const auto &ID : PP->getIdentifierTable())
5454 if (IsInterestingNonMacroIdentifier(ID.second, *this))
5455 IIs.push_back(ID.second);
5456 // Sort the identifiers lexicographically before getting the references so
5457 // that their order is stable.
5458 llvm::sort(IIs, llvm::deref<std::less<>>());
5459 for (const IdentifierInfo *II : IIs)
5460 getIdentifierRef(II);
5462 // Write the set of weak, undeclared identifiers. We always write the
5463 // entire table, since later PCH files in a PCH chain are only interested in
5464 // the results at the end of the chain.
5465 RecordData WeakUndeclaredIdentifiers;
5466 if (SemaPtr) {
5467 for (const auto &WeakUndeclaredIdentifierList :
5468 SemaPtr->WeakUndeclaredIdentifiers) {
5469 const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
5470 for (const auto &WI : WeakUndeclaredIdentifierList.second) {
5471 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
5472 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
5473 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
5478 // Form the record of special types.
5479 RecordData SpecialTypes;
5480 if (SemaPtr) {
5481 ASTContext &Context = SemaPtr->Context;
5482 AddTypeRef(Context, Context.getRawCFConstantStringType(), SpecialTypes);
5483 AddTypeRef(Context, Context.getFILEType(), SpecialTypes);
5484 AddTypeRef(Context, Context.getjmp_bufType(), SpecialTypes);
5485 AddTypeRef(Context, Context.getsigjmp_bufType(), SpecialTypes);
5486 AddTypeRef(Context, Context.ObjCIdRedefinitionType, SpecialTypes);
5487 AddTypeRef(Context, Context.ObjCClassRedefinitionType, SpecialTypes);
5488 AddTypeRef(Context, Context.ObjCSelRedefinitionType, SpecialTypes);
5489 AddTypeRef(Context, Context.getucontext_tType(), SpecialTypes);
5492 if (SemaPtr)
5493 PrepareWritingSpecialDecls(*SemaPtr);
5495 // Write the control block
5496 WriteControlBlock(*PP, isysroot);
5498 // Write the remaining AST contents.
5499 Stream.FlushToWord();
5500 ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
5501 Stream.EnterSubblock(AST_BLOCK_ID, 5);
5502 ASTBlockStartOffset = Stream.GetCurrentBitNo();
5504 // This is so that older clang versions, before the introduction
5505 // of the control block, can read and reject the newer PCH format.
5507 RecordData Record = {VERSION_MAJOR};
5508 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
5511 // For method pool in the module, if it contains an entry for a selector,
5512 // the entry should be complete, containing everything introduced by that
5513 // module and all modules it imports. It's possible that the entry is out of
5514 // date, so we need to pull in the new content here.
5516 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
5517 // safe, we copy all selectors out.
5518 if (SemaPtr) {
5519 llvm::SmallVector<Selector, 256> AllSelectors;
5520 for (auto &SelectorAndID : SelectorIDs)
5521 AllSelectors.push_back(SelectorAndID.first);
5522 for (auto &Selector : AllSelectors)
5523 SemaPtr->ObjC().updateOutOfDateSelector(Selector);
5526 if (Chain) {
5527 // Write the mapping information describing our module dependencies and how
5528 // each of those modules were mapped into our own offset/ID space, so that
5529 // the reader can build the appropriate mapping to its own offset/ID space.
5530 // The map consists solely of a blob with the following format:
5531 // *(module-kind:i8
5532 // module-name-len:i16 module-name:len*i8
5533 // source-location-offset:i32
5534 // identifier-id:i32
5535 // preprocessed-entity-id:i32
5536 // macro-definition-id:i32
5537 // submodule-id:i32
5538 // selector-id:i32
5539 // declaration-id:i32
5540 // c++-base-specifiers-id:i32
5541 // type-id:i32)
5543 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
5544 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
5545 // module name. Otherwise, it is the module file name.
5546 auto Abbrev = std::make_shared<BitCodeAbbrev>();
5547 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
5548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5549 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
5550 SmallString<2048> Buffer;
5552 llvm::raw_svector_ostream Out(Buffer);
5553 for (ModuleFile &M : Chain->ModuleMgr) {
5554 using namespace llvm::support;
5556 endian::Writer LE(Out, llvm::endianness::little);
5557 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
5558 StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
5559 LE.write<uint16_t>(Name.size());
5560 Out.write(Name.data(), Name.size());
5562 // Note: if a base ID was uint max, it would not be possible to load
5563 // another module after it or have more than one entity inside it.
5564 uint32_t None = std::numeric_limits<uint32_t>::max();
5566 auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
5567 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5568 if (ShouldWrite)
5569 LE.write<uint32_t>(BaseID);
5570 else
5571 LE.write<uint32_t>(None);
5574 // These values should be unique within a chain, since they will be read
5575 // as keys into ContinuousRangeMaps.
5576 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5577 writeBaseIDOrNone(M.BasePreprocessedEntityID,
5578 M.NumPreprocessedEntities);
5579 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5580 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5583 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
5584 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
5585 Buffer.data(), Buffer.size());
5588 if (SemaPtr)
5589 WriteDeclAndTypes(SemaPtr->Context);
5591 WriteFileDeclIDsMap();
5592 WriteSourceManagerBlock(PP->getSourceManager());
5593 if (SemaPtr)
5594 WriteComments(SemaPtr->Context);
5595 WritePreprocessor(*PP, isModule);
5596 WriteHeaderSearch(PP->getHeaderSearchInfo());
5597 if (SemaPtr) {
5598 WriteSelectors(*SemaPtr);
5599 WriteReferencedSelectorsPool(*SemaPtr);
5600 WriteLateParsedTemplates(*SemaPtr);
5602 WriteIdentifierTable(*PP, SemaPtr ? &SemaPtr->IdResolver : nullptr, isModule);
5603 if (SemaPtr) {
5604 WriteFPPragmaOptions(SemaPtr->CurFPFeatureOverrides());
5605 WriteOpenCLExtensions(*SemaPtr);
5606 WriteCUDAPragmas(*SemaPtr);
5609 // If we're emitting a module, write out the submodule information.
5610 if (WritingModule)
5611 WriteSubmodules(WritingModule, SemaPtr ? &SemaPtr->Context : nullptr);
5613 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5615 if (SemaPtr)
5616 WriteSpecialDeclRecords(*SemaPtr);
5618 // Write the record containing weak undeclared identifiers.
5619 if (!WeakUndeclaredIdentifiers.empty())
5620 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
5621 WeakUndeclaredIdentifiers);
5623 if (!WritingModule) {
5624 // Write the submodules that were imported, if any.
5625 struct ModuleInfo {
5626 uint64_t ID;
5627 Module *M;
5628 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5630 llvm::SmallVector<ModuleInfo, 64> Imports;
5631 if (SemaPtr) {
5632 for (const auto *I : SemaPtr->Context.local_imports()) {
5633 assert(SubmoduleIDs.contains(I->getImportedModule()));
5634 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5635 I->getImportedModule()));
5639 if (!Imports.empty()) {
5640 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5641 return A.ID < B.ID;
5643 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5644 return A.ID == B.ID;
5647 // Sort and deduplicate module IDs.
5648 llvm::sort(Imports, Cmp);
5649 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
5650 Imports.end());
5652 RecordData ImportedModules;
5653 for (const auto &Import : Imports) {
5654 ImportedModules.push_back(Import.ID);
5655 // FIXME: If the module has macros imported then later has declarations
5656 // imported, this location won't be the right one as a location for the
5657 // declaration imports.
5658 AddSourceLocation(PP->getModuleImportLoc(Import.M), ImportedModules);
5661 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5665 WriteObjCCategories();
5666 if (SemaPtr) {
5667 if (!WritingModule) {
5668 WriteOptimizePragmaOptions(*SemaPtr);
5669 WriteMSStructPragmaOptions(*SemaPtr);
5670 WriteMSPointersToMembersPragmaOptions(*SemaPtr);
5672 WritePackPragmaOptions(*SemaPtr);
5673 WriteFloatControlPragmaOptions(*SemaPtr);
5674 WriteDeclsWithEffectsToVerify(*SemaPtr);
5677 // Some simple statistics
5678 RecordData::value_type Record[] = {
5679 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
5680 Stream.EmitRecord(STATISTICS, Record);
5681 Stream.ExitBlock();
5682 Stream.FlushToWord();
5683 ASTBlockRange.second = Stream.GetCurrentBitNo() >> 3;
5685 // Write the module file extension blocks.
5686 if (SemaPtr)
5687 for (const auto &ExtWriter : ModuleFileExtensionWriters)
5688 WriteModuleFileExtension(*SemaPtr, *ExtWriter);
5690 return backpatchSignature();
5693 void ASTWriter::EnteringModulePurview() {
5694 // In C++20 named modules, all entities before entering the module purview
5695 // lives in the GMF.
5696 if (GeneratingReducedBMI)
5697 DeclUpdatesFromGMF.swap(DeclUpdates);
5700 // Add update records for all mangling numbers and static local numbers.
5701 // These aren't really update records, but this is a convenient way of
5702 // tagging this rare extra data onto the declarations.
5703 void ASTWriter::AddedManglingNumber(const Decl *D, unsigned Number) {
5704 if (D->isFromASTFile())
5705 return;
5707 DeclUpdates[D].push_back(DeclUpdate(UPD_MANGLING_NUMBER, Number));
5709 void ASTWriter::AddedStaticLocalNumbers(const Decl *D, unsigned Number) {
5710 if (D->isFromASTFile())
5711 return;
5713 DeclUpdates[D].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER, Number));
5716 void ASTWriter::AddedAnonymousNamespace(const TranslationUnitDecl *TU,
5717 NamespaceDecl *AnonNamespace) {
5718 // If the translation unit has an anonymous namespace, and we don't already
5719 // have an update block for it, write it as an update block.
5720 // FIXME: Why do we not do this if there's already an update block?
5721 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
5722 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
5723 if (Record.empty())
5724 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
5728 void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
5729 // Keep writing types, declarations, and declaration update records
5730 // until we've emitted all of them.
5731 RecordData DeclUpdatesOffsetsRecord;
5732 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5733 DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5734 WriteTypeAbbrevs();
5735 WriteDeclAbbrevs();
5736 do {
5737 WriteDeclUpdatesBlocks(Context, DeclUpdatesOffsetsRecord);
5738 while (!DeclTypesToEmit.empty()) {
5739 DeclOrType DOT = DeclTypesToEmit.front();
5740 DeclTypesToEmit.pop();
5741 if (DOT.isType())
5742 WriteType(Context, DOT.getType());
5743 else
5744 WriteDecl(Context, DOT.getDecl());
5746 } while (!DeclUpdates.empty());
5748 DoneWritingDeclsAndTypes = true;
5750 // DelayedNamespace is only meaningful in reduced BMI.
5751 // See the comments of DelayedNamespace for details.
5752 assert(DelayedNamespace.empty() || GeneratingReducedBMI);
5753 RecordData DelayedNamespaceRecord;
5754 for (NamespaceDecl *NS : DelayedNamespace) {
5755 uint64_t LexicalOffset = WriteDeclContextLexicalBlock(Context, NS);
5756 uint64_t VisibleOffset = WriteDeclContextVisibleBlock(Context, NS);
5758 // Write the offset relative to current block.
5759 if (LexicalOffset)
5760 LexicalOffset -= DeclTypesBlockStartOffset;
5762 if (VisibleOffset)
5763 VisibleOffset -= DeclTypesBlockStartOffset;
5765 AddDeclRef(NS, DelayedNamespaceRecord);
5766 DelayedNamespaceRecord.push_back(LexicalOffset);
5767 DelayedNamespaceRecord.push_back(VisibleOffset);
5770 // The process of writing lexical and visible block for delayed namespace
5771 // shouldn't introduce any new decls, types or update to emit.
5772 assert(DeclTypesToEmit.empty());
5773 assert(DeclUpdates.empty());
5775 Stream.ExitBlock();
5777 // These things can only be done once we've written out decls and types.
5778 WriteTypeDeclOffsets();
5779 if (!DeclUpdatesOffsetsRecord.empty())
5780 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
5782 if (!DelayedNamespaceRecord.empty())
5783 Stream.EmitRecord(DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD,
5784 DelayedNamespaceRecord);
5786 if (!FunctionToLambdasMap.empty()) {
5787 // TODO: on disk hash table for function to lambda mapping might be more
5788 // efficent becuase it allows lazy deserialization.
5789 RecordData FunctionToLambdasMapRecord;
5790 for (const auto &Pair : FunctionToLambdasMap) {
5791 FunctionToLambdasMapRecord.push_back(Pair.first.getRawValue());
5792 FunctionToLambdasMapRecord.push_back(Pair.second.size());
5793 for (const auto &Lambda : Pair.second)
5794 FunctionToLambdasMapRecord.push_back(Lambda.getRawValue());
5797 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
5798 Abv->Add(llvm::BitCodeAbbrevOp(FUNCTION_DECL_TO_LAMBDAS_MAP));
5799 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Array));
5800 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5801 unsigned FunctionToLambdaMapAbbrev = Stream.EmitAbbrev(std::move(Abv));
5802 Stream.EmitRecord(FUNCTION_DECL_TO_LAMBDAS_MAP, FunctionToLambdasMapRecord,
5803 FunctionToLambdaMapAbbrev);
5806 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5807 // Create a lexical update block containing all of the declarations in the
5808 // translation unit that do not come from other AST files.
5809 SmallVector<DeclID, 128> NewGlobalKindDeclPairs;
5810 for (const auto *D : TU->noload_decls()) {
5811 if (D->isFromASTFile())
5812 continue;
5814 // In reduced BMI, skip unreached declarations.
5815 if (!wasDeclEmitted(D))
5816 continue;
5818 NewGlobalKindDeclPairs.push_back(D->getKind());
5819 NewGlobalKindDeclPairs.push_back(GetDeclRef(D).getRawValue());
5822 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
5823 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
5824 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5825 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
5827 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
5828 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
5829 bytes(NewGlobalKindDeclPairs));
5831 Abv = std::make_shared<llvm::BitCodeAbbrev>();
5832 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
5833 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5834 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5835 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
5837 // And a visible updates block for the translation unit.
5838 WriteDeclContextVisibleUpdate(Context, TU);
5840 // If we have any extern "C" names, write out a visible update for them.
5841 if (Context.ExternCContext)
5842 WriteDeclContextVisibleUpdate(Context, Context.ExternCContext);
5844 // Write the visible updates to DeclContexts.
5845 for (auto *DC : UpdatedDeclContexts)
5846 WriteDeclContextVisibleUpdate(Context, DC);
5849 void ASTWriter::WriteDeclUpdatesBlocks(ASTContext &Context,
5850 RecordDataImpl &OffsetsRecord) {
5851 if (DeclUpdates.empty())
5852 return;
5854 DeclUpdateMap LocalUpdates;
5855 LocalUpdates.swap(DeclUpdates);
5857 for (auto &DeclUpdate : LocalUpdates) {
5858 const Decl *D = DeclUpdate.first;
5860 bool HasUpdatedBody = false;
5861 bool HasAddedVarDefinition = false;
5862 RecordData RecordData;
5863 ASTRecordWriter Record(Context, *this, RecordData);
5864 for (auto &Update : DeclUpdate.second) {
5865 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5867 // An updated body is emitted last, so that the reader doesn't need
5868 // to skip over the lazy body to reach statements for other records.
5869 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5870 HasUpdatedBody = true;
5871 else if (Kind == UPD_CXX_ADDED_VAR_DEFINITION)
5872 HasAddedVarDefinition = true;
5873 else
5874 Record.push_back(Kind);
5876 switch (Kind) {
5877 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5878 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5879 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
5880 assert(Update.getDecl() && "no decl to add?");
5881 Record.AddDeclRef(Update.getDecl());
5882 break;
5884 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
5885 case UPD_CXX_ADDED_VAR_DEFINITION:
5886 break;
5888 case UPD_CXX_POINT_OF_INSTANTIATION:
5889 // FIXME: Do we need to also save the template specialization kind here?
5890 Record.AddSourceLocation(Update.getLoc());
5891 break;
5893 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
5894 Record.writeStmtRef(
5895 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg());
5896 break;
5898 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5899 Record.AddStmt(
5900 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5901 break;
5903 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5904 auto *RD = cast<CXXRecordDecl>(D);
5905 UpdatedDeclContexts.insert(RD->getPrimaryContext());
5906 Record.push_back(RD->isParamDestroyedInCallee());
5907 Record.push_back(llvm::to_underlying(RD->getArgPassingRestrictions()));
5908 Record.AddCXXDefinitionData(RD);
5909 Record.AddOffset(WriteDeclContextLexicalBlock(Context, RD));
5911 // This state is sometimes updated by template instantiation, when we
5912 // switch from the specialization referring to the template declaration
5913 // to it referring to the template definition.
5914 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5915 Record.push_back(MSInfo->getTemplateSpecializationKind());
5916 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
5917 } else {
5918 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5919 Record.push_back(Spec->getTemplateSpecializationKind());
5920 Record.AddSourceLocation(Spec->getPointOfInstantiation());
5922 // The instantiation might have been resolved to a partial
5923 // specialization. If so, record which one.
5924 auto From = Spec->getInstantiatedFrom();
5925 if (auto PartialSpec =
5926 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5927 Record.push_back(true);
5928 Record.AddDeclRef(PartialSpec);
5929 Record.AddTemplateArgumentList(
5930 &Spec->getTemplateInstantiationArgs());
5931 } else {
5932 Record.push_back(false);
5935 Record.push_back(llvm::to_underlying(RD->getTagKind()));
5936 Record.AddSourceLocation(RD->getLocation());
5937 Record.AddSourceLocation(RD->getBeginLoc());
5938 Record.AddSourceRange(RD->getBraceRange());
5940 // Instantiation may change attributes; write them all out afresh.
5941 Record.push_back(D->hasAttrs());
5942 if (D->hasAttrs())
5943 Record.AddAttributes(D->getAttrs());
5945 // FIXME: Ensure we don't get here for explicit instantiations.
5946 break;
5949 case UPD_CXX_RESOLVED_DTOR_DELETE:
5950 Record.AddDeclRef(Update.getDecl());
5951 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
5952 break;
5954 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
5955 auto prototype =
5956 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
5957 Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
5958 break;
5961 case UPD_CXX_DEDUCED_RETURN_TYPE:
5962 Record.push_back(GetOrCreateTypeID(Context, Update.getType()));
5963 break;
5965 case UPD_DECL_MARKED_USED:
5966 break;
5968 case UPD_MANGLING_NUMBER:
5969 case UPD_STATIC_LOCAL_NUMBER:
5970 Record.push_back(Update.getNumber());
5971 break;
5973 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
5974 Record.AddSourceRange(
5975 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
5976 break;
5978 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5979 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5980 Record.push_back(A->getAllocatorType());
5981 Record.AddStmt(A->getAllocator());
5982 Record.AddStmt(A->getAlignment());
5983 Record.AddSourceRange(A->getRange());
5984 break;
5987 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5988 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
5989 Record.AddSourceRange(
5990 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5991 break;
5993 case UPD_DECL_EXPORTED:
5994 Record.push_back(getSubmoduleID(Update.getModule()));
5995 break;
5997 case UPD_ADDED_ATTR_TO_RECORD:
5998 Record.AddAttributes(llvm::ArrayRef(Update.getAttr()));
5999 break;
6003 // Add a trailing update record, if any. These must go last because we
6004 // lazily load their attached statement.
6005 if (!GeneratingReducedBMI || !CanElideDeclDef(D)) {
6006 if (HasUpdatedBody) {
6007 const auto *Def = cast<FunctionDecl>(D);
6008 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
6009 Record.push_back(Def->isInlined());
6010 Record.AddSourceLocation(Def->getInnerLocStart());
6011 Record.AddFunctionDefinition(Def);
6012 } else if (HasAddedVarDefinition) {
6013 const auto *VD = cast<VarDecl>(D);
6014 Record.push_back(UPD_CXX_ADDED_VAR_DEFINITION);
6015 Record.push_back(VD->isInline());
6016 Record.push_back(VD->isInlineSpecified());
6017 Record.AddVarDeclInit(VD);
6021 AddDeclRef(D, OffsetsRecord);
6022 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
6026 void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
6027 RecordDataImpl &Record) {
6028 uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
6029 Record.push_back(Raw);
6032 FileID ASTWriter::getAdjustedFileID(FileID FID) const {
6033 if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
6034 NonAffectingFileIDs.empty())
6035 return FID;
6036 auto It = llvm::lower_bound(NonAffectingFileIDs, FID);
6037 unsigned Idx = std::distance(NonAffectingFileIDs.begin(), It);
6038 unsigned Offset = NonAffectingFileIDAdjustments[Idx];
6039 return FileID::get(FID.getOpaqueValue() - Offset);
6042 unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
6043 unsigned NumCreatedFIDs = PP->getSourceManager()
6044 .getLocalSLocEntry(FID.ID)
6045 .getFile()
6046 .NumCreatedFIDs;
6048 unsigned AdjustedNumCreatedFIDs = 0;
6049 for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
6050 if (IsSLocAffecting[I])
6051 ++AdjustedNumCreatedFIDs;
6052 return AdjustedNumCreatedFIDs;
6055 SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
6056 if (Loc.isInvalid())
6057 return Loc;
6058 return Loc.getLocWithOffset(-getAdjustment(Loc.getOffset()));
6061 SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
6062 return SourceRange(getAdjustedLocation(Range.getBegin()),
6063 getAdjustedLocation(Range.getEnd()));
6066 SourceLocation::UIntTy
6067 ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
6068 return Offset - getAdjustment(Offset);
6071 SourceLocation::UIntTy
6072 ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
6073 if (NonAffectingRanges.empty())
6074 return 0;
6076 if (PP->getSourceManager().isLoadedOffset(Offset))
6077 return 0;
6079 if (Offset > NonAffectingRanges.back().getEnd().getOffset())
6080 return NonAffectingOffsetAdjustments.back();
6082 if (Offset < NonAffectingRanges.front().getBegin().getOffset())
6083 return 0;
6085 auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
6086 return Range.getEnd().getOffset() < Offset;
6089 auto It = llvm::lower_bound(NonAffectingRanges, Offset, Contains);
6090 unsigned Idx = std::distance(NonAffectingRanges.begin(), It);
6091 return NonAffectingOffsetAdjustments[Idx];
6094 void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
6095 Record.push_back(getAdjustedFileID(FID).getOpaqueValue());
6098 SourceLocationEncoding::RawLocEncoding
6099 ASTWriter::getRawSourceLocationEncoding(SourceLocation Loc, LocSeq *Seq) {
6100 unsigned BaseOffset = 0;
6101 unsigned ModuleFileIndex = 0;
6103 // See SourceLocationEncoding.h for the encoding details.
6104 if (PP->getSourceManager().isLoadedSourceLocation(Loc) && Loc.isValid()) {
6105 assert(getChain());
6106 auto SLocMapI = getChain()->GlobalSLocOffsetMap.find(
6107 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6108 assert(SLocMapI != getChain()->GlobalSLocOffsetMap.end() &&
6109 "Corrupted global sloc offset map");
6110 ModuleFile *F = SLocMapI->second;
6111 BaseOffset = F->SLocEntryBaseOffset - 2;
6112 // 0 means the location is not loaded. So we need to add 1 to the index to
6113 // make it clear.
6114 ModuleFileIndex = F->Index + 1;
6115 assert(&getChain()->getModuleManager()[F->Index] == F);
6118 return SourceLocationEncoding::encode(Loc, BaseOffset, ModuleFileIndex, Seq);
6121 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
6122 SourceLocationSequence *Seq) {
6123 Loc = getAdjustedLocation(Loc);
6124 Record.push_back(getRawSourceLocationEncoding(Loc, Seq));
6127 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record,
6128 SourceLocationSequence *Seq) {
6129 AddSourceLocation(Range.getBegin(), Record, Seq);
6130 AddSourceLocation(Range.getEnd(), Record, Seq);
6133 void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
6134 AddAPInt(Value.bitcastToAPInt());
6137 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
6138 Record.push_back(getIdentifierRef(II));
6141 IdentifierID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
6142 if (!II)
6143 return 0;
6145 IdentifierID &ID = IdentifierIDs[II];
6146 if (ID == 0)
6147 ID = NextIdentID++;
6148 return ID;
6151 MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
6152 // Don't emit builtin macros like __LINE__ to the AST file unless they
6153 // have been redefined by the header (in which case they are not
6154 // isBuiltinMacro).
6155 if (!MI || MI->isBuiltinMacro())
6156 return 0;
6158 MacroID &ID = MacroIDs[MI];
6159 if (ID == 0) {
6160 ID = NextMacroID++;
6161 MacroInfoToEmitData Info = { Name, MI, ID };
6162 MacroInfosToEmit.push_back(Info);
6164 return ID;
6167 MacroID ASTWriter::getMacroID(MacroInfo *MI) {
6168 if (!MI || MI->isBuiltinMacro())
6169 return 0;
6171 assert(MacroIDs.contains(MI) && "Macro not emitted!");
6172 return MacroIDs[MI];
6175 uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
6176 return IdentMacroDirectivesOffsetMap.lookup(Name);
6179 void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
6180 Record->push_back(Writer->getSelectorRef(SelRef));
6183 SelectorID ASTWriter::getSelectorRef(Selector Sel) {
6184 if (Sel.getAsOpaquePtr() == nullptr) {
6185 return 0;
6188 SelectorID SID = SelectorIDs[Sel];
6189 if (SID == 0 && Chain) {
6190 // This might trigger a ReadSelector callback, which will set the ID for
6191 // this selector.
6192 Chain->LoadSelector(Sel);
6193 SID = SelectorIDs[Sel];
6195 if (SID == 0) {
6196 SID = NextSelectorID++;
6197 SelectorIDs[Sel] = SID;
6199 return SID;
6202 void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
6203 AddDeclRef(Temp->getDestructor());
6206 void ASTRecordWriter::AddTemplateArgumentLocInfo(
6207 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
6208 switch (Kind) {
6209 case TemplateArgument::Expression:
6210 AddStmt(Arg.getAsExpr());
6211 break;
6212 case TemplateArgument::Type:
6213 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
6214 break;
6215 case TemplateArgument::Template:
6216 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
6217 AddSourceLocation(Arg.getTemplateNameLoc());
6218 break;
6219 case TemplateArgument::TemplateExpansion:
6220 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
6221 AddSourceLocation(Arg.getTemplateNameLoc());
6222 AddSourceLocation(Arg.getTemplateEllipsisLoc());
6223 break;
6224 case TemplateArgument::Null:
6225 case TemplateArgument::Integral:
6226 case TemplateArgument::Declaration:
6227 case TemplateArgument::NullPtr:
6228 case TemplateArgument::StructuralValue:
6229 case TemplateArgument::Pack:
6230 // FIXME: Is this right?
6231 break;
6235 void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
6236 AddTemplateArgument(Arg.getArgument());
6238 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
6239 bool InfoHasSameExpr
6240 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
6241 Record->push_back(InfoHasSameExpr);
6242 if (InfoHasSameExpr)
6243 return; // Avoid storing the same expr twice.
6245 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
6248 void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
6249 if (!TInfo) {
6250 AddTypeRef(QualType());
6251 return;
6254 AddTypeRef(TInfo->getType());
6255 AddTypeLoc(TInfo->getTypeLoc());
6258 void ASTRecordWriter::AddTypeLoc(TypeLoc TL, LocSeq *OuterSeq) {
6259 LocSeq::State Seq(OuterSeq);
6260 TypeLocWriter TLW(*this, Seq);
6261 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6262 TLW.Visit(TL);
6265 void ASTWriter::AddTypeRef(ASTContext &Context, QualType T,
6266 RecordDataImpl &Record) {
6267 Record.push_back(GetOrCreateTypeID(Context, T));
6270 template <typename IdxForTypeTy>
6271 static TypeID MakeTypeID(ASTContext &Context, QualType T,
6272 IdxForTypeTy IdxForType) {
6273 if (T.isNull())
6274 return PREDEF_TYPE_NULL_ID;
6276 unsigned FastQuals = T.getLocalFastQualifiers();
6277 T.removeLocalFastQualifiers();
6279 if (T.hasLocalNonFastQualifiers())
6280 return IdxForType(T).asTypeID(FastQuals);
6282 assert(!T.hasLocalQualifiers());
6284 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr()))
6285 return TypeIdxFromBuiltin(BT).asTypeID(FastQuals);
6287 if (T == Context.AutoDeductTy)
6288 return TypeIdx(0, PREDEF_TYPE_AUTO_DEDUCT).asTypeID(FastQuals);
6289 if (T == Context.AutoRRefDeductTy)
6290 return TypeIdx(0, PREDEF_TYPE_AUTO_RREF_DEDUCT).asTypeID(FastQuals);
6292 return IdxForType(T).asTypeID(FastQuals);
6295 TypeID ASTWriter::GetOrCreateTypeID(ASTContext &Context, QualType T) {
6296 return MakeTypeID(Context, T, [&](QualType T) -> TypeIdx {
6297 if (T.isNull())
6298 return TypeIdx();
6299 assert(!T.getLocalFastQualifiers());
6301 TypeIdx &Idx = TypeIdxs[T];
6302 if (Idx.getValue() == 0) {
6303 if (DoneWritingDeclsAndTypes) {
6304 assert(0 && "New type seen after serializing all the types to emit!");
6305 return TypeIdx();
6308 // We haven't seen this type before. Assign it a new ID and put it
6309 // into the queue of types to emit.
6310 Idx = TypeIdx(0, NextTypeID++);
6311 DeclTypesToEmit.push(T);
6313 return Idx;
6317 void ASTWriter::AddEmittedDeclRef(const Decl *D, RecordDataImpl &Record) {
6318 if (!wasDeclEmitted(D))
6319 return;
6321 AddDeclRef(D, Record);
6324 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
6325 Record.push_back(GetDeclRef(D).getRawValue());
6328 LocalDeclID ASTWriter::GetDeclRef(const Decl *D) {
6329 assert(WritingAST && "Cannot request a declaration ID before AST writing");
6331 if (!D) {
6332 return LocalDeclID();
6335 // If the DeclUpdate from the GMF gets touched, emit it.
6336 if (auto *Iter = DeclUpdatesFromGMF.find(D);
6337 Iter != DeclUpdatesFromGMF.end()) {
6338 for (DeclUpdate &Update : Iter->second)
6339 DeclUpdates[D].push_back(Update);
6340 DeclUpdatesFromGMF.erase(Iter);
6343 // If D comes from an AST file, its declaration ID is already known and
6344 // fixed.
6345 if (D->isFromASTFile()) {
6346 if (isWritingStdCXXNamedModules() && D->getOwningModule())
6347 TouchedTopLevelModules.insert(D->getOwningModule()->getTopLevelModule());
6349 return LocalDeclID(D->getGlobalID());
6352 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
6353 LocalDeclID &ID = DeclIDs[D];
6354 if (ID.isInvalid()) {
6355 if (DoneWritingDeclsAndTypes) {
6356 assert(0 && "New decl seen after serializing all the decls to emit!");
6357 return LocalDeclID();
6360 // We haven't seen this declaration before. Give it a new ID and
6361 // enqueue it in the list of declarations to emit.
6362 ID = NextDeclID++;
6363 DeclTypesToEmit.push(const_cast<Decl *>(D));
6366 return ID;
6369 LocalDeclID ASTWriter::getDeclID(const Decl *D) {
6370 if (!D)
6371 return LocalDeclID();
6373 // If D comes from an AST file, its declaration ID is already known and
6374 // fixed.
6375 if (D->isFromASTFile())
6376 return LocalDeclID(D->getGlobalID());
6378 assert(DeclIDs.contains(D) && "Declaration not emitted!");
6379 return DeclIDs[D];
6382 bool ASTWriter::wasDeclEmitted(const Decl *D) const {
6383 assert(D);
6385 assert(DoneWritingDeclsAndTypes &&
6386 "wasDeclEmitted should only be called after writing declarations");
6388 if (D->isFromASTFile())
6389 return true;
6391 bool Emitted = DeclIDs.contains(D);
6392 assert((Emitted || (!D->getOwningModule() && isWritingStdCXXNamedModules()) ||
6393 GeneratingReducedBMI) &&
6394 "The declaration within modules can only be omitted in reduced BMI.");
6395 return Emitted;
6398 void ASTWriter::associateDeclWithFile(const Decl *D, LocalDeclID ID) {
6399 assert(ID.isValid());
6400 assert(D);
6402 SourceLocation Loc = D->getLocation();
6403 if (Loc.isInvalid())
6404 return;
6406 // We only keep track of the file-level declarations of each file.
6407 if (!D->getLexicalDeclContext()->isFileContext())
6408 return;
6409 // FIXME: ParmVarDecls that are part of a function type of a parameter of
6410 // a function/objc method, should not have TU as lexical context.
6411 // TemplateTemplateParmDecls that are part of an alias template, should not
6412 // have TU as lexical context.
6413 if (isa<ParmVarDecl, TemplateTemplateParmDecl>(D))
6414 return;
6416 SourceManager &SM = PP->getSourceManager();
6417 SourceLocation FileLoc = SM.getFileLoc(Loc);
6418 assert(SM.isLocalSourceLocation(FileLoc));
6419 FileID FID;
6420 unsigned Offset;
6421 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
6422 if (FID.isInvalid())
6423 return;
6424 assert(SM.getSLocEntry(FID).isFile());
6425 assert(IsSLocAffecting[FID.ID]);
6427 std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
6428 if (!Info)
6429 Info = std::make_unique<DeclIDInFileInfo>();
6431 std::pair<unsigned, LocalDeclID> LocDecl(Offset, ID);
6432 LocDeclIDsTy &Decls = Info->DeclIDs;
6433 Decls.push_back(LocDecl);
6436 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
6437 assert(needsAnonymousDeclarationNumber(D) &&
6438 "expected an anonymous declaration");
6440 // Number the anonymous declarations within this context, if we've not
6441 // already done so.
6442 auto It = AnonymousDeclarationNumbers.find(D);
6443 if (It == AnonymousDeclarationNumbers.end()) {
6444 auto *DC = D->getLexicalDeclContext();
6445 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
6446 AnonymousDeclarationNumbers[ND] = Number;
6449 It = AnonymousDeclarationNumbers.find(D);
6450 assert(It != AnonymousDeclarationNumbers.end() &&
6451 "declaration not found within its lexical context");
6454 return It->second;
6457 void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
6458 DeclarationName Name) {
6459 switch (Name.getNameKind()) {
6460 case DeclarationName::CXXConstructorName:
6461 case DeclarationName::CXXDestructorName:
6462 case DeclarationName::CXXConversionFunctionName:
6463 AddTypeSourceInfo(DNLoc.getNamedTypeInfo());
6464 break;
6466 case DeclarationName::CXXOperatorName:
6467 AddSourceRange(DNLoc.getCXXOperatorNameRange());
6468 break;
6470 case DeclarationName::CXXLiteralOperatorName:
6471 AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc());
6472 break;
6474 case DeclarationName::Identifier:
6475 case DeclarationName::ObjCZeroArgSelector:
6476 case DeclarationName::ObjCOneArgSelector:
6477 case DeclarationName::ObjCMultiArgSelector:
6478 case DeclarationName::CXXUsingDirective:
6479 case DeclarationName::CXXDeductionGuideName:
6480 break;
6484 void ASTRecordWriter::AddDeclarationNameInfo(
6485 const DeclarationNameInfo &NameInfo) {
6486 AddDeclarationName(NameInfo.getName());
6487 AddSourceLocation(NameInfo.getLoc());
6488 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
6491 void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
6492 AddNestedNameSpecifierLoc(Info.QualifierLoc);
6493 Record->push_back(Info.NumTemplParamLists);
6494 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
6495 AddTemplateParameterList(Info.TemplParamLists[i]);
6498 void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
6499 // Nested name specifiers usually aren't too long. I think that 8 would
6500 // typically accommodate the vast majority.
6501 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6503 // Push each of the nested-name-specifiers's onto a stack for
6504 // serialization in reverse order.
6505 while (NNS) {
6506 NestedNames.push_back(NNS);
6507 NNS = NNS.getPrefix();
6510 Record->push_back(NestedNames.size());
6511 while(!NestedNames.empty()) {
6512 NNS = NestedNames.pop_back_val();
6513 NestedNameSpecifier::SpecifierKind Kind
6514 = NNS.getNestedNameSpecifier()->getKind();
6515 Record->push_back(Kind);
6516 switch (Kind) {
6517 case NestedNameSpecifier::Identifier:
6518 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
6519 AddSourceRange(NNS.getLocalSourceRange());
6520 break;
6522 case NestedNameSpecifier::Namespace:
6523 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
6524 AddSourceRange(NNS.getLocalSourceRange());
6525 break;
6527 case NestedNameSpecifier::NamespaceAlias:
6528 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
6529 AddSourceRange(NNS.getLocalSourceRange());
6530 break;
6532 case NestedNameSpecifier::TypeSpec:
6533 case NestedNameSpecifier::TypeSpecWithTemplate:
6534 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
6535 AddTypeRef(NNS.getTypeLoc().getType());
6536 AddTypeLoc(NNS.getTypeLoc());
6537 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
6538 break;
6540 case NestedNameSpecifier::Global:
6541 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
6542 break;
6544 case NestedNameSpecifier::Super:
6545 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
6546 AddSourceRange(NNS.getLocalSourceRange());
6547 break;
6552 void ASTRecordWriter::AddTemplateParameterList(
6553 const TemplateParameterList *TemplateParams) {
6554 assert(TemplateParams && "No TemplateParams!");
6555 AddSourceLocation(TemplateParams->getTemplateLoc());
6556 AddSourceLocation(TemplateParams->getLAngleLoc());
6557 AddSourceLocation(TemplateParams->getRAngleLoc());
6559 Record->push_back(TemplateParams->size());
6560 for (const auto &P : *TemplateParams)
6561 AddDeclRef(P);
6562 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
6563 Record->push_back(true);
6564 writeStmtRef(RequiresClause);
6565 } else {
6566 Record->push_back(false);
6570 /// Emit a template argument list.
6571 void ASTRecordWriter::AddTemplateArgumentList(
6572 const TemplateArgumentList *TemplateArgs) {
6573 assert(TemplateArgs && "No TemplateArgs!");
6574 Record->push_back(TemplateArgs->size());
6575 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
6576 AddTemplateArgument(TemplateArgs->get(i));
6579 void ASTRecordWriter::AddASTTemplateArgumentListInfo(
6580 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
6581 assert(ASTTemplArgList && "No ASTTemplArgList!");
6582 AddSourceLocation(ASTTemplArgList->LAngleLoc);
6583 AddSourceLocation(ASTTemplArgList->RAngleLoc);
6584 Record->push_back(ASTTemplArgList->NumTemplateArgs);
6585 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
6586 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
6587 AddTemplateArgumentLoc(TemplArgs[i]);
6590 void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
6591 Record->push_back(Set.size());
6592 for (ASTUnresolvedSet::const_iterator
6593 I = Set.begin(), E = Set.end(); I != E; ++I) {
6594 AddDeclRef(I.getDecl());
6595 Record->push_back(I.getAccess());
6599 // FIXME: Move this out of the main ASTRecordWriter interface.
6600 void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
6601 Record->push_back(Base.isVirtual());
6602 Record->push_back(Base.isBaseOfClass());
6603 Record->push_back(Base.getAccessSpecifierAsWritten());
6604 Record->push_back(Base.getInheritConstructors());
6605 AddTypeSourceInfo(Base.getTypeSourceInfo());
6606 AddSourceRange(Base.getSourceRange());
6607 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
6608 : SourceLocation());
6611 static uint64_t EmitCXXBaseSpecifiers(ASTContext &Context, ASTWriter &W,
6612 ArrayRef<CXXBaseSpecifier> Bases) {
6613 ASTWriter::RecordData Record;
6614 ASTRecordWriter Writer(Context, W, Record);
6615 Writer.push_back(Bases.size());
6617 for (auto &Base : Bases)
6618 Writer.AddCXXBaseSpecifier(Base);
6620 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
6623 // FIXME: Move this out of the main ASTRecordWriter interface.
6624 void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
6625 AddOffset(EmitCXXBaseSpecifiers(getASTContext(), *Writer, Bases));
6628 static uint64_t
6629 EmitCXXCtorInitializers(ASTContext &Context, ASTWriter &W,
6630 ArrayRef<CXXCtorInitializer *> CtorInits) {
6631 ASTWriter::RecordData Record;
6632 ASTRecordWriter Writer(Context, W, Record);
6633 Writer.push_back(CtorInits.size());
6635 for (auto *Init : CtorInits) {
6636 if (Init->isBaseInitializer()) {
6637 Writer.push_back(CTOR_INITIALIZER_BASE);
6638 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
6639 Writer.push_back(Init->isBaseVirtual());
6640 } else if (Init->isDelegatingInitializer()) {
6641 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
6642 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
6643 } else if (Init->isMemberInitializer()){
6644 Writer.push_back(CTOR_INITIALIZER_MEMBER);
6645 Writer.AddDeclRef(Init->getMember());
6646 } else {
6647 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
6648 Writer.AddDeclRef(Init->getIndirectMember());
6651 Writer.AddSourceLocation(Init->getMemberLocation());
6652 Writer.AddStmt(Init->getInit());
6653 Writer.AddSourceLocation(Init->getLParenLoc());
6654 Writer.AddSourceLocation(Init->getRParenLoc());
6655 Writer.push_back(Init->isWritten());
6656 if (Init->isWritten())
6657 Writer.push_back(Init->getSourceOrder());
6660 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
6663 // FIXME: Move this out of the main ASTRecordWriter interface.
6664 void ASTRecordWriter::AddCXXCtorInitializers(
6665 ArrayRef<CXXCtorInitializer *> CtorInits) {
6666 AddOffset(EmitCXXCtorInitializers(getASTContext(), *Writer, CtorInits));
6669 void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
6670 auto &Data = D->data();
6672 Record->push_back(Data.IsLambda);
6674 BitsPacker DefinitionBits;
6676 #define FIELD(Name, Width, Merge) \
6677 if (!DefinitionBits.canWriteNextNBits(Width)) { \
6678 Record->push_back(DefinitionBits); \
6679 DefinitionBits.reset(0); \
6681 DefinitionBits.addBits(Data.Name, Width);
6683 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
6684 #undef FIELD
6686 Record->push_back(DefinitionBits);
6688 // getODRHash will compute the ODRHash if it has not been previously
6689 // computed.
6690 Record->push_back(D->getODRHash());
6692 bool ModulesCodegen =
6693 !D->isDependentType() &&
6694 (Writer->getLangOpts().ModulesDebugInfo || D->isInNamedModule());
6695 Record->push_back(ModulesCodegen);
6696 if (ModulesCodegen)
6697 Writer->AddDeclRef(D, Writer->ModularCodegenDecls);
6699 // IsLambda bit is already saved.
6701 AddUnresolvedSet(Data.Conversions.get(getASTContext()));
6702 Record->push_back(Data.ComputedVisibleConversions);
6703 if (Data.ComputedVisibleConversions)
6704 AddUnresolvedSet(Data.VisibleConversions.get(getASTContext()));
6705 // Data.Definition is the owning decl, no need to write it.
6707 if (!Data.IsLambda) {
6708 Record->push_back(Data.NumBases);
6709 if (Data.NumBases > 0)
6710 AddCXXBaseSpecifiers(Data.bases());
6712 // FIXME: Make VBases lazily computed when needed to avoid storing them.
6713 Record->push_back(Data.NumVBases);
6714 if (Data.NumVBases > 0)
6715 AddCXXBaseSpecifiers(Data.vbases());
6717 AddDeclRef(D->getFirstFriend());
6718 } else {
6719 auto &Lambda = D->getLambdaData();
6721 BitsPacker LambdaBits;
6722 LambdaBits.addBits(Lambda.DependencyKind, /*Width=*/2);
6723 LambdaBits.addBit(Lambda.IsGenericLambda);
6724 LambdaBits.addBits(Lambda.CaptureDefault, /*Width=*/2);
6725 LambdaBits.addBits(Lambda.NumCaptures, /*Width=*/15);
6726 LambdaBits.addBit(Lambda.HasKnownInternalLinkage);
6727 Record->push_back(LambdaBits);
6729 Record->push_back(Lambda.NumExplicitCaptures);
6730 Record->push_back(Lambda.ManglingNumber);
6731 Record->push_back(D->getDeviceLambdaManglingNumber());
6732 // The lambda context declaration and index within the context are provided
6733 // separately, so that they can be used for merging.
6734 AddTypeSourceInfo(Lambda.MethodTyInfo);
6735 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
6736 const LambdaCapture &Capture = Lambda.Captures.front()[I];
6737 AddSourceLocation(Capture.getLocation());
6739 BitsPacker CaptureBits;
6740 CaptureBits.addBit(Capture.isImplicit());
6741 CaptureBits.addBits(Capture.getCaptureKind(), /*Width=*/3);
6742 Record->push_back(CaptureBits);
6744 switch (Capture.getCaptureKind()) {
6745 case LCK_StarThis:
6746 case LCK_This:
6747 case LCK_VLAType:
6748 break;
6749 case LCK_ByCopy:
6750 case LCK_ByRef:
6751 ValueDecl *Var =
6752 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
6753 AddDeclRef(Var);
6754 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
6755 : SourceLocation());
6756 break;
6762 void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
6763 const Expr *Init = VD->getInit();
6764 if (!Init) {
6765 push_back(0);
6766 return;
6769 uint64_t Val = 1;
6770 if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
6771 Val |= (ES->HasConstantInitialization ? 2 : 0);
6772 Val |= (ES->HasConstantDestruction ? 4 : 0);
6773 APValue *Evaluated = VD->getEvaluatedValue();
6774 // If the evaluated result is constant, emit it.
6775 if (Evaluated && (Evaluated->isInt() || Evaluated->isFloat()))
6776 Val |= 8;
6778 push_back(Val);
6779 if (Val & 8) {
6780 AddAPValue(*VD->getEvaluatedValue());
6783 writeStmtRef(Init);
6786 void ASTWriter::ReaderInitialized(ASTReader *Reader) {
6787 assert(Reader && "Cannot remove chain");
6788 assert((!Chain || Chain == Reader) && "Cannot replace chain");
6789 assert(FirstDeclID == NextDeclID &&
6790 FirstTypeID == NextTypeID &&
6791 FirstIdentID == NextIdentID &&
6792 FirstMacroID == NextMacroID &&
6793 FirstSubmoduleID == NextSubmoduleID &&
6794 FirstSelectorID == NextSelectorID &&
6795 "Setting chain after writing has started.");
6797 Chain = Reader;
6799 // Note, this will get called multiple times, once one the reader starts up
6800 // and again each time it's done reading a PCH or module.
6801 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
6802 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
6803 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
6804 NextMacroID = FirstMacroID;
6805 NextSelectorID = FirstSelectorID;
6806 NextSubmoduleID = FirstSubmoduleID;
6809 void ASTWriter::IdentifierRead(IdentifierID ID, IdentifierInfo *II) {
6810 // Don't reuse Type ID from external modules for named modules. See the
6811 // comments in WriteASTCore for details.
6812 if (isWritingStdCXXNamedModules())
6813 return;
6815 IdentifierID &StoredID = IdentifierIDs[II];
6816 unsigned OriginalModuleFileIndex = StoredID >> 32;
6818 // Always keep the local identifier ID. See \p TypeRead() for more
6819 // information.
6820 if (OriginalModuleFileIndex == 0 && StoredID)
6821 return;
6823 // Otherwise, keep the highest ID since the module file comes later has
6824 // higher module file indexes.
6825 if (ID > StoredID)
6826 StoredID = ID;
6829 void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
6830 // Always keep the highest ID. See \p TypeRead() for more information.
6831 MacroID &StoredID = MacroIDs[MI];
6832 if (ID > StoredID)
6833 StoredID = ID;
6836 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
6837 // Don't reuse Type ID from external modules for named modules. See the
6838 // comments in WriteASTCore for details.
6839 if (isWritingStdCXXNamedModules())
6840 return;
6842 // Always take the type index that comes in later module files.
6843 // This copes with an interesting
6844 // case for chained AST writing where we schedule writing the type and then,
6845 // later, deserialize the type from another AST. In this case, we want to
6846 // keep the entry from a later module so that we can properly write it out to
6847 // the AST file.
6848 TypeIdx &StoredIdx = TypeIdxs[T];
6850 // Ignore it if the type comes from the current being written module file.
6851 // Since the current module file being written logically has the highest
6852 // index.
6853 unsigned ModuleFileIndex = StoredIdx.getModuleFileIndex();
6854 if (ModuleFileIndex == 0 && StoredIdx.getValue())
6855 return;
6857 // Otherwise, keep the highest ID since the module file comes later has
6858 // higher module file indexes.
6859 if (Idx.getModuleFileIndex() >= StoredIdx.getModuleFileIndex())
6860 StoredIdx = Idx;
6863 void ASTWriter::PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {
6864 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
6865 DeclIDs[D] = LocalDeclID(ID);
6866 PredefinedDecls.insert(D);
6869 void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
6870 // Always keep the highest ID. See \p TypeRead() for more information.
6871 SelectorID &StoredID = SelectorIDs[S];
6872 if (ID > StoredID)
6873 StoredID = ID;
6876 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
6877 MacroDefinitionRecord *MD) {
6878 assert(!MacroDefinitions.contains(MD));
6879 MacroDefinitions[MD] = ID;
6882 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6883 assert(!SubmoduleIDs.contains(Mod));
6884 SubmoduleIDs[Mod] = ID;
6887 void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
6888 if (Chain && Chain->isProcessingUpdateRecords()) return;
6889 assert(D->isCompleteDefinition());
6890 assert(!WritingAST && "Already writing the AST!");
6891 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
6892 // We are interested when a PCH decl is modified.
6893 if (RD->isFromASTFile()) {
6894 // A forward reference was mutated into a definition. Rewrite it.
6895 // FIXME: This happens during template instantiation, should we
6896 // have created a new definition decl instead ?
6897 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6898 "completed a tag from another module but not by instantiation?");
6899 DeclUpdates[RD].push_back(
6900 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
6905 static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6906 if (D->isFromASTFile())
6907 return true;
6909 // The predefined __va_list_tag struct is imported if we imported any decls.
6910 // FIXME: This is a gross hack.
6911 return D == D->getASTContext().getVaListTagDecl();
6914 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
6915 if (Chain && Chain->isProcessingUpdateRecords()) return;
6916 assert(DC->isLookupContext() &&
6917 "Should not add lookup results to non-lookup contexts!");
6919 // TU is handled elsewhere.
6920 if (isa<TranslationUnitDecl>(DC))
6921 return;
6923 // Namespaces are handled elsewhere, except for template instantiations of
6924 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6925 // local instantiations are added to an imported context. Only happens when
6926 // adding ADL lookup candidates, for example templated friends.
6927 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6928 !isa<FunctionTemplateDecl>(D))
6929 return;
6931 // We're only interested in cases where a local declaration is added to an
6932 // imported context.
6933 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6934 return;
6936 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
6937 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
6938 assert(!WritingAST && "Already writing the AST!");
6939 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6940 // We're adding a visible declaration to a predefined decl context. Ensure
6941 // that we write out all of its lookup results so we don't get a nasty
6942 // surprise when we try to emit its lookup table.
6943 llvm::append_range(DeclsToEmitEvenIfUnreferenced, DC->decls());
6945 DeclsToEmitEvenIfUnreferenced.push_back(D);
6948 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
6949 if (Chain && Chain->isProcessingUpdateRecords()) return;
6950 assert(D->isImplicit());
6952 // We're only interested in cases where a local declaration is added to an
6953 // imported context.
6954 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6955 return;
6957 if (!isa<CXXMethodDecl>(D))
6958 return;
6960 // A decl coming from PCH was modified.
6961 assert(RD->isCompleteDefinition());
6962 assert(!WritingAST && "Already writing the AST!");
6963 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
6966 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
6967 if (Chain && Chain->isProcessingUpdateRecords()) return;
6968 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6969 if (!Chain) return;
6970 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6971 // If we don't already know the exception specification for this redecl
6972 // chain, add an update record for it.
6973 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6974 ->getType()
6975 ->castAs<FunctionProtoType>()
6976 ->getExceptionSpecType()))
6977 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6981 void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
6982 if (Chain && Chain->isProcessingUpdateRecords()) return;
6983 assert(!WritingAST && "Already writing the AST!");
6984 if (!Chain) return;
6985 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6986 DeclUpdates[D].push_back(
6987 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6991 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6992 const FunctionDecl *Delete,
6993 Expr *ThisArg) {
6994 if (Chain && Chain->isProcessingUpdateRecords()) return;
6995 assert(!WritingAST && "Already writing the AST!");
6996 assert(Delete && "Not given an operator delete");
6997 if (!Chain) return;
6998 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
6999 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
7003 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
7004 if (Chain && Chain->isProcessingUpdateRecords()) return;
7005 assert(!WritingAST && "Already writing the AST!");
7006 if (!D->isFromASTFile())
7007 return; // Declaration not imported from PCH.
7009 // Implicit function decl from a PCH was defined.
7010 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
7013 void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
7014 if (Chain && Chain->isProcessingUpdateRecords()) return;
7015 assert(!WritingAST && "Already writing the AST!");
7016 if (!D->isFromASTFile())
7017 return;
7019 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
7022 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
7023 if (Chain && Chain->isProcessingUpdateRecords()) return;
7024 assert(!WritingAST && "Already writing the AST!");
7025 if (!D->isFromASTFile())
7026 return;
7028 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
7031 void ASTWriter::InstantiationRequested(const ValueDecl *D) {
7032 if (Chain && Chain->isProcessingUpdateRecords()) return;
7033 assert(!WritingAST && "Already writing the AST!");
7034 if (!D->isFromASTFile())
7035 return;
7037 // Since the actual instantiation is delayed, this really means that we need
7038 // to update the instantiation location.
7039 SourceLocation POI;
7040 if (auto *VD = dyn_cast<VarDecl>(D))
7041 POI = VD->getPointOfInstantiation();
7042 else
7043 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
7044 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
7047 void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
7048 if (Chain && Chain->isProcessingUpdateRecords()) return;
7049 assert(!WritingAST && "Already writing the AST!");
7050 if (!D->isFromASTFile())
7051 return;
7053 DeclUpdates[D].push_back(
7054 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
7057 void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
7058 assert(!WritingAST && "Already writing the AST!");
7059 if (!D->isFromASTFile())
7060 return;
7062 DeclUpdates[D].push_back(
7063 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
7066 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
7067 const ObjCInterfaceDecl *IFD) {
7068 if (Chain && Chain->isProcessingUpdateRecords()) return;
7069 assert(!WritingAST && "Already writing the AST!");
7070 if (!IFD->isFromASTFile())
7071 return; // Declaration not imported from PCH.
7073 assert(IFD->getDefinition() && "Category on a class without a definition?");
7074 ObjCClassesWithCategories.insert(
7075 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
7078 void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
7079 if (Chain && Chain->isProcessingUpdateRecords()) return;
7080 assert(!WritingAST && "Already writing the AST!");
7082 // If there is *any* declaration of the entity that's not from an AST file,
7083 // we can skip writing the update record. We make sure that isUsed() triggers
7084 // completion of the redeclaration chain of the entity.
7085 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
7086 if (IsLocalDecl(Prev))
7087 return;
7089 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
7092 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
7093 if (Chain && Chain->isProcessingUpdateRecords()) return;
7094 assert(!WritingAST && "Already writing the AST!");
7095 if (!D->isFromASTFile())
7096 return;
7098 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
7101 void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
7102 if (Chain && Chain->isProcessingUpdateRecords()) return;
7103 assert(!WritingAST && "Already writing the AST!");
7104 if (!D->isFromASTFile())
7105 return;
7107 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
7110 void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
7111 const Attr *Attr) {
7112 if (Chain && Chain->isProcessingUpdateRecords()) return;
7113 assert(!WritingAST && "Already writing the AST!");
7114 if (!D->isFromASTFile())
7115 return;
7117 DeclUpdates[D].push_back(
7118 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
7121 void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
7122 if (Chain && Chain->isProcessingUpdateRecords()) return;
7123 assert(!WritingAST && "Already writing the AST!");
7124 assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
7125 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
7128 void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
7129 const RecordDecl *Record) {
7130 if (Chain && Chain->isProcessingUpdateRecords()) return;
7131 assert(!WritingAST && "Already writing the AST!");
7132 if (!Record->isFromASTFile())
7133 return;
7134 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
7137 void ASTWriter::AddedCXXTemplateSpecialization(
7138 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
7139 assert(!WritingAST && "Already writing the AST!");
7141 if (!TD->getFirstDecl()->isFromASTFile())
7142 return;
7143 if (Chain && Chain->isProcessingUpdateRecords())
7144 return;
7146 DeclsToEmitEvenIfUnreferenced.push_back(D);
7149 void ASTWriter::AddedCXXTemplateSpecialization(
7150 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
7151 assert(!WritingAST && "Already writing the AST!");
7153 if (!TD->getFirstDecl()->isFromASTFile())
7154 return;
7155 if (Chain && Chain->isProcessingUpdateRecords())
7156 return;
7158 DeclsToEmitEvenIfUnreferenced.push_back(D);
7161 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
7162 const FunctionDecl *D) {
7163 assert(!WritingAST && "Already writing the AST!");
7165 if (!TD->getFirstDecl()->isFromASTFile())
7166 return;
7167 if (Chain && Chain->isProcessingUpdateRecords())
7168 return;
7170 DeclsToEmitEvenIfUnreferenced.push_back(D);
7173 //===----------------------------------------------------------------------===//
7174 //// OMPClause Serialization
7175 ////===----------------------------------------------------------------------===//
7177 namespace {
7179 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
7180 ASTRecordWriter &Record;
7182 public:
7183 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
7184 #define GEN_CLANG_CLAUSE_CLASS
7185 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
7186 #include "llvm/Frontend/OpenMP/OMP.inc"
7187 void writeClause(OMPClause *C);
7188 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
7189 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
7194 void ASTRecordWriter::writeOMPClause(OMPClause *C) {
7195 OMPClauseWriter(*this).writeClause(C);
7198 void OMPClauseWriter::writeClause(OMPClause *C) {
7199 Record.push_back(unsigned(C->getClauseKind()));
7200 Visit(C);
7201 Record.AddSourceLocation(C->getBeginLoc());
7202 Record.AddSourceLocation(C->getEndLoc());
7205 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
7206 Record.push_back(uint64_t(C->getCaptureRegion()));
7207 Record.AddStmt(C->getPreInitStmt());
7210 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
7211 VisitOMPClauseWithPreInit(C);
7212 Record.AddStmt(C->getPostUpdateExpr());
7215 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
7216 VisitOMPClauseWithPreInit(C);
7217 Record.push_back(uint64_t(C->getNameModifier()));
7218 Record.AddSourceLocation(C->getNameModifierLoc());
7219 Record.AddSourceLocation(C->getColonLoc());
7220 Record.AddStmt(C->getCondition());
7221 Record.AddSourceLocation(C->getLParenLoc());
7224 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
7225 VisitOMPClauseWithPreInit(C);
7226 Record.AddStmt(C->getCondition());
7227 Record.AddSourceLocation(C->getLParenLoc());
7230 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
7231 VisitOMPClauseWithPreInit(C);
7232 Record.AddStmt(C->getNumThreads());
7233 Record.AddSourceLocation(C->getLParenLoc());
7236 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
7237 Record.AddStmt(C->getSafelen());
7238 Record.AddSourceLocation(C->getLParenLoc());
7241 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
7242 Record.AddStmt(C->getSimdlen());
7243 Record.AddSourceLocation(C->getLParenLoc());
7246 void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause *C) {
7247 Record.push_back(C->getNumSizes());
7248 for (Expr *Size : C->getSizesRefs())
7249 Record.AddStmt(Size);
7250 Record.AddSourceLocation(C->getLParenLoc());
7253 void OMPClauseWriter::VisitOMPPermutationClause(OMPPermutationClause *C) {
7254 Record.push_back(C->getNumLoops());
7255 for (Expr *Size : C->getArgsRefs())
7256 Record.AddStmt(Size);
7257 Record.AddSourceLocation(C->getLParenLoc());
7260 void OMPClauseWriter::VisitOMPFullClause(OMPFullClause *C) {}
7262 void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause *C) {
7263 Record.AddStmt(C->getFactor());
7264 Record.AddSourceLocation(C->getLParenLoc());
7267 void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
7268 Record.AddStmt(C->getAllocator());
7269 Record.AddSourceLocation(C->getLParenLoc());
7272 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
7273 Record.AddStmt(C->getNumForLoops());
7274 Record.AddSourceLocation(C->getLParenLoc());
7277 void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
7278 Record.AddStmt(C->getEventHandler());
7279 Record.AddSourceLocation(C->getLParenLoc());
7282 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
7283 Record.push_back(unsigned(C->getDefaultKind()));
7284 Record.AddSourceLocation(C->getLParenLoc());
7285 Record.AddSourceLocation(C->getDefaultKindKwLoc());
7288 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
7289 Record.push_back(unsigned(C->getProcBindKind()));
7290 Record.AddSourceLocation(C->getLParenLoc());
7291 Record.AddSourceLocation(C->getProcBindKindKwLoc());
7294 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
7295 VisitOMPClauseWithPreInit(C);
7296 Record.push_back(C->getScheduleKind());
7297 Record.push_back(C->getFirstScheduleModifier());
7298 Record.push_back(C->getSecondScheduleModifier());
7299 Record.AddStmt(C->getChunkSize());
7300 Record.AddSourceLocation(C->getLParenLoc());
7301 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
7302 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
7303 Record.AddSourceLocation(C->getScheduleKindLoc());
7304 Record.AddSourceLocation(C->getCommaLoc());
7307 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
7308 Record.push_back(C->getLoopNumIterations().size());
7309 Record.AddStmt(C->getNumForLoops());
7310 for (Expr *NumIter : C->getLoopNumIterations())
7311 Record.AddStmt(NumIter);
7312 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
7313 Record.AddStmt(C->getLoopCounter(I));
7314 Record.AddSourceLocation(C->getLParenLoc());
7317 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
7319 void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
7321 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
7323 void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
7325 void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
7327 void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
7328 Record.push_back(C->isExtended() ? 1 : 0);
7329 if (C->isExtended()) {
7330 Record.AddSourceLocation(C->getLParenLoc());
7331 Record.AddSourceLocation(C->getArgumentLoc());
7332 Record.writeEnum(C->getDependencyKind());
7336 void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
7338 void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause *) {}
7340 // Save the parameter of fail clause.
7341 void OMPClauseWriter::VisitOMPFailClause(OMPFailClause *C) {
7342 Record.AddSourceLocation(C->getLParenLoc());
7343 Record.AddSourceLocation(C->getFailParameterLoc());
7344 Record.writeEnum(C->getFailParameter());
7347 void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
7349 void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
7351 void OMPClauseWriter::VisitOMPAbsentClause(OMPAbsentClause *C) {
7352 Record.push_back(static_cast<uint64_t>(C->getDirectiveKinds().size()));
7353 Record.AddSourceLocation(C->getLParenLoc());
7354 for (auto K : C->getDirectiveKinds()) {
7355 Record.writeEnum(K);
7359 void OMPClauseWriter::VisitOMPHoldsClause(OMPHoldsClause *C) {
7360 Record.AddStmt(C->getExpr());
7361 Record.AddSourceLocation(C->getLParenLoc());
7364 void OMPClauseWriter::VisitOMPContainsClause(OMPContainsClause *C) {
7365 Record.push_back(static_cast<uint64_t>(C->getDirectiveKinds().size()));
7366 Record.AddSourceLocation(C->getLParenLoc());
7367 for (auto K : C->getDirectiveKinds()) {
7368 Record.writeEnum(K);
7372 void OMPClauseWriter::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
7374 void OMPClauseWriter::VisitOMPNoOpenMPRoutinesClause(
7375 OMPNoOpenMPRoutinesClause *) {}
7377 void OMPClauseWriter::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
7379 void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
7381 void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
7383 void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
7385 void OMPClauseWriter::VisitOMPWeakClause(OMPWeakClause *) {}
7387 void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
7389 void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
7391 void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
7393 void OMPClauseWriter::VisitOMPInitClause(OMPInitClause *C) {
7394 Record.push_back(C->varlist_size());
7395 for (Expr *VE : C->varlist())
7396 Record.AddStmt(VE);
7397 Record.writeBool(C->getIsTarget());
7398 Record.writeBool(C->getIsTargetSync());
7399 Record.AddSourceLocation(C->getLParenLoc());
7400 Record.AddSourceLocation(C->getVarLoc());
7403 void OMPClauseWriter::VisitOMPUseClause(OMPUseClause *C) {
7404 Record.AddStmt(C->getInteropVar());
7405 Record.AddSourceLocation(C->getLParenLoc());
7406 Record.AddSourceLocation(C->getVarLoc());
7409 void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *C) {
7410 Record.AddStmt(C->getInteropVar());
7411 Record.AddSourceLocation(C->getLParenLoc());
7412 Record.AddSourceLocation(C->getVarLoc());
7415 void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
7416 VisitOMPClauseWithPreInit(C);
7417 Record.AddStmt(C->getCondition());
7418 Record.AddSourceLocation(C->getLParenLoc());
7421 void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause *C) {
7422 VisitOMPClauseWithPreInit(C);
7423 Record.AddStmt(C->getCondition());
7424 Record.AddSourceLocation(C->getLParenLoc());
7427 void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause *C) {
7428 VisitOMPClauseWithPreInit(C);
7429 Record.AddStmt(C->getThreadID());
7430 Record.AddSourceLocation(C->getLParenLoc());
7433 void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause *C) {
7434 Record.AddStmt(C->getAlignment());
7435 Record.AddSourceLocation(C->getLParenLoc());
7438 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
7439 Record.push_back(C->varlist_size());
7440 Record.AddSourceLocation(C->getLParenLoc());
7441 for (auto *VE : C->varlist()) {
7442 Record.AddStmt(VE);
7444 for (auto *VE : C->private_copies()) {
7445 Record.AddStmt(VE);
7449 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
7450 Record.push_back(C->varlist_size());
7451 VisitOMPClauseWithPreInit(C);
7452 Record.AddSourceLocation(C->getLParenLoc());
7453 for (auto *VE : C->varlist()) {
7454 Record.AddStmt(VE);
7456 for (auto *VE : C->private_copies()) {
7457 Record.AddStmt(VE);
7459 for (auto *VE : C->inits()) {
7460 Record.AddStmt(VE);
7464 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
7465 Record.push_back(C->varlist_size());
7466 VisitOMPClauseWithPostUpdate(C);
7467 Record.AddSourceLocation(C->getLParenLoc());
7468 Record.writeEnum(C->getKind());
7469 Record.AddSourceLocation(C->getKindLoc());
7470 Record.AddSourceLocation(C->getColonLoc());
7471 for (auto *VE : C->varlist())
7472 Record.AddStmt(VE);
7473 for (auto *E : C->private_copies())
7474 Record.AddStmt(E);
7475 for (auto *E : C->source_exprs())
7476 Record.AddStmt(E);
7477 for (auto *E : C->destination_exprs())
7478 Record.AddStmt(E);
7479 for (auto *E : C->assignment_ops())
7480 Record.AddStmt(E);
7483 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
7484 Record.push_back(C->varlist_size());
7485 Record.AddSourceLocation(C->getLParenLoc());
7486 for (auto *VE : C->varlist())
7487 Record.AddStmt(VE);
7490 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
7491 Record.push_back(C->varlist_size());
7492 Record.writeEnum(C->getModifier());
7493 VisitOMPClauseWithPostUpdate(C);
7494 Record.AddSourceLocation(C->getLParenLoc());
7495 Record.AddSourceLocation(C->getModifierLoc());
7496 Record.AddSourceLocation(C->getColonLoc());
7497 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
7498 Record.AddDeclarationNameInfo(C->getNameInfo());
7499 for (auto *VE : C->varlist())
7500 Record.AddStmt(VE);
7501 for (auto *VE : C->privates())
7502 Record.AddStmt(VE);
7503 for (auto *E : C->lhs_exprs())
7504 Record.AddStmt(E);
7505 for (auto *E : C->rhs_exprs())
7506 Record.AddStmt(E);
7507 for (auto *E : C->reduction_ops())
7508 Record.AddStmt(E);
7509 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
7510 for (auto *E : C->copy_ops())
7511 Record.AddStmt(E);
7512 for (auto *E : C->copy_array_temps())
7513 Record.AddStmt(E);
7514 for (auto *E : C->copy_array_elems())
7515 Record.AddStmt(E);
7519 void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
7520 Record.push_back(C->varlist_size());
7521 VisitOMPClauseWithPostUpdate(C);
7522 Record.AddSourceLocation(C->getLParenLoc());
7523 Record.AddSourceLocation(C->getColonLoc());
7524 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
7525 Record.AddDeclarationNameInfo(C->getNameInfo());
7526 for (auto *VE : C->varlist())
7527 Record.AddStmt(VE);
7528 for (auto *VE : C->privates())
7529 Record.AddStmt(VE);
7530 for (auto *E : C->lhs_exprs())
7531 Record.AddStmt(E);
7532 for (auto *E : C->rhs_exprs())
7533 Record.AddStmt(E);
7534 for (auto *E : C->reduction_ops())
7535 Record.AddStmt(E);
7538 void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
7539 Record.push_back(C->varlist_size());
7540 VisitOMPClauseWithPostUpdate(C);
7541 Record.AddSourceLocation(C->getLParenLoc());
7542 Record.AddSourceLocation(C->getColonLoc());
7543 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
7544 Record.AddDeclarationNameInfo(C->getNameInfo());
7545 for (auto *VE : C->varlist())
7546 Record.AddStmt(VE);
7547 for (auto *VE : C->privates())
7548 Record.AddStmt(VE);
7549 for (auto *E : C->lhs_exprs())
7550 Record.AddStmt(E);
7551 for (auto *E : C->rhs_exprs())
7552 Record.AddStmt(E);
7553 for (auto *E : C->reduction_ops())
7554 Record.AddStmt(E);
7555 for (auto *E : C->taskgroup_descriptors())
7556 Record.AddStmt(E);
7559 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
7560 Record.push_back(C->varlist_size());
7561 VisitOMPClauseWithPostUpdate(C);
7562 Record.AddSourceLocation(C->getLParenLoc());
7563 Record.AddSourceLocation(C->getColonLoc());
7564 Record.push_back(C->getModifier());
7565 Record.AddSourceLocation(C->getModifierLoc());
7566 for (auto *VE : C->varlist()) {
7567 Record.AddStmt(VE);
7569 for (auto *VE : C->privates()) {
7570 Record.AddStmt(VE);
7572 for (auto *VE : C->inits()) {
7573 Record.AddStmt(VE);
7575 for (auto *VE : C->updates()) {
7576 Record.AddStmt(VE);
7578 for (auto *VE : C->finals()) {
7579 Record.AddStmt(VE);
7581 Record.AddStmt(C->getStep());
7582 Record.AddStmt(C->getCalcStep());
7583 for (auto *VE : C->used_expressions())
7584 Record.AddStmt(VE);
7587 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
7588 Record.push_back(C->varlist_size());
7589 Record.AddSourceLocation(C->getLParenLoc());
7590 Record.AddSourceLocation(C->getColonLoc());
7591 for (auto *VE : C->varlist())
7592 Record.AddStmt(VE);
7593 Record.AddStmt(C->getAlignment());
7596 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
7597 Record.push_back(C->varlist_size());
7598 Record.AddSourceLocation(C->getLParenLoc());
7599 for (auto *VE : C->varlist())
7600 Record.AddStmt(VE);
7601 for (auto *E : C->source_exprs())
7602 Record.AddStmt(E);
7603 for (auto *E : C->destination_exprs())
7604 Record.AddStmt(E);
7605 for (auto *E : C->assignment_ops())
7606 Record.AddStmt(E);
7609 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
7610 Record.push_back(C->varlist_size());
7611 Record.AddSourceLocation(C->getLParenLoc());
7612 for (auto *VE : C->varlist())
7613 Record.AddStmt(VE);
7614 for (auto *E : C->source_exprs())
7615 Record.AddStmt(E);
7616 for (auto *E : C->destination_exprs())
7617 Record.AddStmt(E);
7618 for (auto *E : C->assignment_ops())
7619 Record.AddStmt(E);
7622 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
7623 Record.push_back(C->varlist_size());
7624 Record.AddSourceLocation(C->getLParenLoc());
7625 for (auto *VE : C->varlist())
7626 Record.AddStmt(VE);
7629 void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
7630 Record.AddStmt(C->getDepobj());
7631 Record.AddSourceLocation(C->getLParenLoc());
7634 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
7635 Record.push_back(C->varlist_size());
7636 Record.push_back(C->getNumLoops());
7637 Record.AddSourceLocation(C->getLParenLoc());
7638 Record.AddStmt(C->getModifier());
7639 Record.push_back(C->getDependencyKind());
7640 Record.AddSourceLocation(C->getDependencyLoc());
7641 Record.AddSourceLocation(C->getColonLoc());
7642 Record.AddSourceLocation(C->getOmpAllMemoryLoc());
7643 for (auto *VE : C->varlist())
7644 Record.AddStmt(VE);
7645 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
7646 Record.AddStmt(C->getLoopData(I));
7649 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
7650 VisitOMPClauseWithPreInit(C);
7651 Record.writeEnum(C->getModifier());
7652 Record.AddStmt(C->getDevice());
7653 Record.AddSourceLocation(C->getModifierLoc());
7654 Record.AddSourceLocation(C->getLParenLoc());
7657 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
7658 Record.push_back(C->varlist_size());
7659 Record.push_back(C->getUniqueDeclarationsNum());
7660 Record.push_back(C->getTotalComponentListNum());
7661 Record.push_back(C->getTotalComponentsNum());
7662 Record.AddSourceLocation(C->getLParenLoc());
7663 bool HasIteratorModifier = false;
7664 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
7665 Record.push_back(C->getMapTypeModifier(I));
7666 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
7667 if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)
7668 HasIteratorModifier = true;
7670 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7671 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7672 Record.push_back(C->getMapType());
7673 Record.AddSourceLocation(C->getMapLoc());
7674 Record.AddSourceLocation(C->getColonLoc());
7675 for (auto *E : C->varlist())
7676 Record.AddStmt(E);
7677 for (auto *E : C->mapperlists())
7678 Record.AddStmt(E);
7679 if (HasIteratorModifier)
7680 Record.AddStmt(C->getIteratorModifier());
7681 for (auto *D : C->all_decls())
7682 Record.AddDeclRef(D);
7683 for (auto N : C->all_num_lists())
7684 Record.push_back(N);
7685 for (auto N : C->all_lists_sizes())
7686 Record.push_back(N);
7687 for (auto &M : C->all_components()) {
7688 Record.AddStmt(M.getAssociatedExpression());
7689 Record.AddDeclRef(M.getAssociatedDeclaration());
7693 void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
7694 Record.push_back(C->varlist_size());
7695 Record.writeEnum(C->getAllocatorModifier());
7696 Record.AddSourceLocation(C->getLParenLoc());
7697 Record.AddSourceLocation(C->getColonLoc());
7698 Record.AddStmt(C->getAllocator());
7699 for (auto *VE : C->varlist())
7700 Record.AddStmt(VE);
7703 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
7704 Record.push_back(C->varlist_size());
7705 VisitOMPClauseWithPreInit(C);
7706 Record.AddSourceLocation(C->getLParenLoc());
7707 for (auto *VE : C->varlist())
7708 Record.AddStmt(VE);
7711 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
7712 Record.push_back(C->varlist_size());
7713 VisitOMPClauseWithPreInit(C);
7714 Record.AddSourceLocation(C->getLParenLoc());
7715 for (auto *VE : C->varlist())
7716 Record.AddStmt(VE);
7719 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
7720 VisitOMPClauseWithPreInit(C);
7721 Record.AddStmt(C->getPriority());
7722 Record.AddSourceLocation(C->getLParenLoc());
7725 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
7726 VisitOMPClauseWithPreInit(C);
7727 Record.writeEnum(C->getModifier());
7728 Record.AddStmt(C->getGrainsize());
7729 Record.AddSourceLocation(C->getModifierLoc());
7730 Record.AddSourceLocation(C->getLParenLoc());
7733 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
7734 VisitOMPClauseWithPreInit(C);
7735 Record.writeEnum(C->getModifier());
7736 Record.AddStmt(C->getNumTasks());
7737 Record.AddSourceLocation(C->getModifierLoc());
7738 Record.AddSourceLocation(C->getLParenLoc());
7741 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
7742 Record.AddStmt(C->getHint());
7743 Record.AddSourceLocation(C->getLParenLoc());
7746 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
7747 VisitOMPClauseWithPreInit(C);
7748 Record.push_back(C->getDistScheduleKind());
7749 Record.AddStmt(C->getChunkSize());
7750 Record.AddSourceLocation(C->getLParenLoc());
7751 Record.AddSourceLocation(C->getDistScheduleKindLoc());
7752 Record.AddSourceLocation(C->getCommaLoc());
7755 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
7756 Record.push_back(C->getDefaultmapKind());
7757 Record.push_back(C->getDefaultmapModifier());
7758 Record.AddSourceLocation(C->getLParenLoc());
7759 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
7760 Record.AddSourceLocation(C->getDefaultmapKindLoc());
7763 void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
7764 Record.push_back(C->varlist_size());
7765 Record.push_back(C->getUniqueDeclarationsNum());
7766 Record.push_back(C->getTotalComponentListNum());
7767 Record.push_back(C->getTotalComponentsNum());
7768 Record.AddSourceLocation(C->getLParenLoc());
7769 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7770 Record.push_back(C->getMotionModifier(I));
7771 Record.AddSourceLocation(C->getMotionModifierLoc(I));
7773 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7774 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7775 Record.AddSourceLocation(C->getColonLoc());
7776 for (auto *E : C->varlist())
7777 Record.AddStmt(E);
7778 for (auto *E : C->mapperlists())
7779 Record.AddStmt(E);
7780 for (auto *D : C->all_decls())
7781 Record.AddDeclRef(D);
7782 for (auto N : C->all_num_lists())
7783 Record.push_back(N);
7784 for (auto N : C->all_lists_sizes())
7785 Record.push_back(N);
7786 for (auto &M : C->all_components()) {
7787 Record.AddStmt(M.getAssociatedExpression());
7788 Record.writeBool(M.isNonContiguous());
7789 Record.AddDeclRef(M.getAssociatedDeclaration());
7793 void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
7794 Record.push_back(C->varlist_size());
7795 Record.push_back(C->getUniqueDeclarationsNum());
7796 Record.push_back(C->getTotalComponentListNum());
7797 Record.push_back(C->getTotalComponentsNum());
7798 Record.AddSourceLocation(C->getLParenLoc());
7799 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7800 Record.push_back(C->getMotionModifier(I));
7801 Record.AddSourceLocation(C->getMotionModifierLoc(I));
7803 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7804 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7805 Record.AddSourceLocation(C->getColonLoc());
7806 for (auto *E : C->varlist())
7807 Record.AddStmt(E);
7808 for (auto *E : C->mapperlists())
7809 Record.AddStmt(E);
7810 for (auto *D : C->all_decls())
7811 Record.AddDeclRef(D);
7812 for (auto N : C->all_num_lists())
7813 Record.push_back(N);
7814 for (auto N : C->all_lists_sizes())
7815 Record.push_back(N);
7816 for (auto &M : C->all_components()) {
7817 Record.AddStmt(M.getAssociatedExpression());
7818 Record.writeBool(M.isNonContiguous());
7819 Record.AddDeclRef(M.getAssociatedDeclaration());
7823 void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
7824 Record.push_back(C->varlist_size());
7825 Record.push_back(C->getUniqueDeclarationsNum());
7826 Record.push_back(C->getTotalComponentListNum());
7827 Record.push_back(C->getTotalComponentsNum());
7828 Record.AddSourceLocation(C->getLParenLoc());
7829 for (auto *E : C->varlist())
7830 Record.AddStmt(E);
7831 for (auto *VE : C->private_copies())
7832 Record.AddStmt(VE);
7833 for (auto *VE : C->inits())
7834 Record.AddStmt(VE);
7835 for (auto *D : C->all_decls())
7836 Record.AddDeclRef(D);
7837 for (auto N : C->all_num_lists())
7838 Record.push_back(N);
7839 for (auto N : C->all_lists_sizes())
7840 Record.push_back(N);
7841 for (auto &M : C->all_components()) {
7842 Record.AddStmt(M.getAssociatedExpression());
7843 Record.AddDeclRef(M.getAssociatedDeclaration());
7847 void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
7848 Record.push_back(C->varlist_size());
7849 Record.push_back(C->getUniqueDeclarationsNum());
7850 Record.push_back(C->getTotalComponentListNum());
7851 Record.push_back(C->getTotalComponentsNum());
7852 Record.AddSourceLocation(C->getLParenLoc());
7853 for (auto *E : C->varlist())
7854 Record.AddStmt(E);
7855 for (auto *D : C->all_decls())
7856 Record.AddDeclRef(D);
7857 for (auto N : C->all_num_lists())
7858 Record.push_back(N);
7859 for (auto N : C->all_lists_sizes())
7860 Record.push_back(N);
7861 for (auto &M : C->all_components()) {
7862 Record.AddStmt(M.getAssociatedExpression());
7863 Record.AddDeclRef(M.getAssociatedDeclaration());
7867 void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
7868 Record.push_back(C->varlist_size());
7869 Record.push_back(C->getUniqueDeclarationsNum());
7870 Record.push_back(C->getTotalComponentListNum());
7871 Record.push_back(C->getTotalComponentsNum());
7872 Record.AddSourceLocation(C->getLParenLoc());
7873 for (auto *E : C->varlist())
7874 Record.AddStmt(E);
7875 for (auto *D : C->all_decls())
7876 Record.AddDeclRef(D);
7877 for (auto N : C->all_num_lists())
7878 Record.push_back(N);
7879 for (auto N : C->all_lists_sizes())
7880 Record.push_back(N);
7881 for (auto &M : C->all_components()) {
7882 Record.AddStmt(M.getAssociatedExpression());
7883 Record.AddDeclRef(M.getAssociatedDeclaration());
7887 void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
7888 Record.push_back(C->varlist_size());
7889 Record.push_back(C->getUniqueDeclarationsNum());
7890 Record.push_back(C->getTotalComponentListNum());
7891 Record.push_back(C->getTotalComponentsNum());
7892 Record.AddSourceLocation(C->getLParenLoc());
7893 for (auto *E : C->varlist())
7894 Record.AddStmt(E);
7895 for (auto *D : C->all_decls())
7896 Record.AddDeclRef(D);
7897 for (auto N : C->all_num_lists())
7898 Record.push_back(N);
7899 for (auto N : C->all_lists_sizes())
7900 Record.push_back(N);
7901 for (auto &M : C->all_components()) {
7902 Record.AddStmt(M.getAssociatedExpression());
7903 Record.AddDeclRef(M.getAssociatedDeclaration());
7907 void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
7909 void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7910 OMPUnifiedSharedMemoryClause *) {}
7912 void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
7914 void
7915 OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
7918 void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7919 OMPAtomicDefaultMemOrderClause *C) {
7920 Record.push_back(C->getAtomicDefaultMemOrderKind());
7921 Record.AddSourceLocation(C->getLParenLoc());
7922 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
7925 void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
7926 Record.push_back(C->getAtKind());
7927 Record.AddSourceLocation(C->getLParenLoc());
7928 Record.AddSourceLocation(C->getAtKindKwLoc());
7931 void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
7932 Record.push_back(C->getSeverityKind());
7933 Record.AddSourceLocation(C->getLParenLoc());
7934 Record.AddSourceLocation(C->getSeverityKindKwLoc());
7937 void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
7938 Record.AddStmt(C->getMessageString());
7939 Record.AddSourceLocation(C->getLParenLoc());
7942 void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
7943 Record.push_back(C->varlist_size());
7944 Record.AddSourceLocation(C->getLParenLoc());
7945 for (auto *VE : C->varlist())
7946 Record.AddStmt(VE);
7947 for (auto *E : C->private_refs())
7948 Record.AddStmt(E);
7951 void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
7952 Record.push_back(C->varlist_size());
7953 Record.AddSourceLocation(C->getLParenLoc());
7954 for (auto *VE : C->varlist())
7955 Record.AddStmt(VE);
7958 void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
7959 Record.push_back(C->varlist_size());
7960 Record.AddSourceLocation(C->getLParenLoc());
7961 for (auto *VE : C->varlist())
7962 Record.AddStmt(VE);
7965 void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
7966 Record.writeEnum(C->getKind());
7967 Record.writeEnum(C->getModifier());
7968 Record.AddSourceLocation(C->getLParenLoc());
7969 Record.AddSourceLocation(C->getKindKwLoc());
7970 Record.AddSourceLocation(C->getModifierKwLoc());
7973 void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
7974 Record.push_back(C->getNumberOfAllocators());
7975 Record.AddSourceLocation(C->getLParenLoc());
7976 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
7977 OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
7978 Record.AddStmt(Data.Allocator);
7979 Record.AddStmt(Data.AllocatorTraits);
7980 Record.AddSourceLocation(Data.LParenLoc);
7981 Record.AddSourceLocation(Data.RParenLoc);
7985 void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
7986 Record.push_back(C->varlist_size());
7987 Record.AddSourceLocation(C->getLParenLoc());
7988 Record.AddStmt(C->getModifier());
7989 Record.AddSourceLocation(C->getColonLoc());
7990 for (Expr *E : C->varlist())
7991 Record.AddStmt(E);
7994 void OMPClauseWriter::VisitOMPBindClause(OMPBindClause *C) {
7995 Record.writeEnum(C->getBindKind());
7996 Record.AddSourceLocation(C->getLParenLoc());
7997 Record.AddSourceLocation(C->getBindKindLoc());
8000 void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
8001 VisitOMPClauseWithPreInit(C);
8002 Record.AddStmt(C->getSize());
8003 Record.AddSourceLocation(C->getLParenLoc());
8006 void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
8007 Record.push_back(C->varlist_size());
8008 Record.push_back(C->getNumLoops());
8009 Record.AddSourceLocation(C->getLParenLoc());
8010 Record.push_back(C->getDependenceType());
8011 Record.AddSourceLocation(C->getDependenceLoc());
8012 Record.AddSourceLocation(C->getColonLoc());
8013 for (auto *VE : C->varlist())
8014 Record.AddStmt(VE);
8015 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
8016 Record.AddStmt(C->getLoopData(I));
8019 void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
8020 Record.AddAttributes(C->getAttrs());
8021 Record.AddSourceLocation(C->getBeginLoc());
8022 Record.AddSourceLocation(C->getLParenLoc());
8023 Record.AddSourceLocation(C->getEndLoc());
8026 void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause *C) {}
8028 void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
8029 writeUInt32(TI->Sets.size());
8030 for (const auto &Set : TI->Sets) {
8031 writeEnum(Set.Kind);
8032 writeUInt32(Set.Selectors.size());
8033 for (const auto &Selector : Set.Selectors) {
8034 writeEnum(Selector.Kind);
8035 writeBool(Selector.ScoreOrCondition);
8036 if (Selector.ScoreOrCondition)
8037 writeExprRef(Selector.ScoreOrCondition);
8038 writeUInt32(Selector.Properties.size());
8039 for (const auto &Property : Selector.Properties)
8040 writeEnum(Property.Kind);
8045 void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
8046 if (!Data)
8047 return;
8048 writeUInt32(Data->getNumClauses());
8049 writeUInt32(Data->getNumChildren());
8050 writeBool(Data->hasAssociatedStmt());
8051 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
8052 writeOMPClause(Data->getClauses()[I]);
8053 if (Data->hasAssociatedStmt())
8054 AddStmt(Data->getAssociatedStmt());
8055 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
8056 AddStmt(Data->getChildren()[I]);
8059 void ASTRecordWriter::writeOpenACCVarList(const OpenACCClauseWithVarList *C) {
8060 writeUInt32(C->getVarList().size());
8061 for (Expr *E : C->getVarList())
8062 AddStmt(E);
8065 void ASTRecordWriter::writeOpenACCIntExprList(ArrayRef<Expr *> Exprs) {
8066 writeUInt32(Exprs.size());
8067 for (Expr *E : Exprs)
8068 AddStmt(E);
8071 void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
8072 writeEnum(C->getClauseKind());
8073 writeSourceLocation(C->getBeginLoc());
8074 writeSourceLocation(C->getEndLoc());
8076 switch (C->getClauseKind()) {
8077 case OpenACCClauseKind::Default: {
8078 const auto *DC = cast<OpenACCDefaultClause>(C);
8079 writeSourceLocation(DC->getLParenLoc());
8080 writeEnum(DC->getDefaultClauseKind());
8081 return;
8083 case OpenACCClauseKind::If: {
8084 const auto *IC = cast<OpenACCIfClause>(C);
8085 writeSourceLocation(IC->getLParenLoc());
8086 AddStmt(const_cast<Expr*>(IC->getConditionExpr()));
8087 return;
8089 case OpenACCClauseKind::Self: {
8090 const auto *SC = cast<OpenACCSelfClause>(C);
8091 writeSourceLocation(SC->getLParenLoc());
8092 writeBool(SC->hasConditionExpr());
8093 if (SC->hasConditionExpr())
8094 AddStmt(const_cast<Expr*>(SC->getConditionExpr()));
8095 return;
8097 case OpenACCClauseKind::NumGangs: {
8098 const auto *NGC = cast<OpenACCNumGangsClause>(C);
8099 writeSourceLocation(NGC->getLParenLoc());
8100 writeUInt32(NGC->getIntExprs().size());
8101 for (Expr *E : NGC->getIntExprs())
8102 AddStmt(E);
8103 return;
8105 case OpenACCClauseKind::NumWorkers: {
8106 const auto *NWC = cast<OpenACCNumWorkersClause>(C);
8107 writeSourceLocation(NWC->getLParenLoc());
8108 AddStmt(const_cast<Expr*>(NWC->getIntExpr()));
8109 return;
8111 case OpenACCClauseKind::VectorLength: {
8112 const auto *NWC = cast<OpenACCVectorLengthClause>(C);
8113 writeSourceLocation(NWC->getLParenLoc());
8114 AddStmt(const_cast<Expr*>(NWC->getIntExpr()));
8115 return;
8117 case OpenACCClauseKind::Private: {
8118 const auto *PC = cast<OpenACCPrivateClause>(C);
8119 writeSourceLocation(PC->getLParenLoc());
8120 writeOpenACCVarList(PC);
8121 return;
8123 case OpenACCClauseKind::FirstPrivate: {
8124 const auto *FPC = cast<OpenACCFirstPrivateClause>(C);
8125 writeSourceLocation(FPC->getLParenLoc());
8126 writeOpenACCVarList(FPC);
8127 return;
8129 case OpenACCClauseKind::Attach: {
8130 const auto *AC = cast<OpenACCAttachClause>(C);
8131 writeSourceLocation(AC->getLParenLoc());
8132 writeOpenACCVarList(AC);
8133 return;
8135 case OpenACCClauseKind::DevicePtr: {
8136 const auto *DPC = cast<OpenACCDevicePtrClause>(C);
8137 writeSourceLocation(DPC->getLParenLoc());
8138 writeOpenACCVarList(DPC);
8139 return;
8141 case OpenACCClauseKind::NoCreate: {
8142 const auto *NCC = cast<OpenACCNoCreateClause>(C);
8143 writeSourceLocation(NCC->getLParenLoc());
8144 writeOpenACCVarList(NCC);
8145 return;
8147 case OpenACCClauseKind::Present: {
8148 const auto *PC = cast<OpenACCPresentClause>(C);
8149 writeSourceLocation(PC->getLParenLoc());
8150 writeOpenACCVarList(PC);
8151 return;
8153 case OpenACCClauseKind::Copy:
8154 case OpenACCClauseKind::PCopy:
8155 case OpenACCClauseKind::PresentOrCopy: {
8156 const auto *CC = cast<OpenACCCopyClause>(C);
8157 writeSourceLocation(CC->getLParenLoc());
8158 writeOpenACCVarList(CC);
8159 return;
8161 case OpenACCClauseKind::CopyIn:
8162 case OpenACCClauseKind::PCopyIn:
8163 case OpenACCClauseKind::PresentOrCopyIn: {
8164 const auto *CIC = cast<OpenACCCopyInClause>(C);
8165 writeSourceLocation(CIC->getLParenLoc());
8166 writeBool(CIC->isReadOnly());
8167 writeOpenACCVarList(CIC);
8168 return;
8170 case OpenACCClauseKind::CopyOut:
8171 case OpenACCClauseKind::PCopyOut:
8172 case OpenACCClauseKind::PresentOrCopyOut: {
8173 const auto *COC = cast<OpenACCCopyOutClause>(C);
8174 writeSourceLocation(COC->getLParenLoc());
8175 writeBool(COC->isZero());
8176 writeOpenACCVarList(COC);
8177 return;
8179 case OpenACCClauseKind::Create:
8180 case OpenACCClauseKind::PCreate:
8181 case OpenACCClauseKind::PresentOrCreate: {
8182 const auto *CC = cast<OpenACCCreateClause>(C);
8183 writeSourceLocation(CC->getLParenLoc());
8184 writeBool(CC->isZero());
8185 writeOpenACCVarList(CC);
8186 return;
8188 case OpenACCClauseKind::Async: {
8189 const auto *AC = cast<OpenACCAsyncClause>(C);
8190 writeSourceLocation(AC->getLParenLoc());
8191 writeBool(AC->hasIntExpr());
8192 if (AC->hasIntExpr())
8193 AddStmt(const_cast<Expr*>(AC->getIntExpr()));
8194 return;
8196 case OpenACCClauseKind::Wait: {
8197 const auto *WC = cast<OpenACCWaitClause>(C);
8198 writeSourceLocation(WC->getLParenLoc());
8199 writeBool(WC->getDevNumExpr());
8200 if (Expr *DNE = WC->getDevNumExpr())
8201 AddStmt(DNE);
8202 writeSourceLocation(WC->getQueuesLoc());
8204 writeOpenACCIntExprList(WC->getQueueIdExprs());
8205 return;
8207 case OpenACCClauseKind::DeviceType:
8208 case OpenACCClauseKind::DType: {
8209 const auto *DTC = cast<OpenACCDeviceTypeClause>(C);
8210 writeSourceLocation(DTC->getLParenLoc());
8211 writeUInt32(DTC->getArchitectures().size());
8212 for (const DeviceTypeArgument &Arg : DTC->getArchitectures()) {
8213 writeBool(Arg.first);
8214 if (Arg.first)
8215 AddIdentifierRef(Arg.first);
8216 writeSourceLocation(Arg.second);
8218 return;
8220 case OpenACCClauseKind::Reduction: {
8221 const auto *RC = cast<OpenACCReductionClause>(C);
8222 writeSourceLocation(RC->getLParenLoc());
8223 writeEnum(RC->getReductionOp());
8224 writeOpenACCVarList(RC);
8225 return;
8227 case OpenACCClauseKind::Seq:
8228 case OpenACCClauseKind::Independent:
8229 case OpenACCClauseKind::Auto:
8230 // Nothing to do here, there is no additional information beyond the
8231 // begin/end loc and clause kind.
8232 return;
8233 case OpenACCClauseKind::Collapse: {
8234 const auto *CC = cast<OpenACCCollapseClause>(C);
8235 writeSourceLocation(CC->getLParenLoc());
8236 writeBool(CC->hasForce());
8237 AddStmt(const_cast<Expr *>(CC->getLoopCount()));
8238 return;
8240 case OpenACCClauseKind::Tile: {
8241 const auto *TC = cast<OpenACCTileClause>(C);
8242 writeSourceLocation(TC->getLParenLoc());
8243 writeUInt32(TC->getSizeExprs().size());
8244 for (Expr *E : TC->getSizeExprs())
8245 AddStmt(E);
8246 return;
8248 case OpenACCClauseKind::Gang: {
8249 const auto *GC = cast<OpenACCGangClause>(C);
8250 writeSourceLocation(GC->getLParenLoc());
8251 writeUInt32(GC->getNumExprs());
8252 for (unsigned I = 0; I < GC->getNumExprs(); ++I) {
8253 writeEnum(GC->getExpr(I).first);
8254 AddStmt(const_cast<Expr *>(GC->getExpr(I).second));
8256 return;
8258 case OpenACCClauseKind::Worker: {
8259 const auto *WC = cast<OpenACCWorkerClause>(C);
8260 writeSourceLocation(WC->getLParenLoc());
8261 writeBool(WC->hasIntExpr());
8262 if (WC->hasIntExpr())
8263 AddStmt(const_cast<Expr *>(WC->getIntExpr()));
8264 return;
8266 case OpenACCClauseKind::Vector: {
8267 const auto *VC = cast<OpenACCVectorClause>(C);
8268 writeSourceLocation(VC->getLParenLoc());
8269 writeBool(VC->hasIntExpr());
8270 if (VC->hasIntExpr())
8271 AddStmt(const_cast<Expr *>(VC->getIntExpr()));
8272 return;
8275 case OpenACCClauseKind::Finalize:
8276 case OpenACCClauseKind::IfPresent:
8277 case OpenACCClauseKind::NoHost:
8278 case OpenACCClauseKind::UseDevice:
8279 case OpenACCClauseKind::Delete:
8280 case OpenACCClauseKind::Detach:
8281 case OpenACCClauseKind::Device:
8282 case OpenACCClauseKind::DeviceResident:
8283 case OpenACCClauseKind::Host:
8284 case OpenACCClauseKind::Link:
8285 case OpenACCClauseKind::Bind:
8286 case OpenACCClauseKind::DeviceNum:
8287 case OpenACCClauseKind::DefaultAsync:
8288 case OpenACCClauseKind::Invalid:
8289 llvm_unreachable("Clause serialization not yet implemented");
8291 llvm_unreachable("Invalid Clause Kind");
8294 void ASTRecordWriter::writeOpenACCClauseList(
8295 ArrayRef<const OpenACCClause *> Clauses) {
8296 for (const OpenACCClause *Clause : Clauses)
8297 writeOpenACCClause(Clause);