[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / Serialization / ASTWriter.cpp
blobab70594607530f41a3f7d0339a45f624019354ea
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 // Diagnostic options.
1216 const auto &Diags = Context.getDiagnostics();
1217 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1218 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1219 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1220 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1221 #include "clang/Basic/DiagnosticOptions.def"
1222 Record.push_back(DiagOpts.Warnings.size());
1223 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1224 AddString(DiagOpts.Warnings[I], Record);
1225 Record.push_back(DiagOpts.Remarks.size());
1226 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1227 AddString(DiagOpts.Remarks[I], Record);
1228 // Note: we don't serialize the log or serialization file names, because they
1229 // are generally transient files and will almost always be overridden.
1230 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1231 Record.clear();
1233 // Header search paths.
1234 Record.clear();
1235 const HeaderSearchOptions &HSOpts =
1236 PP.getHeaderSearchInfo().getHeaderSearchOpts();
1238 // Include entries.
1239 Record.push_back(HSOpts.UserEntries.size());
1240 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1241 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1242 AddString(Entry.Path, Record);
1243 Record.push_back(static_cast<unsigned>(Entry.Group));
1244 Record.push_back(Entry.IsFramework);
1245 Record.push_back(Entry.IgnoreSysRoot);
1248 // System header prefixes.
1249 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1250 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1251 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1252 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1255 // VFS overlay files.
1256 Record.push_back(HSOpts.VFSOverlayFiles.size());
1257 for (StringRef VFSOverlayFile : HSOpts.VFSOverlayFiles)
1258 AddString(VFSOverlayFile, Record);
1260 Stream.EmitRecord(HEADER_SEARCH_PATHS, Record);
1262 // Write out the diagnostic/pragma mappings.
1263 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1265 // Header search entry usage.
1266 auto HSEntryUsage = PP.getHeaderSearchInfo().computeUserEntryUsage();
1267 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1268 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_ENTRY_USAGE));
1269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Number of bits.
1270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Bit vector.
1271 unsigned HSUsageAbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1273 RecordData::value_type Record[] = {HEADER_SEARCH_ENTRY_USAGE,
1274 HSEntryUsage.size()};
1275 Stream.EmitRecordWithBlob(HSUsageAbbrevCode, Record, bytes(HSEntryUsage));
1278 // Leave the options block.
1279 Stream.ExitBlock();
1280 UnhashedControlBlockRange.second = Stream.GetCurrentBitNo() >> 3;
1283 /// Write the control block.
1284 void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1285 StringRef isysroot) {
1286 using namespace llvm;
1288 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1289 RecordData Record;
1291 // Metadata
1292 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1293 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1294 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1295 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1296 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1297 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1298 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1299 // Standard C++ module
1300 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1301 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1302 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1303 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1304 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1305 assert((!WritingModule || isysroot.empty()) &&
1306 "writing module as a relocatable PCH?");
1308 RecordData::value_type Record[] = {METADATA,
1309 VERSION_MAJOR,
1310 VERSION_MINOR,
1311 CLANG_VERSION_MAJOR,
1312 CLANG_VERSION_MINOR,
1313 !isysroot.empty(),
1314 isWritingStdCXXNamedModules(),
1315 IncludeTimestamps,
1316 ASTHasCompilerErrors};
1317 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1318 getClangFullRepositoryVersion());
1321 if (WritingModule) {
1322 // Module name
1323 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1324 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1326 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1327 RecordData::value_type Record[] = {MODULE_NAME};
1328 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1331 if (WritingModule && WritingModule->Directory) {
1332 SmallString<128> BaseDir;
1333 if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1334 // Use the current working directory as the base path for all inputs.
1335 auto CWD =
1336 Context.getSourceManager().getFileManager().getOptionalDirectoryRef(
1337 ".");
1338 BaseDir.assign(CWD->getName());
1339 } else {
1340 BaseDir.assign(WritingModule->Directory->getName());
1342 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1344 // If the home of the module is the current working directory, then we
1345 // want to pick up the cwd of the build process loading the module, not
1346 // our cwd, when we load this module.
1347 if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
1348 (!PP.getHeaderSearchInfo()
1349 .getHeaderSearchOpts()
1350 .ModuleMapFileHomeIsCwd ||
1351 WritingModule->Directory->getName() != StringRef("."))) {
1352 // Module directory.
1353 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1354 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1356 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1358 RecordData::value_type Record[] = {MODULE_DIRECTORY};
1359 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1362 // Write out all other paths relative to the base directory if possible.
1363 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1364 } else if (!isysroot.empty()) {
1365 // Write out paths relative to the sysroot if possible.
1366 BaseDirectory = std::string(isysroot);
1369 // Module map file
1370 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1371 Record.clear();
1373 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1374 AddPath(WritingModule->PresumedModuleMapFile.empty()
1375 ? Map.getModuleMapFileForUniquing(WritingModule)
1376 ->getNameAsRequested()
1377 : StringRef(WritingModule->PresumedModuleMapFile),
1378 Record);
1380 // Additional module map files.
1381 if (auto *AdditionalModMaps =
1382 Map.getAdditionalModuleMapFiles(WritingModule)) {
1383 Record.push_back(AdditionalModMaps->size());
1384 SmallVector<FileEntryRef, 1> ModMaps(AdditionalModMaps->begin(),
1385 AdditionalModMaps->end());
1386 llvm::sort(ModMaps, [](FileEntryRef A, FileEntryRef B) {
1387 return A.getName() < B.getName();
1389 for (FileEntryRef F : ModMaps)
1390 AddPath(F.getName(), Record);
1391 } else {
1392 Record.push_back(0);
1395 Stream.EmitRecord(MODULE_MAP_FILE, Record);
1398 // Imports
1399 if (Chain) {
1400 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1401 Record.clear();
1403 for (ModuleFile &M : Mgr) {
1404 // Skip modules that weren't directly imported.
1405 if (!M.isDirectlyImported())
1406 continue;
1408 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1409 Record.push_back(M.StandardCXXModule);
1410 AddSourceLocation(M.ImportLoc, Record);
1412 // If we have calculated signature, there is no need to store
1413 // the size or timestamp.
1414 Record.push_back(M.Signature ? 0 : M.File->getSize());
1415 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1417 llvm::append_range(Record, M.Signature);
1419 AddString(M.ModuleName, Record);
1420 AddPath(M.FileName, Record);
1422 Stream.EmitRecord(IMPORTS, Record);
1425 // Write the options block.
1426 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1428 // Language options.
1429 Record.clear();
1430 const LangOptions &LangOpts = Context.getLangOpts();
1431 #define LANGOPT(Name, Bits, Default, Description) \
1432 Record.push_back(LangOpts.Name);
1433 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1434 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1435 #include "clang/Basic/LangOptions.def"
1436 #define SANITIZER(NAME, ID) \
1437 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1438 #include "clang/Basic/Sanitizers.def"
1440 Record.push_back(LangOpts.ModuleFeatures.size());
1441 for (StringRef Feature : LangOpts.ModuleFeatures)
1442 AddString(Feature, Record);
1444 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1445 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1447 AddString(LangOpts.CurrentModule, Record);
1449 // Comment options.
1450 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1451 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1452 AddString(I, Record);
1454 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1456 // OpenMP offloading options.
1457 Record.push_back(LangOpts.OMPTargetTriples.size());
1458 for (auto &T : LangOpts.OMPTargetTriples)
1459 AddString(T.getTriple(), Record);
1461 AddString(LangOpts.OMPHostIRFile, Record);
1463 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1465 // Target options.
1466 Record.clear();
1467 const TargetInfo &Target = Context.getTargetInfo();
1468 const TargetOptions &TargetOpts = Target.getTargetOpts();
1469 AddString(TargetOpts.Triple, Record);
1470 AddString(TargetOpts.CPU, Record);
1471 AddString(TargetOpts.TuneCPU, Record);
1472 AddString(TargetOpts.ABI, Record);
1473 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1474 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1475 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1477 Record.push_back(TargetOpts.Features.size());
1478 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1479 AddString(TargetOpts.Features[I], Record);
1481 Stream.EmitRecord(TARGET_OPTIONS, Record);
1483 // File system options.
1484 Record.clear();
1485 const FileSystemOptions &FSOpts =
1486 Context.getSourceManager().getFileManager().getFileSystemOpts();
1487 AddString(FSOpts.WorkingDir, Record);
1488 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1490 // Header search options.
1491 Record.clear();
1492 const HeaderSearchOptions &HSOpts =
1493 PP.getHeaderSearchInfo().getHeaderSearchOpts();
1495 AddString(HSOpts.Sysroot, Record);
1496 AddString(HSOpts.ResourceDir, Record);
1497 AddString(HSOpts.ModuleCachePath, Record);
1498 AddString(HSOpts.ModuleUserBuildPath, Record);
1499 Record.push_back(HSOpts.DisableModuleHash);
1500 Record.push_back(HSOpts.ImplicitModuleMaps);
1501 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1502 Record.push_back(HSOpts.EnablePrebuiltImplicitModules);
1503 Record.push_back(HSOpts.UseBuiltinIncludes);
1504 Record.push_back(HSOpts.UseStandardSystemIncludes);
1505 Record.push_back(HSOpts.UseStandardCXXIncludes);
1506 Record.push_back(HSOpts.UseLibcxx);
1507 // Write out the specific module cache path that contains the module files.
1508 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1509 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1511 // Preprocessor options.
1512 Record.clear();
1513 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1515 // If we're building an implicit module with a context hash, the importer is
1516 // guaranteed to have the same macros defined on the command line. Skip
1517 // writing them.
1518 bool SkipMacros = BuildingImplicitModule && !HSOpts.DisableModuleHash;
1519 bool WriteMacros = !SkipMacros;
1520 Record.push_back(WriteMacros);
1521 if (WriteMacros) {
1522 // Macro definitions.
1523 Record.push_back(PPOpts.Macros.size());
1524 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1525 AddString(PPOpts.Macros[I].first, Record);
1526 Record.push_back(PPOpts.Macros[I].second);
1530 // Includes
1531 Record.push_back(PPOpts.Includes.size());
1532 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1533 AddString(PPOpts.Includes[I], Record);
1535 // Macro includes
1536 Record.push_back(PPOpts.MacroIncludes.size());
1537 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1538 AddString(PPOpts.MacroIncludes[I], Record);
1540 Record.push_back(PPOpts.UsePredefines);
1541 // Detailed record is important since it is used for the module cache hash.
1542 Record.push_back(PPOpts.DetailedRecord);
1543 AddString(PPOpts.ImplicitPCHInclude, Record);
1544 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1545 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1547 // Leave the options block.
1548 Stream.ExitBlock();
1550 // Original file name and file ID
1551 SourceManager &SM = Context.getSourceManager();
1552 if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID())) {
1553 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1554 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1555 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1556 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1557 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1559 Record.clear();
1560 Record.push_back(ORIGINAL_FILE);
1561 AddFileID(SM.getMainFileID(), Record);
1562 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1565 Record.clear();
1566 AddFileID(SM.getMainFileID(), Record);
1567 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1569 WriteInputFiles(Context.SourceMgr,
1570 PP.getHeaderSearchInfo().getHeaderSearchOpts());
1571 Stream.ExitBlock();
1574 namespace {
1576 /// An input file.
1577 struct InputFileEntry {
1578 FileEntryRef File;
1579 bool IsSystemFile;
1580 bool IsTransient;
1581 bool BufferOverridden;
1582 bool IsTopLevel;
1583 bool IsModuleMap;
1584 uint32_t ContentHash[2];
1586 InputFileEntry(FileEntryRef File) : File(File) {}
1589 } // namespace
1591 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1592 HeaderSearchOptions &HSOpts) {
1593 using namespace llvm;
1595 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1597 // Create input-file abbreviation.
1598 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1599 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1600 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1601 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1602 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1603 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1604 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1605 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
1606 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1607 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1608 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
1609 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1611 // Create input file hash abbreviation.
1612 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1613 IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1614 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1615 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1616 unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1618 uint64_t InputFilesOffsetBase = Stream.GetCurrentBitNo();
1620 // Get all ContentCache objects for files.
1621 std::vector<InputFileEntry> UserFiles;
1622 std::vector<InputFileEntry> SystemFiles;
1623 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1624 // Get this source location entry.
1625 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1626 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1628 // We only care about file entries that were not overridden.
1629 if (!SLoc->isFile())
1630 continue;
1631 const SrcMgr::FileInfo &File = SLoc->getFile();
1632 const SrcMgr::ContentCache *Cache = &File.getContentCache();
1633 if (!Cache->OrigEntry)
1634 continue;
1636 // Do not emit input files that do not affect current module.
1637 if (!IsSLocAffecting[I])
1638 continue;
1640 InputFileEntry Entry(*Cache->OrigEntry);
1641 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1642 Entry.IsTransient = Cache->IsTransient;
1643 Entry.BufferOverridden = Cache->BufferOverridden;
1644 Entry.IsTopLevel = File.getIncludeLoc().isInvalid();
1645 Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
1647 auto ContentHash = hash_code(-1);
1648 if (PP->getHeaderSearchInfo()
1649 .getHeaderSearchOpts()
1650 .ValidateASTInputFilesContent) {
1651 auto MemBuff = Cache->getBufferIfLoaded();
1652 if (MemBuff)
1653 ContentHash = hash_value(MemBuff->getBuffer());
1654 else
1655 PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1656 << Entry.File.getName();
1658 auto CH = llvm::APInt(64, ContentHash);
1659 Entry.ContentHash[0] =
1660 static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1661 Entry.ContentHash[1] =
1662 static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1664 if (Entry.IsSystemFile)
1665 SystemFiles.push_back(Entry);
1666 else
1667 UserFiles.push_back(Entry);
1670 // User files go at the front, system files at the back.
1671 auto SortedFiles = llvm::concat<InputFileEntry>(std::move(UserFiles),
1672 std::move(SystemFiles));
1674 unsigned UserFilesNum = 0;
1675 // Write out all of the input files.
1676 std::vector<uint64_t> InputFileOffsets;
1677 for (const auto &Entry : SortedFiles) {
1678 uint32_t &InputFileID = InputFileIDs[Entry.File];
1679 if (InputFileID != 0)
1680 continue; // already recorded this file.
1682 // Record this entry's offset.
1683 InputFileOffsets.push_back(Stream.GetCurrentBitNo() - InputFilesOffsetBase);
1685 InputFileID = InputFileOffsets.size();
1687 if (!Entry.IsSystemFile)
1688 ++UserFilesNum;
1690 // Emit size/modification time for this file.
1691 // And whether this file was overridden.
1693 SmallString<128> NameAsRequested = Entry.File.getNameAsRequested();
1694 SmallString<128> Name = Entry.File.getName();
1696 PreparePathForOutput(NameAsRequested);
1697 PreparePathForOutput(Name);
1699 if (Name == NameAsRequested)
1700 Name.clear();
1702 RecordData::value_type Record[] = {
1703 INPUT_FILE,
1704 InputFileOffsets.size(),
1705 (uint64_t)Entry.File.getSize(),
1706 (uint64_t)getTimestampForOutput(Entry.File),
1707 Entry.BufferOverridden,
1708 Entry.IsTransient,
1709 Entry.IsTopLevel,
1710 Entry.IsModuleMap,
1711 NameAsRequested.size()};
1713 Stream.EmitRecordWithBlob(IFAbbrevCode, Record,
1714 (NameAsRequested + Name).str());
1717 // Emit content hash for this file.
1719 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1720 Entry.ContentHash[1]};
1721 Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1725 Stream.ExitBlock();
1727 // Create input file offsets abbreviation.
1728 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1729 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1730 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1731 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1732 // input files
1733 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1734 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1736 // Write input file offsets.
1737 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1738 InputFileOffsets.size(), UserFilesNum};
1739 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1742 //===----------------------------------------------------------------------===//
1743 // Source Manager Serialization
1744 //===----------------------------------------------------------------------===//
1746 /// Create an abbreviation for the SLocEntry that refers to a
1747 /// file.
1748 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1749 using namespace llvm;
1751 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1752 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1753 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1757 // FileEntry fields.
1758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1762 return Stream.EmitAbbrev(std::move(Abbrev));
1765 /// Create an abbreviation for the SLocEntry that refers to a
1766 /// buffer.
1767 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1768 using namespace llvm;
1770 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1771 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1777 return Stream.EmitAbbrev(std::move(Abbrev));
1780 /// Create an abbreviation for the SLocEntry that refers to a
1781 /// buffer's blob.
1782 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1783 bool Compressed) {
1784 using namespace llvm;
1786 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1787 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1788 : SM_SLOC_BUFFER_BLOB));
1789 if (Compressed)
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1792 return Stream.EmitAbbrev(std::move(Abbrev));
1795 /// Create an abbreviation for the SLocEntry that refers to a macro
1796 /// expansion.
1797 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1798 using namespace llvm;
1800 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1801 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1802 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1803 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Start location
1805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // End location
1806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1808 return Stream.EmitAbbrev(std::move(Abbrev));
1811 /// Emit key length and data length as ULEB-encoded data, and return them as a
1812 /// pair.
1813 static std::pair<unsigned, unsigned>
1814 emitULEBKeyDataLength(unsigned KeyLen, unsigned DataLen, raw_ostream &Out) {
1815 llvm::encodeULEB128(KeyLen, Out);
1816 llvm::encodeULEB128(DataLen, Out);
1817 return std::make_pair(KeyLen, DataLen);
1820 namespace {
1822 // Trait used for the on-disk hash table of header search information.
1823 class HeaderFileInfoTrait {
1824 ASTWriter &Writer;
1826 // Keep track of the framework names we've used during serialization.
1827 SmallString<128> FrameworkStringData;
1828 llvm::StringMap<unsigned> FrameworkNameOffset;
1830 public:
1831 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1833 struct key_type {
1834 StringRef Filename;
1835 off_t Size;
1836 time_t ModTime;
1838 using key_type_ref = const key_type &;
1840 using UnresolvedModule =
1841 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1843 struct data_type {
1844 const HeaderFileInfo &HFI;
1845 bool AlreadyIncluded;
1846 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1847 UnresolvedModule Unresolved;
1849 using data_type_ref = const data_type &;
1851 using hash_value_type = unsigned;
1852 using offset_type = unsigned;
1854 hash_value_type ComputeHash(key_type_ref key) {
1855 // The hash is based only on size/time of the file, so that the reader can
1856 // match even when symlinking or excess path elements ("foo/../", "../")
1857 // change the form of the name. However, complete path is still the key.
1858 return llvm::hash_combine(key.Size, key.ModTime);
1861 std::pair<unsigned, unsigned>
1862 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1863 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
1864 unsigned DataLen = 1 + 4 + 4;
1865 for (auto ModInfo : Data.KnownHeaders)
1866 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1867 DataLen += 4;
1868 if (Data.Unresolved.getPointer())
1869 DataLen += 4;
1870 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
1873 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1874 using namespace llvm::support;
1876 endian::Writer LE(Out, llvm::endianness::little);
1877 LE.write<uint64_t>(key.Size);
1878 KeyLen -= 8;
1879 LE.write<uint64_t>(key.ModTime);
1880 KeyLen -= 8;
1881 Out.write(key.Filename.data(), KeyLen);
1884 void EmitData(raw_ostream &Out, key_type_ref key,
1885 data_type_ref Data, unsigned DataLen) {
1886 using namespace llvm::support;
1888 endian::Writer LE(Out, llvm::endianness::little);
1889 uint64_t Start = Out.tell(); (void)Start;
1891 unsigned char Flags = (Data.AlreadyIncluded << 6)
1892 | (Data.HFI.isImport << 5)
1893 | (Data.HFI.isPragmaOnce << 4)
1894 | (Data.HFI.DirInfo << 1)
1895 | Data.HFI.IndexHeaderMapHeader;
1896 LE.write<uint8_t>(Flags);
1898 if (!Data.HFI.ControllingMacro)
1899 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
1900 else
1901 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1903 unsigned Offset = 0;
1904 if (!Data.HFI.Framework.empty()) {
1905 // If this header refers into a framework, save the framework name.
1906 llvm::StringMap<unsigned>::iterator Pos
1907 = FrameworkNameOffset.find(Data.HFI.Framework);
1908 if (Pos == FrameworkNameOffset.end()) {
1909 Offset = FrameworkStringData.size() + 1;
1910 FrameworkStringData.append(Data.HFI.Framework);
1911 FrameworkStringData.push_back(0);
1913 FrameworkNameOffset[Data.HFI.Framework] = Offset;
1914 } else
1915 Offset = Pos->second;
1917 LE.write<uint32_t>(Offset);
1919 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1920 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1921 uint32_t Value = (ModID << 3) | (unsigned)Role;
1922 assert((Value >> 3) == ModID && "overflow in header module info");
1923 LE.write<uint32_t>(Value);
1927 for (auto ModInfo : Data.KnownHeaders)
1928 EmitModule(ModInfo.getModule(), ModInfo.getRole());
1929 if (Data.Unresolved.getPointer())
1930 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
1932 assert(Out.tell() - Start == DataLen && "Wrong data length");
1935 const char *strings_begin() const { return FrameworkStringData.begin(); }
1936 const char *strings_end() const { return FrameworkStringData.end(); }
1939 } // namespace
1941 /// Write the header search block for the list of files that
1943 /// \param HS The header search structure to save.
1944 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1945 HeaderFileInfoTrait GeneratorTrait(*this);
1946 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1947 SmallVector<const char *, 4> SavedStrings;
1948 unsigned NumHeaderSearchEntries = 0;
1950 // Find all unresolved headers for the current module. We generally will
1951 // have resolved them before we get here, but not necessarily: we might be
1952 // compiling a preprocessed module, where there is no requirement for the
1953 // original files to exist any more.
1954 const HeaderFileInfo Empty; // So we can take a reference.
1955 if (WritingModule) {
1956 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1957 while (!Worklist.empty()) {
1958 Module *M = Worklist.pop_back_val();
1959 // We don't care about headers in unimportable submodules.
1960 if (M->isUnimportable())
1961 continue;
1963 // Map to disk files where possible, to pick up any missing stat
1964 // information. This also means we don't need to check the unresolved
1965 // headers list when emitting resolved headers in the first loop below.
1966 // FIXME: It'd be preferable to avoid doing this if we were given
1967 // sufficient stat information in the module map.
1968 HS.getModuleMap().resolveHeaderDirectives(M, /*File=*/std::nullopt);
1970 // If the file didn't exist, we can still create a module if we were given
1971 // enough information in the module map.
1972 for (const auto &U : M->MissingHeaders) {
1973 // Check that we were given enough information to build a module
1974 // without this file existing on disk.
1975 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1976 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1977 << WritingModule->getFullModuleName() << U.Size.has_value()
1978 << U.FileName;
1979 continue;
1982 // Form the effective relative pathname for the file.
1983 SmallString<128> Filename(M->Directory->getName());
1984 llvm::sys::path::append(Filename, U.FileName);
1985 PreparePathForOutput(Filename);
1987 StringRef FilenameDup = strdup(Filename.c_str());
1988 SavedStrings.push_back(FilenameDup.data());
1990 HeaderFileInfoTrait::key_type Key = {
1991 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0};
1992 HeaderFileInfoTrait::data_type Data = {
1993 Empty, false, {}, {M, ModuleMap::headerKindToRole(U.Kind)}};
1994 // FIXME: Deal with cases where there are multiple unresolved header
1995 // directives in different submodules for the same header.
1996 Generator.insert(Key, Data, GeneratorTrait);
1997 ++NumHeaderSearchEntries;
1999 auto SubmodulesRange = M->submodules();
2000 Worklist.append(SubmodulesRange.begin(), SubmodulesRange.end());
2004 SmallVector<OptionalFileEntryRef, 16> FilesByUID;
2005 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
2007 if (FilesByUID.size() > HS.header_file_size())
2008 FilesByUID.resize(HS.header_file_size());
2010 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2011 OptionalFileEntryRef File = FilesByUID[UID];
2012 if (!File)
2013 continue;
2015 // Get the file info. This will load info from the external source if
2016 // necessary. Skip emitting this file if we have no information on it
2017 // as a header file (in which case HFI will be null) or if it hasn't
2018 // changed since it was loaded. Also skip it if it's for a modular header
2019 // from a different module; in that case, we rely on the module(s)
2020 // containing the header to provide this information.
2021 const HeaderFileInfo *HFI =
2022 HS.getExistingFileInfo(*File, /*WantExternal*/!Chain);
2023 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
2024 continue;
2026 // Massage the file path into an appropriate form.
2027 StringRef Filename = File->getName();
2028 SmallString<128> FilenameTmp(Filename);
2029 if (PreparePathForOutput(FilenameTmp)) {
2030 // If we performed any translation on the file name at all, we need to
2031 // save this string, since the generator will refer to it later.
2032 Filename = StringRef(strdup(FilenameTmp.c_str()));
2033 SavedStrings.push_back(Filename.data());
2036 bool Included = PP->alreadyIncluded(*File);
2038 HeaderFileInfoTrait::key_type Key = {
2039 Filename, File->getSize(), getTimestampForOutput(*File)
2041 HeaderFileInfoTrait::data_type Data = {
2042 *HFI, Included, HS.getModuleMap().findResolvedModulesForHeader(*File), {}
2044 Generator.insert(Key, Data, GeneratorTrait);
2045 ++NumHeaderSearchEntries;
2048 // Create the on-disk hash table in a buffer.
2049 SmallString<4096> TableData;
2050 uint32_t BucketOffset;
2052 using namespace llvm::support;
2054 llvm::raw_svector_ostream Out(TableData);
2055 // Make sure that no bucket is at offset 0
2056 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
2057 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2060 // Create a blob abbreviation
2061 using namespace llvm;
2063 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2064 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2069 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2071 // Write the header search table
2072 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2073 NumHeaderSearchEntries, TableData.size()};
2074 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
2075 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
2077 // Free all of the strings we had to duplicate.
2078 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
2079 free(const_cast<char *>(SavedStrings[I]));
2082 static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2083 unsigned SLocBufferBlobCompressedAbbrv,
2084 unsigned SLocBufferBlobAbbrv) {
2085 using RecordDataType = ASTWriter::RecordData::value_type;
2087 // Compress the buffer if possible. We expect that almost all PCM
2088 // consumers will not want its contents.
2089 SmallVector<uint8_t, 0> CompressedBuffer;
2090 if (llvm::compression::zstd::isAvailable()) {
2091 llvm::compression::zstd::compress(
2092 llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer, 9);
2093 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2094 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2095 llvm::toStringRef(CompressedBuffer));
2096 return;
2098 if (llvm::compression::zlib::isAvailable()) {
2099 llvm::compression::zlib::compress(
2100 llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer);
2101 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
2102 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2103 llvm::toStringRef(CompressedBuffer));
2104 return;
2107 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2108 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2111 /// Writes the block containing the serialized form of the
2112 /// source manager.
2114 /// TODO: We should probably use an on-disk hash table (stored in a
2115 /// blob), indexed based on the file name, so that we only create
2116 /// entries for files that we actually need. In the common case (no
2117 /// errors), we probably won't have to create file entries for any of
2118 /// the files in the AST.
2119 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
2120 const Preprocessor &PP) {
2121 RecordData Record;
2123 // Enter the source manager block.
2124 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
2125 const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
2127 // Abbreviations for the various kinds of source-location entries.
2128 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2129 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
2130 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2131 unsigned SLocBufferBlobCompressedAbbrv =
2132 CreateSLocBufferBlobAbbrev(Stream, true);
2133 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
2135 // Write out the source location entry table. We skip the first
2136 // entry, which is always the same dummy entry.
2137 std::vector<uint32_t> SLocEntryOffsets;
2138 uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
2139 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2140 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
2141 I != N; ++I) {
2142 // Get this source location entry.
2143 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
2144 FileID FID = FileID::get(I);
2145 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
2147 // Record the offset of this source-location entry.
2148 uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
2149 assert((Offset >> 32) == 0 && "SLocEntry offset too large");
2151 // Figure out which record code to use.
2152 unsigned Code;
2153 if (SLoc->isFile()) {
2154 const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
2155 if (Cache->OrigEntry) {
2156 Code = SM_SLOC_FILE_ENTRY;
2157 } else
2158 Code = SM_SLOC_BUFFER_ENTRY;
2159 } else
2160 Code = SM_SLOC_EXPANSION_ENTRY;
2161 Record.clear();
2162 Record.push_back(Code);
2164 if (SLoc->isFile()) {
2165 const SrcMgr::FileInfo &File = SLoc->getFile();
2166 const SrcMgr::ContentCache *Content = &File.getContentCache();
2167 // Do not emit files that were not listed as inputs.
2168 if (!IsSLocAffecting[I])
2169 continue;
2170 SLocEntryOffsets.push_back(Offset);
2171 // Starting offset of this entry within this module, so skip the dummy.
2172 Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2173 AddSourceLocation(File.getIncludeLoc(), Record);
2174 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2175 Record.push_back(File.hasLineDirectives());
2177 bool EmitBlob = false;
2178 if (Content->OrigEntry) {
2179 assert(Content->OrigEntry == Content->ContentsEntry &&
2180 "Writing to AST an overridden file is not supported");
2182 // The source location entry is a file. Emit input file ID.
2183 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2184 Record.push_back(InputFileIDs[Content->OrigEntry]);
2186 Record.push_back(getAdjustedNumCreatedFIDs(FID));
2188 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
2189 if (FDI != FileDeclIDs.end()) {
2190 Record.push_back(FDI->second->FirstDeclIndex);
2191 Record.push_back(FDI->second->DeclIDs.size());
2192 } else {
2193 Record.push_back(0);
2194 Record.push_back(0);
2197 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2199 if (Content->BufferOverridden || Content->IsTransient)
2200 EmitBlob = true;
2201 } else {
2202 // The source location entry is a buffer. The blob associated
2203 // with this entry contains the contents of the buffer.
2205 // We add one to the size so that we capture the trailing NULL
2206 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2207 // the reader side).
2208 std::optional<llvm::MemoryBufferRef> Buffer =
2209 Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2210 StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2211 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
2212 StringRef(Name.data(), Name.size() + 1));
2213 EmitBlob = true;
2216 if (EmitBlob) {
2217 // Include the implicit terminating null character in the on-disk buffer
2218 // if we're writing it uncompressed.
2219 std::optional<llvm::MemoryBufferRef> Buffer =
2220 Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2221 if (!Buffer)
2222 Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2223 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2224 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2225 SLocBufferBlobAbbrv);
2227 } else {
2228 // The source location entry is a macro expansion.
2229 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2230 SLocEntryOffsets.push_back(Offset);
2231 // Starting offset of this entry within this module, so skip the dummy.
2232 Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
2233 LocSeq::State Seq;
2234 AddSourceLocation(Expansion.getSpellingLoc(), Record, Seq);
2235 AddSourceLocation(Expansion.getExpansionLocStart(), Record, Seq);
2236 AddSourceLocation(Expansion.isMacroArgExpansion()
2237 ? SourceLocation()
2238 : Expansion.getExpansionLocEnd(),
2239 Record, Seq);
2240 Record.push_back(Expansion.isExpansionTokenRange());
2242 // Compute the token length for this macro expansion.
2243 SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
2244 if (I + 1 != N)
2245 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2246 Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
2247 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
2251 Stream.ExitBlock();
2253 if (SLocEntryOffsets.empty())
2254 return;
2256 // Write the source-location offsets table into the AST block. This
2257 // table is used for lazily loading source-location information.
2258 using namespace llvm;
2260 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2261 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2266 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2268 RecordData::value_type Record[] = {
2269 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2270 getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
2271 SLocEntryOffsetsBase - SourceManagerBlockOffset};
2272 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2273 bytes(SLocEntryOffsets));
2276 // Write the line table. It depends on remapping working, so it must come
2277 // after the source location offsets.
2278 if (SourceMgr.hasLineTable()) {
2279 LineTableInfo &LineTable = SourceMgr.getLineTable();
2281 Record.clear();
2283 // Emit the needed file names.
2284 llvm::DenseMap<int, int> FilenameMap;
2285 FilenameMap[-1] = -1; // For unspecified filenames.
2286 for (const auto &L : LineTable) {
2287 if (L.first.ID < 0)
2288 continue;
2289 for (auto &LE : L.second) {
2290 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2291 FilenameMap.size() - 1)).second)
2292 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2295 Record.push_back(0);
2297 // Emit the line entries
2298 for (const auto &L : LineTable) {
2299 // Only emit entries for local files.
2300 if (L.first.ID < 0)
2301 continue;
2303 AddFileID(L.first, Record);
2305 // Emit the line entries
2306 Record.push_back(L.second.size());
2307 for (const auto &LE : L.second) {
2308 Record.push_back(LE.FileOffset);
2309 Record.push_back(LE.LineNo);
2310 Record.push_back(FilenameMap[LE.FilenameID]);
2311 Record.push_back((unsigned)LE.FileKind);
2312 Record.push_back(LE.IncludeOffset);
2316 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2320 //===----------------------------------------------------------------------===//
2321 // Preprocessor Serialization
2322 //===----------------------------------------------------------------------===//
2324 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2325 const Preprocessor &PP) {
2326 if (MacroInfo *MI = MD->getMacroInfo())
2327 if (MI->isBuiltinMacro())
2328 return true;
2330 if (IsModule) {
2331 SourceLocation Loc = MD->getLocation();
2332 if (Loc.isInvalid())
2333 return true;
2334 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2335 return true;
2338 return false;
2341 /// Writes the block containing the serialized form of the
2342 /// preprocessor.
2343 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2344 uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2346 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2347 if (PPRec)
2348 WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
2350 RecordData Record;
2351 RecordData ModuleMacroRecord;
2353 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2354 if (PP.getCounterValue() != 0) {
2355 RecordData::value_type Record[] = {PP.getCounterValue()};
2356 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2359 // If we have a recorded #pragma assume_nonnull, remember it so it can be
2360 // replayed when the preamble terminates into the main file.
2361 SourceLocation AssumeNonNullLoc =
2362 PP.getPreambleRecordedPragmaAssumeNonNullLoc();
2363 if (AssumeNonNullLoc.isValid()) {
2364 assert(PP.isRecordingPreamble());
2365 AddSourceLocation(AssumeNonNullLoc, Record);
2366 Stream.EmitRecord(PP_ASSUME_NONNULL_LOC, Record);
2367 Record.clear();
2370 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2371 assert(!IsModule);
2372 auto SkipInfo = PP.getPreambleSkipInfo();
2373 if (SkipInfo) {
2374 Record.push_back(true);
2375 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2376 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2377 Record.push_back(SkipInfo->FoundNonSkipPortion);
2378 Record.push_back(SkipInfo->FoundElse);
2379 AddSourceLocation(SkipInfo->ElseLoc, Record);
2380 } else {
2381 Record.push_back(false);
2383 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2384 AddSourceLocation(Cond.IfLoc, Record);
2385 Record.push_back(Cond.WasSkipping);
2386 Record.push_back(Cond.FoundNonSkip);
2387 Record.push_back(Cond.FoundElse);
2389 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2390 Record.clear();
2393 // Enter the preprocessor block.
2394 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2396 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2397 // FIXME: Include a location for the use, and say which one was used.
2398 if (PP.SawDateOrTime())
2399 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2401 // Loop over all the macro directives that are live at the end of the file,
2402 // emitting each to the PP section.
2404 // Construct the list of identifiers with macro directives that need to be
2405 // serialized.
2406 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2407 // It is meaningless to emit macros for named modules. It only wastes times
2408 // and spaces.
2409 if (!isWritingStdCXXNamedModules())
2410 for (auto &Id : PP.getIdentifierTable())
2411 if (Id.second->hadMacroDefinition() &&
2412 (!Id.second->isFromAST() ||
2413 Id.second->hasChangedSinceDeserialization()))
2414 MacroIdentifiers.push_back(Id.second);
2415 // Sort the set of macro definitions that need to be serialized by the
2416 // name of the macro, to provide a stable ordering.
2417 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2419 // Emit the macro directives as a list and associate the offset with the
2420 // identifier they belong to.
2421 for (const IdentifierInfo *Name : MacroIdentifiers) {
2422 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2423 uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2424 assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2426 // Write out any exported module macros.
2427 bool EmittedModuleMacros = false;
2428 // C+=20 Header Units are compiled module interfaces, but they preserve
2429 // macros that are live (i.e. have a defined value) at the end of the
2430 // compilation. So when writing a header unit, we preserve only the final
2431 // value of each macro (and discard any that are undefined). Header units
2432 // do not have sub-modules (although they might import other header units).
2433 // PCH files, conversely, retain the history of each macro's define/undef
2434 // and of leaf macros in sub modules.
2435 if (IsModule && WritingModule->isHeaderUnit()) {
2436 // This is for the main TU when it is a C++20 header unit.
2437 // We preserve the final state of defined macros, and we do not emit ones
2438 // that are undefined.
2439 if (!MD || shouldIgnoreMacro(MD, IsModule, PP) ||
2440 MD->getKind() == MacroDirective::MD_Undefine)
2441 continue;
2442 AddSourceLocation(MD->getLocation(), Record);
2443 Record.push_back(MD->getKind());
2444 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2445 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2446 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2447 Record.push_back(VisMD->isPublic());
2449 ModuleMacroRecord.push_back(getSubmoduleID(WritingModule));
2450 ModuleMacroRecord.push_back(getMacroRef(MD->getMacroInfo(), Name));
2451 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2452 ModuleMacroRecord.clear();
2453 EmittedModuleMacros = true;
2454 } else {
2455 // Emit the macro directives in reverse source order.
2456 for (; MD; MD = MD->getPrevious()) {
2457 // Once we hit an ignored macro, we're done: the rest of the chain
2458 // will all be ignored macros.
2459 if (shouldIgnoreMacro(MD, IsModule, PP))
2460 break;
2461 AddSourceLocation(MD->getLocation(), Record);
2462 Record.push_back(MD->getKind());
2463 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2464 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2465 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2466 Record.push_back(VisMD->isPublic());
2470 // We write out exported module macros for PCH as well.
2471 auto Leafs = PP.getLeafModuleMacros(Name);
2472 SmallVector<ModuleMacro *, 8> Worklist(Leafs.begin(), Leafs.end());
2473 llvm::DenseMap<ModuleMacro *, unsigned> Visits;
2474 while (!Worklist.empty()) {
2475 auto *Macro = Worklist.pop_back_val();
2477 // Emit a record indicating this submodule exports this macro.
2478 ModuleMacroRecord.push_back(getSubmoduleID(Macro->getOwningModule()));
2479 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2480 for (auto *M : Macro->overrides())
2481 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2483 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2484 ModuleMacroRecord.clear();
2486 // Enqueue overridden macros once we've visited all their ancestors.
2487 for (auto *M : Macro->overrides())
2488 if (++Visits[M] == M->getNumOverridingMacros())
2489 Worklist.push_back(M);
2491 EmittedModuleMacros = true;
2494 if (Record.empty() && !EmittedModuleMacros)
2495 continue;
2497 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2498 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2499 Record.clear();
2502 /// Offsets of each of the macros into the bitstream, indexed by
2503 /// the local macro ID
2505 /// For each identifier that is associated with a macro, this map
2506 /// provides the offset into the bitstream where that macro is
2507 /// defined.
2508 std::vector<uint32_t> MacroOffsets;
2510 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2511 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2512 MacroInfo *MI = MacroInfosToEmit[I].MI;
2513 MacroID ID = MacroInfosToEmit[I].ID;
2515 if (ID < FirstMacroID) {
2516 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2517 continue;
2520 // Record the local offset of this macro.
2521 unsigned Index = ID - FirstMacroID;
2522 if (Index >= MacroOffsets.size())
2523 MacroOffsets.resize(Index + 1);
2525 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2526 assert((Offset >> 32) == 0 && "Macro offset too large");
2527 MacroOffsets[Index] = Offset;
2529 AddIdentifierRef(Name, Record);
2530 AddSourceLocation(MI->getDefinitionLoc(), Record);
2531 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2532 Record.push_back(MI->isUsed());
2533 Record.push_back(MI->isUsedForHeaderGuard());
2534 Record.push_back(MI->getNumTokens());
2535 unsigned Code;
2536 if (MI->isObjectLike()) {
2537 Code = PP_MACRO_OBJECT_LIKE;
2538 } else {
2539 Code = PP_MACRO_FUNCTION_LIKE;
2541 Record.push_back(MI->isC99Varargs());
2542 Record.push_back(MI->isGNUVarargs());
2543 Record.push_back(MI->hasCommaPasting());
2544 Record.push_back(MI->getNumParams());
2545 for (const IdentifierInfo *Param : MI->params())
2546 AddIdentifierRef(Param, Record);
2549 // If we have a detailed preprocessing record, record the macro definition
2550 // ID that corresponds to this macro.
2551 if (PPRec)
2552 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2554 Stream.EmitRecord(Code, Record);
2555 Record.clear();
2557 // Emit the tokens array.
2558 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2559 // Note that we know that the preprocessor does not have any annotation
2560 // tokens in it because they are created by the parser, and thus can't
2561 // be in a macro definition.
2562 const Token &Tok = MI->getReplacementToken(TokNo);
2563 AddToken(Tok, Record);
2564 Stream.EmitRecord(PP_TOKEN, Record);
2565 Record.clear();
2567 ++NumMacros;
2570 Stream.ExitBlock();
2572 // Write the offsets table for macro IDs.
2573 using namespace llvm;
2575 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2576 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2582 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2584 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2585 FirstMacroID - NUM_PREDEF_MACRO_IDS,
2586 MacroOffsetsBase - ASTBlockStartOffset};
2587 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2591 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2592 uint64_t MacroOffsetsBase) {
2593 if (PPRec.local_begin() == PPRec.local_end())
2594 return;
2596 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2598 // Enter the preprocessor block.
2599 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2601 // If the preprocessor has a preprocessing record, emit it.
2602 unsigned NumPreprocessingRecords = 0;
2603 using namespace llvm;
2605 // Set up the abbreviation for
2606 unsigned InclusionAbbrev = 0;
2608 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2609 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2615 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2618 unsigned FirstPreprocessorEntityID
2619 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2620 + NUM_PREDEF_PP_ENTITY_IDS;
2621 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2622 RecordData Record;
2623 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2624 EEnd = PPRec.local_end();
2625 E != EEnd;
2626 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2627 Record.clear();
2629 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2630 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2631 PreprocessedEntityOffsets.push_back(
2632 PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
2634 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2635 // Record this macro definition's ID.
2636 MacroDefinitions[MD] = NextPreprocessorEntityID;
2638 AddIdentifierRef(MD->getName(), Record);
2639 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2640 continue;
2643 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2644 Record.push_back(ME->isBuiltinMacro());
2645 if (ME->isBuiltinMacro())
2646 AddIdentifierRef(ME->getName(), Record);
2647 else
2648 Record.push_back(MacroDefinitions[ME->getDefinition()]);
2649 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2650 continue;
2653 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2654 Record.push_back(PPD_INCLUSION_DIRECTIVE);
2655 Record.push_back(ID->getFileName().size());
2656 Record.push_back(ID->wasInQuotes());
2657 Record.push_back(static_cast<unsigned>(ID->getKind()));
2658 Record.push_back(ID->importedModule());
2659 SmallString<64> Buffer;
2660 Buffer += ID->getFileName();
2661 // Check that the FileEntry is not null because it was not resolved and
2662 // we create a PCH even with compiler errors.
2663 if (ID->getFile())
2664 Buffer += ID->getFile()->getName();
2665 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2666 continue;
2669 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2671 Stream.ExitBlock();
2673 // Write the offsets table for the preprocessing record.
2674 if (NumPreprocessingRecords > 0) {
2675 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2677 // Write the offsets table for identifier IDs.
2678 using namespace llvm;
2680 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2681 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2684 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2686 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2687 FirstPreprocessorEntityID -
2688 NUM_PREDEF_PP_ENTITY_IDS};
2689 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2690 bytes(PreprocessedEntityOffsets));
2693 // Write the skipped region table for the preprocessing record.
2694 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2695 if (SkippedRanges.size() > 0) {
2696 std::vector<PPSkippedRange> SerializedSkippedRanges;
2697 SerializedSkippedRanges.reserve(SkippedRanges.size());
2698 for (auto const& Range : SkippedRanges)
2699 SerializedSkippedRanges.emplace_back(Range);
2701 using namespace llvm;
2702 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2703 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2704 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2705 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2707 Record.clear();
2708 Record.push_back(PPD_SKIPPED_RANGES);
2709 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2710 bytes(SerializedSkippedRanges));
2714 unsigned ASTWriter::getLocalOrImportedSubmoduleID(const Module *Mod) {
2715 if (!Mod)
2716 return 0;
2718 auto Known = SubmoduleIDs.find(Mod);
2719 if (Known != SubmoduleIDs.end())
2720 return Known->second;
2722 auto *Top = Mod->getTopLevelModule();
2723 if (Top != WritingModule &&
2724 (getLangOpts().CompilingPCH ||
2725 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
2726 return 0;
2728 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2731 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2732 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2733 // FIXME: This can easily happen, if we have a reference to a submodule that
2734 // did not result in us loading a module file for that submodule. For
2735 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2736 // assert((ID || !Mod) &&
2737 // "asked for module ID for non-local, non-imported module");
2738 return ID;
2741 /// Compute the number of modules within the given tree (including the
2742 /// given module).
2743 static unsigned getNumberOfModules(Module *Mod) {
2744 unsigned ChildModules = 0;
2745 for (auto *Submodule : Mod->submodules())
2746 ChildModules += getNumberOfModules(Submodule);
2748 return ChildModules + 1;
2751 void ASTWriter::WriteSubmodules(Module *WritingModule) {
2752 // Enter the submodule description block.
2753 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2755 // Write the abbreviations needed for the submodules block.
2756 using namespace llvm;
2758 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2759 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Kind
2763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
2764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NamedModuleHasN...
2774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2775 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2777 Abbrev = std::make_shared<BitCodeAbbrev>();
2778 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2780 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2782 Abbrev = std::make_shared<BitCodeAbbrev>();
2783 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2784 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2785 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2787 Abbrev = std::make_shared<BitCodeAbbrev>();
2788 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2790 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2792 Abbrev = std::make_shared<BitCodeAbbrev>();
2793 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2795 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2797 Abbrev = std::make_shared<BitCodeAbbrev>();
2798 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2801 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2803 Abbrev = std::make_shared<BitCodeAbbrev>();
2804 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2806 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2808 Abbrev = std::make_shared<BitCodeAbbrev>();
2809 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2811 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2813 Abbrev = std::make_shared<BitCodeAbbrev>();
2814 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2816 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2818 Abbrev = std::make_shared<BitCodeAbbrev>();
2819 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2820 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2821 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2823 Abbrev = std::make_shared<BitCodeAbbrev>();
2824 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2827 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2829 Abbrev = std::make_shared<BitCodeAbbrev>();
2830 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2832 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2834 Abbrev = std::make_shared<BitCodeAbbrev>();
2835 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2837 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2838 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2840 Abbrev = std::make_shared<BitCodeAbbrev>();
2841 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2843 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2845 // Write the submodule metadata block.
2846 RecordData::value_type Record[] = {
2847 getNumberOfModules(WritingModule),
2848 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2849 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2851 // Write all of the submodules.
2852 std::queue<Module *> Q;
2853 Q.push(WritingModule);
2854 while (!Q.empty()) {
2855 Module *Mod = Q.front();
2856 Q.pop();
2857 unsigned ID = getSubmoduleID(Mod);
2859 uint64_t ParentID = 0;
2860 if (Mod->Parent) {
2861 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2862 ParentID = SubmoduleIDs[Mod->Parent];
2865 uint64_t DefinitionLoc =
2866 SourceLocationEncoding::encode(getAdjustedLocation(Mod->DefinitionLoc));
2868 // Emit the definition of the block.
2870 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2872 ParentID,
2873 (RecordData::value_type)Mod->Kind,
2874 DefinitionLoc,
2875 Mod->IsFramework,
2876 Mod->IsExplicit,
2877 Mod->IsSystem,
2878 Mod->IsExternC,
2879 Mod->InferSubmodules,
2880 Mod->InferExplicitSubmodules,
2881 Mod->InferExportWildcard,
2882 Mod->ConfigMacrosExhaustive,
2883 Mod->ModuleMapIsPrivate,
2884 Mod->NamedModuleHasInit};
2885 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2888 // Emit the requirements.
2889 for (const auto &R : Mod->Requirements) {
2890 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2891 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2894 // Emit the umbrella header, if there is one.
2895 if (std::optional<Module::Header> UmbrellaHeader =
2896 Mod->getUmbrellaHeaderAsWritten()) {
2897 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2898 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2899 UmbrellaHeader->NameAsWritten);
2900 } else if (std::optional<Module::DirectoryName> UmbrellaDir =
2901 Mod->getUmbrellaDirAsWritten()) {
2902 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2903 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2904 UmbrellaDir->NameAsWritten);
2907 // Emit the headers.
2908 struct {
2909 unsigned RecordKind;
2910 unsigned Abbrev;
2911 Module::HeaderKind HeaderKind;
2912 } HeaderLists[] = {
2913 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2914 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2915 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2916 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2917 Module::HK_PrivateTextual},
2918 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2920 for (auto &HL : HeaderLists) {
2921 RecordData::value_type Record[] = {HL.RecordKind};
2922 for (auto &H : Mod->Headers[HL.HeaderKind])
2923 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2926 // Emit the top headers.
2928 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2929 for (FileEntryRef H : Mod->getTopHeaders(PP->getFileManager())) {
2930 SmallString<128> HeaderName(H.getName());
2931 PreparePathForOutput(HeaderName);
2932 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
2936 // Emit the imports.
2937 if (!Mod->Imports.empty()) {
2938 RecordData Record;
2939 for (auto *I : Mod->Imports)
2940 Record.push_back(getSubmoduleID(I));
2941 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2944 // Emit the modules affecting compilation that were not imported.
2945 if (!Mod->AffectingClangModules.empty()) {
2946 RecordData Record;
2947 for (auto *I : Mod->AffectingClangModules)
2948 Record.push_back(getSubmoduleID(I));
2949 Stream.EmitRecord(SUBMODULE_AFFECTING_MODULES, Record);
2952 // Emit the exports.
2953 if (!Mod->Exports.empty()) {
2954 RecordData Record;
2955 for (const auto &E : Mod->Exports) {
2956 // FIXME: This may fail; we don't require that all exported modules
2957 // are local or imported.
2958 Record.push_back(getSubmoduleID(E.getPointer()));
2959 Record.push_back(E.getInt());
2961 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2964 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2965 // Might be unnecessary as use declarations are only used to build the
2966 // module itself.
2968 // TODO: Consider serializing undeclared uses of modules.
2970 // Emit the link libraries.
2971 for (const auto &LL : Mod->LinkLibraries) {
2972 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2973 LL.IsFramework};
2974 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2977 // Emit the conflicts.
2978 for (const auto &C : Mod->Conflicts) {
2979 // FIXME: This may fail; we don't require that all conflicting modules
2980 // are local or imported.
2981 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2982 getSubmoduleID(C.Other)};
2983 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2986 // Emit the configuration macros.
2987 for (const auto &CM : Mod->ConfigMacros) {
2988 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2989 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
2992 // Emit the initializers, if any.
2993 RecordData Inits;
2994 for (Decl *D : Context->getModuleInitializers(Mod))
2995 Inits.push_back(GetDeclRef(D));
2996 if (!Inits.empty())
2997 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2999 // Emit the name of the re-exported module, if any.
3000 if (!Mod->ExportAsModule.empty()) {
3001 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3002 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3005 // Queue up the submodules of this module.
3006 for (auto *M : Mod->submodules())
3007 Q.push(M);
3010 Stream.ExitBlock();
3012 assert((NextSubmoduleID - FirstSubmoduleID ==
3013 getNumberOfModules(WritingModule)) &&
3014 "Wrong # of submodules; found a reference to a non-local, "
3015 "non-imported submodule?");
3018 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3019 bool isModule) {
3020 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3021 DiagStateIDMap;
3022 unsigned CurrID = 0;
3023 RecordData Record;
3025 auto EncodeDiagStateFlags =
3026 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3027 unsigned Result = (unsigned)DS->ExtBehavior;
3028 for (unsigned Val :
3029 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3030 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3031 (unsigned)DS->SuppressSystemWarnings})
3032 Result = (Result << 1) | Val;
3033 return Result;
3036 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3037 Record.push_back(Flags);
3039 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3040 bool IncludeNonPragmaStates) {
3041 // Ensure that the diagnostic state wasn't modified since it was created.
3042 // We will not correctly round-trip this information otherwise.
3043 assert(Flags == EncodeDiagStateFlags(State) &&
3044 "diag state flags vary in single AST file");
3046 // If we ever serialize non-pragma mappings outside the initial state, the
3047 // code below will need to consider more than getDefaultMapping.
3048 assert(!IncludeNonPragmaStates ||
3049 State == Diag.DiagStatesByLoc.FirstDiagState);
3051 unsigned &DiagStateID = DiagStateIDMap[State];
3052 Record.push_back(DiagStateID);
3054 if (DiagStateID == 0) {
3055 DiagStateID = ++CurrID;
3056 SmallVector<std::pair<unsigned, DiagnosticMapping>> Mappings;
3058 // Add a placeholder for the number of mappings.
3059 auto SizeIdx = Record.size();
3060 Record.emplace_back();
3061 for (const auto &I : *State) {
3062 // Maybe skip non-pragmas.
3063 if (!I.second.isPragma() && !IncludeNonPragmaStates)
3064 continue;
3065 // Skip default mappings. We have a mapping for every diagnostic ever
3066 // emitted, regardless of whether it was customized.
3067 if (!I.second.isPragma() &&
3068 I.second == DiagnosticIDs::getDefaultMapping(I.first))
3069 continue;
3070 Mappings.push_back(I);
3073 // Sort by diag::kind for deterministic output.
3074 llvm::sort(Mappings, [](const auto &LHS, const auto &RHS) {
3075 return LHS.first < RHS.first;
3078 for (const auto &I : Mappings) {
3079 Record.push_back(I.first);
3080 Record.push_back(I.second.serialize());
3082 // Update the placeholder.
3083 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
3087 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
3089 // Reserve a spot for the number of locations with state transitions.
3090 auto NumLocationsIdx = Record.size();
3091 Record.emplace_back();
3093 // Emit the state transitions.
3094 unsigned NumLocations = 0;
3095 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3096 if (!FileIDAndFile.first.isValid() ||
3097 !FileIDAndFile.second.HasLocalTransitions)
3098 continue;
3099 ++NumLocations;
3101 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3102 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3103 AddSourceLocation(Loc, Record);
3105 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3106 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3107 Record.push_back(getAdjustedOffset(StatePoint.Offset));
3108 AddDiagState(StatePoint.State, false);
3112 // Backpatch the number of locations.
3113 Record[NumLocationsIdx] = NumLocations;
3115 // Emit CurDiagStateLoc. Do it last in order to match source order.
3117 // This also protects against a hypothetical corner case with simulating
3118 // -Werror settings for implicit modules in the ASTReader, where reading
3119 // CurDiagState out of context could change whether warning pragmas are
3120 // treated as errors.
3121 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3122 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3124 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
3127 //===----------------------------------------------------------------------===//
3128 // Type Serialization
3129 //===----------------------------------------------------------------------===//
3131 /// Write the representation of a type to the AST stream.
3132 void ASTWriter::WriteType(QualType T) {
3133 TypeIdx &IdxRef = TypeIdxs[T];
3134 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3135 IdxRef = TypeIdx(NextTypeID++);
3136 TypeIdx Idx = IdxRef;
3138 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
3140 // Emit the type's representation.
3141 uint64_t Offset = ASTTypeWriter(*this).write(T) - DeclTypesBlockStartOffset;
3143 // Record the offset for this type.
3144 unsigned Index = Idx.getIndex() - FirstTypeID;
3145 if (TypeOffsets.size() == Index)
3146 TypeOffsets.emplace_back(Offset);
3147 else if (TypeOffsets.size() < Index) {
3148 TypeOffsets.resize(Index + 1);
3149 TypeOffsets[Index].setBitOffset(Offset);
3150 } else {
3151 llvm_unreachable("Types emitted in wrong order");
3155 //===----------------------------------------------------------------------===//
3156 // Declaration Serialization
3157 //===----------------------------------------------------------------------===//
3159 /// Write the block containing all of the declaration IDs
3160 /// lexically declared within the given DeclContext.
3162 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3163 /// bitstream, or 0 if no block was written.
3164 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
3165 DeclContext *DC) {
3166 if (DC->decls_empty())
3167 return 0;
3169 uint64_t Offset = Stream.GetCurrentBitNo();
3170 SmallVector<uint32_t, 128> KindDeclPairs;
3171 for (const auto *D : DC->decls()) {
3172 KindDeclPairs.push_back(D->getKind());
3173 KindDeclPairs.push_back(GetDeclRef(D));
3176 ++NumLexicalDeclContexts;
3177 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
3178 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3179 bytes(KindDeclPairs));
3180 return Offset;
3183 void ASTWriter::WriteTypeDeclOffsets() {
3184 using namespace llvm;
3186 // Write the type offsets array
3187 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3188 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
3189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
3190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
3191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
3192 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3194 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3195 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3196 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3199 // Write the declaration offsets array
3200 Abbrev = std::make_shared<BitCodeAbbrev>();
3201 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
3202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
3203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
3204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
3205 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3207 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3208 FirstDeclID - NUM_PREDEF_DECL_IDS};
3209 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3213 void ASTWriter::WriteFileDeclIDsMap() {
3214 using namespace llvm;
3216 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
3217 SortedFileDeclIDs.reserve(FileDeclIDs.size());
3218 for (const auto &P : FileDeclIDs)
3219 SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
3220 llvm::sort(SortedFileDeclIDs, llvm::less_first());
3222 // Join the vectors of DeclIDs from all files.
3223 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3224 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3225 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3226 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3227 llvm::stable_sort(Info.DeclIDs);
3228 for (auto &LocDeclEntry : Info.DeclIDs)
3229 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
3232 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3233 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
3234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3236 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
3237 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3238 FileGroupedDeclIDs.size()};
3239 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
3242 void ASTWriter::WriteComments() {
3243 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
3244 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3245 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3246 return;
3248 // Don't write comments to BMI to reduce the size of BMI.
3249 // If language services (e.g., clangd) want such abilities,
3250 // we can offer a special option then.
3251 if (isWritingStdCXXNamedModules())
3252 return;
3254 RecordData Record;
3255 for (const auto &FO : Context->Comments.OrderedComments) {
3256 for (const auto &OC : FO.second) {
3257 const RawComment *I = OC.second;
3258 Record.clear();
3259 AddSourceRange(I->getSourceRange(), Record);
3260 Record.push_back(I->getKind());
3261 Record.push_back(I->isTrailingComment());
3262 Record.push_back(I->isAlmostTrailingComment());
3263 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3268 //===----------------------------------------------------------------------===//
3269 // Global Method Pool and Selector Serialization
3270 //===----------------------------------------------------------------------===//
3272 namespace {
3274 // Trait used for the on-disk hash table used in the method pool.
3275 class ASTMethodPoolTrait {
3276 ASTWriter &Writer;
3278 public:
3279 using key_type = Selector;
3280 using key_type_ref = key_type;
3282 struct data_type {
3283 SelectorID ID;
3284 ObjCMethodList Instance, Factory;
3286 using data_type_ref = const data_type &;
3288 using hash_value_type = unsigned;
3289 using offset_type = unsigned;
3291 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3293 static hash_value_type ComputeHash(Selector Sel) {
3294 return serialization::ComputeHash(Sel);
3297 std::pair<unsigned, unsigned>
3298 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3299 data_type_ref Methods) {
3300 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
3301 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3302 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3303 Method = Method->getNext())
3304 if (ShouldWriteMethodListNode(Method))
3305 DataLen += 4;
3306 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3307 Method = Method->getNext())
3308 if (ShouldWriteMethodListNode(Method))
3309 DataLen += 4;
3310 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3313 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3314 using namespace llvm::support;
3316 endian::Writer LE(Out, llvm::endianness::little);
3317 uint64_t Start = Out.tell();
3318 assert((Start >> 32) == 0 && "Selector key offset too large");
3319 Writer.SetSelectorOffset(Sel, Start);
3320 unsigned N = Sel.getNumArgs();
3321 LE.write<uint16_t>(N);
3322 if (N == 0)
3323 N = 1;
3324 for (unsigned I = 0; I != N; ++I)
3325 LE.write<uint32_t>(
3326 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
3329 void EmitData(raw_ostream& Out, key_type_ref,
3330 data_type_ref Methods, unsigned DataLen) {
3331 using namespace llvm::support;
3333 endian::Writer LE(Out, llvm::endianness::little);
3334 uint64_t Start = Out.tell(); (void)Start;
3335 LE.write<uint32_t>(Methods.ID);
3336 unsigned NumInstanceMethods = 0;
3337 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3338 Method = Method->getNext())
3339 if (ShouldWriteMethodListNode(Method))
3340 ++NumInstanceMethods;
3342 unsigned NumFactoryMethods = 0;
3343 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3344 Method = Method->getNext())
3345 if (ShouldWriteMethodListNode(Method))
3346 ++NumFactoryMethods;
3348 unsigned InstanceBits = Methods.Instance.getBits();
3349 assert(InstanceBits < 4);
3350 unsigned InstanceHasMoreThanOneDeclBit =
3351 Methods.Instance.hasMoreThanOneDecl();
3352 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3353 (InstanceHasMoreThanOneDeclBit << 2) |
3354 InstanceBits;
3355 unsigned FactoryBits = Methods.Factory.getBits();
3356 assert(FactoryBits < 4);
3357 unsigned FactoryHasMoreThanOneDeclBit =
3358 Methods.Factory.hasMoreThanOneDecl();
3359 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3360 (FactoryHasMoreThanOneDeclBit << 2) |
3361 FactoryBits;
3362 LE.write<uint16_t>(FullInstanceBits);
3363 LE.write<uint16_t>(FullFactoryBits);
3364 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3365 Method = Method->getNext())
3366 if (ShouldWriteMethodListNode(Method))
3367 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3368 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3369 Method = Method->getNext())
3370 if (ShouldWriteMethodListNode(Method))
3371 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3373 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3376 private:
3377 static bool ShouldWriteMethodListNode(const ObjCMethodList *Node) {
3378 return (Node->getMethod() && !Node->getMethod()->isFromASTFile());
3382 } // namespace
3384 /// Write ObjC data: selectors and the method pool.
3386 /// The method pool contains both instance and factory methods, stored
3387 /// in an on-disk hash table indexed by the selector. The hash table also
3388 /// contains an empty entry for every other selector known to Sema.
3389 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3390 using namespace llvm;
3392 // Do we have to do anything at all?
3393 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
3394 return;
3395 unsigned NumTableEntries = 0;
3396 // Create and write out the blob that contains selectors and the method pool.
3398 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3399 ASTMethodPoolTrait Trait(*this);
3401 // Create the on-disk hash table representation. We walk through every
3402 // selector we've seen and look it up in the method pool.
3403 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3404 for (auto &SelectorAndID : SelectorIDs) {
3405 Selector S = SelectorAndID.first;
3406 SelectorID ID = SelectorAndID.second;
3407 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
3408 ASTMethodPoolTrait::data_type Data = {
3410 ObjCMethodList(),
3411 ObjCMethodList()
3413 if (F != SemaRef.MethodPool.end()) {
3414 Data.Instance = F->second.first;
3415 Data.Factory = F->second.second;
3417 // Only write this selector if it's not in an existing AST or something
3418 // changed.
3419 if (Chain && ID < FirstSelectorID) {
3420 // Selector already exists. Did it change?
3421 bool changed = false;
3422 for (ObjCMethodList *M = &Data.Instance; M && M->getMethod();
3423 M = M->getNext()) {
3424 if (!M->getMethod()->isFromASTFile()) {
3425 changed = true;
3426 Data.Instance = *M;
3427 break;
3430 for (ObjCMethodList *M = &Data.Factory; M && M->getMethod();
3431 M = M->getNext()) {
3432 if (!M->getMethod()->isFromASTFile()) {
3433 changed = true;
3434 Data.Factory = *M;
3435 break;
3438 if (!changed)
3439 continue;
3440 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3441 // A new method pool entry.
3442 ++NumTableEntries;
3444 Generator.insert(S, Data, Trait);
3447 // Create the on-disk hash table in a buffer.
3448 SmallString<4096> MethodPool;
3449 uint32_t BucketOffset;
3451 using namespace llvm::support;
3453 ASTMethodPoolTrait Trait(*this);
3454 llvm::raw_svector_ostream Out(MethodPool);
3455 // Make sure that no bucket is at offset 0
3456 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3457 BucketOffset = Generator.Emit(Out, Trait);
3460 // Create a blob abbreviation
3461 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3462 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3463 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3466 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3468 // Write the method pool
3470 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3471 NumTableEntries};
3472 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3475 // Create a blob abbreviation for the selector table offsets.
3476 Abbrev = std::make_shared<BitCodeAbbrev>();
3477 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3481 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3483 // Write the selector offsets table.
3485 RecordData::value_type Record[] = {
3486 SELECTOR_OFFSETS, SelectorOffsets.size(),
3487 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3488 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3489 bytes(SelectorOffsets));
3494 /// Write the selectors referenced in @selector expression into AST file.
3495 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3496 using namespace llvm;
3498 if (SemaRef.ReferencedSelectors.empty())
3499 return;
3501 RecordData Record;
3502 ASTRecordWriter Writer(*this, Record);
3504 // Note: this writes out all references even for a dependent AST. But it is
3505 // very tricky to fix, and given that @selector shouldn't really appear in
3506 // headers, probably not worth it. It's not a correctness issue.
3507 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3508 Selector Sel = SelectorAndLocation.first;
3509 SourceLocation Loc = SelectorAndLocation.second;
3510 Writer.AddSelectorRef(Sel);
3511 Writer.AddSourceLocation(Loc);
3513 Writer.Emit(REFERENCED_SELECTOR_POOL);
3516 //===----------------------------------------------------------------------===//
3517 // Identifier Table Serialization
3518 //===----------------------------------------------------------------------===//
3520 /// Determine the declaration that should be put into the name lookup table to
3521 /// represent the given declaration in this module. This is usually D itself,
3522 /// but if D was imported and merged into a local declaration, we want the most
3523 /// recent local declaration instead. The chosen declaration will be the most
3524 /// recent declaration in any module that imports this one.
3525 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3526 NamedDecl *D) {
3527 if (!LangOpts.Modules || !D->isFromASTFile())
3528 return D;
3530 if (Decl *Redecl = D->getPreviousDecl()) {
3531 // For Redeclarable decls, a prior declaration might be local.
3532 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3533 // If we find a local decl, we're done.
3534 if (!Redecl->isFromASTFile()) {
3535 // Exception: in very rare cases (for injected-class-names), not all
3536 // redeclarations are in the same semantic context. Skip ones in a
3537 // different context. They don't go in this lookup table at all.
3538 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3539 D->getDeclContext()->getRedeclContext()))
3540 continue;
3541 return cast<NamedDecl>(Redecl);
3544 // If we find a decl from a (chained-)PCH stop since we won't find a
3545 // local one.
3546 if (Redecl->getOwningModuleID() == 0)
3547 break;
3549 } else if (Decl *First = D->getCanonicalDecl()) {
3550 // For Mergeable decls, the first decl might be local.
3551 if (!First->isFromASTFile())
3552 return cast<NamedDecl>(First);
3555 // All declarations are imported. Our most recent declaration will also be
3556 // the most recent one in anyone who imports us.
3557 return D;
3560 namespace {
3562 class ASTIdentifierTableTrait {
3563 ASTWriter &Writer;
3564 Preprocessor &PP;
3565 IdentifierResolver &IdResolver;
3566 bool IsModule;
3567 bool NeedDecls;
3568 ASTWriter::RecordData *InterestingIdentifierOffsets;
3570 /// Determines whether this is an "interesting" identifier that needs a
3571 /// full IdentifierInfo structure written into the hash table. Notably, this
3572 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3573 /// to check that.
3574 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3575 if (MacroOffset || II->isPoisoned() ||
3576 (!IsModule && II->getObjCOrBuiltinID()) ||
3577 II->hasRevertedTokenIDToIdentifier() ||
3578 (NeedDecls && II->getFETokenInfo()))
3579 return true;
3581 return false;
3584 public:
3585 using key_type = IdentifierInfo *;
3586 using key_type_ref = key_type;
3588 using data_type = IdentID;
3589 using data_type_ref = data_type;
3591 using hash_value_type = unsigned;
3592 using offset_type = unsigned;
3594 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3595 IdentifierResolver &IdResolver, bool IsModule,
3596 ASTWriter::RecordData *InterestingIdentifierOffsets)
3597 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3598 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3599 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3601 bool needDecls() const { return NeedDecls; }
3603 static hash_value_type ComputeHash(const IdentifierInfo* II) {
3604 return llvm::djbHash(II->getName());
3607 bool isInterestingIdentifier(const IdentifierInfo *II) {
3608 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3609 return isInterestingIdentifier(II, MacroOffset);
3612 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3613 return isInterestingIdentifier(II, 0);
3616 std::pair<unsigned, unsigned>
3617 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3618 // Record the location of the identifier data. This is used when generating
3619 // the mapping from persistent IDs to strings.
3620 Writer.SetIdentifierOffset(II, Out.tell());
3622 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3624 // Emit the offset of the key/data length information to the interesting
3625 // identifiers table if necessary.
3626 if (InterestingIdentifierOffsets &&
3627 isInterestingIdentifier(II, MacroOffset))
3628 InterestingIdentifierOffsets->push_back(Out.tell());
3630 unsigned KeyLen = II->getLength() + 1;
3631 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3632 if (isInterestingIdentifier(II, MacroOffset)) {
3633 DataLen += 2; // 2 bytes for builtin ID
3634 DataLen += 2; // 2 bytes for flags
3635 if (MacroOffset)
3636 DataLen += 4; // MacroDirectives offset.
3638 if (NeedDecls)
3639 DataLen += std::distance(IdResolver.begin(II), IdResolver.end()) * 4;
3641 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3644 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3645 unsigned KeyLen) {
3646 Out.write(II->getNameStart(), KeyLen);
3649 void EmitData(raw_ostream& Out, IdentifierInfo* II,
3650 IdentID ID, unsigned) {
3651 using namespace llvm::support;
3653 endian::Writer LE(Out, llvm::endianness::little);
3655 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3656 if (!isInterestingIdentifier(II, MacroOffset)) {
3657 LE.write<uint32_t>(ID << 1);
3658 return;
3661 LE.write<uint32_t>((ID << 1) | 0x01);
3662 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3663 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3664 LE.write<uint16_t>(Bits);
3665 Bits = 0;
3666 bool HadMacroDefinition = MacroOffset != 0;
3667 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3668 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3669 Bits = (Bits << 1) | unsigned(II->isPoisoned());
3670 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3671 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3672 LE.write<uint16_t>(Bits);
3674 if (HadMacroDefinition)
3675 LE.write<uint32_t>(MacroOffset);
3677 if (NeedDecls) {
3678 // Emit the declaration IDs in reverse order, because the
3679 // IdentifierResolver provides the declarations as they would be
3680 // visible (e.g., the function "stat" would come before the struct
3681 // "stat"), but the ASTReader adds declarations to the end of the list
3682 // (so we need to see the struct "stat" before the function "stat").
3683 // Only emit declarations that aren't from a chained PCH, though.
3684 SmallVector<NamedDecl *, 16> Decls(IdResolver.decls(II));
3685 for (NamedDecl *D : llvm::reverse(Decls))
3686 LE.write<uint32_t>(
3687 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), D)));
3692 } // namespace
3694 /// Write the identifier table into the AST file.
3696 /// The identifier table consists of a blob containing string data
3697 /// (the actual identifiers themselves) and a separate "offsets" index
3698 /// that maps identifier IDs to locations within the blob.
3699 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3700 IdentifierResolver &IdResolver,
3701 bool IsModule) {
3702 using namespace llvm;
3704 RecordData InterestingIdents;
3706 // Create and write out the blob that contains the identifier
3707 // strings.
3709 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3710 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule,
3711 IsModule ? &InterestingIdents : nullptr);
3713 // Look for any identifiers that were named while processing the
3714 // headers, but are otherwise not needed. We add these to the hash
3715 // table to enable checking of the predefines buffer in the case
3716 // where the user adds new macro definitions when building the AST
3717 // file.
3718 SmallVector<const IdentifierInfo *, 128> IIs;
3719 for (const auto &ID : PP.getIdentifierTable())
3720 if (Trait.isInterestingNonMacroIdentifier(ID.second))
3721 IIs.push_back(ID.second);
3722 // Sort the identifiers lexicographically before getting the references so
3723 // that their order is stable.
3724 llvm::sort(IIs, llvm::deref<std::less<>>());
3725 for (const IdentifierInfo *II : IIs)
3726 getIdentifierRef(II);
3728 // Create the on-disk hash table representation. We only store offsets
3729 // for identifiers that appear here for the first time.
3730 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3731 for (auto IdentIDPair : IdentifierIDs) {
3732 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3733 IdentID ID = IdentIDPair.second;
3734 assert(II && "NULL identifier in identifier table");
3735 // Write out identifiers if either the ID is local or the identifier has
3736 // changed since it was loaded.
3737 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3738 || II->hasChangedSinceDeserialization() ||
3739 (Trait.needDecls() &&
3740 II->hasFETokenInfoChangedSinceDeserialization()))
3741 Generator.insert(II, ID, Trait);
3744 // Create the on-disk hash table in a buffer.
3745 SmallString<4096> IdentifierTable;
3746 uint32_t BucketOffset;
3748 using namespace llvm::support;
3750 llvm::raw_svector_ostream Out(IdentifierTable);
3751 // Make sure that no bucket is at offset 0
3752 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
3753 BucketOffset = Generator.Emit(Out, Trait);
3756 // Create a blob abbreviation
3757 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3758 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3761 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3763 // Write the identifier table
3764 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3765 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3768 // Write the offsets table for identifier IDs.
3769 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3770 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3774 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3776 #ifndef NDEBUG
3777 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3778 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3779 #endif
3781 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3782 IdentifierOffsets.size(),
3783 FirstIdentID - NUM_PREDEF_IDENT_IDS};
3784 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3785 bytes(IdentifierOffsets));
3787 // In C++, write the list of interesting identifiers (those that are
3788 // defined as macros, poisoned, or similar unusual things).
3789 if (!InterestingIdents.empty())
3790 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3793 //===----------------------------------------------------------------------===//
3794 // DeclContext's Name Lookup Table Serialization
3795 //===----------------------------------------------------------------------===//
3797 namespace {
3799 // Trait used for the on-disk hash table used in the method pool.
3800 class ASTDeclContextNameLookupTrait {
3801 ASTWriter &Writer;
3802 llvm::SmallVector<DeclID, 64> DeclIDs;
3804 public:
3805 using key_type = DeclarationNameKey;
3806 using key_type_ref = key_type;
3808 /// A start and end index into DeclIDs, representing a sequence of decls.
3809 using data_type = std::pair<unsigned, unsigned>;
3810 using data_type_ref = const data_type &;
3812 using hash_value_type = unsigned;
3813 using offset_type = unsigned;
3815 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3817 template<typename Coll>
3818 data_type getData(const Coll &Decls) {
3819 unsigned Start = DeclIDs.size();
3820 for (NamedDecl *D : Decls) {
3821 DeclIDs.push_back(
3822 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3824 return std::make_pair(Start, DeclIDs.size());
3827 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3828 unsigned Start = DeclIDs.size();
3829 llvm::append_range(DeclIDs, FromReader);
3830 return std::make_pair(Start, DeclIDs.size());
3833 static bool EqualKey(key_type_ref a, key_type_ref b) {
3834 return a == b;
3837 hash_value_type ComputeHash(DeclarationNameKey Name) {
3838 return Name.getHash();
3841 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3842 assert(Writer.hasChain() &&
3843 "have reference to loaded module file but no chain?");
3845 using namespace llvm::support;
3847 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F),
3848 llvm::endianness::little);
3851 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3852 DeclarationNameKey Name,
3853 data_type_ref Lookup) {
3854 unsigned KeyLen = 1;
3855 switch (Name.getKind()) {
3856 case DeclarationName::Identifier:
3857 case DeclarationName::ObjCZeroArgSelector:
3858 case DeclarationName::ObjCOneArgSelector:
3859 case DeclarationName::ObjCMultiArgSelector:
3860 case DeclarationName::CXXLiteralOperatorName:
3861 case DeclarationName::CXXDeductionGuideName:
3862 KeyLen += 4;
3863 break;
3864 case DeclarationName::CXXOperatorName:
3865 KeyLen += 1;
3866 break;
3867 case DeclarationName::CXXConstructorName:
3868 case DeclarationName::CXXDestructorName:
3869 case DeclarationName::CXXConversionFunctionName:
3870 case DeclarationName::CXXUsingDirective:
3871 break;
3874 // 4 bytes for each DeclID.
3875 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3877 return emitULEBKeyDataLength(KeyLen, DataLen, Out);
3880 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3881 using namespace llvm::support;
3883 endian::Writer LE(Out, llvm::endianness::little);
3884 LE.write<uint8_t>(Name.getKind());
3885 switch (Name.getKind()) {
3886 case DeclarationName::Identifier:
3887 case DeclarationName::CXXLiteralOperatorName:
3888 case DeclarationName::CXXDeductionGuideName:
3889 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3890 return;
3891 case DeclarationName::ObjCZeroArgSelector:
3892 case DeclarationName::ObjCOneArgSelector:
3893 case DeclarationName::ObjCMultiArgSelector:
3894 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3895 return;
3896 case DeclarationName::CXXOperatorName:
3897 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3898 "Invalid operator?");
3899 LE.write<uint8_t>(Name.getOperatorKind());
3900 return;
3901 case DeclarationName::CXXConstructorName:
3902 case DeclarationName::CXXDestructorName:
3903 case DeclarationName::CXXConversionFunctionName:
3904 case DeclarationName::CXXUsingDirective:
3905 return;
3908 llvm_unreachable("Invalid name kind?");
3911 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3912 unsigned DataLen) {
3913 using namespace llvm::support;
3915 endian::Writer LE(Out, llvm::endianness::little);
3916 uint64_t Start = Out.tell(); (void)Start;
3917 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3918 LE.write<uint32_t>(DeclIDs[I]);
3919 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3923 } // namespace
3925 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3926 DeclContext *DC) {
3927 return Result.hasExternalDecls() &&
3928 DC->hasNeedToReconcileExternalVisibleStorage();
3931 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3932 DeclContext *DC) {
3933 for (auto *D : Result.getLookupResult())
3934 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3935 return false;
3937 return true;
3940 void
3941 ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3942 llvm::SmallVectorImpl<char> &LookupTable) {
3943 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3944 !ConstDC->hasLazyExternalLexicalLookups() &&
3945 "must call buildLookups first");
3947 // FIXME: We need to build the lookups table, which is logically const.
3948 auto *DC = const_cast<DeclContext*>(ConstDC);
3949 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3951 // Create the on-disk hash table representation.
3952 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3953 ASTDeclContextNameLookupTrait> Generator;
3954 ASTDeclContextNameLookupTrait Trait(*this);
3956 // The first step is to collect the declaration names which we need to
3957 // serialize into the name lookup table, and to collect them in a stable
3958 // order.
3959 SmallVector<DeclarationName, 16> Names;
3961 // We also build up small sets of the constructor and conversion function
3962 // names which are visible.
3963 llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
3965 for (auto &Lookup : *DC->buildLookup()) {
3966 auto &Name = Lookup.first;
3967 auto &Result = Lookup.second;
3969 // If there are no local declarations in our lookup result, we
3970 // don't need to write an entry for the name at all. If we can't
3971 // write out a lookup set without performing more deserialization,
3972 // just skip this entry.
3973 if (isLookupResultExternal(Result, DC) &&
3974 isLookupResultEntirelyExternal(Result, DC))
3975 continue;
3977 // We also skip empty results. If any of the results could be external and
3978 // the currently available results are empty, then all of the results are
3979 // external and we skip it above. So the only way we get here with an empty
3980 // results is when no results could have been external *and* we have
3981 // external results.
3983 // FIXME: While we might want to start emitting on-disk entries for negative
3984 // lookups into a decl context as an optimization, today we *have* to skip
3985 // them because there are names with empty lookup results in decl contexts
3986 // which we can't emit in any stable ordering: we lookup constructors and
3987 // conversion functions in the enclosing namespace scope creating empty
3988 // results for them. This in almost certainly a bug in Clang's name lookup,
3989 // but that is likely to be hard or impossible to fix and so we tolerate it
3990 // here by omitting lookups with empty results.
3991 if (Lookup.second.getLookupResult().empty())
3992 continue;
3994 switch (Lookup.first.getNameKind()) {
3995 default:
3996 Names.push_back(Lookup.first);
3997 break;
3999 case DeclarationName::CXXConstructorName:
4000 assert(isa<CXXRecordDecl>(DC) &&
4001 "Cannot have a constructor name outside of a class!");
4002 ConstructorNameSet.insert(Name);
4003 break;
4005 case DeclarationName::CXXConversionFunctionName:
4006 assert(isa<CXXRecordDecl>(DC) &&
4007 "Cannot have a conversion function name outside of a class!");
4008 ConversionNameSet.insert(Name);
4009 break;
4013 // Sort the names into a stable order.
4014 llvm::sort(Names);
4016 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
4017 // We need to establish an ordering of constructor and conversion function
4018 // names, and they don't have an intrinsic ordering.
4020 // First we try the easy case by forming the current context's constructor
4021 // name and adding that name first. This is a very useful optimization to
4022 // avoid walking the lexical declarations in many cases, and it also
4023 // handles the only case where a constructor name can come from some other
4024 // lexical context -- when that name is an implicit constructor merged from
4025 // another declaration in the redecl chain. Any non-implicit constructor or
4026 // conversion function which doesn't occur in all the lexical contexts
4027 // would be an ODR violation.
4028 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4029 Context->getCanonicalType(Context->getRecordType(D)));
4030 if (ConstructorNameSet.erase(ImplicitCtorName))
4031 Names.push_back(ImplicitCtorName);
4033 // If we still have constructors or conversion functions, we walk all the
4034 // names in the decl and add the constructors and conversion functions
4035 // which are visible in the order they lexically occur within the context.
4036 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4037 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4038 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4039 auto Name = ChildND->getDeclName();
4040 switch (Name.getNameKind()) {
4041 default:
4042 continue;
4044 case DeclarationName::CXXConstructorName:
4045 if (ConstructorNameSet.erase(Name))
4046 Names.push_back(Name);
4047 break;
4049 case DeclarationName::CXXConversionFunctionName:
4050 if (ConversionNameSet.erase(Name))
4051 Names.push_back(Name);
4052 break;
4055 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4056 break;
4059 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4060 "constructors by walking all the "
4061 "lexical members of the context.");
4062 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4063 "conversion functions by walking all "
4064 "the lexical members of the context.");
4067 // Next we need to do a lookup with each name into this decl context to fully
4068 // populate any results from external sources. We don't actually use the
4069 // results of these lookups because we only want to use the results after all
4070 // results have been loaded and the pointers into them will be stable.
4071 for (auto &Name : Names)
4072 DC->lookup(Name);
4074 // Now we need to insert the results for each name into the hash table. For
4075 // constructor names and conversion function names, we actually need to merge
4076 // all of the results for them into one list of results each and insert
4077 // those.
4078 SmallVector<NamedDecl *, 8> ConstructorDecls;
4079 SmallVector<NamedDecl *, 8> ConversionDecls;
4081 // Now loop over the names, either inserting them or appending for the two
4082 // special cases.
4083 for (auto &Name : Names) {
4084 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4086 switch (Name.getNameKind()) {
4087 default:
4088 Generator.insert(Name, Trait.getData(Result), Trait);
4089 break;
4091 case DeclarationName::CXXConstructorName:
4092 ConstructorDecls.append(Result.begin(), Result.end());
4093 break;
4095 case DeclarationName::CXXConversionFunctionName:
4096 ConversionDecls.append(Result.begin(), Result.end());
4097 break;
4101 // Handle our two special cases if we ended up having any. We arbitrarily use
4102 // the first declaration's name here because the name itself isn't part of
4103 // the key, only the kind of name is used.
4104 if (!ConstructorDecls.empty())
4105 Generator.insert(ConstructorDecls.front()->getDeclName(),
4106 Trait.getData(ConstructorDecls), Trait);
4107 if (!ConversionDecls.empty())
4108 Generator.insert(ConversionDecls.front()->getDeclName(),
4109 Trait.getData(ConversionDecls), Trait);
4111 // Create the on-disk hash table. Also emit the existing imported and
4112 // merged table if there is one.
4113 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4114 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
4117 /// Write the block containing all of the declaration IDs
4118 /// visible from the given DeclContext.
4120 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
4121 /// bitstream, or 0 if no block was written.
4122 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4123 DeclContext *DC) {
4124 // If we imported a key declaration of this namespace, write the visible
4125 // lookup results as an update record for it rather than including them
4126 // on this declaration. We will only look at key declarations on reload.
4127 if (isa<NamespaceDecl>(DC) && Chain &&
4128 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4129 // Only do this once, for the first local declaration of the namespace.
4130 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
4131 Prev = Prev->getPreviousDecl())
4132 if (!Prev->isFromASTFile())
4133 return 0;
4135 // Note that we need to emit an update record for the primary context.
4136 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4138 // Make sure all visible decls are written. They will be recorded later. We
4139 // do this using a side data structure so we can sort the names into
4140 // a deterministic order.
4141 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4142 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4143 LookupResults;
4144 if (Map) {
4145 LookupResults.reserve(Map->size());
4146 for (auto &Entry : *Map)
4147 LookupResults.push_back(
4148 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4151 llvm::sort(LookupResults, llvm::less_first());
4152 for (auto &NameAndResult : LookupResults) {
4153 DeclarationName Name = NameAndResult.first;
4154 DeclContext::lookup_result Result = NameAndResult.second;
4155 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4156 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4157 // We have to work around a name lookup bug here where negative lookup
4158 // results for these names get cached in namespace lookup tables (these
4159 // names should never be looked up in a namespace).
4160 assert(Result.empty() && "Cannot have a constructor or conversion "
4161 "function name in a namespace!");
4162 continue;
4165 for (NamedDecl *ND : Result)
4166 if (!ND->isFromASTFile())
4167 GetDeclRef(ND);
4170 return 0;
4173 if (DC->getPrimaryContext() != DC)
4174 return 0;
4176 // Skip contexts which don't support name lookup.
4177 if (!DC->isLookupContext())
4178 return 0;
4180 // If not in C++, we perform name lookup for the translation unit via the
4181 // IdentifierInfo chains, don't bother to build a visible-declarations table.
4182 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
4183 return 0;
4185 // Serialize the contents of the mapping used for lookup. Note that,
4186 // although we have two very different code paths, the serialized
4187 // representation is the same for both cases: a declaration name,
4188 // followed by a size, followed by references to the visible
4189 // declarations that have that name.
4190 uint64_t Offset = Stream.GetCurrentBitNo();
4191 StoredDeclsMap *Map = DC->buildLookup();
4192 if (!Map || Map->empty())
4193 return 0;
4195 // Create the on-disk hash table in a buffer.
4196 SmallString<4096> LookupTable;
4197 GenerateNameLookupTable(DC, LookupTable);
4199 // Write the lookup table
4200 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
4201 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
4202 LookupTable);
4203 ++NumVisibleDeclContexts;
4204 return Offset;
4207 /// Write an UPDATE_VISIBLE block for the given context.
4209 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4210 /// DeclContext in a dependent AST file. As such, they only exist for the TU
4211 /// (in C++), for namespaces, and for classes with forward-declared unscoped
4212 /// enumeration members (in C++11).
4213 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
4214 StoredDeclsMap *Map = DC->getLookupPtr();
4215 if (!Map || Map->empty())
4216 return;
4218 // Create the on-disk hash table in a buffer.
4219 SmallString<4096> LookupTable;
4220 GenerateNameLookupTable(DC, LookupTable);
4222 // If we're updating a namespace, select a key declaration as the key for the
4223 // update record; those are the only ones that will be checked on reload.
4224 if (isa<NamespaceDecl>(DC))
4225 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4227 // Write the lookup table
4228 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
4229 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
4232 /// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4233 void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
4234 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
4235 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4238 /// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4239 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
4240 if (!SemaRef.Context.getLangOpts().OpenCL)
4241 return;
4243 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4244 RecordData Record;
4245 for (const auto &I:Opts.OptMap) {
4246 AddString(I.getKey(), Record);
4247 auto V = I.getValue();
4248 Record.push_back(V.Supported ? 1 : 0);
4249 Record.push_back(V.Enabled ? 1 : 0);
4250 Record.push_back(V.WithPragma ? 1 : 0);
4251 Record.push_back(V.Avail);
4252 Record.push_back(V.Core);
4253 Record.push_back(V.Opt);
4255 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4257 void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4258 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4259 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4260 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4264 void ASTWriter::WriteObjCCategories() {
4265 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4266 RecordData Categories;
4268 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4269 unsigned Size = 0;
4270 unsigned StartIndex = Categories.size();
4272 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4274 // Allocate space for the size.
4275 Categories.push_back(0);
4277 // Add the categories.
4278 for (ObjCInterfaceDecl::known_categories_iterator
4279 Cat = Class->known_categories_begin(),
4280 CatEnd = Class->known_categories_end();
4281 Cat != CatEnd; ++Cat, ++Size) {
4282 assert(getDeclID(*Cat) != 0 && "Bogus category");
4283 AddDeclRef(*Cat, Categories);
4286 // Update the size.
4287 Categories[StartIndex] = Size;
4289 // Record this interface -> category map.
4290 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4291 CategoriesMap.push_back(CatInfo);
4294 // Sort the categories map by the definition ID, since the reader will be
4295 // performing binary searches on this information.
4296 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4298 // Emit the categories map.
4299 using namespace llvm;
4301 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4302 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4305 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
4307 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4308 Stream.EmitRecordWithBlob(AbbrevID, Record,
4309 reinterpret_cast<char *>(CategoriesMap.data()),
4310 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4312 // Emit the category lists.
4313 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4316 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4317 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4319 if (LPTMap.empty())
4320 return;
4322 RecordData Record;
4323 for (auto &LPTMapEntry : LPTMap) {
4324 const FunctionDecl *FD = LPTMapEntry.first;
4325 LateParsedTemplate &LPT = *LPTMapEntry.second;
4326 AddDeclRef(FD, Record);
4327 AddDeclRef(LPT.D, Record);
4328 Record.push_back(LPT.FPO.getAsOpaqueInt());
4329 Record.push_back(LPT.Toks.size());
4331 for (const auto &Tok : LPT.Toks) {
4332 AddToken(Tok, Record);
4335 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4338 /// Write the state of 'pragma clang optimize' at the end of the module.
4339 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4340 RecordData Record;
4341 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4342 AddSourceLocation(PragmaLoc, Record);
4343 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4346 /// Write the state of 'pragma ms_struct' at the end of the module.
4347 void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4348 RecordData Record;
4349 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4350 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4353 /// Write the state of 'pragma pointers_to_members' at the end of the
4354 //module.
4355 void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4356 RecordData Record;
4357 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4358 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4359 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4362 /// Write the state of 'pragma align/pack' at the end of the module.
4363 void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4364 // Don't serialize pragma align/pack state for modules, since it should only
4365 // take effect on a per-submodule basis.
4366 if (WritingModule)
4367 return;
4369 RecordData Record;
4370 AddAlignPackInfo(SemaRef.AlignPackStack.CurrentValue, Record);
4371 AddSourceLocation(SemaRef.AlignPackStack.CurrentPragmaLocation, Record);
4372 Record.push_back(SemaRef.AlignPackStack.Stack.size());
4373 for (const auto &StackEntry : SemaRef.AlignPackStack.Stack) {
4374 AddAlignPackInfo(StackEntry.Value, Record);
4375 AddSourceLocation(StackEntry.PragmaLocation, Record);
4376 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4377 AddString(StackEntry.StackSlotLabel, Record);
4379 Stream.EmitRecord(ALIGN_PACK_PRAGMA_OPTIONS, Record);
4382 /// Write the state of 'pragma float_control' at the end of the module.
4383 void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4384 // Don't serialize pragma float_control state for modules,
4385 // since it should only take effect on a per-submodule basis.
4386 if (WritingModule)
4387 return;
4389 RecordData Record;
4390 Record.push_back(SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4391 AddSourceLocation(SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4392 Record.push_back(SemaRef.FpPragmaStack.Stack.size());
4393 for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4394 Record.push_back(StackEntry.Value.getAsOpaqueInt());
4395 AddSourceLocation(StackEntry.PragmaLocation, Record);
4396 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4397 AddString(StackEntry.StackSlotLabel, Record);
4399 Stream.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS, Record);
4402 void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4403 ModuleFileExtensionWriter &Writer) {
4404 // Enter the extension block.
4405 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4407 // Emit the metadata record abbreviation.
4408 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4409 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4410 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4411 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4412 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4413 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4414 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4415 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
4417 // Emit the metadata record.
4418 RecordData Record;
4419 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4420 Record.push_back(EXTENSION_METADATA);
4421 Record.push_back(Metadata.MajorVersion);
4422 Record.push_back(Metadata.MinorVersion);
4423 Record.push_back(Metadata.BlockName.size());
4424 Record.push_back(Metadata.UserInfo.size());
4425 SmallString<64> Buffer;
4426 Buffer += Metadata.BlockName;
4427 Buffer += Metadata.UserInfo;
4428 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4430 // Emit the contents of the extension block.
4431 Writer.writeExtensionContents(SemaRef, Stream);
4433 // Exit the extension block.
4434 Stream.ExitBlock();
4437 //===----------------------------------------------------------------------===//
4438 // General Serialization Routines
4439 //===----------------------------------------------------------------------===//
4441 void ASTRecordWriter::AddAttr(const Attr *A) {
4442 auto &Record = *this;
4443 // FIXME: Clang can't handle the serialization/deserialization of
4444 // preferred_name properly now. See
4445 // https://github.com/llvm/llvm-project/issues/56490 for example.
4446 if (!A || (isa<PreferredNameAttr>(A) &&
4447 Writer->isWritingStdCXXNamedModules()))
4448 return Record.push_back(0);
4450 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4452 Record.AddIdentifierRef(A->getAttrName());
4453 Record.AddIdentifierRef(A->getScopeName());
4454 Record.AddSourceRange(A->getRange());
4455 Record.AddSourceLocation(A->getScopeLoc());
4456 Record.push_back(A->getParsedKind());
4457 Record.push_back(A->getSyntax());
4458 Record.push_back(A->getAttributeSpellingListIndexRaw());
4459 Record.push_back(A->isRegularKeywordAttribute());
4461 #include "clang/Serialization/AttrPCHWrite.inc"
4464 /// Emit the list of attributes to the specified record.
4465 void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4466 push_back(Attrs.size());
4467 for (const auto *A : Attrs)
4468 AddAttr(A);
4471 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4472 AddSourceLocation(Tok.getLocation(), Record);
4473 // FIXME: Should translate token kind to a stable encoding.
4474 Record.push_back(Tok.getKind());
4475 // FIXME: Should translate token flags to a stable encoding.
4476 Record.push_back(Tok.getFlags());
4478 if (Tok.isAnnotation()) {
4479 AddSourceLocation(Tok.getAnnotationEndLoc(), Record);
4480 switch (Tok.getKind()) {
4481 case tok::annot_pragma_loop_hint: {
4482 auto *Info = static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
4483 AddToken(Info->PragmaName, Record);
4484 AddToken(Info->Option, Record);
4485 Record.push_back(Info->Toks.size());
4486 for (const auto &T : Info->Toks)
4487 AddToken(T, Record);
4488 break;
4490 case tok::annot_pragma_pack: {
4491 auto *Info =
4492 static_cast<Sema::PragmaPackInfo *>(Tok.getAnnotationValue());
4493 Record.push_back(static_cast<unsigned>(Info->Action));
4494 AddString(Info->SlotLabel, Record);
4495 AddToken(Info->Alignment, Record);
4496 break;
4498 // Some annotation tokens do not use the PtrData field.
4499 case tok::annot_pragma_openmp:
4500 case tok::annot_pragma_openmp_end:
4501 case tok::annot_pragma_unused:
4502 break;
4503 default:
4504 llvm_unreachable("missing serialization code for annotation token");
4506 } else {
4507 Record.push_back(Tok.getLength());
4508 // FIXME: When reading literal tokens, reconstruct the literal pointer if it
4509 // is needed.
4510 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4514 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4515 Record.push_back(Str.size());
4516 Record.insert(Record.end(), Str.begin(), Str.end());
4519 bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4520 assert(Context && "should have context when outputting path");
4522 // Leave special file names as they are.
4523 StringRef PathStr(Path.data(), Path.size());
4524 if (PathStr == "<built-in>" || PathStr == "<command line>")
4525 return false;
4527 bool Changed =
4528 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4530 // Remove a prefix to make the path relative, if relevant.
4531 const char *PathBegin = Path.data();
4532 const char *PathPtr =
4533 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4534 if (PathPtr != PathBegin) {
4535 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4536 Changed = true;
4539 return Changed;
4542 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4543 SmallString<128> FilePath(Path);
4544 PreparePathForOutput(FilePath);
4545 AddString(FilePath, Record);
4548 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4549 StringRef Path) {
4550 SmallString<128> FilePath(Path);
4551 PreparePathForOutput(FilePath);
4552 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4555 void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4556 RecordDataImpl &Record) {
4557 Record.push_back(Version.getMajor());
4558 if (std::optional<unsigned> Minor = Version.getMinor())
4559 Record.push_back(*Minor + 1);
4560 else
4561 Record.push_back(0);
4562 if (std::optional<unsigned> Subminor = Version.getSubminor())
4563 Record.push_back(*Subminor + 1);
4564 else
4565 Record.push_back(0);
4568 /// Note that the identifier II occurs at the given offset
4569 /// within the identifier table.
4570 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4571 IdentID ID = IdentifierIDs[II];
4572 // Only store offsets new to this AST file. Other identifier names are looked
4573 // up earlier in the chain and thus don't need an offset.
4574 if (ID >= FirstIdentID)
4575 IdentifierOffsets[ID - FirstIdentID] = Offset;
4578 /// Note that the selector Sel occurs at the given offset
4579 /// within the method pool/selector table.
4580 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4581 unsigned ID = SelectorIDs[Sel];
4582 assert(ID && "Unknown selector");
4583 // Don't record offsets for selectors that are also available in a different
4584 // file.
4585 if (ID < FirstSelectorID)
4586 return;
4587 SelectorOffsets[ID - FirstSelectorID] = Offset;
4590 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4591 SmallVectorImpl<char> &Buffer,
4592 InMemoryModuleCache &ModuleCache,
4593 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4594 bool IncludeTimestamps, bool BuildingImplicitModule)
4595 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4596 IncludeTimestamps(IncludeTimestamps),
4597 BuildingImplicitModule(BuildingImplicitModule) {
4598 for (const auto &Ext : Extensions) {
4599 if (auto Writer = Ext->createExtensionWriter(*this))
4600 ModuleFileExtensionWriters.push_back(std::move(Writer));
4604 ASTWriter::~ASTWriter() = default;
4606 const LangOptions &ASTWriter::getLangOpts() const {
4607 assert(WritingAST && "can't determine lang opts when not writing AST");
4608 return Context->getLangOpts();
4611 time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4612 return IncludeTimestamps ? E->getModificationTime() : 0;
4615 ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
4616 Module *WritingModule, StringRef isysroot,
4617 bool ShouldCacheASTInMemory) {
4618 llvm::TimeTraceScope scope("WriteAST", OutputFile);
4619 WritingAST = true;
4621 ASTHasCompilerErrors =
4622 SemaRef.PP.getDiagnostics().hasUncompilableErrorOccurred();
4624 // Emit the file header.
4625 Stream.Emit((unsigned)'C', 8);
4626 Stream.Emit((unsigned)'P', 8);
4627 Stream.Emit((unsigned)'C', 8);
4628 Stream.Emit((unsigned)'H', 8);
4630 WriteBlockInfoBlock();
4632 Context = &SemaRef.Context;
4633 PP = &SemaRef.PP;
4634 this->WritingModule = WritingModule;
4635 ASTFileSignature Signature = WriteASTCore(SemaRef, isysroot, WritingModule);
4636 Context = nullptr;
4637 PP = nullptr;
4638 this->WritingModule = nullptr;
4639 this->BaseDirectory.clear();
4641 WritingAST = false;
4642 if (ShouldCacheASTInMemory) {
4643 // Construct MemoryBuffer and update buffer manager.
4644 ModuleCache.addBuiltPCM(OutputFile,
4645 llvm::MemoryBuffer::getMemBufferCopy(
4646 StringRef(Buffer.begin(), Buffer.size())));
4648 return Signature;
4651 template<typename Vector>
4652 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4653 ASTWriter::RecordData &Record) {
4654 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4655 I != E; ++I) {
4656 Writer.AddDeclRef(*I, Record);
4660 void ASTWriter::collectNonAffectingInputFiles() {
4661 SourceManager &SrcMgr = PP->getSourceManager();
4662 unsigned N = SrcMgr.local_sloc_entry_size();
4664 IsSLocAffecting.resize(N, true);
4666 if (!WritingModule)
4667 return;
4669 auto AffectingModuleMaps = GetAffectingModuleMaps(*PP, WritingModule);
4671 unsigned FileIDAdjustment = 0;
4672 unsigned OffsetAdjustment = 0;
4674 NonAffectingFileIDAdjustments.reserve(N);
4675 NonAffectingOffsetAdjustments.reserve(N);
4677 NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4678 NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4680 for (unsigned I = 1; I != N; ++I) {
4681 const SrcMgr::SLocEntry *SLoc = &SrcMgr.getLocalSLocEntry(I);
4682 FileID FID = FileID::get(I);
4683 assert(&SrcMgr.getSLocEntry(FID) == SLoc);
4685 if (!SLoc->isFile())
4686 continue;
4687 const SrcMgr::FileInfo &File = SLoc->getFile();
4688 const SrcMgr::ContentCache *Cache = &File.getContentCache();
4689 if (!Cache->OrigEntry)
4690 continue;
4692 if (!isModuleMap(File.getFileCharacteristic()) ||
4693 AffectingModuleMaps.empty() ||
4694 AffectingModuleMaps.find(Cache->OrigEntry) != AffectingModuleMaps.end())
4695 continue;
4697 IsSLocAffecting[I] = false;
4699 FileIDAdjustment += 1;
4700 // Even empty files take up one element in the offset table.
4701 OffsetAdjustment += SrcMgr.getFileIDSize(FID) + 1;
4703 // If the previous file was non-affecting as well, just extend its entry
4704 // with our information.
4705 if (!NonAffectingFileIDs.empty() &&
4706 NonAffectingFileIDs.back().ID == FID.ID - 1) {
4707 NonAffectingFileIDs.back() = FID;
4708 NonAffectingRanges.back().setEnd(SrcMgr.getLocForEndOfFile(FID));
4709 NonAffectingFileIDAdjustments.back() = FileIDAdjustment;
4710 NonAffectingOffsetAdjustments.back() = OffsetAdjustment;
4711 continue;
4714 NonAffectingFileIDs.push_back(FID);
4715 NonAffectingRanges.emplace_back(SrcMgr.getLocForStartOfFile(FID),
4716 SrcMgr.getLocForEndOfFile(FID));
4717 NonAffectingFileIDAdjustments.push_back(FileIDAdjustment);
4718 NonAffectingOffsetAdjustments.push_back(OffsetAdjustment);
4722 ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4723 Module *WritingModule) {
4724 using namespace llvm;
4726 bool isModule = WritingModule != nullptr;
4728 // Make sure that the AST reader knows to finalize itself.
4729 if (Chain)
4730 Chain->finalizeForWriting();
4732 ASTContext &Context = SemaRef.Context;
4733 Preprocessor &PP = SemaRef.PP;
4735 // This needs to be done very early, since everything that writes
4736 // SourceLocations or FileIDs depends on it.
4737 collectNonAffectingInputFiles();
4739 writeUnhashedControlBlock(PP, Context);
4741 // Set up predefined declaration IDs.
4742 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4743 if (D) {
4744 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4745 DeclIDs[D] = ID;
4748 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4749 PREDEF_DECL_TRANSLATION_UNIT_ID);
4750 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4751 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4752 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4753 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4754 PREDEF_DECL_OBJC_PROTOCOL_ID);
4755 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4756 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4757 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4758 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4759 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
4760 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
4761 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4762 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
4763 RegisterPredefDecl(Context.MSGuidTagDecl,
4764 PREDEF_DECL_BUILTIN_MS_GUID_ID);
4765 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
4766 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4767 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
4768 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4769 PREDEF_DECL_CF_CONSTANT_STRING_ID);
4770 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4771 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
4772 RegisterPredefDecl(Context.TypePackElementDecl,
4773 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
4775 // Build a record containing all of the tentative definitions in this file, in
4776 // TentativeDefinitions order. Generally, this record will be empty for
4777 // headers.
4778 RecordData TentativeDefinitions;
4779 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4781 // Build a record containing all of the file scoped decls in this file.
4782 RecordData UnusedFileScopedDecls;
4783 if (!isModule)
4784 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4785 UnusedFileScopedDecls);
4787 // Build a record containing all of the delegating constructors we still need
4788 // to resolve.
4789 RecordData DelegatingCtorDecls;
4790 if (!isModule)
4791 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4793 // Write the set of weak, undeclared identifiers. We always write the
4794 // entire table, since later PCH files in a PCH chain are only interested in
4795 // the results at the end of the chain.
4796 RecordData WeakUndeclaredIdentifiers;
4797 for (const auto &WeakUndeclaredIdentifierList :
4798 SemaRef.WeakUndeclaredIdentifiers) {
4799 const IdentifierInfo *const II = WeakUndeclaredIdentifierList.first;
4800 for (const auto &WI : WeakUndeclaredIdentifierList.second) {
4801 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4802 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4803 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4807 // Build a record containing all of the ext_vector declarations.
4808 RecordData ExtVectorDecls;
4809 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4811 // Build a record containing all of the VTable uses information.
4812 RecordData VTableUses;
4813 if (!SemaRef.VTableUses.empty()) {
4814 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4815 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4816 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4817 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4821 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4822 RecordData UnusedLocalTypedefNameCandidates;
4823 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4824 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4826 // Build a record containing all of pending implicit instantiations.
4827 RecordData PendingInstantiations;
4828 for (const auto &I : SemaRef.PendingInstantiations) {
4829 AddDeclRef(I.first, PendingInstantiations);
4830 AddSourceLocation(I.second, PendingInstantiations);
4832 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4833 "There are local ones at end of translation unit!");
4835 // Build a record containing some declaration references.
4836 RecordData SemaDeclRefs;
4837 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
4838 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4839 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4840 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
4843 RecordData CUDASpecialDeclRefs;
4844 if (Context.getcudaConfigureCallDecl()) {
4845 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4848 // Build a record containing all of the known namespaces.
4849 RecordData KnownNamespaces;
4850 for (const auto &I : SemaRef.KnownNamespaces) {
4851 if (!I.second)
4852 AddDeclRef(I.first, KnownNamespaces);
4855 // Build a record of all used, undefined objects that require definitions.
4856 RecordData UndefinedButUsed;
4858 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4859 SemaRef.getUndefinedButUsed(Undefined);
4860 for (const auto &I : Undefined) {
4861 AddDeclRef(I.first, UndefinedButUsed);
4862 AddSourceLocation(I.second, UndefinedButUsed);
4865 // Build a record containing all delete-expressions that we would like to
4866 // analyze later in AST.
4867 RecordData DeleteExprsToAnalyze;
4869 if (!isModule) {
4870 for (const auto &DeleteExprsInfo :
4871 SemaRef.getMismatchingDeleteExpressions()) {
4872 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4873 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4874 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4875 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4876 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4881 // Write the control block
4882 WriteControlBlock(PP, Context, isysroot);
4884 // Write the remaining AST contents.
4885 Stream.FlushToWord();
4886 ASTBlockRange.first = Stream.GetCurrentBitNo() >> 3;
4887 Stream.EnterSubblock(AST_BLOCK_ID, 5);
4888 ASTBlockStartOffset = Stream.GetCurrentBitNo();
4890 // This is so that older clang versions, before the introduction
4891 // of the control block, can read and reject the newer PCH format.
4893 RecordData Record = {VERSION_MAJOR};
4894 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4897 // Create a lexical update block containing all of the declarations in the
4898 // translation unit that do not come from other AST files.
4899 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4900 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4901 for (const auto *D : TU->noload_decls()) {
4902 if (!D->isFromASTFile()) {
4903 NewGlobalKindDeclPairs.push_back(D->getKind());
4904 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4908 auto Abv = std::make_shared<BitCodeAbbrev>();
4909 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4910 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4911 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
4913 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4914 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4915 bytes(NewGlobalKindDeclPairs));
4918 // And a visible updates block for the translation unit.
4919 Abv = std::make_shared<BitCodeAbbrev>();
4920 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4921 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4922 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4923 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
4924 WriteDeclContextVisibleUpdate(TU);
4926 // If we have any extern "C" names, write out a visible update for them.
4927 if (Context.ExternCContext)
4928 WriteDeclContextVisibleUpdate(Context.ExternCContext);
4930 // If the translation unit has an anonymous namespace, and we don't already
4931 // have an update block for it, write it as an update block.
4932 // FIXME: Why do we not do this if there's already an update block?
4933 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4934 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4935 if (Record.empty())
4936 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4939 // Add update records for all mangling numbers and static local numbers.
4940 // These aren't really update records, but this is a convenient way of
4941 // tagging this rare extra data onto the declarations.
4942 for (const auto &Number : Context.MangleNumbers)
4943 if (!Number.first->isFromASTFile())
4944 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4945 Number.second));
4946 for (const auto &Number : Context.StaticLocalNumbers)
4947 if (!Number.first->isFromASTFile())
4948 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4949 Number.second));
4951 // Make sure visible decls, added to DeclContexts previously loaded from
4952 // an AST file, are registered for serialization. Likewise for template
4953 // specializations added to imported templates.
4954 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
4955 GetDeclRef(I);
4958 // Make sure all decls associated with an identifier are registered for
4959 // serialization, if we're storing decls with identifiers.
4960 if (!WritingModule || !getLangOpts().CPlusPlus) {
4961 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4962 for (const auto &ID : PP.getIdentifierTable()) {
4963 const IdentifierInfo *II = ID.second;
4964 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4965 IIs.push_back(II);
4967 // Sort the identifiers to visit based on their name.
4968 llvm::sort(IIs, llvm::deref<std::less<>>());
4969 for (const IdentifierInfo *II : IIs)
4970 for (const Decl *D : SemaRef.IdResolver.decls(II))
4971 GetDeclRef(D);
4974 // For method pool in the module, if it contains an entry for a selector,
4975 // the entry should be complete, containing everything introduced by that
4976 // module and all modules it imports. It's possible that the entry is out of
4977 // date, so we need to pull in the new content here.
4979 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4980 // safe, we copy all selectors out.
4981 llvm::SmallVector<Selector, 256> AllSelectors;
4982 for (auto &SelectorAndID : SelectorIDs)
4983 AllSelectors.push_back(SelectorAndID.first);
4984 for (auto &Selector : AllSelectors)
4985 SemaRef.updateOutOfDateSelector(Selector);
4987 // Form the record of special types.
4988 RecordData SpecialTypes;
4989 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
4990 AddTypeRef(Context.getFILEType(), SpecialTypes);
4991 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4992 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4993 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4994 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
4995 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
4996 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
4998 if (Chain) {
4999 // Write the mapping information describing our module dependencies and how
5000 // each of those modules were mapped into our own offset/ID space, so that
5001 // the reader can build the appropriate mapping to its own offset/ID space.
5002 // The map consists solely of a blob with the following format:
5003 // *(module-kind:i8
5004 // module-name-len:i16 module-name:len*i8
5005 // source-location-offset:i32
5006 // identifier-id:i32
5007 // preprocessed-entity-id:i32
5008 // macro-definition-id:i32
5009 // submodule-id:i32
5010 // selector-id:i32
5011 // declaration-id:i32
5012 // c++-base-specifiers-id:i32
5013 // type-id:i32)
5015 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
5016 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
5017 // module name. Otherwise, it is the module file name.
5018 auto Abbrev = std::make_shared<BitCodeAbbrev>();
5019 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
5020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
5021 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
5022 SmallString<2048> Buffer;
5024 llvm::raw_svector_ostream Out(Buffer);
5025 for (ModuleFile &M : Chain->ModuleMgr) {
5026 using namespace llvm::support;
5028 endian::Writer LE(Out, llvm::endianness::little);
5029 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
5030 StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
5031 LE.write<uint16_t>(Name.size());
5032 Out.write(Name.data(), Name.size());
5034 // Note: if a base ID was uint max, it would not be possible to load
5035 // another module after it or have more than one entity inside it.
5036 uint32_t None = std::numeric_limits<uint32_t>::max();
5038 auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) {
5039 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5040 if (ShouldWrite)
5041 LE.write<uint32_t>(BaseID);
5042 else
5043 LE.write<uint32_t>(None);
5046 // These values should be unique within a chain, since they will be read
5047 // as keys into ContinuousRangeMaps.
5048 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
5049 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
5050 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5051 writeBaseIDOrNone(M.BasePreprocessedEntityID,
5052 M.NumPreprocessedEntities);
5053 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5054 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5055 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
5056 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
5059 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
5060 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
5061 Buffer.data(), Buffer.size());
5064 // Build a record containing all of the DeclsToCheckForDeferredDiags.
5065 SmallVector<serialization::DeclID, 64> DeclsToCheckForDeferredDiags;
5066 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
5067 DeclsToCheckForDeferredDiags.push_back(GetDeclRef(D));
5069 RecordData DeclUpdatesOffsetsRecord;
5071 // Keep writing types, declarations, and declaration update records
5072 // until we've emitted all of them.
5073 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5074 DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5075 WriteTypeAbbrevs();
5076 WriteDeclAbbrevs();
5077 do {
5078 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5079 while (!DeclTypesToEmit.empty()) {
5080 DeclOrType DOT = DeclTypesToEmit.front();
5081 DeclTypesToEmit.pop();
5082 if (DOT.isType())
5083 WriteType(DOT.getType());
5084 else
5085 WriteDecl(Context, DOT.getDecl());
5087 } while (!DeclUpdates.empty());
5088 Stream.ExitBlock();
5090 DoneWritingDeclsAndTypes = true;
5092 // These things can only be done once we've written out decls and types.
5093 WriteTypeDeclOffsets();
5094 if (!DeclUpdatesOffsetsRecord.empty())
5095 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
5096 WriteFileDeclIDsMap();
5097 WriteSourceManagerBlock(Context.getSourceManager(), PP);
5098 WriteComments();
5099 WritePreprocessor(PP, isModule);
5100 WriteHeaderSearch(PP.getHeaderSearchInfo());
5101 WriteSelectors(SemaRef);
5102 WriteReferencedSelectorsPool(SemaRef);
5103 WriteLateParsedTemplates(SemaRef);
5104 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
5105 WriteFPPragmaOptions(SemaRef.CurFPFeatureOverrides());
5106 WriteOpenCLExtensions(SemaRef);
5107 WriteCUDAPragmas(SemaRef);
5109 // If we're emitting a module, write out the submodule information.
5110 if (WritingModule)
5111 WriteSubmodules(WritingModule);
5113 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5115 // Write the record containing external, unnamed definitions.
5116 if (!EagerlyDeserializedDecls.empty())
5117 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
5119 if (!ModularCodegenDecls.empty())
5120 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5122 // Write the record containing tentative definitions.
5123 if (!TentativeDefinitions.empty())
5124 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
5126 // Write the record containing unused file scoped decls.
5127 if (!UnusedFileScopedDecls.empty())
5128 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
5130 // Write the record containing weak undeclared identifiers.
5131 if (!WeakUndeclaredIdentifiers.empty())
5132 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
5133 WeakUndeclaredIdentifiers);
5135 // Write the record containing ext_vector type names.
5136 if (!ExtVectorDecls.empty())
5137 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
5139 // Write the record containing VTable uses information.
5140 if (!VTableUses.empty())
5141 Stream.EmitRecord(VTABLE_USES, VTableUses);
5143 // Write the record containing potentially unused local typedefs.
5144 if (!UnusedLocalTypedefNameCandidates.empty())
5145 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5146 UnusedLocalTypedefNameCandidates);
5148 // Write the record containing pending implicit instantiations.
5149 if (!PendingInstantiations.empty())
5150 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
5152 // Write the record containing declaration references of Sema.
5153 if (!SemaDeclRefs.empty())
5154 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
5156 // Write the record containing decls to be checked for deferred diags.
5157 if (!DeclsToCheckForDeferredDiags.empty())
5158 Stream.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
5159 DeclsToCheckForDeferredDiags);
5161 // Write the record containing CUDA-specific declaration references.
5162 if (!CUDASpecialDeclRefs.empty())
5163 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
5165 // Write the delegating constructors.
5166 if (!DelegatingCtorDecls.empty())
5167 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
5169 // Write the known namespaces.
5170 if (!KnownNamespaces.empty())
5171 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
5173 // Write the undefined internal functions and variables, and inline functions.
5174 if (!UndefinedButUsed.empty())
5175 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
5177 if (!DeleteExprsToAnalyze.empty())
5178 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5180 // Write the visible updates to DeclContexts.
5181 for (auto *DC : UpdatedDeclContexts)
5182 WriteDeclContextVisibleUpdate(DC);
5184 if (!WritingModule) {
5185 // Write the submodules that were imported, if any.
5186 struct ModuleInfo {
5187 uint64_t ID;
5188 Module *M;
5189 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5191 llvm::SmallVector<ModuleInfo, 64> Imports;
5192 for (const auto *I : Context.local_imports()) {
5193 assert(SubmoduleIDs.contains(I->getImportedModule()));
5194 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5195 I->getImportedModule()));
5198 if (!Imports.empty()) {
5199 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5200 return A.ID < B.ID;
5202 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5203 return A.ID == B.ID;
5206 // Sort and deduplicate module IDs.
5207 llvm::sort(Imports, Cmp);
5208 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
5209 Imports.end());
5211 RecordData ImportedModules;
5212 for (const auto &Import : Imports) {
5213 ImportedModules.push_back(Import.ID);
5214 // FIXME: If the module has macros imported then later has declarations
5215 // imported, this location won't be the right one as a location for the
5216 // declaration imports.
5217 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
5220 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5224 WriteObjCCategories();
5225 if(!WritingModule) {
5226 WriteOptimizePragmaOptions(SemaRef);
5227 WriteMSStructPragmaOptions(SemaRef);
5228 WriteMSPointersToMembersPragmaOptions(SemaRef);
5230 WritePackPragmaOptions(SemaRef);
5231 WriteFloatControlPragmaOptions(SemaRef);
5233 // Some simple statistics
5234 RecordData::value_type Record[] = {
5235 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
5236 Stream.EmitRecord(STATISTICS, Record);
5237 Stream.ExitBlock();
5238 Stream.FlushToWord();
5239 ASTBlockRange.second = Stream.GetCurrentBitNo() >> 3;
5241 // Write the module file extension blocks.
5242 for (const auto &ExtWriter : ModuleFileExtensionWriters)
5243 WriteModuleFileExtension(SemaRef, *ExtWriter);
5245 return backpatchSignature();
5248 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
5249 if (DeclUpdates.empty())
5250 return;
5252 DeclUpdateMap LocalUpdates;
5253 LocalUpdates.swap(DeclUpdates);
5255 for (auto &DeclUpdate : LocalUpdates) {
5256 const Decl *D = DeclUpdate.first;
5258 bool HasUpdatedBody = false;
5259 bool HasAddedVarDefinition = false;
5260 RecordData RecordData;
5261 ASTRecordWriter Record(*this, RecordData);
5262 for (auto &Update : DeclUpdate.second) {
5263 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5265 // An updated body is emitted last, so that the reader doesn't need
5266 // to skip over the lazy body to reach statements for other records.
5267 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5268 HasUpdatedBody = true;
5269 else if (Kind == UPD_CXX_ADDED_VAR_DEFINITION)
5270 HasAddedVarDefinition = true;
5271 else
5272 Record.push_back(Kind);
5274 switch (Kind) {
5275 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5276 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5277 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
5278 assert(Update.getDecl() && "no decl to add?");
5279 Record.push_back(GetDeclRef(Update.getDecl()));
5280 break;
5282 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
5283 case UPD_CXX_ADDED_VAR_DEFINITION:
5284 break;
5286 case UPD_CXX_POINT_OF_INSTANTIATION:
5287 // FIXME: Do we need to also save the template specialization kind here?
5288 Record.AddSourceLocation(Update.getLoc());
5289 break;
5291 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
5292 Record.AddStmt(const_cast<Expr *>(
5293 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
5294 break;
5296 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5297 Record.AddStmt(
5298 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5299 break;
5301 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5302 auto *RD = cast<CXXRecordDecl>(D);
5303 UpdatedDeclContexts.insert(RD->getPrimaryContext());
5304 Record.push_back(RD->isParamDestroyedInCallee());
5305 Record.push_back(llvm::to_underlying(RD->getArgPassingRestrictions()));
5306 Record.AddCXXDefinitionData(RD);
5307 Record.AddOffset(WriteDeclContextLexicalBlock(
5308 *Context, const_cast<CXXRecordDecl *>(RD)));
5310 // This state is sometimes updated by template instantiation, when we
5311 // switch from the specialization referring to the template declaration
5312 // to it referring to the template definition.
5313 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5314 Record.push_back(MSInfo->getTemplateSpecializationKind());
5315 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
5316 } else {
5317 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5318 Record.push_back(Spec->getTemplateSpecializationKind());
5319 Record.AddSourceLocation(Spec->getPointOfInstantiation());
5321 // The instantiation might have been resolved to a partial
5322 // specialization. If so, record which one.
5323 auto From = Spec->getInstantiatedFrom();
5324 if (auto PartialSpec =
5325 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5326 Record.push_back(true);
5327 Record.AddDeclRef(PartialSpec);
5328 Record.AddTemplateArgumentList(
5329 &Spec->getTemplateInstantiationArgs());
5330 } else {
5331 Record.push_back(false);
5334 Record.push_back(RD->getTagKind());
5335 Record.AddSourceLocation(RD->getLocation());
5336 Record.AddSourceLocation(RD->getBeginLoc());
5337 Record.AddSourceRange(RD->getBraceRange());
5339 // Instantiation may change attributes; write them all out afresh.
5340 Record.push_back(D->hasAttrs());
5341 if (D->hasAttrs())
5342 Record.AddAttributes(D->getAttrs());
5344 // FIXME: Ensure we don't get here for explicit instantiations.
5345 break;
5348 case UPD_CXX_RESOLVED_DTOR_DELETE:
5349 Record.AddDeclRef(Update.getDecl());
5350 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
5351 break;
5353 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
5354 auto prototype =
5355 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
5356 Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
5357 break;
5360 case UPD_CXX_DEDUCED_RETURN_TYPE:
5361 Record.push_back(GetOrCreateTypeID(Update.getType()));
5362 break;
5364 case UPD_DECL_MARKED_USED:
5365 break;
5367 case UPD_MANGLING_NUMBER:
5368 case UPD_STATIC_LOCAL_NUMBER:
5369 Record.push_back(Update.getNumber());
5370 break;
5372 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
5373 Record.AddSourceRange(
5374 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
5375 break;
5377 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5378 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5379 Record.push_back(A->getAllocatorType());
5380 Record.AddStmt(A->getAllocator());
5381 Record.AddStmt(A->getAlignment());
5382 Record.AddSourceRange(A->getRange());
5383 break;
5386 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5387 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
5388 Record.AddSourceRange(
5389 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5390 break;
5392 case UPD_DECL_EXPORTED:
5393 Record.push_back(getSubmoduleID(Update.getModule()));
5394 break;
5396 case UPD_ADDED_ATTR_TO_RECORD:
5397 Record.AddAttributes(llvm::ArrayRef(Update.getAttr()));
5398 break;
5402 // Add a trailing update record, if any. These must go last because we
5403 // lazily load their attached statement.
5404 if (HasUpdatedBody) {
5405 const auto *Def = cast<FunctionDecl>(D);
5406 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
5407 Record.push_back(Def->isInlined());
5408 Record.AddSourceLocation(Def->getInnerLocStart());
5409 Record.AddFunctionDefinition(Def);
5410 } else if (HasAddedVarDefinition) {
5411 const auto *VD = cast<VarDecl>(D);
5412 Record.push_back(UPD_CXX_ADDED_VAR_DEFINITION);
5413 Record.push_back(VD->isInline());
5414 Record.push_back(VD->isInlineSpecified());
5415 Record.AddVarDeclInit(VD);
5418 OffsetsRecord.push_back(GetDeclRef(D));
5419 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
5423 void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
5424 RecordDataImpl &Record) {
5425 uint32_t Raw = Sema::AlignPackInfo::getRawEncoding(Info);
5426 Record.push_back(Raw);
5429 FileID ASTWriter::getAdjustedFileID(FileID FID) const {
5430 if (FID.isInvalid() || PP->getSourceManager().isLoadedFileID(FID) ||
5431 NonAffectingFileIDs.empty())
5432 return FID;
5433 auto It = llvm::lower_bound(NonAffectingFileIDs, FID);
5434 unsigned Idx = std::distance(NonAffectingFileIDs.begin(), It);
5435 unsigned Offset = NonAffectingFileIDAdjustments[Idx];
5436 return FileID::get(FID.getOpaqueValue() - Offset);
5439 unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
5440 unsigned NumCreatedFIDs = PP->getSourceManager()
5441 .getLocalSLocEntry(FID.ID)
5442 .getFile()
5443 .NumCreatedFIDs;
5445 unsigned AdjustedNumCreatedFIDs = 0;
5446 for (unsigned I = FID.ID, N = I + NumCreatedFIDs; I != N; ++I)
5447 if (IsSLocAffecting[I])
5448 ++AdjustedNumCreatedFIDs;
5449 return AdjustedNumCreatedFIDs;
5452 SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
5453 if (Loc.isInvalid())
5454 return Loc;
5455 return Loc.getLocWithOffset(-getAdjustment(Loc.getOffset()));
5458 SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
5459 return SourceRange(getAdjustedLocation(Range.getBegin()),
5460 getAdjustedLocation(Range.getEnd()));
5463 SourceLocation::UIntTy
5464 ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
5465 return Offset - getAdjustment(Offset);
5468 SourceLocation::UIntTy
5469 ASTWriter::getAdjustment(SourceLocation::UIntTy Offset) const {
5470 if (NonAffectingRanges.empty())
5471 return 0;
5473 if (PP->getSourceManager().isLoadedOffset(Offset))
5474 return 0;
5476 if (Offset > NonAffectingRanges.back().getEnd().getOffset())
5477 return NonAffectingOffsetAdjustments.back();
5479 if (Offset < NonAffectingRanges.front().getBegin().getOffset())
5480 return 0;
5482 auto Contains = [](const SourceRange &Range, SourceLocation::UIntTy Offset) {
5483 return Range.getEnd().getOffset() < Offset;
5486 auto It = llvm::lower_bound(NonAffectingRanges, Offset, Contains);
5487 unsigned Idx = std::distance(NonAffectingRanges.begin(), It);
5488 return NonAffectingOffsetAdjustments[Idx];
5491 void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
5492 Record.push_back(getAdjustedFileID(FID).getOpaqueValue());
5495 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
5496 SourceLocationSequence *Seq) {
5497 Loc = getAdjustedLocation(Loc);
5498 Record.push_back(SourceLocationEncoding::encode(Loc, Seq));
5501 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record,
5502 SourceLocationSequence *Seq) {
5503 AddSourceLocation(Range.getBegin(), Record, Seq);
5504 AddSourceLocation(Range.getEnd(), Record, Seq);
5507 void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5508 AddAPInt(Value.bitcastToAPInt());
5511 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
5512 Record.push_back(getIdentifierRef(II));
5515 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
5516 if (!II)
5517 return 0;
5519 IdentID &ID = IdentifierIDs[II];
5520 if (ID == 0)
5521 ID = NextIdentID++;
5522 return ID;
5525 MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
5526 // Don't emit builtin macros like __LINE__ to the AST file unless they
5527 // have been redefined by the header (in which case they are not
5528 // isBuiltinMacro).
5529 if (!MI || MI->isBuiltinMacro())
5530 return 0;
5532 MacroID &ID = MacroIDs[MI];
5533 if (ID == 0) {
5534 ID = NextMacroID++;
5535 MacroInfoToEmitData Info = { Name, MI, ID };
5536 MacroInfosToEmit.push_back(Info);
5538 return ID;
5541 MacroID ASTWriter::getMacroID(MacroInfo *MI) {
5542 if (!MI || MI->isBuiltinMacro())
5543 return 0;
5545 assert(MacroIDs.contains(MI) && "Macro not emitted!");
5546 return MacroIDs[MI];
5549 uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
5550 return IdentMacroDirectivesOffsetMap.lookup(Name);
5553 void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5554 Record->push_back(Writer->getSelectorRef(SelRef));
5557 SelectorID ASTWriter::getSelectorRef(Selector Sel) {
5558 if (Sel.getAsOpaquePtr() == nullptr) {
5559 return 0;
5562 SelectorID SID = SelectorIDs[Sel];
5563 if (SID == 0 && Chain) {
5564 // This might trigger a ReadSelector callback, which will set the ID for
5565 // this selector.
5566 Chain->LoadSelector(Sel);
5567 SID = SelectorIDs[Sel];
5569 if (SID == 0) {
5570 SID = NextSelectorID++;
5571 SelectorIDs[Sel] = SID;
5573 return SID;
5576 void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5577 AddDeclRef(Temp->getDestructor());
5580 void ASTRecordWriter::AddTemplateArgumentLocInfo(
5581 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
5582 switch (Kind) {
5583 case TemplateArgument::Expression:
5584 AddStmt(Arg.getAsExpr());
5585 break;
5586 case TemplateArgument::Type:
5587 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
5588 break;
5589 case TemplateArgument::Template:
5590 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5591 AddSourceLocation(Arg.getTemplateNameLoc());
5592 break;
5593 case TemplateArgument::TemplateExpansion:
5594 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5595 AddSourceLocation(Arg.getTemplateNameLoc());
5596 AddSourceLocation(Arg.getTemplateEllipsisLoc());
5597 break;
5598 case TemplateArgument::Null:
5599 case TemplateArgument::Integral:
5600 case TemplateArgument::Declaration:
5601 case TemplateArgument::NullPtr:
5602 case TemplateArgument::Pack:
5603 // FIXME: Is this right?
5604 break;
5608 void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5609 AddTemplateArgument(Arg.getArgument());
5611 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5612 bool InfoHasSameExpr
5613 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5614 Record->push_back(InfoHasSameExpr);
5615 if (InfoHasSameExpr)
5616 return; // Avoid storing the same expr twice.
5618 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
5621 void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
5622 if (!TInfo) {
5623 AddTypeRef(QualType());
5624 return;
5627 AddTypeRef(TInfo->getType());
5628 AddTypeLoc(TInfo->getTypeLoc());
5631 void ASTRecordWriter::AddTypeLoc(TypeLoc TL, LocSeq *OuterSeq) {
5632 LocSeq::State Seq(OuterSeq);
5633 TypeLocWriter TLW(*this, Seq);
5634 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
5635 TLW.Visit(TL);
5638 void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
5639 Record.push_back(GetOrCreateTypeID(T));
5642 TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
5643 assert(Context);
5644 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5645 if (T.isNull())
5646 return TypeIdx();
5647 assert(!T.getLocalFastQualifiers());
5649 TypeIdx &Idx = TypeIdxs[T];
5650 if (Idx.getIndex() == 0) {
5651 if (DoneWritingDeclsAndTypes) {
5652 assert(0 && "New type seen after serializing all the types to emit!");
5653 return TypeIdx();
5656 // We haven't seen this type before. Assign it a new ID and put it
5657 // into the queue of types to emit.
5658 Idx = TypeIdx(NextTypeID++);
5659 DeclTypesToEmit.push(T);
5661 return Idx;
5665 TypeID ASTWriter::getTypeID(QualType T) const {
5666 assert(Context);
5667 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5668 if (T.isNull())
5669 return TypeIdx();
5670 assert(!T.getLocalFastQualifiers());
5672 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5673 assert(I != TypeIdxs.end() && "Type not emitted!");
5674 return I->second;
5678 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
5679 Record.push_back(GetDeclRef(D));
5682 DeclID ASTWriter::GetDeclRef(const Decl *D) {
5683 assert(WritingAST && "Cannot request a declaration ID before AST writing");
5685 if (!D) {
5686 return 0;
5689 // If D comes from an AST file, its declaration ID is already known and
5690 // fixed.
5691 if (D->isFromASTFile())
5692 return D->getGlobalID();
5694 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
5695 DeclID &ID = DeclIDs[D];
5696 if (ID == 0) {
5697 if (DoneWritingDeclsAndTypes) {
5698 assert(0 && "New decl seen after serializing all the decls to emit!");
5699 return 0;
5702 // We haven't seen this declaration before. Give it a new ID and
5703 // enqueue it in the list of declarations to emit.
5704 ID = NextDeclID++;
5705 DeclTypesToEmit.push(const_cast<Decl *>(D));
5708 return ID;
5711 DeclID ASTWriter::getDeclID(const Decl *D) {
5712 if (!D)
5713 return 0;
5715 // If D comes from an AST file, its declaration ID is already known and
5716 // fixed.
5717 if (D->isFromASTFile())
5718 return D->getGlobalID();
5720 assert(DeclIDs.contains(D) && "Declaration not emitted!");
5721 return DeclIDs[D];
5724 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5725 assert(ID);
5726 assert(D);
5728 SourceLocation Loc = D->getLocation();
5729 if (Loc.isInvalid())
5730 return;
5732 // We only keep track of the file-level declarations of each file.
5733 if (!D->getLexicalDeclContext()->isFileContext())
5734 return;
5735 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5736 // a function/objc method, should not have TU as lexical context.
5737 // TemplateTemplateParmDecls that are part of an alias template, should not
5738 // have TU as lexical context.
5739 if (isa<ParmVarDecl, TemplateTemplateParmDecl>(D))
5740 return;
5742 SourceManager &SM = Context->getSourceManager();
5743 SourceLocation FileLoc = SM.getFileLoc(Loc);
5744 assert(SM.isLocalSourceLocation(FileLoc));
5745 FileID FID;
5746 unsigned Offset;
5747 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5748 if (FID.isInvalid())
5749 return;
5750 assert(SM.getSLocEntry(FID).isFile());
5751 assert(IsSLocAffecting[FID.ID]);
5753 std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
5754 if (!Info)
5755 Info = std::make_unique<DeclIDInFileInfo>();
5757 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5758 LocDeclIDsTy &Decls = Info->DeclIDs;
5759 Decls.push_back(LocDecl);
5762 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5763 assert(needsAnonymousDeclarationNumber(D) &&
5764 "expected an anonymous declaration");
5766 // Number the anonymous declarations within this context, if we've not
5767 // already done so.
5768 auto It = AnonymousDeclarationNumbers.find(D);
5769 if (It == AnonymousDeclarationNumbers.end()) {
5770 auto *DC = D->getLexicalDeclContext();
5771 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5772 AnonymousDeclarationNumbers[ND] = Number;
5775 It = AnonymousDeclarationNumbers.find(D);
5776 assert(It != AnonymousDeclarationNumbers.end() &&
5777 "declaration not found within its lexical context");
5780 return It->second;
5783 void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5784 DeclarationName Name) {
5785 switch (Name.getNameKind()) {
5786 case DeclarationName::CXXConstructorName:
5787 case DeclarationName::CXXDestructorName:
5788 case DeclarationName::CXXConversionFunctionName:
5789 AddTypeSourceInfo(DNLoc.getNamedTypeInfo());
5790 break;
5792 case DeclarationName::CXXOperatorName:
5793 AddSourceRange(DNLoc.getCXXOperatorNameRange());
5794 break;
5796 case DeclarationName::CXXLiteralOperatorName:
5797 AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc());
5798 break;
5800 case DeclarationName::Identifier:
5801 case DeclarationName::ObjCZeroArgSelector:
5802 case DeclarationName::ObjCOneArgSelector:
5803 case DeclarationName::ObjCMultiArgSelector:
5804 case DeclarationName::CXXUsingDirective:
5805 case DeclarationName::CXXDeductionGuideName:
5806 break;
5810 void ASTRecordWriter::AddDeclarationNameInfo(
5811 const DeclarationNameInfo &NameInfo) {
5812 AddDeclarationName(NameInfo.getName());
5813 AddSourceLocation(NameInfo.getLoc());
5814 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
5817 void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5818 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5819 Record->push_back(Info.NumTemplParamLists);
5820 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
5821 AddTemplateParameterList(Info.TemplParamLists[i]);
5824 void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
5825 // Nested name specifiers usually aren't too long. I think that 8 would
5826 // typically accommodate the vast majority.
5827 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5829 // Push each of the nested-name-specifiers's onto a stack for
5830 // serialization in reverse order.
5831 while (NNS) {
5832 NestedNames.push_back(NNS);
5833 NNS = NNS.getPrefix();
5836 Record->push_back(NestedNames.size());
5837 while(!NestedNames.empty()) {
5838 NNS = NestedNames.pop_back_val();
5839 NestedNameSpecifier::SpecifierKind Kind
5840 = NNS.getNestedNameSpecifier()->getKind();
5841 Record->push_back(Kind);
5842 switch (Kind) {
5843 case NestedNameSpecifier::Identifier:
5844 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5845 AddSourceRange(NNS.getLocalSourceRange());
5846 break;
5848 case NestedNameSpecifier::Namespace:
5849 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5850 AddSourceRange(NNS.getLocalSourceRange());
5851 break;
5853 case NestedNameSpecifier::NamespaceAlias:
5854 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5855 AddSourceRange(NNS.getLocalSourceRange());
5856 break;
5858 case NestedNameSpecifier::TypeSpec:
5859 case NestedNameSpecifier::TypeSpecWithTemplate:
5860 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5861 AddTypeRef(NNS.getTypeLoc().getType());
5862 AddTypeLoc(NNS.getTypeLoc());
5863 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5864 break;
5866 case NestedNameSpecifier::Global:
5867 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5868 break;
5870 case NestedNameSpecifier::Super:
5871 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5872 AddSourceRange(NNS.getLocalSourceRange());
5873 break;
5878 void ASTRecordWriter::AddTemplateParameterList(
5879 const TemplateParameterList *TemplateParams) {
5880 assert(TemplateParams && "No TemplateParams!");
5881 AddSourceLocation(TemplateParams->getTemplateLoc());
5882 AddSourceLocation(TemplateParams->getLAngleLoc());
5883 AddSourceLocation(TemplateParams->getRAngleLoc());
5885 Record->push_back(TemplateParams->size());
5886 for (const auto &P : *TemplateParams)
5887 AddDeclRef(P);
5888 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
5889 Record->push_back(true);
5890 AddStmt(const_cast<Expr*>(RequiresClause));
5891 } else {
5892 Record->push_back(false);
5896 /// Emit a template argument list.
5897 void ASTRecordWriter::AddTemplateArgumentList(
5898 const TemplateArgumentList *TemplateArgs) {
5899 assert(TemplateArgs && "No TemplateArgs!");
5900 Record->push_back(TemplateArgs->size());
5901 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
5902 AddTemplateArgument(TemplateArgs->get(i));
5905 void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5906 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
5907 assert(ASTTemplArgList && "No ASTTemplArgList!");
5908 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5909 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5910 Record->push_back(ASTTemplArgList->NumTemplateArgs);
5911 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5912 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5913 AddTemplateArgumentLoc(TemplArgs[i]);
5916 void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5917 Record->push_back(Set.size());
5918 for (ASTUnresolvedSet::const_iterator
5919 I = Set.begin(), E = Set.end(); I != E; ++I) {
5920 AddDeclRef(I.getDecl());
5921 Record->push_back(I.getAccess());
5925 // FIXME: Move this out of the main ASTRecordWriter interface.
5926 void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5927 Record->push_back(Base.isVirtual());
5928 Record->push_back(Base.isBaseOfClass());
5929 Record->push_back(Base.getAccessSpecifierAsWritten());
5930 Record->push_back(Base.getInheritConstructors());
5931 AddTypeSourceInfo(Base.getTypeSourceInfo());
5932 AddSourceRange(Base.getSourceRange());
5933 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5934 : SourceLocation());
5937 static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5938 ArrayRef<CXXBaseSpecifier> Bases) {
5939 ASTWriter::RecordData Record;
5940 ASTRecordWriter Writer(W, Record);
5941 Writer.push_back(Bases.size());
5943 for (auto &Base : Bases)
5944 Writer.AddCXXBaseSpecifier(Base);
5946 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
5949 // FIXME: Move this out of the main ASTRecordWriter interface.
5950 void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5951 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5954 static uint64_t
5955 EmitCXXCtorInitializers(ASTWriter &W,
5956 ArrayRef<CXXCtorInitializer *> CtorInits) {
5957 ASTWriter::RecordData Record;
5958 ASTRecordWriter Writer(W, Record);
5959 Writer.push_back(CtorInits.size());
5961 for (auto *Init : CtorInits) {
5962 if (Init->isBaseInitializer()) {
5963 Writer.push_back(CTOR_INITIALIZER_BASE);
5964 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5965 Writer.push_back(Init->isBaseVirtual());
5966 } else if (Init->isDelegatingInitializer()) {
5967 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5968 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5969 } else if (Init->isMemberInitializer()){
5970 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5971 Writer.AddDeclRef(Init->getMember());
5972 } else {
5973 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5974 Writer.AddDeclRef(Init->getIndirectMember());
5977 Writer.AddSourceLocation(Init->getMemberLocation());
5978 Writer.AddStmt(Init->getInit());
5979 Writer.AddSourceLocation(Init->getLParenLoc());
5980 Writer.AddSourceLocation(Init->getRParenLoc());
5981 Writer.push_back(Init->isWritten());
5982 if (Init->isWritten())
5983 Writer.push_back(Init->getSourceOrder());
5986 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
5989 // FIXME: Move this out of the main ASTRecordWriter interface.
5990 void ASTRecordWriter::AddCXXCtorInitializers(
5991 ArrayRef<CXXCtorInitializer *> CtorInits) {
5992 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
5995 void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
5996 auto &Data = D->data();
5997 Record->push_back(Data.IsLambda);
5999 #define FIELD(Name, Width, Merge) \
6000 Record->push_back(Data.Name);
6001 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
6003 // getODRHash will compute the ODRHash if it has not been previously computed.
6004 Record->push_back(D->getODRHash());
6006 bool ModulesDebugInfo =
6007 Writer->Context->getLangOpts().ModulesDebugInfo && !D->isDependentType();
6008 Record->push_back(ModulesDebugInfo);
6009 if (ModulesDebugInfo)
6010 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
6012 // IsLambda bit is already saved.
6014 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
6015 Record->push_back(Data.ComputedVisibleConversions);
6016 if (Data.ComputedVisibleConversions)
6017 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
6018 // Data.Definition is the owning decl, no need to write it.
6020 if (!Data.IsLambda) {
6021 Record->push_back(Data.NumBases);
6022 if (Data.NumBases > 0)
6023 AddCXXBaseSpecifiers(Data.bases());
6025 // FIXME: Make VBases lazily computed when needed to avoid storing them.
6026 Record->push_back(Data.NumVBases);
6027 if (Data.NumVBases > 0)
6028 AddCXXBaseSpecifiers(Data.vbases());
6030 AddDeclRef(D->getFirstFriend());
6031 } else {
6032 auto &Lambda = D->getLambdaData();
6033 Record->push_back(Lambda.DependencyKind);
6034 Record->push_back(Lambda.IsGenericLambda);
6035 Record->push_back(Lambda.CaptureDefault);
6036 Record->push_back(Lambda.NumCaptures);
6037 Record->push_back(Lambda.NumExplicitCaptures);
6038 Record->push_back(Lambda.HasKnownInternalLinkage);
6039 Record->push_back(Lambda.ManglingNumber);
6040 Record->push_back(D->getDeviceLambdaManglingNumber());
6041 // The lambda context declaration and index within the context are provided
6042 // separately, so that they can be used for merging.
6043 AddTypeSourceInfo(Lambda.MethodTyInfo);
6044 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
6045 const LambdaCapture &Capture = Lambda.Captures.front()[I];
6046 AddSourceLocation(Capture.getLocation());
6047 Record->push_back(Capture.isImplicit());
6048 Record->push_back(Capture.getCaptureKind());
6049 switch (Capture.getCaptureKind()) {
6050 case LCK_StarThis:
6051 case LCK_This:
6052 case LCK_VLAType:
6053 break;
6054 case LCK_ByCopy:
6055 case LCK_ByRef:
6056 ValueDecl *Var =
6057 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
6058 AddDeclRef(Var);
6059 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
6060 : SourceLocation());
6061 break;
6067 void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
6068 const Expr *Init = VD->getInit();
6069 if (!Init) {
6070 push_back(0);
6071 return;
6074 uint64_t Val = 1;
6075 if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
6076 Val |= (ES->HasConstantInitialization ? 2 : 0);
6077 Val |= (ES->HasConstantDestruction ? 4 : 0);
6078 APValue *Evaluated = VD->getEvaluatedValue();
6079 // If the evaluated result is constant, emit it.
6080 if (Evaluated && (Evaluated->isInt() || Evaluated->isFloat()))
6081 Val |= 8;
6083 push_back(Val);
6084 if (Val & 8) {
6085 AddAPValue(*VD->getEvaluatedValue());
6088 writeStmtRef(Init);
6091 void ASTWriter::ReaderInitialized(ASTReader *Reader) {
6092 assert(Reader && "Cannot remove chain");
6093 assert((!Chain || Chain == Reader) && "Cannot replace chain");
6094 assert(FirstDeclID == NextDeclID &&
6095 FirstTypeID == NextTypeID &&
6096 FirstIdentID == NextIdentID &&
6097 FirstMacroID == NextMacroID &&
6098 FirstSubmoduleID == NextSubmoduleID &&
6099 FirstSelectorID == NextSelectorID &&
6100 "Setting chain after writing has started.");
6102 Chain = Reader;
6104 // Note, this will get called multiple times, once one the reader starts up
6105 // and again each time it's done reading a PCH or module.
6106 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6107 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6108 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
6109 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
6110 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
6111 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
6112 NextDeclID = FirstDeclID;
6113 NextTypeID = FirstTypeID;
6114 NextIdentID = FirstIdentID;
6115 NextMacroID = FirstMacroID;
6116 NextSelectorID = FirstSelectorID;
6117 NextSubmoduleID = FirstSubmoduleID;
6120 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
6121 // Always keep the highest ID. See \p TypeRead() for more information.
6122 IdentID &StoredID = IdentifierIDs[II];
6123 if (ID > StoredID)
6124 StoredID = ID;
6127 void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
6128 // Always keep the highest ID. See \p TypeRead() for more information.
6129 MacroID &StoredID = MacroIDs[MI];
6130 if (ID > StoredID)
6131 StoredID = ID;
6134 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
6135 // Always take the highest-numbered type index. This copes with an interesting
6136 // case for chained AST writing where we schedule writing the type and then,
6137 // later, deserialize the type from another AST. In this case, we want to
6138 // keep the higher-numbered entry so that we can properly write it out to
6139 // the AST file.
6140 TypeIdx &StoredIdx = TypeIdxs[T];
6141 if (Idx.getIndex() >= StoredIdx.getIndex())
6142 StoredIdx = Idx;
6145 void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
6146 // Always keep the highest ID. See \p TypeRead() for more information.
6147 SelectorID &StoredID = SelectorIDs[S];
6148 if (ID > StoredID)
6149 StoredID = ID;
6152 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
6153 MacroDefinitionRecord *MD) {
6154 assert(!MacroDefinitions.contains(MD));
6155 MacroDefinitions[MD] = ID;
6158 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6159 assert(!SubmoduleIDs.contains(Mod));
6160 SubmoduleIDs[Mod] = ID;
6163 void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
6164 if (Chain && Chain->isProcessingUpdateRecords()) return;
6165 assert(D->isCompleteDefinition());
6166 assert(!WritingAST && "Already writing the AST!");
6167 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
6168 // We are interested when a PCH decl is modified.
6169 if (RD->isFromASTFile()) {
6170 // A forward reference was mutated into a definition. Rewrite it.
6171 // FIXME: This happens during template instantiation, should we
6172 // have created a new definition decl instead ?
6173 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6174 "completed a tag from another module but not by instantiation?");
6175 DeclUpdates[RD].push_back(
6176 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
6181 static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6182 if (D->isFromASTFile())
6183 return true;
6185 // The predefined __va_list_tag struct is imported if we imported any decls.
6186 // FIXME: This is a gross hack.
6187 return D == D->getASTContext().getVaListTagDecl();
6190 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
6191 if (Chain && Chain->isProcessingUpdateRecords()) return;
6192 assert(DC->isLookupContext() &&
6193 "Should not add lookup results to non-lookup contexts!");
6195 // TU is handled elsewhere.
6196 if (isa<TranslationUnitDecl>(DC))
6197 return;
6199 // Namespaces are handled elsewhere, except for template instantiations of
6200 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6201 // local instantiations are added to an imported context. Only happens when
6202 // adding ADL lookup candidates, for example templated friends.
6203 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6204 !isa<FunctionTemplateDecl>(D))
6205 return;
6207 // We're only interested in cases where a local declaration is added to an
6208 // imported context.
6209 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6210 return;
6212 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
6213 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
6214 assert(!WritingAST && "Already writing the AST!");
6215 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6216 // We're adding a visible declaration to a predefined decl context. Ensure
6217 // that we write out all of its lookup results so we don't get a nasty
6218 // surprise when we try to emit its lookup table.
6219 llvm::append_range(DeclsToEmitEvenIfUnreferenced, DC->decls());
6221 DeclsToEmitEvenIfUnreferenced.push_back(D);
6224 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
6225 if (Chain && Chain->isProcessingUpdateRecords()) return;
6226 assert(D->isImplicit());
6228 // We're only interested in cases where a local declaration is added to an
6229 // imported context.
6230 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6231 return;
6233 if (!isa<CXXMethodDecl>(D))
6234 return;
6236 // A decl coming from PCH was modified.
6237 assert(RD->isCompleteDefinition());
6238 assert(!WritingAST && "Already writing the AST!");
6239 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
6242 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
6243 if (Chain && Chain->isProcessingUpdateRecords()) return;
6244 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6245 if (!Chain) return;
6246 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6247 // If we don't already know the exception specification for this redecl
6248 // chain, add an update record for it.
6249 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6250 ->getType()
6251 ->castAs<FunctionProtoType>()
6252 ->getExceptionSpecType()))
6253 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6257 void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
6258 if (Chain && Chain->isProcessingUpdateRecords()) return;
6259 assert(!WritingAST && "Already writing the AST!");
6260 if (!Chain) return;
6261 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6262 DeclUpdates[D].push_back(
6263 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6267 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6268 const FunctionDecl *Delete,
6269 Expr *ThisArg) {
6270 if (Chain && Chain->isProcessingUpdateRecords()) return;
6271 assert(!WritingAST && "Already writing the AST!");
6272 assert(Delete && "Not given an operator delete");
6273 if (!Chain) return;
6274 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
6275 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6279 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
6280 if (Chain && Chain->isProcessingUpdateRecords()) return;
6281 assert(!WritingAST && "Already writing the AST!");
6282 if (!D->isFromASTFile())
6283 return; // Declaration not imported from PCH.
6285 // Implicit function decl from a PCH was defined.
6286 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6289 void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6290 if (Chain && Chain->isProcessingUpdateRecords()) return;
6291 assert(!WritingAST && "Already writing the AST!");
6292 if (!D->isFromASTFile())
6293 return;
6295 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6298 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
6299 if (Chain && Chain->isProcessingUpdateRecords()) return;
6300 assert(!WritingAST && "Already writing the AST!");
6301 if (!D->isFromASTFile())
6302 return;
6304 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6307 void ASTWriter::InstantiationRequested(const ValueDecl *D) {
6308 if (Chain && Chain->isProcessingUpdateRecords()) return;
6309 assert(!WritingAST && "Already writing the AST!");
6310 if (!D->isFromASTFile())
6311 return;
6313 // Since the actual instantiation is delayed, this really means that we need
6314 // to update the instantiation location.
6315 SourceLocation POI;
6316 if (auto *VD = dyn_cast<VarDecl>(D))
6317 POI = VD->getPointOfInstantiation();
6318 else
6319 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6320 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
6323 void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
6324 if (Chain && Chain->isProcessingUpdateRecords()) return;
6325 assert(!WritingAST && "Already writing the AST!");
6326 if (!D->isFromASTFile())
6327 return;
6329 DeclUpdates[D].push_back(
6330 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6333 void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6334 assert(!WritingAST && "Already writing the AST!");
6335 if (!D->isFromASTFile())
6336 return;
6338 DeclUpdates[D].push_back(
6339 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6342 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6343 const ObjCInterfaceDecl *IFD) {
6344 if (Chain && Chain->isProcessingUpdateRecords()) return;
6345 assert(!WritingAST && "Already writing the AST!");
6346 if (!IFD->isFromASTFile())
6347 return; // Declaration not imported from PCH.
6349 assert(IFD->getDefinition() && "Category on a class without a definition?");
6350 ObjCClassesWithCategories.insert(
6351 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
6354 void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
6355 if (Chain && Chain->isProcessingUpdateRecords()) return;
6356 assert(!WritingAST && "Already writing the AST!");
6358 // If there is *any* declaration of the entity that's not from an AST file,
6359 // we can skip writing the update record. We make sure that isUsed() triggers
6360 // completion of the redeclaration chain of the entity.
6361 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6362 if (IsLocalDecl(Prev))
6363 return;
6365 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
6368 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
6369 if (Chain && Chain->isProcessingUpdateRecords()) return;
6370 assert(!WritingAST && "Already writing the AST!");
6371 if (!D->isFromASTFile())
6372 return;
6374 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6377 void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
6378 if (Chain && Chain->isProcessingUpdateRecords()) return;
6379 assert(!WritingAST && "Already writing the AST!");
6380 if (!D->isFromASTFile())
6381 return;
6383 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
6386 void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6387 const Attr *Attr) {
6388 if (Chain && Chain->isProcessingUpdateRecords()) return;
6389 assert(!WritingAST && "Already writing the AST!");
6390 if (!D->isFromASTFile())
6391 return;
6393 DeclUpdates[D].push_back(
6394 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
6397 void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
6398 if (Chain && Chain->isProcessingUpdateRecords()) return;
6399 assert(!WritingAST && "Already writing the AST!");
6400 assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
6401 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
6404 void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6405 const RecordDecl *Record) {
6406 if (Chain && Chain->isProcessingUpdateRecords()) return;
6407 assert(!WritingAST && "Already writing the AST!");
6408 if (!Record->isFromASTFile())
6409 return;
6410 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6413 void ASTWriter::AddedCXXTemplateSpecialization(
6414 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6415 assert(!WritingAST && "Already writing the AST!");
6417 if (!TD->getFirstDecl()->isFromASTFile())
6418 return;
6419 if (Chain && Chain->isProcessingUpdateRecords())
6420 return;
6422 DeclsToEmitEvenIfUnreferenced.push_back(D);
6425 void ASTWriter::AddedCXXTemplateSpecialization(
6426 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6427 assert(!WritingAST && "Already writing the AST!");
6429 if (!TD->getFirstDecl()->isFromASTFile())
6430 return;
6431 if (Chain && Chain->isProcessingUpdateRecords())
6432 return;
6434 DeclsToEmitEvenIfUnreferenced.push_back(D);
6437 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6438 const FunctionDecl *D) {
6439 assert(!WritingAST && "Already writing the AST!");
6441 if (!TD->getFirstDecl()->isFromASTFile())
6442 return;
6443 if (Chain && Chain->isProcessingUpdateRecords())
6444 return;
6446 DeclsToEmitEvenIfUnreferenced.push_back(D);
6449 //===----------------------------------------------------------------------===//
6450 //// OMPClause Serialization
6451 ////===----------------------------------------------------------------------===//
6453 namespace {
6455 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
6456 ASTRecordWriter &Record;
6458 public:
6459 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
6460 #define GEN_CLANG_CLAUSE_CLASS
6461 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
6462 #include "llvm/Frontend/OpenMP/OMP.inc"
6463 void writeClause(OMPClause *C);
6464 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
6465 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
6470 void ASTRecordWriter::writeOMPClause(OMPClause *C) {
6471 OMPClauseWriter(*this).writeClause(C);
6474 void OMPClauseWriter::writeClause(OMPClause *C) {
6475 Record.push_back(unsigned(C->getClauseKind()));
6476 Visit(C);
6477 Record.AddSourceLocation(C->getBeginLoc());
6478 Record.AddSourceLocation(C->getEndLoc());
6481 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
6482 Record.push_back(uint64_t(C->getCaptureRegion()));
6483 Record.AddStmt(C->getPreInitStmt());
6486 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6487 VisitOMPClauseWithPreInit(C);
6488 Record.AddStmt(C->getPostUpdateExpr());
6491 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6492 VisitOMPClauseWithPreInit(C);
6493 Record.push_back(uint64_t(C->getNameModifier()));
6494 Record.AddSourceLocation(C->getNameModifierLoc());
6495 Record.AddSourceLocation(C->getColonLoc());
6496 Record.AddStmt(C->getCondition());
6497 Record.AddSourceLocation(C->getLParenLoc());
6500 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
6501 VisitOMPClauseWithPreInit(C);
6502 Record.AddStmt(C->getCondition());
6503 Record.AddSourceLocation(C->getLParenLoc());
6506 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6507 VisitOMPClauseWithPreInit(C);
6508 Record.AddStmt(C->getNumThreads());
6509 Record.AddSourceLocation(C->getLParenLoc());
6512 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6513 Record.AddStmt(C->getSafelen());
6514 Record.AddSourceLocation(C->getLParenLoc());
6517 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6518 Record.AddStmt(C->getSimdlen());
6519 Record.AddSourceLocation(C->getLParenLoc());
6522 void OMPClauseWriter::VisitOMPSizesClause(OMPSizesClause *C) {
6523 Record.push_back(C->getNumSizes());
6524 for (Expr *Size : C->getSizesRefs())
6525 Record.AddStmt(Size);
6526 Record.AddSourceLocation(C->getLParenLoc());
6529 void OMPClauseWriter::VisitOMPFullClause(OMPFullClause *C) {}
6531 void OMPClauseWriter::VisitOMPPartialClause(OMPPartialClause *C) {
6532 Record.AddStmt(C->getFactor());
6533 Record.AddSourceLocation(C->getLParenLoc());
6536 void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6537 Record.AddStmt(C->getAllocator());
6538 Record.AddSourceLocation(C->getLParenLoc());
6541 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6542 Record.AddStmt(C->getNumForLoops());
6543 Record.AddSourceLocation(C->getLParenLoc());
6546 void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
6547 Record.AddStmt(C->getEventHandler());
6548 Record.AddSourceLocation(C->getLParenLoc());
6551 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
6552 Record.push_back(unsigned(C->getDefaultKind()));
6553 Record.AddSourceLocation(C->getLParenLoc());
6554 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6557 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
6558 Record.push_back(unsigned(C->getProcBindKind()));
6559 Record.AddSourceLocation(C->getLParenLoc());
6560 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6563 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6564 VisitOMPClauseWithPreInit(C);
6565 Record.push_back(C->getScheduleKind());
6566 Record.push_back(C->getFirstScheduleModifier());
6567 Record.push_back(C->getSecondScheduleModifier());
6568 Record.AddStmt(C->getChunkSize());
6569 Record.AddSourceLocation(C->getLParenLoc());
6570 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6571 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6572 Record.AddSourceLocation(C->getScheduleKindLoc());
6573 Record.AddSourceLocation(C->getCommaLoc());
6576 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6577 Record.push_back(C->getLoopNumIterations().size());
6578 Record.AddStmt(C->getNumForLoops());
6579 for (Expr *NumIter : C->getLoopNumIterations())
6580 Record.AddStmt(NumIter);
6581 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
6582 Record.AddStmt(C->getLoopCounter(I));
6583 Record.AddSourceLocation(C->getLParenLoc());
6586 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6588 void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6590 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6592 void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6594 void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6596 void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
6597 Record.push_back(C->isExtended() ? 1 : 0);
6598 if (C->isExtended()) {
6599 Record.AddSourceLocation(C->getLParenLoc());
6600 Record.AddSourceLocation(C->getArgumentLoc());
6601 Record.writeEnum(C->getDependencyKind());
6605 void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6607 void OMPClauseWriter::VisitOMPCompareClause(OMPCompareClause *) {}
6609 void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6611 void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
6613 void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
6615 void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
6617 void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
6619 void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6621 void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6623 void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6625 void OMPClauseWriter::VisitOMPInitClause(OMPInitClause *C) {
6626 Record.push_back(C->varlist_size());
6627 for (Expr *VE : C->varlists())
6628 Record.AddStmt(VE);
6629 Record.writeBool(C->getIsTarget());
6630 Record.writeBool(C->getIsTargetSync());
6631 Record.AddSourceLocation(C->getLParenLoc());
6632 Record.AddSourceLocation(C->getVarLoc());
6635 void OMPClauseWriter::VisitOMPUseClause(OMPUseClause *C) {
6636 Record.AddStmt(C->getInteropVar());
6637 Record.AddSourceLocation(C->getLParenLoc());
6638 Record.AddSourceLocation(C->getVarLoc());
6641 void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *C) {
6642 Record.AddStmt(C->getInteropVar());
6643 Record.AddSourceLocation(C->getLParenLoc());
6644 Record.AddSourceLocation(C->getVarLoc());
6647 void OMPClauseWriter::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
6648 VisitOMPClauseWithPreInit(C);
6649 Record.AddStmt(C->getCondition());
6650 Record.AddSourceLocation(C->getLParenLoc());
6653 void OMPClauseWriter::VisitOMPNocontextClause(OMPNocontextClause *C) {
6654 VisitOMPClauseWithPreInit(C);
6655 Record.AddStmt(C->getCondition());
6656 Record.AddSourceLocation(C->getLParenLoc());
6659 void OMPClauseWriter::VisitOMPFilterClause(OMPFilterClause *C) {
6660 VisitOMPClauseWithPreInit(C);
6661 Record.AddStmt(C->getThreadID());
6662 Record.AddSourceLocation(C->getLParenLoc());
6665 void OMPClauseWriter::VisitOMPAlignClause(OMPAlignClause *C) {
6666 Record.AddStmt(C->getAlignment());
6667 Record.AddSourceLocation(C->getLParenLoc());
6670 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6671 Record.push_back(C->varlist_size());
6672 Record.AddSourceLocation(C->getLParenLoc());
6673 for (auto *VE : C->varlists()) {
6674 Record.AddStmt(VE);
6676 for (auto *VE : C->private_copies()) {
6677 Record.AddStmt(VE);
6681 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6682 Record.push_back(C->varlist_size());
6683 VisitOMPClauseWithPreInit(C);
6684 Record.AddSourceLocation(C->getLParenLoc());
6685 for (auto *VE : C->varlists()) {
6686 Record.AddStmt(VE);
6688 for (auto *VE : C->private_copies()) {
6689 Record.AddStmt(VE);
6691 for (auto *VE : C->inits()) {
6692 Record.AddStmt(VE);
6696 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6697 Record.push_back(C->varlist_size());
6698 VisitOMPClauseWithPostUpdate(C);
6699 Record.AddSourceLocation(C->getLParenLoc());
6700 Record.writeEnum(C->getKind());
6701 Record.AddSourceLocation(C->getKindLoc());
6702 Record.AddSourceLocation(C->getColonLoc());
6703 for (auto *VE : C->varlists())
6704 Record.AddStmt(VE);
6705 for (auto *E : C->private_copies())
6706 Record.AddStmt(E);
6707 for (auto *E : C->source_exprs())
6708 Record.AddStmt(E);
6709 for (auto *E : C->destination_exprs())
6710 Record.AddStmt(E);
6711 for (auto *E : C->assignment_ops())
6712 Record.AddStmt(E);
6715 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6716 Record.push_back(C->varlist_size());
6717 Record.AddSourceLocation(C->getLParenLoc());
6718 for (auto *VE : C->varlists())
6719 Record.AddStmt(VE);
6722 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6723 Record.push_back(C->varlist_size());
6724 Record.writeEnum(C->getModifier());
6725 VisitOMPClauseWithPostUpdate(C);
6726 Record.AddSourceLocation(C->getLParenLoc());
6727 Record.AddSourceLocation(C->getModifierLoc());
6728 Record.AddSourceLocation(C->getColonLoc());
6729 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6730 Record.AddDeclarationNameInfo(C->getNameInfo());
6731 for (auto *VE : C->varlists())
6732 Record.AddStmt(VE);
6733 for (auto *VE : C->privates())
6734 Record.AddStmt(VE);
6735 for (auto *E : C->lhs_exprs())
6736 Record.AddStmt(E);
6737 for (auto *E : C->rhs_exprs())
6738 Record.AddStmt(E);
6739 for (auto *E : C->reduction_ops())
6740 Record.AddStmt(E);
6741 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
6742 for (auto *E : C->copy_ops())
6743 Record.AddStmt(E);
6744 for (auto *E : C->copy_array_temps())
6745 Record.AddStmt(E);
6746 for (auto *E : C->copy_array_elems())
6747 Record.AddStmt(E);
6751 void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6752 Record.push_back(C->varlist_size());
6753 VisitOMPClauseWithPostUpdate(C);
6754 Record.AddSourceLocation(C->getLParenLoc());
6755 Record.AddSourceLocation(C->getColonLoc());
6756 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6757 Record.AddDeclarationNameInfo(C->getNameInfo());
6758 for (auto *VE : C->varlists())
6759 Record.AddStmt(VE);
6760 for (auto *VE : C->privates())
6761 Record.AddStmt(VE);
6762 for (auto *E : C->lhs_exprs())
6763 Record.AddStmt(E);
6764 for (auto *E : C->rhs_exprs())
6765 Record.AddStmt(E);
6766 for (auto *E : C->reduction_ops())
6767 Record.AddStmt(E);
6770 void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6771 Record.push_back(C->varlist_size());
6772 VisitOMPClauseWithPostUpdate(C);
6773 Record.AddSourceLocation(C->getLParenLoc());
6774 Record.AddSourceLocation(C->getColonLoc());
6775 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6776 Record.AddDeclarationNameInfo(C->getNameInfo());
6777 for (auto *VE : C->varlists())
6778 Record.AddStmt(VE);
6779 for (auto *VE : C->privates())
6780 Record.AddStmt(VE);
6781 for (auto *E : C->lhs_exprs())
6782 Record.AddStmt(E);
6783 for (auto *E : C->rhs_exprs())
6784 Record.AddStmt(E);
6785 for (auto *E : C->reduction_ops())
6786 Record.AddStmt(E);
6787 for (auto *E : C->taskgroup_descriptors())
6788 Record.AddStmt(E);
6791 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6792 Record.push_back(C->varlist_size());
6793 VisitOMPClauseWithPostUpdate(C);
6794 Record.AddSourceLocation(C->getLParenLoc());
6795 Record.AddSourceLocation(C->getColonLoc());
6796 Record.push_back(C->getModifier());
6797 Record.AddSourceLocation(C->getModifierLoc());
6798 for (auto *VE : C->varlists()) {
6799 Record.AddStmt(VE);
6801 for (auto *VE : C->privates()) {
6802 Record.AddStmt(VE);
6804 for (auto *VE : C->inits()) {
6805 Record.AddStmt(VE);
6807 for (auto *VE : C->updates()) {
6808 Record.AddStmt(VE);
6810 for (auto *VE : C->finals()) {
6811 Record.AddStmt(VE);
6813 Record.AddStmt(C->getStep());
6814 Record.AddStmt(C->getCalcStep());
6815 for (auto *VE : C->used_expressions())
6816 Record.AddStmt(VE);
6819 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6820 Record.push_back(C->varlist_size());
6821 Record.AddSourceLocation(C->getLParenLoc());
6822 Record.AddSourceLocation(C->getColonLoc());
6823 for (auto *VE : C->varlists())
6824 Record.AddStmt(VE);
6825 Record.AddStmt(C->getAlignment());
6828 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6829 Record.push_back(C->varlist_size());
6830 Record.AddSourceLocation(C->getLParenLoc());
6831 for (auto *VE : C->varlists())
6832 Record.AddStmt(VE);
6833 for (auto *E : C->source_exprs())
6834 Record.AddStmt(E);
6835 for (auto *E : C->destination_exprs())
6836 Record.AddStmt(E);
6837 for (auto *E : C->assignment_ops())
6838 Record.AddStmt(E);
6841 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6842 Record.push_back(C->varlist_size());
6843 Record.AddSourceLocation(C->getLParenLoc());
6844 for (auto *VE : C->varlists())
6845 Record.AddStmt(VE);
6846 for (auto *E : C->source_exprs())
6847 Record.AddStmt(E);
6848 for (auto *E : C->destination_exprs())
6849 Record.AddStmt(E);
6850 for (auto *E : C->assignment_ops())
6851 Record.AddStmt(E);
6854 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6855 Record.push_back(C->varlist_size());
6856 Record.AddSourceLocation(C->getLParenLoc());
6857 for (auto *VE : C->varlists())
6858 Record.AddStmt(VE);
6861 void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
6862 Record.AddStmt(C->getDepobj());
6863 Record.AddSourceLocation(C->getLParenLoc());
6866 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6867 Record.push_back(C->varlist_size());
6868 Record.push_back(C->getNumLoops());
6869 Record.AddSourceLocation(C->getLParenLoc());
6870 Record.AddStmt(C->getModifier());
6871 Record.push_back(C->getDependencyKind());
6872 Record.AddSourceLocation(C->getDependencyLoc());
6873 Record.AddSourceLocation(C->getColonLoc());
6874 Record.AddSourceLocation(C->getOmpAllMemoryLoc());
6875 for (auto *VE : C->varlists())
6876 Record.AddStmt(VE);
6877 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6878 Record.AddStmt(C->getLoopData(I));
6881 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6882 VisitOMPClauseWithPreInit(C);
6883 Record.writeEnum(C->getModifier());
6884 Record.AddStmt(C->getDevice());
6885 Record.AddSourceLocation(C->getModifierLoc());
6886 Record.AddSourceLocation(C->getLParenLoc());
6889 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6890 Record.push_back(C->varlist_size());
6891 Record.push_back(C->getUniqueDeclarationsNum());
6892 Record.push_back(C->getTotalComponentListNum());
6893 Record.push_back(C->getTotalComponentsNum());
6894 Record.AddSourceLocation(C->getLParenLoc());
6895 bool HasIteratorModifier = false;
6896 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
6897 Record.push_back(C->getMapTypeModifier(I));
6898 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6899 if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)
6900 HasIteratorModifier = true;
6902 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6903 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
6904 Record.push_back(C->getMapType());
6905 Record.AddSourceLocation(C->getMapLoc());
6906 Record.AddSourceLocation(C->getColonLoc());
6907 for (auto *E : C->varlists())
6908 Record.AddStmt(E);
6909 for (auto *E : C->mapperlists())
6910 Record.AddStmt(E);
6911 if (HasIteratorModifier)
6912 Record.AddStmt(C->getIteratorModifier());
6913 for (auto *D : C->all_decls())
6914 Record.AddDeclRef(D);
6915 for (auto N : C->all_num_lists())
6916 Record.push_back(N);
6917 for (auto N : C->all_lists_sizes())
6918 Record.push_back(N);
6919 for (auto &M : C->all_components()) {
6920 Record.AddStmt(M.getAssociatedExpression());
6921 Record.AddDeclRef(M.getAssociatedDeclaration());
6925 void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6926 Record.push_back(C->varlist_size());
6927 Record.AddSourceLocation(C->getLParenLoc());
6928 Record.AddSourceLocation(C->getColonLoc());
6929 Record.AddStmt(C->getAllocator());
6930 for (auto *VE : C->varlists())
6931 Record.AddStmt(VE);
6934 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6935 VisitOMPClauseWithPreInit(C);
6936 Record.AddStmt(C->getNumTeams());
6937 Record.AddSourceLocation(C->getLParenLoc());
6940 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6941 VisitOMPClauseWithPreInit(C);
6942 Record.AddStmt(C->getThreadLimit());
6943 Record.AddSourceLocation(C->getLParenLoc());
6946 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
6947 VisitOMPClauseWithPreInit(C);
6948 Record.AddStmt(C->getPriority());
6949 Record.AddSourceLocation(C->getLParenLoc());
6952 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
6953 VisitOMPClauseWithPreInit(C);
6954 Record.writeEnum(C->getModifier());
6955 Record.AddStmt(C->getGrainsize());
6956 Record.AddSourceLocation(C->getModifierLoc());
6957 Record.AddSourceLocation(C->getLParenLoc());
6960 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
6961 VisitOMPClauseWithPreInit(C);
6962 Record.writeEnum(C->getModifier());
6963 Record.AddStmt(C->getNumTasks());
6964 Record.AddSourceLocation(C->getModifierLoc());
6965 Record.AddSourceLocation(C->getLParenLoc());
6968 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
6969 Record.AddStmt(C->getHint());
6970 Record.AddSourceLocation(C->getLParenLoc());
6973 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
6974 VisitOMPClauseWithPreInit(C);
6975 Record.push_back(C->getDistScheduleKind());
6976 Record.AddStmt(C->getChunkSize());
6977 Record.AddSourceLocation(C->getLParenLoc());
6978 Record.AddSourceLocation(C->getDistScheduleKindLoc());
6979 Record.AddSourceLocation(C->getCommaLoc());
6982 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
6983 Record.push_back(C->getDefaultmapKind());
6984 Record.push_back(C->getDefaultmapModifier());
6985 Record.AddSourceLocation(C->getLParenLoc());
6986 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
6987 Record.AddSourceLocation(C->getDefaultmapKindLoc());
6990 void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
6991 Record.push_back(C->varlist_size());
6992 Record.push_back(C->getUniqueDeclarationsNum());
6993 Record.push_back(C->getTotalComponentListNum());
6994 Record.push_back(C->getTotalComponentsNum());
6995 Record.AddSourceLocation(C->getLParenLoc());
6996 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
6997 Record.push_back(C->getMotionModifier(I));
6998 Record.AddSourceLocation(C->getMotionModifierLoc(I));
7000 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7001 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7002 Record.AddSourceLocation(C->getColonLoc());
7003 for (auto *E : C->varlists())
7004 Record.AddStmt(E);
7005 for (auto *E : C->mapperlists())
7006 Record.AddStmt(E);
7007 for (auto *D : C->all_decls())
7008 Record.AddDeclRef(D);
7009 for (auto N : C->all_num_lists())
7010 Record.push_back(N);
7011 for (auto N : C->all_lists_sizes())
7012 Record.push_back(N);
7013 for (auto &M : C->all_components()) {
7014 Record.AddStmt(M.getAssociatedExpression());
7015 Record.writeBool(M.isNonContiguous());
7016 Record.AddDeclRef(M.getAssociatedDeclaration());
7020 void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
7021 Record.push_back(C->varlist_size());
7022 Record.push_back(C->getUniqueDeclarationsNum());
7023 Record.push_back(C->getTotalComponentListNum());
7024 Record.push_back(C->getTotalComponentsNum());
7025 Record.AddSourceLocation(C->getLParenLoc());
7026 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
7027 Record.push_back(C->getMotionModifier(I));
7028 Record.AddSourceLocation(C->getMotionModifierLoc(I));
7030 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7031 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
7032 Record.AddSourceLocation(C->getColonLoc());
7033 for (auto *E : C->varlists())
7034 Record.AddStmt(E);
7035 for (auto *E : C->mapperlists())
7036 Record.AddStmt(E);
7037 for (auto *D : C->all_decls())
7038 Record.AddDeclRef(D);
7039 for (auto N : C->all_num_lists())
7040 Record.push_back(N);
7041 for (auto N : C->all_lists_sizes())
7042 Record.push_back(N);
7043 for (auto &M : C->all_components()) {
7044 Record.AddStmt(M.getAssociatedExpression());
7045 Record.writeBool(M.isNonContiguous());
7046 Record.AddDeclRef(M.getAssociatedDeclaration());
7050 void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
7051 Record.push_back(C->varlist_size());
7052 Record.push_back(C->getUniqueDeclarationsNum());
7053 Record.push_back(C->getTotalComponentListNum());
7054 Record.push_back(C->getTotalComponentsNum());
7055 Record.AddSourceLocation(C->getLParenLoc());
7056 for (auto *E : C->varlists())
7057 Record.AddStmt(E);
7058 for (auto *VE : C->private_copies())
7059 Record.AddStmt(VE);
7060 for (auto *VE : C->inits())
7061 Record.AddStmt(VE);
7062 for (auto *D : C->all_decls())
7063 Record.AddDeclRef(D);
7064 for (auto N : C->all_num_lists())
7065 Record.push_back(N);
7066 for (auto N : C->all_lists_sizes())
7067 Record.push_back(N);
7068 for (auto &M : C->all_components()) {
7069 Record.AddStmt(M.getAssociatedExpression());
7070 Record.AddDeclRef(M.getAssociatedDeclaration());
7074 void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
7075 Record.push_back(C->varlist_size());
7076 Record.push_back(C->getUniqueDeclarationsNum());
7077 Record.push_back(C->getTotalComponentListNum());
7078 Record.push_back(C->getTotalComponentsNum());
7079 Record.AddSourceLocation(C->getLParenLoc());
7080 for (auto *E : C->varlists())
7081 Record.AddStmt(E);
7082 for (auto *D : C->all_decls())
7083 Record.AddDeclRef(D);
7084 for (auto N : C->all_num_lists())
7085 Record.push_back(N);
7086 for (auto N : C->all_lists_sizes())
7087 Record.push_back(N);
7088 for (auto &M : C->all_components()) {
7089 Record.AddStmt(M.getAssociatedExpression());
7090 Record.AddDeclRef(M.getAssociatedDeclaration());
7094 void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
7095 Record.push_back(C->varlist_size());
7096 Record.push_back(C->getUniqueDeclarationsNum());
7097 Record.push_back(C->getTotalComponentListNum());
7098 Record.push_back(C->getTotalComponentsNum());
7099 Record.AddSourceLocation(C->getLParenLoc());
7100 for (auto *E : C->varlists())
7101 Record.AddStmt(E);
7102 for (auto *D : C->all_decls())
7103 Record.AddDeclRef(D);
7104 for (auto N : C->all_num_lists())
7105 Record.push_back(N);
7106 for (auto N : C->all_lists_sizes())
7107 Record.push_back(N);
7108 for (auto &M : C->all_components()) {
7109 Record.AddStmt(M.getAssociatedExpression());
7110 Record.AddDeclRef(M.getAssociatedDeclaration());
7114 void OMPClauseWriter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
7115 Record.push_back(C->varlist_size());
7116 Record.push_back(C->getUniqueDeclarationsNum());
7117 Record.push_back(C->getTotalComponentListNum());
7118 Record.push_back(C->getTotalComponentsNum());
7119 Record.AddSourceLocation(C->getLParenLoc());
7120 for (auto *E : C->varlists())
7121 Record.AddStmt(E);
7122 for (auto *D : C->all_decls())
7123 Record.AddDeclRef(D);
7124 for (auto N : C->all_num_lists())
7125 Record.push_back(N);
7126 for (auto N : C->all_lists_sizes())
7127 Record.push_back(N);
7128 for (auto &M : C->all_components()) {
7129 Record.AddStmt(M.getAssociatedExpression());
7130 Record.AddDeclRef(M.getAssociatedDeclaration());
7134 void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
7136 void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7137 OMPUnifiedSharedMemoryClause *) {}
7139 void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
7141 void
7142 OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
7145 void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7146 OMPAtomicDefaultMemOrderClause *C) {
7147 Record.push_back(C->getAtomicDefaultMemOrderKind());
7148 Record.AddSourceLocation(C->getLParenLoc());
7149 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
7152 void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
7153 Record.push_back(C->getAtKind());
7154 Record.AddSourceLocation(C->getLParenLoc());
7155 Record.AddSourceLocation(C->getAtKindKwLoc());
7158 void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
7159 Record.push_back(C->getSeverityKind());
7160 Record.AddSourceLocation(C->getLParenLoc());
7161 Record.AddSourceLocation(C->getSeverityKindKwLoc());
7164 void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
7165 Record.AddStmt(C->getMessageString());
7166 Record.AddSourceLocation(C->getLParenLoc());
7169 void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
7170 Record.push_back(C->varlist_size());
7171 Record.AddSourceLocation(C->getLParenLoc());
7172 for (auto *VE : C->varlists())
7173 Record.AddStmt(VE);
7174 for (auto *E : C->private_refs())
7175 Record.AddStmt(E);
7178 void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
7179 Record.push_back(C->varlist_size());
7180 Record.AddSourceLocation(C->getLParenLoc());
7181 for (auto *VE : C->varlists())
7182 Record.AddStmt(VE);
7185 void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
7186 Record.push_back(C->varlist_size());
7187 Record.AddSourceLocation(C->getLParenLoc());
7188 for (auto *VE : C->varlists())
7189 Record.AddStmt(VE);
7192 void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
7193 Record.writeEnum(C->getKind());
7194 Record.writeEnum(C->getModifier());
7195 Record.AddSourceLocation(C->getLParenLoc());
7196 Record.AddSourceLocation(C->getKindKwLoc());
7197 Record.AddSourceLocation(C->getModifierKwLoc());
7200 void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
7201 Record.push_back(C->getNumberOfAllocators());
7202 Record.AddSourceLocation(C->getLParenLoc());
7203 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
7204 OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
7205 Record.AddStmt(Data.Allocator);
7206 Record.AddStmt(Data.AllocatorTraits);
7207 Record.AddSourceLocation(Data.LParenLoc);
7208 Record.AddSourceLocation(Data.RParenLoc);
7212 void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
7213 Record.push_back(C->varlist_size());
7214 Record.AddSourceLocation(C->getLParenLoc());
7215 Record.AddStmt(C->getModifier());
7216 Record.AddSourceLocation(C->getColonLoc());
7217 for (Expr *E : C->varlists())
7218 Record.AddStmt(E);
7221 void OMPClauseWriter::VisitOMPBindClause(OMPBindClause *C) {
7222 Record.writeEnum(C->getBindKind());
7223 Record.AddSourceLocation(C->getLParenLoc());
7224 Record.AddSourceLocation(C->getBindKindLoc());
7227 void OMPClauseWriter::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
7228 VisitOMPClauseWithPreInit(C);
7229 Record.AddStmt(C->getSize());
7230 Record.AddSourceLocation(C->getLParenLoc());
7233 void OMPClauseWriter::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
7234 Record.push_back(C->varlist_size());
7235 Record.push_back(C->getNumLoops());
7236 Record.AddSourceLocation(C->getLParenLoc());
7237 Record.push_back(C->getDependenceType());
7238 Record.AddSourceLocation(C->getDependenceLoc());
7239 Record.AddSourceLocation(C->getColonLoc());
7240 for (auto *VE : C->varlists())
7241 Record.AddStmt(VE);
7242 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
7243 Record.AddStmt(C->getLoopData(I));
7246 void OMPClauseWriter::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
7247 Record.AddAttributes(C->getAttrs());
7248 Record.AddSourceLocation(C->getBeginLoc());
7249 Record.AddSourceLocation(C->getLParenLoc());
7250 Record.AddSourceLocation(C->getEndLoc());
7253 void OMPClauseWriter::VisitOMPXBareClause(OMPXBareClause *C) {}
7255 void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
7256 writeUInt32(TI->Sets.size());
7257 for (const auto &Set : TI->Sets) {
7258 writeEnum(Set.Kind);
7259 writeUInt32(Set.Selectors.size());
7260 for (const auto &Selector : Set.Selectors) {
7261 writeEnum(Selector.Kind);
7262 writeBool(Selector.ScoreOrCondition);
7263 if (Selector.ScoreOrCondition)
7264 writeExprRef(Selector.ScoreOrCondition);
7265 writeUInt32(Selector.Properties.size());
7266 for (const auto &Property : Selector.Properties)
7267 writeEnum(Property.Kind);
7272 void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
7273 if (!Data)
7274 return;
7275 writeUInt32(Data->getNumClauses());
7276 writeUInt32(Data->getNumChildren());
7277 writeBool(Data->hasAssociatedStmt());
7278 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
7279 writeOMPClause(Data->getClauses()[I]);
7280 if (Data->hasAssociatedStmt())
7281 AddStmt(Data->getAssociatedStmt());
7282 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
7283 AddStmt(Data->getChildren()[I]);