1 //===- ASTWriter.cpp - AST File Writer ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the ASTWriter class, which writes AST files.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "MultiOnDiskHashTable.h"
16 #include "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/Optional.h"
81 #include "llvm/ADT/PointerIntPair.h"
82 #include "llvm/ADT/STLExtras.h"
83 #include "llvm/ADT/ScopeExit.h"
84 #include "llvm/ADT/SmallPtrSet.h"
85 #include "llvm/ADT/SmallString.h"
86 #include "llvm/ADT/SmallVector.h"
87 #include "llvm/ADT/StringMap.h"
88 #include "llvm/ADT/StringRef.h"
89 #include "llvm/Bitstream/BitCodes.h"
90 #include "llvm/Bitstream/BitstreamWriter.h"
91 #include "llvm/Support/Casting.h"
92 #include "llvm/Support/Compression.h"
93 #include "llvm/Support/DJB.h"
94 #include "llvm/Support/Endian.h"
95 #include "llvm/Support/EndianStream.h"
96 #include "llvm/Support/Error.h"
97 #include "llvm/Support/ErrorHandling.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/VersionTuple.h"
103 #include "llvm/Support/raw_ostream.h"
104 #include <algorithm>
105 #include <cassert>
106 #include <cstdint>
107 #include <cstdlib>
108 #include <cstring>
109 #include <ctime>
110 #include <deque>
111 #include <limits>
112 #include <memory>
113 #include <queue>
114 #include <tuple>
115 #include <utility>
116 #include <vector>
117
118 using namespace clang;
119 using namespace clang::serialization;
120
121 template <typename T, typename Allocator>
bytes(const std::vector<T,Allocator> & v)122 static StringRef bytes(const std::vector<T, Allocator> &v) {
123 if (v.empty()) return StringRef();
124 return StringRef(reinterpret_cast<const char*>(&v[0]),
125 sizeof(T) * v.size());
126 }
127
128 template <typename T>
bytes(const SmallVectorImpl<T> & v)129 static StringRef bytes(const SmallVectorImpl<T> &v) {
130 return StringRef(reinterpret_cast<const char*>(v.data()),
131 sizeof(T) * v.size());
132 }
133
134 //===----------------------------------------------------------------------===//
135 // Type serialization
136 //===----------------------------------------------------------------------===//
137
getTypeCodeForTypeClass(Type::TypeClass id)138 static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
139 switch (id) {
140 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
141 case Type::CLASS_ID: return TYPE_##CODE_ID;
142 #include "clang/Serialization/TypeBitCodes.def"
143 case Type::Builtin:
144 llvm_unreachable("shouldn't be serializing a builtin type this way");
145 }
146 llvm_unreachable("bad type kind");
147 }
148
149 namespace {
150
151 class ASTTypeWriter {
152 ASTWriter &Writer;
153 ASTWriter::RecordData Record;
154 ASTRecordWriter BasicWriter;
155
156 public:
ASTTypeWriter(ASTWriter & Writer)157 ASTTypeWriter(ASTWriter &Writer)
158 : Writer(Writer), BasicWriter(Writer, Record) {}
159
write(QualType T)160 uint64_t write(QualType T) {
161 if (T.hasLocalNonFastQualifiers()) {
162 Qualifiers Qs = T.getLocalQualifiers();
163 BasicWriter.writeQualType(T.getLocalUnqualifiedType());
164 BasicWriter.writeQualifiers(Qs);
165 return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
166 }
167
168 const Type *typePtr = T.getTypePtr();
169 serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
170 atw.write(typePtr);
171 return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
172 /*abbrev*/ 0);
173 }
174 };
175
176 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
177 ASTRecordWriter &Record;
178
179 public:
TypeLocWriter(ASTRecordWriter & Record)180 TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {}
181
182 #define ABSTRACT_TYPELOC(CLASS, PARENT)
183 #define TYPELOC(CLASS, PARENT) \
184 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
185 #include "clang/AST/TypeLocNodes.def"
186
187 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
188 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
189 };
190
191 } // namespace
192
VisitQualifiedTypeLoc(QualifiedTypeLoc TL)193 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
194 // nothing to do
195 }
196
VisitBuiltinTypeLoc(BuiltinTypeLoc TL)197 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
198 Record.AddSourceLocation(TL.getBuiltinLoc());
199 if (TL.needsExtraLocalData()) {
200 Record.push_back(TL.getWrittenTypeSpec());
201 Record.push_back(static_cast<uint64_t>(TL.getWrittenSignSpec()));
202 Record.push_back(static_cast<uint64_t>(TL.getWrittenWidthSpec()));
203 Record.push_back(TL.hasModeAttr());
204 }
205 }
206
VisitComplexTypeLoc(ComplexTypeLoc TL)207 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
208 Record.AddSourceLocation(TL.getNameLoc());
209 }
210
VisitPointerTypeLoc(PointerTypeLoc TL)211 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
212 Record.AddSourceLocation(TL.getStarLoc());
213 }
214
VisitDecayedTypeLoc(DecayedTypeLoc TL)215 void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
216 // nothing to do
217 }
218
VisitAdjustedTypeLoc(AdjustedTypeLoc TL)219 void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
220 // nothing to do
221 }
222
VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL)223 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
224 Record.AddSourceLocation(TL.getCaretLoc());
225 }
226
VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL)227 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
228 Record.AddSourceLocation(TL.getAmpLoc());
229 }
230
VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL)231 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
232 Record.AddSourceLocation(TL.getAmpAmpLoc());
233 }
234
VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL)235 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
236 Record.AddSourceLocation(TL.getStarLoc());
237 Record.AddTypeSourceInfo(TL.getClassTInfo());
238 }
239
VisitArrayTypeLoc(ArrayTypeLoc TL)240 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
241 Record.AddSourceLocation(TL.getLBracketLoc());
242 Record.AddSourceLocation(TL.getRBracketLoc());
243 Record.push_back(TL.getSizeExpr() ? 1 : 0);
244 if (TL.getSizeExpr())
245 Record.AddStmt(TL.getSizeExpr());
246 }
247
VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL)248 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
249 VisitArrayTypeLoc(TL);
250 }
251
VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL)252 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
253 VisitArrayTypeLoc(TL);
254 }
255
VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL)256 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
257 VisitArrayTypeLoc(TL);
258 }
259
VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc TL)260 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
261 DependentSizedArrayTypeLoc TL) {
262 VisitArrayTypeLoc(TL);
263 }
264
VisitDependentAddressSpaceTypeLoc(DependentAddressSpaceTypeLoc TL)265 void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
266 DependentAddressSpaceTypeLoc TL) {
267 Record.AddSourceLocation(TL.getAttrNameLoc());
268 SourceRange range = TL.getAttrOperandParensRange();
269 Record.AddSourceLocation(range.getBegin());
270 Record.AddSourceLocation(range.getEnd());
271 Record.AddStmt(TL.getAttrExprOperand());
272 }
273
VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL)274 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
275 DependentSizedExtVectorTypeLoc TL) {
276 Record.AddSourceLocation(TL.getNameLoc());
277 }
278
VisitVectorTypeLoc(VectorTypeLoc TL)279 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
280 Record.AddSourceLocation(TL.getNameLoc());
281 }
282
VisitDependentVectorTypeLoc(DependentVectorTypeLoc TL)283 void TypeLocWriter::VisitDependentVectorTypeLoc(
284 DependentVectorTypeLoc TL) {
285 Record.AddSourceLocation(TL.getNameLoc());
286 }
287
VisitExtVectorTypeLoc(ExtVectorTypeLoc TL)288 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
289 Record.AddSourceLocation(TL.getNameLoc());
290 }
291
VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL)292 void TypeLocWriter::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
293 Record.AddSourceLocation(TL.getAttrNameLoc());
294 SourceRange range = TL.getAttrOperandParensRange();
295 Record.AddSourceLocation(range.getBegin());
296 Record.AddSourceLocation(range.getEnd());
297 Record.AddStmt(TL.getAttrRowOperand());
298 Record.AddStmt(TL.getAttrColumnOperand());
299 }
300
VisitDependentSizedMatrixTypeLoc(DependentSizedMatrixTypeLoc TL)301 void TypeLocWriter::VisitDependentSizedMatrixTypeLoc(
302 DependentSizedMatrixTypeLoc TL) {
303 Record.AddSourceLocation(TL.getAttrNameLoc());
304 SourceRange range = TL.getAttrOperandParensRange();
305 Record.AddSourceLocation(range.getBegin());
306 Record.AddSourceLocation(range.getEnd());
307 Record.AddStmt(TL.getAttrRowOperand());
308 Record.AddStmt(TL.getAttrColumnOperand());
309 }
310
VisitFunctionTypeLoc(FunctionTypeLoc TL)311 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
312 Record.AddSourceLocation(TL.getLocalRangeBegin());
313 Record.AddSourceLocation(TL.getLParenLoc());
314 Record.AddSourceLocation(TL.getRParenLoc());
315 Record.AddSourceRange(TL.getExceptionSpecRange());
316 Record.AddSourceLocation(TL.getLocalRangeEnd());
317 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
318 Record.AddDeclRef(TL.getParam(i));
319 }
320
VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL)321 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
322 VisitFunctionTypeLoc(TL);
323 }
324
VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL)325 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
326 VisitFunctionTypeLoc(TL);
327 }
328
VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL)329 void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
330 Record.AddSourceLocation(TL.getNameLoc());
331 }
332
VisitTypedefTypeLoc(TypedefTypeLoc TL)333 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
334 Record.AddSourceLocation(TL.getNameLoc());
335 }
336
VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL)337 void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
338 if (TL.getNumProtocols()) {
339 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
340 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
341 }
342 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
343 Record.AddSourceLocation(TL.getProtocolLoc(i));
344 }
345
VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL)346 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
347 Record.AddSourceLocation(TL.getTypeofLoc());
348 Record.AddSourceLocation(TL.getLParenLoc());
349 Record.AddSourceLocation(TL.getRParenLoc());
350 }
351
VisitTypeOfTypeLoc(TypeOfTypeLoc TL)352 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
353 Record.AddSourceLocation(TL.getTypeofLoc());
354 Record.AddSourceLocation(TL.getLParenLoc());
355 Record.AddSourceLocation(TL.getRParenLoc());
356 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
357 }
358
VisitDecltypeTypeLoc(DecltypeTypeLoc TL)359 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
360 Record.AddSourceLocation(TL.getNameLoc());
361 }
362
VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL)363 void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
364 Record.AddSourceLocation(TL.getKWLoc());
365 Record.AddSourceLocation(TL.getLParenLoc());
366 Record.AddSourceLocation(TL.getRParenLoc());
367 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
368 }
369
VisitAutoTypeLoc(AutoTypeLoc TL)370 void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
371 Record.AddSourceLocation(TL.getNameLoc());
372 Record.push_back(TL.isConstrained());
373 if (TL.isConstrained()) {
374 Record.AddNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc());
375 Record.AddSourceLocation(TL.getTemplateKWLoc());
376 Record.AddSourceLocation(TL.getConceptNameLoc());
377 Record.AddDeclRef(TL.getFoundDecl());
378 Record.AddSourceLocation(TL.getLAngleLoc());
379 Record.AddSourceLocation(TL.getRAngleLoc());
380 for (unsigned I = 0; I < TL.getNumArgs(); ++I)
381 Record.AddTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
382 TL.getArgLocInfo(I));
383 }
384 }
385
VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL)386 void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
387 DeducedTemplateSpecializationTypeLoc TL) {
388 Record.AddSourceLocation(TL.getTemplateNameLoc());
389 }
390
VisitRecordTypeLoc(RecordTypeLoc TL)391 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
392 Record.AddSourceLocation(TL.getNameLoc());
393 }
394
VisitEnumTypeLoc(EnumTypeLoc TL)395 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
396 Record.AddSourceLocation(TL.getNameLoc());
397 }
398
VisitAttributedTypeLoc(AttributedTypeLoc TL)399 void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
400 Record.AddAttr(TL.getAttr());
401 }
402
VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL)403 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
404 Record.AddSourceLocation(TL.getNameLoc());
405 }
406
VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL)407 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
408 SubstTemplateTypeParmTypeLoc TL) {
409 Record.AddSourceLocation(TL.getNameLoc());
410 }
411
VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc TL)412 void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
413 SubstTemplateTypeParmPackTypeLoc TL) {
414 Record.AddSourceLocation(TL.getNameLoc());
415 }
416
VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL)417 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
418 TemplateSpecializationTypeLoc TL) {
419 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
420 Record.AddSourceLocation(TL.getTemplateNameLoc());
421 Record.AddSourceLocation(TL.getLAngleLoc());
422 Record.AddSourceLocation(TL.getRAngleLoc());
423 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
424 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
425 TL.getArgLoc(i).getLocInfo());
426 }
427
VisitParenTypeLoc(ParenTypeLoc TL)428 void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
429 Record.AddSourceLocation(TL.getLParenLoc());
430 Record.AddSourceLocation(TL.getRParenLoc());
431 }
432
VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL)433 void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
434 Record.AddSourceLocation(TL.getExpansionLoc());
435 }
436
VisitElaboratedTypeLoc(ElaboratedTypeLoc TL)437 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
438 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
439 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
440 }
441
VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL)442 void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
443 Record.AddSourceLocation(TL.getNameLoc());
444 }
445
VisitDependentNameTypeLoc(DependentNameTypeLoc TL)446 void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
447 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
448 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
449 Record.AddSourceLocation(TL.getNameLoc());
450 }
451
VisitDependentTemplateSpecializationTypeLoc(DependentTemplateSpecializationTypeLoc TL)452 void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
453 DependentTemplateSpecializationTypeLoc TL) {
454 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
455 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
456 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
457 Record.AddSourceLocation(TL.getTemplateNameLoc());
458 Record.AddSourceLocation(TL.getLAngleLoc());
459 Record.AddSourceLocation(TL.getRAngleLoc());
460 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
461 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
462 TL.getArgLoc(I).getLocInfo());
463 }
464
VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL)465 void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
466 Record.AddSourceLocation(TL.getEllipsisLoc());
467 }
468
VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL)469 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
470 Record.AddSourceLocation(TL.getNameLoc());
471 }
472
VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL)473 void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
474 Record.push_back(TL.hasBaseTypeAsWritten());
475 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
476 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
477 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
478 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
479 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
480 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
481 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
482 Record.AddSourceLocation(TL.getProtocolLoc(i));
483 }
484
VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL)485 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
486 Record.AddSourceLocation(TL.getStarLoc());
487 }
488
VisitAtomicTypeLoc(AtomicTypeLoc TL)489 void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
490 Record.AddSourceLocation(TL.getKWLoc());
491 Record.AddSourceLocation(TL.getLParenLoc());
492 Record.AddSourceLocation(TL.getRParenLoc());
493 }
494
VisitPipeTypeLoc(PipeTypeLoc TL)495 void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
496 Record.AddSourceLocation(TL.getKWLoc());
497 }
498
VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL)499 void TypeLocWriter::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
500 Record.AddSourceLocation(TL.getNameLoc());
501 }
VisitDependentExtIntTypeLoc(clang::DependentExtIntTypeLoc TL)502 void TypeLocWriter::VisitDependentExtIntTypeLoc(
503 clang::DependentExtIntTypeLoc TL) {
504 Record.AddSourceLocation(TL.getNameLoc());
505 }
506
WriteTypeAbbrevs()507 void ASTWriter::WriteTypeAbbrevs() {
508 using namespace llvm;
509
510 std::shared_ptr<BitCodeAbbrev> Abv;
511
512 // Abbreviation for TYPE_EXT_QUAL
513 Abv = std::make_shared<BitCodeAbbrev>();
514 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
515 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
516 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
517 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
518
519 // Abbreviation for TYPE_FUNCTION_PROTO
520 Abv = std::make_shared<BitCodeAbbrev>();
521 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
522 // FunctionType
523 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
524 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
525 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
526 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
527 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
528 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
529 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
530 Abv->Add(BitCodeAbbrevOp(0)); // NoCfCheck
531 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // CmseNSCall
532 // FunctionProtoType
533 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
534 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
535 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
536 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
537 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
538 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
539 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
540 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
541 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
542 }
543
544 //===----------------------------------------------------------------------===//
545 // ASTWriter Implementation
546 //===----------------------------------------------------------------------===//
547
EmitBlockID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)548 static void EmitBlockID(unsigned ID, const char *Name,
549 llvm::BitstreamWriter &Stream,
550 ASTWriter::RecordDataImpl &Record) {
551 Record.clear();
552 Record.push_back(ID);
553 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
554
555 // Emit the block name if present.
556 if (!Name || Name[0] == 0)
557 return;
558 Record.clear();
559 while (*Name)
560 Record.push_back(*Name++);
561 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
562 }
563
EmitRecordID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)564 static void EmitRecordID(unsigned ID, const char *Name,
565 llvm::BitstreamWriter &Stream,
566 ASTWriter::RecordDataImpl &Record) {
567 Record.clear();
568 Record.push_back(ID);
569 while (*Name)
570 Record.push_back(*Name++);
571 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
572 }
573
AddStmtsExprs(llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)574 static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
575 ASTWriter::RecordDataImpl &Record) {
576 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
577 RECORD(STMT_STOP);
578 RECORD(STMT_NULL_PTR);
579 RECORD(STMT_REF_PTR);
580 RECORD(STMT_NULL);
581 RECORD(STMT_COMPOUND);
582 RECORD(STMT_CASE);
583 RECORD(STMT_DEFAULT);
584 RECORD(STMT_LABEL);
585 RECORD(STMT_ATTRIBUTED);
586 RECORD(STMT_IF);
587 RECORD(STMT_SWITCH);
588 RECORD(STMT_WHILE);
589 RECORD(STMT_DO);
590 RECORD(STMT_FOR);
591 RECORD(STMT_GOTO);
592 RECORD(STMT_INDIRECT_GOTO);
593 RECORD(STMT_CONTINUE);
594 RECORD(STMT_BREAK);
595 RECORD(STMT_RETURN);
596 RECORD(STMT_DECL);
597 RECORD(STMT_GCCASM);
598 RECORD(STMT_MSASM);
599 RECORD(EXPR_PREDEFINED);
600 RECORD(EXPR_DECL_REF);
601 RECORD(EXPR_INTEGER_LITERAL);
602 RECORD(EXPR_FIXEDPOINT_LITERAL);
603 RECORD(EXPR_FLOATING_LITERAL);
604 RECORD(EXPR_IMAGINARY_LITERAL);
605 RECORD(EXPR_STRING_LITERAL);
606 RECORD(EXPR_CHARACTER_LITERAL);
607 RECORD(EXPR_PAREN);
608 RECORD(EXPR_PAREN_LIST);
609 RECORD(EXPR_UNARY_OPERATOR);
610 RECORD(EXPR_SIZEOF_ALIGN_OF);
611 RECORD(EXPR_ARRAY_SUBSCRIPT);
612 RECORD(EXPR_CALL);
613 RECORD(EXPR_MEMBER);
614 RECORD(EXPR_BINARY_OPERATOR);
615 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
616 RECORD(EXPR_CONDITIONAL_OPERATOR);
617 RECORD(EXPR_IMPLICIT_CAST);
618 RECORD(EXPR_CSTYLE_CAST);
619 RECORD(EXPR_COMPOUND_LITERAL);
620 RECORD(EXPR_EXT_VECTOR_ELEMENT);
621 RECORD(EXPR_INIT_LIST);
622 RECORD(EXPR_DESIGNATED_INIT);
623 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
624 RECORD(EXPR_IMPLICIT_VALUE_INIT);
625 RECORD(EXPR_NO_INIT);
626 RECORD(EXPR_VA_ARG);
627 RECORD(EXPR_ADDR_LABEL);
628 RECORD(EXPR_STMT);
629 RECORD(EXPR_CHOOSE);
630 RECORD(EXPR_GNU_NULL);
631 RECORD(EXPR_SHUFFLE_VECTOR);
632 RECORD(EXPR_BLOCK);
633 RECORD(EXPR_GENERIC_SELECTION);
634 RECORD(EXPR_OBJC_STRING_LITERAL);
635 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
636 RECORD(EXPR_OBJC_ARRAY_LITERAL);
637 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
638 RECORD(EXPR_OBJC_ENCODE);
639 RECORD(EXPR_OBJC_SELECTOR_EXPR);
640 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
641 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
642 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
643 RECORD(EXPR_OBJC_KVC_REF_EXPR);
644 RECORD(EXPR_OBJC_MESSAGE_EXPR);
645 RECORD(STMT_OBJC_FOR_COLLECTION);
646 RECORD(STMT_OBJC_CATCH);
647 RECORD(STMT_OBJC_FINALLY);
648 RECORD(STMT_OBJC_AT_TRY);
649 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
650 RECORD(STMT_OBJC_AT_THROW);
651 RECORD(EXPR_OBJC_BOOL_LITERAL);
652 RECORD(STMT_CXX_CATCH);
653 RECORD(STMT_CXX_TRY);
654 RECORD(STMT_CXX_FOR_RANGE);
655 RECORD(EXPR_CXX_OPERATOR_CALL);
656 RECORD(EXPR_CXX_MEMBER_CALL);
657 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
658 RECORD(EXPR_CXX_CONSTRUCT);
659 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
660 RECORD(EXPR_CXX_STATIC_CAST);
661 RECORD(EXPR_CXX_DYNAMIC_CAST);
662 RECORD(EXPR_CXX_REINTERPRET_CAST);
663 RECORD(EXPR_CXX_CONST_CAST);
664 RECORD(EXPR_CXX_ADDRSPACE_CAST);
665 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
666 RECORD(EXPR_USER_DEFINED_LITERAL);
667 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
668 RECORD(EXPR_CXX_BOOL_LITERAL);
669 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
670 RECORD(EXPR_CXX_TYPEID_EXPR);
671 RECORD(EXPR_CXX_TYPEID_TYPE);
672 RECORD(EXPR_CXX_THIS);
673 RECORD(EXPR_CXX_THROW);
674 RECORD(EXPR_CXX_DEFAULT_ARG);
675 RECORD(EXPR_CXX_DEFAULT_INIT);
676 RECORD(EXPR_CXX_BIND_TEMPORARY);
677 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
678 RECORD(EXPR_CXX_NEW);
679 RECORD(EXPR_CXX_DELETE);
680 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
681 RECORD(EXPR_EXPR_WITH_CLEANUPS);
682 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
683 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
684 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
685 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
686 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
687 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
688 RECORD(EXPR_CXX_NOEXCEPT);
689 RECORD(EXPR_OPAQUE_VALUE);
690 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
691 RECORD(EXPR_TYPE_TRAIT);
692 RECORD(EXPR_ARRAY_TYPE_TRAIT);
693 RECORD(EXPR_PACK_EXPANSION);
694 RECORD(EXPR_SIZEOF_PACK);
695 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
696 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
697 RECORD(EXPR_FUNCTION_PARM_PACK);
698 RECORD(EXPR_MATERIALIZE_TEMPORARY);
699 RECORD(EXPR_CUDA_KERNEL_CALL);
700 RECORD(EXPR_CXX_UUIDOF_EXPR);
701 RECORD(EXPR_CXX_UUIDOF_TYPE);
702 RECORD(EXPR_LAMBDA);
703 #undef RECORD
704 }
705
WriteBlockInfoBlock()706 void ASTWriter::WriteBlockInfoBlock() {
707 RecordData Record;
708 Stream.EnterBlockInfoBlock();
709
710 #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
711 #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
712
713 // Control Block.
714 BLOCK(CONTROL_BLOCK);
715 RECORD(METADATA);
716 RECORD(MODULE_NAME);
717 RECORD(MODULE_DIRECTORY);
718 RECORD(MODULE_MAP_FILE);
719 RECORD(IMPORTS);
720 RECORD(ORIGINAL_FILE);
721 RECORD(ORIGINAL_PCH_DIR);
722 RECORD(ORIGINAL_FILE_ID);
723 RECORD(INPUT_FILE_OFFSETS);
724
725 BLOCK(OPTIONS_BLOCK);
726 RECORD(LANGUAGE_OPTIONS);
727 RECORD(TARGET_OPTIONS);
728 RECORD(FILE_SYSTEM_OPTIONS);
729 RECORD(HEADER_SEARCH_OPTIONS);
730 RECORD(PREPROCESSOR_OPTIONS);
731
732 BLOCK(INPUT_FILES_BLOCK);
733 RECORD(INPUT_FILE);
734 RECORD(INPUT_FILE_HASH);
735
736 // AST Top-Level Block.
737 BLOCK(AST_BLOCK);
738 RECORD(TYPE_OFFSET);
739 RECORD(DECL_OFFSET);
740 RECORD(IDENTIFIER_OFFSET);
741 RECORD(IDENTIFIER_TABLE);
742 RECORD(EAGERLY_DESERIALIZED_DECLS);
743 RECORD(MODULAR_CODEGEN_DECLS);
744 RECORD(SPECIAL_TYPES);
745 RECORD(STATISTICS);
746 RECORD(TENTATIVE_DEFINITIONS);
747 RECORD(SELECTOR_OFFSETS);
748 RECORD(METHOD_POOL);
749 RECORD(PP_COUNTER_VALUE);
750 RECORD(SOURCE_LOCATION_OFFSETS);
751 RECORD(SOURCE_LOCATION_PRELOADS);
752 RECORD(EXT_VECTOR_DECLS);
753 RECORD(UNUSED_FILESCOPED_DECLS);
754 RECORD(PPD_ENTITIES_OFFSETS);
755 RECORD(VTABLE_USES);
756 RECORD(PPD_SKIPPED_RANGES);
757 RECORD(REFERENCED_SELECTOR_POOL);
758 RECORD(TU_UPDATE_LEXICAL);
759 RECORD(SEMA_DECL_REFS);
760 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
761 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
762 RECORD(UPDATE_VISIBLE);
763 RECORD(DECL_UPDATE_OFFSETS);
764 RECORD(DECL_UPDATES);
765 RECORD(CUDA_SPECIAL_DECL_REFS);
766 RECORD(HEADER_SEARCH_TABLE);
767 RECORD(FP_PRAGMA_OPTIONS);
768 RECORD(OPENCL_EXTENSIONS);
769 RECORD(OPENCL_EXTENSION_TYPES);
770 RECORD(OPENCL_EXTENSION_DECLS);
771 RECORD(DELEGATING_CTORS);
772 RECORD(KNOWN_NAMESPACES);
773 RECORD(MODULE_OFFSET_MAP);
774 RECORD(SOURCE_MANAGER_LINE_TABLE);
775 RECORD(OBJC_CATEGORIES_MAP);
776 RECORD(FILE_SORTED_DECLS);
777 RECORD(IMPORTED_MODULES);
778 RECORD(OBJC_CATEGORIES);
779 RECORD(MACRO_OFFSET);
780 RECORD(INTERESTING_IDENTIFIERS);
781 RECORD(UNDEFINED_BUT_USED);
782 RECORD(LATE_PARSED_TEMPLATE);
783 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
784 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
785 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
786 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
787 RECORD(DELETE_EXPRS_TO_ANALYZE);
788 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
789 RECORD(PP_CONDITIONAL_STACK);
790 RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
791
792 // SourceManager Block.
793 BLOCK(SOURCE_MANAGER_BLOCK);
794 RECORD(SM_SLOC_FILE_ENTRY);
795 RECORD(SM_SLOC_BUFFER_ENTRY);
796 RECORD(SM_SLOC_BUFFER_BLOB);
797 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
798 RECORD(SM_SLOC_EXPANSION_ENTRY);
799
800 // Preprocessor Block.
801 BLOCK(PREPROCESSOR_BLOCK);
802 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
803 RECORD(PP_MACRO_FUNCTION_LIKE);
804 RECORD(PP_MACRO_OBJECT_LIKE);
805 RECORD(PP_MODULE_MACRO);
806 RECORD(PP_TOKEN);
807
808 // Submodule Block.
809 BLOCK(SUBMODULE_BLOCK);
810 RECORD(SUBMODULE_METADATA);
811 RECORD(SUBMODULE_DEFINITION);
812 RECORD(SUBMODULE_UMBRELLA_HEADER);
813 RECORD(SUBMODULE_HEADER);
814 RECORD(SUBMODULE_TOPHEADER);
815 RECORD(SUBMODULE_UMBRELLA_DIR);
816 RECORD(SUBMODULE_IMPORTS);
817 RECORD(SUBMODULE_EXPORTS);
818 RECORD(SUBMODULE_REQUIRES);
819 RECORD(SUBMODULE_EXCLUDED_HEADER);
820 RECORD(SUBMODULE_LINK_LIBRARY);
821 RECORD(SUBMODULE_CONFIG_MACRO);
822 RECORD(SUBMODULE_CONFLICT);
823 RECORD(SUBMODULE_PRIVATE_HEADER);
824 RECORD(SUBMODULE_TEXTUAL_HEADER);
825 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
826 RECORD(SUBMODULE_INITIALIZERS);
827 RECORD(SUBMODULE_EXPORT_AS);
828
829 // Comments Block.
830 BLOCK(COMMENTS_BLOCK);
831 RECORD(COMMENTS_RAW_COMMENT);
832
833 // Decls and Types block.
834 BLOCK(DECLTYPES_BLOCK);
835 RECORD(TYPE_EXT_QUAL);
836 RECORD(TYPE_COMPLEX);
837 RECORD(TYPE_POINTER);
838 RECORD(TYPE_BLOCK_POINTER);
839 RECORD(TYPE_LVALUE_REFERENCE);
840 RECORD(TYPE_RVALUE_REFERENCE);
841 RECORD(TYPE_MEMBER_POINTER);
842 RECORD(TYPE_CONSTANT_ARRAY);
843 RECORD(TYPE_INCOMPLETE_ARRAY);
844 RECORD(TYPE_VARIABLE_ARRAY);
845 RECORD(TYPE_VECTOR);
846 RECORD(TYPE_EXT_VECTOR);
847 RECORD(TYPE_FUNCTION_NO_PROTO);
848 RECORD(TYPE_FUNCTION_PROTO);
849 RECORD(TYPE_TYPEDEF);
850 RECORD(TYPE_TYPEOF_EXPR);
851 RECORD(TYPE_TYPEOF);
852 RECORD(TYPE_RECORD);
853 RECORD(TYPE_ENUM);
854 RECORD(TYPE_OBJC_INTERFACE);
855 RECORD(TYPE_OBJC_OBJECT_POINTER);
856 RECORD(TYPE_DECLTYPE);
857 RECORD(TYPE_ELABORATED);
858 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
859 RECORD(TYPE_UNRESOLVED_USING);
860 RECORD(TYPE_INJECTED_CLASS_NAME);
861 RECORD(TYPE_OBJC_OBJECT);
862 RECORD(TYPE_TEMPLATE_TYPE_PARM);
863 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
864 RECORD(TYPE_DEPENDENT_NAME);
865 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
866 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
867 RECORD(TYPE_PAREN);
868 RECORD(TYPE_MACRO_QUALIFIED);
869 RECORD(TYPE_PACK_EXPANSION);
870 RECORD(TYPE_ATTRIBUTED);
871 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
872 RECORD(TYPE_AUTO);
873 RECORD(TYPE_UNARY_TRANSFORM);
874 RECORD(TYPE_ATOMIC);
875 RECORD(TYPE_DECAYED);
876 RECORD(TYPE_ADJUSTED);
877 RECORD(TYPE_OBJC_TYPE_PARAM);
878 RECORD(LOCAL_REDECLARATIONS);
879 RECORD(DECL_TYPEDEF);
880 RECORD(DECL_TYPEALIAS);
881 RECORD(DECL_ENUM);
882 RECORD(DECL_RECORD);
883 RECORD(DECL_ENUM_CONSTANT);
884 RECORD(DECL_FUNCTION);
885 RECORD(DECL_OBJC_METHOD);
886 RECORD(DECL_OBJC_INTERFACE);
887 RECORD(DECL_OBJC_PROTOCOL);
888 RECORD(DECL_OBJC_IVAR);
889 RECORD(DECL_OBJC_AT_DEFS_FIELD);
890 RECORD(DECL_OBJC_CATEGORY);
891 RECORD(DECL_OBJC_CATEGORY_IMPL);
892 RECORD(DECL_OBJC_IMPLEMENTATION);
893 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
894 RECORD(DECL_OBJC_PROPERTY);
895 RECORD(DECL_OBJC_PROPERTY_IMPL);
896 RECORD(DECL_FIELD);
897 RECORD(DECL_MS_PROPERTY);
898 RECORD(DECL_VAR);
899 RECORD(DECL_IMPLICIT_PARAM);
900 RECORD(DECL_PARM_VAR);
901 RECORD(DECL_FILE_SCOPE_ASM);
902 RECORD(DECL_BLOCK);
903 RECORD(DECL_CONTEXT_LEXICAL);
904 RECORD(DECL_CONTEXT_VISIBLE);
905 RECORD(DECL_NAMESPACE);
906 RECORD(DECL_NAMESPACE_ALIAS);
907 RECORD(DECL_USING);
908 RECORD(DECL_USING_SHADOW);
909 RECORD(DECL_USING_DIRECTIVE);
910 RECORD(DECL_UNRESOLVED_USING_VALUE);
911 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
912 RECORD(DECL_LINKAGE_SPEC);
913 RECORD(DECL_CXX_RECORD);
914 RECORD(DECL_CXX_METHOD);
915 RECORD(DECL_CXX_CONSTRUCTOR);
916 RECORD(DECL_CXX_DESTRUCTOR);
917 RECORD(DECL_CXX_CONVERSION);
918 RECORD(DECL_ACCESS_SPEC);
919 RECORD(DECL_FRIEND);
920 RECORD(DECL_FRIEND_TEMPLATE);
921 RECORD(DECL_CLASS_TEMPLATE);
922 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
923 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
924 RECORD(DECL_VAR_TEMPLATE);
925 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
926 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
927 RECORD(DECL_FUNCTION_TEMPLATE);
928 RECORD(DECL_TEMPLATE_TYPE_PARM);
929 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
930 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
931 RECORD(DECL_CONCEPT);
932 RECORD(DECL_REQUIRES_EXPR_BODY);
933 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
934 RECORD(DECL_STATIC_ASSERT);
935 RECORD(DECL_CXX_BASE_SPECIFIERS);
936 RECORD(DECL_CXX_CTOR_INITIALIZERS);
937 RECORD(DECL_INDIRECTFIELD);
938 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
939 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
940 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
941 RECORD(DECL_IMPORT);
942 RECORD(DECL_OMP_THREADPRIVATE);
943 RECORD(DECL_EMPTY);
944 RECORD(DECL_OBJC_TYPE_PARAM);
945 RECORD(DECL_OMP_CAPTUREDEXPR);
946 RECORD(DECL_PRAGMA_COMMENT);
947 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
948 RECORD(DECL_OMP_DECLARE_REDUCTION);
949 RECORD(DECL_OMP_ALLOCATE);
950
951 // Statements and Exprs can occur in the Decls and Types block.
952 AddStmtsExprs(Stream, Record);
953
954 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
955 RECORD(PPD_MACRO_EXPANSION);
956 RECORD(PPD_MACRO_DEFINITION);
957 RECORD(PPD_INCLUSION_DIRECTIVE);
958
959 // Decls and Types block.
960 BLOCK(EXTENSION_BLOCK);
961 RECORD(EXTENSION_METADATA);
962
963 BLOCK(UNHASHED_CONTROL_BLOCK);
964 RECORD(SIGNATURE);
965 RECORD(AST_BLOCK_HASH);
966 RECORD(DIAGNOSTIC_OPTIONS);
967 RECORD(DIAG_PRAGMA_MAPPINGS);
968
969 #undef RECORD
970 #undef BLOCK
971 Stream.ExitBlock();
972 }
973
974 /// Prepares a path for being written to an AST file by converting it
975 /// to an absolute path and removing nested './'s.
976 ///
977 /// \return \c true if the path was changed.
cleanPathForOutput(FileManager & FileMgr,SmallVectorImpl<char> & Path)978 static bool cleanPathForOutput(FileManager &FileMgr,
979 SmallVectorImpl<char> &Path) {
980 bool Changed = FileMgr.makeAbsolutePath(Path);
981 return Changed | llvm::sys::path::remove_dots(Path);
982 }
983
984 /// Adjusts the given filename to only write out the portion of the
985 /// filename that is not part of the system root directory.
986 ///
987 /// \param Filename the file name to adjust.
988 ///
989 /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
990 /// the returned filename will be adjusted by this root directory.
991 ///
992 /// \returns either the original filename (if it needs no adjustment) or the
993 /// adjusted filename (which points into the @p Filename parameter).
994 static const char *
adjustFilenameForRelocatableAST(const char * Filename,StringRef BaseDir)995 adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
996 assert(Filename && "No file name to adjust?");
997
998 if (BaseDir.empty())
999 return Filename;
1000
1001 // Verify that the filename and the system root have the same prefix.
1002 unsigned Pos = 0;
1003 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1004 if (Filename[Pos] != BaseDir[Pos])
1005 return Filename; // Prefixes don't match.
1006
1007 // We hit the end of the filename before we hit the end of the system root.
1008 if (!Filename[Pos])
1009 return Filename;
1010
1011 // If there's not a path separator at the end of the base directory nor
1012 // immediately after it, then this isn't within the base directory.
1013 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1014 if (!llvm::sys::path::is_separator(BaseDir.back()))
1015 return Filename;
1016 } else {
1017 // If the file name has a '/' at the current position, skip over the '/'.
1018 // We distinguish relative paths from absolute paths by the
1019 // absence of '/' at the beginning of relative paths.
1020 //
1021 // FIXME: This is wrong. We distinguish them by asking if the path is
1022 // absolute, which isn't the same thing. And there might be multiple '/'s
1023 // in a row. Use a better mechanism to indicate whether we have emitted an
1024 // absolute or relative path.
1025 ++Pos;
1026 }
1027
1028 return Filename + Pos;
1029 }
1030
1031 std::pair<ASTFileSignature, ASTFileSignature>
createSignature(StringRef AllBytes,StringRef ASTBlockBytes)1032 ASTWriter::createSignature(StringRef AllBytes, StringRef ASTBlockBytes) {
1033 llvm::SHA1 Hasher;
1034 Hasher.update(ASTBlockBytes);
1035 auto Hash = Hasher.result();
1036 ASTFileSignature ASTBlockHash = ASTFileSignature::create(Hash);
1037
1038 // Add the remaining bytes (i.e. bytes before the unhashed control block that
1039 // are not part of the AST block).
1040 Hasher.update(
1041 AllBytes.take_front(ASTBlockBytes.bytes_end() - AllBytes.bytes_begin()));
1042 Hasher.update(
1043 AllBytes.take_back(AllBytes.bytes_end() - ASTBlockBytes.bytes_end()));
1044 Hash = Hasher.result();
1045 ASTFileSignature Signature = ASTFileSignature::create(Hash);
1046
1047 return std::make_pair(ASTBlockHash, Signature);
1048 }
1049
writeUnhashedControlBlock(Preprocessor & PP,ASTContext & Context)1050 ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1051 ASTContext &Context) {
1052 // Flush first to prepare the PCM hash (signature).
1053 Stream.FlushToWord();
1054 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1055
1056 // Enter the block and prepare to write records.
1057 RecordData Record;
1058 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1059
1060 // For implicit modules, write the hash of the PCM as its signature.
1061 ASTFileSignature Signature;
1062 if (WritingModule &&
1063 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1064 ASTFileSignature ASTBlockHash;
1065 auto ASTBlockStartByte = ASTBlockRange.first >> 3;
1066 auto ASTBlockByteLength = (ASTBlockRange.second >> 3) - ASTBlockStartByte;
1067 std::tie(ASTBlockHash, Signature) = createSignature(
1068 StringRef(Buffer.begin(), StartOfUnhashedControl),
1069 StringRef(Buffer.begin() + ASTBlockStartByte, ASTBlockByteLength));
1070
1071 Record.append(ASTBlockHash.begin(), ASTBlockHash.end());
1072 Stream.EmitRecord(AST_BLOCK_HASH, Record);
1073 Record.clear();
1074 Record.append(Signature.begin(), Signature.end());
1075 Stream.EmitRecord(SIGNATURE, Record);
1076 Record.clear();
1077 }
1078
1079 // Diagnostic options.
1080 const auto &Diags = Context.getDiagnostics();
1081 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1082 #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1083 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1084 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1085 #include "clang/Basic/DiagnosticOptions.def"
1086 Record.push_back(DiagOpts.Warnings.size());
1087 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1088 AddString(DiagOpts.Warnings[I], Record);
1089 Record.push_back(DiagOpts.Remarks.size());
1090 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1091 AddString(DiagOpts.Remarks[I], Record);
1092 // Note: we don't serialize the log or serialization file names, because they
1093 // are generally transient files and will almost always be overridden.
1094 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1095
1096 // Write out the diagnostic/pragma mappings.
1097 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
1098
1099 // Leave the options block.
1100 Stream.ExitBlock();
1101 return Signature;
1102 }
1103
1104 /// Write the control block.
WriteControlBlock(Preprocessor & PP,ASTContext & Context,StringRef isysroot,const std::string & OutputFile)1105 void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1106 StringRef isysroot,
1107 const std::string &OutputFile) {
1108 using namespace llvm;
1109
1110 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1111 RecordData Record;
1112
1113 // Metadata
1114 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
1115 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1116 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1117 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1118 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1119 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1120 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1121 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1122 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1123 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1124 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
1125 assert((!WritingModule || isysroot.empty()) &&
1126 "writing module as a relocatable PCH?");
1127 {
1128 RecordData::value_type Record[] = {
1129 METADATA,
1130 VERSION_MAJOR,
1131 VERSION_MINOR,
1132 CLANG_VERSION_MAJOR,
1133 CLANG_VERSION_MINOR,
1134 !isysroot.empty(),
1135 IncludeTimestamps,
1136 ASTHasCompilerErrors};
1137 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1138 getClangFullRepositoryVersion());
1139 }
1140
1141 if (WritingModule) {
1142 // Module name
1143 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1144 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1146 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1147 RecordData::value_type Record[] = {MODULE_NAME};
1148 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1149 }
1150
1151 if (WritingModule && WritingModule->Directory) {
1152 SmallString<128> BaseDir(WritingModule->Directory->getName());
1153 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1154
1155 // If the home of the module is the current working directory, then we
1156 // want to pick up the cwd of the build process loading the module, not
1157 // our cwd, when we load this module.
1158 if (!PP.getHeaderSearchInfo()
1159 .getHeaderSearchOpts()
1160 .ModuleMapFileHomeIsCwd ||
1161 WritingModule->Directory->getName() != StringRef(".")) {
1162 // Module directory.
1163 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1164 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1166 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1167
1168 RecordData::value_type Record[] = {MODULE_DIRECTORY};
1169 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1170 }
1171
1172 // Write out all other paths relative to the base directory if possible.
1173 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1174 } else if (!isysroot.empty()) {
1175 // Write out paths relative to the sysroot if possible.
1176 BaseDirectory = std::string(isysroot);
1177 }
1178
1179 // Module map file
1180 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
1181 Record.clear();
1182
1183 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1184 AddPath(WritingModule->PresumedModuleMapFile.empty()
1185 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1186 : StringRef(WritingModule->PresumedModuleMapFile),
1187 Record);
1188
1189 // Additional module map files.
1190 if (auto *AdditionalModMaps =
1191 Map.getAdditionalModuleMapFiles(WritingModule)) {
1192 Record.push_back(AdditionalModMaps->size());
1193 for (const FileEntry *F : *AdditionalModMaps)
1194 AddPath(F->getName(), Record);
1195 } else {
1196 Record.push_back(0);
1197 }
1198
1199 Stream.EmitRecord(MODULE_MAP_FILE, Record);
1200 }
1201
1202 // Imports
1203 if (Chain) {
1204 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1205 Record.clear();
1206
1207 for (ModuleFile &M : Mgr) {
1208 // Skip modules that weren't directly imported.
1209 if (!M.isDirectlyImported())
1210 continue;
1211
1212 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1213 AddSourceLocation(M.ImportLoc, Record);
1214
1215 // If we have calculated signature, there is no need to store
1216 // the size or timestamp.
1217 Record.push_back(M.Signature ? 0 : M.File->getSize());
1218 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1219
1220 for (auto I : M.Signature)
1221 Record.push_back(I);
1222
1223 AddString(M.ModuleName, Record);
1224 AddPath(M.FileName, Record);
1225 }
1226 Stream.EmitRecord(IMPORTS, Record);
1227 }
1228
1229 // Write the options block.
1230 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1231
1232 // Language options.
1233 Record.clear();
1234 const LangOptions &LangOpts = Context.getLangOpts();
1235 #define LANGOPT(Name, Bits, Default, Description) \
1236 Record.push_back(LangOpts.Name);
1237 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1238 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1239 #include "clang/Basic/LangOptions.def"
1240 #define SANITIZER(NAME, ID) \
1241 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1242 #include "clang/Basic/Sanitizers.def"
1243
1244 Record.push_back(LangOpts.ModuleFeatures.size());
1245 for (StringRef Feature : LangOpts.ModuleFeatures)
1246 AddString(Feature, Record);
1247
1248 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1249 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1250
1251 AddString(LangOpts.CurrentModule, Record);
1252
1253 // Comment options.
1254 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1255 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1256 AddString(I, Record);
1257 }
1258 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1259
1260 // OpenMP offloading options.
1261 Record.push_back(LangOpts.OMPTargetTriples.size());
1262 for (auto &T : LangOpts.OMPTargetTriples)
1263 AddString(T.getTriple(), Record);
1264
1265 AddString(LangOpts.OMPHostIRFile, Record);
1266
1267 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1268
1269 // Target options.
1270 Record.clear();
1271 const TargetInfo &Target = Context.getTargetInfo();
1272 const TargetOptions &TargetOpts = Target.getTargetOpts();
1273 AddString(TargetOpts.Triple, Record);
1274 AddString(TargetOpts.CPU, Record);
1275 AddString(TargetOpts.TuneCPU, Record);
1276 AddString(TargetOpts.ABI, Record);
1277 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1278 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1279 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1280 }
1281 Record.push_back(TargetOpts.Features.size());
1282 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1283 AddString(TargetOpts.Features[I], Record);
1284 }
1285 Stream.EmitRecord(TARGET_OPTIONS, Record);
1286
1287 // File system options.
1288 Record.clear();
1289 const FileSystemOptions &FSOpts =
1290 Context.getSourceManager().getFileManager().getFileSystemOpts();
1291 AddString(FSOpts.WorkingDir, Record);
1292 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1293
1294 // Header search options.
1295 Record.clear();
1296 const HeaderSearchOptions &HSOpts
1297 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1298 AddString(HSOpts.Sysroot, Record);
1299
1300 // Include entries.
1301 Record.push_back(HSOpts.UserEntries.size());
1302 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1303 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1304 AddString(Entry.Path, Record);
1305 Record.push_back(static_cast<unsigned>(Entry.Group));
1306 Record.push_back(Entry.IsFramework);
1307 Record.push_back(Entry.IgnoreSysRoot);
1308 }
1309
1310 // System header prefixes.
1311 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1312 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1313 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1314 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1315 }
1316
1317 AddString(HSOpts.ResourceDir, Record);
1318 AddString(HSOpts.ModuleCachePath, Record);
1319 AddString(HSOpts.ModuleUserBuildPath, Record);
1320 Record.push_back(HSOpts.DisableModuleHash);
1321 Record.push_back(HSOpts.ImplicitModuleMaps);
1322 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
1323 Record.push_back(HSOpts.EnablePrebuiltImplicitModules);
1324 Record.push_back(HSOpts.UseBuiltinIncludes);
1325 Record.push_back(HSOpts.UseStandardSystemIncludes);
1326 Record.push_back(HSOpts.UseStandardCXXIncludes);
1327 Record.push_back(HSOpts.UseLibcxx);
1328 // Write out the specific module cache path that contains the module files.
1329 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1330 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1331
1332 // Preprocessor options.
1333 Record.clear();
1334 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1335
1336 // Macro definitions.
1337 Record.push_back(PPOpts.Macros.size());
1338 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1339 AddString(PPOpts.Macros[I].first, Record);
1340 Record.push_back(PPOpts.Macros[I].second);
1341 }
1342
1343 // Includes
1344 Record.push_back(PPOpts.Includes.size());
1345 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1346 AddString(PPOpts.Includes[I], Record);
1347
1348 // Macro includes
1349 Record.push_back(PPOpts.MacroIncludes.size());
1350 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1351 AddString(PPOpts.MacroIncludes[I], Record);
1352
1353 Record.push_back(PPOpts.UsePredefines);
1354 // Detailed record is important since it is used for the module cache hash.
1355 Record.push_back(PPOpts.DetailedRecord);
1356 AddString(PPOpts.ImplicitPCHInclude, Record);
1357 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1358 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1359
1360 // Leave the options block.
1361 Stream.ExitBlock();
1362
1363 // Original file name and file ID
1364 SourceManager &SM = Context.getSourceManager();
1365 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1366 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
1367 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1368 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1369 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1370 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
1371
1372 Record.clear();
1373 Record.push_back(ORIGINAL_FILE);
1374 Record.push_back(SM.getMainFileID().getOpaqueValue());
1375 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1376 }
1377
1378 Record.clear();
1379 Record.push_back(SM.getMainFileID().getOpaqueValue());
1380 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1381
1382 // Original PCH directory
1383 if (!OutputFile.empty() && OutputFile != "-") {
1384 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1385 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1387 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
1388
1389 SmallString<128> OutputPath(OutputFile);
1390
1391 SM.getFileManager().makeAbsolutePath(OutputPath);
1392 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1393
1394 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
1395 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1396 }
1397
1398 WriteInputFiles(Context.SourceMgr,
1399 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1400 PP.getLangOpts().Modules);
1401 Stream.ExitBlock();
1402 }
1403
1404 namespace {
1405
1406 /// An input file.
1407 struct InputFileEntry {
1408 const FileEntry *File;
1409 bool IsSystemFile;
1410 bool IsTransient;
1411 bool BufferOverridden;
1412 bool IsTopLevelModuleMap;
1413 uint32_t ContentHash[2];
1414 };
1415
1416 } // namespace
1417
WriteInputFiles(SourceManager & SourceMgr,HeaderSearchOptions & HSOpts,bool Modules)1418 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1419 HeaderSearchOptions &HSOpts,
1420 bool Modules) {
1421 using namespace llvm;
1422
1423 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1424
1425 // Create input-file abbreviation.
1426 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
1427 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1428 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1429 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1430 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1431 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1432 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1433 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1434 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1435 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
1436
1437 // Create input file hash abbreviation.
1438 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1439 IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1440 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1441 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1442 unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1443
1444 // Get all ContentCache objects for files, sorted by whether the file is a
1445 // system one or not. System files go at the back, users files at the front.
1446 std::deque<InputFileEntry> SortedFiles;
1447 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1448 // Get this source location entry.
1449 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1450 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1451
1452 // We only care about file entries that were not overridden.
1453 if (!SLoc->isFile())
1454 continue;
1455 const SrcMgr::FileInfo &File = SLoc->getFile();
1456 const SrcMgr::ContentCache *Cache = &File.getContentCache();
1457 if (!Cache->OrigEntry)
1458 continue;
1459
1460 InputFileEntry Entry;
1461 Entry.File = Cache->OrigEntry;
1462 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
1463 Entry.IsTransient = Cache->IsTransient;
1464 Entry.BufferOverridden = Cache->BufferOverridden;
1465 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1466 File.getIncludeLoc().isInvalid();
1467
1468 auto ContentHash = hash_code(-1);
1469 if (PP->getHeaderSearchInfo()
1470 .getHeaderSearchOpts()
1471 .ValidateASTInputFilesContent) {
1472 auto MemBuff = Cache->getBufferIfLoaded();
1473 if (MemBuff)
1474 ContentHash = hash_value(MemBuff->getBuffer());
1475 else
1476 // FIXME: The path should be taken from the FileEntryRef.
1477 PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1478 << Entry.File->getName();
1479 }
1480 auto CH = llvm::APInt(64, ContentHash);
1481 Entry.ContentHash[0] =
1482 static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1483 Entry.ContentHash[1] =
1484 static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1485
1486 if (Entry.IsSystemFile)
1487 SortedFiles.push_back(Entry);
1488 else
1489 SortedFiles.push_front(Entry);
1490 }
1491
1492 unsigned UserFilesNum = 0;
1493 // Write out all of the input files.
1494 std::vector<uint64_t> InputFileOffsets;
1495 for (const auto &Entry : SortedFiles) {
1496 uint32_t &InputFileID = InputFileIDs[Entry.File];
1497 if (InputFileID != 0)
1498 continue; // already recorded this file.
1499
1500 // Record this entry's offset.
1501 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1502
1503 InputFileID = InputFileOffsets.size();
1504
1505 if (!Entry.IsSystemFile)
1506 ++UserFilesNum;
1507
1508 // Emit size/modification time for this file.
1509 // And whether this file was overridden.
1510 {
1511 RecordData::value_type Record[] = {
1512 INPUT_FILE,
1513 InputFileOffsets.size(),
1514 (uint64_t)Entry.File->getSize(),
1515 (uint64_t)getTimestampForOutput(Entry.File),
1516 Entry.BufferOverridden,
1517 Entry.IsTransient,
1518 Entry.IsTopLevelModuleMap};
1519
1520 // FIXME: The path should be taken from the FileEntryRef.
1521 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1522 }
1523
1524 // Emit content hash for this file.
1525 {
1526 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1527 Entry.ContentHash[1]};
1528 Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1529 }
1530 }
1531
1532 Stream.ExitBlock();
1533
1534 // Create input file offsets abbreviation.
1535 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
1536 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1537 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1538 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1539 // input files
1540 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1541 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
1542
1543 // Write input file offsets.
1544 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1545 InputFileOffsets.size(), UserFilesNum};
1546 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1547 }
1548
1549 //===----------------------------------------------------------------------===//
1550 // Source Manager Serialization
1551 //===----------------------------------------------------------------------===//
1552
1553 /// Create an abbreviation for the SLocEntry that refers to a
1554 /// file.
CreateSLocFileAbbrev(llvm::BitstreamWriter & Stream)1555 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1556 using namespace llvm;
1557
1558 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1559 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1564 // FileEntry fields.
1565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1569 return Stream.EmitAbbrev(std::move(Abbrev));
1570 }
1571
1572 /// Create an abbreviation for the SLocEntry that refers to a
1573 /// buffer.
CreateSLocBufferAbbrev(llvm::BitstreamWriter & Stream)1574 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1575 using namespace llvm;
1576
1577 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1578 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
1582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1584 return Stream.EmitAbbrev(std::move(Abbrev));
1585 }
1586
1587 /// Create an abbreviation for the SLocEntry that refers to a
1588 /// buffer's blob.
CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter & Stream,bool Compressed)1589 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1590 bool Compressed) {
1591 using namespace llvm;
1592
1593 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1594 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1595 : SM_SLOC_BUFFER_BLOB));
1596 if (Compressed)
1597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
1598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1599 return Stream.EmitAbbrev(std::move(Abbrev));
1600 }
1601
1602 /// Create an abbreviation for the SLocEntry that refers to a macro
1603 /// expansion.
CreateSLocExpansionAbbrev(llvm::BitstreamWriter & Stream)1604 static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1605 using namespace llvm;
1606
1607 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1608 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
1613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
1614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1615 return Stream.EmitAbbrev(std::move(Abbrev));
1616 }
1617
1618 namespace {
1619
1620 // Trait used for the on-disk hash table of header search information.
1621 class HeaderFileInfoTrait {
1622 ASTWriter &Writer;
1623
1624 // Keep track of the framework names we've used during serialization.
1625 SmallString<128> FrameworkStringData;
1626 llvm::StringMap<unsigned> FrameworkNameOffset;
1627
1628 public:
HeaderFileInfoTrait(ASTWriter & Writer)1629 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1630
1631 struct key_type {
1632 StringRef Filename;
1633 off_t Size;
1634 time_t ModTime;
1635 };
1636 using key_type_ref = const key_type &;
1637
1638 using UnresolvedModule =
1639 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
1640
1641 struct data_type {
1642 const HeaderFileInfo &HFI;
1643 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1644 UnresolvedModule Unresolved;
1645 };
1646 using data_type_ref = const data_type &;
1647
1648 using hash_value_type = unsigned;
1649 using offset_type = unsigned;
1650
ComputeHash(key_type_ref key)1651 hash_value_type ComputeHash(key_type_ref key) {
1652 // The hash is based only on size/time of the file, so that the reader can
1653 // match even when symlinking or excess path elements ("foo/../", "../")
1654 // change the form of the name. However, complete path is still the key.
1655 return llvm::hash_combine(key.Size, key.ModTime);
1656 }
1657
1658 std::pair<unsigned, unsigned>
EmitKeyDataLength(raw_ostream & Out,key_type_ref key,data_type_ref Data)1659 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1660 using namespace llvm::support;
1661
1662 endian::Writer LE(Out, little);
1663 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
1664 LE.write<uint16_t>(KeyLen);
1665 unsigned DataLen = 1 + 2 + 4 + 4;
1666 for (auto ModInfo : Data.KnownHeaders)
1667 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1668 DataLen += 4;
1669 if (Data.Unresolved.getPointer())
1670 DataLen += 4;
1671 LE.write<uint8_t>(DataLen);
1672 return std::make_pair(KeyLen, DataLen);
1673 }
1674
EmitKey(raw_ostream & Out,key_type_ref key,unsigned KeyLen)1675 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1676 using namespace llvm::support;
1677
1678 endian::Writer LE(Out, little);
1679 LE.write<uint64_t>(key.Size);
1680 KeyLen -= 8;
1681 LE.write<uint64_t>(key.ModTime);
1682 KeyLen -= 8;
1683 Out.write(key.Filename.data(), KeyLen);
1684 }
1685
EmitData(raw_ostream & Out,key_type_ref key,data_type_ref Data,unsigned DataLen)1686 void EmitData(raw_ostream &Out, key_type_ref key,
1687 data_type_ref Data, unsigned DataLen) {
1688 using namespace llvm::support;
1689
1690 endian::Writer LE(Out, little);
1691 uint64_t Start = Out.tell(); (void)Start;
1692
1693 unsigned char Flags = (Data.HFI.isImport << 5)
1694 | (Data.HFI.isPragmaOnce << 4)
1695 | (Data.HFI.DirInfo << 1)
1696 | Data.HFI.IndexHeaderMapHeader;
1697 LE.write<uint8_t>(Flags);
1698 LE.write<uint16_t>(Data.HFI.NumIncludes);
1699
1700 if (!Data.HFI.ControllingMacro)
1701 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
1702 else
1703 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1704
1705 unsigned Offset = 0;
1706 if (!Data.HFI.Framework.empty()) {
1707 // If this header refers into a framework, save the framework name.
1708 llvm::StringMap<unsigned>::iterator Pos
1709 = FrameworkNameOffset.find(Data.HFI.Framework);
1710 if (Pos == FrameworkNameOffset.end()) {
1711 Offset = FrameworkStringData.size() + 1;
1712 FrameworkStringData.append(Data.HFI.Framework);
1713 FrameworkStringData.push_back(0);
1714
1715 FrameworkNameOffset[Data.HFI.Framework] = Offset;
1716 } else
1717 Offset = Pos->second;
1718 }
1719 LE.write<uint32_t>(Offset);
1720
1721 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1722 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1723 uint32_t Value = (ModID << 2) | (unsigned)Role;
1724 assert((Value >> 2) == ModID && "overflow in header module info");
1725 LE.write<uint32_t>(Value);
1726 }
1727 };
1728
1729 // FIXME: If the header is excluded, we should write out some
1730 // record of that fact.
1731 for (auto ModInfo : Data.KnownHeaders)
1732 EmitModule(ModInfo.getModule(), ModInfo.getRole());
1733 if (Data.Unresolved.getPointer())
1734 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
1735
1736 assert(Out.tell() - Start == DataLen && "Wrong data length");
1737 }
1738
strings_begin() const1739 const char *strings_begin() const { return FrameworkStringData.begin(); }
strings_end() const1740 const char *strings_end() const { return FrameworkStringData.end(); }
1741 };
1742
1743 } // namespace
1744
1745 /// Write the header search block for the list of files that
1746 ///
1747 /// \param HS The header search structure to save.
WriteHeaderSearch(const HeaderSearch & HS)1748 void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1749 HeaderFileInfoTrait GeneratorTrait(*this);
1750 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1751 SmallVector<const char *, 4> SavedStrings;
1752 unsigned NumHeaderSearchEntries = 0;
1753
1754 // Find all unresolved headers for the current module. We generally will
1755 // have resolved them before we get here, but not necessarily: we might be
1756 // compiling a preprocessed module, where there is no requirement for the
1757 // original files to exist any more.
1758 const HeaderFileInfo Empty; // So we can take a reference.
1759 if (WritingModule) {
1760 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1761 while (!Worklist.empty()) {
1762 Module *M = Worklist.pop_back_val();
1763 // We don't care about headers in unimportable submodules.
1764 if (M->isUnimportable())
1765 continue;
1766
1767 // Map to disk files where possible, to pick up any missing stat
1768 // information. This also means we don't need to check the unresolved
1769 // headers list when emitting resolved headers in the first loop below.
1770 // FIXME: It'd be preferable to avoid doing this if we were given
1771 // sufficient stat information in the module map.
1772 HS.getModuleMap().resolveHeaderDirectives(M);
1773
1774 // If the file didn't exist, we can still create a module if we were given
1775 // enough information in the module map.
1776 for (auto U : M->MissingHeaders) {
1777 // Check that we were given enough information to build a module
1778 // without this file existing on disk.
1779 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1780 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1781 << WritingModule->getFullModuleName() << U.Size.hasValue()
1782 << U.FileName;
1783 continue;
1784 }
1785
1786 // Form the effective relative pathname for the file.
1787 SmallString<128> Filename(M->Directory->getName());
1788 llvm::sys::path::append(Filename, U.FileName);
1789 PreparePathForOutput(Filename);
1790
1791 StringRef FilenameDup = strdup(Filename.c_str());
1792 SavedStrings.push_back(FilenameDup.data());
1793
1794 HeaderFileInfoTrait::key_type Key = {
1795 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
1796 };
1797 HeaderFileInfoTrait::data_type Data = {
1798 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
1799 };
1800 // FIXME: Deal with cases where there are multiple unresolved header
1801 // directives in different submodules for the same header.
1802 Generator.insert(Key, Data, GeneratorTrait);
1803 ++NumHeaderSearchEntries;
1804 }
1805
1806 Worklist.append(M->submodule_begin(), M->submodule_end());
1807 }
1808 }
1809
1810 SmallVector<const FileEntry *, 16> FilesByUID;
1811 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1812
1813 if (FilesByUID.size() > HS.header_file_size())
1814 FilesByUID.resize(HS.header_file_size());
1815
1816 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1817 const FileEntry *File = FilesByUID[UID];
1818 if (!File)
1819 continue;
1820
1821 // Get the file info. This will load info from the external source if
1822 // necessary. Skip emitting this file if we have no information on it
1823 // as a header file (in which case HFI will be null) or if it hasn't
1824 // changed since it was loaded. Also skip it if it's for a modular header
1825 // from a different module; in that case, we rely on the module(s)
1826 // containing the header to provide this information.
1827 const HeaderFileInfo *HFI =
1828 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1829 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
1830 continue;
1831
1832 // Massage the file path into an appropriate form.
1833 StringRef Filename = File->getName();
1834 SmallString<128> FilenameTmp(Filename);
1835 if (PreparePathForOutput(FilenameTmp)) {
1836 // If we performed any translation on the file name at all, we need to
1837 // save this string, since the generator will refer to it later.
1838 Filename = StringRef(strdup(FilenameTmp.c_str()));
1839 SavedStrings.push_back(Filename.data());
1840 }
1841
1842 HeaderFileInfoTrait::key_type Key = {
1843 Filename, File->getSize(), getTimestampForOutput(File)
1844 };
1845 HeaderFileInfoTrait::data_type Data = {
1846 *HFI, HS.getModuleMap().findResolvedModulesForHeader(File), {}
1847 };
1848 Generator.insert(Key, Data, GeneratorTrait);
1849 ++NumHeaderSearchEntries;
1850 }
1851
1852 // Create the on-disk hash table in a buffer.
1853 SmallString<4096> TableData;
1854 uint32_t BucketOffset;
1855 {
1856 using namespace llvm::support;
1857
1858 llvm::raw_svector_ostream Out(TableData);
1859 // Make sure that no bucket is at offset 0
1860 endian::write<uint32_t>(Out, 0, little);
1861 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1862 }
1863
1864 // Create a blob abbreviation
1865 using namespace llvm;
1866
1867 auto Abbrev = std::make_shared<BitCodeAbbrev>();
1868 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1873 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
1874
1875 // Write the header search table
1876 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1877 NumHeaderSearchEntries, TableData.size()};
1878 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
1879 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
1880
1881 // Free all of the strings we had to duplicate.
1882 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1883 free(const_cast<char *>(SavedStrings[I]));
1884 }
1885
emitBlob(llvm::BitstreamWriter & Stream,StringRef Blob,unsigned SLocBufferBlobCompressedAbbrv,unsigned SLocBufferBlobAbbrv)1886 static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
1887 unsigned SLocBufferBlobCompressedAbbrv,
1888 unsigned SLocBufferBlobAbbrv) {
1889 using RecordDataType = ASTWriter::RecordData::value_type;
1890
1891 // Compress the buffer if possible. We expect that almost all PCM
1892 // consumers will not want its contents.
1893 SmallString<0> CompressedBuffer;
1894 if (llvm::zlib::isAvailable()) {
1895 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
1896 if (!E) {
1897 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
1898 Blob.size() - 1};
1899 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
1900 CompressedBuffer);
1901 return;
1902 }
1903 llvm::consumeError(std::move(E));
1904 }
1905
1906 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
1907 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
1908 }
1909
1910 /// Writes the block containing the serialized form of the
1911 /// source manager.
1912 ///
1913 /// TODO: We should probably use an on-disk hash table (stored in a
1914 /// blob), indexed based on the file name, so that we only create
1915 /// entries for files that we actually need. In the common case (no
1916 /// errors), we probably won't have to create file entries for any of
1917 /// the files in the AST.
WriteSourceManagerBlock(SourceManager & SourceMgr,const Preprocessor & PP)1918 void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1919 const Preprocessor &PP) {
1920 RecordData Record;
1921
1922 // Enter the source manager block.
1923 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
1924 const uint64_t SourceManagerBlockOffset = Stream.GetCurrentBitNo();
1925
1926 // Abbreviations for the various kinds of source-location entries.
1927 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1928 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1929 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
1930 unsigned SLocBufferBlobCompressedAbbrv =
1931 CreateSLocBufferBlobAbbrev(Stream, true);
1932 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
1933
1934 // Write out the source location entry table. We skip the first
1935 // entry, which is always the same dummy entry.
1936 std::vector<uint32_t> SLocEntryOffsets;
1937 uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
1938 RecordData PreloadSLocs;
1939 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1940 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
1941 I != N; ++I) {
1942 // Get this source location entry.
1943 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1944 FileID FID = FileID::get(I);
1945 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
1946
1947 // Record the offset of this source-location entry.
1948 uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
1949 assert((Offset >> 32) == 0 && "SLocEntry offset too large");
1950 SLocEntryOffsets.push_back(Offset);
1951
1952 // Figure out which record code to use.
1953 unsigned Code;
1954 if (SLoc->isFile()) {
1955 const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache();
1956 if (Cache->OrigEntry) {
1957 Code = SM_SLOC_FILE_ENTRY;
1958 } else
1959 Code = SM_SLOC_BUFFER_ENTRY;
1960 } else
1961 Code = SM_SLOC_EXPANSION_ENTRY;
1962 Record.clear();
1963 Record.push_back(Code);
1964
1965 // Starting offset of this entry within this module, so skip the dummy.
1966 Record.push_back(SLoc->getOffset() - 2);
1967 if (SLoc->isFile()) {
1968 const SrcMgr::FileInfo &File = SLoc->getFile();
1969 AddSourceLocation(File.getIncludeLoc(), Record);
1970 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1971 Record.push_back(File.hasLineDirectives());
1972
1973 const SrcMgr::ContentCache *Content = &File.getContentCache();
1974 bool EmitBlob = false;
1975 if (Content->OrigEntry) {
1976 assert(Content->OrigEntry == Content->ContentsEntry &&
1977 "Writing to AST an overridden file is not supported");
1978
1979 // The source location entry is a file. Emit input file ID.
1980 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1981 Record.push_back(InputFileIDs[Content->OrigEntry]);
1982
1983 Record.push_back(File.NumCreatedFIDs);
1984
1985 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
1986 if (FDI != FileDeclIDs.end()) {
1987 Record.push_back(FDI->second->FirstDeclIndex);
1988 Record.push_back(FDI->second->DeclIDs.size());
1989 } else {
1990 Record.push_back(0);
1991 Record.push_back(0);
1992 }
1993
1994 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
1995
1996 if (Content->BufferOverridden || Content->IsTransient)
1997 EmitBlob = true;
1998 } else {
1999 // The source location entry is a buffer. The blob associated
2000 // with this entry contains the contents of the buffer.
2001
2002 // We add one to the size so that we capture the trailing NULL
2003 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2004 // the reader side).
2005 llvm::Optional<llvm::MemoryBufferRef> Buffer =
2006 Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2007 StringRef Name = Buffer ? Buffer->getBufferIdentifier() : "";
2008 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
2009 StringRef(Name.data(), Name.size() + 1));
2010 EmitBlob = true;
2011
2012 if (Name == "<built-in>")
2013 PreloadSLocs.push_back(SLocEntryOffsets.size());
2014 }
2015
2016 if (EmitBlob) {
2017 // Include the implicit terminating null character in the on-disk buffer
2018 // if we're writing it uncompressed.
2019 llvm::Optional<llvm::MemoryBufferRef> Buffer =
2020 Content->getBufferOrNone(PP.getDiagnostics(), PP.getFileManager());
2021 if (!Buffer)
2022 Buffer = llvm::MemoryBufferRef("<<<INVALID BUFFER>>>", "");
2023 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2024 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2025 SLocBufferBlobAbbrv);
2026 }
2027 } else {
2028 // The source location entry is a macro expansion.
2029 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
2030 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2031 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2032 AddSourceLocation(Expansion.isMacroArgExpansion()
2033 ? SourceLocation()
2034 : Expansion.getExpansionLocEnd(),
2035 Record);
2036 Record.push_back(Expansion.isExpansionTokenRange());
2037
2038 // Compute the token length for this macro expansion.
2039 unsigned NextOffset = SourceMgr.getNextLocalOffset();
2040 if (I + 1 != N)
2041 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2042 Record.push_back(NextOffset - SLoc->getOffset() - 1);
2043 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
2044 }
2045 }
2046
2047 Stream.ExitBlock();
2048
2049 if (SLocEntryOffsets.empty())
2050 return;
2051
2052 // Write the source-location offsets table into the AST block. This
2053 // table is used for lazily loading source-location information.
2054 using namespace llvm;
2055
2056 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2057 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
2059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
2060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2062 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2063 {
2064 RecordData::value_type Record[] = {
2065 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2066 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */,
2067 SLocEntryOffsetsBase - SourceManagerBlockOffset};
2068 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2069 bytes(SLocEntryOffsets));
2070 }
2071 // Write the source location entry preloads array, telling the AST
2072 // reader which source locations entries it should load eagerly.
2073 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
2074
2075 // Write the line table. It depends on remapping working, so it must come
2076 // after the source location offsets.
2077 if (SourceMgr.hasLineTable()) {
2078 LineTableInfo &LineTable = SourceMgr.getLineTable();
2079
2080 Record.clear();
2081
2082 // Emit the needed file names.
2083 llvm::DenseMap<int, int> FilenameMap;
2084 FilenameMap[-1] = -1; // For unspecified filenames.
2085 for (const auto &L : LineTable) {
2086 if (L.first.ID < 0)
2087 continue;
2088 for (auto &LE : L.second) {
2089 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2090 FilenameMap.size() - 1)).second)
2091 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2092 }
2093 }
2094 Record.push_back(0);
2095
2096 // Emit the line entries
2097 for (const auto &L : LineTable) {
2098 // Only emit entries for local files.
2099 if (L.first.ID < 0)
2100 continue;
2101
2102 // Emit the file ID
2103 Record.push_back(L.first.ID);
2104
2105 // Emit the line entries
2106 Record.push_back(L.second.size());
2107 for (const auto &LE : L.second) {
2108 Record.push_back(LE.FileOffset);
2109 Record.push_back(LE.LineNo);
2110 Record.push_back(FilenameMap[LE.FilenameID]);
2111 Record.push_back((unsigned)LE.FileKind);
2112 Record.push_back(LE.IncludeOffset);
2113 }
2114 }
2115
2116 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2117 }
2118 }
2119
2120 //===----------------------------------------------------------------------===//
2121 // Preprocessor Serialization
2122 //===----------------------------------------------------------------------===//
2123
shouldIgnoreMacro(MacroDirective * MD,bool IsModule,const Preprocessor & PP)2124 static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2125 const Preprocessor &PP) {
2126 if (MacroInfo *MI = MD->getMacroInfo())
2127 if (MI->isBuiltinMacro())
2128 return true;
2129
2130 if (IsModule) {
2131 SourceLocation Loc = MD->getLocation();
2132 if (Loc.isInvalid())
2133 return true;
2134 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2135 return true;
2136 }
2137
2138 return false;
2139 }
2140
2141 /// Writes the block containing the serialized form of the
2142 /// preprocessor.
WritePreprocessor(const Preprocessor & PP,bool IsModule)2143 void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2144 uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2145
2146 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2147 if (PPRec)
2148 WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
2149
2150 RecordData Record;
2151 RecordData ModuleMacroRecord;
2152
2153 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2154 if (PP.getCounterValue() != 0) {
2155 RecordData::value_type Record[] = {PP.getCounterValue()};
2156 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2157 }
2158
2159 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2160 assert(!IsModule);
2161 auto SkipInfo = PP.getPreambleSkipInfo();
2162 if (SkipInfo.hasValue()) {
2163 Record.push_back(true);
2164 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2165 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2166 Record.push_back(SkipInfo->FoundNonSkipPortion);
2167 Record.push_back(SkipInfo->FoundElse);
2168 AddSourceLocation(SkipInfo->ElseLoc, Record);
2169 } else {
2170 Record.push_back(false);
2171 }
2172 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2173 AddSourceLocation(Cond.IfLoc, Record);
2174 Record.push_back(Cond.WasSkipping);
2175 Record.push_back(Cond.FoundNonSkip);
2176 Record.push_back(Cond.FoundElse);
2177 }
2178 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2179 Record.clear();
2180 }
2181
2182 // Enter the preprocessor block.
2183 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2184
2185 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2186 // FIXME: Include a location for the use, and say which one was used.
2187 if (PP.SawDateOrTime())
2188 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2189
2190 // Loop over all the macro directives that are live at the end of the file,
2191 // emitting each to the PP section.
2192
2193 // Construct the list of identifiers with macro directives that need to be
2194 // serialized.
2195 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2196 for (auto &Id : PP.getIdentifierTable())
2197 if (Id.second->hadMacroDefinition() &&
2198 (!Id.second->isFromAST() ||
2199 Id.second->hasChangedSinceDeserialization()))
2200 MacroIdentifiers.push_back(Id.second);
2201 // Sort the set of macro definitions that need to be serialized by the
2202 // name of the macro, to provide a stable ordering.
2203 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
2204
2205 // Emit the macro directives as a list and associate the offset with the
2206 // identifier they belong to.
2207 for (const IdentifierInfo *Name : MacroIdentifiers) {
2208 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2209 uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2210 assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
2211
2212 // Emit the macro directives in reverse source order.
2213 for (; MD; MD = MD->getPrevious()) {
2214 // Once we hit an ignored macro, we're done: the rest of the chain
2215 // will all be ignored macros.
2216 if (shouldIgnoreMacro(MD, IsModule, PP))
2217 break;
2218
2219 AddSourceLocation(MD->getLocation(), Record);
2220 Record.push_back(MD->getKind());
2221 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2222 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2223 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2224 Record.push_back(VisMD->isPublic());
2225 }
2226 }
2227
2228 // Write out any exported module macros.
2229 bool EmittedModuleMacros = false;
2230 // We write out exported module macros for PCH as well.
2231 auto Leafs = PP.getLeafModuleMacros(Name);
2232 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2233 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2234 while (!Worklist.empty()) {
2235 auto *Macro = Worklist.pop_back_val();
2236
2237 // Emit a record indicating this submodule exports this macro.
2238 ModuleMacroRecord.push_back(
2239 getSubmoduleID(Macro->getOwningModule()));
2240 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2241 for (auto *M : Macro->overrides())
2242 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2243
2244 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2245 ModuleMacroRecord.clear();
2246
2247 // Enqueue overridden macros once we've visited all their ancestors.
2248 for (auto *M : Macro->overrides())
2249 if (++Visits[M] == M->getNumOverridingMacros())
2250 Worklist.push_back(M);
2251
2252 EmittedModuleMacros = true;
2253 }
2254
2255 if (Record.empty() && !EmittedModuleMacros)
2256 continue;
2257
2258 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2259 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2260 Record.clear();
2261 }
2262
2263 /// Offsets of each of the macros into the bitstream, indexed by
2264 /// the local macro ID
2265 ///
2266 /// For each identifier that is associated with a macro, this map
2267 /// provides the offset into the bitstream where that macro is
2268 /// defined.
2269 std::vector<uint32_t> MacroOffsets;
2270
2271 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2272 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2273 MacroInfo *MI = MacroInfosToEmit[I].MI;
2274 MacroID ID = MacroInfosToEmit[I].ID;
2275
2276 if (ID < FirstMacroID) {
2277 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2278 continue;
2279 }
2280
2281 // Record the local offset of this macro.
2282 unsigned Index = ID - FirstMacroID;
2283 if (Index >= MacroOffsets.size())
2284 MacroOffsets.resize(Index + 1);
2285
2286 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2287 assert((Offset >> 32) == 0 && "Macro offset too large");
2288 MacroOffsets[Index] = Offset;
2289
2290 AddIdentifierRef(Name, Record);
2291 AddSourceLocation(MI->getDefinitionLoc(), Record);
2292 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2293 Record.push_back(MI->isUsed());
2294 Record.push_back(MI->isUsedForHeaderGuard());
2295 unsigned Code;
2296 if (MI->isObjectLike()) {
2297 Code = PP_MACRO_OBJECT_LIKE;
2298 } else {
2299 Code = PP_MACRO_FUNCTION_LIKE;
2300
2301 Record.push_back(MI->isC99Varargs());
2302 Record.push_back(MI->isGNUVarargs());
2303 Record.push_back(MI->hasCommaPasting());
2304 Record.push_back(MI->getNumParams());
2305 for (const IdentifierInfo *Param : MI->params())
2306 AddIdentifierRef(Param, Record);
2307 }
2308
2309 // If we have a detailed preprocessing record, record the macro definition
2310 // ID that corresponds to this macro.
2311 if (PPRec)
2312 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2313
2314 Stream.EmitRecord(Code, Record);
2315 Record.clear();
2316
2317 // Emit the tokens array.
2318 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2319 // Note that we know that the preprocessor does not have any annotation
2320 // tokens in it because they are created by the parser, and thus can't
2321 // be in a macro definition.
2322 const Token &Tok = MI->getReplacementToken(TokNo);
2323 AddToken(Tok, Record);
2324 Stream.EmitRecord(PP_TOKEN, Record);
2325 Record.clear();
2326 }
2327 ++NumMacros;
2328 }
2329
2330 Stream.ExitBlock();
2331
2332 // Write the offsets table for macro IDs.
2333 using namespace llvm;
2334
2335 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2336 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
2340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2341
2342 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2343 {
2344 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2345 FirstMacroID - NUM_PREDEF_MACRO_IDS,
2346 MacroOffsetsBase - ASTBlockStartOffset};
2347 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2348 }
2349 }
2350
WritePreprocessorDetail(PreprocessingRecord & PPRec,uint64_t MacroOffsetsBase)2351 void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2352 uint64_t MacroOffsetsBase) {
2353 if (PPRec.local_begin() == PPRec.local_end())
2354 return;
2355
2356 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2357
2358 // Enter the preprocessor block.
2359 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2360
2361 // If the preprocessor has a preprocessing record, emit it.
2362 unsigned NumPreprocessingRecords = 0;
2363 using namespace llvm;
2364
2365 // Set up the abbreviation for
2366 unsigned InclusionAbbrev = 0;
2367 {
2368 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2369 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2375 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2376 }
2377
2378 unsigned FirstPreprocessorEntityID
2379 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2380 + NUM_PREDEF_PP_ENTITY_IDS;
2381 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2382 RecordData Record;
2383 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2384 EEnd = PPRec.local_end();
2385 E != EEnd;
2386 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2387 Record.clear();
2388
2389 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2390 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
2391 PreprocessedEntityOffsets.push_back(
2392 PPEntityOffset((*E)->getSourceRange(), Offset));
2393
2394 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2395 // Record this macro definition's ID.
2396 MacroDefinitions[MD] = NextPreprocessorEntityID;
2397
2398 AddIdentifierRef(MD->getName(), Record);
2399 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2400 continue;
2401 }
2402
2403 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2404 Record.push_back(ME->isBuiltinMacro());
2405 if (ME->isBuiltinMacro())
2406 AddIdentifierRef(ME->getName(), Record);
2407 else
2408 Record.push_back(MacroDefinitions[ME->getDefinition()]);
2409 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2410 continue;
2411 }
2412
2413 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2414 Record.push_back(PPD_INCLUSION_DIRECTIVE);
2415 Record.push_back(ID->getFileName().size());
2416 Record.push_back(ID->wasInQuotes());
2417 Record.push_back(static_cast<unsigned>(ID->getKind()));
2418 Record.push_back(ID->importedModule());
2419 SmallString<64> Buffer;
2420 Buffer += ID->getFileName();
2421 // Check that the FileEntry is not null because it was not resolved and
2422 // we create a PCH even with compiler errors.
2423 if (ID->getFile())
2424 Buffer += ID->getFile()->getName();
2425 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2426 continue;
2427 }
2428
2429 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2430 }
2431 Stream.ExitBlock();
2432
2433 // Write the offsets table for the preprocessing record.
2434 if (NumPreprocessingRecords > 0) {
2435 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2436
2437 // Write the offsets table for identifier IDs.
2438 using namespace llvm;
2439
2440 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2441 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2444 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2445
2446 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2447 FirstPreprocessorEntityID -
2448 NUM_PREDEF_PP_ENTITY_IDS};
2449 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2450 bytes(PreprocessedEntityOffsets));
2451 }
2452
2453 // Write the skipped region table for the preprocessing record.
2454 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2455 if (SkippedRanges.size() > 0) {
2456 std::vector<PPSkippedRange> SerializedSkippedRanges;
2457 SerializedSkippedRanges.reserve(SkippedRanges.size());
2458 for (auto const& Range : SkippedRanges)
2459 SerializedSkippedRanges.emplace_back(Range);
2460
2461 using namespace llvm;
2462 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2463 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2465 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2466
2467 Record.clear();
2468 Record.push_back(PPD_SKIPPED_RANGES);
2469 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2470 bytes(SerializedSkippedRanges));
2471 }
2472 }
2473
getLocalOrImportedSubmoduleID(Module * Mod)2474 unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
2475 if (!Mod)
2476 return 0;
2477
2478 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2479 if (Known != SubmoduleIDs.end())
2480 return Known->second;
2481
2482 auto *Top = Mod->getTopLevelModule();
2483 if (Top != WritingModule &&
2484 (getLangOpts().CompilingPCH ||
2485 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
2486 return 0;
2487
2488 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2489 }
2490
getSubmoduleID(Module * Mod)2491 unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2492 // FIXME: This can easily happen, if we have a reference to a submodule that
2493 // did not result in us loading a module file for that submodule. For
2494 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2495 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2496 assert((ID || !Mod) &&
2497 "asked for module ID for non-local, non-imported module");
2498 return ID;
2499 }
2500
2501 /// Compute the number of modules within the given tree (including the
2502 /// given module).
getNumberOfModules(Module * Mod)2503 static unsigned getNumberOfModules(Module *Mod) {
2504 unsigned ChildModules = 0;
2505 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
2506 Sub != SubEnd; ++Sub)
2507 ChildModules += getNumberOfModules(*Sub);
2508
2509 return ChildModules + 1;
2510 }
2511
WriteSubmodules(Module * WritingModule)2512 void ASTWriter::WriteSubmodules(Module *WritingModule) {
2513 // Enter the submodule description block.
2514 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2515
2516 // Write the abbreviations needed for the submodules block.
2517 using namespace llvm;
2518
2519 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2520 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2521 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2523 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
2524 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
2533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2534 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2535
2536 Abbrev = std::make_shared<BitCodeAbbrev>();
2537 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2539 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2540
2541 Abbrev = std::make_shared<BitCodeAbbrev>();
2542 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2544 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2545
2546 Abbrev = std::make_shared<BitCodeAbbrev>();
2547 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2549 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2550
2551 Abbrev = std::make_shared<BitCodeAbbrev>();
2552 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2554 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2555
2556 Abbrev = std::make_shared<BitCodeAbbrev>();
2557 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2560 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2561
2562 Abbrev = std::make_shared<BitCodeAbbrev>();
2563 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2565 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2566
2567 Abbrev = std::make_shared<BitCodeAbbrev>();
2568 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2570 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2571
2572 Abbrev = std::make_shared<BitCodeAbbrev>();
2573 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2575 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2576
2577 Abbrev = std::make_shared<BitCodeAbbrev>();
2578 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2580 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2581
2582 Abbrev = std::make_shared<BitCodeAbbrev>();
2583 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2586 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2587
2588 Abbrev = std::make_shared<BitCodeAbbrev>();
2589 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2591 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2592
2593 Abbrev = std::make_shared<BitCodeAbbrev>();
2594 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2595 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2596 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2597 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2598
2599 Abbrev = std::make_shared<BitCodeAbbrev>();
2600 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2602 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2603
2604 // Write the submodule metadata block.
2605 RecordData::value_type Record[] = {
2606 getNumberOfModules(WritingModule),
2607 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
2608 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2609
2610 // Write all of the submodules.
2611 std::queue<Module *> Q;
2612 Q.push(WritingModule);
2613 while (!Q.empty()) {
2614 Module *Mod = Q.front();
2615 Q.pop();
2616 unsigned ID = getSubmoduleID(Mod);
2617
2618 uint64_t ParentID = 0;
2619 if (Mod->Parent) {
2620 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2621 ParentID = SubmoduleIDs[Mod->Parent];
2622 }
2623
2624 // Emit the definition of the block.
2625 {
2626 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2627 ID,
2628 ParentID,
2629 (RecordData::value_type)Mod->Kind,
2630 Mod->IsFramework,
2631 Mod->IsExplicit,
2632 Mod->IsSystem,
2633 Mod->IsExternC,
2634 Mod->InferSubmodules,
2635 Mod->InferExplicitSubmodules,
2636 Mod->InferExportWildcard,
2637 Mod->ConfigMacrosExhaustive,
2638 Mod->ModuleMapIsPrivate};
2639 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2640 }
2641
2642 // Emit the requirements.
2643 for (const auto &R : Mod->Requirements) {
2644 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2645 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2646 }
2647
2648 // Emit the umbrella header, if there is one.
2649 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
2650 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2651 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2652 UmbrellaHeader.NameAsWritten);
2653 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
2654 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2655 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2656 UmbrellaDir.NameAsWritten);
2657 }
2658
2659 // Emit the headers.
2660 struct {
2661 unsigned RecordKind;
2662 unsigned Abbrev;
2663 Module::HeaderKind HeaderKind;
2664 } HeaderLists[] = {
2665 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2666 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2667 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2668 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2669 Module::HK_PrivateTextual},
2670 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2671 };
2672 for (auto &HL : HeaderLists) {
2673 RecordData::value_type Record[] = {HL.RecordKind};
2674 for (auto &H : Mod->Headers[HL.HeaderKind])
2675 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2676 }
2677
2678 // Emit the top headers.
2679 {
2680 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2681 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2682 for (auto *H : TopHeaders)
2683 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
2684 }
2685
2686 // Emit the imports.
2687 if (!Mod->Imports.empty()) {
2688 RecordData Record;
2689 for (auto *I : Mod->Imports)
2690 Record.push_back(getSubmoduleID(I));
2691 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2692 }
2693
2694 // Emit the exports.
2695 if (!Mod->Exports.empty()) {
2696 RecordData Record;
2697 for (const auto &E : Mod->Exports) {
2698 // FIXME: This may fail; we don't require that all exported modules
2699 // are local or imported.
2700 Record.push_back(getSubmoduleID(E.getPointer()));
2701 Record.push_back(E.getInt());
2702 }
2703 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2704 }
2705
2706 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2707 // Might be unnecessary as use declarations are only used to build the
2708 // module itself.
2709
2710 // Emit the link libraries.
2711 for (const auto &LL : Mod->LinkLibraries) {
2712 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2713 LL.IsFramework};
2714 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2715 }
2716
2717 // Emit the conflicts.
2718 for (const auto &C : Mod->Conflicts) {
2719 // FIXME: This may fail; we don't require that all conflicting modules
2720 // are local or imported.
2721 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2722 getSubmoduleID(C.Other)};
2723 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2724 }
2725
2726 // Emit the configuration macros.
2727 for (const auto &CM : Mod->ConfigMacros) {
2728 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2729 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
2730 }
2731
2732 // Emit the initializers, if any.
2733 RecordData Inits;
2734 for (Decl *D : Context->getModuleInitializers(Mod))
2735 Inits.push_back(GetDeclRef(D));
2736 if (!Inits.empty())
2737 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2738
2739 // Emit the name of the re-exported module, if any.
2740 if (!Mod->ExportAsModule.empty()) {
2741 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
2742 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
2743 }
2744
2745 // Queue up the submodules of this module.
2746 for (auto *M : Mod->submodules())
2747 Q.push(M);
2748 }
2749
2750 Stream.ExitBlock();
2751
2752 assert((NextSubmoduleID - FirstSubmoduleID ==
2753 getNumberOfModules(WritingModule)) &&
2754 "Wrong # of submodules; found a reference to a non-local, "
2755 "non-imported submodule?");
2756 }
2757
WritePragmaDiagnosticMappings(const DiagnosticsEngine & Diag,bool isModule)2758 void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2759 bool isModule) {
2760 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2761 DiagStateIDMap;
2762 unsigned CurrID = 0;
2763 RecordData Record;
2764
2765 auto EncodeDiagStateFlags =
2766 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
2767 unsigned Result = (unsigned)DS->ExtBehavior;
2768 for (unsigned Val :
2769 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
2770 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
2771 (unsigned)DS->SuppressSystemWarnings})
2772 Result = (Result << 1) | Val;
2773 return Result;
2774 };
2775
2776 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
2777 Record.push_back(Flags);
2778
2779 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2780 bool IncludeNonPragmaStates) {
2781 // Ensure that the diagnostic state wasn't modified since it was created.
2782 // We will not correctly round-trip this information otherwise.
2783 assert(Flags == EncodeDiagStateFlags(State) &&
2784 "diag state flags vary in single AST file");
2785
2786 unsigned &DiagStateID = DiagStateIDMap[State];
2787 Record.push_back(DiagStateID);
2788
2789 if (DiagStateID == 0) {
2790 DiagStateID = ++CurrID;
2791
2792 // Add a placeholder for the number of mappings.
2793 auto SizeIdx = Record.size();
2794 Record.emplace_back();
2795 for (const auto &I : *State) {
2796 if (I.second.isPragma() || IncludeNonPragmaStates) {
2797 Record.push_back(I.first);
2798 Record.push_back(I.second.serialize());
2799 }
2800 }
2801 // Update the placeholder.
2802 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
2803 }
2804 };
2805
2806 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2807
2808 // Reserve a spot for the number of locations with state transitions.
2809 auto NumLocationsIdx = Record.size();
2810 Record.emplace_back();
2811
2812 // Emit the state transitions.
2813 unsigned NumLocations = 0;
2814 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2815 if (!FileIDAndFile.first.isValid() ||
2816 !FileIDAndFile.second.HasLocalTransitions)
2817 continue;
2818 ++NumLocations;
2819
2820 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
2821 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
2822 AddSourceLocation(Loc, Record);
2823
2824 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2825 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2826 Record.push_back(StatePoint.Offset);
2827 AddDiagState(StatePoint.State, false);
2828 }
2829 }
2830
2831 // Backpatch the number of locations.
2832 Record[NumLocationsIdx] = NumLocations;
2833
2834 // Emit CurDiagStateLoc. Do it last in order to match source order.
2835 //
2836 // This also protects against a hypothetical corner case with simulating
2837 // -Werror settings for implicit modules in the ASTReader, where reading
2838 // CurDiagState out of context could change whether warning pragmas are
2839 // treated as errors.
2840 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2841 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2842
2843 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
2844 }
2845
2846 //===----------------------------------------------------------------------===//
2847 // Type Serialization
2848 //===----------------------------------------------------------------------===//
2849
2850 /// Write the representation of a type to the AST stream.
WriteType(QualType T)2851 void ASTWriter::WriteType(QualType T) {
2852 TypeIdx &IdxRef = TypeIdxs[T];
2853 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2854 IdxRef = TypeIdx(NextTypeID++);
2855 TypeIdx Idx = IdxRef;
2856
2857 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
2858
2859 // Emit the type's representation.
2860 uint64_t Offset = ASTTypeWriter(*this).write(T) - DeclTypesBlockStartOffset;
2861
2862 // Record the offset for this type.
2863 unsigned Index = Idx.getIndex() - FirstTypeID;
2864 if (TypeOffsets.size() == Index)
2865 TypeOffsets.emplace_back(Offset);
2866 else if (TypeOffsets.size() < Index) {
2867 TypeOffsets.resize(Index + 1);
2868 TypeOffsets[Index].setBitOffset(Offset);
2869 } else {
2870 llvm_unreachable("Types emitted in wrong order");
2871 }
2872 }
2873
2874 //===----------------------------------------------------------------------===//
2875 // Declaration Serialization
2876 //===----------------------------------------------------------------------===//
2877
2878 /// Write the block containing all of the declaration IDs
2879 /// lexically declared within the given DeclContext.
2880 ///
2881 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2882 /// bitstream, or 0 if no block was written.
WriteDeclContextLexicalBlock(ASTContext & Context,DeclContext * DC)2883 uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
2884 DeclContext *DC) {
2885 if (DC->decls_empty())
2886 return 0;
2887
2888 uint64_t Offset = Stream.GetCurrentBitNo();
2889 SmallVector<uint32_t, 128> KindDeclPairs;
2890 for (const auto *D : DC->decls()) {
2891 KindDeclPairs.push_back(D->getKind());
2892 KindDeclPairs.push_back(GetDeclRef(D));
2893 }
2894
2895 ++NumLexicalDeclContexts;
2896 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
2897 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2898 bytes(KindDeclPairs));
2899 return Offset;
2900 }
2901
WriteTypeDeclOffsets()2902 void ASTWriter::WriteTypeDeclOffsets() {
2903 using namespace llvm;
2904
2905 // Write the type offsets array
2906 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2907 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
2908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
2910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2911 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2912 {
2913 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2914 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2915 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2916 }
2917
2918 // Write the declaration offsets array
2919 Abbrev = std::make_shared<BitCodeAbbrev>();
2920 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
2921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
2923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2924 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2925 {
2926 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2927 FirstDeclID - NUM_PREDEF_DECL_IDS};
2928 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2929 }
2930 }
2931
WriteFileDeclIDsMap()2932 void ASTWriter::WriteFileDeclIDsMap() {
2933 using namespace llvm;
2934
2935 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs;
2936 SortedFileDeclIDs.reserve(FileDeclIDs.size());
2937 for (const auto &P : FileDeclIDs)
2938 SortedFileDeclIDs.push_back(std::make_pair(P.first, P.second.get()));
2939 llvm::sort(SortedFileDeclIDs, llvm::less_first());
2940
2941 // Join the vectors of DeclIDs from all files.
2942 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2943 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2944 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2945 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2946 for (auto &LocDeclEntry : Info.DeclIDs)
2947 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
2948 }
2949
2950 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2951 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2954 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
2955 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2956 FileGroupedDeclIDs.size()};
2957 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
2958 }
2959
WriteComments()2960 void ASTWriter::WriteComments() {
2961 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
2962 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
2963 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
2964 return;
2965 RecordData Record;
2966 for (const auto &FO : Context->Comments.OrderedComments) {
2967 for (const auto &OC : FO.second) {
2968 const RawComment *I = OC.second;
2969 Record.clear();
2970 AddSourceRange(I->getSourceRange(), Record);
2971 Record.push_back(I->getKind());
2972 Record.push_back(I->isTrailingComment());
2973 Record.push_back(I->isAlmostTrailingComment());
2974 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2975 }
2976 }
2977 }
2978
2979 //===----------------------------------------------------------------------===//
2980 // Global Method Pool and Selector Serialization
2981 //===----------------------------------------------------------------------===//
2982
2983 namespace {
2984
2985 // Trait used for the on-disk hash table used in the method pool.
2986 class ASTMethodPoolTrait {
2987 ASTWriter &Writer;
2988
2989 public:
2990 using key_type = Selector;
2991 using key_type_ref = key_type;
2992
2993 struct data_type {
2994 SelectorID ID;
2995 ObjCMethodList Instance, Factory;
2996 };
2997 using data_type_ref = const data_type &;
2998
2999 using hash_value_type = unsigned;
3000 using offset_type = unsigned;
3001
ASTMethodPoolTrait(ASTWriter & Writer)3002 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
3003
ComputeHash(Selector Sel)3004 static hash_value_type ComputeHash(Selector Sel) {
3005 return serialization::ComputeHash(Sel);
3006 }
3007
3008 std::pair<unsigned, unsigned>
EmitKeyDataLength(raw_ostream & Out,Selector Sel,data_type_ref Methods)3009 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
3010 data_type_ref Methods) {
3011 using namespace llvm::support;
3012
3013 endian::Writer LE(Out, little);
3014 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
3015 LE.write<uint16_t>(KeyLen);
3016 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3017 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3018 Method = Method->getNext())
3019 if (Method->getMethod())
3020 DataLen += 4;
3021 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3022 Method = Method->getNext())
3023 if (Method->getMethod())
3024 DataLen += 4;
3025 LE.write<uint16_t>(DataLen);
3026 return std::make_pair(KeyLen, DataLen);
3027 }
3028
EmitKey(raw_ostream & Out,Selector Sel,unsigned)3029 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
3030 using namespace llvm::support;
3031
3032 endian::Writer LE(Out, little);
3033 uint64_t Start = Out.tell();
3034 assert((Start >> 32) == 0 && "Selector key offset too large");
3035 Writer.SetSelectorOffset(Sel, Start);
3036 unsigned N = Sel.getNumArgs();
3037 LE.write<uint16_t>(N);
3038 if (N == 0)
3039 N = 1;
3040 for (unsigned I = 0; I != N; ++I)
3041 LE.write<uint32_t>(
3042 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
3043 }
3044
EmitData(raw_ostream & Out,key_type_ref,data_type_ref Methods,unsigned DataLen)3045 void EmitData(raw_ostream& Out, key_type_ref,
3046 data_type_ref Methods, unsigned DataLen) {
3047 using namespace llvm::support;
3048
3049 endian::Writer LE(Out, little);
3050 uint64_t Start = Out.tell(); (void)Start;
3051 LE.write<uint32_t>(Methods.ID);
3052 unsigned NumInstanceMethods = 0;
3053 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3054 Method = Method->getNext())
3055 if (Method->getMethod())
3056 ++NumInstanceMethods;
3057
3058 unsigned NumFactoryMethods = 0;
3059 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3060 Method = Method->getNext())
3061 if (Method->getMethod())
3062 ++NumFactoryMethods;
3063
3064 unsigned InstanceBits = Methods.Instance.getBits();
3065 assert(InstanceBits < 4);
3066 unsigned InstanceHasMoreThanOneDeclBit =
3067 Methods.Instance.hasMoreThanOneDecl();
3068 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3069 (InstanceHasMoreThanOneDeclBit << 2) |
3070 InstanceBits;
3071 unsigned FactoryBits = Methods.Factory.getBits();
3072 assert(FactoryBits < 4);
3073 unsigned FactoryHasMoreThanOneDeclBit =
3074 Methods.Factory.hasMoreThanOneDecl();
3075 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3076 (FactoryHasMoreThanOneDeclBit << 2) |
3077 FactoryBits;
3078 LE.write<uint16_t>(FullInstanceBits);
3079 LE.write<uint16_t>(FullFactoryBits);
3080 for (const ObjCMethodList *Method = &Methods.Instance; Method;
3081 Method = Method->getNext())
3082 if (Method->getMethod())
3083 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3084 for (const ObjCMethodList *Method = &Methods.Factory; Method;
3085 Method = Method->getNext())
3086 if (Method->getMethod())
3087 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
3088
3089 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3090 }
3091 };
3092
3093 } // namespace
3094
3095 /// Write ObjC data: selectors and the method pool.
3096 ///
3097 /// The method pool contains both instance and factory methods, stored
3098 /// in an on-disk hash table indexed by the selector. The hash table also
3099 /// contains an empty entry for every other selector known to Sema.
WriteSelectors(Sema & SemaRef)3100 void ASTWriter::WriteSelectors(Sema &SemaRef) {
3101 using namespace llvm;
3102
3103 // Do we have to do anything at all?
3104 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
3105 return;
3106 unsigned NumTableEntries = 0;
3107 // Create and write out the blob that contains selectors and the method pool.
3108 {
3109 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
3110 ASTMethodPoolTrait Trait(*this);
3111
3112 // Create the on-disk hash table representation. We walk through every
3113 // selector we've seen and look it up in the method pool.
3114 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
3115 for (auto &SelectorAndID : SelectorIDs) {
3116 Selector S = SelectorAndID.first;
3117 SelectorID ID = SelectorAndID.second;
3118 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
3119 ASTMethodPoolTrait::data_type Data = {
3120 ID,
3121 ObjCMethodList(),
3122 ObjCMethodList()
3123 };
3124 if (F != SemaRef.MethodPool.end()) {
3125 Data.Instance = F->second.first;
3126 Data.Factory = F->second.second;
3127 }
3128 // Only write this selector if it's not in an existing AST or something
3129 // changed.
3130 if (Chain && ID < FirstSelectorID) {
3131 // Selector already exists. Did it change?
3132 bool changed = false;
3133 for (ObjCMethodList *M = &Data.Instance;
3134 !changed && M && M->getMethod(); M = M->getNext()) {
3135 if (!M->getMethod()->isFromASTFile())
3136 changed = true;
3137 }
3138 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
3139 M = M->getNext()) {
3140 if (!M->getMethod()->isFromASTFile())
3141 changed = true;
3142 }
3143 if (!changed)
3144 continue;
3145 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3146 // A new method pool entry.
3147 ++NumTableEntries;
3148 }
3149 Generator.insert(S, Data, Trait);
3150 }
3151
3152 // Create the on-disk hash table in a buffer.
3153 SmallString<4096> MethodPool;
3154 uint32_t BucketOffset;
3155 {
3156 using namespace llvm::support;
3157
3158 ASTMethodPoolTrait Trait(*this);
3159 llvm::raw_svector_ostream Out(MethodPool);
3160 // Make sure that no bucket is at offset 0
3161 endian::write<uint32_t>(Out, 0, little);
3162 BucketOffset = Generator.Emit(Out, Trait);
3163 }
3164
3165 // Create a blob abbreviation
3166 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3167 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3169 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3171 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3172
3173 // Write the method pool
3174 {
3175 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3176 NumTableEntries};
3177 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3178 }
3179
3180 // Create a blob abbreviation for the selector table offsets.
3181 Abbrev = std::make_shared<BitCodeAbbrev>();
3182 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3186 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3187
3188 // Write the selector offsets table.
3189 {
3190 RecordData::value_type Record[] = {
3191 SELECTOR_OFFSETS, SelectorOffsets.size(),
3192 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3193 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3194 bytes(SelectorOffsets));
3195 }
3196 }
3197 }
3198
3199 /// Write the selectors referenced in @selector expression into AST file.
WriteReferencedSelectorsPool(Sema & SemaRef)3200 void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3201 using namespace llvm;
3202
3203 if (SemaRef.ReferencedSelectors.empty())
3204 return;
3205
3206 RecordData Record;
3207 ASTRecordWriter Writer(*this, Record);
3208
3209 // Note: this writes out all references even for a dependent AST. But it is
3210 // very tricky to fix, and given that @selector shouldn't really appear in
3211 // headers, probably not worth it. It's not a correctness issue.
3212 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3213 Selector Sel = SelectorAndLocation.first;
3214 SourceLocation Loc = SelectorAndLocation.second;
3215 Writer.AddSelectorRef(Sel);
3216 Writer.AddSourceLocation(Loc);
3217 }
3218 Writer.Emit(REFERENCED_SELECTOR_POOL);
3219 }
3220
3221 //===----------------------------------------------------------------------===//
3222 // Identifier Table Serialization
3223 //===----------------------------------------------------------------------===//
3224
3225 /// Determine the declaration that should be put into the name lookup table to
3226 /// represent the given declaration in this module. This is usually D itself,
3227 /// but if D was imported and merged into a local declaration, we want the most
3228 /// recent local declaration instead. The chosen declaration will be the most
3229 /// recent declaration in any module that imports this one.
getDeclForLocalLookup(const LangOptions & LangOpts,NamedDecl * D)3230 static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3231 NamedDecl *D) {
3232 if (!LangOpts.Modules || !D->isFromASTFile())
3233 return D;
3234
3235 if (Decl *Redecl = D->getPreviousDecl()) {
3236 // For Redeclarable decls, a prior declaration might be local.
3237 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3238 // If we find a local decl, we're done.
3239 if (!Redecl->isFromASTFile()) {
3240 // Exception: in very rare cases (for injected-class-names), not all
3241 // redeclarations are in the same semantic context. Skip ones in a
3242 // different context. They don't go in this lookup table at all.
3243 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3244 D->getDeclContext()->getRedeclContext()))
3245 continue;
3246 return cast<NamedDecl>(Redecl);
3247 }
3248
3249 // If we find a decl from a (chained-)PCH stop since we won't find a
3250 // local one.
3251 if (Redecl->getOwningModuleID() == 0)
3252 break;
3253 }
3254 } else if (Decl *First = D->getCanonicalDecl()) {
3255 // For Mergeable decls, the first decl might be local.
3256 if (!First->isFromASTFile())
3257 return cast<NamedDecl>(First);
3258 }
3259
3260 // All declarations are imported. Our most recent declaration will also be
3261 // the most recent one in anyone who imports us.
3262 return D;
3263 }
3264
3265 namespace {
3266
3267 class ASTIdentifierTableTrait {
3268 ASTWriter &Writer;
3269 Preprocessor &PP;
3270 IdentifierResolver &IdResolver;
3271 bool IsModule;
3272 bool NeedDecls;
3273 ASTWriter::RecordData *InterestingIdentifierOffsets;
3274
3275 /// Determines whether this is an "interesting" identifier that needs a
3276 /// full IdentifierInfo structure written into the hash table. Notably, this
3277 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3278 /// to check that.
isInterestingIdentifier(const IdentifierInfo * II,uint64_t MacroOffset)3279 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3280 if (MacroOffset || II->isPoisoned() ||
3281 (!IsModule && II->getObjCOrBuiltinID()) ||
3282 II->hasRevertedTokenIDToIdentifier() ||
3283 (NeedDecls && II->getFETokenInfo()))
3284 return true;
3285
3286 return false;
3287 }
3288
3289 public:
3290 using key_type = IdentifierInfo *;
3291 using key_type_ref = key_type;
3292
3293 using data_type = IdentID;
3294 using data_type_ref = data_type;
3295
3296 using hash_value_type = unsigned;
3297 using offset_type = unsigned;
3298
ASTIdentifierTableTrait(ASTWriter & Writer,Preprocessor & PP,IdentifierResolver & IdResolver,bool IsModule,ASTWriter::RecordData * InterestingIdentifierOffsets)3299 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3300 IdentifierResolver &IdResolver, bool IsModule,
3301 ASTWriter::RecordData *InterestingIdentifierOffsets)
3302 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3303 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3304 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3305
needDecls() const3306 bool needDecls() const { return NeedDecls; }
3307
ComputeHash(const IdentifierInfo * II)3308 static hash_value_type ComputeHash(const IdentifierInfo* II) {
3309 return llvm::djbHash(II->getName());
3310 }
3311
isInterestingIdentifier(const IdentifierInfo * II)3312 bool isInterestingIdentifier(const IdentifierInfo *II) {
3313 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3314 return isInterestingIdentifier(II, MacroOffset);
3315 }
3316
isInterestingNonMacroIdentifier(const IdentifierInfo * II)3317 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3318 return isInterestingIdentifier(II, 0);
3319 }
3320
3321 std::pair<unsigned, unsigned>
EmitKeyDataLength(raw_ostream & Out,IdentifierInfo * II,IdentID ID)3322 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3323 unsigned KeyLen = II->getLength() + 1;
3324 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3325 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3326 if (isInterestingIdentifier(II, MacroOffset)) {
3327 DataLen += 2; // 2 bytes for builtin ID
3328 DataLen += 2; // 2 bytes for flags
3329 if (MacroOffset)
3330 DataLen += 4; // MacroDirectives offset.
3331
3332 if (NeedDecls) {
3333 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3334 DEnd = IdResolver.end();
3335 D != DEnd; ++D)
3336 DataLen += 4;
3337 }
3338 }
3339
3340 using namespace llvm::support;
3341
3342 endian::Writer LE(Out, little);
3343
3344 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
3345 LE.write<uint16_t>(DataLen);
3346 // We emit the key length after the data length so that every
3347 // string is preceded by a 16-bit length. This matches the PTH
3348 // format for storing identifiers.
3349 LE.write<uint16_t>(KeyLen);
3350 return std::make_pair(KeyLen, DataLen);
3351 }
3352
EmitKey(raw_ostream & Out,const IdentifierInfo * II,unsigned KeyLen)3353 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3354 unsigned KeyLen) {
3355 // Record the location of the key data. This is used when generating
3356 // the mapping from persistent IDs to strings.
3357 Writer.SetIdentifierOffset(II, Out.tell());
3358
3359 // Emit the offset of the key/data length information to the interesting
3360 // identifiers table if necessary.
3361 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3362 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3363
3364 Out.write(II->getNameStart(), KeyLen);
3365 }
3366
EmitData(raw_ostream & Out,IdentifierInfo * II,IdentID ID,unsigned)3367 void EmitData(raw_ostream& Out, IdentifierInfo* II,
3368 IdentID ID, unsigned) {
3369 using namespace llvm::support;
3370
3371 endian::Writer LE(Out, little);
3372
3373 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3374 if (!isInterestingIdentifier(II, MacroOffset)) {
3375 LE.write<uint32_t>(ID << 1);
3376 return;
3377 }
3378
3379 LE.write<uint32_t>((ID << 1) | 0x01);
3380 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3381 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3382 LE.write<uint16_t>(Bits);
3383 Bits = 0;
3384 bool HadMacroDefinition = MacroOffset != 0;
3385 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3386 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3387 Bits = (Bits << 1) | unsigned(II->isPoisoned());
3388 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3389 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3390 LE.write<uint16_t>(Bits);
3391
3392 if (HadMacroDefinition)
3393 LE.write<uint32_t>(MacroOffset);
3394
3395 if (NeedDecls) {
3396 // Emit the declaration IDs in reverse order, because the
3397 // IdentifierResolver provides the declarations as they would be
3398 // visible (e.g., the function "stat" would come before the struct
3399 // "stat"), but the ASTReader adds declarations to the end of the list
3400 // (so we need to see the struct "stat" before the function "stat").
3401 // Only emit declarations that aren't from a chained PCH, though.
3402 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3403 IdResolver.end());
3404 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3405 DEnd = Decls.rend();
3406 D != DEnd; ++D)
3407 LE.write<uint32_t>(
3408 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3409 }
3410 }
3411 };
3412
3413 } // namespace
3414
3415 /// Write the identifier table into the AST file.
3416 ///
3417 /// The identifier table consists of a blob containing string data
3418 /// (the actual identifiers themselves) and a separate "offsets" index
3419 /// that maps identifier IDs to locations within the blob.
WriteIdentifierTable(Preprocessor & PP,IdentifierResolver & IdResolver,bool IsModule)3420 void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3421 IdentifierResolver &IdResolver,
3422 bool IsModule) {
3423 using namespace llvm;
3424
3425 RecordData InterestingIdents;
3426
3427 // Create and write out the blob that contains the identifier
3428 // strings.
3429 {
3430 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3431 ASTIdentifierTableTrait Trait(
3432 *this, PP, IdResolver, IsModule,
3433 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
3434
3435 // Look for any identifiers that were named while processing the
3436 // headers, but are otherwise not needed. We add these to the hash
3437 // table to enable checking of the predefines buffer in the case
3438 // where the user adds new macro definitions when building the AST
3439 // file.
3440 SmallVector<const IdentifierInfo *, 128> IIs;
3441 for (const auto &ID : PP.getIdentifierTable())
3442 IIs.push_back(ID.second);
3443 // Sort the identifiers lexicographically before getting them references so
3444 // that their order is stable.
3445 llvm::sort(IIs, llvm::deref<std::less<>>());
3446 for (const IdentifierInfo *II : IIs)
3447 if (Trait.isInterestingNonMacroIdentifier(II))
3448 getIdentifierRef(II);
3449
3450 // Create the on-disk hash table representation. We only store offsets
3451 // for identifiers that appear here for the first time.
3452 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3453 for (auto IdentIDPair : IdentifierIDs) {
3454 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3455 IdentID ID = IdentIDPair.second;
3456 assert(II && "NULL identifier in identifier table");
3457 // Write out identifiers if either the ID is local or the identifier has
3458 // changed since it was loaded.
3459 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3460 || II->hasChangedSinceDeserialization() ||
3461 (Trait.needDecls() &&
3462 II->hasFETokenInfoChangedSinceDeserialization()))
3463 Generator.insert(II, ID, Trait);
3464 }
3465
3466 // Create the on-disk hash table in a buffer.
3467 SmallString<4096> IdentifierTable;
3468 uint32_t BucketOffset;
3469 {
3470 using namespace llvm::support;
3471
3472 llvm::raw_svector_ostream Out(IdentifierTable);
3473 // Make sure that no bucket is at offset 0
3474 endian::write<uint32_t>(Out, 0, little);
3475 BucketOffset = Generator.Emit(Out, Trait);
3476 }
3477
3478 // Create a blob abbreviation
3479 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3480 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3481 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3482 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3483 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3484
3485 // Write the identifier table
3486 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3487 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3488 }
3489
3490 // Write the offsets table for identifier IDs.
3491 auto Abbrev = std::make_shared<BitCodeAbbrev>();
3492 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3493 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3496 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
3497
3498 #ifndef NDEBUG
3499 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3500 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3501 #endif
3502
3503 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3504 IdentifierOffsets.size(),
3505 FirstIdentID - NUM_PREDEF_IDENT_IDS};
3506 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3507 bytes(IdentifierOffsets));
3508
3509 // In C++, write the list of interesting identifiers (those that are
3510 // defined as macros, poisoned, or similar unusual things).
3511 if (!InterestingIdents.empty())
3512 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3513 }
3514
3515 //===----------------------------------------------------------------------===//
3516 // DeclContext's Name Lookup Table Serialization
3517 //===----------------------------------------------------------------------===//
3518
3519 namespace {
3520
3521 // Trait used for the on-disk hash table used in the method pool.
3522 class ASTDeclContextNameLookupTrait {
3523 ASTWriter &Writer;
3524 llvm::SmallVector<DeclID, 64> DeclIDs;
3525
3526 public:
3527 using key_type = DeclarationNameKey;
3528 using key_type_ref = key_type;
3529
3530 /// A start and end index into DeclIDs, representing a sequence of decls.
3531 using data_type = std::pair<unsigned, unsigned>;
3532 using data_type_ref = const data_type &;
3533
3534 using hash_value_type = unsigned;
3535 using offset_type = unsigned;
3536
ASTDeclContextNameLookupTrait(ASTWriter & Writer)3537 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
3538
3539 template<typename Coll>
getData(const Coll & Decls)3540 data_type getData(const Coll &Decls) {
3541 unsigned Start = DeclIDs.size();
3542 for (NamedDecl *D : Decls) {
3543 DeclIDs.push_back(
3544 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3545 }
3546 return std::make_pair(Start, DeclIDs.size());
3547 }
3548
ImportData(const reader::ASTDeclContextNameLookupTrait::data_type & FromReader)3549 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3550 unsigned Start = DeclIDs.size();
3551 for (auto ID : FromReader)
3552 DeclIDs.push_back(ID);
3553 return std::make_pair(Start, DeclIDs.size());
3554 }
3555
EqualKey(key_type_ref a,key_type_ref b)3556 static bool EqualKey(key_type_ref a, key_type_ref b) {
3557 return a == b;
3558 }
3559
ComputeHash(DeclarationNameKey Name)3560 hash_value_type ComputeHash(DeclarationNameKey Name) {
3561 return Name.getHash();
3562 }
3563
EmitFileRef(raw_ostream & Out,ModuleFile * F) const3564 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3565 assert(Writer.hasChain() &&
3566 "have reference to loaded module file but no chain?");
3567
3568 using namespace llvm::support;
3569
3570 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
3571 }
3572
EmitKeyDataLength(raw_ostream & Out,DeclarationNameKey Name,data_type_ref Lookup)3573 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3574 DeclarationNameKey Name,
3575 data_type_ref Lookup) {
3576 using namespace llvm::support;
3577
3578 endian::Writer LE(Out, little);
3579 unsigned KeyLen = 1;
3580 switch (Name.getKind()) {
3581 case DeclarationName::Identifier:
3582 case DeclarationName::ObjCZeroArgSelector:
3583 case DeclarationName::ObjCOneArgSelector:
3584 case DeclarationName::ObjCMultiArgSelector:
3585 case DeclarationName::CXXLiteralOperatorName:
3586 case DeclarationName::CXXDeductionGuideName:
3587 KeyLen += 4;
3588 break;
3589 case DeclarationName::CXXOperatorName:
3590 KeyLen += 1;
3591 break;
3592 case DeclarationName::CXXConstructorName:
3593 case DeclarationName::CXXDestructorName:
3594 case DeclarationName::CXXConversionFunctionName:
3595 case DeclarationName::CXXUsingDirective:
3596 break;
3597 }
3598 LE.write<uint16_t>(KeyLen);
3599
3600 // 4 bytes for each DeclID.
3601 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3602 assert(uint16_t(DataLen) == DataLen &&
3603 "too many decls for serialized lookup result");
3604 LE.write<uint16_t>(DataLen);
3605
3606 return std::make_pair(KeyLen, DataLen);
3607 }
3608
EmitKey(raw_ostream & Out,DeclarationNameKey Name,unsigned)3609 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3610 using namespace llvm::support;
3611
3612 endian::Writer LE(Out, little);
3613 LE.write<uint8_t>(Name.getKind());
3614 switch (Name.getKind()) {
3615 case DeclarationName::Identifier:
3616 case DeclarationName::CXXLiteralOperatorName:
3617 case DeclarationName::CXXDeductionGuideName:
3618 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3619 return;
3620 case DeclarationName::ObjCZeroArgSelector:
3621 case DeclarationName::ObjCOneArgSelector:
3622 case DeclarationName::ObjCMultiArgSelector:
3623 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3624 return;
3625 case DeclarationName::CXXOperatorName:
3626 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3627 "Invalid operator?");
3628 LE.write<uint8_t>(Name.getOperatorKind());
3629 return;
3630 case DeclarationName::CXXConstructorName:
3631 case DeclarationName::CXXDestructorName:
3632 case DeclarationName::CXXConversionFunctionName:
3633 case DeclarationName::CXXUsingDirective:
3634 return;
3635 }
3636
3637 llvm_unreachable("Invalid name kind?");
3638 }
3639
EmitData(raw_ostream & Out,key_type_ref,data_type Lookup,unsigned DataLen)3640 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3641 unsigned DataLen) {
3642 using namespace llvm::support;
3643
3644 endian::Writer LE(Out, little);
3645 uint64_t Start = Out.tell(); (void)Start;
3646 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3647 LE.write<uint32_t>(DeclIDs[I]);
3648 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3649 }
3650 };
3651
3652 } // namespace
3653
isLookupResultExternal(StoredDeclsList & Result,DeclContext * DC)3654 bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3655 DeclContext *DC) {
3656 return Result.hasExternalDecls() &&
3657 DC->hasNeedToReconcileExternalVisibleStorage();
3658 }
3659
isLookupResultEntirelyExternal(StoredDeclsList & Result,DeclContext * DC)3660 bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3661 DeclContext *DC) {
3662 for (auto *D : Result.getLookupResult())
3663 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3664 return false;
3665
3666 return true;
3667 }
3668
3669 void
GenerateNameLookupTable(const DeclContext * ConstDC,llvm::SmallVectorImpl<char> & LookupTable)3670 ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3671 llvm::SmallVectorImpl<char> &LookupTable) {
3672 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3673 !ConstDC->hasLazyExternalLexicalLookups() &&
3674 "must call buildLookups first");
3675
3676 // FIXME: We need to build the lookups table, which is logically const.
3677 auto *DC = const_cast<DeclContext*>(ConstDC);
3678 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3679
3680 // Create the on-disk hash table representation.
3681 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3682 ASTDeclContextNameLookupTrait> Generator;
3683 ASTDeclContextNameLookupTrait Trait(*this);
3684
3685 // The first step is to collect the declaration names which we need to
3686 // serialize into the name lookup table, and to collect them in a stable
3687 // order.
3688 SmallVector<DeclarationName, 16> Names;
3689
3690 // We also build up small sets of the constructor and conversion function
3691 // names which are visible.
3692 llvm::SmallPtrSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
3693
3694 for (auto &Lookup : *DC->buildLookup()) {
3695 auto &Name = Lookup.first;
3696 auto &Result = Lookup.second;
3697
3698 // If there are no local declarations in our lookup result, we
3699 // don't need to write an entry for the name at all. If we can't
3700 // write out a lookup set without performing more deserialization,
3701 // just skip this entry.
3702 if (isLookupResultExternal(Result, DC) &&
3703 isLookupResultEntirelyExternal(Result, DC))
3704 continue;
3705
3706 // We also skip empty results. If any of the results could be external and
3707 // the currently available results are empty, then all of the results are
3708 // external and we skip it above. So the only way we get here with an empty
3709 // results is when no results could have been external *and* we have
3710 // external results.
3711 //
3712 // FIXME: While we might want to start emitting on-disk entries for negative
3713 // lookups into a decl context as an optimization, today we *have* to skip
3714 // them because there are names with empty lookup results in decl contexts
3715 // which we can't emit in any stable ordering: we lookup constructors and
3716 // conversion functions in the enclosing namespace scope creating empty
3717 // results for them. This in almost certainly a bug in Clang's name lookup,
3718 // but that is likely to be hard or impossible to fix and so we tolerate it
3719 // here by omitting lookups with empty results.
3720 if (Lookup.second.getLookupResult().empty())
3721 continue;
3722
3723 switch (Lookup.first.getNameKind()) {
3724 default:
3725 Names.push_back(Lookup.first);
3726 break;
3727
3728 case DeclarationName::CXXConstructorName:
3729 assert(isa<CXXRecordDecl>(DC) &&
3730 "Cannot have a constructor name outside of a class!");
3731 ConstructorNameSet.insert(Name);
3732 break;
3733
3734 case DeclarationName::CXXConversionFunctionName:
3735 assert(isa<CXXRecordDecl>(DC) &&
3736 "Cannot have a conversion function name outside of a class!");
3737 ConversionNameSet.insert(Name);
3738 break;
3739 }
3740 }
3741
3742 // Sort the names into a stable order.
3743 llvm::sort(Names);
3744
3745 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
3746 // We need to establish an ordering of constructor and conversion function
3747 // names, and they don't have an intrinsic ordering.
3748
3749 // First we try the easy case by forming the current context's constructor
3750 // name and adding that name first. This is a very useful optimization to
3751 // avoid walking the lexical declarations in many cases, and it also
3752 // handles the only case where a constructor name can come from some other
3753 // lexical context -- when that name is an implicit constructor merged from
3754 // another declaration in the redecl chain. Any non-implicit constructor or
3755 // conversion function which doesn't occur in all the lexical contexts
3756 // would be an ODR violation.
3757 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3758 Context->getCanonicalType(Context->getRecordType(D)));
3759 if (ConstructorNameSet.erase(ImplicitCtorName))
3760 Names.push_back(ImplicitCtorName);
3761
3762 // If we still have constructors or conversion functions, we walk all the
3763 // names in the decl and add the constructors and conversion functions
3764 // which are visible in the order they lexically occur within the context.
3765 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3766 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3767 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3768 auto Name = ChildND->getDeclName();
3769 switch (Name.getNameKind()) {
3770 default:
3771 continue;
3772
3773 case DeclarationName::CXXConstructorName:
3774 if (ConstructorNameSet.erase(Name))
3775 Names.push_back(Name);
3776 break;
3777
3778 case DeclarationName::CXXConversionFunctionName:
3779 if (ConversionNameSet.erase(Name))
3780 Names.push_back(Name);
3781 break;
3782 }
3783
3784 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3785 break;
3786 }
3787
3788 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3789 "constructors by walking all the "
3790 "lexical members of the context.");
3791 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3792 "conversion functions by walking all "
3793 "the lexical members of the context.");
3794 }
3795
3796 // Next we need to do a lookup with each name into this decl context to fully
3797 // populate any results from external sources. We don't actually use the
3798 // results of these lookups because we only want to use the results after all
3799 // results have been loaded and the pointers into them will be stable.
3800 for (auto &Name : Names)
3801 DC->lookup(Name);
3802
3803 // Now we need to insert the results for each name into the hash table. For
3804 // constructor names and conversion function names, we actually need to merge
3805 // all of the results for them into one list of results each and insert
3806 // those.
3807 SmallVector<NamedDecl *, 8> ConstructorDecls;
3808 SmallVector<NamedDecl *, 8> ConversionDecls;
3809
3810 // Now loop over the names, either inserting them or appending for the two
3811 // special cases.
3812 for (auto &Name : Names) {
3813 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3814
3815 switch (Name.getNameKind()) {
3816 default:
3817 Generator.insert(Name, Trait.getData(Result), Trait);
3818 break;
3819
3820 case DeclarationName::CXXConstructorName:
3821 ConstructorDecls.append(Result.begin(), Result.end());
3822 break;
3823
3824 case DeclarationName::CXXConversionFunctionName:
3825 ConversionDecls.append(Result.begin(), Result.end());
3826 break;
3827 }
3828 }
3829
3830 // Handle our two special cases if we ended up having any. We arbitrarily use
3831 // the first declaration's name here because the name itself isn't part of
3832 // the key, only the kind of name is used.
3833 if (!ConstructorDecls.empty())
3834 Generator.insert(ConstructorDecls.front()->getDeclName(),
3835 Trait.getData(ConstructorDecls), Trait);
3836 if (!ConversionDecls.empty())
3837 Generator.insert(ConversionDecls.front()->getDeclName(),
3838 Trait.getData(ConversionDecls), Trait);
3839
3840 // Create the on-disk hash table. Also emit the existing imported and
3841 // merged table if there is one.
3842 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3843 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
3844 }
3845
3846 /// Write the block containing all of the declaration IDs
3847 /// visible from the given DeclContext.
3848 ///
3849 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
3850 /// bitstream, or 0 if no block was written.
WriteDeclContextVisibleBlock(ASTContext & Context,DeclContext * DC)3851 uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3852 DeclContext *DC) {
3853 // If we imported a key declaration of this namespace, write the visible
3854 // lookup results as an update record for it rather than including them
3855 // on this declaration. We will only look at key declarations on reload.
3856 if (isa<NamespaceDecl>(DC) && Chain &&
3857 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3858 // Only do this once, for the first local declaration of the namespace.
3859 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
3860 Prev = Prev->getPreviousDecl())
3861 if (!Prev->isFromASTFile())
3862 return 0;
3863
3864 // Note that we need to emit an update record for the primary context.
3865 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3866
3867 // Make sure all visible decls are written. They will be recorded later. We
3868 // do this using a side data structure so we can sort the names into
3869 // a deterministic order.
3870 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3871 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3872 LookupResults;
3873 if (Map) {
3874 LookupResults.reserve(Map->size());
3875 for (auto &Entry : *Map)
3876 LookupResults.push_back(
3877 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3878 }
3879
3880 llvm::sort(LookupResults, llvm::less_first());
3881 for (auto &NameAndResult : LookupResults) {
3882 DeclarationName Name = NameAndResult.first;
3883 DeclContext::lookup_result Result = NameAndResult.second;
3884 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3885 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3886 // We have to work around a name lookup bug here where negative lookup
3887 // results for these names get cached in namespace lookup tables (these
3888 // names should never be looked up in a namespace).
3889 assert(Result.empty() && "Cannot have a constructor or conversion "
3890 "function name in a namespace!");
3891 continue;
3892 }
3893
3894 for (NamedDecl *ND : Result)
3895 if (!ND->isFromASTFile())
3896 GetDeclRef(ND);
3897 }
3898
3899 return 0;
3900 }
3901
3902 if (DC->getPrimaryContext() != DC)
3903 return 0;
3904
3905 // Skip contexts which don't support name lookup.
3906 if (!DC->isLookupContext())
3907 return 0;
3908
3909 // If not in C++, we perform name lookup for the translation unit via the
3910 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3911 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
3912 return 0;
3913
3914 // Serialize the contents of the mapping used for lookup. Note that,
3915 // although we have two very different code paths, the serialized
3916 // representation is the same for both cases: a declaration name,
3917 // followed by a size, followed by references to the visible
3918 // declarations that have that name.
3919 uint64_t Offset = Stream.GetCurrentBitNo();
3920 StoredDeclsMap *Map = DC->buildLookup();
3921 if (!Map || Map->empty())
3922 return 0;
3923
3924 // Create the on-disk hash table in a buffer.
3925 SmallString<4096> LookupTable;
3926 GenerateNameLookupTable(DC, LookupTable);
3927
3928 // Write the lookup table
3929 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
3930 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3931 LookupTable);
3932 ++NumVisibleDeclContexts;
3933 return Offset;
3934 }
3935
3936 /// Write an UPDATE_VISIBLE block for the given context.
3937 ///
3938 /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3939 /// DeclContext in a dependent AST file. As such, they only exist for the TU
3940 /// (in C++), for namespaces, and for classes with forward-declared unscoped
3941 /// enumeration members (in C++11).
WriteDeclContextVisibleUpdate(const DeclContext * DC)3942 void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
3943 StoredDeclsMap *Map = DC->getLookupPtr();
3944 if (!Map || Map->empty())
3945 return;
3946
3947 // Create the on-disk hash table in a buffer.
3948 SmallString<4096> LookupTable;
3949 GenerateNameLookupTable(DC, LookupTable);
3950
3951 // If we're updating a namespace, select a key declaration as the key for the
3952 // update record; those are the only ones that will be checked on reload.
3953 if (isa<NamespaceDecl>(DC))
3954 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3955
3956 // Write the lookup table
3957 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
3958 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
3959 }
3960
3961 /// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
WriteFPPragmaOptions(const FPOptionsOverride & Opts)3962 void ASTWriter::WriteFPPragmaOptions(const FPOptionsOverride &Opts) {
3963 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
3964 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3965 }
3966
3967 /// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
WriteOpenCLExtensions(Sema & SemaRef)3968 void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
3969 if (!SemaRef.Context.getLangOpts().OpenCL)
3970 return;
3971
3972 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3973 RecordData Record;
3974 for (const auto &I:Opts.OptMap) {
3975 AddString(I.getKey(), Record);
3976 auto V = I.getValue();
3977 Record.push_back(V.Supported ? 1 : 0);
3978 Record.push_back(V.Enabled ? 1 : 0);
3979 Record.push_back(V.Avail);
3980 Record.push_back(V.Core);
3981 }
3982 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3983 }
3984
WriteOpenCLExtensionTypes(Sema & SemaRef)3985 void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
3986 if (!SemaRef.Context.getLangOpts().OpenCL)
3987 return;
3988
3989 // Sort the elements of the map OpenCLTypeExtMap by TypeIDs,
3990 // without copying them.
3991 const llvm::DenseMap<const Type *, std::set<std::string>> &OpenCLTypeExtMap =
3992 SemaRef.OpenCLTypeExtMap;
3993 using ElementTy = std::pair<TypeID, const std::set<std::string> *>;
3994 llvm::SmallVector<ElementTy, 8> StableOpenCLTypeExtMap;
3995 StableOpenCLTypeExtMap.reserve(OpenCLTypeExtMap.size());
3996
3997 for (const auto &I : OpenCLTypeExtMap)
3998 StableOpenCLTypeExtMap.emplace_back(
3999 getTypeID(I.first->getCanonicalTypeInternal()), &I.second);
4000
4001 auto CompareByTypeID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
4002 return E1.first < E2.first;
4003 };
4004 llvm::sort(StableOpenCLTypeExtMap, CompareByTypeID);
4005
4006 RecordData Record;
4007 for (const ElementTy &E : StableOpenCLTypeExtMap) {
4008 Record.push_back(E.first); // TypeID
4009 const std::set<std::string> *ExtSet = E.second;
4010 Record.push_back(static_cast<unsigned>(ExtSet->size()));
4011 for (const std::string &Ext : *ExtSet)
4012 AddString(Ext, Record);
4013 }
4014
4015 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4016 }
4017
WriteOpenCLExtensionDecls(Sema & SemaRef)4018 void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4019 if (!SemaRef.Context.getLangOpts().OpenCL)
4020 return;
4021
4022 // Sort the elements of the map OpenCLDeclExtMap by DeclIDs,
4023 // without copying them.
4024 const llvm::DenseMap<const Decl *, std::set<std::string>> &OpenCLDeclExtMap =
4025 SemaRef.OpenCLDeclExtMap;
4026 using ElementTy = std::pair<DeclID, const std::set<std::string> *>;
4027 llvm::SmallVector<ElementTy, 8> StableOpenCLDeclExtMap;
4028 StableOpenCLDeclExtMap.reserve(OpenCLDeclExtMap.size());
4029
4030 for (const auto &I : OpenCLDeclExtMap)
4031 StableOpenCLDeclExtMap.emplace_back(getDeclID(I.first), &I.second);
4032
4033 auto CompareByDeclID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
4034 return E1.first < E2.first;
4035 };
4036 llvm::sort(StableOpenCLDeclExtMap, CompareByDeclID);
4037
4038 RecordData Record;
4039 for (const ElementTy &E : StableOpenCLDeclExtMap) {
4040 Record.push_back(E.first); // DeclID
4041 const std::set<std::string> *ExtSet = E.second;
4042 Record.push_back(static_cast<unsigned>(ExtSet->size()));
4043 for (const std::string &Ext : *ExtSet)
4044 AddString(Ext, Record);
4045 }
4046
4047 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4048 }
4049
WriteCUDAPragmas(Sema & SemaRef)4050 void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4051 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4052 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4053 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4054 }
4055 }
4056
WriteObjCCategories()4057 void ASTWriter::WriteObjCCategories() {
4058 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
4059 RecordData Categories;
4060
4061 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4062 unsigned Size = 0;
4063 unsigned StartIndex = Categories.size();
4064
4065 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4066
4067 // Allocate space for the size.
4068 Categories.push_back(0);
4069
4070 // Add the categories.
4071 for (ObjCInterfaceDecl::known_categories_iterator
4072 Cat = Class->known_categories_begin(),
4073 CatEnd = Class->known_categories_end();
4074 Cat != CatEnd; ++Cat, ++Size) {
4075 assert(getDeclID(*Cat) != 0 && "Bogus category");
4076 AddDeclRef(*Cat, Categories);
4077 }
4078
4079 // Update the size.
4080 Categories[StartIndex] = Size;
4081
4082 // Record this interface -> category map.
4083 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4084 CategoriesMap.push_back(CatInfo);
4085 }
4086
4087 // Sort the categories map by the definition ID, since the reader will be
4088 // performing binary searches on this information.
4089 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4090
4091 // Emit the categories map.
4092 using namespace llvm;
4093
4094 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4095 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4098 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
4099
4100 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4101 Stream.EmitRecordWithBlob(AbbrevID, Record,
4102 reinterpret_cast<char *>(CategoriesMap.data()),
4103 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4104
4105 // Emit the category lists.
4106 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4107 }
4108
WriteLateParsedTemplates(Sema & SemaRef)4109 void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4110 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4111
4112 if (LPTMap.empty())
4113 return;
4114
4115 RecordData Record;
4116 for (auto &LPTMapEntry : LPTMap) {
4117 const FunctionDecl *FD = LPTMapEntry.first;
4118 LateParsedTemplate &LPT = *LPTMapEntry.second;
4119 AddDeclRef(FD, Record);
4120 AddDeclRef(LPT.D, Record);
4121 Record.push_back(LPT.Toks.size());
4122
4123 for (const auto &Tok : LPT.Toks) {
4124 AddToken(Tok, Record);
4125 }
4126 }
4127 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4128 }
4129
4130 /// Write the state of 'pragma clang optimize' at the end of the module.
WriteOptimizePragmaOptions(Sema & SemaRef)4131 void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4132 RecordData Record;
4133 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4134 AddSourceLocation(PragmaLoc, Record);
4135 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4136 }
4137
4138 /// Write the state of 'pragma ms_struct' at the end of the module.
WriteMSStructPragmaOptions(Sema & SemaRef)4139 void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4140 RecordData Record;
4141 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4142 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4143 }
4144
4145 /// Write the state of 'pragma pointers_to_members' at the end of the
4146 //module.
WriteMSPointersToMembersPragmaOptions(Sema & SemaRef)4147 void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4148 RecordData Record;
4149 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4150 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4151 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4152 }
4153
4154 /// Write the state of 'pragma pack' at the end of the module.
WritePackPragmaOptions(Sema & SemaRef)4155 void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4156 // Don't serialize pragma pack state for modules, since it should only take
4157 // effect on a per-submodule basis.
4158 if (WritingModule)
4159 return;
4160
4161 RecordData Record;
4162 Record.push_back(SemaRef.PackStack.CurrentValue);
4163 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4164 Record.push_back(SemaRef.PackStack.Stack.size());
4165 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4166 Record.push_back(StackEntry.Value);
4167 AddSourceLocation(StackEntry.PragmaLocation, Record);
4168 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4169 AddString(StackEntry.StackSlotLabel, Record);
4170 }
4171 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4172 }
4173
4174 /// Write the state of 'pragma float_control' at the end of the module.
WriteFloatControlPragmaOptions(Sema & SemaRef)4175 void ASTWriter::WriteFloatControlPragmaOptions(Sema &SemaRef) {
4176 // Don't serialize pragma float_control state for modules,
4177 // since it should only take effect on a per-submodule basis.
4178 if (WritingModule)
4179 return;
4180
4181 RecordData Record;
4182 Record.push_back(SemaRef.FpPragmaStack.CurrentValue.getAsOpaqueInt());
4183 AddSourceLocation(SemaRef.FpPragmaStack.CurrentPragmaLocation, Record);
4184 Record.push_back(SemaRef.FpPragmaStack.Stack.size());
4185 for (const auto &StackEntry : SemaRef.FpPragmaStack.Stack) {
4186 Record.push_back(StackEntry.Value.getAsOpaqueInt());
4187 AddSourceLocation(StackEntry.PragmaLocation, Record);
4188 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
4189 AddString(StackEntry.StackSlotLabel, Record);
4190 }
4191 Stream.EmitRecord(FLOAT_CONTROL_PRAGMA_OPTIONS, Record);
4192 }
4193
WriteModuleFileExtension(Sema & SemaRef,ModuleFileExtensionWriter & Writer)4194 void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4195 ModuleFileExtensionWriter &Writer) {
4196 // Enter the extension block.
4197 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4198
4199 // Emit the metadata record abbreviation.
4200 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
4201 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4202 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4203 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4204 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4205 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4206 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4207 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
4208
4209 // Emit the metadata record.
4210 RecordData Record;
4211 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4212 Record.push_back(EXTENSION_METADATA);
4213 Record.push_back(Metadata.MajorVersion);
4214 Record.push_back(Metadata.MinorVersion);
4215 Record.push_back(Metadata.BlockName.size());
4216 Record.push_back(Metadata.UserInfo.size());
4217 SmallString<64> Buffer;
4218 Buffer += Metadata.BlockName;
4219 Buffer += Metadata.UserInfo;
4220 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4221
4222 // Emit the contents of the extension block.
4223 Writer.writeExtensionContents(SemaRef, Stream);
4224
4225 // Exit the extension block.
4226 Stream.ExitBlock();
4227 }
4228
4229 //===----------------------------------------------------------------------===//
4230 // General Serialization Routines
4231 //===----------------------------------------------------------------------===//
4232
AddAttr(const Attr * A)4233 void ASTRecordWriter::AddAttr(const Attr *A) {
4234 auto &Record = *this;
4235 if (!A)
4236 return Record.push_back(0);
4237 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4238
4239 Record.AddIdentifierRef(A->getAttrName());
4240 Record.AddIdentifierRef(A->getScopeName());
4241 Record.AddSourceRange(A->getRange());
4242 Record.AddSourceLocation(A->getScopeLoc());
4243 Record.push_back(A->getParsedKind());
4244 Record.push_back(A->getSyntax());
4245 Record.push_back(A->getAttributeSpellingListIndexRaw());
4246
4247 #include "clang/Serialization/AttrPCHWrite.inc"
4248 }
4249
4250 /// Emit the list of attributes to the specified record.
AddAttributes(ArrayRef<const Attr * > Attrs)4251 void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4252 push_back(Attrs.size());
4253 for (const auto *A : Attrs)
4254 AddAttr(A);
4255 }
4256
AddToken(const Token & Tok,RecordDataImpl & Record)4257 void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4258 AddSourceLocation(Tok.getLocation(), Record);
4259 Record.push_back(Tok.getLength());
4260
4261 // FIXME: When reading literal tokens, reconstruct the literal pointer
4262 // if it is needed.
4263 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4264 // FIXME: Should translate token kind to a stable encoding.
4265 Record.push_back(Tok.getKind());
4266 // FIXME: Should translate token flags to a stable encoding.
4267 Record.push_back(Tok.getFlags());
4268 }
4269
AddString(StringRef Str,RecordDataImpl & Record)4270 void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4271 Record.push_back(Str.size());
4272 Record.insert(Record.end(), Str.begin(), Str.end());
4273 }
4274
PreparePathForOutput(SmallVectorImpl<char> & Path)4275 bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4276 assert(Context && "should have context when outputting path");
4277
4278 bool Changed =
4279 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4280
4281 // Remove a prefix to make the path relative, if relevant.
4282 const char *PathBegin = Path.data();
4283 const char *PathPtr =
4284 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4285 if (PathPtr != PathBegin) {
4286 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4287 Changed = true;
4288 }
4289
4290 return Changed;
4291 }
4292
AddPath(StringRef Path,RecordDataImpl & Record)4293 void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4294 SmallString<128> FilePath(Path);
4295 PreparePathForOutput(FilePath);
4296 AddString(FilePath, Record);
4297 }
4298
EmitRecordWithPath(unsigned Abbrev,RecordDataRef Record,StringRef Path)4299 void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
4300 StringRef Path) {
4301 SmallString<128> FilePath(Path);
4302 PreparePathForOutput(FilePath);
4303 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4304 }
4305
AddVersionTuple(const VersionTuple & Version,RecordDataImpl & Record)4306 void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4307 RecordDataImpl &Record) {
4308 Record.push_back(Version.getMajor());
4309 if (Optional<unsigned> Minor = Version.getMinor())
4310 Record.push_back(*Minor + 1);
4311 else
4312 Record.push_back(0);
4313 if (Optional<unsigned> Subminor = Version.getSubminor())
4314 Record.push_back(*Subminor + 1);
4315 else
4316 Record.push_back(0);
4317 }
4318
4319 /// Note that the identifier II occurs at the given offset
4320 /// within the identifier table.
SetIdentifierOffset(const IdentifierInfo * II,uint32_t Offset)4321 void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4322 IdentID ID = IdentifierIDs[II];
4323 // Only store offsets new to this AST file. Other identifier names are looked
4324 // up earlier in the chain and thus don't need an offset.
4325 if (ID >= FirstIdentID)
4326 IdentifierOffsets[ID - FirstIdentID] = Offset;
4327 }
4328
4329 /// Note that the selector Sel occurs at the given offset
4330 /// within the method pool/selector table.
SetSelectorOffset(Selector Sel,uint32_t Offset)4331 void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4332 unsigned ID = SelectorIDs[Sel];
4333 assert(ID && "Unknown selector");
4334 // Don't record offsets for selectors that are also available in a different
4335 // file.
4336 if (ID < FirstSelectorID)
4337 return;
4338 SelectorOffsets[ID - FirstSelectorID] = Offset;
4339 }
4340
ASTWriter(llvm::BitstreamWriter & Stream,SmallVectorImpl<char> & Buffer,InMemoryModuleCache & ModuleCache,ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,bool IncludeTimestamps)4341 ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4342 SmallVectorImpl<char> &Buffer,
4343 InMemoryModuleCache &ModuleCache,
4344 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4345 bool IncludeTimestamps)
4346 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
4347 IncludeTimestamps(IncludeTimestamps) {
4348 for (const auto &Ext : Extensions) {
4349 if (auto Writer = Ext->createExtensionWriter(*this))
4350 ModuleFileExtensionWriters.push_back(std::move(Writer));
4351 }
4352 }
4353
4354 ASTWriter::~ASTWriter() = default;
4355
getLangOpts() const4356 const LangOptions &ASTWriter::getLangOpts() const {
4357 assert(WritingAST && "can't determine lang opts when not writing AST");
4358 return Context->getLangOpts();
4359 }
4360
getTimestampForOutput(const FileEntry * E) const4361 time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4362 return IncludeTimestamps ? E->getModificationTime() : 0;
4363 }
4364
WriteAST(Sema & SemaRef,const std::string & OutputFile,Module * WritingModule,StringRef isysroot,bool hasErrors,bool ShouldCacheASTInMemory)4365 ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4366 const std::string &OutputFile,
4367 Module *WritingModule, StringRef isysroot,
4368 bool hasErrors,
4369 bool ShouldCacheASTInMemory) {
4370 WritingAST = true;
4371
4372 ASTHasCompilerErrors = hasErrors;
4373
4374 // Emit the file header.
4375 Stream.Emit((unsigned)'C', 8);
4376 Stream.Emit((unsigned)'P', 8);
4377 Stream.Emit((unsigned)'C', 8);
4378 Stream.Emit((unsigned)'H', 8);
4379
4380 WriteBlockInfoBlock();
4381
4382 Context = &SemaRef.Context;
4383 PP = &SemaRef.PP;
4384 this->WritingModule = WritingModule;
4385 ASTFileSignature Signature =
4386 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
4387 Context = nullptr;
4388 PP = nullptr;
4389 this->WritingModule = nullptr;
4390 this->BaseDirectory.clear();
4391
4392 WritingAST = false;
4393 if (ShouldCacheASTInMemory) {
4394 // Construct MemoryBuffer and update buffer manager.
4395 ModuleCache.addBuiltPCM(OutputFile,
4396 llvm::MemoryBuffer::getMemBufferCopy(
4397 StringRef(Buffer.begin(), Buffer.size())));
4398 }
4399 return Signature;
4400 }
4401
4402 template<typename Vector>
AddLazyVectorDecls(ASTWriter & Writer,Vector & Vec,ASTWriter::RecordData & Record)4403 static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4404 ASTWriter::RecordData &Record) {
4405 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4406 I != E; ++I) {
4407 Writer.AddDeclRef(*I, Record);
4408 }
4409 }
4410
WriteASTCore(Sema & SemaRef,StringRef isysroot,const std::string & OutputFile,Module * WritingModule)4411 ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4412 const std::string &OutputFile,
4413 Module *WritingModule) {
4414 using namespace llvm;
4415
4416 bool isModule = WritingModule != nullptr;
4417
4418 // Make sure that the AST reader knows to finalize itself.
4419 if (Chain)
4420 Chain->finalizeForWriting();
4421
4422 ASTContext &Context = SemaRef.Context;
4423 Preprocessor &PP = SemaRef.PP;
4424
4425 // Set up predefined declaration IDs.
4426 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4427 if (D) {
4428 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4429 DeclIDs[D] = ID;
4430 }
4431 };
4432 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4433 PREDEF_DECL_TRANSLATION_UNIT_ID);
4434 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4435 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4436 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4437 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4438 PREDEF_DECL_OBJC_PROTOCOL_ID);
4439 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4440 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4441 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4442 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4443 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
4444 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
4445 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4446 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
4447 RegisterPredefDecl(Context.MSGuidTagDecl,
4448 PREDEF_DECL_BUILTIN_MS_GUID_ID);
4449 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
4450 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4451 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
4452 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4453 PREDEF_DECL_CF_CONSTANT_STRING_ID);
4454 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4455 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
4456 RegisterPredefDecl(Context.TypePackElementDecl,
4457 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
4458
4459 // Build a record containing all of the tentative definitions in this file, in
4460 // TentativeDefinitions order. Generally, this record will be empty for
4461 // headers.
4462 RecordData TentativeDefinitions;
4463 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4464
4465 // Build a record containing all of the file scoped decls in this file.
4466 RecordData UnusedFileScopedDecls;
4467 if (!isModule)
4468 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4469 UnusedFileScopedDecls);
4470
4471 // Build a record containing all of the delegating constructors we still need
4472 // to resolve.
4473 RecordData DelegatingCtorDecls;
4474 if (!isModule)
4475 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4476
4477 // Write the set of weak, undeclared identifiers. We always write the
4478 // entire table, since later PCH files in a PCH chain are only interested in
4479 // the results at the end of the chain.
4480 RecordData WeakUndeclaredIdentifiers;
4481 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4482 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4483 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4484 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4485 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4486 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4487 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
4488 }
4489
4490 // Build a record containing all of the ext_vector declarations.
4491 RecordData ExtVectorDecls;
4492 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4493
4494 // Build a record containing all of the VTable uses information.
4495 RecordData VTableUses;
4496 if (!SemaRef.VTableUses.empty()) {
4497 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4498 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4499 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4500 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4501 }
4502 }
4503
4504 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4505 RecordData UnusedLocalTypedefNameCandidates;
4506 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4507 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4508
4509 // Build a record containing all of pending implicit instantiations.
4510 RecordData PendingInstantiations;
4511 for (const auto &I : SemaRef.PendingInstantiations) {
4512 AddDeclRef(I.first, PendingInstantiations);
4513 AddSourceLocation(I.second, PendingInstantiations);
4514 }
4515 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4516 "There are local ones at end of translation unit!");
4517
4518 // Build a record containing some declaration references.
4519 RecordData SemaDeclRefs;
4520 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
4521 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4522 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4523 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
4524 }
4525
4526 RecordData CUDASpecialDeclRefs;
4527 if (Context.getcudaConfigureCallDecl()) {
4528 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4529 }
4530
4531 // Build a record containing all of the known namespaces.
4532 RecordData KnownNamespaces;
4533 for (const auto &I : SemaRef.KnownNamespaces) {
4534 if (!I.second)
4535 AddDeclRef(I.first, KnownNamespaces);
4536 }
4537
4538 // Build a record of all used, undefined objects that require definitions.
4539 RecordData UndefinedButUsed;
4540
4541 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4542 SemaRef.getUndefinedButUsed(Undefined);
4543 for (const auto &I : Undefined) {
4544 AddDeclRef(I.first, UndefinedButUsed);
4545 AddSourceLocation(I.second, UndefinedButUsed);
4546 }
4547
4548 // Build a record containing all delete-expressions that we would like to
4549 // analyze later in AST.
4550 RecordData DeleteExprsToAnalyze;
4551
4552 if (!isModule) {
4553 for (const auto &DeleteExprsInfo :
4554 SemaRef.getMismatchingDeleteExpressions()) {
4555 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4556 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4557 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4558 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4559 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4560 }
4561 }
4562 }
4563
4564 // Write the control block
4565 WriteControlBlock(PP, Context, isysroot, OutputFile);
4566
4567 // Write the remaining AST contents.
4568 Stream.FlushToWord();
4569 ASTBlockRange.first = Stream.GetCurrentBitNo();
4570 Stream.EnterSubblock(AST_BLOCK_ID, 5);
4571 ASTBlockStartOffset = Stream.GetCurrentBitNo();
4572
4573 // This is so that older clang versions, before the introduction
4574 // of the control block, can read and reject the newer PCH format.
4575 {
4576 RecordData Record = {VERSION_MAJOR};
4577 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4578 }
4579
4580 // Create a lexical update block containing all of the declarations in the
4581 // translation unit that do not come from other AST files.
4582 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4583 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4584 for (const auto *D : TU->noload_decls()) {
4585 if (!D->isFromASTFile()) {
4586 NewGlobalKindDeclPairs.push_back(D->getKind());
4587 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4588 }
4589 }
4590
4591 auto Abv = std::make_shared<BitCodeAbbrev>();
4592 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4593 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4594 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
4595 {
4596 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4597 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4598 bytes(NewGlobalKindDeclPairs));
4599 }
4600
4601 // And a visible updates block for the translation unit.
4602 Abv = std::make_shared<BitCodeAbbrev>();
4603 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4604 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4605 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4606 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
4607 WriteDeclContextVisibleUpdate(TU);
4608
4609 // If we have any extern "C" names, write out a visible update for them.
4610 if (Context.ExternCContext)
4611 WriteDeclContextVisibleUpdate(Context.ExternCContext);
4612
4613 // If the translation unit has an anonymous namespace, and we don't already
4614 // have an update block for it, write it as an update block.
4615 // FIXME: Why do we not do this if there's already an update block?
4616 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4617 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4618 if (Record.empty())
4619 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4620 }
4621
4622 // Add update records for all mangling numbers and static local numbers.
4623 // These aren't really update records, but this is a convenient way of
4624 // tagging this rare extra data onto the declarations.
4625 for (const auto &Number : Context.MangleNumbers)
4626 if (!Number.first->isFromASTFile())
4627 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4628 Number.second));
4629 for (const auto &Number : Context.StaticLocalNumbers)
4630 if (!Number.first->isFromASTFile())
4631 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4632 Number.second));
4633
4634 // Make sure visible decls, added to DeclContexts previously loaded from
4635 // an AST file, are registered for serialization. Likewise for template
4636 // specializations added to imported templates.
4637 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
4638 GetDeclRef(I);
4639 }
4640
4641 // Make sure all decls associated with an identifier are registered for
4642 // serialization, if we're storing decls with identifiers.
4643 if (!WritingModule || !getLangOpts().CPlusPlus) {
4644 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4645 for (const auto &ID : PP.getIdentifierTable()) {
4646 const IdentifierInfo *II = ID.second;
4647 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4648 IIs.push_back(II);
4649 }
4650 // Sort the identifiers to visit based on their name.
4651 llvm::sort(IIs, llvm::deref<std::less<>>());
4652 for (const IdentifierInfo *II : IIs) {
4653 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4654 DEnd = SemaRef.IdResolver.end();
4655 D != DEnd; ++D) {
4656 GetDeclRef(*D);
4657 }
4658 }
4659 }
4660
4661 // For method pool in the module, if it contains an entry for a selector,
4662 // the entry should be complete, containing everything introduced by that
4663 // module and all modules it imports. It's possible that the entry is out of
4664 // date, so we need to pull in the new content here.
4665
4666 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4667 // safe, we copy all selectors out.
4668 llvm::SmallVector<Selector, 256> AllSelectors;
4669 for (auto &SelectorAndID : SelectorIDs)
4670 AllSelectors.push_back(SelectorAndID.first);
4671 for (auto &Selector : AllSelectors)
4672 SemaRef.updateOutOfDateSelector(Selector);
4673
4674 // Form the record of special types.
4675 RecordData SpecialTypes;
4676 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
4677 AddTypeRef(Context.getFILEType(), SpecialTypes);
4678 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4679 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4680 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4681 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
4682 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
4683 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
4684
4685 if (Chain) {
4686 // Write the mapping information describing our module dependencies and how
4687 // each of those modules were mapped into our own offset/ID space, so that
4688 // the reader can build the appropriate mapping to its own offset/ID space.
4689 // The map consists solely of a blob with the following format:
4690 // *(module-kind:i8
4691 // module-name-len:i16 module-name:len*i8
4692 // source-location-offset:i32
4693 // identifier-id:i32
4694 // preprocessed-entity-id:i32
4695 // macro-definition-id:i32
4696 // submodule-id:i32
4697 // selector-id:i32
4698 // declaration-id:i32
4699 // c++-base-specifiers-id:i32
4700 // type-id:i32)
4701 //
4702 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule,
4703 // MK_ExplicitModule or MK_ImplicitModule, then the module-name is the
4704 // module name. Otherwise, it is the module file name.
4705 auto Abbrev = std::make_shared<BitCodeAbbrev>();
4706 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4707 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4708 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
4709 SmallString<2048> Buffer;
4710 {
4711 llvm::raw_svector_ostream Out(Buffer);
4712 for (ModuleFile &M : Chain->ModuleMgr) {
4713 using namespace llvm::support;
4714
4715 endian::Writer LE(Out, little);
4716 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
4717 StringRef Name = M.isModule() ? M.ModuleName : M.FileName;
4718 LE.write<uint16_t>(Name.size());
4719 Out.write(Name.data(), Name.size());
4720
4721 // Note: if a base ID was uint max, it would not be possible to load
4722 // another module after it or have more than one entity inside it.
4723 uint32_t None = std::numeric_limits<uint32_t>::max();
4724
4725 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4726 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4727 if (ShouldWrite)
4728 LE.write<uint32_t>(BaseID);
4729 else
4730 LE.write<uint32_t>(None);
4731 };
4732
4733 // These values should be unique within a chain, since they will be read
4734 // as keys into ContinuousRangeMaps.
4735 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4736 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4737 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4738 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4739 M.NumPreprocessedEntities);
4740 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4741 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4742 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4743 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
4744 }
4745 }
4746 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
4747 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4748 Buffer.data(), Buffer.size());
4749 }
4750
4751 // Build a record containing all of the DeclsToCheckForDeferredDiags.
4752 RecordData DeclsToCheckForDeferredDiags;
4753 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
4754 AddDeclRef(D, DeclsToCheckForDeferredDiags);
4755
4756 RecordData DeclUpdatesOffsetsRecord;
4757
4758 // Keep writing types, declarations, and declaration update records
4759 // until we've emitted all of them.
4760 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4761 DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
4762 WriteTypeAbbrevs();
4763 WriteDeclAbbrevs();
4764 do {
4765 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4766 while (!DeclTypesToEmit.empty()) {
4767 DeclOrType DOT = DeclTypesToEmit.front();
4768 DeclTypesToEmit.pop();
4769 if (DOT.isType())
4770 WriteType(DOT.getType());
4771 else
4772 WriteDecl(Context, DOT.getDecl());
4773 }
4774 } while (!DeclUpdates.empty());
4775 Stream.ExitBlock();
4776
4777 DoneWritingDeclsAndTypes = true;
4778
4779 // These things can only be done once we've written out decls and types.
4780 WriteTypeDeclOffsets();
4781 if (!DeclUpdatesOffsetsRecord.empty())
4782 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4783 WriteFileDeclIDsMap();
4784 WriteSourceManagerBlock(Context.getSourceManager(), PP);
4785 WriteComments();
4786 WritePreprocessor(PP, isModule);
4787 WriteHeaderSearch(PP.getHeaderSearchInfo());
4788 WriteSelectors(SemaRef);
4789 WriteReferencedSelectorsPool(SemaRef);
4790 WriteLateParsedTemplates(SemaRef);
4791 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
4792 WriteFPPragmaOptions(SemaRef.CurFPFeatureOverrides());
4793 WriteOpenCLExtensions(SemaRef);
4794 WriteOpenCLExtensionTypes(SemaRef);
4795 WriteCUDAPragmas(SemaRef);
4796
4797 // If we're emitting a module, write out the submodule information.
4798 if (WritingModule)
4799 WriteSubmodules(WritingModule);
4800
4801 // We need to have information about submodules to correctly deserialize
4802 // decls from OpenCLExtensionDecls block
4803 WriteOpenCLExtensionDecls(SemaRef);
4804
4805 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4806
4807 // Write the record containing external, unnamed definitions.
4808 if (!EagerlyDeserializedDecls.empty())
4809 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
4810
4811 if (!ModularCodegenDecls.empty())
4812 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4813
4814 // Write the record containing tentative definitions.
4815 if (!TentativeDefinitions.empty())
4816 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
4817
4818 // Write the record containing unused file scoped decls.
4819 if (!UnusedFileScopedDecls.empty())
4820 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
4821
4822 // Write the record containing weak undeclared identifiers.
4823 if (!WeakUndeclaredIdentifiers.empty())
4824 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
4825 WeakUndeclaredIdentifiers);
4826
4827 // Write the record containing ext_vector type names.
4828 if (!ExtVectorDecls.empty())
4829 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
4830
4831 // Write the record containing VTable uses information.
4832 if (!VTableUses.empty())
4833 Stream.EmitRecord(VTABLE_USES, VTableUses);
4834
4835 // Write the record containing potentially unused local typedefs.
4836 if (!UnusedLocalTypedefNameCandidates.empty())
4837 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4838 UnusedLocalTypedefNameCandidates);
4839
4840 // Write the record containing pending implicit instantiations.
4841 if (!PendingInstantiations.empty())
4842 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
4843
4844 // Write the record containing declaration references of Sema.
4845 if (!SemaDeclRefs.empty())
4846 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
4847
4848 // Write the record containing decls to be checked for deferred diags.
4849 if (!DeclsToCheckForDeferredDiags.empty())
4850 Stream.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
4851 DeclsToCheckForDeferredDiags);
4852
4853 // Write the record containing CUDA-specific declaration references.
4854 if (!CUDASpecialDeclRefs.empty())
4855 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
4856
4857 // Write the delegating constructors.
4858 if (!DelegatingCtorDecls.empty())
4859 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
4860
4861 // Write the known namespaces.
4862 if (!KnownNamespaces.empty())
4863 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
4864
4865 // Write the undefined internal functions and variables, and inline functions.
4866 if (!UndefinedButUsed.empty())
4867 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
4868
4869 if (!DeleteExprsToAnalyze.empty())
4870 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4871
4872 // Write the visible updates to DeclContexts.
4873 for (auto *DC : UpdatedDeclContexts)
4874 WriteDeclContextVisibleUpdate(DC);
4875
4876 if (!WritingModule) {
4877 // Write the submodules that were imported, if any.
4878 struct ModuleInfo {
4879 uint64_t ID;
4880 Module *M;
4881 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4882 };
4883 llvm::SmallVector<ModuleInfo, 64> Imports;
4884 for (const auto *I : Context.local_imports()) {
4885 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4886 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4887 I->getImportedModule()));
4888 }
4889
4890 if (!Imports.empty()) {
4891 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4892 return A.ID < B.ID;
4893 };
4894 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4895 return A.ID == B.ID;
4896 };
4897
4898 // Sort and deduplicate module IDs.
4899 llvm::sort(Imports, Cmp);
4900 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
4901 Imports.end());
4902
4903 RecordData ImportedModules;
4904 for (const auto &Import : Imports) {
4905 ImportedModules.push_back(Import.ID);
4906 // FIXME: If the module has macros imported then later has declarations
4907 // imported, this location won't be the right one as a location for the
4908 // declaration imports.
4909 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
4910 }
4911
4912 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4913 }
4914 }
4915
4916 WriteObjCCategories();
4917 if(!WritingModule) {
4918 WriteOptimizePragmaOptions(SemaRef);
4919 WriteMSStructPragmaOptions(SemaRef);
4920 WriteMSPointersToMembersPragmaOptions(SemaRef);
4921 }
4922 WritePackPragmaOptions(SemaRef);
4923 WriteFloatControlPragmaOptions(SemaRef);
4924
4925 // Some simple statistics
4926 RecordData::value_type Record[] = {
4927 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
4928 Stream.EmitRecord(STATISTICS, Record);
4929 Stream.ExitBlock();
4930 Stream.FlushToWord();
4931 ASTBlockRange.second = Stream.GetCurrentBitNo();
4932
4933 // Write the module file extension blocks.
4934 for (const auto &ExtWriter : ModuleFileExtensionWriters)
4935 WriteModuleFileExtension(SemaRef, *ExtWriter);
4936
4937 return writeUnhashedControlBlock(PP, Context);
4938 }
4939
WriteDeclUpdatesBlocks(RecordDataImpl & OffsetsRecord)4940 void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
4941 if (DeclUpdates.empty())
4942 return;
4943
4944 DeclUpdateMap LocalUpdates;
4945 LocalUpdates.swap(DeclUpdates);
4946
4947 for (auto &DeclUpdate : LocalUpdates) {
4948 const Decl *D = DeclUpdate.first;
4949
4950 bool HasUpdatedBody = false;
4951 RecordData RecordData;
4952 ASTRecordWriter Record(*this, RecordData);
4953 for (auto &Update : DeclUpdate.second) {
4954 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4955
4956 // An updated body is emitted last, so that the reader doesn't need
4957 // to skip over the lazy body to reach statements for other records.
4958 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4959 HasUpdatedBody = true;
4960 else
4961 Record.push_back(Kind);
4962
4963 switch (Kind) {
4964 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4965 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4966 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4967 assert(Update.getDecl() && "no decl to add?");
4968 Record.push_back(GetDeclRef(Update.getDecl()));
4969 break;
4970
4971 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
4972 break;
4973
4974 case UPD_CXX_POINT_OF_INSTANTIATION:
4975 // FIXME: Do we need to also save the template specialization kind here?
4976 Record.AddSourceLocation(Update.getLoc());
4977 break;
4978
4979 case UPD_CXX_ADDED_VAR_DEFINITION: {
4980 const VarDecl *VD = cast<VarDecl>(D);
4981 Record.push_back(VD->isInline());
4982 Record.push_back(VD->isInlineSpecified());
4983 Record.AddVarDeclInit(VD);
4984 break;
4985 }
4986
4987 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
4988 Record.AddStmt(const_cast<Expr *>(
4989 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
4990 break;
4991
4992 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4993 Record.AddStmt(
4994 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4995 break;
4996
4997 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4998 auto *RD = cast<CXXRecordDecl>(D);
4999 UpdatedDeclContexts.insert(RD->getPrimaryContext());
5000 Record.push_back(RD->isParamDestroyedInCallee());
5001 Record.push_back(RD->getArgPassingRestrictions());
5002 Record.AddCXXDefinitionData(RD);
5003 Record.AddOffset(WriteDeclContextLexicalBlock(
5004 *Context, const_cast<CXXRecordDecl *>(RD)));
5005
5006 // This state is sometimes updated by template instantiation, when we
5007 // switch from the specialization referring to the template declaration
5008 // to it referring to the template definition.
5009 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5010 Record.push_back(MSInfo->getTemplateSpecializationKind());
5011 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
5012 } else {
5013 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5014 Record.push_back(Spec->getTemplateSpecializationKind());
5015 Record.AddSourceLocation(Spec->getPointOfInstantiation());
5016
5017 // The instantiation might have been resolved to a partial
5018 // specialization. If so, record which one.
5019 auto From = Spec->getInstantiatedFrom();
5020 if (auto PartialSpec =
5021 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5022 Record.push_back(true);
5023 Record.AddDeclRef(PartialSpec);
5024 Record.AddTemplateArgumentList(
5025 &Spec->getTemplateInstantiationArgs());
5026 } else {
5027 Record.push_back(false);
5028 }
5029 }
5030 Record.push_back(RD->getTagKind());
5031 Record.AddSourceLocation(RD->getLocation());
5032 Record.AddSourceLocation(RD->getBeginLoc());
5033 Record.AddSourceRange(RD->getBraceRange());
5034
5035 // Instantiation may change attributes; write them all out afresh.
5036 Record.push_back(D->hasAttrs());
5037 if (D->hasAttrs())
5038 Record.AddAttributes(D->getAttrs());
5039
5040 // FIXME: Ensure we don't get here for explicit instantiations.
5041 break;
5042 }
5043
5044 case UPD_CXX_RESOLVED_DTOR_DELETE:
5045 Record.AddDeclRef(Update.getDecl());
5046 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
5047 break;
5048
5049 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
5050 auto prototype =
5051 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
5052 Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
5053 break;
5054 }
5055
5056 case UPD_CXX_DEDUCED_RETURN_TYPE:
5057 Record.push_back(GetOrCreateTypeID(Update.getType()));
5058 break;
5059
5060 case UPD_DECL_MARKED_USED:
5061 break;
5062
5063 case UPD_MANGLING_NUMBER:
5064 case UPD_STATIC_LOCAL_NUMBER:
5065 Record.push_back(Update.getNumber());
5066 break;
5067
5068 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
5069 Record.AddSourceRange(
5070 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
5071 break;
5072
5073 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5074 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5075 Record.push_back(A->getAllocatorType());
5076 Record.AddStmt(A->getAllocator());
5077 Record.AddSourceRange(A->getRange());
5078 break;
5079 }
5080
5081 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5082 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
5083 Record.AddSourceRange(
5084 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5085 break;
5086
5087 case UPD_DECL_EXPORTED:
5088 Record.push_back(getSubmoduleID(Update.getModule()));
5089 break;
5090
5091 case UPD_ADDED_ATTR_TO_RECORD:
5092 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
5093 break;
5094 }
5095 }
5096
5097 if (HasUpdatedBody) {
5098 const auto *Def = cast<FunctionDecl>(D);
5099 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
5100 Record.push_back(Def->isInlined());
5101 Record.AddSourceLocation(Def->getInnerLocStart());
5102 Record.AddFunctionDefinition(Def);
5103 }
5104
5105 OffsetsRecord.push_back(GetDeclRef(D));
5106 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
5107 }
5108 }
5109
AddSourceLocation(SourceLocation Loc,RecordDataImpl & Record)5110 void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
5111 uint32_t Raw = Loc.getRawEncoding();
5112 Record.push_back((Raw << 1) | (Raw >> 31));
5113 }
5114
AddSourceRange(SourceRange Range,RecordDataImpl & Record)5115 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
5116 AddSourceLocation(Range.getBegin(), Record);
5117 AddSourceLocation(Range.getEnd(), Record);
5118 }
5119
AddAPFloat(const llvm::APFloat & Value)5120 void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5121 AddAPInt(Value.bitcastToAPInt());
5122 }
5123
WriteFixedPointSemantics(ASTRecordWriter & Record,llvm::FixedPointSemantics FPSema)5124 static void WriteFixedPointSemantics(ASTRecordWriter &Record,
5125 llvm::FixedPointSemantics FPSema) {
5126 Record.push_back(FPSema.getWidth());
5127 Record.push_back(FPSema.getScale());
5128 Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 |
5129 FPSema.hasUnsignedPadding() << 2);
5130 }
5131
AddAPValue(const APValue & Value)5132 void ASTRecordWriter::AddAPValue(const APValue &Value) {
5133 APValue::ValueKind Kind = Value.getKind();
5134 push_back(static_cast<uint64_t>(Kind));
5135 switch (Kind) {
5136 case APValue::None:
5137 case APValue::Indeterminate:
5138 return;
5139 case APValue::Int:
5140 AddAPSInt(Value.getInt());
5141 return;
5142 case APValue::Float:
5143 push_back(static_cast<uint64_t>(
5144 llvm::APFloatBase::SemanticsToEnum(Value.getFloat().getSemantics())));
5145 AddAPFloat(Value.getFloat());
5146 return;
5147 case APValue::FixedPoint: {
5148 WriteFixedPointSemantics(*this, Value.getFixedPoint().getSemantics());
5149 AddAPSInt(Value.getFixedPoint().getValue());
5150 return;
5151 }
5152 case APValue::ComplexInt: {
5153 AddAPSInt(Value.getComplexIntReal());
5154 AddAPSInt(Value.getComplexIntImag());
5155 return;
5156 }
5157 case APValue::ComplexFloat: {
5158 assert(llvm::APFloatBase::SemanticsToEnum(
5159 Value.getComplexFloatImag().getSemantics()) ==
5160 llvm::APFloatBase::SemanticsToEnum(
5161 Value.getComplexFloatReal().getSemantics()));
5162 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5163 Value.getComplexFloatReal().getSemantics())));
5164 AddAPFloat(Value.getComplexFloatReal());
5165 AddAPFloat(Value.getComplexFloatImag());
5166 return;
5167 }
5168 case APValue::Vector:
5169 push_back(Value.getVectorLength());
5170 for (unsigned Idx = 0; Idx < Value.getVectorLength(); Idx++)
5171 AddAPValue(Value.getVectorElt(Idx));
5172 return;
5173 case APValue::Array:
5174 push_back(Value.getArrayInitializedElts());
5175 push_back(Value.getArraySize());
5176 for (unsigned Idx = 0; Idx < Value.getArrayInitializedElts(); Idx++)
5177 AddAPValue(Value.getArrayInitializedElt(Idx));
5178 if (Value.hasArrayFiller())
5179 AddAPValue(Value.getArrayFiller());
5180 return;
5181 case APValue::Struct:
5182 push_back(Value.getStructNumBases());
5183 push_back(Value.getStructNumFields());
5184 for (unsigned Idx = 0; Idx < Value.getStructNumBases(); Idx++)
5185 AddAPValue(Value.getStructBase(Idx));
5186 for (unsigned Idx = 0; Idx < Value.getStructNumFields(); Idx++)
5187 AddAPValue(Value.getStructField(Idx));
5188 return;
5189 case APValue::Union:
5190 AddDeclRef(Value.getUnionField());
5191 AddAPValue(Value.getUnionValue());
5192 return;
5193 case APValue::AddrLabelDiff:
5194 AddStmt(const_cast<AddrLabelExpr *>(Value.getAddrLabelDiffLHS()));
5195 AddStmt(const_cast<AddrLabelExpr *>(Value.getAddrLabelDiffRHS()));
5196 return;
5197 case APValue::MemberPointer: {
5198 push_back(Value.isMemberPointerToDerivedMember());
5199 AddDeclRef(Value.getMemberPointerDecl());
5200 ArrayRef<const CXXRecordDecl *> RecordPath = Value.getMemberPointerPath();
5201 push_back(RecordPath.size());
5202 for (auto Elem : RecordPath)
5203 AddDeclRef(Elem);
5204 return;
5205 }
5206 case APValue::LValue: {
5207 push_back(Value.hasLValuePath() | Value.isLValueOnePastTheEnd() << 1 |
5208 Value.getLValueBase().is<const Expr *>() << 2 |
5209 Value.getLValueBase().is<TypeInfoLValue>() << 3 |
5210 Value.isNullPointer() << 4 |
5211 static_cast<bool>(Value.getLValueBase()) << 5);
5212 QualType ElemTy;
5213 if (Value.getLValueBase()) {
5214 assert(!Value.getLValueBase().is<DynamicAllocLValue>() &&
5215 "in C++20 dynamic allocation are transient so they shouldn't "
5216 "appear in the AST");
5217 if (!Value.getLValueBase().is<TypeInfoLValue>()) {
5218 push_back(Value.getLValueBase().getCallIndex());
5219 push_back(Value.getLValueBase().getVersion());
5220 if (const auto *E = Value.getLValueBase().dyn_cast<const Expr *>()) {
5221 AddStmt(const_cast<Expr *>(E));
5222 ElemTy = E->getType();
5223 } else {
5224 AddDeclRef(Value.getLValueBase().get<const ValueDecl *>());
5225 ElemTy = Value.getLValueBase().get<const ValueDecl *>()->getType();
5226 }
5227 } else {
5228 AddTypeRef(
5229 QualType(Value.getLValueBase().get<TypeInfoLValue>().getType(), 0));
5230 AddTypeRef(Value.getLValueBase().getTypeInfoType());
5231 ElemTy = Value.getLValueBase().getTypeInfoType();
5232 }
5233 }
5234 push_back(Value.getLValueOffset().getQuantity());
5235 push_back(Value.getLValuePath().size());
5236 if (Value.hasLValuePath()) {
5237 ArrayRef<APValue::LValuePathEntry> Path = Value.getLValuePath();
5238 for (auto Elem : Path) {
5239 if (ElemTy->getAs<RecordType>()) {
5240 push_back(Elem.getAsBaseOrMember().getInt());
5241 const Decl *BaseOrMember = Elem.getAsBaseOrMember().getPointer();
5242 if (const auto *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
5243 AddDeclRef(RD);
5244 ElemTy = Writer->Context->getRecordType(RD);
5245 } else {
5246 const auto *VD = cast<ValueDecl>(BaseOrMember);
5247 AddDeclRef(VD);
5248 ElemTy = VD->getType();
5249 }
5250 } else {
5251 push_back(Elem.getAsArrayIndex());
5252 ElemTy = Writer->Context->getAsArrayType(ElemTy)->getElementType();
5253 }
5254 }
5255 }
5256 }
5257 return;
5258 }
5259 llvm_unreachable("Invalid APValue::ValueKind");
5260 }
5261
AddIdentifierRef(const IdentifierInfo * II,RecordDataImpl & Record)5262 void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
5263 Record.push_back(getIdentifierRef(II));
5264 }
5265
getIdentifierRef(const IdentifierInfo * II)5266 IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
5267 if (!II)
5268 return 0;
5269
5270 IdentID &ID = IdentifierIDs[II];
5271 if (ID == 0)
5272 ID = NextIdentID++;
5273 return ID;
5274 }
5275
getMacroRef(MacroInfo * MI,const IdentifierInfo * Name)5276 MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
5277 // Don't emit builtin macros like __LINE__ to the AST file unless they
5278 // have been redefined by the header (in which case they are not
5279 // isBuiltinMacro).
5280 if (!MI || MI->isBuiltinMacro())
5281 return 0;
5282
5283 MacroID &ID = MacroIDs[MI];
5284 if (ID == 0) {
5285 ID = NextMacroID++;
5286 MacroInfoToEmitData Info = { Name, MI, ID };
5287 MacroInfosToEmit.push_back(Info);
5288 }
5289 return ID;
5290 }
5291
getMacroID(MacroInfo * MI)5292 MacroID ASTWriter::getMacroID(MacroInfo *MI) {
5293 if (!MI || MI->isBuiltinMacro())
5294 return 0;
5295
5296 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5297 return MacroIDs[MI];
5298 }
5299
getMacroDirectivesOffset(const IdentifierInfo * Name)5300 uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
5301 return IdentMacroDirectivesOffsetMap.lookup(Name);
5302 }
5303
AddSelectorRef(const Selector SelRef)5304 void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5305 Record->push_back(Writer->getSelectorRef(SelRef));
5306 }
5307
getSelectorRef(Selector Sel)5308 SelectorID ASTWriter::getSelectorRef(Selector Sel) {
5309 if (Sel.getAsOpaquePtr() == nullptr) {
5310 return 0;
5311 }
5312
5313 SelectorID SID = SelectorIDs[Sel];
5314 if (SID == 0 && Chain) {
5315 // This might trigger a ReadSelector callback, which will set the ID for
5316 // this selector.
5317 Chain->LoadSelector(Sel);
5318 SID = SelectorIDs[Sel];
5319 }
5320 if (SID == 0) {
5321 SID = NextSelectorID++;
5322 SelectorIDs[Sel] = SID;
5323 }
5324 return SID;
5325 }
5326
AddCXXTemporary(const CXXTemporary * Temp)5327 void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5328 AddDeclRef(Temp->getDestructor());
5329 }
5330
AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,const TemplateArgumentLocInfo & Arg)5331 void ASTRecordWriter::AddTemplateArgumentLocInfo(
5332 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
5333 switch (Kind) {
5334 case TemplateArgument::Expression:
5335 AddStmt(Arg.getAsExpr());
5336 break;
5337 case TemplateArgument::Type:
5338 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
5339 break;
5340 case TemplateArgument::Template:
5341 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5342 AddSourceLocation(Arg.getTemplateNameLoc());
5343 break;
5344 case TemplateArgument::TemplateExpansion:
5345 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5346 AddSourceLocation(Arg.getTemplateNameLoc());
5347 AddSourceLocation(Arg.getTemplateEllipsisLoc());
5348 break;
5349 case TemplateArgument::Null:
5350 case TemplateArgument::Integral:
5351 case TemplateArgument::Declaration:
5352 case TemplateArgument::NullPtr:
5353 case TemplateArgument::Pack:
5354 // FIXME: Is this right?
5355 break;
5356 }
5357 }
5358
AddTemplateArgumentLoc(const TemplateArgumentLoc & Arg)5359 void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5360 AddTemplateArgument(Arg.getArgument());
5361
5362 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5363 bool InfoHasSameExpr
5364 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5365 Record->push_back(InfoHasSameExpr);
5366 if (InfoHasSameExpr)
5367 return; // Avoid storing the same expr twice.
5368 }
5369 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
5370 }
5371
AddTypeSourceInfo(TypeSourceInfo * TInfo)5372 void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
5373 if (!TInfo) {
5374 AddTypeRef(QualType());
5375 return;
5376 }
5377
5378 AddTypeRef(TInfo->getType());
5379 AddTypeLoc(TInfo->getTypeLoc());
5380 }
5381
AddTypeLoc(TypeLoc TL)5382 void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5383 TypeLocWriter TLW(*this);
5384 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
5385 TLW.Visit(TL);
5386 }
5387
AddTypeRef(QualType T,RecordDataImpl & Record)5388 void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
5389 Record.push_back(GetOrCreateTypeID(T));
5390 }
5391
GetOrCreateTypeID(QualType T)5392 TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
5393 assert(Context);
5394 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5395 if (T.isNull())
5396 return TypeIdx();
5397 assert(!T.getLocalFastQualifiers());
5398
5399 TypeIdx &Idx = TypeIdxs[T];
5400 if (Idx.getIndex() == 0) {
5401 if (DoneWritingDeclsAndTypes) {
5402 assert(0 && "New type seen after serializing all the types to emit!");
5403 return TypeIdx();
5404 }
5405
5406 // We haven't seen this type before. Assign it a new ID and put it
5407 // into the queue of types to emit.
5408 Idx = TypeIdx(NextTypeID++);
5409 DeclTypesToEmit.push(T);
5410 }
5411 return Idx;
5412 });
5413 }
5414
getTypeID(QualType T) const5415 TypeID ASTWriter::getTypeID(QualType T) const {
5416 assert(Context);
5417 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5418 if (T.isNull())
5419 return TypeIdx();
5420 assert(!T.getLocalFastQualifiers());
5421
5422 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5423 assert(I != TypeIdxs.end() && "Type not emitted!");
5424 return I->second;
5425 });
5426 }
5427
AddDeclRef(const Decl * D,RecordDataImpl & Record)5428 void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
5429 Record.push_back(GetDeclRef(D));
5430 }
5431
GetDeclRef(const Decl * D)5432 DeclID ASTWriter::GetDeclRef(const Decl *D) {
5433 assert(WritingAST && "Cannot request a declaration ID before AST writing");
5434
5435 if (!D) {
5436 return 0;
5437 }
5438
5439 // If D comes from an AST file, its declaration ID is already known and
5440 // fixed.
5441 if (D->isFromASTFile())
5442 return D->getGlobalID();
5443
5444 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
5445 DeclID &ID = DeclIDs[D];
5446 if (ID == 0) {
5447 if (DoneWritingDeclsAndTypes) {
5448 assert(0 && "New decl seen after serializing all the decls to emit!");
5449 return 0;
5450 }
5451
5452 // We haven't seen this declaration before. Give it a new ID and
5453 // enqueue it in the list of declarations to emit.
5454 ID = NextDeclID++;
5455 DeclTypesToEmit.push(const_cast<Decl *>(D));
5456 }
5457
5458 return ID;
5459 }
5460
getDeclID(const Decl * D)5461 DeclID ASTWriter::getDeclID(const Decl *D) {
5462 if (!D)
5463 return 0;
5464
5465 // If D comes from an AST file, its declaration ID is already known and
5466 // fixed.
5467 if (D->isFromASTFile())
5468 return D->getGlobalID();
5469
5470 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5471 return DeclIDs[D];
5472 }
5473
associateDeclWithFile(const Decl * D,DeclID ID)5474 void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5475 assert(ID);
5476 assert(D);
5477
5478 SourceLocation Loc = D->getLocation();
5479 if (Loc.isInvalid())
5480 return;
5481
5482 // We only keep track of the file-level declarations of each file.
5483 if (!D->getLexicalDeclContext()->isFileContext())
5484 return;
5485 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5486 // a function/objc method, should not have TU as lexical context.
5487 // TemplateTemplateParmDecls that are part of an alias template, should not
5488 // have TU as lexical context.
5489 if (isa<ParmVarDecl>(D) || isa<TemplateTemplateParmDecl>(D))
5490 return;
5491
5492 SourceManager &SM = Context->getSourceManager();
5493 SourceLocation FileLoc = SM.getFileLoc(Loc);
5494 assert(SM.isLocalSourceLocation(FileLoc));
5495 FileID FID;
5496 unsigned Offset;
5497 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5498 if (FID.isInvalid())
5499 return;
5500 assert(SM.getSLocEntry(FID).isFile());
5501
5502 std::unique_ptr<DeclIDInFileInfo> &Info = FileDeclIDs[FID];
5503 if (!Info)
5504 Info = std::make_unique<DeclIDInFileInfo>();
5505
5506 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5507 LocDeclIDsTy &Decls = Info->DeclIDs;
5508
5509 if (Decls.empty() || Decls.back().first <= Offset) {
5510 Decls.push_back(LocDecl);
5511 return;
5512 }
5513
5514 LocDeclIDsTy::iterator I =
5515 llvm::upper_bound(Decls, LocDecl, llvm::less_first());
5516
5517 Decls.insert(I, LocDecl);
5518 }
5519
getAnonymousDeclarationNumber(const NamedDecl * D)5520 unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5521 assert(needsAnonymousDeclarationNumber(D) &&
5522 "expected an anonymous declaration");
5523
5524 // Number the anonymous declarations within this context, if we've not
5525 // already done so.
5526 auto It = AnonymousDeclarationNumbers.find(D);
5527 if (It == AnonymousDeclarationNumbers.end()) {
5528 auto *DC = D->getLexicalDeclContext();
5529 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5530 AnonymousDeclarationNumbers[ND] = Number;
5531 });
5532
5533 It = AnonymousDeclarationNumbers.find(D);
5534 assert(It != AnonymousDeclarationNumbers.end() &&
5535 "declaration not found within its lexical context");
5536 }
5537
5538 return It->second;
5539 }
5540
AddDeclarationNameLoc(const DeclarationNameLoc & DNLoc,DeclarationName Name)5541 void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5542 DeclarationName Name) {
5543 switch (Name.getNameKind()) {
5544 case DeclarationName::CXXConstructorName:
5545 case DeclarationName::CXXDestructorName:
5546 case DeclarationName::CXXConversionFunctionName:
5547 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
5548 break;
5549
5550 case DeclarationName::CXXOperatorName:
5551 AddSourceLocation(SourceLocation::getFromRawEncoding(
5552 DNLoc.CXXOperatorName.BeginOpNameLoc));
5553 AddSourceLocation(
5554 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
5555 break;
5556
5557 case DeclarationName::CXXLiteralOperatorName:
5558 AddSourceLocation(SourceLocation::getFromRawEncoding(
5559 DNLoc.CXXLiteralOperatorName.OpNameLoc));
5560 break;
5561
5562 case DeclarationName::Identifier:
5563 case DeclarationName::ObjCZeroArgSelector:
5564 case DeclarationName::ObjCOneArgSelector:
5565 case DeclarationName::ObjCMultiArgSelector:
5566 case DeclarationName::CXXUsingDirective:
5567 case DeclarationName::CXXDeductionGuideName:
5568 break;
5569 }
5570 }
5571
AddDeclarationNameInfo(const DeclarationNameInfo & NameInfo)5572 void ASTRecordWriter::AddDeclarationNameInfo(
5573 const DeclarationNameInfo &NameInfo) {
5574 AddDeclarationName(NameInfo.getName());
5575 AddSourceLocation(NameInfo.getLoc());
5576 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
5577 }
5578
AddQualifierInfo(const QualifierInfo & Info)5579 void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5580 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5581 Record->push_back(Info.NumTemplParamLists);
5582 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
5583 AddTemplateParameterList(Info.TemplParamLists[i]);
5584 }
5585
AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)5586 void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
5587 // Nested name specifiers usually aren't too long. I think that 8 would
5588 // typically accommodate the vast majority.
5589 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5590
5591 // Push each of the nested-name-specifiers's onto a stack for
5592 // serialization in reverse order.
5593 while (NNS) {
5594 NestedNames.push_back(NNS);
5595 NNS = NNS.getPrefix();
5596 }
5597
5598 Record->push_back(NestedNames.size());
5599 while(!NestedNames.empty()) {
5600 NNS = NestedNames.pop_back_val();
5601 NestedNameSpecifier::SpecifierKind Kind
5602 = NNS.getNestedNameSpecifier()->getKind();
5603 Record->push_back(Kind);
5604 switch (Kind) {
5605 case NestedNameSpecifier::Identifier:
5606 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5607 AddSourceRange(NNS.getLocalSourceRange());
5608 break;
5609
5610 case NestedNameSpecifier::Namespace:
5611 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5612 AddSourceRange(NNS.getLocalSourceRange());
5613 break;
5614
5615 case NestedNameSpecifier::NamespaceAlias:
5616 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5617 AddSourceRange(NNS.getLocalSourceRange());
5618 break;
5619
5620 case NestedNameSpecifier::TypeSpec:
5621 case NestedNameSpecifier::TypeSpecWithTemplate:
5622 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5623 AddTypeRef(NNS.getTypeLoc().getType());
5624 AddTypeLoc(NNS.getTypeLoc());
5625 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5626 break;
5627
5628 case NestedNameSpecifier::Global:
5629 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
5630 break;
5631
5632 case NestedNameSpecifier::Super:
5633 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5634 AddSourceRange(NNS.getLocalSourceRange());
5635 break;
5636 }
5637 }
5638 }
5639
AddTemplateParameterList(const TemplateParameterList * TemplateParams)5640 void ASTRecordWriter::AddTemplateParameterList(
5641 const TemplateParameterList *TemplateParams) {
5642 assert(TemplateParams && "No TemplateParams!");
5643 AddSourceLocation(TemplateParams->getTemplateLoc());
5644 AddSourceLocation(TemplateParams->getLAngleLoc());
5645 AddSourceLocation(TemplateParams->getRAngleLoc());
5646
5647 Record->push_back(TemplateParams->size());
5648 for (const auto &P : *TemplateParams)
5649 AddDeclRef(P);
5650 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
5651 Record->push_back(true);
5652 AddStmt(const_cast<Expr*>(RequiresClause));
5653 } else {
5654 Record->push_back(false);
5655 }
5656 }
5657
5658 /// Emit a template argument list.
AddTemplateArgumentList(const TemplateArgumentList * TemplateArgs)5659 void ASTRecordWriter::AddTemplateArgumentList(
5660 const TemplateArgumentList *TemplateArgs) {
5661 assert(TemplateArgs && "No TemplateArgs!");
5662 Record->push_back(TemplateArgs->size());
5663 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
5664 AddTemplateArgument(TemplateArgs->get(i));
5665 }
5666
AddASTTemplateArgumentListInfo(const ASTTemplateArgumentListInfo * ASTTemplArgList)5667 void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5668 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
5669 assert(ASTTemplArgList && "No ASTTemplArgList!");
5670 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5671 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5672 Record->push_back(ASTTemplArgList->NumTemplateArgs);
5673 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5674 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5675 AddTemplateArgumentLoc(TemplArgs[i]);
5676 }
5677
AddUnresolvedSet(const ASTUnresolvedSet & Set)5678 void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5679 Record->push_back(Set.size());
5680 for (ASTUnresolvedSet::const_iterator
5681 I = Set.begin(), E = Set.end(); I != E; ++I) {
5682 AddDeclRef(I.getDecl());
5683 Record->push_back(I.getAccess());
5684 }
5685 }
5686
5687 // FIXME: Move this out of the main ASTRecordWriter interface.
AddCXXBaseSpecifier(const CXXBaseSpecifier & Base)5688 void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5689 Record->push_back(Base.isVirtual());
5690 Record->push_back(Base.isBaseOfClass());
5691 Record->push_back(Base.getAccessSpecifierAsWritten());
5692 Record->push_back(Base.getInheritConstructors());
5693 AddTypeSourceInfo(Base.getTypeSourceInfo());
5694 AddSourceRange(Base.getSourceRange());
5695 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5696 : SourceLocation());
5697 }
5698
EmitCXXBaseSpecifiers(ASTWriter & W,ArrayRef<CXXBaseSpecifier> Bases)5699 static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5700 ArrayRef<CXXBaseSpecifier> Bases) {
5701 ASTWriter::RecordData Record;
5702 ASTRecordWriter Writer(W, Record);
5703 Writer.push_back(Bases.size());
5704
5705 for (auto &Base : Bases)
5706 Writer.AddCXXBaseSpecifier(Base);
5707
5708 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
5709 }
5710
5711 // FIXME: Move this out of the main ASTRecordWriter interface.
AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases)5712 void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5713 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5714 }
5715
5716 static uint64_t
EmitCXXCtorInitializers(ASTWriter & W,ArrayRef<CXXCtorInitializer * > CtorInits)5717 EmitCXXCtorInitializers(ASTWriter &W,
5718 ArrayRef<CXXCtorInitializer *> CtorInits) {
5719 ASTWriter::RecordData Record;
5720 ASTRecordWriter Writer(W, Record);
5721 Writer.push_back(CtorInits.size());
5722
5723 for (auto *Init : CtorInits) {
5724 if (Init->isBaseInitializer()) {
5725 Writer.push_back(CTOR_INITIALIZER_BASE);
5726 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5727 Writer.push_back(Init->isBaseVirtual());
5728 } else if (Init->isDelegatingInitializer()) {
5729 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5730 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5731 } else if (Init->isMemberInitializer()){
5732 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5733 Writer.AddDeclRef(Init->getMember());
5734 } else {
5735 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5736 Writer.AddDeclRef(Init->getIndirectMember());
5737 }
5738
5739 Writer.AddSourceLocation(Init->getMemberLocation());
5740 Writer.AddStmt(Init->getInit());
5741 Writer.AddSourceLocation(Init->getLParenLoc());
5742 Writer.AddSourceLocation(Init->getRParenLoc());
5743 Writer.push_back(Init->isWritten());
5744 if (Init->isWritten())
5745 Writer.push_back(Init->getSourceOrder());
5746 }
5747
5748 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
5749 }
5750
5751 // FIXME: Move this out of the main ASTRecordWriter interface.
AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer * > CtorInits)5752 void ASTRecordWriter::AddCXXCtorInitializers(
5753 ArrayRef<CXXCtorInitializer *> CtorInits) {
5754 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
5755 }
5756
AddCXXDefinitionData(const CXXRecordDecl * D)5757 void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
5758 auto &Data = D->data();
5759 Record->push_back(Data.IsLambda);
5760
5761 #define FIELD(Name, Width, Merge) \
5762 Record->push_back(Data.Name);
5763 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
5764
5765 // getODRHash will compute the ODRHash if it has not been previously computed.
5766 Record->push_back(D->getODRHash());
5767 bool ModulesDebugInfo =
5768 Writer->Context->getLangOpts().ModulesDebugInfo && !D->isDependentType();
5769 Record->push_back(ModulesDebugInfo);
5770 if (ModulesDebugInfo)
5771 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
5772
5773 // IsLambda bit is already saved.
5774
5775 Record->push_back(Data.NumBases);
5776 if (Data.NumBases > 0)
5777 AddCXXBaseSpecifiers(Data.bases());
5778
5779 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5780 Record->push_back(Data.NumVBases);
5781 if (Data.NumVBases > 0)
5782 AddCXXBaseSpecifiers(Data.vbases());
5783
5784 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5785 Record->push_back(Data.ComputedVisibleConversions);
5786 if (Data.ComputedVisibleConversions)
5787 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
5788 // Data.Definition is the owning decl, no need to write it.
5789 AddDeclRef(D->getFirstFriend());
5790
5791 // Add lambda-specific data.
5792 if (Data.IsLambda) {
5793 auto &Lambda = D->getLambdaData();
5794 Record->push_back(Lambda.Dependent);
5795 Record->push_back(Lambda.IsGenericLambda);
5796 Record->push_back(Lambda.CaptureDefault);
5797 Record->push_back(Lambda.NumCaptures);
5798 Record->push_back(Lambda.NumExplicitCaptures);
5799 Record->push_back(Lambda.HasKnownInternalLinkage);
5800 Record->push_back(Lambda.ManglingNumber);
5801 AddDeclRef(D->getLambdaContextDecl());
5802 AddTypeSourceInfo(Lambda.MethodTyInfo);
5803 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5804 const LambdaCapture &Capture = Lambda.Captures[I];
5805 AddSourceLocation(Capture.getLocation());
5806 Record->push_back(Capture.isImplicit());
5807 Record->push_back(Capture.getCaptureKind());
5808 switch (Capture.getCaptureKind()) {
5809 case LCK_StarThis:
5810 case LCK_This:
5811 case LCK_VLAType:
5812 break;
5813 case LCK_ByCopy:
5814 case LCK_ByRef:
5815 VarDecl *Var =
5816 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
5817 AddDeclRef(Var);
5818 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5819 : SourceLocation());
5820 break;
5821 }
5822 }
5823 }
5824 }
5825
AddVarDeclInit(const VarDecl * VD)5826 void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
5827 const Expr *Init = VD->getInit();
5828 if (!Init) {
5829 push_back(0);
5830 return;
5831 }
5832
5833 unsigned Val = 1;
5834 if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
5835 Val |= (ES->HasConstantInitialization ? 2 : 0);
5836 Val |= (ES->HasConstantDestruction ? 4 : 0);
5837 // FIXME: Also emit the constant initializer value.
5838 }
5839 push_back(Val);
5840 writeStmtRef(Init);
5841 }
5842
ReaderInitialized(ASTReader * Reader)5843 void ASTWriter::ReaderInitialized(ASTReader *Reader) {
5844 assert(Reader && "Cannot remove chain");
5845 assert((!Chain || Chain == Reader) && "Cannot replace chain");
5846 assert(FirstDeclID == NextDeclID &&
5847 FirstTypeID == NextTypeID &&
5848 FirstIdentID == NextIdentID &&
5849 FirstMacroID == NextMacroID &&
5850 FirstSubmoduleID == NextSubmoduleID &&
5851 FirstSelectorID == NextSelectorID &&
5852 "Setting chain after writing has started.");
5853
5854 Chain = Reader;
5855
5856 // Note, this will get called multiple times, once one the reader starts up
5857 // and again each time it's done reading a PCH or module.
5858 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5859 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5860 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
5861 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
5862 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
5863 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
5864 NextDeclID = FirstDeclID;
5865 NextTypeID = FirstTypeID;
5866 NextIdentID = FirstIdentID;
5867 NextMacroID = FirstMacroID;
5868 NextSelectorID = FirstSelectorID;
5869 NextSubmoduleID = FirstSubmoduleID;
5870 }
5871
IdentifierRead(IdentID ID,IdentifierInfo * II)5872 void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
5873 // Always keep the highest ID. See \p TypeRead() for more information.
5874 IdentID &StoredID = IdentifierIDs[II];
5875 if (ID > StoredID)
5876 StoredID = ID;
5877 }
5878
MacroRead(serialization::MacroID ID,MacroInfo * MI)5879 void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
5880 // Always keep the highest ID. See \p TypeRead() for more information.
5881 MacroID &StoredID = MacroIDs[MI];
5882 if (ID > StoredID)
5883 StoredID = ID;
5884 }
5885
TypeRead(TypeIdx Idx,QualType T)5886 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
5887 // Always take the highest-numbered type index. This copes with an interesting
5888 // case for chained AST writing where we schedule writing the type and then,
5889 // later, deserialize the type from another AST. In this case, we want to
5890 // keep the higher-numbered entry so that we can properly write it out to
5891 // the AST file.
5892 TypeIdx &StoredIdx = TypeIdxs[T];
5893 if (Idx.getIndex() >= StoredIdx.getIndex())
5894 StoredIdx = Idx;
5895 }
5896
SelectorRead(SelectorID ID,Selector S)5897 void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
5898 // Always keep the highest ID. See \p TypeRead() for more information.
5899 SelectorID &StoredID = SelectorIDs[S];
5900 if (ID > StoredID)
5901 StoredID = ID;
5902 }
5903
MacroDefinitionRead(serialization::PreprocessedEntityID ID,MacroDefinitionRecord * MD)5904 void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
5905 MacroDefinitionRecord *MD) {
5906 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
5907 MacroDefinitions[MD] = ID;
5908 }
5909
ModuleRead(serialization::SubmoduleID ID,Module * Mod)5910 void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5911 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5912 SubmoduleIDs[Mod] = ID;
5913 }
5914
CompletedTagDefinition(const TagDecl * D)5915 void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
5916 if (Chain && Chain->isProcessingUpdateRecords()) return;
5917 assert(D->isCompleteDefinition());
5918 assert(!WritingAST && "Already writing the AST!");
5919 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
5920 // We are interested when a PCH decl is modified.
5921 if (RD->isFromASTFile()) {
5922 // A forward reference was mutated into a definition. Rewrite it.
5923 // FIXME: This happens during template instantiation, should we
5924 // have created a new definition decl instead ?
5925 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5926 "completed a tag from another module but not by instantiation?");
5927 DeclUpdates[RD].push_back(
5928 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
5929 }
5930 }
5931 }
5932
isImportedDeclContext(ASTReader * Chain,const Decl * D)5933 static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5934 if (D->isFromASTFile())
5935 return true;
5936
5937 // The predefined __va_list_tag struct is imported if we imported any decls.
5938 // FIXME: This is a gross hack.
5939 return D == D->getASTContext().getVaListTagDecl();
5940 }
5941
AddedVisibleDecl(const DeclContext * DC,const Decl * D)5942 void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5943 if (Chain && Chain->isProcessingUpdateRecords()) return;
5944 assert(DC->isLookupContext() &&
5945 "Should not add lookup results to non-lookup contexts!");
5946
5947 // TU is handled elsewhere.
5948 if (isa<TranslationUnitDecl>(DC))
5949 return;
5950
5951 // Namespaces are handled elsewhere, except for template instantiations of
5952 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5953 // local instantiations are added to an imported context. Only happens when
5954 // adding ADL lookup candidates, for example templated friends.
5955 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5956 !isa<FunctionTemplateDecl>(D))
5957 return;
5958
5959 // We're only interested in cases where a local declaration is added to an
5960 // imported context.
5961 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5962 return;
5963
5964 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
5965 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
5966 assert(!WritingAST && "Already writing the AST!");
5967 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5968 // We're adding a visible declaration to a predefined decl context. Ensure
5969 // that we write out all of its lookup results so we don't get a nasty
5970 // surprise when we try to emit its lookup table.
5971 for (auto *Child : DC->decls())
5972 DeclsToEmitEvenIfUnreferenced.push_back(Child);
5973 }
5974 DeclsToEmitEvenIfUnreferenced.push_back(D);
5975 }
5976
AddedCXXImplicitMember(const CXXRecordDecl * RD,const Decl * D)5977 void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5978 if (Chain && Chain->isProcessingUpdateRecords()) return;
5979 assert(D->isImplicit());
5980
5981 // We're only interested in cases where a local declaration is added to an
5982 // imported context.
5983 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5984 return;
5985
5986 if (!isa<CXXMethodDecl>(D))
5987 return;
5988
5989 // A decl coming from PCH was modified.
5990 assert(RD->isCompleteDefinition());
5991 assert(!WritingAST && "Already writing the AST!");
5992 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
5993 }
5994
ResolvedExceptionSpec(const FunctionDecl * FD)5995 void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5996 if (Chain && Chain->isProcessingUpdateRecords()) return;
5997 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5998 if (!Chain) return;
5999 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6000 // If we don't already know the exception specification for this redecl
6001 // chain, add an update record for it.
6002 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6003 ->getType()
6004 ->castAs<FunctionProtoType>()
6005 ->getExceptionSpecType()))
6006 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6007 });
6008 }
6009
DeducedReturnType(const FunctionDecl * FD,QualType ReturnType)6010 void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
6011 if (Chain && Chain->isProcessingUpdateRecords()) return;
6012 assert(!WritingAST && "Already writing the AST!");
6013 if (!Chain) return;
6014 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
6015 DeclUpdates[D].push_back(
6016 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6017 });
6018 }
6019
ResolvedOperatorDelete(const CXXDestructorDecl * DD,const FunctionDecl * Delete,Expr * ThisArg)6020 void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6021 const FunctionDecl *Delete,
6022 Expr *ThisArg) {
6023 if (Chain && Chain->isProcessingUpdateRecords()) return;
6024 assert(!WritingAST && "Already writing the AST!");
6025 assert(Delete && "Not given an operator delete");
6026 if (!Chain) return;
6027 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
6028 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6029 });
6030 }
6031
CompletedImplicitDefinition(const FunctionDecl * D)6032 void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
6033 if (Chain && Chain->isProcessingUpdateRecords()) return;
6034 assert(!WritingAST && "Already writing the AST!");
6035 if (!D->isFromASTFile())
6036 return; // Declaration not imported from PCH.
6037
6038 // Implicit function decl from a PCH was defined.
6039 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6040 }
6041
VariableDefinitionInstantiated(const VarDecl * D)6042 void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6043 if (Chain && Chain->isProcessingUpdateRecords()) return;
6044 assert(!WritingAST && "Already writing the AST!");
6045 if (!D->isFromASTFile())
6046 return;
6047
6048 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6049 }
6050
FunctionDefinitionInstantiated(const FunctionDecl * D)6051 void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
6052 if (Chain && Chain->isProcessingUpdateRecords()) return;
6053 assert(!WritingAST && "Already writing the AST!");
6054 if (!D->isFromASTFile())
6055 return;
6056
6057 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
6058 }
6059
InstantiationRequested(const ValueDecl * D)6060 void ASTWriter::InstantiationRequested(const ValueDecl *D) {
6061 if (Chain && Chain->isProcessingUpdateRecords()) return;
6062 assert(!WritingAST && "Already writing the AST!");
6063 if (!D->isFromASTFile())
6064 return;
6065
6066 // Since the actual instantiation is delayed, this really means that we need
6067 // to update the instantiation location.
6068 SourceLocation POI;
6069 if (auto *VD = dyn_cast<VarDecl>(D))
6070 POI = VD->getPointOfInstantiation();
6071 else
6072 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6073 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
6074 }
6075
DefaultArgumentInstantiated(const ParmVarDecl * D)6076 void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
6077 if (Chain && Chain->isProcessingUpdateRecords()) return;
6078 assert(!WritingAST && "Already writing the AST!");
6079 if (!D->isFromASTFile())
6080 return;
6081
6082 DeclUpdates[D].push_back(
6083 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6084 }
6085
DefaultMemberInitializerInstantiated(const FieldDecl * D)6086 void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6087 assert(!WritingAST && "Already writing the AST!");
6088 if (!D->isFromASTFile())
6089 return;
6090
6091 DeclUpdates[D].push_back(
6092 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6093 }
6094
AddedObjCCategoryToInterface(const ObjCCategoryDecl * CatD,const ObjCInterfaceDecl * IFD)6095 void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6096 const ObjCInterfaceDecl *IFD) {
6097 if (Chain && Chain->isProcessingUpdateRecords()) return;
6098 assert(!WritingAST && "Already writing the AST!");
6099 if (!IFD->isFromASTFile())
6100 return; // Declaration not imported from PCH.
6101
6102 assert(IFD->getDefinition() && "Category on a class without a definition?");
6103 ObjCClassesWithCategories.insert(
6104 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
6105 }
6106
DeclarationMarkedUsed(const Decl * D)6107 void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
6108 if (Chain && Chain->isProcessingUpdateRecords()) return;
6109 assert(!WritingAST && "Already writing the AST!");
6110
6111 // If there is *any* declaration of the entity that's not from an AST file,
6112 // we can skip writing the update record. We make sure that isUsed() triggers
6113 // completion of the redeclaration chain of the entity.
6114 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6115 if (IsLocalDecl(Prev))
6116 return;
6117
6118 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
6119 }
6120
DeclarationMarkedOpenMPThreadPrivate(const Decl * D)6121 void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
6122 if (Chain && Chain->isProcessingUpdateRecords()) return;
6123 assert(!WritingAST && "Already writing the AST!");
6124 if (!D->isFromASTFile())
6125 return;
6126
6127 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6128 }
6129
DeclarationMarkedOpenMPAllocate(const Decl * D,const Attr * A)6130 void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
6131 if (Chain && Chain->isProcessingUpdateRecords()) return;
6132 assert(!WritingAST && "Already writing the AST!");
6133 if (!D->isFromASTFile())
6134 return;
6135
6136 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
6137 }
6138
DeclarationMarkedOpenMPDeclareTarget(const Decl * D,const Attr * Attr)6139 void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6140 const Attr *Attr) {
6141 if (Chain && Chain->isProcessingUpdateRecords()) return;
6142 assert(!WritingAST && "Already writing the AST!");
6143 if (!D->isFromASTFile())
6144 return;
6145
6146 DeclUpdates[D].push_back(
6147 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
6148 }
6149
RedefinedHiddenDefinition(const NamedDecl * D,Module * M)6150 void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
6151 if (Chain && Chain->isProcessingUpdateRecords()) return;
6152 assert(!WritingAST && "Already writing the AST!");
6153 assert(!D->isUnconditionallyVisible() && "expected a hidden declaration");
6154 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
6155 }
6156
AddedAttributeToRecord(const Attr * Attr,const RecordDecl * Record)6157 void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6158 const RecordDecl *Record) {
6159 if (Chain && Chain->isProcessingUpdateRecords()) return;
6160 assert(!WritingAST && "Already writing the AST!");
6161 if (!Record->isFromASTFile())
6162 return;
6163 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6164 }
6165
AddedCXXTemplateSpecialization(const ClassTemplateDecl * TD,const ClassTemplateSpecializationDecl * D)6166 void ASTWriter::AddedCXXTemplateSpecialization(
6167 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6168 assert(!WritingAST && "Already writing the AST!");
6169
6170 if (!TD->getFirstDecl()->isFromASTFile())
6171 return;
6172 if (Chain && Chain->isProcessingUpdateRecords())
6173 return;
6174
6175 DeclsToEmitEvenIfUnreferenced.push_back(D);
6176 }
6177
AddedCXXTemplateSpecialization(const VarTemplateDecl * TD,const VarTemplateSpecializationDecl * D)6178 void ASTWriter::AddedCXXTemplateSpecialization(
6179 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6180 assert(!WritingAST && "Already writing the AST!");
6181
6182 if (!TD->getFirstDecl()->isFromASTFile())
6183 return;
6184 if (Chain && Chain->isProcessingUpdateRecords())
6185 return;
6186
6187 DeclsToEmitEvenIfUnreferenced.push_back(D);
6188 }
6189
AddedCXXTemplateSpecialization(const FunctionTemplateDecl * TD,const FunctionDecl * D)6190 void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6191 const FunctionDecl *D) {
6192 assert(!WritingAST && "Already writing the AST!");
6193
6194 if (!TD->getFirstDecl()->isFromASTFile())
6195 return;
6196 if (Chain && Chain->isProcessingUpdateRecords())
6197 return;
6198
6199 DeclsToEmitEvenIfUnreferenced.push_back(D);
6200 }
6201
6202 //===----------------------------------------------------------------------===//
6203 //// OMPClause Serialization
6204 ////===----------------------------------------------------------------------===//
6205
6206 namespace {
6207
6208 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
6209 ASTRecordWriter &Record;
6210
6211 public:
OMPClauseWriter(ASTRecordWriter & Record)6212 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
6213 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
6214 #include "llvm/Frontend/OpenMP/OMPKinds.def"
6215 void writeClause(OMPClause *C);
6216 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
6217 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
6218 };
6219
6220 }
6221
writeOMPClause(OMPClause * C)6222 void ASTRecordWriter::writeOMPClause(OMPClause *C) {
6223 OMPClauseWriter(*this).writeClause(C);
6224 }
6225
writeClause(OMPClause * C)6226 void OMPClauseWriter::writeClause(OMPClause *C) {
6227 Record.push_back(unsigned(C->getClauseKind()));
6228 Visit(C);
6229 Record.AddSourceLocation(C->getBeginLoc());
6230 Record.AddSourceLocation(C->getEndLoc());
6231 }
6232
VisitOMPClauseWithPreInit(OMPClauseWithPreInit * C)6233 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
6234 Record.push_back(uint64_t(C->getCaptureRegion()));
6235 Record.AddStmt(C->getPreInitStmt());
6236 }
6237
VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate * C)6238 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6239 VisitOMPClauseWithPreInit(C);
6240 Record.AddStmt(C->getPostUpdateExpr());
6241 }
6242
VisitOMPIfClause(OMPIfClause * C)6243 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6244 VisitOMPClauseWithPreInit(C);
6245 Record.push_back(uint64_t(C->getNameModifier()));
6246 Record.AddSourceLocation(C->getNameModifierLoc());
6247 Record.AddSourceLocation(C->getColonLoc());
6248 Record.AddStmt(C->getCondition());
6249 Record.AddSourceLocation(C->getLParenLoc());
6250 }
6251
VisitOMPFinalClause(OMPFinalClause * C)6252 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
6253 VisitOMPClauseWithPreInit(C);
6254 Record.AddStmt(C->getCondition());
6255 Record.AddSourceLocation(C->getLParenLoc());
6256 }
6257
VisitOMPNumThreadsClause(OMPNumThreadsClause * C)6258 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6259 VisitOMPClauseWithPreInit(C);
6260 Record.AddStmt(C->getNumThreads());
6261 Record.AddSourceLocation(C->getLParenLoc());
6262 }
6263
VisitOMPSafelenClause(OMPSafelenClause * C)6264 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6265 Record.AddStmt(C->getSafelen());
6266 Record.AddSourceLocation(C->getLParenLoc());
6267 }
6268
VisitOMPSimdlenClause(OMPSimdlenClause * C)6269 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6270 Record.AddStmt(C->getSimdlen());
6271 Record.AddSourceLocation(C->getLParenLoc());
6272 }
6273
VisitOMPAllocatorClause(OMPAllocatorClause * C)6274 void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6275 Record.AddStmt(C->getAllocator());
6276 Record.AddSourceLocation(C->getLParenLoc());
6277 }
6278
VisitOMPCollapseClause(OMPCollapseClause * C)6279 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6280 Record.AddStmt(C->getNumForLoops());
6281 Record.AddSourceLocation(C->getLParenLoc());
6282 }
6283
VisitOMPDetachClause(OMPDetachClause * C)6284 void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
6285 Record.AddStmt(C->getEventHandler());
6286 Record.AddSourceLocation(C->getLParenLoc());
6287 }
6288
VisitOMPDefaultClause(OMPDefaultClause * C)6289 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
6290 Record.push_back(unsigned(C->getDefaultKind()));
6291 Record.AddSourceLocation(C->getLParenLoc());
6292 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6293 }
6294
VisitOMPProcBindClause(OMPProcBindClause * C)6295 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
6296 Record.push_back(unsigned(C->getProcBindKind()));
6297 Record.AddSourceLocation(C->getLParenLoc());
6298 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6299 }
6300
VisitOMPScheduleClause(OMPScheduleClause * C)6301 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6302 VisitOMPClauseWithPreInit(C);
6303 Record.push_back(C->getScheduleKind());
6304 Record.push_back(C->getFirstScheduleModifier());
6305 Record.push_back(C->getSecondScheduleModifier());
6306 Record.AddStmt(C->getChunkSize());
6307 Record.AddSourceLocation(C->getLParenLoc());
6308 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6309 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6310 Record.AddSourceLocation(C->getScheduleKindLoc());
6311 Record.AddSourceLocation(C->getCommaLoc());
6312 }
6313
VisitOMPOrderedClause(OMPOrderedClause * C)6314 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6315 Record.push_back(C->getLoopNumIterations().size());
6316 Record.AddStmt(C->getNumForLoops());
6317 for (Expr *NumIter : C->getLoopNumIterations())
6318 Record.AddStmt(NumIter);
6319 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
6320 Record.AddStmt(C->getLoopCounter(I));
6321 Record.AddSourceLocation(C->getLParenLoc());
6322 }
6323
VisitOMPNowaitClause(OMPNowaitClause *)6324 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6325
VisitOMPUntiedClause(OMPUntiedClause *)6326 void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6327
VisitOMPMergeableClause(OMPMergeableClause *)6328 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6329
VisitOMPReadClause(OMPReadClause *)6330 void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6331
VisitOMPWriteClause(OMPWriteClause *)6332 void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6333
VisitOMPUpdateClause(OMPUpdateClause * C)6334 void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
6335 Record.push_back(C->isExtended() ? 1 : 0);
6336 if (C->isExtended()) {
6337 Record.AddSourceLocation(C->getLParenLoc());
6338 Record.AddSourceLocation(C->getArgumentLoc());
6339 Record.writeEnum(C->getDependencyKind());
6340 }
6341 }
6342
VisitOMPCaptureClause(OMPCaptureClause *)6343 void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6344
VisitOMPSeqCstClause(OMPSeqCstClause *)6345 void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6346
VisitOMPAcqRelClause(OMPAcqRelClause *)6347 void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
6348
VisitOMPAcquireClause(OMPAcquireClause *)6349 void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
6350
VisitOMPReleaseClause(OMPReleaseClause *)6351 void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
6352
VisitOMPRelaxedClause(OMPRelaxedClause *)6353 void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
6354
VisitOMPThreadsClause(OMPThreadsClause *)6355 void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6356
VisitOMPSIMDClause(OMPSIMDClause *)6357 void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6358
VisitOMPNogroupClause(OMPNogroupClause *)6359 void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6360
VisitOMPDestroyClause(OMPDestroyClause *)6361 void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *) {}
6362
VisitOMPPrivateClause(OMPPrivateClause * C)6363 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6364 Record.push_back(C->varlist_size());
6365 Record.AddSourceLocation(C->getLParenLoc());
6366 for (auto *VE : C->varlists()) {
6367 Record.AddStmt(VE);
6368 }
6369 for (auto *VE : C->private_copies()) {
6370 Record.AddStmt(VE);
6371 }
6372 }
6373
VisitOMPFirstprivateClause(OMPFirstprivateClause * C)6374 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6375 Record.push_back(C->varlist_size());
6376 VisitOMPClauseWithPreInit(C);
6377 Record.AddSourceLocation(C->getLParenLoc());
6378 for (auto *VE : C->varlists()) {
6379 Record.AddStmt(VE);
6380 }
6381 for (auto *VE : C->private_copies()) {
6382 Record.AddStmt(VE);
6383 }
6384 for (auto *VE : C->inits()) {
6385 Record.AddStmt(VE);
6386 }
6387 }
6388
VisitOMPLastprivateClause(OMPLastprivateClause * C)6389 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6390 Record.push_back(C->varlist_size());
6391 VisitOMPClauseWithPostUpdate(C);
6392 Record.AddSourceLocation(C->getLParenLoc());
6393 Record.writeEnum(C->getKind());
6394 Record.AddSourceLocation(C->getKindLoc());
6395 Record.AddSourceLocation(C->getColonLoc());
6396 for (auto *VE : C->varlists())
6397 Record.AddStmt(VE);
6398 for (auto *E : C->private_copies())
6399 Record.AddStmt(E);
6400 for (auto *E : C->source_exprs())
6401 Record.AddStmt(E);
6402 for (auto *E : C->destination_exprs())
6403 Record.AddStmt(E);
6404 for (auto *E : C->assignment_ops())
6405 Record.AddStmt(E);
6406 }
6407
VisitOMPSharedClause(OMPSharedClause * C)6408 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6409 Record.push_back(C->varlist_size());
6410 Record.AddSourceLocation(C->getLParenLoc());
6411 for (auto *VE : C->varlists())
6412 Record.AddStmt(VE);
6413 }
6414
VisitOMPReductionClause(OMPReductionClause * C)6415 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6416 Record.push_back(C->varlist_size());
6417 Record.writeEnum(C->getModifier());
6418 VisitOMPClauseWithPostUpdate(C);
6419 Record.AddSourceLocation(C->getLParenLoc());
6420 Record.AddSourceLocation(C->getModifierLoc());
6421 Record.AddSourceLocation(C->getColonLoc());
6422 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6423 Record.AddDeclarationNameInfo(C->getNameInfo());
6424 for (auto *VE : C->varlists())
6425 Record.AddStmt(VE);
6426 for (auto *VE : C->privates())
6427 Record.AddStmt(VE);
6428 for (auto *E : C->lhs_exprs())
6429 Record.AddStmt(E);
6430 for (auto *E : C->rhs_exprs())
6431 Record.AddStmt(E);
6432 for (auto *E : C->reduction_ops())
6433 Record.AddStmt(E);
6434 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
6435 for (auto *E : C->copy_ops())
6436 Record.AddStmt(E);
6437 for (auto *E : C->copy_array_temps())
6438 Record.AddStmt(E);
6439 for (auto *E : C->copy_array_elems())
6440 Record.AddStmt(E);
6441 }
6442 }
6443
VisitOMPTaskReductionClause(OMPTaskReductionClause * C)6444 void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6445 Record.push_back(C->varlist_size());
6446 VisitOMPClauseWithPostUpdate(C);
6447 Record.AddSourceLocation(C->getLParenLoc());
6448 Record.AddSourceLocation(C->getColonLoc());
6449 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6450 Record.AddDeclarationNameInfo(C->getNameInfo());
6451 for (auto *VE : C->varlists())
6452 Record.AddStmt(VE);
6453 for (auto *VE : C->privates())
6454 Record.AddStmt(VE);
6455 for (auto *E : C->lhs_exprs())
6456 Record.AddStmt(E);
6457 for (auto *E : C->rhs_exprs())
6458 Record.AddStmt(E);
6459 for (auto *E : C->reduction_ops())
6460 Record.AddStmt(E);
6461 }
6462
VisitOMPInReductionClause(OMPInReductionClause * C)6463 void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6464 Record.push_back(C->varlist_size());
6465 VisitOMPClauseWithPostUpdate(C);
6466 Record.AddSourceLocation(C->getLParenLoc());
6467 Record.AddSourceLocation(C->getColonLoc());
6468 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6469 Record.AddDeclarationNameInfo(C->getNameInfo());
6470 for (auto *VE : C->varlists())
6471 Record.AddStmt(VE);
6472 for (auto *VE : C->privates())
6473 Record.AddStmt(VE);
6474 for (auto *E : C->lhs_exprs())
6475 Record.AddStmt(E);
6476 for (auto *E : C->rhs_exprs())
6477 Record.AddStmt(E);
6478 for (auto *E : C->reduction_ops())
6479 Record.AddStmt(E);
6480 for (auto *E : C->taskgroup_descriptors())
6481 Record.AddStmt(E);
6482 }
6483
VisitOMPLinearClause(OMPLinearClause * C)6484 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6485 Record.push_back(C->varlist_size());
6486 VisitOMPClauseWithPostUpdate(C);
6487 Record.AddSourceLocation(C->getLParenLoc());
6488 Record.AddSourceLocation(C->getColonLoc());
6489 Record.push_back(C->getModifier());
6490 Record.AddSourceLocation(C->getModifierLoc());
6491 for (auto *VE : C->varlists()) {
6492 Record.AddStmt(VE);
6493 }
6494 for (auto *VE : C->privates()) {
6495 Record.AddStmt(VE);
6496 }
6497 for (auto *VE : C->inits()) {
6498 Record.AddStmt(VE);
6499 }
6500 for (auto *VE : C->updates()) {
6501 Record.AddStmt(VE);
6502 }
6503 for (auto *VE : C->finals()) {
6504 Record.AddStmt(VE);
6505 }
6506 Record.AddStmt(C->getStep());
6507 Record.AddStmt(C->getCalcStep());
6508 for (auto *VE : C->used_expressions())
6509 Record.AddStmt(VE);
6510 }
6511
VisitOMPAlignedClause(OMPAlignedClause * C)6512 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6513 Record.push_back(C->varlist_size());
6514 Record.AddSourceLocation(C->getLParenLoc());
6515 Record.AddSourceLocation(C->getColonLoc());
6516 for (auto *VE : C->varlists())
6517 Record.AddStmt(VE);
6518 Record.AddStmt(C->getAlignment());
6519 }
6520
VisitOMPCopyinClause(OMPCopyinClause * C)6521 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6522 Record.push_back(C->varlist_size());
6523 Record.AddSourceLocation(C->getLParenLoc());
6524 for (auto *VE : C->varlists())
6525 Record.AddStmt(VE);
6526 for (auto *E : C->source_exprs())
6527 Record.AddStmt(E);
6528 for (auto *E : C->destination_exprs())
6529 Record.AddStmt(E);
6530 for (auto *E : C->assignment_ops())
6531 Record.AddStmt(E);
6532 }
6533
VisitOMPCopyprivateClause(OMPCopyprivateClause * C)6534 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6535 Record.push_back(C->varlist_size());
6536 Record.AddSourceLocation(C->getLParenLoc());
6537 for (auto *VE : C->varlists())
6538 Record.AddStmt(VE);
6539 for (auto *E : C->source_exprs())
6540 Record.AddStmt(E);
6541 for (auto *E : C->destination_exprs())
6542 Record.AddStmt(E);
6543 for (auto *E : C->assignment_ops())
6544 Record.AddStmt(E);
6545 }
6546
VisitOMPFlushClause(OMPFlushClause * C)6547 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6548 Record.push_back(C->varlist_size());
6549 Record.AddSourceLocation(C->getLParenLoc());
6550 for (auto *VE : C->varlists())
6551 Record.AddStmt(VE);
6552 }
6553
VisitOMPDepobjClause(OMPDepobjClause * C)6554 void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
6555 Record.AddStmt(C->getDepobj());
6556 Record.AddSourceLocation(C->getLParenLoc());
6557 }
6558
VisitOMPDependClause(OMPDependClause * C)6559 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6560 Record.push_back(C->varlist_size());
6561 Record.push_back(C->getNumLoops());
6562 Record.AddSourceLocation(C->getLParenLoc());
6563 Record.AddStmt(C->getModifier());
6564 Record.push_back(C->getDependencyKind());
6565 Record.AddSourceLocation(C->getDependencyLoc());
6566 Record.AddSourceLocation(C->getColonLoc());
6567 for (auto *VE : C->varlists())
6568 Record.AddStmt(VE);
6569 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6570 Record.AddStmt(C->getLoopData(I));
6571 }
6572
VisitOMPDeviceClause(OMPDeviceClause * C)6573 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6574 VisitOMPClauseWithPreInit(C);
6575 Record.writeEnum(C->getModifier());
6576 Record.AddStmt(C->getDevice());
6577 Record.AddSourceLocation(C->getModifierLoc());
6578 Record.AddSourceLocation(C->getLParenLoc());
6579 }
6580
VisitOMPMapClause(OMPMapClause * C)6581 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6582 Record.push_back(C->varlist_size());
6583 Record.push_back(C->getUniqueDeclarationsNum());
6584 Record.push_back(C->getTotalComponentListNum());
6585 Record.push_back(C->getTotalComponentsNum());
6586 Record.AddSourceLocation(C->getLParenLoc());
6587 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
6588 Record.push_back(C->getMapTypeModifier(I));
6589 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6590 }
6591 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6592 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
6593 Record.push_back(C->getMapType());
6594 Record.AddSourceLocation(C->getMapLoc());
6595 Record.AddSourceLocation(C->getColonLoc());
6596 for (auto *E : C->varlists())
6597 Record.AddStmt(E);
6598 for (auto *E : C->mapperlists())
6599 Record.AddStmt(E);
6600 for (auto *D : C->all_decls())
6601 Record.AddDeclRef(D);
6602 for (auto N : C->all_num_lists())
6603 Record.push_back(N);
6604 for (auto N : C->all_lists_sizes())
6605 Record.push_back(N);
6606 for (auto &M : C->all_components()) {
6607 Record.AddStmt(M.getAssociatedExpression());
6608 Record.AddDeclRef(M.getAssociatedDeclaration());
6609 }
6610 }
6611
VisitOMPAllocateClause(OMPAllocateClause * C)6612 void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6613 Record.push_back(C->varlist_size());
6614 Record.AddSourceLocation(C->getLParenLoc());
6615 Record.AddSourceLocation(C->getColonLoc());
6616 Record.AddStmt(C->getAllocator());
6617 for (auto *VE : C->varlists())
6618 Record.AddStmt(VE);
6619 }
6620
VisitOMPNumTeamsClause(OMPNumTeamsClause * C)6621 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6622 VisitOMPClauseWithPreInit(C);
6623 Record.AddStmt(C->getNumTeams());
6624 Record.AddSourceLocation(C->getLParenLoc());
6625 }
6626
VisitOMPThreadLimitClause(OMPThreadLimitClause * C)6627 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6628 VisitOMPClauseWithPreInit(C);
6629 Record.AddStmt(C->getThreadLimit());
6630 Record.AddSourceLocation(C->getLParenLoc());
6631 }
6632
VisitOMPPriorityClause(OMPPriorityClause * C)6633 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
6634 VisitOMPClauseWithPreInit(C);
6635 Record.AddStmt(C->getPriority());
6636 Record.AddSourceLocation(C->getLParenLoc());
6637 }
6638
VisitOMPGrainsizeClause(OMPGrainsizeClause * C)6639 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
6640 VisitOMPClauseWithPreInit(C);
6641 Record.AddStmt(C->getGrainsize());
6642 Record.AddSourceLocation(C->getLParenLoc());
6643 }
6644
VisitOMPNumTasksClause(OMPNumTasksClause * C)6645 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
6646 VisitOMPClauseWithPreInit(C);
6647 Record.AddStmt(C->getNumTasks());
6648 Record.AddSourceLocation(C->getLParenLoc());
6649 }
6650
VisitOMPHintClause(OMPHintClause * C)6651 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
6652 Record.AddStmt(C->getHint());
6653 Record.AddSourceLocation(C->getLParenLoc());
6654 }
6655
VisitOMPDistScheduleClause(OMPDistScheduleClause * C)6656 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
6657 VisitOMPClauseWithPreInit(C);
6658 Record.push_back(C->getDistScheduleKind());
6659 Record.AddStmt(C->getChunkSize());
6660 Record.AddSourceLocation(C->getLParenLoc());
6661 Record.AddSourceLocation(C->getDistScheduleKindLoc());
6662 Record.AddSourceLocation(C->getCommaLoc());
6663 }
6664
VisitOMPDefaultmapClause(OMPDefaultmapClause * C)6665 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
6666 Record.push_back(C->getDefaultmapKind());
6667 Record.push_back(C->getDefaultmapModifier());
6668 Record.AddSourceLocation(C->getLParenLoc());
6669 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
6670 Record.AddSourceLocation(C->getDefaultmapKindLoc());
6671 }
6672
VisitOMPToClause(OMPToClause * C)6673 void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
6674 Record.push_back(C->varlist_size());
6675 Record.push_back(C->getUniqueDeclarationsNum());
6676 Record.push_back(C->getTotalComponentListNum());
6677 Record.push_back(C->getTotalComponentsNum());
6678 Record.AddSourceLocation(C->getLParenLoc());
6679 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
6680 Record.push_back(C->getMotionModifier(I));
6681 Record.AddSourceLocation(C->getMotionModifierLoc(I));
6682 }
6683 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6684 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
6685 Record.AddSourceLocation(C->getColonLoc());
6686 for (auto *E : C->varlists())
6687 Record.AddStmt(E);
6688 for (auto *E : C->mapperlists())
6689 Record.AddStmt(E);
6690 for (auto *D : C->all_decls())
6691 Record.AddDeclRef(D);
6692 for (auto N : C->all_num_lists())
6693 Record.push_back(N);
6694 for (auto N : C->all_lists_sizes())
6695 Record.push_back(N);
6696 for (auto &M : C->all_components()) {
6697 Record.AddStmt(M.getAssociatedExpression());
6698 Record.writeBool(M.isNonContiguous());
6699 Record.AddDeclRef(M.getAssociatedDeclaration());
6700 }
6701 }
6702
VisitOMPFromClause(OMPFromClause * C)6703 void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
6704 Record.push_back(C->varlist_size());
6705 Record.push_back(C->getUniqueDeclarationsNum());
6706 Record.push_back(C->getTotalComponentListNum());
6707 Record.push_back(C->getTotalComponentsNum());
6708 Record.AddSourceLocation(C->getLParenLoc());
6709 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
6710 Record.push_back(C->getMotionModifier(I));
6711 Record.AddSourceLocation(C->getMotionModifierLoc(I));
6712 }
6713 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6714 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
6715 Record.AddSourceLocation(C->getColonLoc());
6716 for (auto *E : C->varlists())
6717 Record.AddStmt(E);
6718 for (auto *E : C->mapperlists())
6719 Record.AddStmt(E);
6720 for (auto *D : C->all_decls())
6721 Record.AddDeclRef(D);
6722 for (auto N : C->all_num_lists())
6723 Record.push_back(N);
6724 for (auto N : C->all_lists_sizes())
6725 Record.push_back(N);
6726 for (auto &M : C->all_components()) {
6727 Record.AddStmt(M.getAssociatedExpression());
6728 Record.writeBool(M.isNonContiguous());
6729 Record.AddDeclRef(M.getAssociatedDeclaration());
6730 }
6731 }
6732
VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause * C)6733 void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
6734 Record.push_back(C->varlist_size());
6735 Record.push_back(C->getUniqueDeclarationsNum());
6736 Record.push_back(C->getTotalComponentListNum());
6737 Record.push_back(C->getTotalComponentsNum());
6738 Record.AddSourceLocation(C->getLParenLoc());
6739 for (auto *E : C->varlists())
6740 Record.AddStmt(E);
6741 for (auto *VE : C->private_copies())
6742 Record.AddStmt(VE);
6743 for (auto *VE : C->inits())
6744 Record.AddStmt(VE);
6745 for (auto *D : C->all_decls())
6746 Record.AddDeclRef(D);
6747 for (auto N : C->all_num_lists())
6748 Record.push_back(N);
6749 for (auto N : C->all_lists_sizes())
6750 Record.push_back(N);
6751 for (auto &M : C->all_components()) {
6752 Record.AddStmt(M.getAssociatedExpression());
6753 Record.AddDeclRef(M.getAssociatedDeclaration());
6754 }
6755 }
6756
VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause * C)6757 void OMPClauseWriter::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
6758 Record.push_back(C->varlist_size());
6759 Record.push_back(C->getUniqueDeclarationsNum());
6760 Record.push_back(C->getTotalComponentListNum());
6761 Record.push_back(C->getTotalComponentsNum());
6762 Record.AddSourceLocation(C->getLParenLoc());
6763 for (auto *E : C->varlists())
6764 Record.AddStmt(E);
6765 for (auto *D : C->all_decls())
6766 Record.AddDeclRef(D);
6767 for (auto N : C->all_num_lists())
6768 Record.push_back(N);
6769 for (auto N : C->all_lists_sizes())
6770 Record.push_back(N);
6771 for (auto &M : C->all_components()) {
6772 Record.AddStmt(M.getAssociatedExpression());
6773 Record.AddDeclRef(M.getAssociatedDeclaration());
6774 }
6775 }
6776
VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause * C)6777 void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
6778 Record.push_back(C->varlist_size());
6779 Record.push_back(C->getUniqueDeclarationsNum());
6780 Record.push_back(C->getTotalComponentListNum());
6781 Record.push_back(C->getTotalComponentsNum());
6782 Record.AddSourceLocation(C->getLParenLoc());
6783 for (auto *E : C->varlists())
6784 Record.AddStmt(E);
6785 for (auto *D : C->all_decls())
6786 Record.AddDeclRef(D);
6787 for (auto N : C->all_num_lists())
6788 Record.push_back(N);
6789 for (auto N : C->all_lists_sizes())
6790 Record.push_back(N);
6791 for (auto &M : C->all_components()) {
6792 Record.AddStmt(M.getAssociatedExpression());
6793 Record.AddDeclRef(M.getAssociatedDeclaration());
6794 }
6795 }
6796
VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *)6797 void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
6798
VisitOMPUnifiedSharedMemoryClause(OMPUnifiedSharedMemoryClause *)6799 void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
6800 OMPUnifiedSharedMemoryClause *) {}
6801
VisitOMPReverseOffloadClause(OMPReverseOffloadClause *)6802 void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
6803
6804 void
VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *)6805 OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
6806 }
6807
VisitOMPAtomicDefaultMemOrderClause(OMPAtomicDefaultMemOrderClause * C)6808 void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
6809 OMPAtomicDefaultMemOrderClause *C) {
6810 Record.push_back(C->getAtomicDefaultMemOrderKind());
6811 Record.AddSourceLocation(C->getLParenLoc());
6812 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
6813 }
6814
VisitOMPNontemporalClause(OMPNontemporalClause * C)6815 void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
6816 Record.push_back(C->varlist_size());
6817 Record.AddSourceLocation(C->getLParenLoc());
6818 for (auto *VE : C->varlists())
6819 Record.AddStmt(VE);
6820 for (auto *E : C->private_refs())
6821 Record.AddStmt(E);
6822 }
6823
VisitOMPInclusiveClause(OMPInclusiveClause * C)6824 void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
6825 Record.push_back(C->varlist_size());
6826 Record.AddSourceLocation(C->getLParenLoc());
6827 for (auto *VE : C->varlists())
6828 Record.AddStmt(VE);
6829 }
6830
VisitOMPExclusiveClause(OMPExclusiveClause * C)6831 void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
6832 Record.push_back(C->varlist_size());
6833 Record.AddSourceLocation(C->getLParenLoc());
6834 for (auto *VE : C->varlists())
6835 Record.AddStmt(VE);
6836 }
6837
VisitOMPOrderClause(OMPOrderClause * C)6838 void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
6839 Record.writeEnum(C->getKind());
6840 Record.AddSourceLocation(C->getLParenLoc());
6841 Record.AddSourceLocation(C->getKindKwLoc());
6842 }
6843
VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause * C)6844 void OMPClauseWriter::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
6845 Record.push_back(C->getNumberOfAllocators());
6846 Record.AddSourceLocation(C->getLParenLoc());
6847 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
6848 OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I);
6849 Record.AddStmt(Data.Allocator);
6850 Record.AddStmt(Data.AllocatorTraits);
6851 Record.AddSourceLocation(Data.LParenLoc);
6852 Record.AddSourceLocation(Data.RParenLoc);
6853 }
6854 }
6855
VisitOMPAffinityClause(OMPAffinityClause * C)6856 void OMPClauseWriter::VisitOMPAffinityClause(OMPAffinityClause *C) {
6857 Record.push_back(C->varlist_size());
6858 Record.AddSourceLocation(C->getLParenLoc());
6859 Record.AddStmt(C->getModifier());
6860 Record.AddSourceLocation(C->getColonLoc());
6861 for (Expr *E : C->varlists())
6862 Record.AddStmt(E);
6863 }
6864
writeOMPTraitInfo(const OMPTraitInfo * TI)6865 void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
6866 writeUInt32(TI->Sets.size());
6867 for (const auto &Set : TI->Sets) {
6868 writeEnum(Set.Kind);
6869 writeUInt32(Set.Selectors.size());
6870 for (const auto &Selector : Set.Selectors) {
6871 writeEnum(Selector.Kind);
6872 writeBool(Selector.ScoreOrCondition);
6873 if (Selector.ScoreOrCondition)
6874 writeExprRef(Selector.ScoreOrCondition);
6875 writeUInt32(Selector.Properties.size());
6876 for (const auto &Property : Selector.Properties)
6877 writeEnum(Property.Kind);
6878 }
6879 }
6880 }
6881
writeOMPChildren(OMPChildren * Data)6882 void ASTRecordWriter::writeOMPChildren(OMPChildren *Data) {
6883 if (!Data)
6884 return;
6885 writeUInt32(Data->getNumClauses());
6886 writeUInt32(Data->getNumChildren());
6887 writeBool(Data->hasAssociatedStmt());
6888 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
6889 writeOMPClause(Data->getClauses()[I]);
6890 if (Data->hasAssociatedStmt())
6891 AddStmt(Data->getAssociatedStmt());
6892 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
6893 AddStmt(Data->getChildren()[I]);
6894 }
6895