[libc++][doc] Update the release notes for LLVM 18 (#78324)
[llvm-project.git] / clang / lib / Serialization / ASTWriter.cpp
bloba3fa99c3d7e49f751c6a33a70ae9a387dc792fe9
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/OpenMPClause.h"
33 #include "clang/AST/RawCommentList.h"
34 #include "clang/AST/TemplateName.h"
35 #include "clang/AST/Type.h"
36 #include "clang/AST/TypeLocVisitor.h"
37 #include "clang/Basic/Diagnostic.h"
38 #include "clang/Basic/DiagnosticOptions.h"
39 #include "clang/Basic/FileManager.h"
40 #include "clang/Basic/FileSystemOptions.h"
41 #include "clang/Basic/IdentifierTable.h"
42 #include "clang/Basic/LLVM.h"
43 #include "clang/Basic/Lambda.h"
44 #include "clang/Basic/LangOptions.h"
45 #include "clang/Basic/Module.h"
46 #include "clang/Basic/ObjCRuntime.h"
47 #include "clang/Basic/OpenCLOptions.h"
48 #include "clang/Basic/SourceLocation.h"
49 #include "clang/Basic/SourceManager.h"
50 #include "clang/Basic/SourceManagerInternals.h"
51 #include "clang/Basic/Specifiers.h"
52 #include "clang/Basic/TargetInfo.h"
53 #include "clang/Basic/TargetOptions.h"
54 #include "clang/Basic/Version.h"
55 #include "clang/Lex/HeaderSearch.h"
56 #include "clang/Lex/HeaderSearchOptions.h"
57 #include "clang/Lex/MacroInfo.h"
58 #include "clang/Lex/ModuleMap.h"
59 #include "clang/Lex/PreprocessingRecord.h"
60 #include "clang/Lex/Preprocessor.h"
61 #include "clang/Lex/PreprocessorOptions.h"
62 #include "clang/Lex/Token.h"
63 #include "clang/Sema/IdentifierResolver.h"
64 #include "clang/Sema/ObjCMethodList.h"
65 #include "clang/Sema/Sema.h"
66 #include "clang/Sema/Weak.h"
67 #include "clang/Serialization/ASTBitCodes.h"
68 #include "clang/Serialization/ASTReader.h"
69 #include "clang/Serialization/ASTRecordWriter.h"
70 #include "clang/Serialization/InMemoryModuleCache.h"
71 #include "clang/Serialization/ModuleFile.h"
72 #include "clang/Serialization/ModuleFileExtension.h"
73 #include "clang/Serialization/SerializationDiagnostic.h"
74 #include "llvm/ADT/APFloat.h"
75 #include "llvm/ADT/APInt.h"
76 #include "llvm/ADT/APSInt.h"
77 #include "llvm/ADT/ArrayRef.h"
78 #include "llvm/ADT/DenseMap.h"
79 #include "llvm/ADT/Hashing.h"
80 #include "llvm/ADT/PointerIntPair.h"
81 #include "llvm/ADT/STLExtras.h"
82 #include "llvm/ADT/ScopeExit.h"
83 #include "llvm/ADT/SmallPtrSet.h"
84 #include "llvm/ADT/SmallString.h"
85 #include "llvm/ADT/SmallVector.h"
86 #include "llvm/ADT/StringMap.h"
87 #include "llvm/ADT/StringRef.h"
88 #include "llvm/Bitstream/BitCodes.h"
89 #include "llvm/Bitstream/BitstreamWriter.h"
90 #include "llvm/Support/Casting.h"
91 #include "llvm/Support/Compression.h"
92 #include "llvm/Support/DJB.h"
93 #include "llvm/Support/Endian.h"
94 #include "llvm/Support/EndianStream.h"
95 #include "llvm/Support/Error.h"
96 #include "llvm/Support/ErrorHandling.h"
97 #include "llvm/Support/LEB128.h"
98 #include "llvm/Support/MemoryBuffer.h"
99 #include "llvm/Support/OnDiskHashTable.h"
100 #include "llvm/Support/Path.h"
101 #include "llvm/Support/SHA1.h"
102 #include "llvm/Support/TimeProfiler.h"
103 #include "llvm/Support/VersionTuple.h"
104 #include "llvm/Support/raw_ostream.h"
105 #include <algorithm>
106 #include <cassert>
107 #include <cstdint>
108 #include <cstdlib>
109 #include <cstring>
110 #include <ctime>
111 #include <limits>
112 #include <memory>
113 #include <optional>
114 #include <queue>
115 #include <tuple>
116 #include <utility>
117 #include <vector>
119 using namespace clang;
120 using namespace clang::serialization;
122 template <typename T, typename Allocator>
123 static StringRef bytes(const std::vector<T, Allocator> &v) {
124 if (v.empty()) return StringRef();
125 return StringRef(reinterpret_cast<const char*>(&v[0]),
126 sizeof(T) * v.size());
129 template <typename T>
130 static StringRef bytes(const SmallVectorImpl<T> &v) {
131 return StringRef(reinterpret_cast<const char*>(v.data()),
132 sizeof(T) * v.size());
135 static std::string bytes(const std::vector<bool> &V) {
136 std::string Str;
137 Str.reserve(V.size() / 8);
138 for (unsigned I = 0, E = V.size(); I < E;) {
139 char Byte = 0;
140 for (unsigned Bit = 0; Bit < 8 && I < E; ++Bit, ++I)
141 Byte |= V[I] << Bit;
142 Str += Byte;
144 return Str;
147 //===----------------------------------------------------------------------===//
148 // Type serialization
149 //===----------------------------------------------------------------------===//
151 static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
152 switch (id) {
153 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
154 case Type::CLASS_ID: return TYPE_##CODE_ID;
155 #include "clang/Serialization/TypeBitCodes.def"
156 case Type::Builtin:
157 llvm_unreachable("shouldn't be serializing a builtin type this way");
159 llvm_unreachable("bad type kind");
162 namespace {
164 std::set<const FileEntry *> GetAffectingModuleMaps(const Preprocessor &PP,
165 Module *RootModule) {
166 SmallVector<const Module *> ModulesToProcess{RootModule};
168 const HeaderSearch &HS = PP.getHeaderSearchInfo();
170 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
171 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
173 if (FilesByUID.size() > HS.header_file_size())
174 FilesByUID.resize(HS.header_file_size());
176 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
177 OptionalFileEntryRef File = FilesByUID[UID];
178 if (!File)
179 continue;
181 const HeaderFileInfo *HFI =
182 HS.getExistingFileInfo(*File, /*WantExternal*/ false);
183 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
184 continue;
186 for (const auto &KH : HS.findResolvedModulesForHeader(*File)) {
187 if (!KH.getModule())
188 continue;
189 ModulesToProcess.push_back(KH.getModule());
193 const ModuleMap &MM = HS.getModuleMap();
194 SourceManager &SourceMgr = PP.getSourceManager();
196 std::set<const FileEntry *> ModuleMaps{};
197 auto CollectIncludingModuleMaps = [&](FileEntryRef F) {
198 if (!ModuleMaps.insert(F).second)
199 return;
200 FileID FID = SourceMgr.translateFile(F);
201 SourceLocation Loc = SourceMgr.getIncludeLoc(FID);
202 // The include location of inferred module maps can point into the header
203 // file that triggered the inferring. Cut off the walk if that's the case.
204 while (Loc.isValid() && isModuleMap(SourceMgr.getFileCharacteristic(Loc))) {
205 FID = SourceMgr.getFileID(Loc);
206 if (!ModuleMaps.insert(*SourceMgr.getFileEntryRefForID(FID)).second)
207 break;
208 Loc = SourceMgr.getIncludeLoc(FID);
212 std::set<const Module *> ProcessedModules;
213 auto CollectIncludingMapsFromAncestors = [&](const Module *M) {
214 for (const Module *Mod = M; Mod; Mod = Mod->Parent) {
215 if (!ProcessedModules.insert(Mod).second)
216 break;
217 // The containing module map is affecting, because it's being pointed
218 // into by Module::DefinitionLoc.
219 if (auto ModuleMapFile = MM.getContainingModuleMapFile(Mod))
220 CollectIncludingModuleMaps(*ModuleMapFile);
221 // For inferred modules, the module map that allowed inferring is not in
222 // the include chain of the virtual containing module map file. It did
223 // affect the compilation, though.
224 if (auto ModuleMapFile = MM.getModuleMapFileForUniquing(Mod))
225 CollectIncludingModuleMaps(*ModuleMapFile);
229 for (const Module *CurrentModule : ModulesToProcess) {
230 CollectIncludingMapsFromAncestors(CurrentModule);
231 for (const Module *ImportedModule : CurrentModule->Imports)
232 CollectIncludingMapsFromAncestors(ImportedModule);
233 for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses)
234 CollectIncludingMapsFromAncestors(UndeclaredModule);
237 return ModuleMaps;
240 class ASTTypeWriter {
241 ASTWriter &Writer;
242 ASTWriter::RecordData Record;
243 ASTRecordWriter BasicWriter;
245 public:
246 ASTTypeWriter(ASTWriter &Writer)
247 : Writer(Writer), BasicWriter(Writer, Record) {}
249 uint64_t write(QualType T) {
250 if (T.hasLocalNonFastQualifiers()) {
251 Qualifiers Qs = T.getLocalQualifiers();
252 BasicWriter.writeQualType(T.getLocalUnqualifiedType());
253 BasicWriter.writeQualifiers(Qs);
254 return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
257 const Type *typePtr = T.getTypePtr();
258 serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
259 atw.write(typePtr);
260 return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
261 /*abbrev*/ 0);
265 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
266 using LocSeq = SourceLocationSequence;
268 ASTRecordWriter &Record;
269 LocSeq *Seq;
271 void addSourceLocation(SourceLocation Loc) {
272 Record.AddSourceLocation(Loc, Seq);
274 void addSourceRange(SourceRange Range) { Record.AddSourceRange(Range, Seq); }
276 public:
277 TypeLocWriter(ASTRecordWriter &Record, LocSeq *Seq)
278 : Record(Record), Seq(Seq) {}
280 #define ABSTRACT_TYPELOC(CLASS, PARENT)
281 #define TYPELOC(CLASS, PARENT) \
282 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
283 #include "clang/AST/TypeLocNodes.def"
285 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
286 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
289 } // namespace
291 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
292 // nothing to do
295 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
296 addSourceLocation(TL.getBuiltinLoc());
297 if (TL.needsExtraLocalData()) {
298 Record.push_back(TL.getWrittenTypeSpec());
299 Record.push_back(static_cast<uint64_t>(TL.getWrittenSignSpec()));
300 Record.push_back(static_cast<uint64_t>(TL.getWrittenWidthSpec()));
301 Record.push_back(TL.hasModeAttr());
305 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
306 addSourceLocation(TL.getNameLoc());
309 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
310 addSourceLocation(TL.getStarLoc());
313 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
314 // nothing to do
317 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
318 // nothing to do
321 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
322 addSourceLocation(TL.getCaretLoc());
325 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
326 addSourceLocation(TL.getAmpLoc());
329 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
330 addSourceLocation(TL.getAmpAmpLoc());
333 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
334 addSourceLocation(TL.getStarLoc());
335 Record.AddTypeSourceInfo(TL.getClassTInfo());
338 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
339 addSourceLocation(TL.getLBracketLoc());
340 addSourceLocation(TL.getRBracketLoc());
341 Record.push_back(TL.getSizeExpr() ? 1 : 0);
342 if (TL.getSizeExpr())
343 Record.AddStmt(TL.getSizeExpr());
346 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
347 VisitArrayTypeLoc(TL);
350 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
351 VisitArrayTypeLoc(TL);
354 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
355 VisitArrayTypeLoc(TL);
358 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
359 DependentSizedArrayTypeLoc TL) {
360 VisitArrayTypeLoc(TL);
363 void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
364 DependentAddressSpaceTypeLoc TL) {
365 addSourceLocation(TL.getAttrNameLoc());
366 SourceRange range = TL.getAttrOperandParensRange();
367 addSourceLocation(range.getBegin());
368 addSourceLocation(range.getEnd());
369 Record.AddStmt(TL.getAttrExprOperand());
372 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
373 DependentSizedExtVectorTypeLoc TL) {
374 addSourceLocation(TL.getNameLoc());
377 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
378 addSourceLocation(TL.getNameLoc());
381 void TypeLocWriter::VisitDependentVectorTypeLoc(
382 DependentVectorTypeLoc TL) {
383 addSourceLocation(TL.getNameLoc());
386 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
387 addSourceLocation(TL.getNameLoc());
390 void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
391 addSourceLocation(TL.getAttrNameLoc());
392 SourceRange range = TL.getAttrOperandParensRange();
393 addSourceLocation(range.getBegin());
394 addSourceLocation(range.getEnd());
395 Record.AddStmt(TL.getAttrRowOperand());
396 Record.AddStmt(TL.getAttrColumnOperand());
399 void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
400 DependentSizedMatrixTypeLoc TL) {
401 addSourceLocation(TL.getAttrNameLoc());
402 SourceRange range = TL.getAttrOperandParensRange();
403 addSourceLocation(range.getBegin());
404 addSourceLocation(range.getEnd());
405 Record.AddStmt(TL.getAttrRowOperand());
406 Record.AddStmt(TL.getAttrColumnOperand());
409 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
410 addSourceLocation(TL.getLocalRangeBegin());
411 addSourceLocation(TL.getLParenLoc());
412 addSourceLocation(TL.getRParenLoc());
413 addSourceRange(TL.getExceptionSpecRange());
414 addSourceLocation(TL.getLocalRangeEnd());
415 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
416 Record.AddDeclRef(TL.getParam(i));
419 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
420 VisitFunctionTypeLoc(TL);
423 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
424 VisitFunctionTypeLoc(TL);
427 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
428 addSourceLocation(TL.getNameLoc());
431 void TypeLocWriter::VisitUsingTypeLoc(UsingTypeLoc TL) {
432 addSourceLocation(TL.getNameLoc());
435 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
436 addSourceLocation(TL.getNameLoc());
439 void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
440 if (TL.getNumProtocols()) {
441 addSourceLocation(TL.getProtocolLAngleLoc());
442 addSourceLocation(TL.getProtocolRAngleLoc());
444 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
445 addSourceLocation(TL.getProtocolLoc(i));
448 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
449 addSourceLocation(TL.getTypeofLoc());
450 addSourceLocation(TL.getLParenLoc());
451 addSourceLocation(TL.getRParenLoc());
454 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
455 addSourceLocation(TL.getTypeofLoc());
456 addSourceLocation(TL.getLParenLoc());
457 addSourceLocation(TL.getRParenLoc());
458 Record.AddTypeSourceInfo(TL.getUnmodifiedTInfo());
461 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
462 addSourceLocation(TL.getDecltypeLoc());
463 addSourceLocation(TL.getRParenLoc());
466 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
467 addSourceLocation(TL.getKWLoc());
468 addSourceLocation(TL.getLParenLoc());
469 addSourceLocation(TL.getRParenLoc());
470 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
473 void ASTRecordWriter::AddConceptReference(const ConceptReference *CR) {
474 assert(CR);
475 AddNestedNameSpecifierLoc(CR->getNestedNameSpecifierLoc());
476 AddSourceLocation(CR->getTemplateKWLoc());
477 AddDeclarationNameInfo(CR->getConceptNameInfo());
478 AddDeclRef(CR->getFoundDecl());
479 AddDeclRef(CR->getNamedConcept());
480 push_back(CR->getTemplateArgsAsWritten() != nullptr);
481 if (CR->getTemplateArgsAsWritten())
482 AddASTTemplateArgumentListInfo(CR->getTemplateArgsAsWritten());
485 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
486 addSourceLocation(TL.getNameLoc());
487 auto *CR = TL.getConceptReference();
488 Record.push_back(TL.isConstrained() && CR);
489 if (TL.isConstrained() && CR)
490 Record.AddConceptReference(CR);
491 Record.push_back(TL.isDecltypeAuto());
492 if (TL.isDecltypeAuto())
493 addSourceLocation(TL.getRParenLoc());
496 void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
497 DeducedTemplateSpecializationTypeLoc TL) {
498 addSourceLocation(TL.getTemplateNameLoc());
501 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
502 addSourceLocation(TL.getNameLoc());
505 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
506 addSourceLocation(TL.getNameLoc());
509 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
510 Record.AddAttr(TL.getAttr());
513 void TypeLocWriter::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
514 // Nothing to do.
517 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
518 addSourceLocation(TL.getNameLoc());
521 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
522 SubstTemplateTypeParmTypeLoc TL) {
523 addSourceLocation(TL.getNameLoc());
526 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
527 SubstTemplateTypeParmPackTypeLoc TL) {
528 addSourceLocation(TL.getNameLoc());
531 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
532 TemplateSpecializationTypeLoc TL) {
533 addSourceLocation(TL.getTemplateKeywordLoc());
534 addSourceLocation(TL.getTemplateNameLoc());
535 addSourceLocation(TL.getLAngleLoc());
536 addSourceLocation(TL.getRAngleLoc());
537 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
538 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
539 TL.getArgLoc(i).getLocInfo());
542 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
543 addSourceLocation(TL.getLParenLoc());
544 addSourceLocation(TL.getRParenLoc());
547 void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
548 addSourceLocation(TL.getExpansionLoc());
551 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
552 addSourceLocation(TL.getElaboratedKeywordLoc());
553 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
556 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
557 addSourceLocation(TL.getNameLoc());
560 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
561 addSourceLocation(TL.getElaboratedKeywordLoc());
562 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
563 addSourceLocation(TL.getNameLoc());
566 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
567 DependentTemplateSpecializationTypeLoc TL) {
568 addSourceLocation(TL.getElaboratedKeywordLoc());
569 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
570 addSourceLocation(TL.getTemplateKeywordLoc());
571 addSourceLocation(TL.getTemplateNameLoc());
572 addSourceLocation(TL.getLAngleLoc());
573 addSourceLocation(TL.getRAngleLoc());
574 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
575 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
576 TL.getArgLoc(I).getLocInfo());
579 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
580 addSourceLocation(TL.getEllipsisLoc());
583 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
584 addSourceLocation(TL.getNameLoc());
585 addSourceLocation(TL.getNameEndLoc());
588 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
589 Record.push_back(TL.hasBaseTypeAsWritten());
590 addSourceLocation(TL.getTypeArgsLAngleLoc());
591 addSourceLocation(TL.getTypeArgsRAngleLoc());
592 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
593 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
594 addSourceLocation(TL.getProtocolLAngleLoc());
595 addSourceLocation(TL.getProtocolRAngleLoc());
596 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
597 addSourceLocation(TL.getProtocolLoc(i));
600 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
601 addSourceLocation(TL.getStarLoc());
604 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
605 addSourceLocation(TL.getKWLoc());
606 addSourceLocation(TL.getLParenLoc());
607 addSourceLocation(TL.getRParenLoc());
610 void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
611 addSourceLocation(TL.getKWLoc());
614 void TypeLocWriter::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
615 addSourceLocation(TL.getNameLoc());
617 void TypeLocWriter::VisitDependentBitIntTypeLoc(
618 clang::DependentBitIntTypeLoc TL) {
619 addSourceLocation(TL.getNameLoc());
622 void ASTWriter::WriteTypeAbbrevs() {
623 using namespace llvm;
625 std::shared_ptr<BitCodeAbbrev> Abv;
627 // Abbreviation for TYPE_EXT_QUAL
628 Abv = std::make_shared<BitCodeAbbrev>();
629 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
630 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
631 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
632 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
635 //===----------------------------------------------------------------------===//
636 // ASTWriter Implementation
637 //===----------------------------------------------------------------------===//
639 static void EmitBlockID(unsigned ID, const char *Name,
640 llvm::BitstreamWriter &Stream,
641 ASTWriter::RecordDataImpl &Record) {
642 Record.clear();
643 Record.push_back(ID);
644 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
646 // Emit the block name if present.
647 if (!Name || Name[0] == 0)
648 return;
649 Record.clear();
650 while (*Name)
651 Record.push_back(*Name++);
652 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
655 static void EmitRecordID(unsigned ID, const char *Name,
656 llvm::BitstreamWriter &Stream,
657 ASTWriter::RecordDataImpl &Record) {
658 Record.clear();
659 Record.push_back(ID);
660 while (*Name)
661 Record.push_back(*Name++);
662 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
665 static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
666 ASTWriter::RecordDataImpl &Record) {
667 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
668 RECORD(STMT_STOP);
669 RECORD(STMT_NULL_PTR);
670 RECORD(STMT_REF_PTR);
671 RECORD(STMT_NULL);
672 RECORD(STMT_COMPOUND);
673 RECORD(STMT_CASE);
674 RECORD(STMT_DEFAULT);
675 RECORD(STMT_LABEL);
676 RECORD(STMT_ATTRIBUTED);
677 RECORD(STMT_IF);
678 RECORD(STMT_SWITCH);
679 RECORD(STMT_WHILE);
680 RECORD(STMT_DO);
681 RECORD(STMT_FOR);
682 RECORD(STMT_GOTO);
683 RECORD(STMT_INDIRECT_GOTO);
684 RECORD(STMT_CONTINUE);
685 RECORD(STMT_BREAK);
686 RECORD(STMT_RETURN);
687 RECORD(STMT_DECL);
688 RECORD(STMT_GCCASM);
689 RECORD(STMT_MSASM);
690 RECORD(EXPR_PREDEFINED);
691 RECORD(EXPR_DECL_REF);
692 RECORD(EXPR_INTEGER_LITERAL);
693 RECORD(EXPR_FIXEDPOINT_LITERAL);
694 RECORD(EXPR_FLOATING_LITERAL);
695 RECORD(EXPR_IMAGINARY_LITERAL);
696 RECORD(EXPR_STRING_LITERAL);
697 RECORD(EXPR_CHARACTER_LITERAL);
698 RECORD(EXPR_PAREN);
699 RECORD(EXPR_PAREN_LIST);
700 RECORD(EXPR_UNARY_OPERATOR);
701 RECORD(EXPR_SIZEOF_ALIGN_OF);
702 RECORD(EXPR_ARRAY_SUBSCRIPT);
703 RECORD(EXPR_CALL);
704 RECORD(EXPR_MEMBER);
705 RECORD(EXPR_BINARY_OPERATOR);
706 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
707 RECORD(EXPR_CONDITIONAL_OPERATOR);
708 RECORD(EXPR_IMPLICIT_CAST);
709 RECORD(EXPR_CSTYLE_CAST);
710 RECORD(EXPR_COMPOUND_LITERAL);
711 RECORD(EXPR_EXT_VECTOR_ELEMENT);
712 RECORD(EXPR_INIT_LIST);
713 RECORD(EXPR_DESIGNATED_INIT);
714 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
715 RECORD(EXPR_IMPLICIT_VALUE_INIT);
716 RECORD(EXPR_NO_INIT);
717 RECORD(EXPR_VA_ARG);
718 RECORD(EXPR_ADDR_LABEL);
719 RECORD(EXPR_STMT);
720 RECORD(EXPR_CHOOSE);
721 RECORD(EXPR_GNU_NULL);
722 RECORD(EXPR_SHUFFLE_VECTOR);
723 RECORD(EXPR_BLOCK);
724 RECORD(EXPR_GENERIC_SELECTION);
725 RECORD(EXPR_OBJC_STRING_LITERAL);
726 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
727 RECORD(EXPR_OBJC_ARRAY_LITERAL);
728 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
729 RECORD(EXPR_OBJC_ENCODE);
730 RECORD(EXPR_OBJC_SELECTOR_EXPR);
731 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
732 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
733 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
734 RECORD(EXPR_OBJC_KVC_REF_EXPR);
735 RECORD(EXPR_OBJC_MESSAGE_EXPR);
736 RECORD(STMT_OBJC_FOR_COLLECTION);
737 RECORD(STMT_OBJC_CATCH);
738 RECORD(STMT_OBJC_FINALLY);
739 RECORD(STMT_OBJC_AT_TRY);
740 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
741 RECORD(STMT_OBJC_AT_THROW);
742 RECORD(EXPR_OBJC_BOOL_LITERAL);
743 RECORD(STMT_CXX_CATCH);
744 RECORD(STMT_CXX_TRY);
745 RECORD(STMT_CXX_FOR_RANGE);
746 RECORD(EXPR_CXX_OPERATOR_CALL);
747 RECORD(EXPR_CXX_MEMBER_CALL);
748 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
749 RECORD(EXPR_CXX_CONSTRUCT);
750 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
751 RECORD(EXPR_CXX_STATIC_CAST);
752 RECORD(EXPR_CXX_DYNAMIC_CAST);
753 RECORD(EXPR_CXX_REINTERPRET_CAST);
754 RECORD(EXPR_CXX_CONST_CAST);
755 RECORD(EXPR_CXX_ADDRSPACE_CAST);
756 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
757 RECORD(EXPR_USER_DEFINED_LITERAL);
758 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
759 RECORD(EXPR_CXX_BOOL_LITERAL);
760 RECORD(EXPR_CXX_PAREN_LIST_INIT);
761 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
762 RECORD(EXPR_CXX_TYPEID_EXPR);
763 RECORD(EXPR_CXX_TYPEID_TYPE);
764 RECORD(EXPR_CXX_THIS);
765 RECORD(EXPR_CXX_THROW);
766 RECORD(EXPR_CXX_DEFAULT_ARG);
767 RECORD(EXPR_CXX_DEFAULT_INIT);
768 RECORD(EXPR_CXX_BIND_TEMPORARY);
769 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
770 RECORD(EXPR_CXX_NEW);
771 RECORD(EXPR_CXX_DELETE);
772 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
773 RECORD(EXPR_EXPR_WITH_CLEANUPS);
774 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
775 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
776 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
777 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
778 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
779 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
780 RECORD(EXPR_CXX_NOEXCEPT);
781 RECORD(EXPR_OPAQUE_VALUE);
782 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
783 RECORD(EXPR_TYPE_TRAIT);
784 RECORD(EXPR_ARRAY_TYPE_TRAIT);
785 RECORD(EXPR_PACK_EXPANSION);
786 RECORD(EXPR_SIZEOF_PACK);
787 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
788 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
789 RECORD(EXPR_FUNCTION_PARM_PACK);
790 RECORD(EXPR_MATERIALIZE_TEMPORARY);
791 RECORD(EXPR_CUDA_KERNEL_CALL);
792 RECORD(EXPR_CXX_UUIDOF_EXPR);
793 RECORD(EXPR_CXX_UUIDOF_TYPE);
794 RECORD(EXPR_LAMBDA);
795 #undef RECORD
798 void ASTWriter::WriteBlockInfoBlock() {
799 RecordData Record;
800 Stream.EnterBlockInfoBlock();
802 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
803 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
805 // Control Block.
806 BLOCK(CONTROL_BLOCK);
807 RECORD(METADATA);
808 RECORD(MODULE_NAME);
809 RECORD(MODULE_DIRECTORY);
810 RECORD(MODULE_MAP_FILE);
811 RECORD(IMPORTS);
812 RECORD(ORIGINAL_FILE);
813 RECORD(ORIGINAL_FILE_ID);
814 RECORD(INPUT_FILE_OFFSETS);
816 BLOCK(OPTIONS_BLOCK);
817 RECORD(LANGUAGE_OPTIONS);
818 RECORD(TARGET_OPTIONS);
819 RECORD(FILE_SYSTEM_OPTIONS);
820 RECORD(HEADER_SEARCH_OPTIONS);
821 RECORD(PREPROCESSOR_OPTIONS);
823 BLOCK(INPUT_FILES_BLOCK);
824 RECORD(INPUT_FILE);
825 RECORD(INPUT_FILE_HASH);
827 // AST Top-Level Block.
828 BLOCK(AST_BLOCK);
829 RECORD(TYPE_OFFSET);
830 RECORD(DECL_OFFSET);
831 RECORD(IDENTIFIER_OFFSET);
832 RECORD(IDENTIFIER_TABLE);
833 RECORD(EAGERLY_DESERIALIZED_DECLS);
834 RECORD(MODULAR_CODEGEN_DECLS);
835 RECORD(SPECIAL_TYPES);
836 RECORD(STATISTICS);
837 RECORD(TENTATIVE_DEFINITIONS);
838 RECORD(SELECTOR_OFFSETS);
839 RECORD(METHOD_POOL);
840 RECORD(PP_COUNTER_VALUE);
841 RECORD(SOURCE_LOCATION_OFFSETS);
842 RECORD(EXT_VECTOR_DECLS);
843 RECORD(UNUSED_FILESCOPED_DECLS);
844 RECORD(PPD_ENTITIES_OFFSETS);
845 RECORD(VTABLE_USES);
846 RECORD(PPD_SKIPPED_RANGES);
847 RECORD(REFERENCED_SELECTOR_POOL);
848 RECORD(TU_UPDATE_LEXICAL);
849 RECORD(SEMA_DECL_REFS);
850 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
851 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
852 RECORD(UPDATE_VISIBLE);
853 RECORD(DECL_UPDATE_OFFSETS);
854 RECORD(DECL_UPDATES);
855 RECORD(CUDA_SPECIAL_DECL_REFS);
856 RECORD(HEADER_SEARCH_TABLE);
857 RECORD(FP_PRAGMA_OPTIONS);
858 RECORD(OPENCL_EXTENSIONS);
859 RECORD(OPENCL_EXTENSION_TYPES);
860 RECORD(OPENCL_EXTENSION_DECLS);
861 RECORD(DELEGATING_CTORS);
862 RECORD(KNOWN_NAMESPACES);
863 RECORD(MODULE_OFFSET_MAP);
864 RECORD(SOURCE_MANAGER_LINE_TABLE);
865 RECORD(OBJC_CATEGORIES_MAP);
866 RECORD(FILE_SORTED_DECLS);
867 RECORD(IMPORTED_MODULES);
868 RECORD(OBJC_CATEGORIES);
869 RECORD(MACRO_OFFSET);
870 RECORD(INTERESTING_IDENTIFIERS);
871 RECORD(UNDEFINED_BUT_USED);
872 RECORD(LATE_PARSED_TEMPLATE);
873 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
874 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
875 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
876 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
877 RECORD(DELETE_EXPRS_TO_ANALYZE);
878 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
879 RECORD(PP_CONDITIONAL_STACK);
880 RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
881 RECORD(PP_ASSUME_NONNULL_LOC);
883 // SourceManager Block.
884 BLOCK(SOURCE_MANAGER_BLOCK);
885 RECORD(SM_SLOC_FILE_ENTRY);
886 RECORD(SM_SLOC_BUFFER_ENTRY);
887 RECORD(SM_SLOC_BUFFER_BLOB);
888 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
889 RECORD(SM_SLOC_EXPANSION_ENTRY);
891 // Preprocessor Block.
892 BLOCK(PREPROCESSOR_BLOCK);
893 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
894 RECORD(PP_MACRO_FUNCTION_LIKE);
895 RECORD(PP_MACRO_OBJECT_LIKE);
896 RECORD(PP_MODULE_MACRO);
897 RECORD(PP_TOKEN);
899 // Submodule Block.
900 BLOCK(SUBMODULE_BLOCK);
901 RECORD(SUBMODULE_METADATA);
902 RECORD(SUBMODULE_DEFINITION);
903 RECORD(SUBMODULE_UMBRELLA_HEADER);
904 RECORD(SUBMODULE_HEADER);
905 RECORD(SUBMODULE_TOPHEADER);
906 RECORD(SUBMODULE_UMBRELLA_DIR);
907 RECORD(SUBMODULE_IMPORTS);
908 RECORD(SUBMODULE_AFFECTING_MODULES);
909 RECORD(SUBMODULE_EXPORTS);
910 RECORD(SUBMODULE_REQUIRES);
911 RECORD(SUBMODULE_EXCLUDED_HEADER);
912 RECORD(SUBMODULE_LINK_LIBRARY);
913 RECORD(SUBMODULE_CONFIG_MACRO);
914 RECORD(SUBMODULE_CONFLICT);
915 RECORD(SUBMODULE_PRIVATE_HEADER);
916 RECORD(SUBMODULE_TEXTUAL_HEADER);
917 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
918 RECORD(SUBMODULE_INITIALIZERS);
919 RECORD(SUBMODULE_EXPORT_AS);
921 // Comments Block.
922 BLOCK(COMMENTS_BLOCK);
923 RECORD(COMMENTS_RAW_COMMENT);
925 // Decls and Types block.
926 BLOCK(DECLTYPES_BLOCK);
927 RECORD(TYPE_EXT_QUAL);
928 RECORD(TYPE_COMPLEX);
929 RECORD(TYPE_POINTER);
930 RECORD(TYPE_BLOCK_POINTER);
931 RECORD(TYPE_LVALUE_REFERENCE);
932 RECORD(TYPE_RVALUE_REFERENCE);
933 RECORD(TYPE_MEMBER_POINTER);
934 RECORD(TYPE_CONSTANT_ARRAY);
935 RECORD(TYPE_INCOMPLETE_ARRAY);
936 RECORD(TYPE_VARIABLE_ARRAY);
937 RECORD(TYPE_VECTOR);
938 RECORD(TYPE_EXT_VECTOR);
939 RECORD(TYPE_FUNCTION_NO_PROTO);
940 RECORD(TYPE_FUNCTION_PROTO);
941 RECORD(TYPE_TYPEDEF);
942 RECORD(TYPE_TYPEOF_EXPR);
943 RECORD(TYPE_TYPEOF);
944 RECORD(TYPE_RECORD);
945 RECORD(TYPE_ENUM);
946 RECORD(TYPE_OBJC_INTERFACE);
947 RECORD(TYPE_OBJC_OBJECT_POINTER);
948 RECORD(TYPE_DECLTYPE);
949 RECORD(TYPE_ELABORATED);
950 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
951 RECORD(TYPE_UNRESOLVED_USING);
952 RECORD(TYPE_INJECTED_CLASS_NAME);
953 RECORD(TYPE_OBJC_OBJECT);
954 RECORD(TYPE_TEMPLATE_TYPE_PARM);
955 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
956 RECORD(TYPE_DEPENDENT_NAME);
957 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
958 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
959 RECORD(TYPE_PAREN);
960 RECORD(TYPE_MACRO_QUALIFIED);
961 RECORD(TYPE_PACK_EXPANSION);
962 RECORD(TYPE_ATTRIBUTED);
963 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
964 RECORD(TYPE_AUTO);
965 RECORD(TYPE_UNARY_TRANSFORM);
966 RECORD(TYPE_ATOMIC);
967 RECORD(TYPE_DECAYED);
968 RECORD(TYPE_ADJUSTED);
969 RECORD(TYPE_OBJC_TYPE_PARAM);
970 RECORD(LOCAL_REDECLARATIONS);
971 RECORD(DECL_TYPEDEF);
972 RECORD(DECL_TYPEALIAS);
973 RECORD(DECL_ENUM);
974 RECORD(DECL_RECORD);
975 RECORD(DECL_ENUM_CONSTANT);
976 RECORD(DECL_FUNCTION);
977 RECORD(DECL_OBJC_METHOD);
978 RECORD(DECL_OBJC_INTERFACE);
979 RECORD(DECL_OBJC_PROTOCOL);
980 RECORD(DECL_OBJC_IVAR);
981 RECORD(DECL_OBJC_AT_DEFS_FIELD);
982 RECORD(DECL_OBJC_CATEGORY);
983 RECORD(DECL_OBJC_CATEGORY_IMPL);
984 RECORD(DECL_OBJC_IMPLEMENTATION);
985 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
986 RECORD(DECL_OBJC_PROPERTY);
987 RECORD(DECL_OBJC_PROPERTY_IMPL);
988 RECORD(DECL_FIELD);
989 RECORD(DECL_MS_PROPERTY);
990 RECORD(DECL_VAR);
991 RECORD(DECL_IMPLICIT_PARAM);
992 RECORD(DECL_PARM_VAR);
993 RECORD(DECL_FILE_SCOPE_ASM);
994 RECORD(DECL_BLOCK);
995 RECORD(DECL_CONTEXT_LEXICAL);
996 RECORD(DECL_CONTEXT_VISIBLE);
997 RECORD(DECL_NAMESPACE);
998 RECORD(DECL_NAMESPACE_ALIAS);
999 RECORD(DECL_USING);
1000 RECORD(DECL_USING_SHADOW);
1001 RECORD(DECL_USING_DIRECTIVE);
1002 RECORD(DECL_UNRESOLVED_USING_VALUE);
1003 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1004 RECORD(DECL_LINKAGE_SPEC);
1005 RECORD(DECL_CXX_RECORD);
1006 RECORD(DECL_CXX_METHOD);
1007 RECORD(DECL_CXX_CONSTRUCTOR);
1008 RECORD(DECL_CXX_DESTRUCTOR);
1009 RECORD(DECL_CXX_CONVERSION);
1010 RECORD(DECL_ACCESS_SPEC);
1011 RECORD(DECL_FRIEND);
1012 RECORD(DECL_FRIEND_TEMPLATE);
1013 RECORD(DECL_CLASS_TEMPLATE);
1014 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1015 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
1016 RECORD(DECL_VAR_TEMPLATE);
1017 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1018 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
1019 RECORD(DECL_FUNCTION_TEMPLATE);
1020 RECORD(DECL_TEMPLATE_TYPE_PARM);
1021 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1022 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1023 RECORD(DECL_CONCEPT);
1024 RECORD(DECL_REQUIRES_EXPR_BODY);
1025 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
1026 RECORD(DECL_STATIC_ASSERT);
1027 RECORD(DECL_CXX_BASE_SPECIFIERS);
1028 RECORD(DECL_CXX_CTOR_INITIALIZERS);
1029 RECORD(DECL_INDIRECTFIELD);
1030 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1031 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1032 RECORD(DECL_IMPORT);
1033 RECORD(DECL_OMP_THREADPRIVATE);
1034 RECORD(DECL_EMPTY);
1035 RECORD(DECL_OBJC_TYPE_PARAM);
1036 RECORD(DECL_OMP_CAPTUREDEXPR);
1037 RECORD(DECL_PRAGMA_COMMENT);
1038 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1039 RECORD(DECL_OMP_DECLARE_REDUCTION);
1040 RECORD(DECL_OMP_ALLOCATE);
1041 RECORD(DECL_HLSL_BUFFER);
1043 // Statements and Exprs can occur in the Decls and Types block.
1044 AddStmtsExprs(Stream, Record);
1046 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1047 RECORD(PPD_MACRO_EXPANSION);
1048 RECORD(PPD_MACRO_DEFINITION);
1049 RECORD(PPD_INCLUSION_DIRECTIVE);
1051 // Decls and Types block.
1052 BLOCK(EXTENSION_BLOCK);
1053 RECORD(EXTENSION_METADATA);
1055 BLOCK(UNHASHED_CONTROL_BLOCK);
1056 RECORD(SIGNATURE);
1057 RECORD(AST_BLOCK_HASH);
1058 RECORD(DIAGNOSTIC_OPTIONS);
1059 RECORD(HEADER_SEARCH_PATHS);
1060 RECORD(DIAG_PRAGMA_MAPPINGS);
1062 #undef RECORD
1063 #undef BLOCK
1064 Stream.ExitBlock();
1067 /// Prepares a path for being written to an AST file by converting it
1068 /// to an absolute path and removing nested './'s.
1070 /// \return \c true if the path was changed.
1071 static bool cleanPathForOutput(FileManager &FileMgr,
1072 SmallVectorImpl<char> &Path) {
1073 bool Changed = FileMgr.makeAbsolutePath(Path);
1074 return Changed | llvm::sys::path::remove_dots(Path);
1077 /// Adjusts the given filename to only write out the portion of the
1078 /// filename that is not part of the system root directory.
1080 /// \param Filename the file name to adjust.
1082 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1083 /// the returned filename will be adjusted by this root directory.
1085 /// \returns either the original filename (if it needs no adjustment) or the
1086 /// adjusted filename (which points into the @p Filename parameter).
1087 static const char *
1088 adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1089 assert(Filename && "No file name to adjust?");
1091 if (BaseDir.empty())
1092 return Filename;
1094 // Verify that the filename and the system root have the same prefix.
1095 unsigned Pos = 0;
1096 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1097 if (Filename[Pos] != BaseDir[Pos])
1098 return Filename; // Prefixes don't match.
1100 // We hit the end of the filename before we hit the end of the system root.
1101 if (!Filename[Pos])
1102 return Filename;
1104 // If there's not a path separator at the end of the base directory nor
1105 // immediately after it, then this isn't within the base directory.
1106 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1107 if (!llvm::sys::path::is_separator(BaseDir.back()))
1108 return Filename;
1109 } else {
1110 // If the file name has a '/' at the current position, skip over the '/'.
1111 // We distinguish relative paths from absolute paths by the
1112 // absence of '/' at the beginning of relative paths.
1114 // FIXME: This is wrong. We distinguish them by asking if the path is
1115 // absolute, which isn't the same thing. And there might be multiple '/'s
1116 // in a row. Use a better mechanism to indicate whether we have emitted an
1117 // absolute or relative path.
1118 ++Pos;
1121 return Filename + Pos;
1124 std::pair<ASTFileSignature, ASTFileSignature>
1125 ASTWriter::createSignature() const {
1126 StringRef AllBytes(Buffer.data(), Buffer.size());
1128 llvm::SHA1 Hasher;
1129 Hasher.update(AllBytes.slice(ASTBlockRange.first, ASTBlockRange.second));
1130 ASTFileSignature ASTBlockHash = ASTFileSignature::create(Hasher.result());
1132 // Add the remaining bytes:
1133 // 1. Before the unhashed control block.
1134 Hasher.update(AllBytes.slice(0, UnhashedControlBlockRange.first));
1135 // 2. Between the unhashed control block and the AST block.
1136 Hasher.update(
1137 AllBytes.slice(UnhashedControlBlockRange.second, ASTBlockRange.first));
1138 // 3. After the AST block.
1139 Hasher.update(AllBytes.slice(ASTBlockRange.second, StringRef::npos));
1140 ASTFileSignature Signature = ASTFileSignature::create(Hasher.result());
1142 return std::make_pair(ASTBlockHash, Signature);
1145 ASTFileSignature ASTWriter::backpatchSignature() {
1146 if (!WritingModule ||
1147 !PP->getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent)
1148 return {};
1150 // For implicit modules, write the hash of the PCM as its signature.
1152 auto BackpatchSignatureAt = [&](const ASTFileSignature &S, uint64_t BitNo) {
1153 for (uint8_t Byte : S) {
1154 Stream.BackpatchByte(BitNo, Byte);
1155 BitNo += 8;
1159 ASTFileSignature ASTBlockHash;
1160 ASTFileSignature Signature;
1161 std::tie(ASTBlockHash, Signature) = createSignature();
1163 BackpatchSignatureAt(ASTBlockHash, ASTBlockHashOffset);
1164 BackpatchSignatureAt(Signature, SignatureOffset);
1166 return Signature;
1169 void ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1170 ASTContext &Context) {
1171 using namespace llvm;
1173 // Flush first to prepare the PCM hash (signature).
1174 Stream.FlushToWord();
1175 UnhashedControlBlockRange.first = Stream.GetCurrentBitNo() >> 3;
1177 // Enter the block and prepare to write records.
1178 RecordData Record;
1179 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1181 // For implicit modules, write the hash of the PCM as its signature.
1182 if (WritingModule &&
1183 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1184 // At this point, we don't know the actual signature of the file or the AST
1185 // block - we're only able to compute those at the end of the serialization
1186 // process. Let's store dummy signatures for now, and replace them with the
1187 // real ones later on.
1188 // The bitstream VBR-encodes record elements, which makes backpatching them
1189 // really difficult. Let's store the signatures as blobs instead - they are
1190 // guaranteed to be word-aligned, and we control their format/encoding.
1191 auto Dummy = ASTFileSignature::createDummy();
1192 SmallString<128> Blob{Dummy.begin(), Dummy.end()};
1194 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1195 Abbrev->Add(BitCodeAbbrevOp(AST_BLOCK_HASH));
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1197 unsigned ASTBlockHashAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1199 Abbrev = std::make_shared<BitCodeAbbrev>();
1200 Abbrev->Add(BitCodeAbbrevOp(SIGNATURE));
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1202 unsigned SignatureAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1204 Record.push_back(AST_BLOCK_HASH);
1205 Stream.EmitRecordWithBlob(ASTBlockHashAbbrev, Record, Blob);
1206 ASTBlockHashOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1207 Record.clear();
1209 Record.push_back(SIGNATURE);
1210 Stream.EmitRecordWithBlob(SignatureAbbrev, Record, Blob);
1211 SignatureOffset = Stream.GetCurrentBitNo() - Blob.size() * 8;
1212 Record.clear();
1215 const auto &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1217 // Diagnostic options.
1218 const auto &Diags = Context.getDiagnostics();
1219 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1220 if (!HSOpts.ModulesSkipDiagnosticOptions) {
1221 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1222 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1223 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1224 #include "clang/Basic/DiagnosticOptions.def"
1225 Record.push_back(DiagOpts.Warnings.size());
1226 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1227 AddString(DiagOpts.Warnings[I], Record);
1228 Record.push_back(DiagOpts.Remarks.size());
1229 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1230 AddString(DiagOpts.Remarks[I], Record);
1231 // Note: we don't serialize the log or serialization file names, because
1232 // they are generally transient files and will almost always be overridden.
1233 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1234 Record.clear();
1237 // Header search paths.
1238 if (!HSOpts.ModulesSkipHeaderSearchPaths) {
1239 // Include entries.
1240 Record.push_back(HSOpts.UserEntries.size());
1241 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1242 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1243 AddString(Entry.Path, Record);
1244 Record.push_back(static_cast<unsigned>(Entry.Group));
1245 Record.push_back(Entry.IsFramework);
1246 Record.push_back(Entry.IgnoreSysRoot);
1249 // System header prefixes.
1250 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1251 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1252 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1253 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1256 // VFS overlay files.
1257 Record.push_back(HSOpts.VFSOverlayFiles.size());
1258 for (StringRef VFSOverlayFile : HSOpts.VFSOverlayFiles)
1259 AddString(VFSOverlayFile, Record);
1261 Stream.EmitRecord(HEADER_SEARCH_PATHS, Record);
1264 if (!HSOpts.ModulesSkipPragmaDiagnosticMappings)
1265 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1267 // Header search entry usage.
1268 auto HSEntryUsage = PP.getHeaderSearchInfo().computeUserEntryUsage();
1269 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1270 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1273 unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1275 RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1276 HSEntryUsage.size()};
1277 Stream.EmitRecordWithBlob(HSUsageAbbrevCode, Record, bytes(HSEntryUsage));
1280 // Leave the options block.
1281 Stream.ExitBlock();
1282 UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
1285 /// Write the control block.
1286 void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1287 StringRef isysroot) {
1288 using namespace llvm;
1290 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1291 RecordData Record;
1293 // Metadata
1294 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1295 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1296 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1297 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1298 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1299 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1300 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1301 // Standard C++ module
1302 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1303 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1304 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1305 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1306 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1307 assert((!WritingModule || isysroot.empty()) &&
1308 "writing module as a relocatable PCH?");
1310 RecordData::value_type Record[] = {METADATA,
1311 VERSION_MAJOR,
1312 VERSION_MINOR,
1313 CLANG_VERSION_MAJOR,
1314 CLANG_VERSION_MINOR,
1315 !isysroot.empty(),
1316 isWritingStdCXXNamedModules(),
1317 IncludeTimestamps,
1318 ASTHasCompilerErrors};
1319 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1320 getClangFullRepositoryVersion());
1323 if (WritingModule) {
1324 // Module name
1325 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1326 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1328 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1329 RecordData::value_type Record[] = {MODULE_NAME};
1330 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1333 if (WritingModule && WritingModule->Directory) {
1334 SmallString<128> BaseDir;
1335 if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1336 // Use the current working directory as the base path for all inputs.
1337 auto CWD =
1338 Context.getSourceManager().getFileManager().getOptionalDirectoryRef(
1339 ".");
1340 BaseDir.assign(CWD->getName());
1341 } else {
1342 BaseDir.assign(WritingModule->Directory->getName());
1344 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1346 // If the home of the module is the current working directory, then we
1347 // want to pick up the cwd of the build process loading the module, not
1348 // our cwd, when we load this module.
1349 if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1350 (!PP.getHeaderSearchInfo()
1351 .getHeaderSearchOpts()
1352 .ModuleMapFileHomeIsCwd ||
1353 WritingModule->Directory->getName() != StringRef("."))) {
1354 // Module directory.
1355 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1356 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1358 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1360 RecordData::value_type Record[] = {MODULE_DIRECTORY};
1361 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1364 // Write out all other paths relative to the base directory if possible.
1365 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1366 } else if (!isysroot.empty()) {
1367 // Write out paths relative to the sysroot if possible.
1368 BaseDirectory = std::string(isysroot);
1371 // Module map file
1372 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1373 Record.clear();
1375 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1376 AddPath(WritingModule->PresumedModuleMapFile.empty()
1377 ? Map.getModuleMapFileForUniquing(WritingModule)
1378 ->getNameAsRequested()
1379 : StringRef(WritingModule->PresumedModuleMapFile),
1380 Record);
1382 // Additional module map files.
1383 if (auto *AdditionalModMaps =
1384 Map.getAdditionalModuleMapFiles(WritingModule)) {
1385 Record.push_back(AdditionalModMaps->size());
1386 SmallVector<FileEntryRef, 1> ModMaps(AdditionalModMaps->begin(),
1387 AdditionalModMaps->end());
1388 llvm::sort(ModMaps, [](FileEntryRef A, FileEntryRef B) {
1389 return A.getName() < B.getName();
1391 for (FileEntryRef F : ModMaps)
1392 AddPath(F.getName(), Record);
1393 } else {
1394 Record.push_back(0);
1397 Stream.EmitRecord(MODULE_MAP_FILE, Record);
1400 // Imports
1401 if (Chain) {
1402 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1403 Record.clear();
1405 for (ModuleFile &M : Mgr) {
1406 // Skip modules that weren't directly imported.
1407 if (!M.isDirectlyImported())
1408 continue;
1410 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1411 Record.push_back(M.StandardCXXModule);
1412 AddSourceLocation(M.ImportLoc, Record);
1414 // We don't want to hard code the information about imported modules
1415 // in the C++20 named modules.
1416 if (!M.StandardCXXModule) {
1417 // If we have calculated signature, there is no need to store
1418 // the size or timestamp.
1419 Record.push_back(M.Signature ? 0 : M.File.getSize());
1420 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1421 llvm::append_range(Record, M.Signature);
1424 AddString(M.ModuleName, Record);
1426 if (!M.StandardCXXModule)
1427 AddPath(M.FileName, Record);
1429 Stream.EmitRecord(IMPORTS, Record);
1432 // Write the options block.
1433 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1435 // Language options.
1436 Record.clear();
1437 const LangOptions &LangOpts = Context.getLangOpts();
1438 #define LANGOPT(Name, Bits, Default, Description) \
1439 Record.push_back(LangOpts.Name);
1440 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1441 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1442 #include "clang/Basic/LangOptions.def"
1443 #define SANITIZER(NAME, ID) \
1444 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1445 #include "clang/Basic/Sanitizers.def"
1447 Record.push_back(LangOpts.ModuleFeatures.size());
1448 for (StringRef Feature : LangOpts.ModuleFeatures)
1449 AddString(Feature, Record);
1451 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1452 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1454 AddString(LangOpts.CurrentModule, Record);
1456 // Comment options.
1457 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1458 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1459 AddString(I, Record);
1461 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1463 // OpenMP offloading options.
1464 Record.push_back(LangOpts.OMPTargetTriples.size());
1465 for (auto &T : LangOpts.OMPTargetTriples)
1466 AddString(T.getTriple(), Record);
1468 AddString(LangOpts.OMPHostIRFile, Record);
1470 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1472 // Target options.
1473 Record.clear();
1474 const TargetInfo &Target = Context.getTargetInfo();
1475 const TargetOptions &TargetOpts = Target.getTargetOpts();
1476 AddString(TargetOpts.Triple, Record);
1477 AddString(TargetOpts.CPU, Record);
1478 AddString(TargetOpts.TuneCPU, Record);
1479 AddString(TargetOpts.ABI, Record);
1480 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1481 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1482 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1484 Record.push_back(TargetOpts.Features.size());
1485 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1486 AddString(TargetOpts.Features[I], Record);
1488 Stream.EmitRecord(TARGET_OPTIONS, Record);
1490 // File system options.
1491 Record.clear();
1492 const FileSystemOptions &FSOpts =
1493 Context.getSourceManager().getFileManager().getFileSystemOpts();
1494 AddString(FSOpts.WorkingDir, Record);
1495 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1497 // Header search options.
1498 Record.clear();
1499 const HeaderSearchOptions &HSOpts =
1500 PP.getHeaderSearchInfo().getHeaderSearchOpts();
1502 AddString(HSOpts.Sysroot, Record);
1503 AddString(HSOpts.ResourceDir, Record);
1504 AddString(HSOpts.ModuleCachePath, Record);
1505 AddString(HSOpts.ModuleUserBuildPath, Record);
1506 Record.push_back(HSOpts.DisableModuleHash);
1507 Record.push_back(HSOpts.ImplicitModuleMaps);
1508 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1509 Record.push_back(HSOpts.EnablePrebuiltImplicitModules);
1510 Record.push_back(HSOpts.UseBuiltinIncludes);
1511 Record.push_back(HSOpts.UseStandardSystemIncludes);
1512 Record.push_back(HSOpts.UseStandardCXXIncludes);
1513 Record.push_back(HSOpts.UseLibcxx);
1514 // Write out the specific module cache path that contains the module files.
1515 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1516 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1518 // Preprocessor options.
1519 Record.clear();
1520 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1522 // If we're building an implicit module with a context hash, the importer is
1523 // guaranteed to have the same macros defined on the command line. Skip
1524 // writing them.
1525 bool SkipMacros = BuildingImplicitModule && !HSOpts.DisableModuleHash;
1526 bool WriteMacros = !SkipMacros;
1527 Record.push_back(WriteMacros);
1528 if (WriteMacros) {
1529 // Macro definitions.
1530 Record.push_back(PPOpts.Macros.size());
1531 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1532 AddString(PPOpts.Macros[I].first, Record);
1533 Record.push_back(PPOpts.Macros[I].second);
1537 // Includes
1538 Record.push_back(PPOpts.Includes.size());
1539 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1540 AddString(PPOpts.Includes[I], Record);
1542 // Macro includes
1543 Record.push_back(PPOpts.MacroIncludes.size());
1544 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1545 AddString(PPOpts.MacroIncludes[I], Record);
1547 Record.push_back(PPOpts.UsePredefines);
1548 // Detailed record is important since it is used for the module cache hash.
1549 Record.push_back(PPOpts.DetailedRecord);
1550 AddString(PPOpts.ImplicitPCHInclude, Record);
1551 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1552 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1554 // Leave the options block.
1555 Stream.ExitBlock();
1557 // Original file name and file ID
1558 SourceManager &SM = Context.getSourceManager();
1559 if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID())) {
1560 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1561 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1562 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1563 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1564 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1566 Record.clear();
1567 Record.push_back(ORIGINAL_FILE);
1568 AddFileID(SM.getMainFileID(), Record);
1569 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1572 Record.clear();
1573 AddFileID(SM.getMainFileID(), Record);
1574 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1576 WriteInputFiles(Context.SourceMgr,
1577 PP.getHeaderSearchInfo().getHeaderSearchOpts());
1578 Stream.ExitBlock();
1581 namespace {
1583 /// An input file.
1584 struct InputFileEntry {
1585 FileEntryRef File;
1586 bool IsSystemFile;
1587 bool IsTransient;
1588 bool BufferOverridden;
1589 bool IsTopLevel;
1590 bool IsModuleMap;
1591 uint32_t ContentHash[2];
1593 InputFileEntry(FileEntryRef File) : File(File) {}
1596 } // namespace
1598 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1599 HeaderSearchOptions &HSOpts) {
1600 using namespace llvm;
1602 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1604 // Create input-file abbreviation.
1605 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1606 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1607 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1608 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1609 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1610 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1611 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1612 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1613 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1614 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1615 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
1616 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1618 // Create input file hash abbreviation.
1619 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1620 IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1621 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1622 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1623 unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1625 uint64_t InputFilesOffsetBase = Stream.GetCurrentBitNo();
1627 // Get all ContentCache objects for files.
1628 std::vector<InputFileEntry> UserFiles;
1629 std::vector<InputFileEntry> SystemFiles;
1630 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1631 // Get this source location entry.
1632 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1633 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1635 // We only care about file entries that were not overridden.
1636 if (!SLoc->isFile())
1637 continue;
1638 const SrcMgr::FileInfo &File = SLoc->getFile();
1639 const SrcMgr::ContentCache *Cache = &File.getContentCache();
1640 if (!Cache->OrigEntry)
1641 continue;
1643 // Do not emit input files that do not affect current module.
1644 if (!IsSLocAffecting[I])
1645 continue;
1647 InputFileEntry Entry(*Cache->OrigEntry);
1648 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1649 Entry.IsTransient = Cache->IsTransient;
1650 Entry.BufferOverridden = Cache->BufferOverridden;
1651 Entry.IsTopLevel = File.getIncludeLoc().isInvalid();
1652 Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
1654 auto ContentHash = hash_code(-1);
1655 if (PP->getHeaderSearchInfo()
1656 .getHeaderSearchOpts()
1657 .ValidateASTInputFilesContent) {
1658 auto MemBuff = Cache->getBufferIfLoaded();
1659 if (MemBuff)
1660 ContentHash = hash_value(MemBuff->getBuffer());
1661 else
1662 PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1663 << Entry.File.getName();
1665 auto CH = llvm::APInt(64, ContentHash);
1666 Entry.ContentHash[0] =
1667 static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1668 Entry.ContentHash[1] =
1669 static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1671 if (Entry.IsSystemFile)
1672 SystemFiles.push_back(Entry);
1673 else
1674 UserFiles.push_back(Entry);
1677 // User files go at the front, system files at the back.
1678 auto SortedFiles = llvm::concat<InputFileEntry>(std::move(UserFiles),
1679 std::move(SystemFiles));
1681 unsigned UserFilesNum = 0;
1682 // Write out all of the input files.
1683 std::vector<uint64_t> InputFileOffsets;
1684 for (const auto &Entry : SortedFiles) {
1685 uint32_t &InputFileID = InputFileIDs[Entry.File];
1686 if (InputFileID != 0)
1687 continue; // already recorded this file.
1689 // Record this entry's offset.
1690 InputFileOffsets.push_back(Stream.GetCurrentBitNo() - InputFilesOffsetBase);
1692 InputFileID = InputFileOffsets.size();
1694 if (!Entry.IsSystemFile)
1695 ++UserFilesNum;
1697 // Emit size/modification time for this file.
1698 // And whether this file was overridden.
1700 SmallString<128> NameAsRequested = Entry.File.getNameAsRequested();
1701 SmallString<128> Name = Entry.File.getName();
1703 PreparePathForOutput(NameAsRequested);
1704 PreparePathForOutput(Name);
1706 if (Name == NameAsRequested)
1707 Name.clear();
1709 RecordData::value_type Record[] = {
1710 INPUT_FILE,
1711 InputFileOffsets.size(),
1712 (uint64_t)Entry.File.getSize(),
1713 (uint64_t)getTimestampForOutput(Entry.File),
1714 Entry.BufferOverridden,
1715 Entry.IsTransient,
1716 Entry.IsTopLevel,
1717 Entry.IsModuleMap,
1718 NameAsRequested.size()};
1720 Stream.EmitRecordWithBlob(IFAbbrevCode, Record,
1721 (NameAsRequested + Name).str());
1724 // Emit content hash for this file.
1726 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1727 Entry.ContentHash[1]};
1728 Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1732 Stream.ExitBlock();
1734 // Create input file offsets abbreviation.
1735 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1736 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1737 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1738 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1739 // input files
1740 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1741 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1743 // Write input file offsets.
1744 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1745 InputFileOffsets.size(), UserFilesNum};
1746 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1749 //===----------------------------------------------------------------------===//
1750 // Source Manager Serialization
1751 //===----------------------------------------------------------------------===//
1753 /// Create an abbreviation for the SLocEntry that refers to a
1754 /// file.
1755 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1756 using namespace llvm;
1758 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1759 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1764 // FileEntry fields.
1765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1769 return Stream.EmitAbbrev(std::move(Abbrev));
1772 /// Create an abbreviation for the SLocEntry that refers to a
1773 /// buffer.
1774 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1775 using namespace llvm;
1777 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1778 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1784 return Stream.EmitAbbrev(std::move(Abbrev));
1787 /// Create an abbreviation for the SLocEntry that refers to a
1788 /// buffer's blob.
1789 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1790 bool Compressed) {
1791 using namespace llvm;
1793 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1794 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1795 : SM_SLOC_BUFFER_BLOB));
1796 if (Compressed)
1797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1799 return Stream.EmitAbbrev(std::move(Abbrev));
1802 /// Create an abbreviation for the SLocEntry that refers to a macro
1803 /// expansion.
1804 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1805 using namespace llvm;
1807 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1808 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
1812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1815 return Stream.EmitAbbrev(std::move(Abbrev));
1818 /// Emit key length and data length as ULEB-encoded data, and return them as a
1819 /// pair.
1820 static std::pair<unsigned, unsigned>
1821 emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
1822 llvm::encodeULEB128(KeyLen, Out);
1823 llvm::encodeULEB128(DataLen, Out);
1824 return std::make_pair(KeyLen, DataLen);
1827 namespace {
1829 // Trait used for the on-disk hash table of header search information.
1830 class HeaderFileInfoTrait {
1831 ASTWriter &Writer;
1833 // Keep track of the framework names we've used during serialization.
1834 SmallString<128> FrameworkStringData;
1835 llvm::StringMap<unsigned> FrameworkNameOffset;
1837 public:
1838 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1840 struct key_type {
1841 StringRef Filename;
1842 off_t Size;
1843 time_t ModTime;
1845 using key_type_ref = const key_type &;
1847 using UnresolvedModule =
1848 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1850 struct data_type {
1851 const HeaderFileInfo &HFI;
1852 bool AlreadyIncluded;
1853 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1854 UnresolvedModule Unresolved;
1856 using data_type_ref = const data_type &;
1858 using hash_value_type = unsigned;
1859 using offset_type = unsigned;
1861 hash_value_type ComputeHash(key_type_ref key) {
1862 // The hash is based only on size/time of the file, so that the reader can
1863 // match even when symlinking or excess path elements ("foo/../", "../")
1864 // change the form of the name. However, complete path is still the key.
1865 return llvm::hash_combine(key.Size, key.ModTime);
1868 std::pair<unsigned, unsigned>
1869 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1870 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
1871 unsigned DataLen = 1 + 4 + 4;
1872 for (auto ModInfo : Data.KnownHeaders)
1873 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1874 DataLen += 4;
1875 if (Data.Unresolved.getPointer())
1876 DataLen += 4;
1877 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
1880 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1881 using namespace llvm::support;
1883 endian::Writer LE(Out, llvm::endianness::little);
1884 LE.write<uint64_t>(key.Size);
1885 KeyLen -= 8;
1886 LE.write<uint64_t>(key.ModTime);
1887 KeyLen -= 8;
1888 Out.write(key.Filename.data(), KeyLen);
1891 void EmitData(raw_ostream &Out, key_type_ref key,
1892 data_type_ref Data, unsigned DataLen) {
1893 using namespace llvm::support;
1895 endian::Writer LE(Out, llvm::endianness::little);
1896 uint64_t Start = Out.tell(); (void)Start;
1898 unsigned char Flags = (Data.AlreadyIncluded << 6)
1899 | (Data.HFI.isImport << 5)
1900 | (Writer.isWritingStdCXXNamedModules() ? 0 :
1901 Data.HFI.isPragmaOnce << 4)
1902 | (Data.HFI.DirInfo << 1)
1903 | Data.HFI.IndexHeaderMapHeader;
1904 LE.write<uint8_t>(Flags);
1906 if (!Data.HFI.ControllingMacro)
1907 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
1908 else
1909 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1911 unsigned Offset = 0;
1912 if (!Data.HFI.Framework.empty()) {
1913 // If this header refers into a framework, save the framework name.
1914 llvm::StringMap<unsigned>::iterator Pos
1915 = FrameworkNameOffset.find(Data.HFI.Framework);
1916 if (Pos == FrameworkNameOffset.end()) {
1917 Offset = FrameworkStringData.size() + 1;
1918 FrameworkStringData.append(Data.HFI.Framework);
1919 FrameworkStringData.push_back(0);
1921 FrameworkNameOffset[Data.HFI.Framework] = Offset;
1922 } else
1923 Offset = Pos->second;
1925 LE.write<uint32_t>(Offset);
1927 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1928 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1929 uint32_t Value = (ModID << 3) | (unsigned)Role;
1930 assert((Value >> 3) == ModID && "overflow in header module info");
1931 LE.write<uint32_t>(Value);
1935 for (auto ModInfo : Data.KnownHeaders)
1936 EmitModule(ModInfo.getModule(), ModInfo.getRole());
1937 if (Data.Unresolved.getPointer())
1938 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
1940 assert(Out.tell() - Start == DataLen && "Wrong data length");
1943 const char *strings_begin() const { return FrameworkStringData.begin(); }
1944 const char *strings_end() const { return FrameworkStringData.end(); }
1947 } // namespace
1949 /// Write the header search block for the list of files that
1951 /// \param HS The header search structure to save.
1952 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1953 HeaderFileInfoTrait GeneratorTrait(*this);
1954 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1955 SmallVector<const char *, 4> SavedStrings;
1956 unsigned NumHeaderSearchEntries = 0;
1958 // Find all unresolved headers for the current module. We generally will
1959 // have resolved them before we get here, but not necessarily: we might be
1960 // compiling a preprocessed module, where there is no requirement for the
1961 // original files to exist any more.
1962 const HeaderFileInfo Empty; // So we can take a reference.
1963 if (WritingModule) {
1964 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1965 while (!Worklist.empty()) {
1966 Module *M = Worklist.pop_back_val();
1967 // We don't care about headers in unimportable submodules.
1968 if (M->isUnimportable())
1969 continue;
1971 // Map to disk files where possible, to pick up any missing stat
1972 // information. This also means we don't need to check the unresolved
1973 // headers list when emitting resolved headers in the first loop below.
1974 // FIXME: It'd be preferable to avoid doing this if we were given
1975 // sufficient stat information in the module map.
1976 HS.getModuleMap().resolveHeaderDirectives(M, /*File=*/std::nullopt);
1978 // If the file didn't exist, we can still create a module if we were given
1979 // enough information in the module map.
1980 for (const auto &U : M->MissingHeaders) {
1981 // Check that we were given enough information to build a module
1982 // without this file existing on disk.
1983 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1984 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1985 << WritingModule->getFullModuleName() << U.Size.has_value()
1986 << U.FileName;
1987 continue;
1990 // Form the effective relative pathname for the file.
1991 SmallString<128> Filename(M->Directory->getName());
1992 llvm::sys::path::append(Filename, U.FileName);
1993 PreparePathForOutput(Filename);
1995 StringRef FilenameDup = strdup(Filename.c_str());
1996 SavedStrings.push_back(FilenameDup.data());
1998 HeaderFileInfoTrait::key_type Key = {
1999 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0};
2000 HeaderFileInfoTrait::data_type Data = {
2001 Empty, false, {}, {M, ModuleMap::headerKindToRole(U.Kind)}};
2002 // FIXME: Deal with cases where there are multiple unresolved header
2003 // directives in different submodules for the same header.
2004 Generator.insert(Key, Data, GeneratorTrait);
2005 ++NumHeaderSearchEntries;
2007 auto SubmodulesRange = M->submodules();
2008 Worklist.append(SubmodulesRange.begin(), SubmodulesRange.end());
2012 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
2013 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
2015 if (FilesByUID.size() > HS.header_file_size())
2016 FilesByUID.resize(HS.header_file_size());
2018 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2019 OptionalFileEntryRef File = FilesByUID[UID];
2020 if (!File)
2021 continue;
2023 // Get the file info. This will load info from the external source if
2024 // necessary. Skip emitting this file if we have no information on it
2025 // as a header file (in which case HFI will be null) or if it hasn't
2026 // changed since it was loaded. Also skip it if it's for a modular header
2027 // from a different module; in that case, we rely on the module(s)
2028 // containing the header to provide this information.
2029 const HeaderFileInfo *HFI =
2030 HS.getExistingFileInfo(*File, /*WantExternal*/!Chain);
2031 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
2032 continue;
2034 // Massage the file path into an appropriate form.
2035 StringRef Filename = File->getName();
2036 SmallString<128> FilenameTmp(Filename);
2037 if (PreparePathForOutput(FilenameTmp)) {
2038 // If we performed any translation on the file name at all, we need to
2039 // save this string, since the generator will refer to it later.
2040 Filename = StringRef(strdup(FilenameTmp.c_str()));
2041 SavedStrings.push_back(Filename.data());
2044 bool Included = PP->alreadyIncluded(*File);
2046 HeaderFileInfoTrait::key_type Key = {
2047 Filename, File->getSize(), getTimestampForOutput(*File)
2049 HeaderFileInfoTrait::data_type Data = {
2050 *HFI, Included, HS.getModuleMap().findResolvedModulesForHeader(*File), {}
2052 Generator.insert(Key, Data, GeneratorTrait);
2053 ++NumHeaderSearchEntries;
2056 // Create the on-disk hash table in a buffer.
2057 SmallString<4096> TableData;
2058 uint32_t BucketOffset;
2060 using namespace llvm::support;
2062 llvm::raw_svector_ostream Out(TableData);
2063 // Make sure that no bucket is at offset 0
2064 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
2065 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2068 // Create a blob abbreviation
2069 using namespace llvm;
2071 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2072 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2077 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2079 // Write the header search table
2080 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2081 NumHeaderSearchEntries, TableData.size()};
2082 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
2083 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
2085 // Free all of the strings we had to duplicate.
2086 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
2087 free(const_cast<char *>(SavedStrings[I]));
2090 static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2091 unsigned SLocBufferBlobCompressedAbbrv,
2092 unsigned SLocBufferBlobAbbrv) {
2093 using RecordDataType = ASTWriter::RecordData::value_type;
2095 // Compress the buffer if possible. We expect that almost all PCM
2096 // consumers will not want its contents.
2097 SmallVector<uint8_t, 0> CompressedBuffer;
2098 if (llvm::compression::zstd::isAvailable()) {
2099 llvm::compression::zstd::compress(
2100 llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer, 9);
2101 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2102 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2103 llvm::toStringRef(CompressedBuffer));
2104 return;
2106 if (llvm::compression::zlib::isAvailable()) {
2107 llvm::compression::zlib::compress(
2108 llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer);
2109 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2110 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2111 llvm::toStringRef(CompressedBuffer));
2112 return;
2115 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2116 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2119 /// Writes the block containing the serialized form of the
2120 /// source manager.
2122 /// TODO: We should probably use an on-disk hash table (stored in a
2123 /// blob), indexed based on the file name, so that we only create
2124 /// entries for files that we actually need. In the common case (no
2125 /// errors), we probably won't have to create file entries for any of
2126 /// the files in the AST.
2127 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2128 const Preprocessor &PP) {
2129 RecordData Record;
2131 // Enter the source manager block.
2132 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
2133 const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
2135 // Abbreviations for the various kinds of source-location entries.
2136 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2137 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
2138 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2139 unsigned SLocBufferBlobCompressedAbbrv =
2140 CreateSLocBufferBlobAbbrev(Stream, true);
2141 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
2143 // Write out the source location entry table. We skip the first
2144 // entry, which is always the same dummy entry.
2145 std::vector<uint32_t> SLocEntryOffsets;
2146 uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
2147 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2148 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
2149 I != N; ++I) {
2150 // Get this source location entry.
2151 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
2152 FileID FID = FileID::get(I);
2153 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
2155 // Record the offset of this source-location entry.
2156 uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
2157 assert((Offset >> 32) == 0 && "SLocEntry offset too large");
2159 // Figure out which record code to use.
2160 unsigned Code;
2161 if (SLoc->isFile()) {
2162 const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
2163 if (Cache->OrigEntry) {
2164 Code = SM_SLOC_FILE_ENTRY;
2165 } else
2166 Code = SM_SLOC_BUFFER_ENTRY;
2167 } else
2168 Code = SM_SLOC_EXPANSION_ENTRY;
2169 Record.clear();
2170 Record.push_back(Code);
2172 if (SLoc->isFile()) {
2173 const SrcMgr::FileInfo &File = SLoc->getFile();
2174 const SrcMgr::ContentCache *Content = &File.getContentCache();
2175 // Do not emit files that were not listed as inputs.
2176 if (!IsSLocAffecting[I])
2177 continue;
2178 SLocEntryOffsets.push_back(Offset);
2179 // Starting offset of this entry within this module, so skip the dummy.
2180 Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2181 AddSourceLocation(File.getIncludeLoc(), Record);
2182 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2183 Record.push_back(File.hasLineDirectives());
2185 bool EmitBlob = false;
2186 if (Content->OrigEntry) {
2187 assert(Content->OrigEntry == Content->ContentsEntry &&
2188 "Writing to AST an overridden file is not supported");
2190 // The source location entry is a file. Emit input file ID.
2191 assert(InputFileIDs[*Content->OrigEntry] != 0 && "Missed file entry");
2192 Record.push_back(InputFileIDs[*Content->OrigEntry]);
2194 Record.push_back(getAdjustedNumCreatedFIDs(FID));
2196 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
2197 if (FDI != FileDeclIDs.end()) {
2198 Record.push_back(FDI->second->FirstDeclIndex);
2199 Record.push_back(FDI->second->DeclIDs.size());
2200 } else {
2201 Record.push_back(0);
2202 Record.push_back(0);
2205 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2207 if (Content->BufferOverridden || Content->IsTransient)
2208 EmitBlob = true;
2209 } else {
2210 // The source location entry is a buffer. The blob associated
2211 // with this entry contains the contents of the buffer.
2213 // We add one to the size so that we capture the trailing NULL
2214 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2215 // the reader side).
2216 std::optional<llvm::MemoryBufferRef> Buffer =
2217 Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2218 StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2219 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
2220 StringRef(Name.data(), Name.size() + 1));
2221 EmitBlob = true;
2224 if (EmitBlob) {
2225 // Include the implicit terminating null character in the on-disk buffer
2226 // if we're writing it uncompressed.
2227 std::optional<llvm::MemoryBufferRef> Buffer =
2228 Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2229 if (!Buffer)
2230 Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2231 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2232 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2233 SLocBufferBlobAbbrv);
2235 } else {
2236 // The source location entry is a macro expansion.
2237 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2238 SLocEntryOffsets.push_back(Offset);
2239 // Starting offset of this entry within this module, so skip the dummy.
2240 Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2241 LocSeq::State Seq;
2242 AddSourceLocation(Expansion.getSpellingLoc(), Record, Seq);
2243 AddSourceLocation(Expansion.getExpansionLocStart(), Record, Seq);
2244 AddSourceLocation(Expansion.isMacroArgExpansion()
2245 ? SourceLocation()
2246 : Expansion.getExpansionLocEnd(),
2247 Record, Seq);
2248 Record.push_back(Expansion.isExpansionTokenRange());
2250 // Compute the token length for this macro expansion.
2251 SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
2252 if (I + 1 != N)
2253 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2254 Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
2255 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
2259 Stream.ExitBlock();
2261 if (SLocEntryOffsets.empty())
2262 return;
2264 // Write the source-location offsets table into the AST block. This
2265 // table is used for lazily loading source-location information.
2266 using namespace llvm;
2268 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2269 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2274 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2276 RecordData::value_type Record[] = {
2277 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2278 getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2279 SLocEntryOffsetsBase - SourceManagerBlockOffset};
2280 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2281 bytes(SLocEntryOffsets));
2284 // Write the line table. It depends on remapping working, so it must come
2285 // after the source location offsets.
2286 if (SourceMgr.hasLineTable()) {
2287 LineTableInfo &LineTable = SourceMgr.getLineTable();
2289 Record.clear();
2291 // Emit the needed file names.
2292 llvm::DenseMap<int, int> FilenameMap;
2293 FilenameMap[-1] = -1; // For unspecified filenames.
2294 for (const auto &L : LineTable) {
2295 if (L.first.ID < 0)
2296 continue;
2297 for (auto &LE : L.second) {
2298 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2299 FilenameMap.size() - 1)).second)
2300 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2303 Record.push_back(0);
2305 // Emit the line entries
2306 for (const auto &L : LineTable) {
2307 // Only emit entries for local files.
2308 if (L.first.ID < 0)
2309 continue;
2311 AddFileID(L.first, Record);
2313 // Emit the line entries
2314 Record.push_back(L.second.size());
2315 for (const auto &LE : L.second) {
2316 Record.push_back(LE.FileOffset);
2317 Record.push_back(LE.LineNo);
2318 Record.push_back(FilenameMap[LE.FilenameID]);
2319 Record.push_back((unsigned)LE.FileKind);
2320 Record.push_back(LE.IncludeOffset);
2324 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2328 //===----------------------------------------------------------------------===//
2329 // Preprocessor Serialization
2330 //===----------------------------------------------------------------------===//
2332 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2333 const Preprocessor &PP) {
2334 if (MacroInfo *MI = MD->getMacroInfo())
2335 if (MI->isBuiltinMacro())
2336 return true;
2338 if (IsModule) {
2339 SourceLocation Loc = MD->getLocation();
2340 if (Loc.isInvalid())
2341 return true;
2342 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2343 return true;
2346 return false;
2349 /// Writes the block containing the serialized form of the
2350 /// preprocessor.
2351 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2352 uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2354 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2355 if (PPRec)
2356 WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
2358 RecordData Record;
2359 RecordData ModuleMacroRecord;
2361 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2362 if (PP.getCounterValue() != 0) {
2363 RecordData::value_type Record[] = {PP.getCounterValue()};
2364 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2367 // If we have a recorded #pragma assume_nonnull, remember it so it can be
2368 // replayed when the preamble terminates into the main file.
2369 SourceLocation AssumeNonNullLoc =
2370 PP.getPreambleRecordedPragmaAssumeNonNullLoc();
2371 if (AssumeNonNullLoc.isValid()) {
2372 assert(PP.isRecordingPreamble());
2373 AddSourceLocation(AssumeNonNullLoc, Record);
2374 Stream.EmitRecord(PP_ASSUME_NONNULL_LOC, Record);
2375 Record.clear();
2378 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2379 assert(!IsModule);
2380 auto SkipInfo = PP.getPreambleSkipInfo();
2381 if (SkipInfo) {
2382 Record.push_back(true);
2383 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2384 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2385 Record.push_back(SkipInfo->FoundNonSkipPortion);
2386 Record.push_back(SkipInfo->FoundElse);
2387 AddSourceLocation(SkipInfo->ElseLoc, Record);
2388 } else {
2389 Record.push_back(false);
2391 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2392 AddSourceLocation(Cond.IfLoc, Record);
2393 Record.push_back(Cond.WasSkipping);
2394 Record.push_back(Cond.FoundNonSkip);
2395 Record.push_back(Cond.FoundElse);
2397 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2398 Record.clear();
2401 // Enter the preprocessor block.
2402 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2404 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2405 // FIXME: Include a location for the use, and say which one was used.
2406 if (PP.SawDateOrTime())
2407 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2409 // Loop over all the macro directives that are live at the end of the file,
2410 // emitting each to the PP section.
2412 // Construct the list of identifiers with macro directives that need to be
2413 // serialized.
2414 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2415 // It is meaningless to emit macros for named modules. It only wastes times
2416 // and spaces.
2417 if (!isWritingStdCXXNamedModules())
2418 for (auto &Id : PP.getIdentifierTable())
2419 if (Id.second->hadMacroDefinition() &&
2420 (!Id.second->isFromAST() ||
2421 Id.second->hasChangedSinceDeserialization()))
2422 MacroIdentifiers.push_back(Id.second);
2423 // Sort the set of macro definitions that need to be serialized by the
2424 // name of the macro, to provide a stable ordering.
2425 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2427 // Emit the macro directives as a list and associate the offset with the
2428 // identifier they belong to.
2429 for (const IdentifierInfo *Name : MacroIdentifiers) {
2430 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2431 uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2432 assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2434 // Write out any exported module macros.
2435 bool EmittedModuleMacros = false;
2436 // C+=20 Header Units are compiled module interfaces, but they preserve
2437 // macros that are live (i.e. have a defined value) at the end of the
2438 // compilation. So when writing a header unit, we preserve only the final
2439 // value of each macro (and discard any that are undefined). Header units
2440 // do not have sub-modules (although they might import other header units).
2441 // PCH files, conversely, retain the history of each macro's define/undef
2442 // and of leaf macros in sub modules.
2443 if (IsModule && WritingModule->isHeaderUnit()) {
2444 // This is for the main TU when it is a C++20 header unit.
2445 // We preserve the final state of defined macros, and we do not emit ones
2446 // that are undefined.
2447 if (!MD || shouldIgnoreMacro(MD, IsModule, PP) ||
2448 MD->getKind() == MacroDirective::MD_Undefine)
2449 continue;
2450 AddSourceLocation(MD->getLocation(), Record);
2451 Record.push_back(MD->getKind());
2452 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2453 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2454 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2455 Record.push_back(VisMD->isPublic());
2457 ModuleMacroRecord.push_back(getSubmoduleID(WritingModule));
2458 ModuleMacroRecord.push_back(getMacroRef(MD->getMacroInfo(), Name));
2459 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2460 ModuleMacroRecord.clear();
2461 EmittedModuleMacros = true;
2462 } else {
2463 // Emit the macro directives in reverse source order.
2464 for (; MD; MD = MD->getPrevious()) {
2465 // Once we hit an ignored macro, we're done: the rest of the chain
2466 // will all be ignored macros.
2467 if (shouldIgnoreMacro(MD, IsModule, PP))
2468 break;
2469 AddSourceLocation(MD->getLocation(), Record);
2470 Record.push_back(MD->getKind());
2471 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2472 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2473 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2474 Record.push_back(VisMD->isPublic());
2478 // We write out exported module macros for PCH as well.
2479 auto Leafs = PP.getLeafModuleMacros(Name);
2480 SmallVector<ModuleMacro *, 8> Worklist(Leafs.begin(), Leafs.end());
2481 llvm::DenseMap<ModuleMacro *, unsigned> Visits;
2482 while (!Worklist.empty()) {
2483 auto *Macro = Worklist.pop_back_val();
2485 // Emit a record indicating this submodule exports this macro.
2486 ModuleMacroRecord.push_back(getSubmoduleID(Macro->getOwningModule()));
2487 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2488 for (auto *M : Macro->overrides())
2489 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2491 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2492 ModuleMacroRecord.clear();
2494 // Enqueue overridden macros once we've visited all their ancestors.
2495 for (auto *M : Macro->overrides())
2496 if (++Visits[M] == M->getNumOverridingMacros())
2497 Worklist.push_back(M);
2499 EmittedModuleMacros = true;
2502 if (Record.empty() && !EmittedModuleMacros)
2503 continue;
2505 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2506 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2507 Record.clear();
2510 /// Offsets of each of the macros into the bitstream, indexed by
2511 /// the local macro ID
2513 /// For each identifier that is associated with a macro, this map
2514 /// provides the offset into the bitstream where that macro is
2515 /// defined.
2516 std::vector<uint32_t> MacroOffsets;
2518 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2519 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2520 MacroInfo *MI = MacroInfosToEmit[I].MI;
2521 MacroID ID = MacroInfosToEmit[I].ID;
2523 if (ID < FirstMacroID) {
2524 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2525 continue;
2528 // Record the local offset of this macro.
2529 unsigned Index = ID - FirstMacroID;
2530 if (Index >= MacroOffsets.size())
2531 MacroOffsets.resize(Index + 1);
2533 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2534 assert((Offset >> 32) == 0 && "Macro offset too large");
2535 MacroOffsets[Index] = Offset;
2537 AddIdentifierRef(Name, Record);
2538 AddSourceLocation(MI->getDefinitionLoc(), Record);
2539 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2540 Record.push_back(MI->isUsed());
2541 Record.push_back(MI->isUsedForHeaderGuard());
2542 Record.push_back(MI->getNumTokens());
2543 unsigned Code;
2544 if (MI->isObjectLike()) {
2545 Code = PP_MACRO_OBJECT_LIKE;
2546 } else {
2547 Code = PP_MACRO_FUNCTION_LIKE;
2549 Record.push_back(MI->isC99Varargs());
2550 Record.push_back(MI->isGNUVarargs());
2551 Record.push_back(MI->hasCommaPasting());
2552 Record.push_back(MI->getNumParams());
2553 for (const IdentifierInfo *Param : MI->params())
2554 AddIdentifierRef(Param, Record);
2557 // If we have a detailed preprocessing record, record the macro definition
2558 // ID that corresponds to this macro.
2559 if (PPRec)
2560 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2562 Stream.EmitRecord(Code, Record);
2563 Record.clear();
2565 // Emit the tokens array.
2566 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2567 // Note that we know that the preprocessor does not have any annotation
2568 // tokens in it because they are created by the parser, and thus can't
2569 // be in a macro definition.
2570 const Token &Tok = MI->getReplacementToken(TokNo);
2571 AddToken(Tok, Record);
2572 Stream.EmitRecord(PP_TOKEN, Record);
2573 Record.clear();
2575 ++NumMacros;
2578 Stream.ExitBlock();
2580 // Write the offsets table for macro IDs.
2581 using namespace llvm;
2583 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2584 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2590 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2592 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2593 FirstMacroID - NUM_PREDEF_MACRO_IDS,
2594 MacroOffsetsBase - ASTBlockStartOffset};
2595 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2599 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2600 uint64_t MacroOffsetsBase) {
2601 if (PPRec.local_begin() == PPRec.local_end())
2602 return;
2604 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2606 // Enter the preprocessor block.
2607 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2609 // If the preprocessor has a preprocessing record, emit it.
2610 unsigned NumPreprocessingRecords = 0;
2611 using namespace llvm;
2613 // Set up the abbreviation for
2614 unsigned InclusionAbbrev = 0;
2616 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2617 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2623 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2626 unsigned FirstPreprocessorEntityID
2627 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2628 + NUM_PREDEF_PP_ENTITY_IDS;
2629 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2630 RecordData Record;
2631 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2632 EEnd = PPRec.local_end();
2633 E != EEnd;
2634 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2635 Record.clear();
2637 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2638 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2639 PreprocessedEntityOffsets.push_back(
2640 PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
2642 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2643 // Record this macro definition's ID.
2644 MacroDefinitions[MD] = NextPreprocessorEntityID;
2646 AddIdentifierRef(MD->getName(), Record);
2647 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2648 continue;
2651 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2652 Record.push_back(ME->isBuiltinMacro());
2653 if (ME->isBuiltinMacro())
2654 AddIdentifierRef(ME->getName(), Record);
2655 else
2656 Record.push_back(MacroDefinitions[ME->getDefinition()]);
2657 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2658 continue;
2661 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2662 Record.push_back(PPD_INCLUSION_DIRECTIVE);
2663 Record.push_back(ID->getFileName().size());
2664 Record.push_back(ID->wasInQuotes());
2665 Record.push_back(static_cast<unsigned>(ID->getKind()));
2666 Record.push_back(ID->importedModule());
2667 SmallString<64> Buffer;
2668 Buffer += ID->getFileName();
2669 // Check that the FileEntry is not null because it was not resolved and
2670 // we create a PCH even with compiler errors.
2671 if (ID->getFile())
2672 Buffer += ID->getFile()->getName();
2673 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2674 continue;
2677 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2679 Stream.ExitBlock();
2681 // Write the offsets table for the preprocessing record.
2682 if (NumPreprocessingRecords > 0) {
2683 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2685 // Write the offsets table for identifier IDs.
2686 using namespace llvm;
2688 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2689 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2692 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2694 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2695 FirstPreprocessorEntityID -
2696 NUM_PREDEF_PP_ENTITY_IDS};
2697 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2698 bytes(PreprocessedEntityOffsets));
2701 // Write the skipped region table for the preprocessing record.
2702 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2703 if (SkippedRanges.size() > 0) {
2704 std::vector<PPSkippedRange> SerializedSkippedRanges;
2705 SerializedSkippedRanges.reserve(SkippedRanges.size());
2706 for (auto const& Range : SkippedRanges)
2707 SerializedSkippedRanges.emplace_back(Range);
2709 using namespace llvm;
2710 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2711 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2713 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2715 Record.clear();
2716 Record.push_back(PPD_SKIPPED_RANGES);
2717 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2718 bytes(SerializedSkippedRanges));
2722 unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module *Mod) {
2723 if (!Mod)
2724 return 0;
2726 auto Known = SubmoduleIDs.find(Mod);
2727 if (Known != SubmoduleIDs.end())
2728 return Known->second;
2730 auto *Top = Mod->getTopLevelModule();
2731 if (Top != WritingModule &&
2732 (getLangOpts().CompilingPCH ||
2733 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
2734 return 0;
2736 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2739 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2740 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2741 // FIXME: This can easily happen, if we have a reference to a submodule that
2742 // did not result in us loading a module file for that submodule. For
2743 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2744 // assert((ID || !Mod) &&
2745 // "asked for module ID for non-local, non-imported module");
2746 return ID;
2749 /// Compute the number of modules within the given tree (including the
2750 /// given module).
2751 static unsigned getNumberOfModules(Module *Mod) {
2752 unsigned ChildModules = 0;
2753 for (auto *Submodule : Mod->submodules())
2754 ChildModules += getNumberOfModules(Submodule);
2756 return ChildModules + 1;
2759 void ASTWriter::WriteSubmodules(Module *WritingModule) {
2760 // Enter the submodule description block.
2761 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2763 // Write the abbreviations needed for the submodules block.
2764 using namespace llvm;
2766 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2767 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Kind
2771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
2772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NamedModuleHasN...
2782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2783 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2785 Abbrev = std::make_shared<BitCodeAbbrev>();
2786 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2788 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2790 Abbrev = std::make_shared<BitCodeAbbrev>();
2791 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2793 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2795 Abbrev = std::make_shared<BitCodeAbbrev>();
2796 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2798 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2800 Abbrev = std::make_shared<BitCodeAbbrev>();
2801 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2802 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2803 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2805 Abbrev = std::make_shared<BitCodeAbbrev>();
2806 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2809 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2811 Abbrev = std::make_shared<BitCodeAbbrev>();
2812 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2814 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2816 Abbrev = std::make_shared<BitCodeAbbrev>();
2817 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2818 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2819 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2821 Abbrev = std::make_shared<BitCodeAbbrev>();
2822 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2823 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2824 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2826 Abbrev = std::make_shared<BitCodeAbbrev>();
2827 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2829 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2831 Abbrev = std::make_shared<BitCodeAbbrev>();
2832 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2834 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2835 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2837 Abbrev = std::make_shared<BitCodeAbbrev>();
2838 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2840 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2842 Abbrev = std::make_shared<BitCodeAbbrev>();
2843 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2846 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2848 Abbrev = std::make_shared<BitCodeAbbrev>();
2849 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2851 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2853 // Write the submodule metadata block.
2854 RecordData::value_type Record[] = {
2855 getNumberOfModules(WritingModule),
2856 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2857 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2859 // Write all of the submodules.
2860 std::queue<Module *> Q;
2861 Q.push(WritingModule);
2862 while (!Q.empty()) {
2863 Module *Mod = Q.front();
2864 Q.pop();
2865 unsigned ID = getSubmoduleID(Mod);
2867 uint64_t ParentID = 0;
2868 if (Mod->Parent) {
2869 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2870 ParentID = SubmoduleIDs[Mod->Parent];
2873 uint64_t DefinitionLoc =
2874 SourceLocationEncoding::encode(getAdjustedLocation(Mod->DefinitionLoc));
2876 // Emit the definition of the block.
2878 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2880 ParentID,
2881 (RecordData::value_type)Mod->Kind,
2882 DefinitionLoc,
2883 Mod->IsFramework,
2884 Mod->IsExplicit,
2885 Mod->IsSystem,
2886 Mod->IsExternC,
2887 Mod->InferSubmodules,
2888 Mod->InferExplicitSubmodules,
2889 Mod->InferExportWildcard,
2890 Mod->ConfigMacrosExhaustive,
2891 Mod->ModuleMapIsPrivate,
2892 Mod->NamedModuleHasInit};
2893 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2896 // Emit the requirements.
2897 for (const auto &R : Mod->Requirements) {
2898 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2899 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2902 // Emit the umbrella header, if there is one.
2903 if (std::optional<Module::Header> UmbrellaHeader =
2904 Mod->getUmbrellaHeaderAsWritten()) {
2905 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2906 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2907 UmbrellaHeader->NameAsWritten);
2908 } else if (std::optional<Module::DirectoryName> UmbrellaDir =
2909 Mod->getUmbrellaDirAsWritten()) {
2910 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2911 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2912 UmbrellaDir->NameAsWritten);
2915 // Emit the headers.
2916 struct {
2917 unsigned RecordKind;
2918 unsigned Abbrev;
2919 Module::HeaderKind HeaderKind;
2920 } HeaderLists[] = {
2921 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2922 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2923 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2924 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2925 Module::HK_PrivateTextual},
2926 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2928 for (auto &HL : HeaderLists) {
2929 RecordData::value_type Record[] = {HL.RecordKind};
2930 for (auto &H : Mod->Headers[HL.HeaderKind])
2931 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2934 // Emit the top headers.
2936 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2937 for (FileEntryRef H : Mod->getTopHeaders(PP->getFileManager())) {
2938 SmallString<128> HeaderName(H.getName());
2939 PreparePathForOutput(HeaderName);
2940 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
2944 // Emit the imports.
2945 if (!Mod->Imports.empty()) {
2946 RecordData Record;
2947 for (auto *I : Mod->Imports)
2948 Record.push_back(getSubmoduleID(I));
2949 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2952 // Emit the modules affecting compilation that were not imported.
2953 if (!Mod->AffectingClangModules.empty()) {
2954 RecordData Record;
2955 for (auto *I : Mod->AffectingClangModules)
2956 Record.push_back(getSubmoduleID(I));
2957 Stream.EmitRecord(SUBMODULE_AFFECTING_MODULES, Record);
2960 // Emit the exports.
2961 if (!Mod->Exports.empty()) {
2962 RecordData Record;
2963 for (const auto &E : Mod->Exports) {
2964 // FIXME: This may fail; we don't require that all exported modules
2965 // are local or imported.
2966 Record.push_back(getSubmoduleID(E.getPointer()));
2967 Record.push_back(E.getInt());
2969 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2972 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2973 // Might be unnecessary as use declarations are only used to build the
2974 // module itself.
2976 // TODO: Consider serializing undeclared uses of modules.
2978 // Emit the link libraries.
2979 for (const auto &LL : Mod->LinkLibraries) {
2980 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2981 LL.IsFramework};
2982 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2985 // Emit the conflicts.
2986 for (const auto &C : Mod->Conflicts) {
2987 // FIXME: This may fail; we don't require that all conflicting modules
2988 // are local or imported.
2989 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2990 getSubmoduleID(C.Other)};
2991 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2994 // Emit the configuration macros.
2995 for (const auto &CM : Mod->ConfigMacros) {
2996 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2997 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
3000 // Emit the initializers, if any.
3001 RecordData Inits;
3002 for (Decl *D : Context->getModuleInitializers(Mod))
3003 Inits.push_back(GetDeclRef(D));
3004 if (!Inits.empty())
3005 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3007 // Emit the name of the re-exported module, if any.
3008 if (!Mod->ExportAsModule.empty()) {
3009 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3010 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3013 // Queue up the submodules of this module.
3014 for (auto *M : Mod->submodules())
3015 Q.push(M);
3018 Stream.ExitBlock();
3020 assert((NextSubmoduleID - FirstSubmoduleID ==
3021 getNumberOfModules(WritingModule)) &&
3022 "Wrong # of submodules; found a reference to a non-local, "
3023 "non-imported submodule?");
3026 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3027 bool isModule) {
3028 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3029 DiagStateIDMap;
3030 unsigned CurrID = 0;
3031 RecordData Record;
3033 auto EncodeDiagStateFlags =
3034 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3035 unsigned Result = (unsigned)DS->ExtBehavior;
3036 for (unsigned Val :
3037 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3038 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3039 (unsigned)DS->SuppressSystemWarnings})
3040 Result = (Result << 1) | Val;
3041 return Result;
3044 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3045 Record.push_back(Flags);
3047 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3048 bool IncludeNonPragmaStates) {
3049 // Ensure that the diagnostic state wasn't modified since it was created.
3050 // We will not correctly round-trip this information otherwise.
3051 assert(Flags == EncodeDiagStateFlags(State) &&
3052 "diag state flags vary in single AST file");
3054 // If we ever serialize non-pragma mappings outside the initial state, the
3055 // code below will need to consider more than getDefaultMapping.
3056 assert(!IncludeNonPragmaStates ||
3057 State == Diag.DiagStatesByLoc.FirstDiagState);
3059 unsigned &DiagStateID = DiagStateIDMap[State];
3060 Record.push_back(DiagStateID);
3062 if (DiagStateID == 0) {
3063 DiagStateID = ++CurrID;
3064 SmallVector<std::pair<unsigned, DiagnosticMapping>> Mappings;
3066 // Add a placeholder for the number of mappings.
3067 auto SizeIdx = Record.size();
3068 Record.emplace_back();
3069 for (const auto &I : *State) {
3070 // Maybe skip non-pragmas.
3071 if (!I.second.isPragma() && !IncludeNonPragmaStates)
3072 continue;
3073 // Skip default mappings. We have a mapping for every diagnostic ever
3074 // emitted, regardless of whether it was customized.
3075 if (!I.second.isPragma() &&
3076 I.second == DiagnosticIDs::getDefaultMapping(I.first))
3077 continue;
3078 Mappings.push_back(I);
3081 // Sort by diag::kind for deterministic output.
3082 llvm::sort(Mappings, [](const auto &LHS, const auto &RHS) {
3083 return LHS.first < RHS.first;
3086 for (const auto &I : Mappings) {
3087 Record.push_back(I.first);
3088 Record.push_back(I.second.serialize());
3090 // Update the placeholder.
3091 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
3095 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
3097 // Reserve a spot for the number of locations with state transitions.
3098 auto NumLocationsIdx = Record.size();
3099 Record.emplace_back();
3101 // Emit the state transitions.
3102 unsigned NumLocations = 0;
3103 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3104 if (!FileIDAndFile.first.isValid() ||
3105 !FileIDAndFile.second.HasLocalTransitions)
3106 continue;
3107 ++NumLocations;
3109 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3110 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3111 AddSourceLocation(Loc, Record);
3113 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3114 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3115 Record.push_back(getAdjustedOffset(StatePoint.Offset));
3116 AddDiagState(StatePoint.State, false);
3120 // Backpatch the number of locations.
3121 Record[NumLocationsIdx] = NumLocations;
3123 // Emit CurDiagStateLoc. Do it last in order to match source order.
3125 // This also protects against a hypothetical corner case with simulating
3126 // -Werror settings for implicit modules in the ASTReader, where reading
3127 // CurDiagState out of context could change whether warning pragmas are
3128 // treated as errors.
3129 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3130 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3132 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
3135 //===----------------------------------------------------------------------===//
3136 // Type Serialization
3137 //===----------------------------------------------------------------------===//
3139 /// Write the representation of a type to the AST stream.
3140 void ASTWriter::WriteType(QualType T) {
3141 TypeIdx &IdxRef = TypeIdxs[T];
3142 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3143 IdxRef = TypeIdx(NextTypeID++);
3144 TypeIdx Idx = IdxRef;
3146 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
3148 // Emit the type's representation.
3149 uint64_t Offset = ASTTypeWriter(*this).write(T) - DeclTypesBlockStartOffset;
3151 // Record the offset for this type.
3152 unsigned Index = Idx.getIndex() - FirstTypeID;
3153 if (TypeOffsets.size() == Index)
3154 TypeOffsets.emplace_back(Offset);
3155 else if (TypeOffsets.size() < Index) {
3156 TypeOffsets.resize(Index + 1);
3157 TypeOffsets[Index].setBitOffset(Offset);
3158 } else {
3159 llvm_unreachable("Types emitted in wrong order");
3163 //===----------------------------------------------------------------------===//
3164 // Declaration Serialization
3165 //===----------------------------------------------------------------------===//
3167 /// Write the block containing all of the declaration IDs
3168 /// lexically declared within the given DeclContext.
3170 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3171 /// bitstream, or 0 if no block was written.
3172 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3173 DeclContext *DC) {
3174 if (DC->decls_empty())
3175 return 0;
3177 uint64_t Offset = Stream.GetCurrentBitNo();
3178 SmallVector<uint32_t, 128> KindDeclPairs;
3179 for (const auto *D : DC->decls()) {
3180 KindDeclPairs.push_back(D->getKind());
3181 KindDeclPairs.push_back(GetDeclRef(D));
3184 ++NumLexicalDeclContexts;
3185 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3186 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3187 bytes(KindDeclPairs));
3188 return Offset;
3191 void ASTWriter::WriteTypeDeclOffsets() {
3192 using namespace llvm;
3194 // Write the type offsets array
3195 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3196 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
3197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
3199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3200 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3202 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3203 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3204 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3207 // Write the declaration offsets array
3208 Abbrev = std::make_shared<BitCodeAbbrev>();
3209 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
3210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
3212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3213 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3215 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3216 FirstDeclID - NUM_PREDEF_DECL_IDS};
3217 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3221 void ASTWriter::WriteFileDeclIDsMap() {
3222 using namespace llvm;
3224 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
3225 SortedFileDeclIDs.reserve(FileDeclIDs.size());
3226 for (const auto &P : FileDeclIDs)
3227 SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
3228 llvm::sort(SortedFileDeclIDs, llvm::less_first());
3230 // Join the vectors of DeclIDs from all files.
3231 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3232 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3233 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3234 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3235 llvm::stable_sort(Info.DeclIDs);
3236 for (auto &LocDeclEntry : Info.DeclIDs)
3237 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
3240 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3241 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
3242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3244 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
3245 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3246 FileGroupedDeclIDs.size()};
3247 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
3250 void ASTWriter::WriteComments() {
3251 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
3252 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3253 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3254 return;
3256 // Don't write comments to BMI to reduce the size of BMI.
3257 // If language services (e.g., clangd) want such abilities,
3258 // we can offer a special option then.
3259 if (isWritingStdCXXNamedModules())
3260 return;
3262 RecordData Record;
3263 for (const auto &FO : Context->Comments.OrderedComments) {
3264 for (const auto &OC : FO.second) {
3265 const RawComment *I = OC.second;
3266 Record.clear();
3267 AddSourceRange(I->getSourceRange(), Record);
3268 Record.push_back(I->getKind());
3269 Record.push_back(I->isTrailingComment());
3270 Record.push_back(I->isAlmostTrailingComment());
3271 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3276 //===----------------------------------------------------------------------===//
3277 // Global Method Pool and Selector Serialization
3278 //===----------------------------------------------------------------------===//
3280 namespace {
3282 // Trait used for the on-disk hash table used in the method pool.
3283 class ASTMethodPoolTrait {
3284 ASTWriter &Writer;
3286 public:
3287 using key_type = Selector;
3288 using key_type_ref = key_type;
3290 struct data_type {
3291 SelectorID ID;
3292 ObjCMethodList Instance, Factory;
3294 using data_type_ref = const data_type &;
3296 using hash_value_type = unsigned;
3297 using offset_type = unsigned;
3299 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3301 static hash_value_type ComputeHash(Selector Sel) {
3302 return serialization::ComputeHash(Sel);
3305 std::pair<unsigned, unsigned>
3306 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3307 data_type_ref Methods) {
3308 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
3309 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3310 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3311 Method = Method->getNext())
3312 if (ShouldWriteMethodListNode(Method))
3313 DataLen += 4;
3314 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3315 Method = Method->getNext())
3316 if (ShouldWriteMethodListNode(Method))
3317 DataLen += 4;
3318 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3321 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3322 using namespace llvm::support;
3324 endian::Writer LE(Out, llvm::endianness::little);
3325 uint64_t Start = Out.tell();
3326 assert((Start >> 32) == 0 && "Selector key offset too large");
3327 Writer.SetSelectorOffset(Sel, Start);
3328 unsigned N = Sel.getNumArgs();
3329 LE.write<uint16_t>(N);
3330 if (N == 0)
3331 N = 1;
3332 for (unsigned I = 0; I != N; ++I)
3333 LE.write<uint32_t>(
3334 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
3337 void EmitData(raw_ostream& Out, key_type_ref,
3338 data_type_ref Methods, unsigned DataLen) {
3339 using namespace llvm::support;
3341 endian::Writer LE(Out, llvm::endianness::little);
3342 uint64_t Start = Out.tell(); (void)Start;
3343 LE.write<uint32_t>(Methods.ID);
3344 unsigned NumInstanceMethods = 0;
3345 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3346 Method = Method->getNext())
3347 if (ShouldWriteMethodListNode(Method))
3348 ++NumInstanceMethods;
3350 unsigned NumFactoryMethods = 0;
3351 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3352 Method = Method->getNext())
3353 if (ShouldWriteMethodListNode(Method))
3354 ++NumFactoryMethods;
3356 unsigned InstanceBits = Methods.Instance.getBits();
3357 assert(InstanceBits < 4);
3358 unsigned InstanceHasMoreThanOneDeclBit =
3359 Methods.Instance.hasMoreThanOneDecl();
3360 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3361 (InstanceHasMoreThanOneDeclBit << 2) |
3362 InstanceBits;
3363 unsigned FactoryBits = Methods.Factory.getBits();
3364 assert(FactoryBits < 4);
3365 unsigned FactoryHasMoreThanOneDeclBit =
3366 Methods.Factory.hasMoreThanOneDecl();
3367 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3368 (FactoryHasMoreThanOneDeclBit << 2) |
3369 FactoryBits;
3370 LE.write<uint16_t>(FullInstanceBits);
3371 LE.write<uint16_t>(FullFactoryBits);
3372 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3373 Method = Method->getNext())
3374 if (ShouldWriteMethodListNode(Method))
3375 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3376 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3377 Method = Method->getNext())
3378 if (ShouldWriteMethodListNode(Method))
3379 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3381 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3384 private:
3385 static bool ShouldWriteMethodListNode(const ObjCMethodList *Node) {
3386 return (Node->getMethod() && !Node->getMethod()->isFromASTFile());
3390 } // namespace
3392 /// Write ObjC data: selectors and the method pool.
3394 /// The method pool contains both instance and factory methods, stored
3395 /// in an on-disk hash table indexed by the selector. The hash table also
3396 /// contains an empty entry for every other selector known to Sema.
3397 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3398 using namespace llvm;
3400 // Do we have to do anything at all?
3401 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
3402 return;
3403 unsigned NumTableEntries = 0;
3404 // Create and write out the blob that contains selectors and the method pool.
3406 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3407 ASTMethodPoolTrait Trait(*this);
3409 // Create the on-disk hash table representation. We walk through every
3410 // selector we've seen and look it up in the method pool.
3411 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3412 for (auto &SelectorAndID : SelectorIDs) {
3413 Selector S = SelectorAndID.first;
3414 SelectorID ID = SelectorAndID.second;
3415 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
3416 ASTMethodPoolTrait::data_type Data = {
3418 ObjCMethodList(),
3419 ObjCMethodList()
3421 if (F != SemaRef.MethodPool.end()) {
3422 Data.Instance = F->second.first;
3423 Data.Factory = F->second.second;
3425 // Only write this selector if it's not in an existing AST or something
3426 // changed.
3427 if (Chain && ID < FirstSelectorID) {
3428 // Selector already exists. Did it change?
3429 bool changed = false;
3430 for (ObjCMethodList *M = &Data.Instance; M && M->getMethod();
3431 M = M->getNext()) {
3432 if (!M->getMethod()->isFromASTFile()) {
3433 changed = true;
3434 Data.Instance = *M;
3435 break;
3438 for (ObjCMethodList *M = &Data.Factory; M && M->getMethod();
3439 M = M->getNext()) {
3440 if (!M->getMethod()->isFromASTFile()) {
3441 changed = true;
3442 Data.Factory = *M;
3443 break;
3446 if (!changed)
3447 continue;
3448 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3449 // A new method pool entry.
3450 ++NumTableEntries;
3452 Generator.insert(S, Data, Trait);
3455 // Create the on-disk hash table in a buffer.
3456 SmallString<4096> MethodPool;
3457 uint32_t BucketOffset;
3459 using namespace llvm::support;
3461 ASTMethodPoolTrait Trait(*this);
3462 llvm::raw_svector_ostream Out(MethodPool);
3463 // Make sure that no bucket is at offset 0
3464 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3465 BucketOffset = Generator.Emit(Out, Trait);
3468 // Create a blob abbreviation
3469 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3470 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3473 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3474 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3476 // Write the method pool
3478 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3479 NumTableEntries};
3480 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3483 // Create a blob abbreviation for the selector table offsets.
3484 Abbrev = std::make_shared<BitCodeAbbrev>();
3485 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3486 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3487 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3489 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3491 // Write the selector offsets table.
3493 RecordData::value_type Record[] = {
3494 SELECTOR_OFFSETS, SelectorOffsets.size(),
3495 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3496 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3497 bytes(SelectorOffsets));
3502 /// Write the selectors referenced in @selector expression into AST file.
3503 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3504 using namespace llvm;
3506 if (SemaRef.ReferencedSelectors.empty())
3507 return;
3509 RecordData Record;
3510 ASTRecordWriter Writer(*this, Record);
3512 // Note: this writes out all references even for a dependent AST. But it is
3513 // very tricky to fix, and given that @selector shouldn't really appear in
3514 // headers, probably not worth it. It's not a correctness issue.
3515 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3516 Selector Sel = SelectorAndLocation.first;
3517 SourceLocation Loc = SelectorAndLocation.second;
3518 Writer.AddSelectorRef(Sel);
3519 Writer.AddSourceLocation(Loc);
3521 Writer.Emit(REFERENCED_SELECTOR_POOL);
3524 //===----------------------------------------------------------------------===//
3525 // Identifier Table Serialization
3526 //===----------------------------------------------------------------------===//
3528 /// Determine the declaration that should be put into the name lookup table to
3529 /// represent the given declaration in this module. This is usually D itself,
3530 /// but if D was imported and merged into a local declaration, we want the most
3531 /// recent local declaration instead. The chosen declaration will be the most
3532 /// recent declaration in any module that imports this one.
3533 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3534 NamedDecl *D) {
3535 if (!LangOpts.Modules || !D->isFromASTFile())
3536 return D;
3538 if (Decl *Redecl = D->getPreviousDecl()) {
3539 // For Redeclarable decls, a prior declaration might be local.
3540 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3541 // If we find a local decl, we're done.
3542 if (!Redecl->isFromASTFile()) {
3543 // Exception: in very rare cases (for injected-class-names), not all
3544 // redeclarations are in the same semantic context. Skip ones in a
3545 // different context. They don't go in this lookup table at all.
3546 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3547 D->getDeclContext()->getRedeclContext()))
3548 continue;
3549 return cast<NamedDecl>(Redecl);
3552 // If we find a decl from a (chained-)PCH stop since we won't find a
3553 // local one.
3554 if (Redecl->getOwningModuleID() == 0)
3555 break;
3557 } else if (Decl *First = D->getCanonicalDecl()) {
3558 // For Mergeable decls, the first decl might be local.
3559 if (!First->isFromASTFile())
3560 return cast<NamedDecl>(First);
3563 // All declarations are imported. Our most recent declaration will also be
3564 // the most recent one in anyone who imports us.
3565 return D;
3568 namespace {
3570 class ASTIdentifierTableTrait {
3571 ASTWriter &Writer;
3572 Preprocessor &PP;
3573 IdentifierResolver &IdResolver;
3574 bool IsModule;
3575 bool NeedDecls;
3576 ASTWriter::RecordData *InterestingIdentifierOffsets;
3578 /// Determines whether this is an "interesting" identifier that needs a
3579 /// full IdentifierInfo structure written into the hash table. Notably, this
3580 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3581 /// to check that.
3582 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3583 if (MacroOffset || II->isPoisoned() ||
3584 (!IsModule && II->getObjCOrBuiltinID()) ||
3585 II->hasRevertedTokenIDToIdentifier() ||
3586 (NeedDecls && II->getFETokenInfo()))
3587 return true;
3589 return false;
3592 public:
3593 using key_type = IdentifierInfo *;
3594 using key_type_ref = key_type;
3596 using data_type = IdentID;
3597 using data_type_ref = data_type;
3599 using hash_value_type = unsigned;
3600 using offset_type = unsigned;
3602 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3603 IdentifierResolver &IdResolver, bool IsModule,
3604 ASTWriter::RecordData *InterestingIdentifierOffsets)
3605 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3606 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3607 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3609 bool needDecls() const { return NeedDecls; }
3611 static hash_value_type ComputeHash(const IdentifierInfo* II) {
3612 return llvm::djbHash(II->getName());
3615 bool isInterestingIdentifier(const IdentifierInfo *II) {
3616 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3617 return isInterestingIdentifier(II, MacroOffset);
3620 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3621 return isInterestingIdentifier(II, 0);
3624 std::pair<unsigned, unsigned>
3625 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3626 // Record the location of the identifier data. This is used when generating
3627 // the mapping from persistent IDs to strings.
3628 Writer.SetIdentifierOffset(II, Out.tell());
3630 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3632 // Emit the offset of the key/data length information to the interesting
3633 // identifiers table if necessary.
3634 if (InterestingIdentifierOffsets &&
3635 isInterestingIdentifier(II, MacroOffset))
3636 InterestingIdentifierOffsets->push_back(Out.tell());
3638 unsigned KeyLen = II->getLength() + 1;
3639 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3640 if (isInterestingIdentifier(II, MacroOffset)) {
3641 DataLen += 2; // 2 bytes for builtin ID
3642 DataLen += 2; // 2 bytes for flags
3643 if (MacroOffset)
3644 DataLen += 4; // MacroDirectives offset.
3646 if (NeedDecls)
3647 DataLen += std::distance(IdResolver.begin(II), IdResolver.end()) * 4;
3649 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3652 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3653 unsigned KeyLen) {
3654 Out.write(II->getNameStart(), KeyLen);
3657 void EmitData(raw_ostream& Out, IdentifierInfo* II,
3658 IdentID ID, unsigned) {
3659 using namespace llvm::support;
3661 endian::Writer LE(Out, llvm::endianness::little);
3663 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3664 if (!isInterestingIdentifier(II, MacroOffset)) {
3665 LE.write<uint32_t>(ID << 1);
3666 return;
3669 LE.write<uint32_t>((ID << 1) | 0x01);
3670 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3671 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3672 LE.write<uint16_t>(Bits);
3673 Bits = 0;
3674 bool HadMacroDefinition = MacroOffset != 0;
3675 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3676 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3677 Bits = (Bits << 1) | unsigned(II->isPoisoned());
3678 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3679 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3680 LE.write<uint16_t>(Bits);
3682 if (HadMacroDefinition)
3683 LE.write<uint32_t>(MacroOffset);
3685 if (NeedDecls) {
3686 // Emit the declaration IDs in reverse order, because the
3687 // IdentifierResolver provides the declarations as they would be
3688 // visible (e.g., the function "stat" would come before the struct
3689 // "stat"), but the ASTReader adds declarations to the end of the list
3690 // (so we need to see the struct "stat" before the function "stat").
3691 // Only emit declarations that aren't from a chained PCH, though.
3692 SmallVector<NamedDecl *, 16> Decls(IdResolver.decls(II));
3693 for (NamedDecl *D : llvm::reverse(Decls))
3694 LE.write<uint32_t>(
3695 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), D)));
3700 } // namespace
3702 /// Write the identifier table into the AST file.
3704 /// The identifier table consists of a blob containing string data
3705 /// (the actual identifiers themselves) and a separate "offsets" index
3706 /// that maps identifier IDs to locations within the blob.
3707 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3708 IdentifierResolver &IdResolver,
3709 bool IsModule) {
3710 using namespace llvm;
3712 RecordData InterestingIdents;
3714 // Create and write out the blob that contains the identifier
3715 // strings.
3717 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3718 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule,
3719 IsModule ? &InterestingIdents : nullptr);
3721 // Look for any identifiers that were named while processing the
3722 // headers, but are otherwise not needed. We add these to the hash
3723 // table to enable checking of the predefines buffer in the case
3724 // where the user adds new macro definitions when building the AST
3725 // file.
3726 SmallVector<const IdentifierInfo *, 128> IIs;
3727 for (const auto &ID : PP.getIdentifierTable())
3728 if (Trait.isInterestingNonMacroIdentifier(ID.second))
3729 IIs.push_back(ID.second);
3730 // Sort the identifiers lexicographically before getting the references so
3731 // that their order is stable.
3732 llvm::sort(IIs, llvm::deref<std::less<>>());
3733 for (const IdentifierInfo *II : IIs)
3734 getIdentifierRef(II);
3736 // Create the on-disk hash table representation. We only store offsets
3737 // for identifiers that appear here for the first time.
3738 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3739 for (auto IdentIDPair : IdentifierIDs) {
3740 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3741 IdentID ID = IdentIDPair.second;
3742 assert(II && "NULL identifier in identifier table");
3743 // Write out identifiers if either the ID is local or the identifier has
3744 // changed since it was loaded.
3745 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3746 || II->hasChangedSinceDeserialization() ||
3747 (Trait.needDecls() &&
3748 II->hasFETokenInfoChangedSinceDeserialization()))
3749 Generator.insert(II, ID, Trait);
3752 // Create the on-disk hash table in a buffer.
3753 SmallString<4096> IdentifierTable;
3754 uint32_t BucketOffset;
3756 using namespace llvm::support;
3758 llvm::raw_svector_ostream Out(IdentifierTable);
3759 // Make sure that no bucket is at offset 0
3760 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3761 BucketOffset = Generator.Emit(Out, Trait);
3764 // Create a blob abbreviation
3765 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3766 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3769 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3771 // Write the identifier table
3772 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3773 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3776 // Write the offsets table for identifier IDs.
3777 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3778 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3782 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3784 #ifndef NDEBUG
3785 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3786 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3787 #endif
3789 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3790 IdentifierOffsets.size(),
3791 FirstIdentID - NUM_PREDEF_IDENT_IDS};
3792 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3793 bytes(IdentifierOffsets));
3795 // In C++, write the list of interesting identifiers (those that are
3796 // defined as macros, poisoned, or similar unusual things).
3797 if (!InterestingIdents.empty())
3798 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3801 //===----------------------------------------------------------------------===//
3802 // DeclContext's Name Lookup Table Serialization
3803 //===----------------------------------------------------------------------===//
3805 namespace {
3807 // Trait used for the on-disk hash table used in the method pool.
3808 class ASTDeclContextNameLookupTrait {
3809 ASTWriter &Writer;
3810 llvm::SmallVector<DeclID, 64> DeclIDs;
3812 public:
3813 using key_type = DeclarationNameKey;
3814 using key_type_ref = key_type;
3816 /// A start and end index into DeclIDs, representing a sequence of decls.
3817 using data_type = std::pair<unsigned, unsigned>;
3818 using data_type_ref = const data_type &;
3820 using hash_value_type = unsigned;
3821 using offset_type = unsigned;
3823 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3825 template<typename Coll>
3826 data_type getData(const Coll &Decls) {
3827 unsigned Start = DeclIDs.size();
3828 for (NamedDecl *D : Decls) {
3829 DeclIDs.push_back(
3830 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3832 return std::make_pair(Start, DeclIDs.size());
3835 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3836 unsigned Start = DeclIDs.size();
3837 llvm::append_range(DeclIDs, FromReader);
3838 return std::make_pair(Start, DeclIDs.size());
3841 static bool EqualKey(key_type_ref a, key_type_ref b) {
3842 return a == b;
3845 hash_value_type ComputeHash(DeclarationNameKey Name) {
3846 return Name.getHash();
3849 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3850 assert(Writer.hasChain() &&
3851 "have reference to loaded module file but no chain?");
3853 using namespace llvm::support;
3855 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F),
3856 llvm::endianness::little);
3859 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3860 DeclarationNameKey Name,
3861 data_type_ref Lookup) {
3862 unsigned KeyLen = 1;
3863 switch (Name.getKind()) {
3864 case DeclarationName::Identifier:
3865 case DeclarationName::ObjCZeroArgSelector:
3866 case DeclarationName::ObjCOneArgSelector:
3867 case DeclarationName::ObjCMultiArgSelector:
3868 case DeclarationName::CXXLiteralOperatorName:
3869 case DeclarationName::CXXDeductionGuideName:
3870 KeyLen += 4;
3871 break;
3872 case DeclarationName::CXXOperatorName:
3873 KeyLen += 1;
3874 break;
3875 case DeclarationName::CXXConstructorName:
3876 case DeclarationName::CXXDestructorName:
3877 case DeclarationName::CXXConversionFunctionName:
3878 case DeclarationName::CXXUsingDirective:
3879 break;
3882 // 4 bytes for each DeclID.
3883 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3885 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3888 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3889 using namespace llvm::support;
3891 endian::Writer LE(Out, llvm::endianness::little);
3892 LE.write<uint8_t>(Name.getKind());
3893 switch (Name.getKind()) {
3894 case DeclarationName::Identifier:
3895 case DeclarationName::CXXLiteralOperatorName:
3896 case DeclarationName::CXXDeductionGuideName:
3897 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3898 return;
3899 case DeclarationName::ObjCZeroArgSelector:
3900 case DeclarationName::ObjCOneArgSelector:
3901 case DeclarationName::ObjCMultiArgSelector:
3902 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3903 return;
3904 case DeclarationName::CXXOperatorName:
3905 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3906 "Invalid operator?");
3907 LE.write<uint8_t>(Name.getOperatorKind());
3908 return;
3909 case DeclarationName::CXXConstructorName:
3910 case DeclarationName::CXXDestructorName:
3911 case DeclarationName::CXXConversionFunctionName:
3912 case DeclarationName::CXXUsingDirective:
3913 return;
3916 llvm_unreachable("Invalid name kind?");
3919 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3920 unsigned DataLen) {
3921 using namespace llvm::support;
3923 endian::Writer LE(Out, llvm::endianness::little);
3924 uint64_t Start = Out.tell(); (void)Start;
3925 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3926 LE.write<uint32_t>(DeclIDs[I]);
3927 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3931 } // namespace
3933 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3934 DeclContext *DC) {
3935 return Result.hasExternalDecls() &&
3936 DC->hasNeedToReconcileExternalVisibleStorage();
3939 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3940 DeclContext *DC) {
3941 for (auto *D : Result.getLookupResult())
3942 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3943 return false;
3945 return true;
3948 void
3949 ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3950 llvm::SmallVectorImpl<char> &LookupTable) {
3951 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3952 !ConstDC->hasLazyExternalLexicalLookups() &&
3953 "must call buildLookups first");
3955 // FIXME: We need to build the lookups table, which is logically const.
3956 auto *DC = const_cast<DeclContext*>(ConstDC);
3957 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3959 // Create the on-disk hash table representation.
3960 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3961 ASTDeclContextNameLookupTrait> Generator;
3962 ASTDeclContextNameLookupTrait Trait(*this);
3964 // The first step is to collect the declaration names which we need to
3965 // serialize into the name lookup table, and to collect them in a stable
3966 // order.
3967 SmallVector<DeclarationName, 16> Names;
3969 // We also build up small sets of the constructor and conversion function
3970 // names which are visible.
3971 llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
3973 for (auto &Lookup : *DC->buildLookup()) {
3974 auto &Name = Lookup.first;
3975 auto &Result = Lookup.second;
3977 // If there are no local declarations in our lookup result, we
3978 // don't need to write an entry for the name at all. If we can't
3979 // write out a lookup set without performing more deserialization,
3980 // just skip this entry.
3981 if (isLookupResultExternal(Result, DC) &&
3982 isLookupResultEntirelyExternal(Result, DC))
3983 continue;
3985 // We also skip empty results. If any of the results could be external and
3986 // the currently available results are empty, then all of the results are
3987 // external and we skip it above. So the only way we get here with an empty
3988 // results is when no results could have been external *and* we have
3989 // external results.
3991 // FIXME: While we might want to start emitting on-disk entries for negative
3992 // lookups into a decl context as an optimization, today we *have* to skip
3993 // them because there are names with empty lookup results in decl contexts
3994 // which we can't emit in any stable ordering: we lookup constructors and
3995 // conversion functions in the enclosing namespace scope creating empty
3996 // results for them. This in almost certainly a bug in Clang's name lookup,
3997 // but that is likely to be hard or impossible to fix and so we tolerate it
3998 // here by omitting lookups with empty results.
3999 if (Lookup.second.getLookupResult().empty())
4000 continue;
4002 switch (Lookup.first.getNameKind()) {
4003 default:
4004 Names.push_back(Lookup.first);
4005 break;
4007 case DeclarationName::CXXConstructorName:
4008 assert(isa<CXXRecordDecl>(DC) &&
4009 "Cannot have a constructor name outside of a class!");
4010 ConstructorNameSet.insert(Name);
4011 break;
4013 case DeclarationName::CXXConversionFunctionName:
4014 assert(isa<CXXRecordDecl>(DC) &&
4015 "Cannot have a conversion function name outside of a class!");
4016 ConversionNameSet.insert(Name);
4017 break;
4021 // Sort the names into a stable order.
4022 llvm::sort(Names);
4024 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
4025 // We need to establish an ordering of constructor and conversion function
4026 // names, and they don't have an intrinsic ordering.
4028 // First we try the easy case by forming the current context's constructor
4029 // name and adding that name first. This is a very useful optimization to
4030 // avoid walking the lexical declarations in many cases, and it also
4031 // handles the only case where a constructor name can come from some other
4032 // lexical context -- when that name is an implicit constructor merged from
4033 // another declaration in the redecl chain. Any non-implicit constructor or
4034 // conversion function which doesn't occur in all the lexical contexts
4035 // would be an ODR violation.
4036 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4037 Context->getCanonicalType(Context->getRecordType(D)));
4038 if (ConstructorNameSet.erase(ImplicitCtorName))
4039 Names.push_back(ImplicitCtorName);
4041 // If we still have constructors or conversion functions, we walk all the
4042 // names in the decl and add the constructors and conversion functions
4043 // which are visible in the order they lexically occur within the context.
4044 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4045 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4046 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4047 auto Name = ChildND->getDeclName();
4048 switch (Name.getNameKind()) {
4049 default:
4050 continue;
4052 case DeclarationName::CXXConstructorName:
4053 if (ConstructorNameSet.erase(Name))
4054 Names.push_back(Name);
4055 break;
4057 case DeclarationName::CXXConversionFunctionName:
4058 if (ConversionNameSet.erase(Name))
4059 Names.push_back(Name);
4060 break;
4063 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4064 break;
4067 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4068 "constructors by walking all the "
4069 "lexical members of the context.");
4070 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4071 "conversion functions by walking all "
4072 "the lexical members of the context.");
4075 // Next we need to do a lookup with each name into this decl context to fully
4076 // populate any results from external sources. We don't actually use the
4077 // results of these lookups because we only want to use the results after all
4078 // results have been loaded and the pointers into them will be stable.
4079 for (auto &Name : Names)
4080 DC->lookup(Name);
4082 // Now we need to insert the results for each name into the hash table. For
4083 // constructor names and conversion function names, we actually need to merge
4084 // all of the results for them into one list of results each and insert
4085 // those.
4086 SmallVector<NamedDecl *, 8> ConstructorDecls;
4087 SmallVector<NamedDecl *, 8> ConversionDecls;
4089 // Now loop over the names, either inserting them or appending for the two
4090 // special cases.
4091 for (auto &Name : Names) {
4092 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4094 switch (Name.getNameKind()) {
4095 default:
4096 Generator.insert(Name, Trait.getData(Result), Trait);
4097 break;
4099 case DeclarationName::CXXConstructorName:
4100 ConstructorDecls.append(Result.begin(), Result.end());
4101 break;
4103 case DeclarationName::CXXConversionFunctionName:
4104 ConversionDecls.append(Result.begin(), Result.end());
4105 break;
4109 // Handle our two special cases if we ended up having any. We arbitrarily use
4110 // the first declaration's name here because the name itself isn't part of
4111 // the key, only the kind of name is used.
4112 if (!ConstructorDecls.empty())
4113 Generator.insert(ConstructorDecls.front()->getDeclName(),
4114 Trait.getData(ConstructorDecls), Trait);
4115 if (!ConversionDecls.empty())
4116 Generator.insert(ConversionDecls.front()->getDeclName(),
4117 Trait.getData(ConversionDecls), Trait);
4119 // Create the on-disk hash table. Also emit the existing imported and
4120 // merged table if there is one.
4121 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4122 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
4125 /// Write the block containing all of the declaration IDs
4126 /// visible from the given DeclContext.
4128 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4129 /// bitstream, or 0 if no block was written.
4130 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4131 DeclContext *DC) {
4132 // If we imported a key declaration of this namespace, write the visible
4133 // lookup results as an update record for it rather than including them
4134 // on this declaration. We will only look at key declarations on reload.
4135 if (isa<NamespaceDecl>(DC) && Chain &&
4136 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4137 // Only do this once, for the first local declaration of the namespace.
4138 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
4139 Prev = Prev->getPreviousDecl())
4140 if (!Prev->isFromASTFile())
4141 return 0;
4143 // Note that we need to emit an update record for the primary context.
4144 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4146 // Make sure all visible decls are written. They will be recorded later. We
4147 // do this using a side data structure so we can sort the names into
4148 // a deterministic order.
4149 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4150 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4151 LookupResults;
4152 if (Map) {
4153 LookupResults.reserve(Map->size());
4154 for (auto &Entry : *Map)
4155 LookupResults.push_back(
4156 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4159 llvm::sort(LookupResults, llvm::less_first());
4160 for (auto &NameAndResult : LookupResults) {
4161 DeclarationName Name = NameAndResult.first;
4162 DeclContext::lookup_result Result = NameAndResult.second;
4163 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4164 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4165 // We have to work around a name lookup bug here where negative lookup
4166 // results for these names get cached in namespace lookup tables (these
4167 // names should never be looked up in a namespace).
4168 assert(Result.empty() && "Cannot have a constructor or conversion "
4169 "function name in a namespace!");
4170 continue;
4173 for (NamedDecl *ND : Result)
4174 if (!ND->isFromASTFile())
4175 GetDeclRef(ND);
4178 return 0;
4181 if (DC->getPrimaryContext() != DC)
4182 return 0;
4184 // Skip contexts which don't support name lookup.
4185 if (!DC->isLookupContext())
4186 return 0;
4188 // If not in C++, we perform name lookup for the translation unit via the
4189 // IdentifierInfo chains, don't bother to build a visible-declarations table.
4190 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4191 return 0;
4193 // Serialize the contents of the mapping used for lookup. Note that,
4194 // although we have two very different code paths, the serialized
4195 // representation is the same for both cases: a declaration name,
4196 // followed by a size, followed by references to the visible
4197 // declarations that have that name.
4198 uint64_t Offset = Stream.GetCurrentBitNo();
4199 StoredDeclsMap *Map = DC->buildLookup();
4200 if (!Map || Map->empty())
4201 return 0;
4203 // Create the on-disk hash table in a buffer.
4204 SmallString<4096> LookupTable;
4205 GenerateNameLookupTable(DC, LookupTable);
4207 // Write the lookup table
4208 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4209 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
4210 LookupTable);
4211 ++NumVisibleDeclContexts;
4212 return Offset;
4215 /// Write an UPDATE_VISIBLE block for the given context.
4217 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4218 /// DeclContext in a dependent AST file. As such, they only exist for the TU
4219 /// (in C++), for namespaces, and for classes with forward-declared unscoped
4220 /// enumeration members (in C++11).
4221 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
4222 StoredDeclsMap *Map = DC->getLookupPtr();
4223 if (!Map || Map->empty())
4224 return;
4226 // Create the on-disk hash table in a buffer.
4227 SmallString<4096> LookupTable;
4228 GenerateNameLookupTable(DC, LookupTable);
4230 // If we're updating a namespace, select a key declaration as the key for the
4231 // update record; those are the only ones that will be checked on reload.
4232 if (isa<NamespaceDecl>(DC))
4233 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4235 // Write the lookup table
4236 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
4237 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
4240 /// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4241 void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4242 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4243 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4246 /// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4247 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4248 if (!SemaRef.Context.getLangOpts().OpenCL)
4249 return;
4251 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4252 RecordData Record;
4253 for (const auto &I:Opts.OptMap) {
4254 AddString(I.getKey(), Record);
4255 auto V = I.getValue();
4256 Record.push_back(V.Supported ? 1 : 0);
4257 Record.push_back(V.Enabled ? 1 : 0);
4258 Record.push_back(V.WithPragma ? 1 : 0);
4259 Record.push_back(V.Avail);
4260 Record.push_back(V.Core);
4261 Record.push_back(V.Opt);
4263 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4265 void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4266 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4267 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4268 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4272 void ASTWriter::WriteObjCCategories() {
4273 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4274 RecordData Categories;
4276 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4277 unsigned Size = 0;
4278 unsigned StartIndex = Categories.size();
4280 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4282 // Allocate space for the size.
4283 Categories.push_back(0);
4285 // Add the categories.
4286 for (ObjCInterfaceDecl::known_categories_iterator
4287 Cat = Class->known_categories_begin(),
4288 CatEnd = Class->known_categories_end();
4289 Cat != CatEnd; ++Cat, ++Size) {
4290 assert(getDeclID(*Cat) != 0 && "Bogus category");
4291 AddDeclRef(*Cat, Categories);
4294 // Update the size.
4295 Categories[StartIndex] = Size;
4297 // Record this interface -> category map.
4298 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4299 CategoriesMap.push_back(CatInfo);
4302 // Sort the categories map by the definition ID, since the reader will be
4303 // performing binary searches on this information.
4304 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4306 // Emit the categories map.
4307 using namespace llvm;
4309 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4310 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4313 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
4315 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4316 Stream.EmitRecordWithBlob(AbbrevID, Record,
4317 reinterpret_cast<char *>(CategoriesMap.data()),
4318 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4320 // Emit the category lists.
4321 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4324 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4325 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4327 if (LPTMap.empty())
4328 return;
4330 RecordData Record;
4331 for (auto &LPTMapEntry : LPTMap) {
4332 const FunctionDecl *FD = LPTMapEntry.first;
4333 LateParsedTemplate &LPT = *LPTMapEntry.second;
4334 AddDeclRef(FD, Record);
4335 AddDeclRef(LPT.D, Record);
4336 Record.push_back(LPT.FPO.getAsOpaqueInt());
4337 Record.push_back(LPT.Toks.size());
4339 for (const auto &Tok : LPT.Toks) {
4340 AddToken(Tok, Record);
4343 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4346 /// Write the state of 'pragma clang optimize' at the end of the module.
4347 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4348 RecordData Record;
4349 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4350 AddSourceLocation(PragmaLoc, Record);
4351 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4354 /// Write the state of 'pragma ms_struct' at the end of the module.
4355 void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4356 RecordData Record;
4357 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4358 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4361 /// Write the state of 'pragma pointers_to_members' at the end of the
4362 //module.
4363 void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4364 RecordData Record;
4365 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4366 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4367 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4370 /// Write the state of 'pragma align/pack' at the end of the module.
4371 void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4372 // Don't serialize pragma align/pack state for modules, since it should only
4373 // take effect on a per-submodule basis.
4374 if (WritingModule)
4375 return;
4377 RecordData Record;
4378 AddAlignPackInfo(SemaRef.AlignPackStack.CurrentValue, Record);
4379 AddSourceLocation(SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
4380 Record.push_back(SemaRef.AlignPackStack.Stack.size());
4381 for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
4382 AddAlignPackInfo(StackEntry.Value, Record);
4383 AddSourceLocation(StackEntry.PragmaLocation, Record);
4384 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4385 AddString(StackEntry.StackSlotLabel, Record);
4387 Stream.EmitRecord(ALIGN_PACK_PRAGMA_OPTIONS, Record);
4390 /// Write the state of 'pragma float_control' at the end of the module.
4391 void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4392 // Don't serialize pragma float_control state for modules,
4393 // since it should only take effect on a per-submodule basis.
4394 if (WritingModule)
4395 return;
4397 RecordData Record;
4398 Record.push_back(SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4399 AddSourceLocation(SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4400 Record.push_back(SemaRef.FpPragmaStack.Stack.size());
4401 for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4402 Record.push_back(StackEntry.Value.getAsOpaqueInt());
4403 AddSourceLocation(StackEntry.PragmaLocation, Record);
4404 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4405 AddString(StackEntry.StackSlotLabel, Record);
4407 Stream.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS, Record);
4410 void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4411 ModuleFileExtensionWriter &Writer) {
4412 // Enter the extension block.
4413 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4415 // Emit the metadata record abbreviation.
4416 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4417 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4418 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4419 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4420 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4421 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4422 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4423 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
4425 // Emit the metadata record.
4426 RecordData Record;
4427 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4428 Record.push_back(EXTENSION_METADATA);
4429 Record.push_back(Metadata.MajorVersion);
4430 Record.push_back(Metadata.MinorVersion);
4431 Record.push_back(Metadata.BlockName.size());
4432 Record.push_back(Metadata.UserInfo.size());
4433 SmallString<64> Buffer;
4434 Buffer += Metadata.BlockName;
4435 Buffer += Metadata.UserInfo;
4436 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4438 // Emit the contents of the extension block.
4439 Writer.writeExtensionContents(SemaRef, Stream);
4441 // Exit the extension block.
4442 Stream.ExitBlock();
4445 //===----------------------------------------------------------------------===//
4446 // General Serialization Routines
4447 //===----------------------------------------------------------------------===//
4449 void ASTRecordWriter::AddAttr(const Attr *A) {
4450 auto &Record = *this;
4451 // FIXME: Clang can't handle the serialization/deserialization of
4452 // preferred_name properly now. See
4453 // https://github.com/llvm/llvm-project/issues/56490 for example.
4454 if (!A || (isa<PreferredNameAttr>(A) &&
4455 Writer->isWritingStdCXXNamedModules()))
4456 return Record.push_back(0);
4458 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4460 Record.AddIdentifierRef(A->getAttrName());
4461 Record.AddIdentifierRef(A->getScopeName());
4462 Record.AddSourceRange(A->getRange());
4463 Record.AddSourceLocation(A->getScopeLoc());
4464 Record.push_back(A->getParsedKind());
4465 Record.push_back(A->getSyntax());
4466 Record.push_back(A->getAttributeSpellingListIndexRaw());
4467 Record.push_back(A->isRegularKeywordAttribute());
4469 #include "clang/Serialization/AttrPCHWrite.inc"
4472 /// Emit the list of attributes to the specified record.
4473 void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4474 push_back(Attrs.size());
4475 for (const auto *A : Attrs)
4476 AddAttr(A);
4479 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4480 AddSourceLocation(Tok.getLocation(), Record);
4481 // FIXME: Should translate token kind to a stable encoding.
4482 Record.push_back(Tok.getKind());
4483 // FIXME: Should translate token flags to a stable encoding.
4484 Record.push_back(Tok.getFlags());
4486 if (Tok.isAnnotation()) {
4487 AddSourceLocation(Tok.getAnnotationEndLoc(), Record);
4488 switch (Tok.getKind()) {
4489 case tok::annot_pragma_loop_hint: {
4490 auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
4491 AddToken(Info->PragmaName, Record);
4492 AddToken(Info->Option, Record);
4493 Record.push_back(Info->Toks.size());
4494 for (const auto &T : Info->Toks)
4495 AddToken(T, Record);
4496 break;
4498 case tok::annot_pragma_pack: {
4499 auto *Info =
4500 static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
4501 Record.push_back(static_cast<unsigned>(Info->Action));
4502 AddString(Info->SlotLabel, Record);
4503 AddToken(Info->Alignment, Record);
4504 break;
4506 // Some annotation tokens do not use the PtrData field.
4507 case tok::annot_pragma_openmp:
4508 case tok::annot_pragma_openmp_end:
4509 case tok::annot_pragma_unused:
4510 case tok::annot_pragma_openacc:
4511 case tok::annot_pragma_openacc_end:
4512 break;
4513 default:
4514 llvm_unreachable("missing serialization code for annotation token");
4516 } else {
4517 Record.push_back(Tok.getLength());
4518 // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4519 // is needed.
4520 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4524 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4525 Record.push_back(Str.size());
4526 Record.insert(Record.end(), Str.begin(), Str.end());
4529 bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4530 assert(Context && "should have context when outputting path");
4532 // Leave special file names as they are.
4533 StringRef PathStr(Path.data(), Path.size());
4534 if (PathStr == "<built-in>" || PathStr == "<command line>")
4535 return false;
4537 bool Changed =
4538 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4540 // Remove a prefix to make the path relative, if relevant.
4541 const char *PathBegin = Path.data();
4542 const char *PathPtr =
4543 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4544 if (PathPtr != PathBegin) {
4545 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4546 Changed = true;
4549 return Changed;
4552 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4553 SmallString<128> FilePath(Path);
4554 PreparePathForOutput(FilePath);
4555 AddString(FilePath, Record);
4558 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4559 StringRef Path) {
4560 SmallString<128> FilePath(Path);
4561 PreparePathForOutput(FilePath);
4562 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4565 void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4566 RecordDataImpl &Record) {
4567 Record.push_back(Version.getMajor());
4568 if (std::optional<unsigned> Minor = Version.getMinor())
4569 Record.push_back(*Minor + 1);
4570 else
4571 Record.push_back(0);
4572 if (std::optional<unsigned> Subminor = Version.getSubminor())
4573 Record.push_back(*Subminor + 1);
4574 else
4575 Record.push_back(0);
4578 /// Note that the identifier II occurs at the given offset
4579 /// within the identifier table.
4580 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4581 IdentID ID = IdentifierIDs[II];
4582 // Only store offsets new to this AST file. Other identifier names are looked
4583 // up earlier in the chain and thus don't need an offset.
4584 if (ID >= FirstIdentID)
4585 IdentifierOffsets[ID - FirstIdentID] = Offset;
4588 /// Note that the selector Sel occurs at the given offset
4589 /// within the method pool/selector table.
4590 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4591 unsigned ID = SelectorIDs[Sel];
4592 assert(ID && "Unknown selector");
4593 // Don't record offsets for selectors that are also available in a different
4594 // file.
4595 if (ID < FirstSelectorID)
4596 return;
4597 SelectorOffsets[ID - FirstSelectorID] = Offset;
4600 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4601 SmallVectorImpl<char> &Buffer,
4602 InMemoryModuleCache &ModuleCache,
4603 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4604 bool IncludeTimestamps, bool BuildingImplicitModule)
4605 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4606 IncludeTimestamps(IncludeTimestamps),
4607 BuildingImplicitModule(BuildingImplicitModule) {
4608 for (const auto &Ext : Extensions) {
4609 if (auto Writer = Ext->createExtensionWriter(*this))
4610 ModuleFileExtensionWriters.push_back(std::move(Writer));
4614 ASTWriter::~ASTWriter() = default;
4616 const LangOptions &ASTWriter::getLangOpts() const {
4617 assert(WritingAST && "can't determine lang opts when not writing AST");
4618 return Context->getLangOpts();
4621 time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4622 return IncludeTimestamps ? E->getModificationTime() : 0;
4625 ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
4626 Module *WritingModule, StringRef isysroot,
4627 bool ShouldCacheASTInMemory) {
4628 llvm::TimeTraceScope scope("WriteAST", OutputFile);
4629 WritingAST = true;
4631 ASTHasCompilerErrors =
4632 SemaRef.PP.getDiagnostics().hasUncompilableErrorOccurred();
4634 // Emit the file header.
4635 Stream.Emit((unsigned)'C', 8);
4636 Stream.Emit((unsigned)'P', 8);
4637 Stream.Emit((unsigned)'C', 8);
4638 Stream.Emit((unsigned)'H', 8);
4640 WriteBlockInfoBlock();
4642 Context = &SemaRef.Context;
4643 PP = &SemaRef.PP;
4644 this->WritingModule = WritingModule;
4645 ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
4646 Context = nullptr;
4647 PP = nullptr;
4648 this->WritingModule = nullptr;
4649 this->BaseDirectory.clear();
4651 WritingAST = false;
4652 if (ShouldCacheASTInMemory) {
4653 // Construct MemoryBuffer and update buffer manager.
4654 ModuleCache.addBuiltPCM(OutputFile,
4655 llvm::MemoryBuffer::getMemBufferCopy(
4656 StringRef(Buffer.begin(), Buffer.size())));
4658 return Signature;
4661 template<typename Vector>
4662 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4663 ASTWriter::RecordData &Record) {
4664 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4665 I != E; ++I) {
4666 Writer.AddDeclRef(*I, Record);
4670 void ASTWriter::collectNonAffectingInputFiles() {
4671 SourceManager &SrcMgr = PP->getSourceManager();
4672 unsigned N = SrcMgr.local_sloc_entry_size();
4674 IsSLocAffecting.resize(N, true);
4676 if (!WritingModule)
4677 return;
4679 auto AffectingModuleMaps = GetAffectingModuleMaps(*PP, WritingModule);
4681 unsigned FileIDAdjustment = 0;
4682 unsigned OffsetAdjustment = 0;
4684 NonAffectingFileIDAdjustments.reserve(N);
4685 NonAffectingOffsetAdjustments.reserve(N);
4687 NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4688 NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4690 for (unsigned I = 1; I != N; ++I) {
4691 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(I);
4692 FileID FID = FileID::get(I);
4693 assert(&SrcMgr.getSLocEntry(FID) == SLoc);
4695 if (!SLoc->isFile())
4696 continue;
4697 const SrcMgr::FileInfo &File = SLoc->getFile();
4698 const SrcMgr::ContentCache *Cache = &File.getContentCache();
4699 if (!Cache->OrigEntry)
4700 continue;
4702 if (!isModuleMap(File.getFileCharacteristic()) ||
4703 AffectingModuleMaps.empty() ||
4704 llvm::is_contained(AffectingModuleMaps, *Cache->OrigEntry))
4705 continue;
4707 IsSLocAffecting[I] = false;
4709 FileIDAdjustment += 1;
4710 // Even empty files take up one element in the offset table.
4711 OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
4713 // If the previous file was non-affecting as well, just extend its entry
4714 // with our information.
4715 if (!NonAffectingFileIDs.empty() &&
4716 NonAffectingFileIDs.back().ID == FID.ID - 1) {
4717 NonAffectingFileIDs.back() = FID;
4718 NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
4719 NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
4720 NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
4721 continue;
4724 NonAffectingFileIDs.push_back(FID);
4725 NonAffectingRanges.emplace_back(SrcMgr.getLocForStartOfFile(FID),
4726 SrcMgr.getLocForEndOfFile(FID));
4727 NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4728 NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4732 ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4733 Module *WritingModule) {
4734 using namespace llvm;
4736 bool isModule = WritingModule != nullptr;
4738 // Make sure that the AST reader knows to finalize itself.
4739 if (Chain)
4740 Chain->finalizeForWriting();
4742 ASTContext &Context = SemaRef.Context;
4743 Preprocessor &PP = SemaRef.PP;
4745 // This needs to be done very early, since everything that writes
4746 // SourceLocations or FileIDs depends on it.
4747 collectNonAffectingInputFiles();
4749 writeUnhashedControlBlock(PP, Context);
4751 // Set up predefined declaration IDs.
4752 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4753 if (D) {
4754 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4755 DeclIDs[D] = ID;
4758 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4759 PREDEF_DECL_TRANSLATION_UNIT_ID);
4760 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4761 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4762 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4763 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4764 PREDEF_DECL_OBJC_PROTOCOL_ID);
4765 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4766 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4767 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4768 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4769 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
4770 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
4771 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4772 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
4773 RegisterPredefDecl(Context.MSGuidTagDecl,
4774 PREDEF_DECL_BUILTIN_MS_GUID_ID);
4775 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
4776 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4777 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
4778 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4779 PREDEF_DECL_CF_CONSTANT_STRING_ID);
4780 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4781 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
4782 RegisterPredefDecl(Context.TypePackElementDecl,
4783 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
4785 // Build a record containing all of the tentative definitions in this file, in
4786 // TentativeDefinitions order. Generally, this record will be empty for
4787 // headers.
4788 RecordData TentativeDefinitions;
4789 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4791 // Build a record containing all of the file scoped decls in this file.
4792 RecordData UnusedFileScopedDecls;
4793 if (!isModule)
4794 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4795 UnusedFileScopedDecls);
4797 // Build a record containing all of the delegating constructors we still need
4798 // to resolve.
4799 RecordData DelegatingCtorDecls;
4800 if (!isModule)
4801 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4803 // Write the set of weak, undeclared identifiers. We always write the
4804 // entire table, since later PCH files in a PCH chain are only interested in
4805 // the results at the end of the chain.
4806 RecordData WeakUndeclaredIdentifiers;
4807 for (const auto &WeakUndeclaredIdentifierList :
4808 SemaRef.WeakUndeclaredIdentifiers) {
4809 const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
4810 for (const auto &WI : WeakUndeclaredIdentifierList.second) {
4811 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4812 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4813 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4817 // Build a record containing all of the ext_vector declarations.
4818 RecordData ExtVectorDecls;
4819 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4821 // Build a record containing all of the VTable uses information.
4822 RecordData VTableUses;
4823 if (!SemaRef.VTableUses.empty()) {
4824 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4825 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4826 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4827 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4831 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4832 RecordData UnusedLocalTypedefNameCandidates;
4833 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4834 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4836 // Build a record containing all of pending implicit instantiations.
4837 RecordData PendingInstantiations;
4838 for (const auto &I : SemaRef.PendingInstantiations) {
4839 AddDeclRef(I.first, PendingInstantiations);
4840 AddSourceLocation(I.second, PendingInstantiations);
4842 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4843 "There are local ones at end of translation unit!");
4845 // Build a record containing some declaration references.
4846 RecordData SemaDeclRefs;
4847 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
4848 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4849 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4850 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
4853 RecordData CUDASpecialDeclRefs;
4854 if (Context.getcudaConfigureCallDecl()) {
4855 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4858 // Build a record containing all of the known namespaces.
4859 RecordData KnownNamespaces;
4860 for (const auto &I : SemaRef.KnownNamespaces) {
4861 if (!I.second)
4862 AddDeclRef(I.first, KnownNamespaces);
4865 // Build a record of all used, undefined objects that require definitions.
4866 RecordData UndefinedButUsed;
4868 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4869 SemaRef.getUndefinedButUsed(Undefined);
4870 for (const auto &I : Undefined) {
4871 AddDeclRef(I.first, UndefinedButUsed);
4872 AddSourceLocation(I.second, UndefinedButUsed);
4875 // Build a record containing all delete-expressions that we would like to
4876 // analyze later in AST.
4877 RecordData DeleteExprsToAnalyze;
4879 if (!isModule) {
4880 for (const auto &DeleteExprsInfo :
4881 SemaRef.getMismatchingDeleteExpressions()) {
4882 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4883 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4884 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4885 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4886 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4891 // Write the control block
4892 WriteControlBlock(PP, Context, isysroot);
4894 // Write the remaining AST contents.
4895 Stream.FlushToWord();
4896 ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
4897 Stream.EnterSubblock(AST_BLOCK_ID, 5);
4898 ASTBlockStartOffset = Stream.GetCurrentBitNo();
4900 // This is so that older clang versions, before the introduction
4901 // of the control block, can read and reject the newer PCH format.
4903 RecordData Record = {VERSION_MAJOR};
4904 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4907 // Create a lexical update block containing all of the declarations in the
4908 // translation unit that do not come from other AST files.
4909 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4910 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4911 for (const auto *D : TU->noload_decls()) {
4912 if (!D->isFromASTFile()) {
4913 NewGlobalKindDeclPairs.push_back(D->getKind());
4914 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4918 auto Abv = std::make_shared<BitCodeAbbrev>();
4919 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4920 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4921 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
4923 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4924 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4925 bytes(NewGlobalKindDeclPairs));
4928 // And a visible updates block for the translation unit.
4929 Abv = std::make_shared<BitCodeAbbrev>();
4930 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4931 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4932 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4933 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
4934 WriteDeclContextVisibleUpdate(TU);
4936 // If we have any extern "C" names, write out a visible update for them.
4937 if (Context.ExternCContext)
4938 WriteDeclContextVisibleUpdate(Context.ExternCContext);
4940 // If the translation unit has an anonymous namespace, and we don't already
4941 // have an update block for it, write it as an update block.
4942 // FIXME: Why do we not do this if there's already an update block?
4943 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4944 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4945 if (Record.empty())
4946 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4949 // Add update records for all mangling numbers and static local numbers.
4950 // These aren't really update records, but this is a convenient way of
4951 // tagging this rare extra data onto the declarations.
4952 for (const auto &Number : Context.MangleNumbers)
4953 if (!Number.first->isFromASTFile())
4954 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4955 Number.second));
4956 for (const auto &Number : Context.StaticLocalNumbers)
4957 if (!Number.first->isFromASTFile())
4958 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4959 Number.second));
4961 // Make sure visible decls, added to DeclContexts previously loaded from
4962 // an AST file, are registered for serialization. Likewise for template
4963 // specializations added to imported templates.
4964 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
4965 GetDeclRef(I);
4968 // Make sure all decls associated with an identifier are registered for
4969 // serialization, if we're storing decls with identifiers.
4970 if (!WritingModule || !getLangOpts().CPlusPlus) {
4971 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4972 for (const auto &ID : PP.getIdentifierTable()) {
4973 const IdentifierInfo *II = ID.second;
4974 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4975 IIs.push_back(II);
4977 // Sort the identifiers to visit based on their name.
4978 llvm::sort(IIs, llvm::deref<std::less<>>());
4979 for (const IdentifierInfo *II : IIs)
4980 for (const Decl *D : SemaRef.IdResolver.decls(II))
4981 GetDeclRef(D);
4984 // For method pool in the module, if it contains an entry for a selector,
4985 // the entry should be complete, containing everything introduced by that
4986 // module and all modules it imports. It's possible that the entry is out of
4987 // date, so we need to pull in the new content here.
4989 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4990 // safe, we copy all selectors out.
4991 llvm::SmallVector<Selector, 256> AllSelectors;
4992 for (auto &SelectorAndID : SelectorIDs)
4993 AllSelectors.push_back(SelectorAndID.first);
4994 for (auto &Selector : AllSelectors)
4995 SemaRef.updateOutOfDateSelector(Selector);
4997 // Form the record of special types.
4998 RecordData SpecialTypes;
4999 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
5000 AddTypeRef(Context.getFILEType(), SpecialTypes);
5001 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
5002 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
5003 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
5004 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
5005 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
5006 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
5008 if (Chain) {
5009 // Write the mapping information describing our module dependencies and how
5010 // each of those modules were mapped into our own offset/ID space, so that
5011 // the reader can build the appropriate mapping to its own offset/ID space.
5012 // The map consists solely of a blob with the following format:
5013 // *(module-kind:i8
5014 // module-name-len:i16 module-name:len*i8
5015 // source-location-offset:i32
5016 // identifier-id:i32
5017 // preprocessed-entity-id:i32
5018 // macro-definition-id:i32
5019 // submodule-id:i32
5020 // selector-id:i32
5021 // declaration-id:i32
5022 // c++-base-specifiers-id:i32
5023 // type-id:i32)
5025 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
5026 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
5027 // module name. Otherwise, it is the module file name.
5028 auto Abbrev = std::make_shared<BitCodeAbbrev>();
5029 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
5030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5031 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
5032 SmallString<2048> Buffer;
5034 llvm::raw_svector_ostream Out(Buffer);
5035 for (ModuleFile &M : Chain->ModuleMgr) {
5036 using namespace llvm::support;
5038 endian::Writer LE(Out, llvm::endianness::little);
5039 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
5040 StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
5041 LE.write<uint16_t>(Name.size());
5042 Out.write(Name.data(), Name.size());
5044 // Note: if a base ID was uint max, it would not be possible to load
5045 // another module after it or have more than one entity inside it.
5046 uint32_t None = std::numeric_limits<uint32_t>::max();
5048 auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
5049 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5050 if (ShouldWrite)
5051 LE.write<uint32_t>(BaseID);
5052 else
5053 LE.write<uint32_t>(None);
5056 // These values should be unique within a chain, since they will be read
5057 // as keys into ContinuousRangeMaps.
5058 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
5059 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
5060 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5061 writeBaseIDOrNone(M.BasePreprocessedEntityID,
5062 M.NumPreprocessedEntities);
5063 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5064 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5065 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
5066 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
5069 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
5070 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
5071 Buffer.data(), Buffer.size());
5074 // Build a record containing all of the DeclsToCheckForDeferredDiags.
5075 SmallVector<serialization::DeclID, 64> DeclsToCheckForDeferredDiags;
5076 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5077 DeclsToCheckForDeferredDiags.push_back(GetDeclRef(D));
5079 RecordData DeclUpdatesOffsetsRecord;
5081 // Keep writing types, declarations, and declaration update records
5082 // until we've emitted all of them.
5083 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5084 DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5085 WriteTypeAbbrevs();
5086 WriteDeclAbbrevs();
5087 do {
5088 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5089 while (!DeclTypesToEmit.empty()) {
5090 DeclOrType DOT = DeclTypesToEmit.front();
5091 DeclTypesToEmit.pop();
5092 if (DOT.isType())
5093 WriteType(DOT.getType());
5094 else
5095 WriteDecl(Context, DOT.getDecl());
5097 } while (!DeclUpdates.empty());
5098 Stream.ExitBlock();
5100 DoneWritingDeclsAndTypes = true;
5102 // These things can only be done once we've written out decls and types.
5103 WriteTypeDeclOffsets();
5104 if (!DeclUpdatesOffsetsRecord.empty())
5105 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
5106 WriteFileDeclIDsMap();
5107 WriteSourceManagerBlock(Context.getSourceManager(), PP);
5108 WriteComments();
5109 WritePreprocessor(PP, isModule);
5110 WriteHeaderSearch(PP.getHeaderSearchInfo());
5111 WriteSelectors(SemaRef);
5112 WriteReferencedSelectorsPool(SemaRef);
5113 WriteLateParsedTemplates(SemaRef);
5114 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
5115 WriteFPPragmaOptions(SemaRef.CurFPFeatureOverrides());
5116 WriteOpenCLExtensions(SemaRef);
5117 WriteCUDAPragmas(SemaRef);
5119 // If we're emitting a module, write out the submodule information.
5120 if (WritingModule)
5121 WriteSubmodules(WritingModule);
5123 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5125 // Write the record containing external, unnamed definitions.
5126 if (!EagerlyDeserializedDecls.empty())
5127 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
5129 if (!ModularCodegenDecls.empty())
5130 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5132 // Write the record containing tentative definitions.
5133 if (!TentativeDefinitions.empty())
5134 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
5136 // Write the record containing unused file scoped decls.
5137 if (!UnusedFileScopedDecls.empty())
5138 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
5140 // Write the record containing weak undeclared identifiers.
5141 if (!WeakUndeclaredIdentifiers.empty())
5142 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
5143 WeakUndeclaredIdentifiers);
5145 // Write the record containing ext_vector type names.
5146 if (!ExtVectorDecls.empty())
5147 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
5149 // Write the record containing VTable uses information.
5150 if (!VTableUses.empty())
5151 Stream.EmitRecord(VTABLE_USES, VTableUses);
5153 // Write the record containing potentially unused local typedefs.
5154 if (!UnusedLocalTypedefNameCandidates.empty())
5155 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5156 UnusedLocalTypedefNameCandidates);
5158 // Write the record containing pending implicit instantiations.
5159 if (!PendingInstantiations.empty())
5160 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
5162 // Write the record containing declaration references of Sema.
5163 if (!SemaDeclRefs.empty())
5164 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
5166 // Write the record containing decls to be checked for deferred diags.
5167 if (!DeclsToCheckForDeferredDiags.empty())
5168 Stream.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5169 DeclsToCheckForDeferredDiags);
5171 // Write the record containing CUDA-specific declaration references.
5172 if (!CUDASpecialDeclRefs.empty())
5173 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
5175 // Write the delegating constructors.
5176 if (!DelegatingCtorDecls.empty())
5177 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
5179 // Write the known namespaces.
5180 if (!KnownNamespaces.empty())
5181 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
5183 // Write the undefined internal functions and variables, and inline functions.
5184 if (!UndefinedButUsed.empty())
5185 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
5187 if (!DeleteExprsToAnalyze.empty())
5188 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5190 // Write the visible updates to DeclContexts.
5191 for (auto *DC : UpdatedDeclContexts)
5192 WriteDeclContextVisibleUpdate(DC);
5194 if (!WritingModule) {
5195 // Write the submodules that were imported, if any.
5196 struct ModuleInfo {
5197 uint64_t ID;
5198 Module *M;
5199 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5201 llvm::SmallVector<ModuleInfo, 64> Imports;
5202 for (const auto *I : Context.local_imports()) {
5203 assert(SubmoduleIDs.contains(I->getImportedModule()));
5204 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5205 I->getImportedModule()));
5208 if (!Imports.empty()) {
5209 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5210 return A.ID < B.ID;
5212 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5213 return A.ID == B.ID;
5216 // Sort and deduplicate module IDs.
5217 llvm::sort(Imports, Cmp);
5218 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
5219 Imports.end());
5221 RecordData ImportedModules;
5222 for (const auto &Import : Imports) {
5223 ImportedModules.push_back(Import.ID);
5224 // FIXME: If the module has macros imported then later has declarations
5225 // imported, this location won't be the right one as a location for the
5226 // declaration imports.
5227 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
5230 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5234 WriteObjCCategories();
5235 if(!WritingModule) {
5236 WriteOptimizePragmaOptions(SemaRef);
5237 WriteMSStructPragmaOptions(SemaRef);
5238 WriteMSPointersToMembersPragmaOptions(SemaRef);
5240 WritePackPragmaOptions(SemaRef);
5241 WriteFloatControlPragmaOptions(SemaRef);
5243 // Some simple statistics
5244 RecordData::value_type Record[] = {
5245 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
5246 Stream.EmitRecord(STATISTICS, Record);
5247 Stream.ExitBlock();
5248 Stream.FlushToWord();
5249 ASTBlockRange.second = Stream.GetCurrentBitNo() >> 3;
5251 // Write the module file extension blocks.
5252 for (const auto &ExtWriter : ModuleFileExtensionWriters)
5253 WriteModuleFileExtension(SemaRef, *ExtWriter);
5255 return backpatchSignature();
5258 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
5259 if (DeclUpdates.empty())
5260 return;
5262 DeclUpdateMap LocalUpdates;
5263 LocalUpdates.swap(DeclUpdates);
5265 for (auto &DeclUpdate : LocalUpdates) {
5266 const Decl *D = DeclUpdate.first;
5268 bool HasUpdatedBody = false;
5269 bool HasAddedVarDefinition = false;
5270 RecordData RecordData;
5271 ASTRecordWriter Record(*this, RecordData);
5272 for (auto &Update : DeclUpdate.second) {
5273 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5275 // An updated body is emitted last, so that the reader doesn't need
5276 // to skip over the lazy body to reach statements for other records.
5277 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5278 HasUpdatedBody = true;
5279 else if (Kind == UPD_CXX_ADDED_VAR_DEFINITION)
5280 HasAddedVarDefinition = true;
5281 else
5282 Record.push_back(Kind);
5284 switch (Kind) {
5285 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5286 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5287 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
5288 assert(Update.getDecl() && "no decl to add?");
5289 Record.push_back(GetDeclRef(Update.getDecl()));
5290 break;
5292 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
5293 case UPD_CXX_ADDED_VAR_DEFINITION:
5294 break;
5296 case UPD_CXX_POINT_OF_INSTANTIATION:
5297 // FIXME: Do we need to also save the template specialization kind here?
5298 Record.AddSourceLocation(Update.getLoc());
5299 break;
5301 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
5302 Record.AddStmt(const_cast<Expr *>(
5303 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
5304 break;
5306 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5307 Record.AddStmt(
5308 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5309 break;
5311 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5312 auto *RD = cast<CXXRecordDecl>(D);
5313 UpdatedDeclContexts.insert(RD->getPrimaryContext());
5314 Record.push_back(RD->isParamDestroyedInCallee());
5315 Record.push_back(llvm::to_underlying(RD->getArgPassingRestrictions()));
5316 Record.AddCXXDefinitionData(RD);
5317 Record.AddOffset(WriteDeclContextLexicalBlock(
5318 *Context, const_cast<CXXRecordDecl *>(RD)));
5320 // This state is sometimes updated by template instantiation, when we
5321 // switch from the specialization referring to the template declaration
5322 // to it referring to the template definition.
5323 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5324 Record.push_back(MSInfo->getTemplateSpecializationKind());
5325 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
5326 } else {
5327 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5328 Record.push_back(Spec->getTemplateSpecializationKind());
5329 Record.AddSourceLocation(Spec->getPointOfInstantiation());
5331 // The instantiation might have been resolved to a partial
5332 // specialization. If so, record which one.
5333 auto From = Spec->getInstantiatedFrom();
5334 if (auto PartialSpec =
5335 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5336 Record.push_back(true);
5337 Record.AddDeclRef(PartialSpec);
5338 Record.AddTemplateArgumentList(
5339 &Spec->getTemplateInstantiationArgs());
5340 } else {
5341 Record.push_back(false);
5344 Record.push_back(llvm::to_underlying(RD->getTagKind()));
5345 Record.AddSourceLocation(RD->getLocation());
5346 Record.AddSourceLocation(RD->getBeginLoc());
5347 Record.AddSourceRange(RD->getBraceRange());
5349 // Instantiation may change attributes; write them all out afresh.
5350 Record.push_back(D->hasAttrs());
5351 if (D->hasAttrs())
5352 Record.AddAttributes(D->getAttrs());
5354 // FIXME: Ensure we don't get here for explicit instantiations.
5355 break;
5358 case UPD_CXX_RESOLVED_DTOR_DELETE:
5359 Record.AddDeclRef(Update.getDecl());
5360 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
5361 break;
5363 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
5364 auto prototype =
5365 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
5366 Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
5367 break;
5370 case UPD_CXX_DEDUCED_RETURN_TYPE:
5371 Record.push_back(GetOrCreateTypeID(Update.getType()));
5372 break;
5374 case UPD_DECL_MARKED_USED:
5375 break;
5377 case UPD_MANGLING_NUMBER:
5378 case UPD_STATIC_LOCAL_NUMBER:
5379 Record.push_back(Update.getNumber());
5380 break;
5382 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
5383 Record.AddSourceRange(
5384 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
5385 break;
5387 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5388 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5389 Record.push_back(A->getAllocatorType());
5390 Record.AddStmt(A->getAllocator());
5391 Record.AddStmt(A->getAlignment());
5392 Record.AddSourceRange(A->getRange());
5393 break;
5396 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5397 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
5398 Record.AddSourceRange(
5399 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5400 break;
5402 case UPD_DECL_EXPORTED:
5403 Record.push_back(getSubmoduleID(Update.getModule()));
5404 break;
5406 case UPD_ADDED_ATTR_TO_RECORD:
5407 Record.AddAttributes(llvm::ArrayRef(Update.getAttr()));
5408 break;
5412 // Add a trailing update record, if any. These must go last because we
5413 // lazily load their attached statement.
5414 if (HasUpdatedBody) {
5415 const auto *Def = cast<FunctionDecl>(D);
5416 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
5417 Record.push_back(Def->isInlined());
5418 Record.AddSourceLocation(Def->getInnerLocStart());
5419 Record.AddFunctionDefinition(Def);
5420 } else if (HasAddedVarDefinition) {
5421 const auto *VD = cast<VarDecl>(D);
5422 Record.push_back(UPD_CXX_ADDED_VAR_DEFINITION);
5423 Record.push_back(VD->isInline());
5424 Record.push_back(VD->isInlineSpecified());
5425 Record.AddVarDeclInit(VD);
5428 OffsetsRecord.push_back(GetDeclRef(D));
5429 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
5433 void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
5434 RecordDataImpl &Record) {
5435 uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
5436 Record.push_back(Raw);
5439 FileID ASTWriter::getAdjustedFileID(FileID FID) const {
5440 if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
5441 NonAffectingFileIDs.empty())
5442 return FID;
5443 auto It = llvm::lower_bound(NonAffectingFileIDs, FID);
5444 unsigned Idx = std::distance(NonAffectingFileIDs.begin(), It);
5445 unsigned Offset = NonAffectingFileIDAdjustments[Idx];
5446 return FileID::get(FID.getOpaqueValue() - Offset);
5449 unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
5450 unsigned NumCreatedFIDs = PP->getSourceManager()
5451 .getLocalSLocEntry(FID.ID)
5452 .getFile()
5453 .NumCreatedFIDs;
5455 unsigned AdjustedNumCreatedFIDs = 0;
5456 for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
5457 if (IsSLocAffecting[I])
5458 ++AdjustedNumCreatedFIDs;
5459 return AdjustedNumCreatedFIDs;
5462 SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
5463 if (Loc.isInvalid())
5464 return Loc;
5465 return Loc.getLocWithOffset(-getAdjustment(Loc.getOffset()));
5468 SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
5469 return SourceRange(getAdjustedLocation(Range.getBegin()),
5470 getAdjustedLocation(Range.getEnd()));
5473 SourceLocation::UIntTy
5474 ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
5475 return Offset - getAdjustment(Offset);
5478 SourceLocation::UIntTy
5479 ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
5480 if (NonAffectingRanges.empty())
5481 return 0;
5483 if (PP->getSourceManager().isLoadedOffset(Offset))
5484 return 0;
5486 if (Offset > NonAffectingRanges.back().getEnd().getOffset())
5487 return NonAffectingOffsetAdjustments.back();
5489 if (Offset < NonAffectingRanges.front().getBegin().getOffset())
5490 return 0;
5492 auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
5493 return Range.getEnd().getOffset() < Offset;
5496 auto It = llvm::lower_bound(NonAffectingRanges, Offset, Contains);
5497 unsigned Idx = std::distance(NonAffectingRanges.begin(), It);
5498 return NonAffectingOffsetAdjustments[Idx];
5501 void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
5502 Record.push_back(getAdjustedFileID(FID).getOpaqueValue());
5505 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
5506 SourceLocationSequence *Seq) {
5507 Loc = getAdjustedLocation(Loc);
5508 Record.push_back(SourceLocationEncoding::encode(Loc, Seq));
5511 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record,
5512 SourceLocationSequence *Seq) {
5513 AddSourceLocation(Range.getBegin(), Record, Seq);
5514 AddSourceLocation(Range.getEnd(), Record, Seq);
5517 void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5518 AddAPInt(Value.bitcastToAPInt());
5521 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
5522 Record.push_back(getIdentifierRef(II));
5525 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
5526 if (!II)
5527 return 0;
5529 IdentID &ID = IdentifierIDs[II];
5530 if (ID == 0)
5531 ID = NextIdentID++;
5532 return ID;
5535 MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
5536 // Don't emit builtin macros like __LINE__ to the AST file unless they
5537 // have been redefined by the header (in which case they are not
5538 // isBuiltinMacro).
5539 if (!MI || MI->isBuiltinMacro())
5540 return 0;
5542 MacroID &ID = MacroIDs[MI];
5543 if (ID == 0) {
5544 ID = NextMacroID++;
5545 MacroInfoToEmitData Info = { Name, MI, ID };
5546 MacroInfosToEmit.push_back(Info);
5548 return ID;
5551 MacroID ASTWriter::getMacroID(MacroInfo *MI) {
5552 if (!MI || MI->isBuiltinMacro())
5553 return 0;
5555 assert(MacroIDs.contains(MI) && "Macro not emitted!");
5556 return MacroIDs[MI];
5559 uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
5560 return IdentMacroDirectivesOffsetMap.lookup(Name);
5563 void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5564 Record->push_back(Writer->getSelectorRef(SelRef));
5567 SelectorID ASTWriter::getSelectorRef(Selector Sel) {
5568 if (Sel.getAsOpaquePtr() == nullptr) {
5569 return 0;
5572 SelectorID SID = SelectorIDs[Sel];
5573 if (SID == 0 && Chain) {
5574 // This might trigger a ReadSelector callback, which will set the ID for
5575 // this selector.
5576 Chain->LoadSelector(Sel);
5577 SID = SelectorIDs[Sel];
5579 if (SID == 0) {
5580 SID = NextSelectorID++;
5581 SelectorIDs[Sel] = SID;
5583 return SID;
5586 void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5587 AddDeclRef(Temp->getDestructor());
5590 void ASTRecordWriter::AddTemplateArgumentLocInfo(
5591 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
5592 switch (Kind) {
5593 case TemplateArgument::Expression:
5594 AddStmt(Arg.getAsExpr());
5595 break;
5596 case TemplateArgument::Type:
5597 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
5598 break;
5599 case TemplateArgument::Template:
5600 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5601 AddSourceLocation(Arg.getTemplateNameLoc());
5602 break;
5603 case TemplateArgument::TemplateExpansion:
5604 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5605 AddSourceLocation(Arg.getTemplateNameLoc());
5606 AddSourceLocation(Arg.getTemplateEllipsisLoc());
5607 break;
5608 case TemplateArgument::Null:
5609 case TemplateArgument::Integral:
5610 case TemplateArgument::Declaration:
5611 case TemplateArgument::NullPtr:
5612 case TemplateArgument::Pack:
5613 // FIXME: Is this right?
5614 break;
5618 void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5619 AddTemplateArgument(Arg.getArgument());
5621 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5622 bool InfoHasSameExpr
5623 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5624 Record->push_back(InfoHasSameExpr);
5625 if (InfoHasSameExpr)
5626 return; // Avoid storing the same expr twice.
5628 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
5631 void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
5632 if (!TInfo) {
5633 AddTypeRef(QualType());
5634 return;
5637 AddTypeRef(TInfo->getType());
5638 AddTypeLoc(TInfo->getTypeLoc());
5641 void ASTRecordWriter::AddTypeLoc(TypeLoc TL, LocSeq *OuterSeq) {
5642 LocSeq::State Seq(OuterSeq);
5643 TypeLocWriter TLW(*this, Seq);
5644 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
5645 TLW.Visit(TL);
5648 void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
5649 Record.push_back(GetOrCreateTypeID(T));
5652 TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
5653 assert(Context);
5654 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5655 if (T.isNull())
5656 return TypeIdx();
5657 assert(!T.getLocalFastQualifiers());
5659 TypeIdx &Idx = TypeIdxs[T];
5660 if (Idx.getIndex() == 0) {
5661 if (DoneWritingDeclsAndTypes) {
5662 assert(0 && "New type seen after serializing all the types to emit!");
5663 return TypeIdx();
5666 // We haven't seen this type before. Assign it a new ID and put it
5667 // into the queue of types to emit.
5668 Idx = TypeIdx(NextTypeID++);
5669 DeclTypesToEmit.push(T);
5671 return Idx;
5675 TypeID ASTWriter::getTypeID(QualType T) const {
5676 assert(Context);
5677 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5678 if (T.isNull())
5679 return TypeIdx();
5680 assert(!T.getLocalFastQualifiers());
5682 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5683 assert(I != TypeIdxs.end() && "Type not emitted!");
5684 return I->second;
5688 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
5689 Record.push_back(GetDeclRef(D));
5692 DeclID ASTWriter::GetDeclRef(const Decl *D) {
5693 assert(WritingAST && "Cannot request a declaration ID before AST writing");
5695 if (!D) {
5696 return 0;
5699 // If D comes from an AST file, its declaration ID is already known and
5700 // fixed.
5701 if (D->isFromASTFile())
5702 return D->getGlobalID();
5704 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
5705 DeclID &ID = DeclIDs[D];
5706 if (ID == 0) {
5707 if (DoneWritingDeclsAndTypes) {
5708 assert(0 && "New decl seen after serializing all the decls to emit!");
5709 return 0;
5712 // We haven't seen this declaration before. Give it a new ID and
5713 // enqueue it in the list of declarations to emit.
5714 ID = NextDeclID++;
5715 DeclTypesToEmit.push(const_cast<Decl *>(D));
5718 return ID;
5721 DeclID ASTWriter::getDeclID(const Decl *D) {
5722 if (!D)
5723 return 0;
5725 // If D comes from an AST file, its declaration ID is already known and
5726 // fixed.
5727 if (D->isFromASTFile())
5728 return D->getGlobalID();
5730 assert(DeclIDs.contains(D) && "Declaration not emitted!");
5731 return DeclIDs[D];
5734 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5735 assert(ID);
5736 assert(D);
5738 SourceLocation Loc = D->getLocation();
5739 if (Loc.isInvalid())
5740 return;
5742 // We only keep track of the file-level declarations of each file.
5743 if (!D->getLexicalDeclContext()->isFileContext())
5744 return;
5745 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5746 // a function/objc method, should not have TU as lexical context.
5747 // TemplateTemplateParmDecls that are part of an alias template, should not
5748 // have TU as lexical context.
5749 if (isa<ParmVarDecl, TemplateTemplateParmDecl>(D))
5750 return;
5752 SourceManager &SM = Context->getSourceManager();
5753 SourceLocation FileLoc = SM.getFileLoc(Loc);
5754 assert(SM.isLocalSourceLocation(FileLoc));
5755 FileID FID;
5756 unsigned Offset;
5757 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5758 if (FID.isInvalid())
5759 return;
5760 assert(SM.getSLocEntry(FID).isFile());
5761 assert(IsSLocAffecting[FID.ID]);
5763 std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
5764 if (!Info)
5765 Info = std::make_unique<DeclIDInFileInfo>();
5767 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5768 LocDeclIDsTy &Decls = Info->DeclIDs;
5769 Decls.push_back(LocDecl);
5772 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5773 assert(needsAnonymousDeclarationNumber(D) &&
5774 "expected an anonymous declaration");
5776 // Number the anonymous declarations within this context, if we've not
5777 // already done so.
5778 auto It = AnonymousDeclarationNumbers.find(D);
5779 if (It == AnonymousDeclarationNumbers.end()) {
5780 auto *DC = D->getLexicalDeclContext();
5781 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5782 AnonymousDeclarationNumbers[ND] = Number;
5785 It = AnonymousDeclarationNumbers.find(D);
5786 assert(It != AnonymousDeclarationNumbers.end() &&
5787 "declaration not found within its lexical context");
5790 return It->second;
5793 void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5794 DeclarationName Name) {
5795 switch (Name.getNameKind()) {
5796 case DeclarationName::CXXConstructorName:
5797 case DeclarationName::CXXDestructorName:
5798 case DeclarationName::CXXConversionFunctionName:
5799 AddTypeSourceInfo(DNLoc.getNamedTypeInfo());
5800 break;
5802 case DeclarationName::CXXOperatorName:
5803 AddSourceRange(DNLoc.getCXXOperatorNameRange());
5804 break;
5806 case DeclarationName::CXXLiteralOperatorName:
5807 AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc());
5808 break;
5810 case DeclarationName::Identifier:
5811 case DeclarationName::ObjCZeroArgSelector:
5812 case DeclarationName::ObjCOneArgSelector:
5813 case DeclarationName::ObjCMultiArgSelector:
5814 case DeclarationName::CXXUsingDirective:
5815 case DeclarationName::CXXDeductionGuideName:
5816 break;
5820 void ASTRecordWriter::AddDeclarationNameInfo(
5821 const DeclarationNameInfo &NameInfo) {
5822 AddDeclarationName(NameInfo.getName());
5823 AddSourceLocation(NameInfo.getLoc());
5824 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
5827 void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5828 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5829 Record->push_back(Info.NumTemplParamLists);
5830 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
5831 AddTemplateParameterList(Info.TemplParamLists[i]);
5834 void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
5835 // Nested name specifiers usually aren't too long. I think that 8 would
5836 // typically accommodate the vast majority.
5837 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5839 // Push each of the nested-name-specifiers's onto a stack for
5840 // serialization in reverse order.
5841 while (NNS) {
5842 NestedNames.push_back(NNS);
5843 NNS = NNS.getPrefix();
5846 Record->push_back(NestedNames.size());
5847 while(!NestedNames.empty()) {
5848 NNS = NestedNames.pop_back_val();
5849 NestedNameSpecifier::SpecifierKind Kind
5850 = NNS.getNestedNameSpecifier()->getKind();
5851 Record->push_back(Kind);
5852 switch (Kind) {
5853 case NestedNameSpecifier::Identifier:
5854 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5855 AddSourceRange(NNS.getLocalSourceRange());
5856 break;
5858 case NestedNameSpecifier::Namespace:
5859 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5860 AddSourceRange(NNS.getLocalSourceRange());
5861 break;
5863 case NestedNameSpecifier::NamespaceAlias:
5864 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5865 AddSourceRange(NNS.getLocalSourceRange());
5866 break;
5868 case NestedNameSpecifier::TypeSpec:
5869 case NestedNameSpecifier::TypeSpecWithTemplate:
5870 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5871 AddTypeRef(NNS.getTypeLoc().getType());
5872 AddTypeLoc(NNS.getTypeLoc());
5873 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5874 break;
5876 case NestedNameSpecifier::Global:
5877 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5878 break;
5880 case NestedNameSpecifier::Super:
5881 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5882 AddSourceRange(NNS.getLocalSourceRange());
5883 break;
5888 void ASTRecordWriter::AddTemplateParameterList(
5889 const TemplateParameterList *TemplateParams) {
5890 assert(TemplateParams && "No TemplateParams!");
5891 AddSourceLocation(TemplateParams->getTemplateLoc());
5892 AddSourceLocation(TemplateParams->getLAngleLoc());
5893 AddSourceLocation(TemplateParams->getRAngleLoc());
5895 Record->push_back(TemplateParams->size());
5896 for (const auto &P : *TemplateParams)
5897 AddDeclRef(P);
5898 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
5899 Record->push_back(true);
5900 AddStmt(const_cast<Expr*>(RequiresClause));
5901 } else {
5902 Record->push_back(false);
5906 /// Emit a template argument list.
5907 void ASTRecordWriter::AddTemplateArgumentList(
5908 const TemplateArgumentList *TemplateArgs) {
5909 assert(TemplateArgs && "No TemplateArgs!");
5910 Record->push_back(TemplateArgs->size());
5911 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
5912 AddTemplateArgument(TemplateArgs->get(i));
5915 void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5916 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
5917 assert(ASTTemplArgList && "No ASTTemplArgList!");
5918 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5919 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5920 Record->push_back(ASTTemplArgList->NumTemplateArgs);
5921 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5922 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5923 AddTemplateArgumentLoc(TemplArgs[i]);
5926 void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5927 Record->push_back(Set.size());
5928 for (ASTUnresolvedSet::const_iterator
5929 I = Set.begin(), E = Set.end(); I != E; ++I) {
5930 AddDeclRef(I.getDecl());
5931 Record->push_back(I.getAccess());
5935 // FIXME: Move this out of the main ASTRecordWriter interface.
5936 void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5937 Record->push_back(Base.isVirtual());
5938 Record->push_back(Base.isBaseOfClass());
5939 Record->push_back(Base.getAccessSpecifierAsWritten());
5940 Record->push_back(Base.getInheritConstructors());
5941 AddTypeSourceInfo(Base.getTypeSourceInfo());
5942 AddSourceRange(Base.getSourceRange());
5943 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5944 : SourceLocation());
5947 static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5948 ArrayRef<CXXBaseSpecifier> Bases) {
5949 ASTWriter::RecordData Record;
5950 ASTRecordWriter Writer(W, Record);
5951 Writer.push_back(Bases.size());
5953 for (auto &Base : Bases)
5954 Writer.AddCXXBaseSpecifier(Base);
5956 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
5959 // FIXME: Move this out of the main ASTRecordWriter interface.
5960 void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5961 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5964 static uint64_t
5965 EmitCXXCtorInitializers(ASTWriter &W,
5966 ArrayRef<CXXCtorInitializer *> CtorInits) {
5967 ASTWriter::RecordData Record;
5968 ASTRecordWriter Writer(W, Record);
5969 Writer.push_back(CtorInits.size());
5971 for (auto *Init : CtorInits) {
5972 if (Init->isBaseInitializer()) {
5973 Writer.push_back(CTOR_INITIALIZER_BASE);
5974 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5975 Writer.push_back(Init->isBaseVirtual());
5976 } else if (Init->isDelegatingInitializer()) {
5977 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5978 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5979 } else if (Init->isMemberInitializer()){
5980 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5981 Writer.AddDeclRef(Init->getMember());
5982 } else {
5983 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5984 Writer.AddDeclRef(Init->getIndirectMember());
5987 Writer.AddSourceLocation(Init->getMemberLocation());
5988 Writer.AddStmt(Init->getInit());
5989 Writer.AddSourceLocation(Init->getLParenLoc());
5990 Writer.AddSourceLocation(Init->getRParenLoc());
5991 Writer.push_back(Init->isWritten());
5992 if (Init->isWritten())
5993 Writer.push_back(Init->getSourceOrder());
5996 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
5999 // FIXME: Move this out of the main ASTRecordWriter interface.
6000 void ASTRecordWriter::AddCXXCtorInitializers(
6001 ArrayRef<CXXCtorInitializer *> CtorInits) {
6002 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
6005 void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
6006 auto &Data = D->data();
6008 Record->push_back(Data.IsLambda);
6010 BitsPacker DefinitionBits;
6012 #define FIELD(Name, Width, Merge) \
6013 if (!DefinitionBits.canWriteNextNBits(Width)) { \
6014 Record->push_back(DefinitionBits); \
6015 DefinitionBits.reset(0); \
6017 DefinitionBits.addBits(Data.Name, Width);
6019 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
6020 #undef FIELD
6022 Record->push_back(DefinitionBits);
6024 // getODRHash will compute the ODRHash if it has not been previously computed.
6025 Record->push_back(D->getODRHash());
6027 bool ModulesDebugInfo =
6028 Writer->Context->getLangOpts().ModulesDebugInfo && !D->isDependentType();
6029 Record->push_back(ModulesDebugInfo);
6030 if (ModulesDebugInfo)
6031 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
6033 // IsLambda bit is already saved.
6035 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
6036 Record->push_back(Data.ComputedVisibleConversions);
6037 if (Data.ComputedVisibleConversions)
6038 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
6039 // Data.Definition is the owning decl, no need to write it.
6041 if (!Data.IsLambda) {
6042 Record->push_back(Data.NumBases);
6043 if (Data.NumBases > 0)
6044 AddCXXBaseSpecifiers(Data.bases());
6046 // FIXME: Make VBases lazily computed when needed to avoid storing them.
6047 Record->push_back(Data.NumVBases);
6048 if (Data.NumVBases > 0)
6049 AddCXXBaseSpecifiers(Data.vbases());
6051 AddDeclRef(D->getFirstFriend());
6052 } else {
6053 auto &Lambda = D->getLambdaData();
6055 BitsPacker LambdaBits;
6056 LambdaBits.addBits(Lambda.DependencyKind, /*Width=*/2);
6057 LambdaBits.addBit(Lambda.IsGenericLambda);
6058 LambdaBits.addBits(Lambda.CaptureDefault, /*Width=*/2);
6059 LambdaBits.addBits(Lambda.NumCaptures, /*Width=*/15);
6060 LambdaBits.addBit(Lambda.HasKnownInternalLinkage);
6061 Record->push_back(LambdaBits);
6063 Record->push_back(Lambda.NumExplicitCaptures);
6064 Record->push_back(Lambda.ManglingNumber);
6065 Record->push_back(D->getDeviceLambdaManglingNumber());
6066 // The lambda context declaration and index within the context are provided
6067 // separately, so that they can be used for merging.
6068 AddTypeSourceInfo(Lambda.MethodTyInfo);
6069 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
6070 const LambdaCapture &Capture = Lambda.Captures.front()[I];
6071 AddSourceLocation(Capture.getLocation());
6073 BitsPacker CaptureBits;
6074 CaptureBits.addBit(Capture.isImplicit());
6075 CaptureBits.addBits(Capture.getCaptureKind(), /*Width=*/3);
6076 Record->push_back(CaptureBits);
6078 switch (Capture.getCaptureKind()) {
6079 case LCK_StarThis:
6080 case LCK_This:
6081 case LCK_VLAType:
6082 break;
6083 case LCK_ByCopy:
6084 case LCK_ByRef:
6085 ValueDecl *Var =
6086 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
6087 AddDeclRef(Var);
6088 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
6089 : SourceLocation());
6090 break;
6096 void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
6097 const Expr *Init = VD->getInit();
6098 if (!Init) {
6099 push_back(0);
6100 return;
6103 uint64_t Val = 1;
6104 if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
6105 Val |= (ES->HasConstantInitialization ? 2 : 0);
6106 Val |= (ES->HasConstantDestruction ? 4 : 0);
6107 APValue *Evaluated = VD->getEvaluatedValue();
6108 // If the evaluated result is constant, emit it.
6109 if (Evaluated && (Evaluated->isInt() || Evaluated->isFloat()))
6110 Val |= 8;
6112 push_back(Val);
6113 if (Val & 8) {
6114 AddAPValue(*VD->getEvaluatedValue());
6117 writeStmtRef(Init);
6120 void ASTWriter::ReaderInitialized(ASTReader *Reader) {
6121 assert(Reader && "Cannot remove chain");
6122 assert((!Chain || Chain == Reader) && "Cannot replace chain");
6123 assert(FirstDeclID == NextDeclID &&
6124 FirstTypeID == NextTypeID &&
6125 FirstIdentID == NextIdentID &&
6126 FirstMacroID == NextMacroID &&
6127 FirstSubmoduleID == NextSubmoduleID &&
6128 FirstSelectorID == NextSelectorID &&
6129 "Setting chain after writing has started.");
6131 Chain = Reader;
6133 // Note, this will get called multiple times, once one the reader starts up
6134 // and again each time it's done reading a PCH or module.
6135 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6136 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6137 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
6138 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
6139 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
6140 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
6141 NextDeclID = FirstDeclID;
6142 NextTypeID = FirstTypeID;
6143 NextIdentID = FirstIdentID;
6144 NextMacroID = FirstMacroID;
6145 NextSelectorID = FirstSelectorID;
6146 NextSubmoduleID = FirstSubmoduleID;
6149 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
6150 // Always keep the highest ID. See \p TypeRead() for more information.
6151 IdentID &StoredID = IdentifierIDs[II];
6152 if (ID > StoredID)
6153 StoredID = ID;
6156 void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
6157 // Always keep the highest ID. See \p TypeRead() for more information.
6158 MacroID &StoredID = MacroIDs[MI];
6159 if (ID > StoredID)
6160 StoredID = ID;
6163 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
6164 // Always take the highest-numbered type index. This copes with an interesting
6165 // case for chained AST writing where we schedule writing the type and then,
6166 // later, deserialize the type from another AST. In this case, we want to
6167 // keep the higher-numbered entry so that we can properly write it out to
6168 // the AST file.
6169 TypeIdx &StoredIdx = TypeIdxs[T];
6170 if (Idx.getIndex() >= StoredIdx.getIndex())
6171 StoredIdx = Idx;
6174 void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
6175 // Always keep the highest ID. See \p TypeRead() for more information.
6176 SelectorID &StoredID = SelectorIDs[S];
6177 if (ID > StoredID)
6178 StoredID = ID;
6181 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
6182 MacroDefinitionRecord *MD) {
6183 assert(!MacroDefinitions.contains(MD));
6184 MacroDefinitions[MD] = ID;
6187 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6188 assert(!SubmoduleIDs.contains(Mod));
6189 SubmoduleIDs[Mod] = ID;
6192 void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
6193 if (Chain && Chain->isProcessingUpdateRecords()) return;
6194 assert(D->isCompleteDefinition());
6195 assert(!WritingAST && "Already writing the AST!");
6196 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
6197 // We are interested when a PCH decl is modified.
6198 if (RD->isFromASTFile()) {
6199 // A forward reference was mutated into a definition. Rewrite it.
6200 // FIXME: This happens during template instantiation, should we
6201 // have created a new definition decl instead ?
6202 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6203 "completed a tag from another module but not by instantiation?");
6204 DeclUpdates[RD].push_back(
6205 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
6210 static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6211 if (D->isFromASTFile())
6212 return true;
6214 // The predefined __va_list_tag struct is imported if we imported any decls.
6215 // FIXME: This is a gross hack.
6216 return D == D->getASTContext().getVaListTagDecl();
6219 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
6220 if (Chain && Chain->isProcessingUpdateRecords()) return;
6221 assert(DC->isLookupContext() &&
6222 "Should not add lookup results to non-lookup contexts!");
6224 // TU is handled elsewhere.
6225 if (isa<TranslationUnitDecl>(DC))
6226 return;
6228 // Namespaces are handled elsewhere, except for template instantiations of
6229 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6230 // local instantiations are added to an imported context. Only happens when
6231 // adding ADL lookup candidates, for example templated friends.
6232 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6233 !isa<FunctionTemplateDecl>(D))
6234 return;
6236 // We're only interested in cases where a local declaration is added to an
6237 // imported context.
6238 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6239 return;
6241 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
6242 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
6243 assert(!WritingAST && "Already writing the AST!");
6244 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6245 // We're adding a visible declaration to a predefined decl context. Ensure
6246 // that we write out all of its lookup results so we don't get a nasty
6247 // surprise when we try to emit its lookup table.
6248 llvm::append_range(DeclsToEmitEvenIfUnreferenced, DC->decls());
6250 DeclsToEmitEvenIfUnreferenced.push_back(D);
6253 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
6254 if (Chain && Chain->isProcessingUpdateRecords()) return;
6255 assert(D->isImplicit());
6257 // We're only interested in cases where a local declaration is added to an
6258 // imported context.
6259 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6260 return;
6262 if (!isa<CXXMethodDecl>(D))
6263 return;
6265 // A decl coming from PCH was modified.
6266 assert(RD->isCompleteDefinition());
6267 assert(!WritingAST && "Already writing the AST!");
6268 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
6271 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
6272 if (Chain && Chain->isProcessingUpdateRecords()) return;
6273 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6274 if (!Chain) return;
6275 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6276 // If we don't already know the exception specification for this redecl
6277 // chain, add an update record for it.
6278 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6279 ->getType()
6280 ->castAs<FunctionProtoType>()
6281 ->getExceptionSpecType()))
6282 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6286 void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
6287 if (Chain && Chain->isProcessingUpdateRecords()) return;
6288 assert(!WritingAST && "Already writing the AST!");
6289 if (!Chain) return;
6290 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6291 DeclUpdates[D].push_back(
6292 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6296 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6297 const FunctionDecl *Delete,
6298 Expr *ThisArg) {
6299 if (Chain && Chain->isProcessingUpdateRecords()) return;
6300 assert(!WritingAST && "Already writing the AST!");
6301 assert(Delete && "Not given an operator delete");
6302 if (!Chain) return;
6303 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
6304 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6308 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
6309 if (Chain && Chain->isProcessingUpdateRecords()) return;
6310 assert(!WritingAST && "Already writing the AST!");
6311 if (!D->isFromASTFile())
6312 return; // Declaration not imported from PCH.
6314 // Implicit function decl from a PCH was defined.
6315 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6318 void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6319 if (Chain && Chain->isProcessingUpdateRecords()) return;
6320 assert(!WritingAST && "Already writing the AST!");
6321 if (!D->isFromASTFile())
6322 return;
6324 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6327 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
6328 if (Chain && Chain->isProcessingUpdateRecords()) return;
6329 assert(!WritingAST && "Already writing the AST!");
6330 if (!D->isFromASTFile())
6331 return;
6333 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6336 void ASTWriter::InstantiationRequested(const ValueDecl *D) {
6337 if (Chain && Chain->isProcessingUpdateRecords()) return;
6338 assert(!WritingAST && "Already writing the AST!");
6339 if (!D->isFromASTFile())
6340 return;
6342 // Since the actual instantiation is delayed, this really means that we need
6343 // to update the instantiation location.
6344 SourceLocation POI;
6345 if (auto *VD = dyn_cast<VarDecl>(D))
6346 POI = VD->getPointOfInstantiation();
6347 else
6348 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6349 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
6352 void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
6353 if (Chain && Chain->isProcessingUpdateRecords()) return;
6354 assert(!WritingAST && "Already writing the AST!");
6355 if (!D->isFromASTFile())
6356 return;
6358 DeclUpdates[D].push_back(
6359 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6362 void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6363 assert(!WritingAST && "Already writing the AST!");
6364 if (!D->isFromASTFile())
6365 return;
6367 DeclUpdates[D].push_back(
6368 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6371 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6372 const ObjCInterfaceDecl *IFD) {
6373 if (Chain && Chain->isProcessingUpdateRecords()) return;
6374 assert(!WritingAST && "Already writing the AST!");
6375 if (!IFD->isFromASTFile())
6376 return; // Declaration not imported from PCH.
6378 assert(IFD->getDefinition() && "Category on a class without a definition?");
6379 ObjCClassesWithCategories.insert(
6380 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
6383 void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
6384 if (Chain && Chain->isProcessingUpdateRecords()) return;
6385 assert(!WritingAST && "Already writing the AST!");
6387 // If there is *any* declaration of the entity that's not from an AST file,
6388 // we can skip writing the update record. We make sure that isUsed() triggers
6389 // completion of the redeclaration chain of the entity.
6390 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6391 if (IsLocalDecl(Prev))
6392 return;
6394 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
6397 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
6398 if (Chain && Chain->isProcessingUpdateRecords()) return;
6399 assert(!WritingAST && "Already writing the AST!");
6400 if (!D->isFromASTFile())
6401 return;
6403 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6406 void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
6407 if (Chain && Chain->isProcessingUpdateRecords()) return;
6408 assert(!WritingAST && "Already writing the AST!");
6409 if (!D->isFromASTFile())
6410 return;
6412 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
6415 void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6416 const Attr *Attr) {
6417 if (Chain && Chain->isProcessingUpdateRecords()) return;
6418 assert(!WritingAST && "Already writing the AST!");
6419 if (!D->isFromASTFile())
6420 return;
6422 DeclUpdates[D].push_back(
6423 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
6426 void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
6427 if (Chain && Chain->isProcessingUpdateRecords()) return;
6428 assert(!WritingAST && "Already writing the AST!");
6429 assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
6430 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
6433 void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6434 const RecordDecl *Record) {
6435 if (Chain && Chain->isProcessingUpdateRecords()) return;
6436 assert(!WritingAST && "Already writing the AST!");
6437 if (!Record->isFromASTFile())
6438 return;
6439 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6442 void ASTWriter::AddedCXXTemplateSpecialization(
6443 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6444 assert(!WritingAST && "Already writing the AST!");
6446 if (!TD->getFirstDecl()->isFromASTFile())
6447 return;
6448 if (Chain && Chain->isProcessingUpdateRecords())
6449 return;
6451 DeclsToEmitEvenIfUnreferenced.push_back(D);
6454 void ASTWriter::AddedCXXTemplateSpecialization(
6455 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6456 assert(!WritingAST && "Already writing the AST!");
6458 if (!TD->getFirstDecl()->isFromASTFile())
6459 return;
6460 if (Chain && Chain->isProcessingUpdateRecords())
6461 return;
6463 DeclsToEmitEvenIfUnreferenced.push_back(D);
6466 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6467 const FunctionDecl *D) {
6468 assert(!WritingAST && "Already writing the AST!");
6470 if (!TD->getFirstDecl()->isFromASTFile())
6471 return;
6472 if (Chain && Chain->isProcessingUpdateRecords())
6473 return;
6475 DeclsToEmitEvenIfUnreferenced.push_back(D);
6478 //===----------------------------------------------------------------------===//
6479 //// OMPClause Serialization
6480 ////===----------------------------------------------------------------------===//
6482 namespace {
6484 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
6485 ASTRecordWriter &Record;
6487 public:
6488 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
6489 #define GEN_CLANG_CLAUSE_CLASS
6490 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
6491 #include "llvm/Frontend/OpenMP/OMP.inc"
6492 void writeClause(OMPClause *C);
6493 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
6494 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
6499 void ASTRecordWriter::writeOMPClause(OMPClause *C) {
6500 OMPClauseWriter(*this).writeClause(C);
6503 void OMPClauseWriter::writeClause(OMPClause *C) {
6504 Record.push_back(unsigned(C->getClauseKind()));
6505 Visit(C);
6506 Record.AddSourceLocation(C->getBeginLoc());
6507 Record.AddSourceLocation(C->getEndLoc());
6510 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
6511 Record.push_back(uint64_t(C->getCaptureRegion()));
6512 Record.AddStmt(C->getPreInitStmt());
6515 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6516 VisitOMPClauseWithPreInit(C);
6517 Record.AddStmt(C->getPostUpdateExpr());
6520 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6521 VisitOMPClauseWithPreInit(C);
6522 Record.push_back(uint64_t(C->getNameModifier()));
6523 Record.AddSourceLocation(C->getNameModifierLoc());
6524 Record.AddSourceLocation(C->getColonLoc());
6525 Record.AddStmt(C->getCondition());
6526 Record.AddSourceLocation(C->getLParenLoc());
6529 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
6530 VisitOMPClauseWithPreInit(C);
6531 Record.AddStmt(C->getCondition());
6532 Record.AddSourceLocation(C->getLParenLoc());
6535 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6536 VisitOMPClauseWithPreInit(C);
6537 Record.AddStmt(C->getNumThreads());
6538 Record.AddSourceLocation(C->getLParenLoc());
6541 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6542 Record.AddStmt(C->getSafelen());
6543 Record.AddSourceLocation(C->getLParenLoc());
6546 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6547 Record.AddStmt(C->getSimdlen());
6548 Record.AddSourceLocation(C->getLParenLoc());
6551 void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause *C) {
6552 Record.push_back(C->getNumSizes());
6553 for (Expr *Size : C->getSizesRefs())
6554 Record.AddStmt(Size);
6555 Record.AddSourceLocation(C->getLParenLoc());
6558 void OMPClauseWriter::VisitOMPFullClause(OMPFullClause *C) {}
6560 void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause *C) {
6561 Record.AddStmt(C->getFactor());
6562 Record.AddSourceLocation(C->getLParenLoc());
6565 void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6566 Record.AddStmt(C->getAllocator());
6567 Record.AddSourceLocation(C->getLParenLoc());
6570 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6571 Record.AddStmt(C->getNumForLoops());
6572 Record.AddSourceLocation(C->getLParenLoc());
6575 void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
6576 Record.AddStmt(C->getEventHandler());
6577 Record.AddSourceLocation(C->getLParenLoc());
6580 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
6581 Record.push_back(unsigned(C->getDefaultKind()));
6582 Record.AddSourceLocation(C->getLParenLoc());
6583 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6586 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
6587 Record.push_back(unsigned(C->getProcBindKind()));
6588 Record.AddSourceLocation(C->getLParenLoc());
6589 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6592 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6593 VisitOMPClauseWithPreInit(C);
6594 Record.push_back(C->getScheduleKind());
6595 Record.push_back(C->getFirstScheduleModifier());
6596 Record.push_back(C->getSecondScheduleModifier());
6597 Record.AddStmt(C->getChunkSize());
6598 Record.AddSourceLocation(C->getLParenLoc());
6599 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6600 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6601 Record.AddSourceLocation(C->getScheduleKindLoc());
6602 Record.AddSourceLocation(C->getCommaLoc());
6605 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6606 Record.push_back(C->getLoopNumIterations().size());
6607 Record.AddStmt(C->getNumForLoops());
6608 for (Expr *NumIter : C->getLoopNumIterations())
6609 Record.AddStmt(NumIter);
6610 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
6611 Record.AddStmt(C->getLoopCounter(I));
6612 Record.AddSourceLocation(C->getLParenLoc());
6615 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6617 void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6619 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6621 void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6623 void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6625 void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
6626 Record.push_back(C->isExtended() ? 1 : 0);
6627 if (C->isExtended()) {
6628 Record.AddSourceLocation(C->getLParenLoc());
6629 Record.AddSourceLocation(C->getArgumentLoc());
6630 Record.writeEnum(C->getDependencyKind());
6634 void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6636 void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause *) {}
6638 // Save the parameter of fail clause.
6639 void OMPClauseWriter::VisitOMPFailClause(OMPFailClause *C) {
6640 Record.AddSourceLocation(C->getLParenLoc());
6641 Record.AddSourceLocation(C->getFailParameterLoc());
6642 Record.writeEnum(C->getFailParameter());
6645 void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6647 void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
6649 void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
6651 void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
6653 void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
6655 void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6657 void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6659 void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6661 void OMPClauseWriter::VisitOMPInitClause(OMPInitClause *C) {
6662 Record.push_back(C->varlist_size());
6663 for (Expr *VE : C->varlists())
6664 Record.AddStmt(VE);
6665 Record.writeBool(C->getIsTarget());
6666 Record.writeBool(C->getIsTargetSync());
6667 Record.AddSourceLocation(C->getLParenLoc());
6668 Record.AddSourceLocation(C->getVarLoc());
6671 void OMPClauseWriter::VisitOMPUseClause(OMPUseClause *C) {
6672 Record.AddStmt(C->getInteropVar());
6673 Record.AddSourceLocation(C->getLParenLoc());
6674 Record.AddSourceLocation(C->getVarLoc());
6677 void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *C) {
6678 Record.AddStmt(C->getInteropVar());
6679 Record.AddSourceLocation(C->getLParenLoc());
6680 Record.AddSourceLocation(C->getVarLoc());
6683 void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
6684 VisitOMPClauseWithPreInit(C);
6685 Record.AddStmt(C->getCondition());
6686 Record.AddSourceLocation(C->getLParenLoc());
6689 void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause *C) {
6690 VisitOMPClauseWithPreInit(C);
6691 Record.AddStmt(C->getCondition());
6692 Record.AddSourceLocation(C->getLParenLoc());
6695 void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause *C) {
6696 VisitOMPClauseWithPreInit(C);
6697 Record.AddStmt(C->getThreadID());
6698 Record.AddSourceLocation(C->getLParenLoc());
6701 void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause *C) {
6702 Record.AddStmt(C->getAlignment());
6703 Record.AddSourceLocation(C->getLParenLoc());
6706 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6707 Record.push_back(C->varlist_size());
6708 Record.AddSourceLocation(C->getLParenLoc());
6709 for (auto *VE : C->varlists()) {
6710 Record.AddStmt(VE);
6712 for (auto *VE : C->private_copies()) {
6713 Record.AddStmt(VE);
6717 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6718 Record.push_back(C->varlist_size());
6719 VisitOMPClauseWithPreInit(C);
6720 Record.AddSourceLocation(C->getLParenLoc());
6721 for (auto *VE : C->varlists()) {
6722 Record.AddStmt(VE);
6724 for (auto *VE : C->private_copies()) {
6725 Record.AddStmt(VE);
6727 for (auto *VE : C->inits()) {
6728 Record.AddStmt(VE);
6732 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6733 Record.push_back(C->varlist_size());
6734 VisitOMPClauseWithPostUpdate(C);
6735 Record.AddSourceLocation(C->getLParenLoc());
6736 Record.writeEnum(C->getKind());
6737 Record.AddSourceLocation(C->getKindLoc());
6738 Record.AddSourceLocation(C->getColonLoc());
6739 for (auto *VE : C->varlists())
6740 Record.AddStmt(VE);
6741 for (auto *E : C->private_copies())
6742 Record.AddStmt(E);
6743 for (auto *E : C->source_exprs())
6744 Record.AddStmt(E);
6745 for (auto *E : C->destination_exprs())
6746 Record.AddStmt(E);
6747 for (auto *E : C->assignment_ops())
6748 Record.AddStmt(E);
6751 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6752 Record.push_back(C->varlist_size());
6753 Record.AddSourceLocation(C->getLParenLoc());
6754 for (auto *VE : C->varlists())
6755 Record.AddStmt(VE);
6758 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6759 Record.push_back(C->varlist_size());
6760 Record.writeEnum(C->getModifier());
6761 VisitOMPClauseWithPostUpdate(C);
6762 Record.AddSourceLocation(C->getLParenLoc());
6763 Record.AddSourceLocation(C->getModifierLoc());
6764 Record.AddSourceLocation(C->getColonLoc());
6765 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6766 Record.AddDeclarationNameInfo(C->getNameInfo());
6767 for (auto *VE : C->varlists())
6768 Record.AddStmt(VE);
6769 for (auto *VE : C->privates())
6770 Record.AddStmt(VE);
6771 for (auto *E : C->lhs_exprs())
6772 Record.AddStmt(E);
6773 for (auto *E : C->rhs_exprs())
6774 Record.AddStmt(E);
6775 for (auto *E : C->reduction_ops())
6776 Record.AddStmt(E);
6777 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
6778 for (auto *E : C->copy_ops())
6779 Record.AddStmt(E);
6780 for (auto *E : C->copy_array_temps())
6781 Record.AddStmt(E);
6782 for (auto *E : C->copy_array_elems())
6783 Record.AddStmt(E);
6787 void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6788 Record.push_back(C->varlist_size());
6789 VisitOMPClauseWithPostUpdate(C);
6790 Record.AddSourceLocation(C->getLParenLoc());
6791 Record.AddSourceLocation(C->getColonLoc());
6792 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6793 Record.AddDeclarationNameInfo(C->getNameInfo());
6794 for (auto *VE : C->varlists())
6795 Record.AddStmt(VE);
6796 for (auto *VE : C->privates())
6797 Record.AddStmt(VE);
6798 for (auto *E : C->lhs_exprs())
6799 Record.AddStmt(E);
6800 for (auto *E : C->rhs_exprs())
6801 Record.AddStmt(E);
6802 for (auto *E : C->reduction_ops())
6803 Record.AddStmt(E);
6806 void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6807 Record.push_back(C->varlist_size());
6808 VisitOMPClauseWithPostUpdate(C);
6809 Record.AddSourceLocation(C->getLParenLoc());
6810 Record.AddSourceLocation(C->getColonLoc());
6811 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6812 Record.AddDeclarationNameInfo(C->getNameInfo());
6813 for (auto *VE : C->varlists())
6814 Record.AddStmt(VE);
6815 for (auto *VE : C->privates())
6816 Record.AddStmt(VE);
6817 for (auto *E : C->lhs_exprs())
6818 Record.AddStmt(E);
6819 for (auto *E : C->rhs_exprs())
6820 Record.AddStmt(E);
6821 for (auto *E : C->reduction_ops())
6822 Record.AddStmt(E);
6823 for (auto *E : C->taskgroup_descriptors())
6824 Record.AddStmt(E);
6827 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6828 Record.push_back(C->varlist_size());
6829 VisitOMPClauseWithPostUpdate(C);
6830 Record.AddSourceLocation(C->getLParenLoc());
6831 Record.AddSourceLocation(C->getColonLoc());
6832 Record.push_back(C->getModifier());
6833 Record.AddSourceLocation(C->getModifierLoc());
6834 for (auto *VE : C->varlists()) {
6835 Record.AddStmt(VE);
6837 for (auto *VE : C->privates()) {
6838 Record.AddStmt(VE);
6840 for (auto *VE : C->inits()) {
6841 Record.AddStmt(VE);
6843 for (auto *VE : C->updates()) {
6844 Record.AddStmt(VE);
6846 for (auto *VE : C->finals()) {
6847 Record.AddStmt(VE);
6849 Record.AddStmt(C->getStep());
6850 Record.AddStmt(C->getCalcStep());
6851 for (auto *VE : C->used_expressions())
6852 Record.AddStmt(VE);
6855 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6856 Record.push_back(C->varlist_size());
6857 Record.AddSourceLocation(C->getLParenLoc());
6858 Record.AddSourceLocation(C->getColonLoc());
6859 for (auto *VE : C->varlists())
6860 Record.AddStmt(VE);
6861 Record.AddStmt(C->getAlignment());
6864 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6865 Record.push_back(C->varlist_size());
6866 Record.AddSourceLocation(C->getLParenLoc());
6867 for (auto *VE : C->varlists())
6868 Record.AddStmt(VE);
6869 for (auto *E : C->source_exprs())
6870 Record.AddStmt(E);
6871 for (auto *E : C->destination_exprs())
6872 Record.AddStmt(E);
6873 for (auto *E : C->assignment_ops())
6874 Record.AddStmt(E);
6877 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6878 Record.push_back(C->varlist_size());
6879 Record.AddSourceLocation(C->getLParenLoc());
6880 for (auto *VE : C->varlists())
6881 Record.AddStmt(VE);
6882 for (auto *E : C->source_exprs())
6883 Record.AddStmt(E);
6884 for (auto *E : C->destination_exprs())
6885 Record.AddStmt(E);
6886 for (auto *E : C->assignment_ops())
6887 Record.AddStmt(E);
6890 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6891 Record.push_back(C->varlist_size());
6892 Record.AddSourceLocation(C->getLParenLoc());
6893 for (auto *VE : C->varlists())
6894 Record.AddStmt(VE);
6897 void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
6898 Record.AddStmt(C->getDepobj());
6899 Record.AddSourceLocation(C->getLParenLoc());
6902 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6903 Record.push_back(C->varlist_size());
6904 Record.push_back(C->getNumLoops());
6905 Record.AddSourceLocation(C->getLParenLoc());
6906 Record.AddStmt(C->getModifier());
6907 Record.push_back(C->getDependencyKind());
6908 Record.AddSourceLocation(C->getDependencyLoc());
6909 Record.AddSourceLocation(C->getColonLoc());
6910 Record.AddSourceLocation(C->getOmpAllMemoryLoc());
6911 for (auto *VE : C->varlists())
6912 Record.AddStmt(VE);
6913 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6914 Record.AddStmt(C->getLoopData(I));
6917 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6918 VisitOMPClauseWithPreInit(C);
6919 Record.writeEnum(C->getModifier());
6920 Record.AddStmt(C->getDevice());
6921 Record.AddSourceLocation(C->getModifierLoc());
6922 Record.AddSourceLocation(C->getLParenLoc());
6925 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6926 Record.push_back(C->varlist_size());
6927 Record.push_back(C->getUniqueDeclarationsNum());
6928 Record.push_back(C->getTotalComponentListNum());
6929 Record.push_back(C->getTotalComponentsNum());
6930 Record.AddSourceLocation(C->getLParenLoc());
6931 bool HasIteratorModifier = false;
6932 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
6933 Record.push_back(C->getMapTypeModifier(I));
6934 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6935 if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)
6936 HasIteratorModifier = true;
6938 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6939 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
6940 Record.push_back(C->getMapType());
6941 Record.AddSourceLocation(C->getMapLoc());
6942 Record.AddSourceLocation(C->getColonLoc());
6943 for (auto *E : C->varlists())
6944 Record.AddStmt(E);
6945 for (auto *E : C->mapperlists())
6946 Record.AddStmt(E);
6947 if (HasIteratorModifier)
6948 Record.AddStmt(C->getIteratorModifier());
6949 for (auto *D : C->all_decls())
6950 Record.AddDeclRef(D);
6951 for (auto N : C->all_num_lists())
6952 Record.push_back(N);
6953 for (auto N : C->all_lists_sizes())
6954 Record.push_back(N);
6955 for (auto &M : C->all_components()) {
6956 Record.AddStmt(M.getAssociatedExpression());
6957 Record.AddDeclRef(M.getAssociatedDeclaration());
6961 void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6962 Record.push_back(C->varlist_size());
6963 Record.AddSourceLocation(C->getLParenLoc());
6964 Record.AddSourceLocation(C->getColonLoc());
6965 Record.AddStmt(C->getAllocator());
6966 for (auto *VE : C->varlists())
6967 Record.AddStmt(VE);
6970 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6971 VisitOMPClauseWithPreInit(C);
6972 Record.AddStmt(C->getNumTeams());
6973 Record.AddSourceLocation(C->getLParenLoc());
6976 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6977 VisitOMPClauseWithPreInit(C);
6978 Record.AddStmt(C->getThreadLimit());
6979 Record.AddSourceLocation(C->getLParenLoc());
6982 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
6983 VisitOMPClauseWithPreInit(C);
6984 Record.AddStmt(C->getPriority());
6985 Record.AddSourceLocation(C->getLParenLoc());
6988 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
6989 VisitOMPClauseWithPreInit(C);
6990 Record.writeEnum(C->getModifier());
6991 Record.AddStmt(C->getGrainsize());
6992 Record.AddSourceLocation(C->getModifierLoc());
6993 Record.AddSourceLocation(C->getLParenLoc());
6996 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
6997 VisitOMPClauseWithPreInit(C);
6998 Record.writeEnum(C->getModifier());
6999 Record.AddStmt(C->getNumTasks());
7000 Record.AddSourceLocation(C->getModifierLoc());
7001 Record.AddSourceLocation(C->getLParenLoc());
7004 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
7005 Record.AddStmt(C->getHint());
7006 Record.AddSourceLocation(C->getLParenLoc());
7009 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
7010 VisitOMPClauseWithPreInit(C);
7011 Record.push_back(C->getDistScheduleKind());
7012 Record.AddStmt(C->getChunkSize());
7013 Record.AddSourceLocation(C->getLParenLoc());
7014 Record.AddSourceLocation(C->getDistScheduleKindLoc());
7015 Record.AddSourceLocation(C->getCommaLoc());
7018 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
7019 Record.push_back(C->getDefaultmapKind());
7020 Record.push_back(C->getDefaultmapModifier());
7021 Record.AddSourceLocation(C->getLParenLoc());
7022 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
7023 Record.AddSourceLocation(C->getDefaultmapKindLoc());
7026 void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
7027 Record.push_back(C->varlist_size());
7028 Record.push_back(C->getUniqueDeclarationsNum());
7029 Record.push_back(C->getTotalComponentListNum());
7030 Record.push_back(C->getTotalComponentsNum());
7031 Record.AddSourceLocation(C->getLParenLoc());
7032 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7033 Record.push_back(C->getMotionModifier(I));
7034 Record.AddSourceLocation(C->getMotionModifierLoc(I));
7036 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7037 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7038 Record.AddSourceLocation(C->getColonLoc());
7039 for (auto *E : C->varlists())
7040 Record.AddStmt(E);
7041 for (auto *E : C->mapperlists())
7042 Record.AddStmt(E);
7043 for (auto *D : C->all_decls())
7044 Record.AddDeclRef(D);
7045 for (auto N : C->all_num_lists())
7046 Record.push_back(N);
7047 for (auto N : C->all_lists_sizes())
7048 Record.push_back(N);
7049 for (auto &M : C->all_components()) {
7050 Record.AddStmt(M.getAssociatedExpression());
7051 Record.writeBool(M.isNonContiguous());
7052 Record.AddDeclRef(M.getAssociatedDeclaration());
7056 void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
7057 Record.push_back(C->varlist_size());
7058 Record.push_back(C->getUniqueDeclarationsNum());
7059 Record.push_back(C->getTotalComponentListNum());
7060 Record.push_back(C->getTotalComponentsNum());
7061 Record.AddSourceLocation(C->getLParenLoc());
7062 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7063 Record.push_back(C->getMotionModifier(I));
7064 Record.AddSourceLocation(C->getMotionModifierLoc(I));
7066 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7067 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7068 Record.AddSourceLocation(C->getColonLoc());
7069 for (auto *E : C->varlists())
7070 Record.AddStmt(E);
7071 for (auto *E : C->mapperlists())
7072 Record.AddStmt(E);
7073 for (auto *D : C->all_decls())
7074 Record.AddDeclRef(D);
7075 for (auto N : C->all_num_lists())
7076 Record.push_back(N);
7077 for (auto N : C->all_lists_sizes())
7078 Record.push_back(N);
7079 for (auto &M : C->all_components()) {
7080 Record.AddStmt(M.getAssociatedExpression());
7081 Record.writeBool(M.isNonContiguous());
7082 Record.AddDeclRef(M.getAssociatedDeclaration());
7086 void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
7087 Record.push_back(C->varlist_size());
7088 Record.push_back(C->getUniqueDeclarationsNum());
7089 Record.push_back(C->getTotalComponentListNum());
7090 Record.push_back(C->getTotalComponentsNum());
7091 Record.AddSourceLocation(C->getLParenLoc());
7092 for (auto *E : C->varlists())
7093 Record.AddStmt(E);
7094 for (auto *VE : C->private_copies())
7095 Record.AddStmt(VE);
7096 for (auto *VE : C->inits())
7097 Record.AddStmt(VE);
7098 for (auto *D : C->all_decls())
7099 Record.AddDeclRef(D);
7100 for (auto N : C->all_num_lists())
7101 Record.push_back(N);
7102 for (auto N : C->all_lists_sizes())
7103 Record.push_back(N);
7104 for (auto &M : C->all_components()) {
7105 Record.AddStmt(M.getAssociatedExpression());
7106 Record.AddDeclRef(M.getAssociatedDeclaration());
7110 void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
7111 Record.push_back(C->varlist_size());
7112 Record.push_back(C->getUniqueDeclarationsNum());
7113 Record.push_back(C->getTotalComponentListNum());
7114 Record.push_back(C->getTotalComponentsNum());
7115 Record.AddSourceLocation(C->getLParenLoc());
7116 for (auto *E : C->varlists())
7117 Record.AddStmt(E);
7118 for (auto *D : C->all_decls())
7119 Record.AddDeclRef(D);
7120 for (auto N : C->all_num_lists())
7121 Record.push_back(N);
7122 for (auto N : C->all_lists_sizes())
7123 Record.push_back(N);
7124 for (auto &M : C->all_components()) {
7125 Record.AddStmt(M.getAssociatedExpression());
7126 Record.AddDeclRef(M.getAssociatedDeclaration());
7130 void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
7131 Record.push_back(C->varlist_size());
7132 Record.push_back(C->getUniqueDeclarationsNum());
7133 Record.push_back(C->getTotalComponentListNum());
7134 Record.push_back(C->getTotalComponentsNum());
7135 Record.AddSourceLocation(C->getLParenLoc());
7136 for (auto *E : C->varlists())
7137 Record.AddStmt(E);
7138 for (auto *D : C->all_decls())
7139 Record.AddDeclRef(D);
7140 for (auto N : C->all_num_lists())
7141 Record.push_back(N);
7142 for (auto N : C->all_lists_sizes())
7143 Record.push_back(N);
7144 for (auto &M : C->all_components()) {
7145 Record.AddStmt(M.getAssociatedExpression());
7146 Record.AddDeclRef(M.getAssociatedDeclaration());
7150 void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
7151 Record.push_back(C->varlist_size());
7152 Record.push_back(C->getUniqueDeclarationsNum());
7153 Record.push_back(C->getTotalComponentListNum());
7154 Record.push_back(C->getTotalComponentsNum());
7155 Record.AddSourceLocation(C->getLParenLoc());
7156 for (auto *E : C->varlists())
7157 Record.AddStmt(E);
7158 for (auto *D : C->all_decls())
7159 Record.AddDeclRef(D);
7160 for (auto N : C->all_num_lists())
7161 Record.push_back(N);
7162 for (auto N : C->all_lists_sizes())
7163 Record.push_back(N);
7164 for (auto &M : C->all_components()) {
7165 Record.AddStmt(M.getAssociatedExpression());
7166 Record.AddDeclRef(M.getAssociatedDeclaration());
7170 void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
7172 void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7173 OMPUnifiedSharedMemoryClause *) {}
7175 void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
7177 void
7178 OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
7181 void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7182 OMPAtomicDefaultMemOrderClause *C) {
7183 Record.push_back(C->getAtomicDefaultMemOrderKind());
7184 Record.AddSourceLocation(C->getLParenLoc());
7185 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
7188 void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
7189 Record.push_back(C->getAtKind());
7190 Record.AddSourceLocation(C->getLParenLoc());
7191 Record.AddSourceLocation(C->getAtKindKwLoc());
7194 void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
7195 Record.push_back(C->getSeverityKind());
7196 Record.AddSourceLocation(C->getLParenLoc());
7197 Record.AddSourceLocation(C->getSeverityKindKwLoc());
7200 void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
7201 Record.AddStmt(C->getMessageString());
7202 Record.AddSourceLocation(C->getLParenLoc());
7205 void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
7206 Record.push_back(C->varlist_size());
7207 Record.AddSourceLocation(C->getLParenLoc());
7208 for (auto *VE : C->varlists())
7209 Record.AddStmt(VE);
7210 for (auto *E : C->private_refs())
7211 Record.AddStmt(E);
7214 void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
7215 Record.push_back(C->varlist_size());
7216 Record.AddSourceLocation(C->getLParenLoc());
7217 for (auto *VE : C->varlists())
7218 Record.AddStmt(VE);
7221 void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
7222 Record.push_back(C->varlist_size());
7223 Record.AddSourceLocation(C->getLParenLoc());
7224 for (auto *VE : C->varlists())
7225 Record.AddStmt(VE);
7228 void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
7229 Record.writeEnum(C->getKind());
7230 Record.writeEnum(C->getModifier());
7231 Record.AddSourceLocation(C->getLParenLoc());
7232 Record.AddSourceLocation(C->getKindKwLoc());
7233 Record.AddSourceLocation(C->getModifierKwLoc());
7236 void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
7237 Record.push_back(C->getNumberOfAllocators());
7238 Record.AddSourceLocation(C->getLParenLoc());
7239 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
7240 OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
7241 Record.AddStmt(Data.Allocator);
7242 Record.AddStmt(Data.AllocatorTraits);
7243 Record.AddSourceLocation(Data.LParenLoc);
7244 Record.AddSourceLocation(Data.RParenLoc);
7248 void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
7249 Record.push_back(C->varlist_size());
7250 Record.AddSourceLocation(C->getLParenLoc());
7251 Record.AddStmt(C->getModifier());
7252 Record.AddSourceLocation(C->getColonLoc());
7253 for (Expr *E : C->varlists())
7254 Record.AddStmt(E);
7257 void OMPClauseWriter::VisitOMPBindClause(OMPBindClause *C) {
7258 Record.writeEnum(C->getBindKind());
7259 Record.AddSourceLocation(C->getLParenLoc());
7260 Record.AddSourceLocation(C->getBindKindLoc());
7263 void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
7264 VisitOMPClauseWithPreInit(C);
7265 Record.AddStmt(C->getSize());
7266 Record.AddSourceLocation(C->getLParenLoc());
7269 void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
7270 Record.push_back(C->varlist_size());
7271 Record.push_back(C->getNumLoops());
7272 Record.AddSourceLocation(C->getLParenLoc());
7273 Record.push_back(C->getDependenceType());
7274 Record.AddSourceLocation(C->getDependenceLoc());
7275 Record.AddSourceLocation(C->getColonLoc());
7276 for (auto *VE : C->varlists())
7277 Record.AddStmt(VE);
7278 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
7279 Record.AddStmt(C->getLoopData(I));
7282 void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
7283 Record.AddAttributes(C->getAttrs());
7284 Record.AddSourceLocation(C->getBeginLoc());
7285 Record.AddSourceLocation(C->getLParenLoc());
7286 Record.AddSourceLocation(C->getEndLoc());
7289 void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause *C) {}
7291 void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
7292 writeUInt32(TI->Sets.size());
7293 for (const auto &Set : TI->Sets) {
7294 writeEnum(Set.Kind);
7295 writeUInt32(Set.Selectors.size());
7296 for (const auto &Selector : Set.Selectors) {
7297 writeEnum(Selector.Kind);
7298 writeBool(Selector.ScoreOrCondition);
7299 if (Selector.ScoreOrCondition)
7300 writeExprRef(Selector.ScoreOrCondition);
7301 writeUInt32(Selector.Properties.size());
7302 for (const auto &Property : Selector.Properties)
7303 writeEnum(Property.Kind);
7308 void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
7309 if (!Data)
7310 return;
7311 writeUInt32(Data->getNumClauses());
7312 writeUInt32(Data->getNumChildren());
7313 writeBool(Data->hasAssociatedStmt());
7314 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
7315 writeOMPClause(Data->getClauses()[I]);
7316 if (Data->hasAssociatedStmt())
7317 AddStmt(Data->getAssociatedStmt());
7318 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
7319 AddStmt(Data->getChildren()[I]);