blob: bf893c7ff3bffe8e273a8cb23bc8c8b230c681b0 [file] [log] [blame]
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001//===- ASTWriter.cpp - AST File Writer ------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006//
7//===----------------------------------------------------------------------===//
8//
Sebastian Redl55c0ad52010-08-18 23:56:21 +00009// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000010//
11//===----------------------------------------------------------------------===//
12
Alexey Bataevb6e70842019-12-16 15:54:17 -050013#include "clang/AST/OpenMPClause.h"
John McCall2ac702a2019-12-14 03:17:03 -050014#include "clang/Serialization/ASTRecordWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000016#include "ASTReaderInternals.h"
17#include "MultiOnDiskHashTable.h"
John McCalld505e572019-12-13 21:54:44 -050018#include "clang/AST/AbstractTypeWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000020#include "clang/AST/ASTUnresolvedSet.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000021#include "clang/AST/Attr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/Decl.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000023#include "clang/AST/DeclBase.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000024#include "clang/AST/DeclCXX.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000025#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000026#include "clang/AST/DeclFriend.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000027#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "clang/AST/DeclTemplate.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000029#include "clang/AST/DeclarationName.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000030#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000031#include "clang/AST/ExprCXX.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000032#include "clang/AST/LambdaCapture.h"
33#include "clang/AST/NestedNameSpecifier.h"
34#include "clang/AST/RawCommentList.h"
35#include "clang/AST/TemplateName.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000036#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000037#include "clang/AST/TypeLocVisitor.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000038#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000039#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000040#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000041#include "clang/Basic/FileSystemOptions.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000042#include "clang/Basic/IdentifierTable.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000043#include "clang/Basic/LLVM.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000044#include "clang/Basic/Lambda.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000045#include "clang/Basic/LangOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000046#include "clang/Basic/Module.h"
47#include "clang/Basic/ObjCRuntime.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000048#include "clang/Basic/OpenCLOptions.h"
49#include "clang/Basic/SourceLocation.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000050#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000051#include "clang/Basic/SourceManagerInternals.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000052#include "clang/Basic/Specifiers.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000053#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000054#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000055#include "clang/Basic/Version.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000056#include "clang/Lex/HeaderSearch.h"
57#include "clang/Lex/HeaderSearchOptions.h"
58#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000059#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000060#include "clang/Lex/PreprocessingRecord.h"
61#include "clang/Lex/Preprocessor.h"
62#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000063#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000064#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000065#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000066#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000067#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000068#include "clang/Serialization/ASTReader.h"
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000069#include "clang/Serialization/InMemoryModuleCache.h"
Duncan P. N. Exon Smithf7170d12019-11-21 18:49:05 -080070#include "clang/Serialization/ModuleFile.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000071#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000072#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000073#include "llvm/ADT/APFloat.h"
74#include "llvm/ADT/APInt.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000075#include "llvm/ADT/APSInt.h"
76#include "llvm/ADT/ArrayRef.h"
77#include "llvm/ADT/DenseMap.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000078#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000079#include "llvm/ADT/Optional.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000080#include "llvm/ADT/PointerIntPair.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000081#include "llvm/ADT/STLExtras.h"
Ilya Biryukov45643102018-07-09 11:33:23 +000082#include "llvm/ADT/ScopeExit.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000083#include "llvm/ADT/SmallSet.h"
84#include "llvm/ADT/SmallString.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000085#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000086#include "llvm/ADT/StringMap.h"
87#include "llvm/ADT/StringRef.h"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +000088#include "llvm/Bitstream/BitCodes.h"
89#include "llvm/Bitstream/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000090#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000091#include "llvm/Support/Compression.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000092#include "llvm/Support/DJB.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000093#include "llvm/Support/Endian.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000094#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000095#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000096#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000097#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000098#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000099#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +0000100#include "llvm/Support/SHA1.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000101#include "llvm/Support/VersionTuple.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000102#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +0000103#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000104#include <cassert>
105#include <cstdint>
106#include <cstdlib>
107#include <cstring>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000108#include <ctime>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000109#include <deque>
110#include <limits>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000111#include <memory>
112#include <queue>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000113#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +0000114#include <utility>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000115#include <vector>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000116
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +0000118using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119
Sebastian Redl3df5a082010-07-30 17:03:48 +0000120template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +0000121static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000122 if (v.empty()) return StringRef();
123 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000124 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000125}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000126
127template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000128static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000129 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000130 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000131}
132
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133//===----------------------------------------------------------------------===//
134// Type serialization
135//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000136
John McCalld505e572019-12-13 21:54:44 -0500137static TypeCode getTypeCodeForTypeClass(Type::TypeClass id) {
138 switch (id) {
139#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
140 case Type::CLASS_ID: return TYPE_##CODE_ID;
141#include "clang/Serialization/TypeBitCodes.def"
142 case Type::Builtin:
143 llvm_unreachable("shouldn't be serializing a builtin type this way");
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000144 }
John McCalld505e572019-12-13 21:54:44 -0500145 llvm_unreachable("bad type kind");
Xiuli Pan9c14e282016-01-09 12:53:17 +0000146}
147
John McCall8f115c62009-10-16 21:56:05 +0000148namespace {
149
John McCalld505e572019-12-13 21:54:44 -0500150class ASTTypeWriter {
151 ASTWriter &Writer;
152 ASTWriter::RecordData Record;
153 ASTRecordWriter BasicWriter;
154
155public:
156 ASTTypeWriter(ASTWriter &Writer)
157 : Writer(Writer), BasicWriter(Writer, Record) {}
158
159 uint64_t write(QualType T) {
160 if (T.hasLocalNonFastQualifiers()) {
161 Qualifiers Qs = T.getLocalQualifiers();
162 BasicWriter.writeQualType(T.getLocalUnqualifiedType());
163 BasicWriter.writeQualifiers(Qs);
164 return BasicWriter.Emit(TYPE_EXT_QUAL, Writer.getTypeExtQualAbbrev());
165 }
166
167 const Type *typePtr = T.getTypePtr();
168 serialization::AbstractTypeWriter<ASTRecordWriter> atw(BasicWriter);
169 atw.write(typePtr);
170 return BasicWriter.Emit(getTypeCodeForTypeClass(typePtr->getTypeClass()),
171 /*abbrev*/ 0);
172 }
173};
174
John McCall8f115c62009-10-16 21:56:05 +0000175class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000176 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000177
178public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000179 TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {}
John McCall8f115c62009-10-16 21:56:05 +0000180
John McCall17001972009-10-18 01:05:36 +0000181#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000182#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000183 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000184#include "clang/AST/TypeLocNodes.def"
185
John McCall17001972009-10-18 01:05:36 +0000186 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
187 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000188};
189
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000190} // namespace
John McCall8f115c62009-10-16 21:56:05 +0000191
John McCall17001972009-10-18 01:05:36 +0000192void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
193 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000194}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000195
John McCall17001972009-10-18 01:05:36 +0000196void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000197 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000198 if (TL.needsExtraLocalData()) {
Faisal Vali090da2d2018-01-01 18:23:28 +0000199 Record.push_back(TL.getWrittenTypeSpec());
200 Record.push_back(TL.getWrittenSignSpec());
201 Record.push_back(TL.getWrittenWidthSpec());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000202 Record.push_back(TL.hasModeAttr());
203 }
John McCall8f115c62009-10-16 21:56:05 +0000204}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000205
John McCall17001972009-10-18 01:05:36 +0000206void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000207 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000208}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000209
John McCall17001972009-10-18 01:05:36 +0000210void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000211 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000212}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000213
Reid Kleckner8a365022013-06-24 17:51:48 +0000214void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
215 // nothing to do
216}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000217
Reid Kleckner0503a872013-12-05 01:23:43 +0000218void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
219 // nothing to do
220}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000221
John McCall17001972009-10-18 01:05:36 +0000222void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000223 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000224}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000225
John McCall17001972009-10-18 01:05:36 +0000226void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000227 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000228}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000229
John McCall17001972009-10-18 01:05:36 +0000230void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000231 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000232}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000233
John McCall17001972009-10-18 01:05:36 +0000234void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000235 Record.AddSourceLocation(TL.getStarLoc());
236 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000237}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000238
John McCall17001972009-10-18 01:05:36 +0000239void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000240 Record.AddSourceLocation(TL.getLBracketLoc());
241 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000242 Record.push_back(TL.getSizeExpr() ? 1 : 0);
243 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000244 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000245}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000246
John McCall17001972009-10-18 01:05:36 +0000247void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
248 VisitArrayTypeLoc(TL);
249}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000250
John McCall17001972009-10-18 01:05:36 +0000251void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
252 VisitArrayTypeLoc(TL);
253}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000254
John McCall17001972009-10-18 01:05:36 +0000255void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
256 VisitArrayTypeLoc(TL);
257}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000258
John McCall17001972009-10-18 01:05:36 +0000259void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
260 DependentSizedArrayTypeLoc TL) {
261 VisitArrayTypeLoc(TL);
262}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000263
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000264void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
265 DependentAddressSpaceTypeLoc TL) {
266 Record.AddSourceLocation(TL.getAttrNameLoc());
267 SourceRange range = TL.getAttrOperandParensRange();
268 Record.AddSourceLocation(range.getBegin());
269 Record.AddSourceLocation(range.getEnd());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000270 Record.AddStmt(TL.getAttrExprOperand());
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000271}
272
John McCall17001972009-10-18 01:05:36 +0000273void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
274 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000275 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000276}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000277
John McCall17001972009-10-18 01:05:36 +0000278void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000279 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000280}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000281
Erich Keanef702b022018-07-13 19:46:04 +0000282void TypeLocWriter::VisitDependentVectorTypeLoc(
283 DependentVectorTypeLoc TL) {
284 Record.AddSourceLocation(TL.getNameLoc());
285}
286
John McCall17001972009-10-18 01:05:36 +0000287void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000288 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000289}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000290
John McCall17001972009-10-18 01:05:36 +0000291void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000292 Record.AddSourceLocation(TL.getLocalRangeBegin());
293 Record.AddSourceLocation(TL.getLParenLoc());
294 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000295 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000296 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000297 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000298 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000299}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000300
John McCall17001972009-10-18 01:05:36 +0000301void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
302 VisitFunctionTypeLoc(TL);
303}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000304
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
306 VisitFunctionTypeLoc(TL);
307}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000308
John McCallb96ec562009-12-04 22:46:56 +0000309void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000310 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000311}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000312
John McCall17001972009-10-18 01:05:36 +0000313void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000314 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000315}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000316
Manman Rene6be26c2016-09-13 17:25:08 +0000317void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
318 if (TL.getNumProtocols()) {
319 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
320 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
321 }
322 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
323 Record.AddSourceLocation(TL.getProtocolLoc(i));
324}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000325
John McCall17001972009-10-18 01:05:36 +0000326void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000327 Record.AddSourceLocation(TL.getTypeofLoc());
328 Record.AddSourceLocation(TL.getLParenLoc());
329 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000330}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000331
John McCall17001972009-10-18 01:05:36 +0000332void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000333 Record.AddSourceLocation(TL.getTypeofLoc());
334 Record.AddSourceLocation(TL.getLParenLoc());
335 Record.AddSourceLocation(TL.getRParenLoc());
336 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000337}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000338
John McCall17001972009-10-18 01:05:36 +0000339void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000340 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000341}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000342
Alexis Hunte852b102011-05-24 22:41:36 +0000343void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000344 Record.AddSourceLocation(TL.getKWLoc());
345 Record.AddSourceLocation(TL.getLParenLoc());
346 Record.AddSourceLocation(TL.getRParenLoc());
347 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000348}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000349
Richard Smith30482bc2011-02-20 03:19:35 +0000350void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000351 Record.AddSourceLocation(TL.getNameLoc());
Saar Razb481f022020-01-22 02:03:05 +0200352 Record.push_back(TL.isConstrained());
353 if (TL.isConstrained()) {
354 Record.AddNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc());
355 Record.AddSourceLocation(TL.getTemplateKWLoc());
356 Record.AddSourceLocation(TL.getConceptNameLoc());
357 Record.AddDeclRef(TL.getFoundDecl());
358 Record.AddSourceLocation(TL.getLAngleLoc());
359 Record.AddSourceLocation(TL.getRAngleLoc());
360 for (unsigned I = 0; I < TL.getNumArgs(); ++I)
361 Record.AddTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
362 TL.getArgLocInfo(I));
363 }
Richard Smith30482bc2011-02-20 03:19:35 +0000364}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000365
Richard Smith600b5262017-01-26 20:40:47 +0000366void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
367 DeducedTemplateSpecializationTypeLoc TL) {
368 Record.AddSourceLocation(TL.getTemplateNameLoc());
369}
370
John McCall17001972009-10-18 01:05:36 +0000371void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000372 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000373}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000374
John McCall17001972009-10-18 01:05:36 +0000375void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000376 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000377}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000378
John McCall81904512011-01-06 01:58:22 +0000379void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smithe43e2b32018-08-20 21:47:29 +0000380 Record.AddAttr(TL.getAttr());
John McCall81904512011-01-06 01:58:22 +0000381}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000382
John McCall17001972009-10-18 01:05:36 +0000383void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000384 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000385}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000386
John McCallcebee162009-10-18 09:09:24 +0000387void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
388 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000389 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000390}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000391
Douglas Gregorada4b792011-01-14 02:55:32 +0000392void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
393 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000394 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000395}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000396
John McCall17001972009-10-18 01:05:36 +0000397void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
398 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000399 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
400 Record.AddSourceLocation(TL.getTemplateNameLoc());
401 Record.AddSourceLocation(TL.getLAngleLoc());
402 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000403 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000404 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
405 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000406}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000407
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000408void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000409 Record.AddSourceLocation(TL.getLParenLoc());
410 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000411}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000412
Leonard Chanc72aaf62019-05-07 03:20:17 +0000413void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
414 Record.AddSourceLocation(TL.getExpansionLoc());
415}
416
Abramo Bagnara6150c882010-05-11 21:36:43 +0000417void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000418 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
419 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000420}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000421
John McCalle78aac42010-03-10 03:28:59 +0000422void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000423 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000424}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000425
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000426void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000427 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
428 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
429 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000430}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000431
John McCallc392f372010-06-11 00:33:02 +0000432void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
433 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000434 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
435 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
436 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
437 Record.AddSourceLocation(TL.getTemplateNameLoc());
438 Record.AddSourceLocation(TL.getLAngleLoc());
439 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000440 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000441 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
442 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000443}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000444
Douglas Gregord2fa7662010-12-20 02:24:11 +0000445void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000446 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000447}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000448
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000450 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000451}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000452
John McCall8b07ec22010-05-15 11:32:37 +0000453void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
454 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000455 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
456 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000457 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000458 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
459 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
460 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000461 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000462 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000463}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000464
John McCallfc93cf92009-10-22 22:37:11 +0000465void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000466 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000467}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000468
Eli Friedman0dfb8892011-10-06 23:00:33 +0000469void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000470 Record.AddSourceLocation(TL.getKWLoc());
471 Record.AddSourceLocation(TL.getLParenLoc());
472 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000473}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000474
Xiuli Pan9c14e282016-01-09 12:53:17 +0000475void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000476 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000477}
John McCall8f115c62009-10-16 21:56:05 +0000478
Richard Smith01b2cb42014-07-26 06:37:51 +0000479void ASTWriter::WriteTypeAbbrevs() {
480 using namespace llvm;
481
David Blaikieb44f0bf2017-01-04 22:36:43 +0000482 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000483
484 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000485 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000486 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
487 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
488 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000489 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000490
491 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000492 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000493 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
494 // FunctionType
495 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
496 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
497 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
498 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
499 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
500 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000501 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000502 Abv->Add(BitCodeAbbrevOp(0)); // NoCfCheck
Richard Smith01b2cb42014-07-26 06:37:51 +0000503 // FunctionProtoType
504 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
505 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
506 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
507 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
508 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
509 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
510 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
511 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000512 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000513}
514
Chris Lattner19cea4e2009-04-22 05:57:30 +0000515//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000516// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000517//===----------------------------------------------------------------------===//
518
Chris Lattner28fa4e62009-04-26 22:26:21 +0000519static void EmitBlockID(unsigned ID, const char *Name,
520 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000521 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000522 Record.clear();
523 Record.push_back(ID);
524 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
525
526 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000527 if (!Name || Name[0] == 0)
528 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000529 Record.clear();
530 while (*Name)
531 Record.push_back(*Name++);
532 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
533}
534
535static void EmitRecordID(unsigned ID, const char *Name,
536 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000537 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538 Record.clear();
539 Record.push_back(ID);
540 while (*Name)
541 Record.push_back(*Name++);
542 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000543}
544
545static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000546 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000547#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000548 RECORD(STMT_STOP);
549 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000550 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000551 RECORD(STMT_NULL);
552 RECORD(STMT_COMPOUND);
553 RECORD(STMT_CASE);
554 RECORD(STMT_DEFAULT);
555 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000556 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000557 RECORD(STMT_IF);
558 RECORD(STMT_SWITCH);
559 RECORD(STMT_WHILE);
560 RECORD(STMT_DO);
561 RECORD(STMT_FOR);
562 RECORD(STMT_GOTO);
563 RECORD(STMT_INDIRECT_GOTO);
564 RECORD(STMT_CONTINUE);
565 RECORD(STMT_BREAK);
566 RECORD(STMT_RETURN);
567 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000568 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000569 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000570 RECORD(EXPR_PREDEFINED);
571 RECORD(EXPR_DECL_REF);
572 RECORD(EXPR_INTEGER_LITERAL);
573 RECORD(EXPR_FLOATING_LITERAL);
574 RECORD(EXPR_IMAGINARY_LITERAL);
575 RECORD(EXPR_STRING_LITERAL);
576 RECORD(EXPR_CHARACTER_LITERAL);
577 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000578 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000579 RECORD(EXPR_UNARY_OPERATOR);
580 RECORD(EXPR_SIZEOF_ALIGN_OF);
581 RECORD(EXPR_ARRAY_SUBSCRIPT);
582 RECORD(EXPR_CALL);
583 RECORD(EXPR_MEMBER);
584 RECORD(EXPR_BINARY_OPERATOR);
585 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
586 RECORD(EXPR_CONDITIONAL_OPERATOR);
587 RECORD(EXPR_IMPLICIT_CAST);
588 RECORD(EXPR_CSTYLE_CAST);
589 RECORD(EXPR_COMPOUND_LITERAL);
590 RECORD(EXPR_EXT_VECTOR_ELEMENT);
591 RECORD(EXPR_INIT_LIST);
592 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000593 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000594 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000595 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000596 RECORD(EXPR_VA_ARG);
597 RECORD(EXPR_ADDR_LABEL);
598 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000599 RECORD(EXPR_CHOOSE);
600 RECORD(EXPR_GNU_NULL);
601 RECORD(EXPR_SHUFFLE_VECTOR);
602 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000603 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000604 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000605 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000606 RECORD(EXPR_OBJC_ARRAY_LITERAL);
607 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000608 RECORD(EXPR_OBJC_ENCODE);
609 RECORD(EXPR_OBJC_SELECTOR_EXPR);
610 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
611 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
612 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
613 RECORD(EXPR_OBJC_KVC_REF_EXPR);
614 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000615 RECORD(STMT_OBJC_FOR_COLLECTION);
616 RECORD(STMT_OBJC_CATCH);
617 RECORD(STMT_OBJC_FINALLY);
618 RECORD(STMT_OBJC_AT_TRY);
619 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
620 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000621 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000622 RECORD(STMT_CXX_CATCH);
623 RECORD(STMT_CXX_TRY);
624 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000625 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000626 RECORD(EXPR_CXX_MEMBER_CALL);
Richard Smith778dc0f2019-10-19 00:04:38 +0000627 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000628 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000629 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000630 RECORD(EXPR_CXX_STATIC_CAST);
631 RECORD(EXPR_CXX_DYNAMIC_CAST);
632 RECORD(EXPR_CXX_REINTERPRET_CAST);
633 RECORD(EXPR_CXX_CONST_CAST);
634 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000635 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000636 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000637 RECORD(EXPR_CXX_BOOL_LITERAL);
638 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000639 RECORD(EXPR_CXX_TYPEID_EXPR);
640 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000641 RECORD(EXPR_CXX_THIS);
642 RECORD(EXPR_CXX_THROW);
643 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000644 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000645 RECORD(EXPR_CXX_BIND_TEMPORARY);
646 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
647 RECORD(EXPR_CXX_NEW);
648 RECORD(EXPR_CXX_DELETE);
649 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
650 RECORD(EXPR_EXPR_WITH_CLEANUPS);
651 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
652 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
653 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
654 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
655 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000656 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000657 RECORD(EXPR_CXX_NOEXCEPT);
658 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000659 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
660 RECORD(EXPR_TYPE_TRAIT);
661 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000662 RECORD(EXPR_PACK_EXPANSION);
663 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000664 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000665 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000666 RECORD(EXPR_FUNCTION_PARM_PACK);
667 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000668 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000669 RECORD(EXPR_CXX_UUIDOF_EXPR);
670 RECORD(EXPR_CXX_UUIDOF_TYPE);
671 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000672#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000673}
Mike Stump11289f42009-09-09 15:08:12 +0000674
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000675void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000676 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000677 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +0000678
Sebastian Redl539c5062010-08-18 23:57:32 +0000679#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
680#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000681
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000682 // Control Block.
683 BLOCK(CONTROL_BLOCK);
684 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000685 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +0000686 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000687 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000688 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000689 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000690 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000691 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000692 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +0000693
694 BLOCK(OPTIONS_BLOCK);
695 RECORD(LANGUAGE_OPTIONS);
696 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000697 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000698 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000699 RECORD(PREPROCESSOR_OPTIONS);
700
Douglas Gregor108cb222012-10-19 00:45:00 +0000701 BLOCK(INPUT_FILES_BLOCK);
702 RECORD(INPUT_FILE);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +0000703 RECORD(INPUT_FILE_HASH);
Douglas Gregor108cb222012-10-19 00:45:00 +0000704
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000705 // AST Top-Level Block.
706 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000707 RECORD(TYPE_OFFSET);
708 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000709 RECORD(IDENTIFIER_OFFSET);
710 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000711 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +0000712 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 RECORD(SPECIAL_TYPES);
714 RECORD(STATISTICS);
715 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000716 RECORD(SELECTOR_OFFSETS);
717 RECORD(METHOD_POOL);
718 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000719 RECORD(SOURCE_LOCATION_OFFSETS);
720 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000721 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +0000722 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000723 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +0000724 RECORD(VTABLE_USES);
Cameron Desrochersb60f1b62018-01-15 19:14:16 +0000725 RECORD(PPD_SKIPPED_RANGES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000726 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000727 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000728 RECORD(SEMA_DECL_REFS);
729 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
730 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000731 RECORD(UPDATE_VISIBLE);
732 RECORD(DECL_UPDATE_OFFSETS);
733 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000734 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000735 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000736 RECORD(FP_PRAGMA_OPTIONS);
737 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +0000738 RECORD(OPENCL_EXTENSION_TYPES);
739 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000740 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000741 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000742 RECORD(MODULE_OFFSET_MAP);
743 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000744 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000745 RECORD(FILE_SORTED_DECLS);
746 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000747 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000748 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +0000749 RECORD(INTERESTING_IDENTIFIERS);
750 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000751 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000752 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +0000753 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +0000754 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +0000755 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +0000756 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +0000757 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +0000758 RECORD(PP_CONDITIONAL_STACK);
Douglas Gregor358cd442012-01-15 16:58:34 +0000759
Chris Lattner28fa4e62009-04-26 22:26:21 +0000760 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000761 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000762 RECORD(SM_SLOC_FILE_ENTRY);
763 RECORD(SM_SLOC_BUFFER_ENTRY);
764 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +0000765 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000766 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattner28fa4e62009-04-26 22:26:21 +0000768 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000769 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000770 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000771 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +0000772 RECORD(PP_MACRO_OBJECT_LIKE);
773 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000774 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000775
Richard Smithfa715652015-08-31 22:43:10 +0000776 // Submodule Block.
777 BLOCK(SUBMODULE_BLOCK);
778 RECORD(SUBMODULE_METADATA);
779 RECORD(SUBMODULE_DEFINITION);
780 RECORD(SUBMODULE_UMBRELLA_HEADER);
781 RECORD(SUBMODULE_HEADER);
782 RECORD(SUBMODULE_TOPHEADER);
783 RECORD(SUBMODULE_UMBRELLA_DIR);
784 RECORD(SUBMODULE_IMPORTS);
785 RECORD(SUBMODULE_EXPORTS);
786 RECORD(SUBMODULE_REQUIRES);
787 RECORD(SUBMODULE_EXCLUDED_HEADER);
788 RECORD(SUBMODULE_LINK_LIBRARY);
789 RECORD(SUBMODULE_CONFIG_MACRO);
790 RECORD(SUBMODULE_CONFLICT);
791 RECORD(SUBMODULE_PRIVATE_HEADER);
792 RECORD(SUBMODULE_TEXTUAL_HEADER);
793 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +0000794 RECORD(SUBMODULE_INITIALIZERS);
Douglas Gregorf0b11de2017-09-14 23:38:44 +0000795 RECORD(SUBMODULE_EXPORT_AS);
Richard Smithfa715652015-08-31 22:43:10 +0000796
797 // Comments Block.
798 BLOCK(COMMENTS_BLOCK);
799 RECORD(COMMENTS_RAW_COMMENT);
800
Douglas Gregor12bfa382009-10-17 00:13:19 +0000801 // Decls and Types block.
802 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000803 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000804 RECORD(TYPE_COMPLEX);
805 RECORD(TYPE_POINTER);
806 RECORD(TYPE_BLOCK_POINTER);
807 RECORD(TYPE_LVALUE_REFERENCE);
808 RECORD(TYPE_RVALUE_REFERENCE);
809 RECORD(TYPE_MEMBER_POINTER);
810 RECORD(TYPE_CONSTANT_ARRAY);
811 RECORD(TYPE_INCOMPLETE_ARRAY);
812 RECORD(TYPE_VARIABLE_ARRAY);
813 RECORD(TYPE_VECTOR);
814 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000815 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000816 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000817 RECORD(TYPE_TYPEDEF);
818 RECORD(TYPE_TYPEOF_EXPR);
819 RECORD(TYPE_TYPEOF);
820 RECORD(TYPE_RECORD);
821 RECORD(TYPE_ENUM);
822 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000823 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000824 RECORD(TYPE_DECLTYPE);
825 RECORD(TYPE_ELABORATED);
826 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
827 RECORD(TYPE_UNRESOLVED_USING);
828 RECORD(TYPE_INJECTED_CLASS_NAME);
829 RECORD(TYPE_OBJC_OBJECT);
830 RECORD(TYPE_TEMPLATE_TYPE_PARM);
831 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
832 RECORD(TYPE_DEPENDENT_NAME);
833 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
834 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
835 RECORD(TYPE_PAREN);
Leonard Chanc72aaf62019-05-07 03:20:17 +0000836 RECORD(TYPE_MACRO_QUALIFIED);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000837 RECORD(TYPE_PACK_EXPANSION);
838 RECORD(TYPE_ATTRIBUTED);
839 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000840 RECORD(TYPE_AUTO);
841 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000842 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000843 RECORD(TYPE_DECAYED);
844 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +0000845 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +0000846 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000847 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +0000848 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000849 RECORD(DECL_ENUM);
850 RECORD(DECL_RECORD);
851 RECORD(DECL_ENUM_CONSTANT);
852 RECORD(DECL_FUNCTION);
853 RECORD(DECL_OBJC_METHOD);
854 RECORD(DECL_OBJC_INTERFACE);
855 RECORD(DECL_OBJC_PROTOCOL);
856 RECORD(DECL_OBJC_IVAR);
857 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000858 RECORD(DECL_OBJC_CATEGORY);
859 RECORD(DECL_OBJC_CATEGORY_IMPL);
860 RECORD(DECL_OBJC_IMPLEMENTATION);
861 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
862 RECORD(DECL_OBJC_PROPERTY);
863 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000864 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000865 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000866 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000867 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000868 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000869 RECORD(DECL_FILE_SCOPE_ASM);
870 RECORD(DECL_BLOCK);
871 RECORD(DECL_CONTEXT_LEXICAL);
872 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000873 RECORD(DECL_NAMESPACE);
874 RECORD(DECL_NAMESPACE_ALIAS);
875 RECORD(DECL_USING);
876 RECORD(DECL_USING_SHADOW);
877 RECORD(DECL_USING_DIRECTIVE);
878 RECORD(DECL_UNRESOLVED_USING_VALUE);
879 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
880 RECORD(DECL_LINKAGE_SPEC);
881 RECORD(DECL_CXX_RECORD);
882 RECORD(DECL_CXX_METHOD);
883 RECORD(DECL_CXX_CONSTRUCTOR);
884 RECORD(DECL_CXX_DESTRUCTOR);
885 RECORD(DECL_CXX_CONVERSION);
886 RECORD(DECL_ACCESS_SPEC);
887 RECORD(DECL_FRIEND);
888 RECORD(DECL_FRIEND_TEMPLATE);
889 RECORD(DECL_CLASS_TEMPLATE);
890 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
891 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000892 RECORD(DECL_VAR_TEMPLATE);
893 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
894 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000895 RECORD(DECL_FUNCTION_TEMPLATE);
896 RECORD(DECL_TEMPLATE_TYPE_PARM);
897 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
898 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Saar Razd7aae332019-07-10 21:25:49 +0000899 RECORD(DECL_CONCEPT);
Saar Raza0f50d72020-01-18 09:11:43 +0200900 RECORD(DECL_REQUIRES_EXPR_BODY);
Richard Smithefc884a2016-04-13 07:41:35 +0000901 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000902 RECORD(DECL_STATIC_ASSERT);
903 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +0000904 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000905 RECORD(DECL_INDIRECTFIELD);
906 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +0000907 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
908 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
909 RECORD(DECL_IMPORT);
910 RECORD(DECL_OMP_THREADPRIVATE);
911 RECORD(DECL_EMPTY);
912 RECORD(DECL_OBJC_TYPE_PARAM);
913 RECORD(DECL_OMP_CAPTUREDEXPR);
914 RECORD(DECL_PRAGMA_COMMENT);
915 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
916 RECORD(DECL_OMP_DECLARE_REDUCTION);
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000917 RECORD(DECL_OMP_ALLOCATE);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000918
Douglas Gregor03412ba2011-06-03 02:27:19 +0000919 // Statements and Exprs can occur in the Decls and Types block.
920 AddStmtsExprs(Stream, Record);
921
Douglas Gregor92a96f52011-02-08 21:58:10 +0000922 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000923 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000924 RECORD(PPD_MACRO_DEFINITION);
925 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000926
927 // Decls and Types block.
928 BLOCK(EXTENSION_BLOCK);
929 RECORD(EXTENSION_METADATA);
930
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +0000931 BLOCK(UNHASHED_CONTROL_BLOCK);
932 RECORD(SIGNATURE);
933 RECORD(DIAGNOSTIC_OPTIONS);
934 RECORD(DIAG_PRAGMA_MAPPINGS);
935
Chris Lattner28fa4e62009-04-26 22:26:21 +0000936#undef RECORD
937#undef BLOCK
938 Stream.ExitBlock();
939}
940
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000941/// Prepares a path for being written to an AST file by converting it
Richard Smith54cc3c22014-12-11 20:50:24 +0000942/// to an absolute path and removing nested './'s.
943///
944/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +0000945static bool cleanPathForOutput(FileManager &FileMgr,
946 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +0000947 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +0000948 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +0000949}
950
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000951/// Adjusts the given filename to only write out the portion of the
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000952/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000953///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000954/// \param Filename the file name to adjust.
955///
Richard Smith7ed1bc92014-12-05 22:42:13 +0000956/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
957/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958///
959/// \returns either the original filename (if it needs no adjustment) or the
960/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000961static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +0000962adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000964
Richard Smith7ed1bc92014-12-05 22:42:13 +0000965 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000966 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000967
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000968 // Verify that the filename and the system root have the same prefix.
969 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +0000970 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
971 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000972 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000973
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000974 // We hit the end of the filename before we hit the end of the system root.
975 if (!Filename[Pos])
976 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000977
Richard Smith7ed1bc92014-12-05 22:42:13 +0000978 // If there's not a path separator at the end of the base directory nor
979 // immediately after it, then this isn't within the base directory.
980 if (!llvm::sys::path::is_separator(Filename[Pos])) {
981 if (!llvm::sys::path::is_separator(BaseDir.back()))
982 return Filename;
983 } else {
984 // If the file name has a '/' at the current position, skip over the '/'.
985 // We distinguish relative paths from absolute paths by the
986 // absence of '/' at the beginning of relative paths.
987 //
988 // FIXME: This is wrong. We distinguish them by asking if the path is
989 // absolute, which isn't the same thing. And there might be multiple '/'s
990 // in a row. Use a better mechanism to indicate whether we have emitted an
991 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000992 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +0000993 }
Mike Stump11289f42009-09-09 15:08:12 +0000994
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000995 return Filename + Pos;
996}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000997
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +0000998ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
999 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1000 llvm::SHA1 Hasher;
1001 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1002 auto Hash = Hasher.result();
1003
1004 // Convert to an array [5*i32].
1005 ASTFileSignature Signature;
1006 auto LShift = [&](unsigned char Val, unsigned Shift) {
1007 return (uint32_t)Val << Shift;
1008 };
1009 for (int I = 0; I != 5; ++I)
1010 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1011 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1012
1013 return Signature;
1014}
1015
1016ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1017 ASTContext &Context) {
1018 // Flush first to prepare the PCM hash (signature).
1019 Stream.FlushToWord();
1020 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1021
1022 // Enter the block and prepare to write records.
1023 RecordData Record;
1024 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1025
1026 // For implicit modules, write the hash of the PCM as its signature.
1027 ASTFileSignature Signature;
1028 if (WritingModule &&
1029 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1030 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1031 Record.append(Signature.begin(), Signature.end());
1032 Stream.EmitRecord(SIGNATURE, Record);
1033 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001034 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001035
1036 // Diagnostic options.
1037 const auto &Diags = Context.getDiagnostics();
1038 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1039#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1040#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1041 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1042#include "clang/Basic/DiagnosticOptions.def"
1043 Record.push_back(DiagOpts.Warnings.size());
1044 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1045 AddString(DiagOpts.Warnings[I], Record);
1046 Record.push_back(DiagOpts.Remarks.size());
1047 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1048 AddString(DiagOpts.Remarks[I], Record);
1049 // Note: we don't serialize the log or serialization file names, because they
1050 // are generally transient files and will almost always be overridden.
1051 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1052
1053 // Write out the diagnostic/pragma mappings.
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001054 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001055
1056 // Leave the options block.
1057 Stream.ExitBlock();
1058 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001059}
1060
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001061/// Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001062void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1063 StringRef isysroot,
1064 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001065 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001066
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001067 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001068 RecordData Record;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001069
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001070 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001071 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001072 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1073 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1074 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1075 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1076 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1077 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001078 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001079 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PCHHasObjectFile
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001080 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1081 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001082 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001083 assert((!WritingModule || isysroot.empty()) &&
1084 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001085 {
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001086 RecordData::value_type Record[] = {
1087 METADATA,
1088 VERSION_MAJOR,
1089 VERSION_MINOR,
1090 CLANG_VERSION_MAJOR,
1091 CLANG_VERSION_MINOR,
1092 !isysroot.empty(),
1093 IncludeTimestamps,
1094 Context.getLangOpts().BuildingPCHWithObjectFile,
1095 ASTHasCompilerErrors};
Mehdi Amini57a41912015-09-10 01:46:39 +00001096 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1097 getClangFullRepositoryVersion());
1098 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001099
Richard Smith8b706102017-05-31 20:56:55 +00001100 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001101 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001102 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001103 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001105 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001106 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001107 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1108 }
1109
Richard Smith7ed1bc92014-12-05 22:42:13 +00001110 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001111 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001112 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001113
1114 // If the home of the module is the current working directory, then we
1115 // want to pick up the cwd of the build process loading the module, not
1116 // our cwd, when we load this module.
1117 if (!PP.getHeaderSearchInfo()
1118 .getHeaderSearchOpts()
1119 .ModuleMapFileHomeIsCwd ||
1120 WritingModule->Directory->getName() != StringRef(".")) {
1121 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001122 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001123 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001125 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001126
Mehdi Amini57a41912015-09-10 01:46:39 +00001127 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001128 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1129 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001130
1131 // Write out all other paths relative to the base directory if possible.
1132 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1133 } else if (!isysroot.empty()) {
1134 // Write out paths relative to the sysroot if possible.
Benjamin Krameradcd0262020-01-28 20:23:46 +01001135 BaseDirectory = std::string(isysroot);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001136 }
1137
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001138 // Module map file
Richard Smithd19389a2017-07-05 07:47:11 +00001139 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001140 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001141
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001142 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001143 AddPath(WritingModule->PresumedModuleMapFile.empty()
1144 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1145 : StringRef(WritingModule->PresumedModuleMapFile),
1146 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001147
1148 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001149 if (auto *AdditionalModMaps =
1150 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001151 Record.push_back(AdditionalModMaps->size());
1152 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001153 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001154 } else {
1155 Record.push_back(0);
1156 }
1157
1158 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001159 }
1160
Douglas Gregor112b9072012-10-18 05:31:06 +00001161 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001162 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001163 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001164 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001165
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001166 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001167 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001168 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001169 continue;
1170
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001171 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1172 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001173
1174 // If we have calculated signature, there is no need to store
1175 // the size or timestamp.
1176 Record.push_back(M.Signature ? 0 : M.File->getSize());
1177 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1178
1179 for (auto I : M.Signature)
1180 Record.push_back(I);
1181
Boris Kolpackovd30446f2017-08-31 06:26:43 +00001182 AddString(M.ModuleName, Record);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001183 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001184 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001185 Stream.EmitRecord(IMPORTS, Record);
1186 }
Mike Stump11289f42009-09-09 15:08:12 +00001187
Richard Smith0516b182015-09-08 19:40:14 +00001188 // Write the options block.
1189 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1190
Douglas Gregor112b9072012-10-18 05:31:06 +00001191 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001192 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001193 const LangOptions &LangOpts = Context.getLangOpts();
1194#define LANGOPT(Name, Bits, Default, Description) \
1195 Record.push_back(LangOpts.Name);
1196#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1197 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001198#include "clang/Basic/LangOptions.def"
1199#define SANITIZER(NAME, ID) \
1200 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001201#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001202
Ben Langmuircd98cb72015-06-23 18:20:18 +00001203 Record.push_back(LangOpts.ModuleFeatures.size());
1204 for (StringRef Feature : LangOpts.ModuleFeatures)
1205 AddString(Feature, Record);
1206
Douglas Gregor112b9072012-10-18 05:31:06 +00001207 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1208 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001209
1210 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001211
1212 // Comment options.
1213 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001214 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1215 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001216 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001217 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001218
Samuel Antaoee8fb302016-01-06 13:42:12 +00001219 // OpenMP offloading options.
1220 Record.push_back(LangOpts.OMPTargetTriples.size());
1221 for (auto &T : LangOpts.OMPTargetTriples)
1222 AddString(T.getTriple(), Record);
1223
1224 AddString(LangOpts.OMPHostIRFile, Record);
1225
Douglas Gregor112b9072012-10-18 05:31:06 +00001226 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1227
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001228 // Target options.
1229 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001230 const TargetInfo &Target = Context.getTargetInfo();
1231 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001232 AddString(TargetOpts.Triple, Record);
1233 AddString(TargetOpts.CPU, Record);
1234 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001235 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1236 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1237 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1238 }
1239 Record.push_back(TargetOpts.Features.size());
1240 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1241 AddString(TargetOpts.Features[I], Record);
1242 }
1243 Stream.EmitRecord(TARGET_OPTIONS, Record);
1244
Douglas Gregorc6317db2012-10-24 15:49:58 +00001245 // File system options.
1246 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001247 const FileSystemOptions &FSOpts =
1248 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001249 AddString(FSOpts.WorkingDir, Record);
1250 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1251
Douglas Gregor2d302362012-10-24 16:50:34 +00001252 // Header search options.
1253 Record.clear();
1254 const HeaderSearchOptions &HSOpts
1255 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1256 AddString(HSOpts.Sysroot, Record);
1257
1258 // Include entries.
1259 Record.push_back(HSOpts.UserEntries.size());
1260 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1261 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1262 AddString(Entry.Path, Record);
1263 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001264 Record.push_back(Entry.IsFramework);
1265 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001266 }
1267
1268 // System header prefixes.
1269 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1270 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1271 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1272 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1273 }
1274
1275 AddString(HSOpts.ResourceDir, Record);
1276 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001277 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001278 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001279 Record.push_back(HSOpts.ImplicitModuleMaps);
1280 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001281 Record.push_back(HSOpts.UseBuiltinIncludes);
1282 Record.push_back(HSOpts.UseStandardSystemIncludes);
1283 Record.push_back(HSOpts.UseStandardCXXIncludes);
1284 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001285 // Write out the specific module cache path that contains the module files.
1286 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001287 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1288
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001289 // Preprocessor options.
1290 Record.clear();
1291 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1292
1293 // Macro definitions.
1294 Record.push_back(PPOpts.Macros.size());
1295 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1296 AddString(PPOpts.Macros[I].first, Record);
1297 Record.push_back(PPOpts.Macros[I].second);
1298 }
1299
1300 // Includes
1301 Record.push_back(PPOpts.Includes.size());
1302 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1303 AddString(PPOpts.Includes[I], Record);
1304
1305 // Macro includes
1306 Record.push_back(PPOpts.MacroIncludes.size());
1307 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1308 AddString(PPOpts.MacroIncludes[I], Record);
1309
Douglas Gregorb6368752012-10-24 23:41:50 +00001310 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001311 // Detailed record is important since it is used for the module cache hash.
1312 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001313 AddString(PPOpts.ImplicitPCHInclude, Record);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001314 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1315 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1316
Richard Smith0516b182015-09-08 19:40:14 +00001317 // Leave the options block.
1318 Stream.ExitBlock();
1319
Douglas Gregora3b20262011-05-06 21:43:30 +00001320 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001321 SourceManager &SM = Context.getSourceManager();
1322 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001323 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001324 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1325 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001326 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001327 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001328
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001329 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001330 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001331 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001332 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001333 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001334
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001335 Record.clear();
1336 Record.push_back(SM.getMainFileID().getOpaqueValue());
1337 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1338
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001339 // Original PCH directory
1340 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001341 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001342 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001344 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001345
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001346 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001347
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001348 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001349 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1350
Mehdi Amini57a41912015-09-10 01:46:39 +00001351 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001352 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1353 }
1354
Douglas Gregor49491f72013-03-15 22:15:07 +00001355 WriteInputFiles(Context.SourceMgr,
1356 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001357 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001358 Stream.ExitBlock();
1359}
1360
Douglas Gregor49491f72013-03-15 22:15:07 +00001361namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001362
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001363/// An input file.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001364struct InputFileEntry {
1365 const FileEntry *File;
1366 bool IsSystemFile;
1367 bool IsTransient;
1368 bool BufferOverridden;
1369 bool IsTopLevelModuleMap;
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001370 uint32_t ContentHash[2];
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001371};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001372
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001373} // namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001374
1375void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1376 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001377 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001378 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001379
Douglas Gregor72be3902012-10-19 00:38:02 +00001380 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001381
Douglas Gregor72be3902012-10-19 00:38:02 +00001382 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001383 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001384 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001385 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001386 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1387 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001388 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001389 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Richard Smithf3f84612017-06-29 02:19:42 +00001390 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
Douglas Gregor72be3902012-10-19 00:38:02 +00001391 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001392 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001393
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001394 // Create input file hash abbreviation.
1395 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1396 IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1397 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1398 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1399 unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1400
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001401 // Get all ContentCache objects for files, sorted by whether the file is a
1402 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001403 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001404 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1405 // Get this source location entry.
1406 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001407 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001408
1409 // We only care about file entries that were not overridden.
1410 if (!SLoc->isFile())
1411 continue;
Richard Smithf3f84612017-06-29 02:19:42 +00001412 const SrcMgr::FileInfo &File = SLoc->getFile();
1413 const SrcMgr::ContentCache *Cache = File.getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001414 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001415 continue;
1416
Douglas Gregor49491f72013-03-15 22:15:07 +00001417 InputFileEntry Entry;
1418 Entry.File = Cache->OrigEntry;
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001419 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
Richard Smitha8cfffa2015-11-26 02:04:16 +00001420 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001421 Entry.BufferOverridden = Cache->BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001422 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1423 File.getIncludeLoc().isInvalid();
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001424
1425 auto ContentHash = hash_code(-1);
1426 if (PP->getHeaderSearchInfo()
1427 .getHeaderSearchOpts()
1428 .ValidateASTInputFilesContent) {
1429 auto *MemBuff = Cache->getRawBuffer();
1430 if (MemBuff)
1431 ContentHash = hash_value(MemBuff->getBuffer());
1432 else
1433 // FIXME: The path should be taken from the FileEntryRef.
1434 PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1435 << Entry.File->getName();
1436 }
1437 auto CH = llvm::APInt(64, ContentHash);
1438 Entry.ContentHash[0] =
1439 static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1440 Entry.ContentHash[1] =
1441 static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1442
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001443 if (Entry.IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001444 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001445 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001446 SortedFiles.push_front(Entry);
1447 }
1448
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001449 unsigned UserFilesNum = 0;
1450 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001451 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001452 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001453 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001454 if (InputFileID != 0)
1455 continue; // already recorded this file.
1456
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001457 // Record this entry's offset.
1458 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001459
1460 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001461
Douglas Gregor49491f72013-03-15 22:15:07 +00001462 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001463 ++UserFilesNum;
1464
Douglas Gregor72be3902012-10-19 00:38:02 +00001465 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001466 // And whether this file was overridden.
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001467 {
1468 RecordData::value_type Record[] = {
1469 INPUT_FILE,
1470 InputFileOffsets.size(),
1471 (uint64_t)Entry.File->getSize(),
1472 (uint64_t)getTimestampForOutput(Entry.File),
1473 Entry.BufferOverridden,
1474 Entry.IsTransient,
1475 Entry.IsTopLevelModuleMap};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001476
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001477 // FIXME: The path should be taken from the FileEntryRef.
1478 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1479 }
1480
1481 // Emit content hash for this file.
1482 {
1483 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1484 Entry.ContentHash[1]};
1485 Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1486 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001487 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001488
Douglas Gregor112b9072012-10-18 05:31:06 +00001489 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001490
1491 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001492 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001493 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1494 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001495 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1496 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001497 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001498 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001499
1500 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001501 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1502 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001503 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001504}
1505
Douglas Gregora7f71a92009-04-10 03:52:48 +00001506//===----------------------------------------------------------------------===//
1507// Source Manager Serialization
1508//===----------------------------------------------------------------------===//
1509
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001510/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001511/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001512static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001513 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001514
David Blaikieb44f0bf2017-01-04 22:36:43 +00001515 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001516 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001517 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1518 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001521 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001523 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001524 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001526 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001527}
1528
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001529/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001530/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001531static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001532 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001533
David Blaikieb44f0bf2017-01-04 22:36:43 +00001534 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001535 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001541 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001542}
1543
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001544/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001545/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001546static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1547 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001548 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001549
David Blaikieb44f0bf2017-01-04 22:36:43 +00001550 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001551 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1552 : SM_SLOC_BUFFER_BLOB));
1553 if (Compressed)
1554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001556 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001557}
1558
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001559/// Create an abbreviation for the SLocEntry that refers to a macro
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001560/// expansion.
1561static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001562 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001563
David Blaikieb44f0bf2017-01-04 22:36:43 +00001564 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001565 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Richard Smithb5f81712018-04-30 05:25:48 +00001570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
Douglas Gregor83243272009-04-15 18:05:10 +00001571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001572 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573}
1574
Douglas Gregor09b69892011-02-10 17:09:37 +00001575namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001576
Douglas Gregor09b69892011-02-10 17:09:37 +00001577 // Trait used for the on-disk hash table of header search information.
1578 class HeaderFileInfoTrait {
1579 ASTWriter &Writer;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001580
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001581 // Keep track of the framework names we've used during serialization.
1582 SmallVector<char, 128> FrameworkStringData;
1583 llvm::StringMap<unsigned> FrameworkNameOffset;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001584
Douglas Gregor09b69892011-02-10 17:09:37 +00001585 public:
Richard Smith040e1262017-06-02 01:55:39 +00001586 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1587
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001588 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001589 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001590 off_t Size;
1591 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001592 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001593 using key_type_ref = const key_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001594
1595 using UnresolvedModule =
1596 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001597
Richard Smith040e1262017-06-02 01:55:39 +00001598 struct data_type {
1599 const HeaderFileInfo &HFI;
1600 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1601 UnresolvedModule Unresolved;
1602 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001603 using data_type_ref = const data_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001604
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001605 using hash_value_type = unsigned;
1606 using offset_type = unsigned;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001607
Richard Smithe75ee0f2015-08-17 07:13:32 +00001608 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001609 // The hash is based only on size/time of the file, so that the reader can
1610 // match even when symlinking or excess path elements ("foo/../", "../")
1611 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00001612 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00001613 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001614
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001615 std::pair<unsigned, unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001616 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001617 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001618
Peter Collingbournee3f65292018-05-18 19:46:24 +00001619 endian::Writer LE(Out, little);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001620 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001621 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001622 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00001623 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00001624 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1625 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00001626 if (Data.Unresolved.getPointer())
1627 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00001628 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001629 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001630 }
Richard Smith040e1262017-06-02 01:55:39 +00001631
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001632 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001633 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001634
Peter Collingbournee3f65292018-05-18 19:46:24 +00001635 endian::Writer LE(Out, little);
Richard Smith040e1262017-06-02 01:55:39 +00001636 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001637 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00001638 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001639 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001640 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001641 }
Richard Smith040e1262017-06-02 01:55:39 +00001642
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001643 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001644 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001645 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001646
Peter Collingbournee3f65292018-05-18 19:46:24 +00001647 endian::Writer LE(Out, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00001648 uint64_t Start = Out.tell(); (void)Start;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001649
Richard Smithf3f84612017-06-29 02:19:42 +00001650 unsigned char Flags = (Data.HFI.isImport << 5)
1651 | (Data.HFI.isPragmaOnce << 4)
Richard Smith040e1262017-06-02 01:55:39 +00001652 | (Data.HFI.DirInfo << 1)
1653 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001654 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00001655 LE.write<uint16_t>(Data.HFI.NumIncludes);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001656
Richard Smith040e1262017-06-02 01:55:39 +00001657 if (!Data.HFI.ControllingMacro)
1658 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001659 else
Richard Smith040e1262017-06-02 01:55:39 +00001660 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1661
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001662 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00001663 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001664 // If this header refers into a framework, save the framework name.
1665 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00001666 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001667 if (Pos == FrameworkNameOffset.end()) {
1668 Offset = FrameworkStringData.size() + 1;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001669 FrameworkStringData.append(Data.HFI.Framework.begin(),
Richard Smith040e1262017-06-02 01:55:39 +00001670 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001671 FrameworkStringData.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001672
Richard Smith040e1262017-06-02 01:55:39 +00001673 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001674 } else
1675 Offset = Pos->second;
1676 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001677 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001678
Richard Smith040e1262017-06-02 01:55:39 +00001679 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1680 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1681 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00001682 assert((Value >> 2) == ModID && "overflow in header module info");
1683 LE.write<uint32_t>(Value);
1684 }
Richard Smith040e1262017-06-02 01:55:39 +00001685 };
1686
1687 // FIXME: If the header is excluded, we should write out some
1688 // record of that fact.
1689 for (auto ModInfo : Data.KnownHeaders)
1690 EmitModule(ModInfo.getModule(), ModInfo.getRole());
1691 if (Data.Unresolved.getPointer())
1692 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001693
Douglas Gregor09b69892011-02-10 17:09:37 +00001694 assert(Out.tell() - Start == DataLen && "Wrong data length");
1695 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001696
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001697 const char *strings_begin() const { return FrameworkStringData.begin(); }
1698 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001699 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001700
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001701} // namespace
Douglas Gregor09b69892011-02-10 17:09:37 +00001702
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001703/// Write the header search block for the list of files that
Douglas Gregor09b69892011-02-10 17:09:37 +00001704///
1705/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001706void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00001707 HeaderFileInfoTrait GeneratorTrait(*this);
1708 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1709 SmallVector<const char *, 4> SavedStrings;
1710 unsigned NumHeaderSearchEntries = 0;
1711
1712 // Find all unresolved headers for the current module. We generally will
1713 // have resolved them before we get here, but not necessarily: we might be
1714 // compiling a preprocessed module, where there is no requirement for the
1715 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00001716 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00001717 if (WritingModule) {
1718 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1719 while (!Worklist.empty()) {
1720 Module *M = Worklist.pop_back_val();
1721 if (!M->isAvailable())
1722 continue;
1723
1724 // Map to disk files where possible, to pick up any missing stat
1725 // information. This also means we don't need to check the unresolved
1726 // headers list when emitting resolved headers in the first loop below.
1727 // FIXME: It'd be preferable to avoid doing this if we were given
1728 // sufficient stat information in the module map.
1729 HS.getModuleMap().resolveHeaderDirectives(M);
1730
1731 // If the file didn't exist, we can still create a module if we were given
1732 // enough information in the module map.
1733 for (auto U : M->MissingHeaders) {
1734 // Check that we were given enough information to build a module
1735 // without this file existing on disk.
1736 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1737 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1738 << WritingModule->getFullModuleName() << U.Size.hasValue()
1739 << U.FileName;
1740 continue;
1741 }
1742
1743 // Form the effective relative pathname for the file.
1744 SmallString<128> Filename(M->Directory->getName());
1745 llvm::sys::path::append(Filename, U.FileName);
1746 PreparePathForOutput(Filename);
1747
1748 StringRef FilenameDup = strdup(Filename.c_str());
1749 SavedStrings.push_back(FilenameDup.data());
1750
1751 HeaderFileInfoTrait::key_type Key = {
1752 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
1753 };
1754 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00001755 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00001756 };
1757 // FIXME: Deal with cases where there are multiple unresolved header
1758 // directives in different submodules for the same header.
1759 Generator.insert(Key, Data, GeneratorTrait);
1760 ++NumHeaderSearchEntries;
1761 }
1762
1763 Worklist.append(M->submodule_begin(), M->submodule_end());
1764 }
1765 }
1766
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001767 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001768 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001769
Douglas Gregor09b69892011-02-10 17:09:37 +00001770 if (FilesByUID.size() > HS.header_file_size())
1771 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00001772
Douglas Gregor09b69892011-02-10 17:09:37 +00001773 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1774 const FileEntry *File = FilesByUID[UID];
1775 if (!File)
1776 continue;
1777
Richard Smith386bb072015-08-18 23:42:23 +00001778 // Get the file info. This will load info from the external source if
1779 // necessary. Skip emitting this file if we have no information on it
1780 // as a header file (in which case HFI will be null) or if it hasn't
1781 // changed since it was loaded. Also skip it if it's for a modular header
1782 // from a different module; in that case, we rely on the module(s)
1783 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001784 const HeaderFileInfo *HFI =
1785 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1786 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001787 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001788
Richard Smith7ed1bc92014-12-05 22:42:13 +00001789 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001790 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001791 SmallString<128> FilenameTmp(Filename);
1792 if (PreparePathForOutput(FilenameTmp)) {
1793 // If we performed any translation on the file name at all, we need to
1794 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001795 Filename = StringRef(strdup(FilenameTmp.c_str()));
1796 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001797 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001798
Richard Smith040e1262017-06-02 01:55:39 +00001799 HeaderFileInfoTrait::key_type Key = {
1800 Filename, File->getSize(), getTimestampForOutput(File)
1801 };
1802 HeaderFileInfoTrait::data_type Data = {
1803 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
1804 };
1805 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001806 ++NumHeaderSearchEntries;
1807 }
Richard Smith040e1262017-06-02 01:55:39 +00001808
Douglas Gregor09b69892011-02-10 17:09:37 +00001809 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001810 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001811 uint32_t BucketOffset;
1812 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001813 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001814
Douglas Gregor09b69892011-02-10 17:09:37 +00001815 llvm::raw_svector_ostream Out(TableData);
1816 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00001817 endian::write<uint32_t>(Out, 0, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00001818 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1819 }
1820
1821 // Create a blob abbreviation
1822 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001823
David Blaikieb44f0bf2017-01-04 22:36:43 +00001824 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00001825 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00001830 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001831
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001832 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001833 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1834 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001835 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001836 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001837
Douglas Gregor09b69892011-02-10 17:09:37 +00001838 // Free all of the strings we had to duplicate.
1839 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001840 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001841}
1842
George Rimarc39f5492017-01-17 15:45:31 +00001843static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
1844 unsigned SLocBufferBlobCompressedAbbrv,
1845 unsigned SLocBufferBlobAbbrv) {
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001846 using RecordDataType = ASTWriter::RecordData::value_type;
George Rimarc39f5492017-01-17 15:45:31 +00001847
1848 // Compress the buffer if possible. We expect that almost all PCM
1849 // consumers will not want its contents.
1850 SmallString<0> CompressedBuffer;
1851 if (llvm::zlib::isAvailable()) {
1852 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
1853 if (!E) {
1854 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
1855 Blob.size() - 1};
1856 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
1857 CompressedBuffer);
1858 return;
1859 }
1860 llvm::consumeError(std::move(E));
1861 }
1862
1863 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
1864 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
1865}
1866
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001867/// Writes the block containing the serialized form of the
Douglas Gregora7f71a92009-04-10 03:52:48 +00001868/// source manager.
1869///
1870/// TODO: We should probably use an on-disk hash table (stored in a
1871/// blob), indexed based on the file name, so that we only create
1872/// entries for files that we actually need. In the common case (no
1873/// errors), we probably won't have to create file entries for any of
1874/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001875void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001876 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001877 RecordData Record;
1878
Chris Lattner0910e3b2009-04-10 17:16:57 +00001879 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00001880 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001881
1882 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001883 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1884 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00001885 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
1886 unsigned SLocBufferBlobCompressedAbbrv =
1887 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001888 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001889
Douglas Gregor258ae542009-04-27 06:38:32 +00001890 // Write out the source location entry table. We skip the first
1891 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001892 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001893 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001894 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1895 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001896 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001897 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001898 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001899 FileID FID = FileID::get(I);
1900 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001901
Douglas Gregor258ae542009-04-27 06:38:32 +00001902 // Record the offset of this source-location entry.
1903 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1904
1905 // Figure out which record code to use.
1906 unsigned Code;
1907 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001908 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1909 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001910 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001911 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001912 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001913 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001914 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001915 Record.clear();
1916 Record.push_back(Code);
1917
Douglas Gregor925296b2011-07-19 16:10:42 +00001918 // Starting offset of this entry within this module, so skip the dummy.
1919 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001920 if (SLoc->isFile()) {
1921 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00001922 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001923 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1924 Record.push_back(File.hasLineDirectives());
1925
1926 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00001927 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001928 if (Content->OrigEntry) {
1929 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001930 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001931
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001932 // The source location entry is a file. Emit input file ID.
1933 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1934 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001935
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001936 Record.push_back(File.NumCreatedFIDs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001937
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001938 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001939 if (FDI != FileDeclIDs.end()) {
1940 Record.push_back(FDI->second->FirstDeclIndex);
1941 Record.push_back(FDI->second->DeclIDs.size());
1942 } else {
1943 Record.push_back(0);
1944 Record.push_back(0);
1945 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001946
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001947 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001948
Richard Smithaada85c2016-02-06 02:06:43 +00001949 if (Content->BufferOverridden || Content->IsTransient)
1950 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00001951 } else {
1952 // The source location entry is a buffer. The blob associated
1953 // with this entry contains the contents of the buffer.
1954
1955 // We add one to the size so that we capture the trailing NULL
1956 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1957 // the reader side).
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001958 const llvm::MemoryBuffer *Buffer =
1959 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00001960 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001961 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00001962 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00001963 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00001964
Mehdi Amini99d1b292016-10-01 16:38:28 +00001965 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00001966 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001967 }
Richard Smithaada85c2016-02-06 02:06:43 +00001968
1969 if (EmitBlob) {
1970 // Include the implicit terminating null character in the on-disk buffer
1971 // if we're writing it uncompressed.
1972 const llvm::MemoryBuffer *Buffer =
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001973 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Richard Smithaada85c2016-02-06 02:06:43 +00001974 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00001975 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
1976 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00001977 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001978 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001979 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001980 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00001981 AddSourceLocation(Expansion.getSpellingLoc(), Record);
1982 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
1983 AddSourceLocation(Expansion.isMacroArgExpansion()
1984 ? SourceLocation()
1985 : Expansion.getExpansionLocEnd(),
1986 Record);
Richard Smithb5f81712018-04-30 05:25:48 +00001987 Record.push_back(Expansion.isExpansionTokenRange());
Douglas Gregor258ae542009-04-27 06:38:32 +00001988
1989 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001990 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001991 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001992 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001993 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001994 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001995 }
1996 }
1997
Douglas Gregor8f45df52009-04-16 22:23:12 +00001998 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001999
2000 if (SLocEntryOffsets.empty())
2001 return;
2002
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002003 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002004 // table is used for lazily loading source-location information.
2005 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002006
David Blaikieb44f0bf2017-01-04 22:36:43 +00002007 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002008 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002012 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002013 {
2014 RecordData::value_type Record[] = {
2015 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2016 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2017 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2018 bytes(SLocEntryOffsets));
2019 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002020 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002021 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002022 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002023
2024 // Write the line table. It depends on remapping working, so it must come
2025 // after the source location offsets.
2026 if (SourceMgr.hasLineTable()) {
2027 LineTableInfo &LineTable = SourceMgr.getLineTable();
2028
2029 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002030
2031 // Emit the needed file names.
2032 llvm::DenseMap<int, int> FilenameMap;
Hans Wennborg14487362017-12-04 22:28:45 +00002033 FilenameMap[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00002034 for (const auto &L : LineTable) {
2035 if (L.first.ID < 0)
2036 continue;
2037 for (auto &LE : L.second) {
2038 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
Hans Wennborg14487362017-12-04 22:28:45 +00002039 FilenameMap.size() - 1)).second)
Richard Smith63078492015-09-01 07:41:55 +00002040 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2041 }
2042 }
2043 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002044
2045 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002046 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002047 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002048 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002049 continue;
2050
2051 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002052 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002053
2054 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002055 Record.push_back(L.second.size());
2056 for (const auto &LE : L.second) {
2057 Record.push_back(LE.FileOffset);
2058 Record.push_back(LE.LineNo);
2059 Record.push_back(FilenameMap[LE.FilenameID]);
2060 Record.push_back((unsigned)LE.FileKind);
2061 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002062 }
2063 }
Richard Smith63078492015-09-01 07:41:55 +00002064
Douglas Gregor925296b2011-07-19 16:10:42 +00002065 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2066 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002067}
2068
Douglas Gregorc5046832009-04-27 18:38:38 +00002069//===----------------------------------------------------------------------===//
2070// Preprocessor Serialization
2071//===----------------------------------------------------------------------===//
2072
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002073static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2074 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002075 if (MacroInfo *MI = MD->getMacroInfo())
2076 if (MI->isBuiltinMacro())
2077 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002078
2079 if (IsModule) {
2080 SourceLocation Loc = MD->getLocation();
2081 if (Loc.isInvalid())
2082 return true;
2083 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2084 return true;
2085 }
2086
2087 return false;
2088}
2089
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002090/// Writes the block containing the serialized form of the
Chris Lattnereeffaef2009-04-10 17:15:23 +00002091/// preprocessor.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002092void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002093 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2094 if (PPRec)
2095 WritePreprocessorDetail(*PPRec);
2096
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002097 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002098 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002099
Chris Lattner0af3ba12009-04-13 01:29:17 +00002100 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2101 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002102 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002103 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002104 }
2105
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002106 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2107 assert(!IsModule);
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00002108 auto SkipInfo = PP.getPreambleSkipInfo();
2109 if (SkipInfo.hasValue()) {
2110 Record.push_back(true);
2111 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2112 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2113 Record.push_back(SkipInfo->FoundNonSkipPortion);
2114 Record.push_back(SkipInfo->FoundElse);
2115 AddSourceLocation(SkipInfo->ElseLoc, Record);
2116 } else {
2117 Record.push_back(false);
2118 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002119 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2120 AddSourceLocation(Cond.IfLoc, Record);
2121 Record.push_back(Cond.WasSkipping);
2122 Record.push_back(Cond.FoundNonSkip);
2123 Record.push_back(Cond.FoundElse);
2124 }
2125 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2126 Record.clear();
2127 }
2128
Douglas Gregoreda6a892009-04-26 00:07:37 +00002129 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002130 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002131
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002132 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002133 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002134 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002135 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002136
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002137 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002138 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002139
Richard Smithee977932015-05-01 21:22:17 +00002140 // Construct the list of identifiers with macro directives that need to be
2141 // serialized.
2142 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2143 for (auto &Id : PP.getIdentifierTable())
2144 if (Id.second->hadMacroDefinition() &&
2145 (!Id.second->isFromAST() ||
2146 Id.second->hasChangedSinceDeserialization()))
2147 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002148 // Sort the set of macro definitions that need to be serialized by the
2149 // name of the macro, to provide a stable ordering.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00002150 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002151
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002152 // Emit the macro directives as a list and associate the offset with the
2153 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002154 for (const IdentifierInfo *Name : MacroIdentifiers) {
2155 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002156 auto StartOffset = Stream.GetCurrentBitNo();
2157
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002158 // Emit the macro directives in reverse source order.
2159 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002160 // Once we hit an ignored macro, we're done: the rest of the chain
2161 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002162 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002163 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002164
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002165 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002166 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002167 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002168 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002169 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002170 Record.push_back(VisMD->isPublic());
2171 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002172 }
Richard Smithd7329392015-04-21 21:46:32 +00002173
Richard Smithb8b2ed62015-04-23 18:18:26 +00002174 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002175 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002176 // We write out exported module macros for PCH as well.
2177 auto Leafs = PP.getLeafModuleMacros(Name);
2178 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2179 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2180 while (!Worklist.empty()) {
2181 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002182
Manman Ren33d80292016-05-31 18:19:32 +00002183 // Emit a record indicating this submodule exports this macro.
2184 ModuleMacroRecord.push_back(
2185 getSubmoduleID(Macro->getOwningModule()));
2186 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2187 for (auto *M : Macro->overrides())
2188 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002189
Manman Ren33d80292016-05-31 18:19:32 +00002190 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2191 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002192
Manman Ren33d80292016-05-31 18:19:32 +00002193 // Enqueue overridden macros once we've visited all their ancestors.
2194 for (auto *M : Macro->overrides())
2195 if (++Visits[M] == M->getNumOverridingMacros())
2196 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002197
Manman Ren33d80292016-05-31 18:19:32 +00002198 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002199 }
Richard Smithd7329392015-04-21 21:46:32 +00002200
Richard Smithee977932015-05-01 21:22:17 +00002201 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002202 continue;
2203
Richard Smithd7329392015-04-21 21:46:32 +00002204 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002205 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2206 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002207 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002209 /// Offsets of each of the macros into the bitstream, indexed by
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002210 /// the local macro ID
2211 ///
2212 /// For each identifier that is associated with a macro, this map
2213 /// provides the offset into the bitstream where that macro is
2214 /// defined.
2215 std::vector<uint32_t> MacroOffsets;
2216
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002217 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2218 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2219 MacroInfo *MI = MacroInfosToEmit[I].MI;
2220 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002221
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002222 if (ID < FirstMacroID) {
2223 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2224 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002225 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002226
2227 // Record the local offset of this macro.
2228 unsigned Index = ID - FirstMacroID;
2229 if (Index == MacroOffsets.size())
2230 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2231 else {
2232 if (Index > MacroOffsets.size())
2233 MacroOffsets.resize(Index + 1);
2234
2235 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2236 }
2237
2238 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002239 AddSourceLocation(MI->getDefinitionLoc(), Record);
2240 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2241 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002242 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002243 unsigned Code;
2244 if (MI->isObjectLike()) {
2245 Code = PP_MACRO_OBJECT_LIKE;
2246 } else {
2247 Code = PP_MACRO_FUNCTION_LIKE;
2248
2249 Record.push_back(MI->isC99Varargs());
2250 Record.push_back(MI->isGNUVarargs());
2251 Record.push_back(MI->hasCommaPasting());
Faisal Valiac506d72017-07-17 17:18:43 +00002252 Record.push_back(MI->getNumParams());
2253 for (const IdentifierInfo *Param : MI->params())
2254 AddIdentifierRef(Param, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002255 }
2256
2257 // If we have a detailed preprocessing record, record the macro definition
2258 // ID that corresponds to this macro.
2259 if (PPRec)
2260 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2261
2262 Stream.EmitRecord(Code, Record);
2263 Record.clear();
2264
2265 // Emit the tokens array.
2266 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2267 // Note that we know that the preprocessor does not have any annotation
2268 // tokens in it because they are created by the parser, and thus can't
2269 // be in a macro definition.
2270 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002271 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002272 Stream.EmitRecord(PP_TOKEN, Record);
2273 Record.clear();
2274 }
2275 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002276 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002277
Douglas Gregor92a96f52011-02-08 21:58:10 +00002278 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002279
2280 // Write the offsets table for macro IDs.
2281 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002282
David Blaikieb44f0bf2017-01-04 22:36:43 +00002283 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002284 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2288
David Blaikieb44f0bf2017-01-04 22:36:43 +00002289 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002290 {
2291 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2292 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2293 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2294 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002295}
2296
2297void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002298 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002299 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002300
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002301 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002302
Douglas Gregor92a96f52011-02-08 21:58:10 +00002303 // Enter the preprocessor block.
2304 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002305
Douglas Gregoraae92242010-03-19 21:51:54 +00002306 // If the preprocessor has a preprocessing record, emit it.
2307 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002308 using namespace llvm;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002309
2310 // Set up the abbreviation for
Douglas Gregor92a96f52011-02-08 21:58:10 +00002311 unsigned InclusionAbbrev = 0;
2312 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002313 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002314 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002319 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002320 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002321 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002322
2323 unsigned FirstPreprocessorEntityID
2324 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002325 + NUM_PREDEF_PP_ENTITY_IDS;
2326 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002327 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002328 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2329 EEnd = PPRec.local_end();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002330 E != EEnd;
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002331 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002332 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002333
Richard Smith66a81862015-05-04 02:25:31 +00002334 PreprocessedEntityOffsets.push_back(
2335 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002336
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002337 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002338 // Record this macro definition's ID.
2339 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002340
Douglas Gregor92a96f52011-02-08 21:58:10 +00002341 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002342 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2343 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002344 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002345
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002346 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002347 Record.push_back(ME->isBuiltinMacro());
2348 if (ME->isBuiltinMacro())
2349 AddIdentifierRef(ME->getName(), Record);
2350 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002351 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002352 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002353 continue;
2354 }
2355
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002356 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002357 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002358 Record.push_back(ID->getFileName().size());
2359 Record.push_back(ID->wasInQuotes());
2360 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002361 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002362 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002363 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002364 // Check that the FileEntry is not null because it was not resolved and
2365 // we create a PCH even with compiler errors.
2366 if (ID->getFile())
2367 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002368 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002369 continue;
2370 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002371
Douglas Gregor92a96f52011-02-08 21:58:10 +00002372 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2373 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002374 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002375
Douglas Gregoraae92242010-03-19 21:51:54 +00002376 // Write the offsets table for the preprocessing record.
2377 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002378 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2379
Douglas Gregoraae92242010-03-19 21:51:54 +00002380 // Write the offsets table for identifier IDs.
2381 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002382
David Blaikieb44f0bf2017-01-04 22:36:43 +00002383 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002384 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002387 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002388
Mehdi Amini57a41912015-09-10 01:46:39 +00002389 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2390 FirstPreprocessorEntityID -
2391 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002392 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002393 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002394 }
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00002395
2396 // Write the skipped region table for the preprocessing record.
2397 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2398 if (SkippedRanges.size() > 0) {
2399 std::vector<PPSkippedRange> SerializedSkippedRanges;
2400 SerializedSkippedRanges.reserve(SkippedRanges.size());
2401 for (auto const& Range : SkippedRanges)
2402 SerializedSkippedRanges.emplace_back(Range);
2403
2404 using namespace llvm;
2405 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2406 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2408 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2409
2410 Record.clear();
2411 Record.push_back(PPD_SKIPPED_RANGES);
2412 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2413 bytes(SerializedSkippedRanges));
2414 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002415}
2416
Richard Smith386bb072015-08-18 23:42:23 +00002417unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002418 if (!Mod)
2419 return 0;
2420
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002421 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2422 if (Known != SubmoduleIDs.end())
2423 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002424
Richard Smithdc1f0422016-07-20 19:10:16 +00002425 auto *Top = Mod->getTopLevelModule();
2426 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002427 (getLangOpts().CompilingPCH ||
2428 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002429 return 0;
2430
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002431 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2432}
2433
Richard Smith386bb072015-08-18 23:42:23 +00002434unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2435 // FIXME: This can easily happen, if we have a reference to a submodule that
2436 // did not result in us loading a module file for that submodule. For
2437 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2438 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2439 assert((ID || !Mod) &&
2440 "asked for module ID for non-local, non-imported module");
2441 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002442}
2443
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002444/// Compute the number of modules within the given tree (including the
Douglas Gregor253eefe2011-12-01 00:59:36 +00002445/// given module).
2446static unsigned getNumberOfModules(Module *Mod) {
2447 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002448 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002449 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002450 ChildModules += getNumberOfModules(*Sub);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002451
Douglas Gregor253eefe2011-12-01 00:59:36 +00002452 return ChildModules + 1;
2453}
2454
Douglas Gregorde3ef502011-11-30 23:21:26 +00002455void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002456 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002457 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002458
Douglas Gregor69021972011-11-30 17:33:56 +00002459 // Write the abbreviations needed for the submodules block.
2460 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002461
David Blaikieb44f0bf2017-01-04 22:36:43 +00002462 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002463 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
Richard Smithdd8b5332017-09-04 05:37:53 +00002466 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
Douglas Gregor69021972011-11-30 17:33:56 +00002467 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2468 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2470 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002473 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002475 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
Douglas Gregor69021972011-11-30 17:33:56 +00002476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002477 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002478
David Blaikieb44f0bf2017-01-04 22:36:43 +00002479 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002480 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002481 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002482 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002483
David Blaikieb44f0bf2017-01-04 22:36:43 +00002484 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002485 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2486 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002487 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002488
David Blaikieb44f0bf2017-01-04 22:36:43 +00002489 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002490 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2491 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002492 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002493
David Blaikieb44f0bf2017-01-04 22:36:43 +00002494 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002495 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002497 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002498
David Blaikieb44f0bf2017-01-04 22:36:43 +00002499 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002500 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002503 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002504
David Blaikieb44f0bf2017-01-04 22:36:43 +00002505 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002506 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2507 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002508 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002509
David Blaikieb44f0bf2017-01-04 22:36:43 +00002510 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002511 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002513 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002514
David Blaikieb44f0bf2017-01-04 22:36:43 +00002515 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002516 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2517 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002518 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002519
David Blaikieb44f0bf2017-01-04 22:36:43 +00002520 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002521 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002523 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002524
David Blaikieb44f0bf2017-01-04 22:36:43 +00002525 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002526 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002529 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002530
David Blaikieb44f0bf2017-01-04 22:36:43 +00002531 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002532 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002534 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002535
David Blaikieb44f0bf2017-01-04 22:36:43 +00002536 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002537 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002540 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002541
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002542 Abbrev = std::make_shared<BitCodeAbbrev>();
2543 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2545 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2546
Douglas Gregor253eefe2011-12-01 00:59:36 +00002547 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002548 RecordData::value_type Record[] = {
2549 getNumberOfModules(WritingModule),
Richard Smithdd8b5332017-09-04 05:37:53 +00002550 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002551 Stream.EmitRecord(SUBMODULE_METADATA, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002552
Douglas Gregor69021972011-11-30 17:33:56 +00002553 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002554 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002555 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002556 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002557 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002558 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002559 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002560
2561 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002562 if (Mod->Parent) {
2563 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002564 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002565 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002566
2567 // Emit the definition of the block.
2568 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002569 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2570 ID,
2571 ParentID,
Simon Pilgrimf331a662017-09-04 10:54:39 +00002572 (RecordData::value_type)Mod->Kind,
David Blaikie9ffe5a32017-01-30 05:00:26 +00002573 Mod->IsFramework,
2574 Mod->IsExplicit,
2575 Mod->IsSystem,
2576 Mod->IsExternC,
2577 Mod->InferSubmodules,
2578 Mod->InferExplicitSubmodules,
2579 Mod->InferExportWildcard,
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002580 Mod->ConfigMacrosExhaustive,
2581 Mod->ModuleMapIsPrivate};
Mehdi Amini57a41912015-09-10 01:46:39 +00002582 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2583 }
2584
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002585 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002586 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002587 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002588 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002589 }
2590
Douglas Gregor69021972011-11-30 17:33:56 +00002591 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002592 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002593 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002594 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2595 UmbrellaHeader.NameAsWritten);
2596 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002597 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2598 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002599 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002600 }
Richard Smith306d8922014-10-22 23:50:56 +00002601
Douglas Gregor69021972011-11-30 17:33:56 +00002602 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002603 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002604 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002605 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002606 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002607 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002608 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2609 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2610 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002611 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002612 Module::HK_PrivateTextual},
2613 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002614 };
2615 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002616 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002617 for (auto &H : Mod->Headers[HL.HeaderKind])
2618 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2619 }
2620
2621 // Emit the top headers.
2622 {
2623 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002624 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002625 for (auto *H : TopHeaders)
2626 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002627 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002628
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002629 // Emit the imports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002630 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002631 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002632 for (auto *I : Mod->Imports)
2633 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002634 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2635 }
2636
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002637 // Emit the exports.
Douglas Gregor24bb9232011-12-02 18:58:38 +00002638 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002639 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002640 for (const auto &E : Mod->Exports) {
2641 // FIXME: This may fail; we don't require that all exported modules
2642 // are local or imported.
2643 Record.push_back(getSubmoduleID(E.getPointer()));
2644 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002645 }
2646 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2647 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002648
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002649 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2650 // Might be unnecessary as use declarations are only used to build the
2651 // module itself.
2652
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002653 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002654 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002655 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2656 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002657 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002658 }
2659
Douglas Gregorfb912652013-03-20 21:10:35 +00002660 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002661 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002662 // FIXME: This may fail; we don't require that all conflicting modules
2663 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002664 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2665 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002666 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002667 }
2668
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002669 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002670 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002671 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002672 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002673 }
2674
Richard Smithdc1f0422016-07-20 19:10:16 +00002675 // Emit the initializers, if any.
2676 RecordData Inits;
2677 for (Decl *D : Context->getModuleInitializers(Mod))
2678 Inits.push_back(GetDeclRef(D));
2679 if (!Inits.empty())
2680 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2681
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002682 // Emit the name of the re-exported module, if any.
2683 if (!Mod->ExportAsModule.empty()) {
2684 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
2685 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
2686 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002687
Douglas Gregor69021972011-11-30 17:33:56 +00002688 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002689 for (auto *M : Mod->submodules())
2690 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002691 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002692
Douglas Gregor69021972011-11-30 17:33:56 +00002693 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002694
Richard Smith23d8d032015-05-14 00:45:20 +00002695 assert((NextSubmoduleID - FirstSubmoduleID ==
2696 getNumberOfModules(WritingModule)) &&
2697 "Wrong # of submodules; found a reference to a non-local, "
2698 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002699}
2700
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002701void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2702 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002703 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2704 DiagStateIDMap;
2705 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002706 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002707
Richard Smithe37391c2017-05-03 00:28:49 +00002708 auto EncodeDiagStateFlags =
2709 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
2710 unsigned Result = (unsigned)DS->ExtBehavior;
2711 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00002712 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
2713 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
2714 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00002715 Result = (Result << 1) | Val;
2716 return Result;
2717 };
2718
2719 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
2720 Record.push_back(Flags);
2721
Richard Smithd230de22017-01-26 01:01:01 +00002722 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2723 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00002724 // Ensure that the diagnostic state wasn't modified since it was created.
2725 // We will not correctly round-trip this information otherwise.
2726 assert(Flags == EncodeDiagStateFlags(State) &&
2727 "diag state flags vary in single AST file");
2728
Richard Smithd230de22017-01-26 01:01:01 +00002729 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002730 Record.push_back(DiagStateID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002731
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002732 if (DiagStateID == 0) {
2733 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002734
2735 // Add a placeholder for the number of mappings.
2736 auto SizeIdx = Record.size();
2737 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00002738 for (const auto &I : *State) {
2739 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002740 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00002741 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002742 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002743 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002744 // Update the placeholder.
2745 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00002746 }
2747 };
2748
2749 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00002750
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002751 // Reserve a spot for the number of locations with state transitions.
2752 auto NumLocationsIdx = Record.size();
2753 Record.emplace_back();
2754
2755 // Emit the state transitions.
2756 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00002757 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2758 if (!FileIDAndFile.first.isValid() ||
2759 !FileIDAndFile.second.HasLocalTransitions)
2760 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002761 ++NumLocations;
Richard Smith6c2b5a82018-02-09 01:15:13 +00002762
2763 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
2764 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
2765 AddSourceLocation(Loc, Record);
2766
Richard Smithd230de22017-01-26 01:01:01 +00002767 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2768 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2769 Record.push_back(StatePoint.Offset);
2770 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002771 }
2772 }
2773
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002774 // Backpatch the number of locations.
2775 Record[NumLocationsIdx] = NumLocations;
2776
2777 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00002778 //
2779 // This also protects against a hypothetical corner case with simulating
2780 // -Werror settings for implicit modules in the ASTReader, where reading
2781 // CurDiagState out of context could change whether warning pragmas are
2782 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002783 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2784 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2785
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00002786 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002787}
2788
Douglas Gregorc5046832009-04-27 18:38:38 +00002789//===----------------------------------------------------------------------===//
2790// Type Serialization
2791//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002792
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002793/// Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002794void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002795 TypeIdx &IdxRef = TypeIdxs[T];
2796 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2797 IdxRef = TypeIdx(NextTypeID++);
2798 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002799
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002800 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002801
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002802 // Emit the type's representation.
John McCalld505e572019-12-13 21:54:44 -05002803 uint64_t Offset = ASTTypeWriter(*this).write(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002804
Richard Smith290d8012016-04-06 17:06:00 +00002805 // Record the offset for this type.
2806 unsigned Index = Idx.getIndex() - FirstTypeID;
2807 if (TypeOffsets.size() == Index)
2808 TypeOffsets.push_back(Offset);
2809 else if (TypeOffsets.size() < Index) {
2810 TypeOffsets.resize(Index + 1);
2811 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002812 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002813 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002814 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002815}
2816
Douglas Gregorc5046832009-04-27 18:38:38 +00002817//===----------------------------------------------------------------------===//
2818// Declaration Serialization
2819//===----------------------------------------------------------------------===//
2820
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002821/// Write the block containing all of the declaration IDs
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002822/// lexically declared within the given DeclContext.
2823///
2824/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002825/// bitstream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002826uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002827 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002828 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002829 return 0;
2830
Douglas Gregor8f45df52009-04-16 22:23:12 +00002831 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002832 SmallVector<uint32_t, 128> KindDeclPairs;
2833 for (const auto *D : DC->decls()) {
2834 KindDeclPairs.push_back(D->getKind());
2835 KindDeclPairs.push_back(GetDeclRef(D));
2836 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002837
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002838 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002839 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002840 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2841 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002842 return Offset;
2843}
2844
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002845void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002846 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002847
2848 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002849 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002850 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002853 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002854 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002855 {
2856 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2857 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2858 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2859 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002860
2861 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002862 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002863 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002867 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002868 {
2869 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2870 FirstDeclID - NUM_PREDEF_DECL_IDS};
2871 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2872 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002873}
2874
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002875void ASTWriter::WriteFileDeclIDsMap() {
2876 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002877
Chandler Carruthb306d732015-03-27 00:31:20 +00002878 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2879 FileDeclIDs.begin(), FileDeclIDs.end());
Fangrui Song55fab262018-09-26 22:16:28 +00002880 llvm::sort(SortedFileDeclIDs, llvm::less_first());
Chandler Carruthb306d732015-03-27 00:31:20 +00002881
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002882 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002883 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2884 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2885 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2886 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2887 for (auto &LocDeclEntry : Info.DeclIDs)
2888 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002889 }
2890
David Blaikieb44f0bf2017-01-04 22:36:43 +00002891 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002892 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002895 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002896 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2897 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002898 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002899}
2900
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002901void ASTWriter::WriteComments() {
2902 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Ilya Biryukov45643102018-07-09 11:33:23 +00002903 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
2904 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
2905 return;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002906 RecordData Record;
Jan Korousf31d8df2019-08-13 18:11:44 +00002907 for (const auto &FO : Context->Comments.OrderedComments) {
2908 for (const auto &OC : FO.second) {
2909 const RawComment *I = OC.second;
2910 Record.clear();
2911 AddSourceRange(I->getSourceRange(), Record);
2912 Record.push_back(I->getKind());
2913 Record.push_back(I->isTrailingComment());
2914 Record.push_back(I->isAlmostTrailingComment());
2915 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2916 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002917 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002918}
2919
Douglas Gregorc5046832009-04-27 18:38:38 +00002920//===----------------------------------------------------------------------===//
2921// Global Method Pool and Selector Serialization
2922//===----------------------------------------------------------------------===//
2923
Douglas Gregore84a9da2009-04-20 20:36:09 +00002924namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002925
Douglas Gregorc78d3462009-04-24 21:10:55 +00002926// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002927class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002928 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002929
2930public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002931 using key_type = Selector;
2932 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00002933
Sebastian Redl834bb972010-08-04 17:20:04 +00002934 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002935 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002936 ObjCMethodList Instance, Factory;
2937 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002938 using data_type_ref = const data_type &;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002939
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002940 using hash_value_type = unsigned;
2941 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00002942
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002943 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
Mike Stump11289f42009-09-09 15:08:12 +00002944
Justin Bogner25463f12014-04-18 20:27:24 +00002945 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002946 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002947 }
Mike Stump11289f42009-09-09 15:08:12 +00002948
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002949 std::pair<unsigned, unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002950 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002951 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002952 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002953
Peter Collingbournee3f65292018-05-18 19:46:24 +00002954 endian::Writer LE(Out, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002955 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002956 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002957 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2958 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002959 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002960 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002961 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002962 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002963 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002964 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002965 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002966 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002967 return std::make_pair(KeyLen, DataLen);
2968 }
Mike Stump11289f42009-09-09 15:08:12 +00002969
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002970 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002971 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002972
Peter Collingbournee3f65292018-05-18 19:46:24 +00002973 endian::Writer LE(Out, little);
Mike Stump11289f42009-09-09 15:08:12 +00002974 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002975 assert((Start >> 32) == 0 && "Selector key offset too large");
2976 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002977 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002978 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002979 if (N == 0)
2980 N = 1;
2981 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002982 LE.write<uint32_t>(
2983 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002984 }
Mike Stump11289f42009-09-09 15:08:12 +00002985
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002986 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002987 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002988 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002989
Peter Collingbournee3f65292018-05-18 19:46:24 +00002990 endian::Writer LE(Out, little);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002991 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002992 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002993 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002994 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002995 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002996 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002997 ++NumInstanceMethods;
2998
2999 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003000 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003001 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003002 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003003 ++NumFactoryMethods;
3004
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003005 unsigned InstanceBits = Methods.Instance.getBits();
3006 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003007 unsigned InstanceHasMoreThanOneDeclBit =
3008 Methods.Instance.hasMoreThanOneDecl();
3009 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3010 (InstanceHasMoreThanOneDeclBit << 2) |
3011 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003012 unsigned FactoryBits = Methods.Factory.getBits();
3013 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003014 unsigned FactoryHasMoreThanOneDeclBit =
3015 Methods.Factory.hasMoreThanOneDecl();
3016 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3017 (FactoryHasMoreThanOneDeclBit << 2) |
3018 FactoryBits;
3019 LE.write<uint16_t>(FullInstanceBits);
3020 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003021 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003022 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003023 if (Method->getMethod())
3024 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003025 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003026 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003027 if (Method->getMethod())
3028 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003029
3030 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003031 }
3032};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003033
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003034} // namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +00003035
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003036/// Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003037///
3038/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003039/// in an on-disk hash table indexed by the selector. The hash table also
3040/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003041void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003042 using namespace llvm;
3043
Sebastian Redla19a67f2010-08-03 21:58:15 +00003044 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003045 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003046 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003047 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003048 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003049 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003050 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003051 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003052
Sebastian Redla19a67f2010-08-03 21:58:15 +00003053 // Create the on-disk hash table representation. We walk through every
3054 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003055 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003056 for (auto &SelectorAndID : SelectorIDs) {
3057 Selector S = SelectorAndID.first;
3058 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003059 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003060 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003061 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003062 ObjCMethodList(),
3063 ObjCMethodList()
3064 };
3065 if (F != SemaRef.MethodPool.end()) {
3066 Data.Instance = F->second.first;
3067 Data.Factory = F->second.second;
3068 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003069 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003070 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003071 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003072 // Selector already exists. Did it change?
3073 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003074 for (ObjCMethodList *M = &Data.Instance;
3075 !changed && M && M->getMethod(); M = M->getNext()) {
3076 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003077 changed = true;
3078 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003079 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003080 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003081 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003082 changed = true;
3083 }
3084 if (!changed)
3085 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003086 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003087 // A new method pool entry.
3088 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003089 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003090 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003091 }
3092
Douglas Gregorc78d3462009-04-24 21:10:55 +00003093 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003094 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003095 uint32_t BucketOffset;
3096 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003097 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003098
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003099 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003100 llvm::raw_svector_ostream Out(MethodPool);
3101 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003102 endian::write<uint32_t>(Out, 0, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003103 BucketOffset = Generator.Emit(Out, Trait);
3104 }
3105
3106 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003107 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003108 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003109 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003110 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003111 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003112 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003113
Douglas Gregor95c13f52009-04-25 17:48:32 +00003114 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003115 {
3116 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3117 NumTableEntries};
3118 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3119 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003120
3121 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003122 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003123 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003127 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003128
3129 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003130 {
3131 RecordData::value_type Record[] = {
3132 SELECTOR_OFFSETS, SelectorOffsets.size(),
3133 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3134 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3135 bytes(SelectorOffsets));
3136 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003137 }
3138}
3139
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003140/// Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003141void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003142 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003143
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003144 if (SemaRef.ReferencedSelectors.empty())
3145 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003146
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003147 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003148 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003150 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003151 // very tricky to fix, and given that @selector shouldn't really appear in
3152 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003153 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3154 Selector Sel = SelectorAndLocation.first;
3155 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003156 Writer.AddSelectorRef(Sel);
3157 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003158 }
Richard Smithe64e7452016-04-18 21:54:58 +00003159 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003160}
3161
Douglas Gregorc5046832009-04-27 18:38:38 +00003162//===----------------------------------------------------------------------===//
3163// Identifier Table Serialization
3164//===----------------------------------------------------------------------===//
3165
Richard Smithb3761912015-02-05 23:08:52 +00003166/// Determine the declaration that should be put into the name lookup table to
3167/// represent the given declaration in this module. This is usually D itself,
3168/// but if D was imported and merged into a local declaration, we want the most
3169/// recent local declaration instead. The chosen declaration will be the most
3170/// recent declaration in any module that imports this one.
3171static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3172 NamedDecl *D) {
3173 if (!LangOpts.Modules || !D->isFromASTFile())
3174 return D;
3175
3176 if (Decl *Redecl = D->getPreviousDecl()) {
3177 // For Redeclarable decls, a prior declaration might be local.
3178 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003179 // If we find a local decl, we're done.
3180 if (!Redecl->isFromASTFile()) {
3181 // Exception: in very rare cases (for injected-class-names), not all
3182 // redeclarations are in the same semantic context. Skip ones in a
3183 // different context. They don't go in this lookup table at all.
3184 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3185 D->getDeclContext()->getRedeclContext()))
3186 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003187 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003188 }
3189
Richard Smithfe620d22015-03-05 23:24:12 +00003190 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003191 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003192 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003193 break;
3194 }
3195 } else if (Decl *First = D->getCanonicalDecl()) {
3196 // For Mergeable decls, the first decl might be local.
3197 if (!First->isFromASTFile())
3198 return cast<NamedDecl>(First);
3199 }
3200
3201 // All declarations are imported. Our most recent declaration will also be
3202 // the most recent one in anyone who imports us.
3203 return D;
3204}
3205
Douglas Gregorc78d3462009-04-24 21:10:55 +00003206namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003207
Richard Smithcf97cf62015-04-19 01:34:23 +00003208class ASTIdentifierTableTrait {
3209 ASTWriter &Writer;
3210 Preprocessor &PP;
3211 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003212 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003213 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003214 ASTWriter::RecordData *InterestingIdentifierOffsets;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003216 /// Determines whether this is an "interesting" identifier that needs a
Richard Smithcf97cf62015-04-19 01:34:23 +00003217 /// full IdentifierInfo structure written into the hash table. Notably, this
3218 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3219 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003220 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003221 if (MacroOffset ||
3222 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003223 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003224 II->hasRevertedTokenIDToIdentifier() ||
Bruno Ricci366ba732018-09-21 12:53:22 +00003225 (NeedDecls && II->getFETokenInfo()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003226 return true;
3227
3228 return false;
3229 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003230
Douglas Gregore84a9da2009-04-20 20:36:09 +00003231public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003232 using key_type = IdentifierInfo *;
3233 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003234
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003235 using data_type = IdentID;
3236 using data_type_ref = data_type;
Mike Stump11289f42009-09-09 15:08:12 +00003237
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003238 using hash_value_type = unsigned;
3239 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003240
Richard Smithd7329392015-04-21 21:46:32 +00003241 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003242 IdentifierResolver &IdResolver, bool IsModule,
3243 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003244 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003245 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3246 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003247
Richard Smithd79514e2016-02-05 19:03:40 +00003248 bool needDecls() const { return NeedDecls; }
3249
Justin Bogner25463f12014-04-18 20:27:24 +00003250 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003251 return llvm::djbHash(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003252 }
Mike Stump11289f42009-09-09 15:08:12 +00003253
Richard Smith33e0f7e2015-07-22 02:08:40 +00003254 bool isInterestingIdentifier(const IdentifierInfo *II) {
3255 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3256 return isInterestingIdentifier(II, MacroOffset);
3257 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003258
Richard Smith9c254182015-07-19 21:41:12 +00003259 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3260 return isInterestingIdentifier(II, 0);
3261 }
3262
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003263 std::pair<unsigned, unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003264 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003265 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003266 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003267 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3268 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003269 DataLen += 2; // 2 bytes for builtin ID
3270 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003271 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003272 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003273
Richard Smitha534a312015-07-21 23:54:07 +00003274 if (NeedDecls) {
3275 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3276 DEnd = IdResolver.end();
3277 D != DEnd; ++D)
3278 DataLen += 4;
3279 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003280 }
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003281
Justin Bognere1c147c2014-03-28 22:03:19 +00003282 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003283
Peter Collingbournee3f65292018-05-18 19:46:24 +00003284 endian::Writer LE(Out, little);
Justin Bognere1c147c2014-03-28 22:03:19 +00003285
Richard Smithdf8a8312015-03-13 04:05:01 +00003286 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003287 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003288 // We emit the key length after the data length so that every
3289 // string is preceded by a 16-bit length. This matches the PTH
3290 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003291 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003292 return std::make_pair(KeyLen, DataLen);
3293 }
Mike Stump11289f42009-09-09 15:08:12 +00003294
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003295 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003296 unsigned KeyLen) {
3297 // Record the location of the key data. This is used when generating
3298 // the mapping from persistent IDs to strings.
3299 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003300
3301 // Emit the offset of the key/data length information to the interesting
3302 // identifiers table if necessary.
3303 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3304 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3305
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003306 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003307 }
Mike Stump11289f42009-09-09 15:08:12 +00003308
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003309 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003310 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003311 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003312
Peter Collingbournee3f65292018-05-18 19:46:24 +00003313 endian::Writer LE(Out, little);
Richard Smithcf97cf62015-04-19 01:34:23 +00003314
Richard Smithd7329392015-04-21 21:46:32 +00003315 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3316 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003317 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003318 return;
3319 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003320
Justin Bognere1c147c2014-03-28 22:03:19 +00003321 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003322 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3323 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003324 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003325 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003326 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003327 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003328 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3329 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003330 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003331 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003332 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003333 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003334
Richard Smithd7329392015-04-21 21:46:32 +00003335 if (HadMacroDefinition)
3336 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003337
Richard Smitha534a312015-07-21 23:54:07 +00003338 if (NeedDecls) {
3339 // Emit the declaration IDs in reverse order, because the
3340 // IdentifierResolver provides the declarations as they would be
3341 // visible (e.g., the function "stat" would come before the struct
3342 // "stat"), but the ASTReader adds declarations to the end of the list
3343 // (so we need to see the struct "stat" before the function "stat").
3344 // Only emit declarations that aren't from a chained PCH, though.
3345 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3346 IdResolver.end());
3347 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3348 DEnd = Decls.rend();
3349 D != DEnd; ++D)
3350 LE.write<uint32_t>(
3351 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3352 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003353 }
3354};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003355
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003356} // namespace
Douglas Gregore84a9da2009-04-20 20:36:09 +00003357
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003358/// Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003359///
3360/// The identifier table consists of a blob containing string data
3361/// (the actual identifiers themselves) and a separate "offsets" index
3362/// that maps identifier IDs to locations within the blob.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003363void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003364 IdentifierResolver &IdResolver,
3365 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003366 using namespace llvm;
3367
Richard Smith33e0f7e2015-07-22 02:08:40 +00003368 RecordData InterestingIdents;
3369
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003370 // Create and write out the blob that contains the identifier
3371 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003372 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003373 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003374 ASTIdentifierTableTrait Trait(
3375 *this, PP, IdResolver, IsModule,
3376 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003377
Douglas Gregore6648fb2009-04-28 20:33:11 +00003378 // Look for any identifiers that were named while processing the
3379 // headers, but are otherwise not needed. We add these to the hash
3380 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003381 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003382 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003383 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003384 for (const auto &ID : PP.getIdentifierTable())
3385 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003386 // Sort the identifiers lexicographically before getting them references so
3387 // that their order is stable.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00003388 llvm::sort(IIs, llvm::deref<std::less<>>());
Chandler Carruth8440c982015-03-26 23:54:15 +00003389 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003390 if (Trait.isInterestingNonMacroIdentifier(II))
3391 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003392
Sebastian Redlff4a2952010-07-23 23:49:55 +00003393 // Create the on-disk hash table representation. We only store offsets
3394 // for identifiers that appear here for the first time.
3395 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003396 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003397 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003398 IdentID ID = IdentIDPair.second;
3399 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003400 // Write out identifiers if either the ID is local or the identifier has
3401 // changed since it was loaded.
3402 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3403 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003404 (Trait.needDecls() &&
3405 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003406 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003407 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003408
Douglas Gregore84a9da2009-04-20 20:36:09 +00003409 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003410 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003411 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003412 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003413 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003414
Douglas Gregore84a9da2009-04-20 20:36:09 +00003415 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003416 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003417 endian::write<uint32_t>(Out, 0, little);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003418 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003419 }
3420
3421 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003422 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003423 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003426 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003427
3428 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003429 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003430 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003431 }
3432
3433 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003434 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003435 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003439 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003440
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003441#ifndef NDEBUG
3442 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3443 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3444#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003445
3446 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3447 IdentifierOffsets.size(),
3448 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003449 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003450 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003451
3452 // In C++, write the list of interesting identifiers (those that are
3453 // defined as macros, poisoned, or similar unusual things).
3454 if (!InterestingIdents.empty())
3455 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003456}
3457
Douglas Gregorc5046832009-04-27 18:38:38 +00003458//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003459// DeclContext's Name Lookup Table Serialization
3460//===----------------------------------------------------------------------===//
3461
3462namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003463
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003464// Trait used for the on-disk hash table used in the method pool.
3465class ASTDeclContextNameLookupTrait {
3466 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003467 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003468
3469public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003470 using key_type = DeclarationNameKey;
3471 using key_type_ref = key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003472
Richard Smithd88a7f12015-09-01 20:35:42 +00003473 /// A start and end index into DeclIDs, representing a sequence of decls.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003474 using data_type = std::pair<unsigned, unsigned>;
3475 using data_type_ref = const data_type &;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003476
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003477 using hash_value_type = unsigned;
3478 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003479
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003480 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003481
Richard Smithd88a7f12015-09-01 20:35:42 +00003482 template<typename Coll>
3483 data_type getData(const Coll &Decls) {
3484 unsigned Start = DeclIDs.size();
3485 for (NamedDecl *D : Decls) {
3486 DeclIDs.push_back(
3487 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3488 }
3489 return std::make_pair(Start, DeclIDs.size());
3490 }
3491
3492 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3493 unsigned Start = DeclIDs.size();
3494 for (auto ID : FromReader)
3495 DeclIDs.push_back(ID);
3496 return std::make_pair(Start, DeclIDs.size());
3497 }
3498
3499 static bool EqualKey(key_type_ref a, key_type_ref b) {
3500 return a == b;
3501 }
3502
Richard Smitha06c7e62015-08-26 23:55:49 +00003503 hash_value_type ComputeHash(DeclarationNameKey Name) {
3504 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003505 }
3506
Richard Smithd88a7f12015-09-01 20:35:42 +00003507 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3508 assert(Writer.hasChain() &&
3509 "have reference to loaded module file but no chain?");
3510
3511 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003512
Peter Collingbournee3f65292018-05-18 19:46:24 +00003513 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
Richard Smithd88a7f12015-09-01 20:35:42 +00003514 }
3515
Richard Smitha06c7e62015-08-26 23:55:49 +00003516 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3517 DeclarationNameKey Name,
3518 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003519 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003520
Peter Collingbournee3f65292018-05-18 19:46:24 +00003521 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003522 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003523 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003524 case DeclarationName::Identifier:
3525 case DeclarationName::ObjCZeroArgSelector:
3526 case DeclarationName::ObjCOneArgSelector:
3527 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003528 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003529 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003530 KeyLen += 4;
3531 break;
3532 case DeclarationName::CXXOperatorName:
3533 KeyLen += 1;
3534 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003535 case DeclarationName::CXXConstructorName:
3536 case DeclarationName::CXXDestructorName:
3537 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003538 case DeclarationName::CXXUsingDirective:
3539 break;
3540 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003541 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003542
Richard Smithf02662d2015-07-30 03:17:16 +00003543 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003544 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3545 assert(uint16_t(DataLen) == DataLen &&
3546 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003547 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003548
3549 return std::make_pair(KeyLen, DataLen);
3550 }
3551
Richard Smitha06c7e62015-08-26 23:55:49 +00003552 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003553 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003554
Peter Collingbournee3f65292018-05-18 19:46:24 +00003555 endian::Writer LE(Out, little);
Richard Smitha06c7e62015-08-26 23:55:49 +00003556 LE.write<uint8_t>(Name.getKind());
3557 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003558 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003559 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003560 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003561 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003562 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003563 case DeclarationName::ObjCZeroArgSelector:
3564 case DeclarationName::ObjCOneArgSelector:
3565 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003566 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003567 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003568 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003569 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003570 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003571 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003572 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003573 case DeclarationName::CXXConstructorName:
3574 case DeclarationName::CXXDestructorName:
3575 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003576 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003577 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003578 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003579
3580 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003581 }
3582
Richard Smitha06c7e62015-08-26 23:55:49 +00003583 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3584 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003585 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003586
Peter Collingbournee3f65292018-05-18 19:46:24 +00003587 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003588 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003589 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3590 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003591 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3592 }
3593};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003594
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003595} // namespace
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003596
Chandler Carruthe972c362015-03-26 03:11:40 +00003597bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3598 DeclContext *DC) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003599 return Result.hasExternalDecls() &&
3600 DC->hasNeedToReconcileExternalVisibleStorage();
Chandler Carruthe972c362015-03-26 03:11:40 +00003601}
3602
3603bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3604 DeclContext *DC) {
3605 for (auto *D : Result.getLookupResult())
3606 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3607 return false;
3608
3609 return true;
3610}
3611
Richard Smithd88a7f12015-09-01 20:35:42 +00003612void
Chandler Carruthe972c362015-03-26 03:11:40 +00003613ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003614 llvm::SmallVectorImpl<char> &LookupTable) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003615 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3616 !ConstDC->hasLazyExternalLexicalLookups() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003617 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003618
Chandler Carruthe972c362015-03-26 03:11:40 +00003619 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003620 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003621 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3622
3623 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003624 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3625 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003626 ASTDeclContextNameLookupTrait Trait(*this);
3627
Chandler Carruthe972c362015-03-26 03:11:40 +00003628 // The first step is to collect the declaration names which we need to
3629 // serialize into the name lookup table, and to collect them in a stable
3630 // order.
3631 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003632
Chandler Carruthe972c362015-03-26 03:11:40 +00003633 // We also build up small sets of the constructor and conversion function
3634 // names which are visible.
3635 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003636
Chandler Carruthe972c362015-03-26 03:11:40 +00003637 for (auto &Lookup : *DC->buildLookup()) {
3638 auto &Name = Lookup.first;
3639 auto &Result = Lookup.second;
3640
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003641 // If there are no local declarations in our lookup result, we
3642 // don't need to write an entry for the name at all. If we can't
3643 // write out a lookup set without performing more deserialization,
3644 // just skip this entry.
3645 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003646 isLookupResultEntirelyExternal(Result, DC))
3647 continue;
3648
3649 // We also skip empty results. If any of the results could be external and
3650 // the currently available results are empty, then all of the results are
3651 // external and we skip it above. So the only way we get here with an empty
3652 // results is when no results could have been external *and* we have
3653 // external results.
3654 //
3655 // FIXME: While we might want to start emitting on-disk entries for negative
3656 // lookups into a decl context as an optimization, today we *have* to skip
3657 // them because there are names with empty lookup results in decl contexts
3658 // which we can't emit in any stable ordering: we lookup constructors and
3659 // conversion functions in the enclosing namespace scope creating empty
3660 // results for them. This in almost certainly a bug in Clang's name lookup,
3661 // but that is likely to be hard or impossible to fix and so we tolerate it
3662 // here by omitting lookups with empty results.
3663 if (Lookup.second.getLookupResult().empty())
3664 continue;
3665
3666 switch (Lookup.first.getNameKind()) {
3667 default:
3668 Names.push_back(Lookup.first);
3669 break;
3670
Richard Smithcd45dbc2014-04-19 03:48:30 +00003671 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003672 assert(isa<CXXRecordDecl>(DC) &&
3673 "Cannot have a constructor name outside of a class!");
3674 ConstructorNameSet.insert(Name);
3675 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003676
3677 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003678 assert(isa<CXXRecordDecl>(DC) &&
3679 "Cannot have a conversion function name outside of a class!");
3680 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003681 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003682 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003683 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003684
Chandler Carruthe972c362015-03-26 03:11:40 +00003685 // Sort the names into a stable order.
Fangrui Song55fab262018-09-26 22:16:28 +00003686 llvm::sort(Names);
Chandler Carruthe972c362015-03-26 03:11:40 +00003687
Chandler Carruthffbf7052015-03-26 22:27:09 +00003688 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003689 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003690 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003691
Chandler Carruthffbf7052015-03-26 22:27:09 +00003692 // First we try the easy case by forming the current context's constructor
3693 // name and adding that name first. This is a very useful optimization to
3694 // avoid walking the lexical declarations in many cases, and it also
3695 // handles the only case where a constructor name can come from some other
3696 // lexical context -- when that name is an implicit constructor merged from
3697 // another declaration in the redecl chain. Any non-implicit constructor or
3698 // conversion function which doesn't occur in all the lexical contexts
3699 // would be an ODR violation.
3700 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3701 Context->getCanonicalType(Context->getRecordType(D)));
3702 if (ConstructorNameSet.erase(ImplicitCtorName))
3703 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003704
Chandler Carruthffbf7052015-03-26 22:27:09 +00003705 // If we still have constructors or conversion functions, we walk all the
3706 // names in the decl and add the constructors and conversion functions
3707 // which are visible in the order they lexically occur within the context.
3708 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3709 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3710 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3711 auto Name = ChildND->getDeclName();
3712 switch (Name.getNameKind()) {
3713 default:
3714 continue;
3715
3716 case DeclarationName::CXXConstructorName:
3717 if (ConstructorNameSet.erase(Name))
3718 Names.push_back(Name);
3719 break;
3720
3721 case DeclarationName::CXXConversionFunctionName:
3722 if (ConversionNameSet.erase(Name))
3723 Names.push_back(Name);
3724 break;
3725 }
3726
3727 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3728 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003729 }
3730
Chandler Carruthe972c362015-03-26 03:11:40 +00003731 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3732 "constructors by walking all the "
3733 "lexical members of the context.");
3734 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3735 "conversion functions by walking all "
3736 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003737 }
3738
Chandler Carruthe972c362015-03-26 03:11:40 +00003739 // Next we need to do a lookup with each name into this decl context to fully
3740 // populate any results from external sources. We don't actually use the
3741 // results of these lookups because we only want to use the results after all
3742 // results have been loaded and the pointers into them will be stable.
3743 for (auto &Name : Names)
3744 DC->lookup(Name);
3745
3746 // Now we need to insert the results for each name into the hash table. For
3747 // constructor names and conversion function names, we actually need to merge
3748 // all of the results for them into one list of results each and insert
3749 // those.
3750 SmallVector<NamedDecl *, 8> ConstructorDecls;
3751 SmallVector<NamedDecl *, 8> ConversionDecls;
3752
3753 // Now loop over the names, either inserting them or appending for the two
3754 // special cases.
3755 for (auto &Name : Names) {
3756 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3757
3758 switch (Name.getNameKind()) {
3759 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003760 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003761 break;
3762
3763 case DeclarationName::CXXConstructorName:
3764 ConstructorDecls.append(Result.begin(), Result.end());
3765 break;
3766
3767 case DeclarationName::CXXConversionFunctionName:
3768 ConversionDecls.append(Result.begin(), Result.end());
3769 break;
3770 }
3771 }
3772
3773 // Handle our two special cases if we ended up having any. We arbitrarily use
3774 // the first declaration's name here because the name itself isn't part of
3775 // the key, only the kind of name is used.
3776 if (!ConstructorDecls.empty())
3777 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003778 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003779 if (!ConversionDecls.empty())
3780 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003781 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003782
Richard Smithd88a7f12015-09-01 20:35:42 +00003783 // Create the on-disk hash table. Also emit the existing imported and
3784 // merged table if there is one.
3785 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3786 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003787}
3788
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003789/// Write the block containing all of the declaration IDs
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003790/// visible from the given DeclContext.
3791///
3792/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003793/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003794uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3795 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003796 // If we imported a key declaration of this namespace, write the visible
3797 // lookup results as an update record for it rather than including them
3798 // on this declaration. We will only look at key declarations on reload.
3799 if (isa<NamespaceDecl>(DC) && Chain &&
3800 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3801 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003802 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003803 Prev = Prev->getPreviousDecl())
3804 if (!Prev->isFromASTFile())
3805 return 0;
3806
3807 // Note that we need to emit an update record for the primary context.
3808 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3809
3810 // Make sure all visible decls are written. They will be recorded later. We
3811 // do this using a side data structure so we can sort the names into
3812 // a deterministic order.
3813 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3814 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3815 LookupResults;
3816 if (Map) {
3817 LookupResults.reserve(Map->size());
3818 for (auto &Entry : *Map)
3819 LookupResults.push_back(
3820 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3821 }
3822
Fangrui Song55fab262018-09-26 22:16:28 +00003823 llvm::sort(LookupResults, llvm::less_first());
Richard Smith5fc18a92015-07-12 23:43:21 +00003824 for (auto &NameAndResult : LookupResults) {
3825 DeclarationName Name = NameAndResult.first;
3826 DeclContext::lookup_result Result = NameAndResult.second;
3827 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3828 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3829 // We have to work around a name lookup bug here where negative lookup
3830 // results for these names get cached in namespace lookup tables (these
3831 // names should never be looked up in a namespace).
3832 assert(Result.empty() && "Cannot have a constructor or conversion "
3833 "function name in a namespace!");
3834 continue;
3835 }
3836
3837 for (NamedDecl *ND : Result)
3838 if (!ND->isFromASTFile())
3839 GetDeclRef(ND);
3840 }
3841
3842 return 0;
3843 }
3844
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003845 if (DC->getPrimaryContext() != DC)
3846 return 0;
3847
Chandler Carruthe972c362015-03-26 03:11:40 +00003848 // Skip contexts which don't support name lookup.
3849 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003850 return 0;
3851
3852 // If not in C++, we perform name lookup for the translation unit via the
3853 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003854 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003855 return 0;
3856
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003857 // Serialize the contents of the mapping used for lookup. Note that,
3858 // although we have two very different code paths, the serialized
3859 // representation is the same for both cases: a declaration name,
3860 // followed by a size, followed by references to the visible
3861 // declarations that have that name.
3862 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003863 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003864 if (!Map || Map->empty())
3865 return 0;
3866
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003867 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003868 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003869 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003870
3871 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003872 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003873 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003874 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003875 ++NumVisibleDeclContexts;
3876 return Offset;
3877}
3878
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003879/// Write an UPDATE_VISIBLE block for the given context.
Sebastian Redla4071b42010-08-24 00:50:09 +00003880///
3881/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3882/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003883/// (in C++), for namespaces, and for classes with forward-declared unscoped
3884/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003885void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003886 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003887 if (!Map || Map->empty())
3888 return;
3889
Sebastian Redla4071b42010-08-24 00:50:09 +00003890 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003891 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003892 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003893
Richard Smith5fc18a92015-07-12 23:43:21 +00003894 // If we're updating a namespace, select a key declaration as the key for the
3895 // update record; those are the only ones that will be checked on reload.
3896 if (isa<NamespaceDecl>(DC))
3897 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3898
Sebastian Redla4071b42010-08-24 00:50:09 +00003899 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003900 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003901 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003902}
3903
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003904/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003905void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00003906 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003907 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3908}
3909
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003910/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003911void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003912 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003913 return;
3914
3915 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3916 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00003917 for (const auto &I:Opts.OptMap) {
3918 AddString(I.getKey(), Record);
3919 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00003920 Record.push_back(V.Supported ? 1 : 0);
3921 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00003922 Record.push_back(V.Avail);
3923 Record.push_back(V.Core);
3924 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003925 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3926}
3927
Yaxun Liu5b746652016-12-18 05:18:55 +00003928void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
3929 if (!SemaRef.Context.getLangOpts().OpenCL)
3930 return;
3931
Bruno Ricci2751b692019-04-18 15:13:27 +00003932 // Sort the elements of the map OpenCLTypeExtMap by TypeIDs,
3933 // without copying them.
3934 const llvm::DenseMap<const Type *, std::set<std::string>> &OpenCLTypeExtMap =
3935 SemaRef.OpenCLTypeExtMap;
3936 using ElementTy = std::pair<TypeID, const std::set<std::string> *>;
3937 llvm::SmallVector<ElementTy, 8> StableOpenCLTypeExtMap;
3938 StableOpenCLTypeExtMap.reserve(OpenCLTypeExtMap.size());
3939
3940 for (const auto &I : OpenCLTypeExtMap)
3941 StableOpenCLTypeExtMap.emplace_back(
3942 getTypeID(I.first->getCanonicalTypeInternal()), &I.second);
3943
3944 auto CompareByTypeID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
3945 return E1.first < E2.first;
3946 };
3947 llvm::sort(StableOpenCLTypeExtMap, CompareByTypeID);
3948
Yaxun Liu5b746652016-12-18 05:18:55 +00003949 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00003950 for (const ElementTy &E : StableOpenCLTypeExtMap) {
3951 Record.push_back(E.first); // TypeID
3952 const std::set<std::string> *ExtSet = E.second;
3953 Record.push_back(static_cast<unsigned>(ExtSet->size()));
3954 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00003955 AddString(Ext, Record);
3956 }
Bruno Ricci2751b692019-04-18 15:13:27 +00003957
Yaxun Liu5b746652016-12-18 05:18:55 +00003958 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
3959}
3960
3961void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
3962 if (!SemaRef.Context.getLangOpts().OpenCL)
3963 return;
3964
Bruno Ricci2751b692019-04-18 15:13:27 +00003965 // Sort the elements of the map OpenCLDeclExtMap by DeclIDs,
3966 // without copying them.
3967 const llvm::DenseMap<const Decl *, std::set<std::string>> &OpenCLDeclExtMap =
3968 SemaRef.OpenCLDeclExtMap;
3969 using ElementTy = std::pair<DeclID, const std::set<std::string> *>;
3970 llvm::SmallVector<ElementTy, 8> StableOpenCLDeclExtMap;
3971 StableOpenCLDeclExtMap.reserve(OpenCLDeclExtMap.size());
3972
3973 for (const auto &I : OpenCLDeclExtMap)
3974 StableOpenCLDeclExtMap.emplace_back(getDeclID(I.first), &I.second);
3975
3976 auto CompareByDeclID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
3977 return E1.first < E2.first;
3978 };
3979 llvm::sort(StableOpenCLDeclExtMap, CompareByDeclID);
3980
Yaxun Liu5b746652016-12-18 05:18:55 +00003981 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00003982 for (const ElementTy &E : StableOpenCLDeclExtMap) {
3983 Record.push_back(E.first); // DeclID
3984 const std::set<std::string> *ExtSet = E.second;
3985 Record.push_back(static_cast<unsigned>(ExtSet->size()));
3986 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00003987 AddString(Ext, Record);
3988 }
Bruno Ricci2751b692019-04-18 15:13:27 +00003989
Yaxun Liu5b746652016-12-18 05:18:55 +00003990 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
3991}
3992
Justin Lebar67a78a62016-10-08 22:15:58 +00003993void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
3994 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
3995 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
3996 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
3997 }
3998}
3999
Douglas Gregor404cdde2012-01-27 01:47:08 +00004000void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004001 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004002 RecordData Categories;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004003
Douglas Gregor404cdde2012-01-27 01:47:08 +00004004 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4005 unsigned Size = 0;
4006 unsigned StartIndex = Categories.size();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004007
Douglas Gregor404cdde2012-01-27 01:47:08 +00004008 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004009
Douglas Gregor404cdde2012-01-27 01:47:08 +00004010 // Allocate space for the size.
4011 Categories.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004012
Douglas Gregor404cdde2012-01-27 01:47:08 +00004013 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004014 for (ObjCInterfaceDecl::known_categories_iterator
4015 Cat = Class->known_categories_begin(),
4016 CatEnd = Class->known_categories_end();
4017 Cat != CatEnd; ++Cat, ++Size) {
4018 assert(getDeclID(*Cat) != 0 && "Bogus category");
4019 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004020 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004021
Douglas Gregor404cdde2012-01-27 01:47:08 +00004022 // Update the size.
4023 Categories[StartIndex] = Size;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004024
Douglas Gregor404cdde2012-01-27 01:47:08 +00004025 // Record this interface -> category map.
4026 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4027 CategoriesMap.push_back(CatInfo);
4028 }
4029
4030 // Sort the categories map by the definition ID, since the reader will be
4031 // performing binary searches on this information.
4032 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4033
4034 // Emit the categories map.
4035 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004036
David Blaikieb44f0bf2017-01-04 22:36:43 +00004037 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004038 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004041 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004042
4043 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4044 Stream.EmitRecordWithBlob(AbbrevID, Record,
4045 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004046 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004047
Douglas Gregor404cdde2012-01-27 01:47:08 +00004048 // Emit the category lists.
4049 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4050}
4051
Richard Smithe40f2ba2013-08-07 21:41:30 +00004052void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4053 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4054
4055 if (LPTMap.empty())
4056 return;
4057
4058 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004059 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004060 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004061 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004062 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004063 AddDeclRef(LPT.D, Record);
4064 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004065
Justin Lebar28f09c52016-10-10 16:26:08 +00004066 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004067 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004068 }
4069 }
4070 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4071}
4072
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004073/// Write the state of 'pragma clang optimize' at the end of the module.
Dario Domizioli13a0a382014-05-23 12:13:25 +00004074void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4075 RecordData Record;
4076 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4077 AddSourceLocation(PragmaLoc, Record);
4078 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4079}
4080
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004081/// Write the state of 'pragma ms_struct' at the end of the module.
Nico Weber779355f2016-03-02 23:22:00 +00004082void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4083 RecordData Record;
4084 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4085 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4086}
4087
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004088/// Write the state of 'pragma pointers_to_members' at the end of the
Nico Weber42932312016-03-03 00:17:35 +00004089//module.
4090void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4091 RecordData Record;
4092 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4093 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4094 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4095}
4096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004097/// Write the state of 'pragma pack' at the end of the module.
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004098void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004099 // Don't serialize pragma pack state for modules, since it should only take
4100 // effect on a per-submodule basis.
4101 if (WritingModule)
4102 return;
4103
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004104 RecordData Record;
4105 Record.push_back(SemaRef.PackStack.CurrentValue);
4106 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4107 Record.push_back(SemaRef.PackStack.Stack.size());
4108 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4109 Record.push_back(StackEntry.Value);
4110 AddSourceLocation(StackEntry.PragmaLocation, Record);
Alex Lorenz45b40142017-07-28 14:41:21 +00004111 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004112 AddString(StackEntry.StackSlotLabel, Record);
4113 }
4114 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4115}
4116
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004117void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4118 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004119 // Enter the extension block.
4120 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4121
4122 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004123 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004124 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4125 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4126 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4127 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4128 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4129 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004130 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004131
4132 // Emit the metadata record.
4133 RecordData Record;
4134 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4135 Record.push_back(EXTENSION_METADATA);
4136 Record.push_back(Metadata.MajorVersion);
4137 Record.push_back(Metadata.MinorVersion);
4138 Record.push_back(Metadata.BlockName.size());
4139 Record.push_back(Metadata.UserInfo.size());
4140 SmallString<64> Buffer;
4141 Buffer += Metadata.BlockName;
4142 Buffer += Metadata.UserInfo;
4143 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4144
4145 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004146 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004147
4148 // Exit the extension block.
4149 Stream.ExitBlock();
4150}
4151
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004152//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004153// General Serialization Routines
4154//===----------------------------------------------------------------------===//
4155
Richard Smithe43e2b32018-08-20 21:47:29 +00004156void ASTRecordWriter::AddAttr(const Attr *A) {
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004157 auto &Record = *this;
Richard Smithe43e2b32018-08-20 21:47:29 +00004158 if (!A)
4159 return Record.push_back(0);
4160 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
Erich Keane6a24e802019-09-13 17:39:31 +00004161
4162 Record.AddIdentifierRef(A->getAttrName());
4163 Record.AddIdentifierRef(A->getScopeName());
Richard Smithe43e2b32018-08-20 21:47:29 +00004164 Record.AddSourceRange(A->getRange());
Erich Keane6a24e802019-09-13 17:39:31 +00004165 Record.AddSourceLocation(A->getScopeLoc());
4166 Record.push_back(A->getParsedKind());
4167 Record.push_back(A->getSyntax());
4168 Record.push_back(A->getAttributeSpellingListIndexRaw());
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004169
4170#include "clang/Serialization/AttrPCHWrite.inc"
Richard Smithe43e2b32018-08-20 21:47:29 +00004171}
4172
4173/// Emit the list of attributes to the specified record.
4174void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4175 push_back(Attrs.size());
4176 for (const auto *A : Attrs)
4177 AddAttr(A);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004178}
4179
John McCallf413f5e2013-05-03 00:10:13 +00004180void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4181 AddSourceLocation(Tok.getLocation(), Record);
4182 Record.push_back(Tok.getLength());
4183
4184 // FIXME: When reading literal tokens, reconstruct the literal pointer
4185 // if it is needed.
4186 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4187 // FIXME: Should translate token kind to a stable encoding.
4188 Record.push_back(Tok.getKind());
4189 // FIXME: Should translate token flags to a stable encoding.
4190 Record.push_back(Tok.getFlags());
4191}
4192
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004193void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004194 Record.push_back(Str.size());
4195 Record.insert(Record.end(), Str.begin(), Str.end());
4196}
4197
Richard Smith7ed1bc92014-12-05 22:42:13 +00004198bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004199 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004200
Richard Smith54cc3c22014-12-11 20:50:24 +00004201 bool Changed =
4202 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004203
4204 // Remove a prefix to make the path relative, if relevant.
4205 const char *PathBegin = Path.data();
4206 const char *PathPtr =
4207 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4208 if (PathPtr != PathBegin) {
4209 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4210 Changed = true;
4211 }
4212
4213 return Changed;
4214}
4215
4216void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4217 SmallString<128> FilePath(Path);
4218 PreparePathForOutput(FilePath);
4219 AddString(FilePath, Record);
4220}
4221
Mehdi Amini57a41912015-09-10 01:46:39 +00004222void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004223 StringRef Path) {
4224 SmallString<128> FilePath(Path);
4225 PreparePathForOutput(FilePath);
4226 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4227}
4228
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004229void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4230 RecordDataImpl &Record) {
4231 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004232 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004233 Record.push_back(*Minor + 1);
4234 else
4235 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004236 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004237 Record.push_back(*Subminor + 1);
4238 else
4239 Record.push_back(0);
4240}
4241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004242/// Note that the identifier II occurs at the given offset
Douglas Gregore84a9da2009-04-20 20:36:09 +00004243/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004244void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004245 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004246 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004247 // up earlier in the chain and thus don't need an offset.
4248 if (ID >= FirstIdentID)
4249 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004250}
4251
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004252/// Note that the selector Sel occurs at the given offset
Douglas Gregor95c13f52009-04-25 17:48:32 +00004253/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004254void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004255 unsigned ID = SelectorIDs[Sel];
4256 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004257 // Don't record offsets for selectors that are also available in a different
4258 // file.
4259 if (ID < FirstSelectorID)
4260 return;
4261 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004262}
4263
David Blaikie61137e12017-01-05 18:23:18 +00004264ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004265 SmallVectorImpl<char> &Buffer,
4266 InMemoryModuleCache &ModuleCache,
David Blaikie61137e12017-01-05 18:23:18 +00004267 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4268 bool IncludeTimestamps)
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004269 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004270 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004271 for (const auto &Ext : Extensions) {
4272 if (auto Writer = Ext->createExtensionWriter(*this))
4273 ModuleFileExtensionWriters.push_back(std::move(Writer));
4274 }
4275}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004276
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004277ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004278 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004279}
4280
Richard Smithb3761912015-02-05 23:08:52 +00004281const LangOptions &ASTWriter::getLangOpts() const {
4282 assert(WritingAST && "can't determine lang opts when not writing AST");
4283 return Context->getLangOpts();
4284}
4285
Richard Smithe75ee0f2015-08-17 07:13:32 +00004286time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4287 return IncludeTimestamps ? E->getModificationTime() : 0;
4288}
4289
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004290ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4291 const std::string &OutputFile,
4292 Module *WritingModule, StringRef isysroot,
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004293 bool hasErrors,
4294 bool ShouldCacheASTInMemory) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004295 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004296
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004297 ASTHasCompilerErrors = hasErrors;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004298
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004299 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004300 Stream.Emit((unsigned)'C', 8);
4301 Stream.Emit((unsigned)'P', 8);
4302 Stream.Emit((unsigned)'C', 8);
4303 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004304
Chris Lattner28fa4e62009-04-26 22:26:21 +00004305 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004306
Douglas Gregoreda8e122011-08-09 15:13:55 +00004307 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004308 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004309 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004310 ASTFileSignature Signature =
4311 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004312 Context = nullptr;
4313 PP = nullptr;
4314 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004315 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004316
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004317 WritingAST = false;
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004318 if (ShouldCacheASTInMemory) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004319 // Construct MemoryBuffer and update buffer manager.
Volodymyr Sapsai83f4c3a2020-01-16 17:12:41 -08004320 ModuleCache.addFinalPCM(OutputFile,
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004321 llvm::MemoryBuffer::getMemBufferCopy(
4322 StringRef(Buffer.begin(), Buffer.size())));
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004323 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004324 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004325}
4326
Douglas Gregora94a1542011-07-27 21:45:57 +00004327template<typename Vector>
4328static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4329 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004330 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4331 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004332 Writer.AddDeclRef(*I, Record);
4333 }
4334}
4335
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004336ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4337 const std::string &OutputFile,
4338 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004339 using namespace llvm;
4340
Craig Toppera13603a2014-05-22 05:54:18 +00004341 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004342
Douglas Gregorcf68c582011-12-01 22:20:10 +00004343 // Make sure that the AST reader knows to finalize itself.
4344 if (Chain)
4345 Chain->finalizeForWriting();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004346
Sebastian Redl143413f2010-07-12 22:02:52 +00004347 ASTContext &Context = SemaRef.Context;
4348 Preprocessor &PP = SemaRef.PP;
4349
Douglas Gregordab42432011-08-12 00:15:20 +00004350 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004351 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4352 if (D) {
4353 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4354 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004355 }
4356 };
4357 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4358 PREDEF_DECL_TRANSLATION_UNIT_ID);
4359 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4360 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4361 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4362 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4363 PREDEF_DECL_OBJC_PROTOCOL_ID);
4364 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4365 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4366 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4367 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4368 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004369 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004370 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4371 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004372 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004373 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4374 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004375 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4376 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004377 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4378 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004379 RegisterPredefDecl(Context.TypePackElementDecl,
4380 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004381
Chris Lattner0c797362009-09-08 18:19:27 +00004382 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004383 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004384 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004385 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004386 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004387
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004388 // Build a record containing all of the file scoped decls in this file.
4389 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004390 if (!isModule)
4391 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4392 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004393
Douglas Gregor851443c2011-08-12 01:39:19 +00004394 // Build a record containing all of the delegating constructors we still need
4395 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004396 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004397 if (!isModule)
4398 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004399
Douglas Gregor851443c2011-08-12 01:39:19 +00004400 // Write the set of weak, undeclared identifiers. We always write the
4401 // entire table, since later PCH files in a PCH chain are only interested in
4402 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004403 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004404 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4405 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4406 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4407 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4408 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4409 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4410 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004411 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004412
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004413 // Build a record containing all of the ext_vector declarations.
4414 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004415 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004416
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004417 // Build a record containing all of the VTable uses information.
4418 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004419 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004420 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4421 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4422 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4423 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4424 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004425 }
4426
Nico Weber72889432014-09-06 01:25:55 +00004427 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4428 RecordData UnusedLocalTypedefNameCandidates;
4429 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4430 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4431
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004432 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004433 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004434 for (const auto &I : SemaRef.PendingInstantiations) {
4435 AddDeclRef(I.first, PendingInstantiations);
4436 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004437 }
4438 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4439 "There are local ones at end of translation unit!");
4440
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004441 // Build a record containing some declaration references.
4442 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004443 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004444 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4445 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004446 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004447 }
4448
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004449 RecordData CUDASpecialDeclRefs;
4450 if (Context.getcudaConfigureCallDecl()) {
4451 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4452 }
4453
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004454 // Build a record containing all of the known namespaces.
4455 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004456 for (const auto &I : SemaRef.KnownNamespaces) {
4457 if (!I.second)
4458 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004459 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004460
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004461 // Build a record of all used, undefined objects that require definitions.
4462 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004463
4464 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004465 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004466 for (const auto &I : Undefined) {
4467 AddDeclRef(I.first, UndefinedButUsed);
4468 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004469 }
4470
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004471 // Build a record containing all delete-expressions that we would like to
4472 // analyze later in AST.
4473 RecordData DeleteExprsToAnalyze;
4474
Richard Smithf5262c62018-06-28 01:57:04 +00004475 if (!isModule) {
4476 for (const auto &DeleteExprsInfo :
4477 SemaRef.getMismatchingDeleteExpressions()) {
4478 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4479 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4480 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4481 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4482 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4483 }
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004484 }
4485 }
4486
Douglas Gregor112b9072012-10-18 05:31:06 +00004487 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004488 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004489
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004490 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004491 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004492
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004493 // This is so that older clang versions, before the introduction
4494 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004495 {
4496 RecordData Record = {VERSION_MAJOR};
4497 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4498 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004499
Douglas Gregor851443c2011-08-12 01:39:19 +00004500 // Create a lexical update block containing all of the declarations in the
4501 // translation unit that do not come from other AST files.
4502 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004503 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4504 for (const auto *D : TU->noload_decls()) {
4505 if (!D->isFromASTFile()) {
4506 NewGlobalKindDeclPairs.push_back(D->getKind());
4507 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4508 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004509 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004510
David Blaikieb44f0bf2017-01-04 22:36:43 +00004511 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004512 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4513 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004514 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004515 {
4516 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4517 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4518 bytes(NewGlobalKindDeclPairs));
4519 }
4520
Douglas Gregor851443c2011-08-12 01:39:19 +00004521 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004522 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004523 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4524 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004525 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004526 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004527 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004528
4529 // If we have any extern "C" names, write out a visible update for them.
4530 if (Context.ExternCContext)
4531 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004532
Douglas Gregor851443c2011-08-12 01:39:19 +00004533 // If the translation unit has an anonymous namespace, and we don't already
4534 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004535 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004536 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4537 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004538 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004539 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004540 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004541
Richard Smith5652c0f2014-03-21 01:48:23 +00004542 // Add update records for all mangling numbers and static local numbers.
4543 // These aren't really update records, but this is a convenient way of
4544 // tagging this rare extra data onto the declarations.
4545 for (const auto &Number : Context.MangleNumbers)
4546 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004547 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4548 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004549 for (const auto &Number : Context.StaticLocalNumbers)
4550 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004551 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4552 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004553
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004554 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004555 // an AST file, are registered for serialization. Likewise for template
4556 // specializations added to imported templates.
4557 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004558 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004559 }
4560
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004561 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004562 // serialization, if we're storing decls with identifiers.
4563 if (!WritingModule || !getLangOpts().CPlusPlus) {
4564 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004565 for (const auto &ID : PP.getIdentifierTable()) {
4566 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004567 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4568 IIs.push_back(II);
4569 }
4570 // Sort the identifiers to visit based on their name.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00004571 llvm::sort(IIs, llvm::deref<std::less<>>());
Richard Smith79bf9202015-08-24 03:33:22 +00004572 for (const IdentifierInfo *II : IIs) {
4573 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4574 DEnd = SemaRef.IdResolver.end();
4575 D != DEnd; ++D) {
4576 GetDeclRef(*D);
4577 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004578 }
4579 }
4580
Manman Rena0f31a02016-04-29 19:04:05 +00004581 // For method pool in the module, if it contains an entry for a selector,
4582 // the entry should be complete, containing everything introduced by that
4583 // module and all modules it imports. It's possible that the entry is out of
4584 // date, so we need to pull in the new content here.
4585
4586 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4587 // safe, we copy all selectors out.
4588 llvm::SmallVector<Selector, 256> AllSelectors;
4589 for (auto &SelectorAndID : SelectorIDs)
4590 AllSelectors.push_back(SelectorAndID.first);
4591 for (auto &Selector : AllSelectors)
4592 SemaRef.updateOutOfDateSelector(Selector);
4593
Douglas Gregor5204bde2011-08-02 16:26:37 +00004594 // Form the record of special types.
4595 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004596 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004597 AddTypeRef(Context.getFILEType(), SpecialTypes);
4598 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4599 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4600 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4601 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004602 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004603 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004604
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004605 if (Chain) {
4606 // Write the mapping information describing our module dependencies and how
4607 // each of those modules were mapped into our own offset/ID space, so that
4608 // the reader can build the appropriate mapping to its own offset/ID space.
4609 // The map consists solely of a blob with the following format:
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004610 // *(module-kind:i8
4611 // module-name-len:i16 module-name:len*i8
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004612 // source-location-offset:i32
4613 // identifier-id:i32
4614 // preprocessed-entity-id:i32
4615 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004616 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004617 // selector-id:i32
4618 // declaration-id:i32
4619 // c++-base-specifiers-id:i32
4620 // type-id:i32)
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004621 //
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004622 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule or
4623 // MK_ExplicitModule, then the module-name is the module name. Otherwise,
4624 // it is the module file name.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004625 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004626 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004628 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004629 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004630 {
4631 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004632 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004633 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00004634
Peter Collingbournee3f65292018-05-18 19:46:24 +00004635 endian::Writer LE(Out, little);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004636 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
4637 StringRef Name =
4638 M.Kind == MK_PrebuiltModule || M.Kind == MK_ExplicitModule
4639 ? M.ModuleName
4640 : M.FileName;
4641 LE.write<uint16_t>(Name.size());
4642 Out.write(Name.data(), Name.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004643
Ben Langmuir785180e2014-10-20 16:27:30 +00004644 // Note: if a base ID was uint max, it would not be possible to load
4645 // another module after it or have more than one entity inside it.
4646 uint32_t None = std::numeric_limits<uint32_t>::max();
4647
4648 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4649 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4650 if (ShouldWrite)
4651 LE.write<uint32_t>(BaseID);
4652 else
4653 LE.write<uint32_t>(None);
4654 };
4655
Ben Langmuirfe971d92014-08-16 04:54:18 +00004656 // These values should be unique within a chain, since they will be read
4657 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004658 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4659 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4660 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4661 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4662 M.NumPreprocessedEntities);
4663 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4664 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4665 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4666 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004667 }
4668 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004669 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004670 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4671 Buffer.data(), Buffer.size());
4672 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004673
Richard Smithb9eab6d2014-03-20 19:44:17 +00004674 RecordData DeclUpdatesOffsetsRecord;
4675
Richard Smith59442b42014-03-20 20:07:19 +00004676 // Keep writing types, declarations, and declaration update records
4677 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004678 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4679 WriteTypeAbbrevs();
4680 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004681 do {
4682 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4683 while (!DeclTypesToEmit.empty()) {
4684 DeclOrType DOT = DeclTypesToEmit.front();
4685 DeclTypesToEmit.pop();
4686 if (DOT.isType())
4687 WriteType(DOT.getType());
4688 else
4689 WriteDecl(Context, DOT.getDecl());
4690 }
4691 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004692 Stream.ExitBlock();
4693
Richard Smithb9eab6d2014-03-20 19:44:17 +00004694 DoneWritingDeclsAndTypes = true;
4695
4696 // These things can only be done once we've written out decls and types.
4697 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004698 if (!DeclUpdatesOffsetsRecord.empty())
4699 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004700 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004701 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004702 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004703 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004704 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004705 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004706 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004707 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004708 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004709 WriteFPPragmaOptions(SemaRef.getFPOptions());
4710 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004711 WriteOpenCLExtensionTypes(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004712 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004713
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004714 // If we're emitting a module, write out the submodule information.
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004715 if (WritingModule)
4716 WriteSubmodules(WritingModule);
4717
Andrew Savonichev52d674d2018-10-29 11:14:01 +00004718 // We need to have information about submodules to correctly deserialize
4719 // decls from OpenCLExtensionDecls block
4720 WriteOpenCLExtensionDecls(SemaRef);
4721
Douglas Gregor5204bde2011-08-02 16:26:37 +00004722 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4723
Douglas Gregord4df8652009-04-22 22:02:47 +00004724 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004725 if (!EagerlyDeserializedDecls.empty())
4726 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004727
David Blaikief63556d2017-04-12 20:58:33 +00004728 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00004729 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4730
Douglas Gregord4df8652009-04-22 22:02:47 +00004731 // Write the record containing tentative definitions.
4732 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004733 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004734
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004735 // Write the record containing unused file scoped decls.
4736 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004737 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004738
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004739 // Write the record containing weak undeclared identifiers.
4740 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004741 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004742 WeakUndeclaredIdentifiers);
4743
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004744 // Write the record containing ext_vector type names.
4745 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004746 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004747
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004748 // Write the record containing VTable uses information.
4749 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004750 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004751
Nico Weber72889432014-09-06 01:25:55 +00004752 // Write the record containing potentially unused local typedefs.
4753 if (!UnusedLocalTypedefNameCandidates.empty())
4754 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4755 UnusedLocalTypedefNameCandidates);
4756
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004757 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004758 if (!PendingInstantiations.empty())
4759 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004760
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004761 // Write the record containing declaration references of Sema.
4762 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004763 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004764
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004765 // Write the record containing CUDA-specific declaration references.
4766 if (!CUDASpecialDeclRefs.empty())
4767 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004768
Alexis Hunt27a761d2011-05-04 23:29:54 +00004769 // Write the delegating constructors.
4770 if (!DelegatingCtorDecls.empty())
4771 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004772
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004773 // Write the known namespaces.
4774 if (!KnownNamespaces.empty())
4775 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004776
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004777 // Write the undefined internal functions and variables, and inline functions.
4778 if (!UndefinedButUsed.empty())
4779 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004780
4781 if (!DeleteExprsToAnalyze.empty())
4782 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4783
Douglas Gregor851443c2011-08-12 01:39:19 +00004784 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004785 for (auto *DC : UpdatedDeclContexts)
4786 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004787
Douglas Gregor959bb062011-12-03 01:15:29 +00004788 if (!WritingModule) {
4789 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004790 struct ModuleInfo {
4791 uint64_t ID;
4792 Module *M;
4793 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4794 };
Richard Smith56be7542014-03-21 00:33:59 +00004795 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004796 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004797 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004798 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4799 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004800 }
Richard Smith56be7542014-03-21 00:33:59 +00004801
4802 if (!Imports.empty()) {
4803 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4804 return A.ID < B.ID;
4805 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004806 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4807 return A.ID == B.ID;
4808 };
Richard Smith56be7542014-03-21 00:33:59 +00004809
4810 // Sort and deduplicate module IDs.
Fangrui Song55fab262018-09-26 22:16:28 +00004811 llvm::sort(Imports, Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004812 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004813 Imports.end());
4814
4815 RecordData ImportedModules;
4816 for (const auto &Import : Imports) {
4817 ImportedModules.push_back(Import.ID);
4818 // FIXME: If the module has macros imported then later has declarations
4819 // imported, this location won't be the right one as a location for the
4820 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004821 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004822 }
4823
Douglas Gregor959bb062011-12-03 01:15:29 +00004824 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4825 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004826 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004827
Douglas Gregor404cdde2012-01-27 01:47:08 +00004828 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004829 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004830 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004831 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004832 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004833 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004834 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004835
Douglas Gregor08f01292009-04-17 22:13:46 +00004836 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004837 RecordData::value_type Record[] = {
4838 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004839 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004840 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004841
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004842 // Write the module file extension blocks.
4843 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004844 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004845
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004846 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004847}
4848
Richard Smithb9eab6d2014-03-20 19:44:17 +00004849void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004850 if (DeclUpdates.empty())
4851 return;
4852
Richard Smith59442b42014-03-20 20:07:19 +00004853 DeclUpdateMap LocalUpdates;
4854 LocalUpdates.swap(DeclUpdates);
4855
Richard Smith6ef42932014-03-20 21:02:00 +00004856 for (auto &DeclUpdate : LocalUpdates) {
4857 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004858
Richard Smithd28ac5b2014-03-22 23:33:22 +00004859 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004860 RecordData RecordData;
4861 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004862 for (auto &Update : DeclUpdate.second) {
4863 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4864
Richard Smith69c82bf2016-04-01 22:52:03 +00004865 // An updated body is emitted last, so that the reader doesn't need
4866 // to skip over the lazy body to reach statements for other records.
4867 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4868 HasUpdatedBody = true;
4869 else
4870 Record.push_back(Kind);
4871
Richard Smith6ef42932014-03-20 21:02:00 +00004872 switch (Kind) {
4873 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4874 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4875 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004876 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004877 Record.push_back(GetDeclRef(Update.getDecl()));
4878 break;
4879
Richard Smith4d235792014-08-07 18:53:08 +00004880 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004881 break;
4882
Richard Smith891fc7f2017-12-05 01:31:47 +00004883 case UPD_CXX_POINT_OF_INSTANTIATION:
4884 // FIXME: Do we need to also save the template specialization kind here?
Richard Smith69c82bf2016-04-01 22:52:03 +00004885 Record.AddSourceLocation(Update.getLoc());
Richard Smith891fc7f2017-12-05 01:31:47 +00004886 break;
4887
4888 case UPD_CXX_ADDED_VAR_DEFINITION: {
4889 const VarDecl *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004890 Record.push_back(VD->isInline());
4891 Record.push_back(VD->isInlineSpecified());
Richard Smith05a21352017-06-22 22:18:46 +00004892 if (VD->getInit()) {
4893 Record.push_back(!VD->isInitKnownICE() ? 1
4894 : (VD->isInitICE() ? 3 : 2));
4895 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
4896 } else {
4897 Record.push_back(0);
4898 }
Richard Smith4d235792014-08-07 18:53:08 +00004899 break;
Richard Smith05a21352017-06-22 22:18:46 +00004900 }
Richard Smith4d235792014-08-07 18:53:08 +00004901
John McCall32791cc2016-01-06 22:34:54 +00004902 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004903 Record.AddStmt(const_cast<Expr *>(
4904 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004905 break;
4906
Richard Smith4b054b22016-08-24 21:25:37 +00004907 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4908 Record.AddStmt(
4909 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4910 break;
4911
Richard Smithcd45dbc2014-04-19 03:48:30 +00004912 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4913 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004914 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004915 Record.push_back(RD->isParamDestroyedInCallee());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004916 Record.push_back(RD->getArgPassingRestrictions());
Richard Smith69c82bf2016-04-01 22:52:03 +00004917 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004918 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004919 *Context, const_cast<CXXRecordDecl *>(RD)));
4920
4921 // This state is sometimes updated by template instantiation, when we
4922 // switch from the specialization referring to the template declaration
4923 // to it referring to the template definition.
4924 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4925 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004926 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004927 } else {
4928 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4929 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004930 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004931
4932 // The instantiation might have been resolved to a partial
4933 // specialization. If so, record which one.
4934 auto From = Spec->getInstantiatedFrom();
4935 if (auto PartialSpec =
4936 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4937 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004938 Record.AddDeclRef(PartialSpec);
4939 Record.AddTemplateArgumentList(
4940 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004941 } else {
4942 Record.push_back(false);
4943 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004944 }
4945 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004946 Record.AddSourceLocation(RD->getLocation());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004947 Record.AddSourceLocation(RD->getBeginLoc());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004948 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004949
4950 // Instantiation may change attributes; write them all out afresh.
4951 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004952 if (D->hasAttrs())
4953 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004954
4955 // FIXME: Ensure we don't get here for explicit instantiations.
4956 break;
4957 }
4958
Richard Smithf8134002015-03-10 01:41:22 +00004959 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004960 Record.AddDeclRef(Update.getDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004961 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
Richard Smithf8134002015-03-10 01:41:22 +00004962 break;
4963
John McCalld505e572019-12-13 21:54:44 -05004964 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
4965 auto prototype =
4966 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
4967 Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
Richard Smith564417a2014-03-20 21:47:22 +00004968 break;
John McCalld505e572019-12-13 21:54:44 -05004969 }
Richard Smith564417a2014-03-20 21:47:22 +00004970
Richard Smith6ef42932014-03-20 21:02:00 +00004971 case UPD_CXX_DEDUCED_RETURN_TYPE:
4972 Record.push_back(GetOrCreateTypeID(Update.getType()));
4973 break;
4974
4975 case UPD_DECL_MARKED_USED:
4976 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004977
4978 case UPD_MANGLING_NUMBER:
4979 case UPD_STATIC_LOCAL_NUMBER:
4980 Record.push_back(Update.getNumber());
4981 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004982
Alexey Bataev97720002014-11-11 04:05:39 +00004983 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004984 Record.AddSourceRange(
4985 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004986 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004987
Alexey Bataev27ef9512019-03-20 20:14:22 +00004988 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
4989 auto *A = D->getAttr<OMPAllocateDeclAttr>();
4990 Record.push_back(A->getAllocatorType());
4991 Record.AddStmt(A->getAllocator());
4992 Record.AddSourceRange(A->getRange());
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004993 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00004994 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004995
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004996 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00004997 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004998 Record.AddSourceRange(
4999 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5000 break;
5001
Richard Smith65ebb4a2015-03-26 04:09:53 +00005002 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005003 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005004 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005005
5006 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005007 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005008 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005009 }
5010 }
5011
Richard Smithd28ac5b2014-03-22 23:33:22 +00005012 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005013 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005014 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005015 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005016 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005017 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005018 }
5019
Richard Smithcd45dbc2014-04-19 03:48:30 +00005020 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005021 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005022 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005023}
5024
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005025void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005026 uint32_t Raw = Loc.getRawEncoding();
5027 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005028}
5029
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005030void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005031 AddSourceLocation(Range.getBegin(), Record);
5032 AddSourceLocation(Range.getEnd(), Record);
5033}
5034
Richard Smithf144b542016-04-08 21:54:32 +00005035void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5036 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005037}
5038
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00005039static void WriteFixedPointSemantics(ASTRecordWriter &Record,
5040 FixedPointSemantics FPSema) {
5041 Record.push_back(FPSema.getWidth());
5042 Record.push_back(FPSema.getScale());
5043 Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 |
5044 FPSema.hasUnsignedPadding() << 2);
5045}
5046
5047void ASTRecordWriter::AddAPValue(const APValue &Value) {
5048 APValue::ValueKind Kind = Value.getKind();
5049 push_back(static_cast<uint64_t>(Kind));
5050 switch (Kind) {
5051 case APValue::None:
5052 case APValue::Indeterminate:
5053 return;
5054 case APValue::Int:
5055 AddAPSInt(Value.getInt());
5056 return;
5057 case APValue::Float:
5058 push_back(static_cast<uint64_t>(
5059 llvm::APFloatBase::SemanticsToEnum(Value.getFloat().getSemantics())));
5060 AddAPFloat(Value.getFloat());
5061 return;
5062 case APValue::FixedPoint: {
5063 WriteFixedPointSemantics(*this, Value.getFixedPoint().getSemantics());
5064 AddAPSInt(Value.getFixedPoint().getValue());
5065 return;
5066 }
5067 case APValue::ComplexInt: {
5068 AddAPSInt(Value.getComplexIntReal());
5069 AddAPSInt(Value.getComplexIntImag());
5070 return;
5071 }
5072 case APValue::ComplexFloat: {
5073 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5074 Value.getComplexFloatReal().getSemantics())));
5075 AddAPFloat(Value.getComplexFloatReal());
5076 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5077 Value.getComplexFloatImag().getSemantics())));
5078 AddAPFloat(Value.getComplexFloatImag());
5079 return;
5080 }
5081 case APValue::LValue:
5082 case APValue::Vector:
5083 case APValue::Array:
5084 case APValue::Struct:
5085 case APValue::Union:
5086 case APValue::MemberPointer:
5087 case APValue::AddrLabelDiff:
5088 // TODO : Handle all these APValue::ValueKind.
5089 return;
5090 }
5091 llvm_unreachable("Invalid APValue::ValueKind");
5092}
5093
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005094void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005095 Record.push_back(getIdentifierRef(II));
5096}
5097
Sebastian Redl539c5062010-08-18 23:57:32 +00005098IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005099 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005100 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005101
Sebastian Redl539c5062010-08-18 23:57:32 +00005102 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005103 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005104 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005105 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005106}
5107
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005108MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005109 // Don't emit builtin macros like __LINE__ to the AST file unless they
5110 // have been redefined by the header (in which case they are not
5111 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005112 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005113 return 0;
5114
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005115 MacroID &ID = MacroIDs[MI];
5116 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005117 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005118 MacroInfoToEmitData Info = { Name, MI, ID };
5119 MacroInfosToEmit.push_back(Info);
5120 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005121 return ID;
5122}
5123
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005124MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005125 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005126 return 0;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005127
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005128 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5129 return MacroIDs[MI];
5130}
5131
5132uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005133 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005134}
5135
Richard Smithe64e7452016-04-18 21:54:58 +00005136void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5137 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005138}
5139
Sebastian Redl539c5062010-08-18 23:57:32 +00005140SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005141 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005142 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005143 }
5144
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005145 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005146 if (SID == 0 && Chain) {
5147 // This might trigger a ReadSelector callback, which will set the ID for
5148 // this selector.
5149 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005150 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005151 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005152 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005153 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005154 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005155 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005156 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005157}
5158
Richard Smithe64e7452016-04-18 21:54:58 +00005159void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5160 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005161}
5162
Richard Smith290d8012016-04-06 17:06:00 +00005163void ASTRecordWriter::AddTemplateArgumentLocInfo(
5164 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005165 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005166 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005167 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005168 break;
5169 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005170 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005171 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005172 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005173 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5174 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005175 break;
5176 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005177 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5178 AddSourceLocation(Arg.getTemplateNameLoc());
5179 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005180 break;
John McCall0ad16662009-10-29 08:12:44 +00005181 case TemplateArgument::Null:
5182 case TemplateArgument::Integral:
5183 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005184 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005185 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005186 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005187 break;
5188 }
5189}
5190
Richard Smith290d8012016-04-06 17:06:00 +00005191void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5192 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005193
5194 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5195 bool InfoHasSameExpr
5196 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005197 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005198 if (InfoHasSameExpr)
5199 return; // Avoid storing the same expr twice.
5200 }
Richard Smith290d8012016-04-06 17:06:00 +00005201 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005202}
5203
Richard Smith290d8012016-04-06 17:06:00 +00005204void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005205 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005206 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005207 return;
5208 }
5209
Richard Smithc23d7342018-06-29 20:46:25 +00005210 AddTypeRef(TInfo->getType());
Richard Smith290d8012016-04-06 17:06:00 +00005211 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005212}
5213
Richard Smith290d8012016-04-06 17:06:00 +00005214void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
Richard Smith290d8012016-04-06 17:06:00 +00005215 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005216 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005217 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005218}
5219
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005220void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005221 Record.push_back(GetOrCreateTypeID(T));
5222}
5223
Richard Smith2095ffe2015-03-16 20:11:03 +00005224TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005225 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005226 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5227 if (T.isNull())
5228 return TypeIdx();
5229 assert(!T.getLocalFastQualifiers());
5230
5231 TypeIdx &Idx = TypeIdxs[T];
5232 if (Idx.getIndex() == 0) {
5233 if (DoneWritingDeclsAndTypes) {
5234 assert(0 && "New type seen after serializing all the types to emit!");
5235 return TypeIdx();
5236 }
5237
5238 // We haven't seen this type before. Assign it a new ID and put it
5239 // into the queue of types to emit.
5240 Idx = TypeIdx(NextTypeID++);
5241 DeclTypesToEmit.push(T);
5242 }
5243 return Idx;
5244 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005245}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005246
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005247TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005248 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005249 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5250 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005251 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005252 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005253
Richard Smith2095ffe2015-03-16 20:11:03 +00005254 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5255 assert(I != TypeIdxs.end() && "Type not emitted!");
5256 return I->second;
5257 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005258}
5259
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005260void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005261 Record.push_back(GetDeclRef(D));
5262}
5263
Sebastian Redl539c5062010-08-18 23:57:32 +00005264DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005265 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005266
5267 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005268 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005269 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005270
Douglas Gregorb3163e52012-01-05 22:33:30 +00005271 // If D comes from an AST file, its declaration ID is already known and
5272 // fixed.
5273 if (D->isFromASTFile())
5274 return D->getGlobalID();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005275
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005276 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005277 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005278 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005279 if (DoneWritingDeclsAndTypes) {
5280 assert(0 && "New decl seen after serializing all the decls to emit!");
5281 return 0;
5282 }
5283
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005284 // We haven't seen this declaration before. Give it a new ID and
5285 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005286 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005287 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005288 }
5289
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005290 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005291}
5292
Sebastian Redl539c5062010-08-18 23:57:32 +00005293DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005294 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005295 return 0;
5296
Douglas Gregorb3163e52012-01-05 22:33:30 +00005297 // If D comes from an AST file, its declaration ID is already known and
5298 // fixed.
5299 if (D->isFromASTFile())
5300 return D->getGlobalID();
5301
Douglas Gregore84a9da2009-04-20 20:36:09 +00005302 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5303 return DeclIDs[D];
5304}
5305
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005306void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005307 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005308 assert(D);
5309
5310 SourceLocation Loc = D->getLocation();
5311 if (Loc.isInvalid())
5312 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005313
5314 // We only keep track of the file-level declarations of each file.
5315 if (!D->getLexicalDeclContext()->isFileContext())
5316 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005317 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5318 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis71b74eb2018-01-26 19:26:12 +00005319 // TemplateTemplateParmDecls that are part of an alias template, should not
5320 // have TU as lexical context.
5321 if (isa<ParmVarDecl>(D) || isa<TemplateTemplateParmDecl>(D))
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005322 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005323
5324 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005325 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005326 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005327 FileID FID;
5328 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005329 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005330 if (FID.isInvalid())
5331 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005332 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005333
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005334 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005335 if (!Info)
5336 Info = new DeclIDInFileInfo();
5337
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005338 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005339 LocDeclIDsTy &Decls = Info->DeclIDs;
5340
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005341 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005342 Decls.push_back(LocDecl);
5343 return;
5344 }
5345
Benjamin Kramer45025c02013-08-24 13:22:59 +00005346 LocDeclIDsTy::iterator I =
Fangrui Song7264a472019-07-03 08:13:17 +00005347 llvm::upper_bound(Decls, LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005348
5349 Decls.insert(I, LocDecl);
5350}
5351
Richard Smithd08aeb62014-08-28 01:33:39 +00005352unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5353 assert(needsAnonymousDeclarationNumber(D) &&
5354 "expected an anonymous declaration");
5355
5356 // Number the anonymous declarations within this context, if we've not
5357 // already done so.
5358 auto It = AnonymousDeclarationNumbers.find(D);
5359 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005360 auto *DC = D->getLexicalDeclContext();
5361 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5362 AnonymousDeclarationNumbers[ND] = Number;
5363 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005364
5365 It = AnonymousDeclarationNumbers.find(D);
5366 assert(It != AnonymousDeclarationNumbers.end() &&
5367 "declaration not found within its lexical context");
5368 }
5369
5370 return It->second;
5371}
5372
Richard Smith290d8012016-04-06 17:06:00 +00005373void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5374 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005375 switch (Name.getNameKind()) {
5376 case DeclarationName::CXXConstructorName:
5377 case DeclarationName::CXXDestructorName:
5378 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005379 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005380 break;
5381
5382 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005383 AddSourceLocation(SourceLocation::getFromRawEncoding(
5384 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005385 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005386 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005387 break;
5388
5389 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005390 AddSourceLocation(SourceLocation::getFromRawEncoding(
5391 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005392 break;
5393
5394 case DeclarationName::Identifier:
5395 case DeclarationName::ObjCZeroArgSelector:
5396 case DeclarationName::ObjCOneArgSelector:
5397 case DeclarationName::ObjCMultiArgSelector:
5398 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005399 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005400 break;
5401 }
5402}
5403
Richard Smith290d8012016-04-06 17:06:00 +00005404void ASTRecordWriter::AddDeclarationNameInfo(
5405 const DeclarationNameInfo &NameInfo) {
5406 AddDeclarationName(NameInfo.getName());
5407 AddSourceLocation(NameInfo.getLoc());
5408 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005409}
5410
Richard Smith290d8012016-04-06 17:06:00 +00005411void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5412 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5413 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005414 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005415 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005416}
5417
Richard Smith290d8012016-04-06 17:06:00 +00005418void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005419 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005420 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005421 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005422
5423 // Push each of the nested-name-specifiers's onto a stack for
5424 // serialization in reverse order.
5425 while (NNS) {
5426 NestedNames.push_back(NNS);
5427 NNS = NNS.getPrefix();
5428 }
5429
Richard Smith290d8012016-04-06 17:06:00 +00005430 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005431 while(!NestedNames.empty()) {
5432 NNS = NestedNames.pop_back_val();
5433 NestedNameSpecifier::SpecifierKind Kind
5434 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005435 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005436 switch (Kind) {
5437 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005438 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5439 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005440 break;
5441
5442 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005443 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5444 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005445 break;
5446
5447 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005448 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5449 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005450 break;
5451
5452 case NestedNameSpecifier::TypeSpec:
5453 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005454 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Richard Smithc23d7342018-06-29 20:46:25 +00005455 AddTypeRef(NNS.getTypeLoc().getType());
Richard Smith290d8012016-04-06 17:06:00 +00005456 AddTypeLoc(NNS.getTypeLoc());
5457 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005458 break;
5459
5460 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005461 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005462 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005463
5464 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005465 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5466 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005467 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005468 }
5469 }
5470}
5471
Richard Smithe64e7452016-04-18 21:54:58 +00005472void ASTRecordWriter::AddTemplateParameterList(
5473 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005474 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005475 AddSourceLocation(TemplateParams->getTemplateLoc());
5476 AddSourceLocation(TemplateParams->getLAngleLoc());
5477 AddSourceLocation(TemplateParams->getRAngleLoc());
Saar Raz0330fba2019-10-15 18:44:06 +00005478
Richard Smithe64e7452016-04-18 21:54:58 +00005479 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005480 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005481 AddDeclRef(P);
Saar Raz0330fba2019-10-15 18:44:06 +00005482 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
5483 Record->push_back(true);
5484 AddStmt(const_cast<Expr*>(RequiresClause));
5485 } else {
5486 Record->push_back(false);
5487 }
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005488}
5489
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005490/// Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005491void ASTRecordWriter::AddTemplateArgumentList(
5492 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005493 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005494 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005495 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005496 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005497}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005498
Richard Smith290d8012016-04-06 17:06:00 +00005499void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5500 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005501 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005502 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5503 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5504 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005505 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005506 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005507 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005508}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005509
Richard Smithe64e7452016-04-18 21:54:58 +00005510void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5511 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005512 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005513 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005514 AddDeclRef(I.getDecl());
5515 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005516 }
5517}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005518
Richard Smith290d8012016-04-06 17:06:00 +00005519// FIXME: Move this out of the main ASTRecordWriter interface.
5520void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5521 Record->push_back(Base.isVirtual());
5522 Record->push_back(Base.isBaseOfClass());
5523 Record->push_back(Base.getAccessSpecifierAsWritten());
5524 Record->push_back(Base.getInheritConstructors());
5525 AddTypeSourceInfo(Base.getTypeSourceInfo());
5526 AddSourceRange(Base.getSourceRange());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005527 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005528 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005529}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005530
Richard Smith645d2cf2016-04-14 00:29:55 +00005531static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5532 ArrayRef<CXXBaseSpecifier> Bases) {
5533 ASTWriter::RecordData Record;
5534 ASTRecordWriter Writer(W, Record);
5535 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005536
Richard Smith645d2cf2016-04-14 00:29:55 +00005537 for (auto &Base : Bases)
5538 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005539
Richard Smith645d2cf2016-04-14 00:29:55 +00005540 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005541}
5542
Richard Smith290d8012016-04-06 17:06:00 +00005543// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005544void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5545 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5546}
5547
Richard Smithaa165cf2016-04-13 21:57:08 +00005548static uint64_t
5549EmitCXXCtorInitializers(ASTWriter &W,
5550 ArrayRef<CXXCtorInitializer *> CtorInits) {
5551 ASTWriter::RecordData Record;
5552 ASTRecordWriter Writer(W, Record);
5553 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005554
Richard Smithaa165cf2016-04-13 21:57:08 +00005555 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005556 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005557 Writer.push_back(CTOR_INITIALIZER_BASE);
5558 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5559 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005560 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005561 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5562 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005563 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005564 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5565 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005566 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005567 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5568 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005569 }
Francois Pichetd583da02010-12-04 09:14:42 +00005570
Richard Smithaa165cf2016-04-13 21:57:08 +00005571 Writer.AddSourceLocation(Init->getMemberLocation());
5572 Writer.AddStmt(Init->getInit());
5573 Writer.AddSourceLocation(Init->getLParenLoc());
5574 Writer.AddSourceLocation(Init->getRParenLoc());
5575 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005576 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005577 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005578 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005579
5580 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005581}
5582
Richard Smithaa165cf2016-04-13 21:57:08 +00005583// FIXME: Move this out of the main ASTRecordWriter interface.
5584void ASTRecordWriter::AddCXXCtorInitializers(
5585 ArrayRef<CXXCtorInitializer *> CtorInits) {
5586 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005587}
5588
Richard Smith290d8012016-04-06 17:06:00 +00005589void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005590 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005591 Record->push_back(Data.IsLambda);
Richard Smith91aeacc2019-10-11 00:29:04 +00005592
5593 #define FIELD(Name, Width, Merge) \
5594 Record->push_back(Data.Name);
5595 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
Richard Trieufd1acbb2017-04-11 21:31:00 +00005596
5597 // getODRHash will compute the ODRHash if it has not been previously computed.
5598 Record->push_back(D->getODRHash());
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +01005599 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
5600 Writer->WritingModule && !D->isDependentType();
David Blaikief63556d2017-04-12 20:58:33 +00005601 Record->push_back(ModulesDebugInfo);
5602 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00005603 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
5604
Richard Smith561fb152012-02-25 07:33:38 +00005605 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005606
Richard Smith290d8012016-04-06 17:06:00 +00005607 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005608 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005609 AddCXXBaseSpecifiers(Data.bases());
5610
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005611 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005612 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005613 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005614 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005615
Richard Smith290d8012016-04-06 17:06:00 +00005616 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
Richard Smith91aeacc2019-10-11 00:29:04 +00005617 Record->push_back(Data.ComputedVisibleConversions);
5618 if (Data.ComputedVisibleConversions)
5619 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005620 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005621 AddDeclRef(D->getFirstFriend());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005622
Douglas Gregor99ae8062012-02-14 17:54:36 +00005623 // Add lambda-specific data.
5624 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005625 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005626 Record->push_back(Lambda.Dependent);
5627 Record->push_back(Lambda.IsGenericLambda);
5628 Record->push_back(Lambda.CaptureDefault);
5629 Record->push_back(Lambda.NumCaptures);
5630 Record->push_back(Lambda.NumExplicitCaptures);
Michael Liao243ebfb2019-10-19 00:15:19 +00005631 Record->push_back(Lambda.HasKnownInternalLinkage);
Richard Smith290d8012016-04-06 17:06:00 +00005632 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005633 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005634 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005635 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005636 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005637 AddSourceLocation(Capture.getLocation());
5638 Record->push_back(Capture.isImplicit());
5639 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005640 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005641 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005642 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005643 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005644 break;
5645 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005646 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005647 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005648 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005649 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005650 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005651 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005652 break;
5653 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005654 }
5655 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005656}
5657
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005658void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005659 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005660 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005661 assert(FirstDeclID == NextDeclID &&
5662 FirstTypeID == NextTypeID &&
5663 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005664 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005665 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005666 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005667 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005668
Sebastian Redl07a89a82010-07-30 00:29:29 +00005669 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005670
Richard Smith7f330cd2015-03-18 01:42:29 +00005671 // Note, this will get called multiple times, once one the reader starts up
5672 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005673 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5674 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5675 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005676 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005677 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005678 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005679 NextDeclID = FirstDeclID;
5680 NextTypeID = FirstTypeID;
5681 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005682 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005683 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005684 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005685}
5686
Sebastian Redl539c5062010-08-18 23:57:32 +00005687void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005688 // Always keep the highest ID. See \p TypeRead() for more information.
5689 IdentID &StoredID = IdentifierIDs[II];
5690 if (ID > StoredID)
5691 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005692}
5693
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005694void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005695 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005696 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005697 if (ID > StoredID)
5698 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005699}
5700
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005701void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005702 // Always take the highest-numbered type index. This copes with an interesting
5703 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005704 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005705 // keep the higher-numbered entry so that we can properly write it out to
5706 // the AST file.
5707 TypeIdx &StoredIdx = TypeIdxs[T];
5708 if (Idx.getIndex() >= StoredIdx.getIndex())
5709 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005710}
5711
Sebastian Redl539c5062010-08-18 23:57:32 +00005712void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005713 // Always keep the highest ID. See \p TypeRead() for more information.
5714 SelectorID &StoredID = SelectorIDs[S];
5715 if (ID > StoredID)
5716 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005717}
Douglas Gregor91096292010-10-02 19:29:26 +00005718
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005719void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005720 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005721 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005722 MacroDefinitions[MD] = ID;
5723}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005724
Douglas Gregore37a85a2011-12-02 17:30:13 +00005725void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5726 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5727 SubmoduleIDs[Mod] = ID;
5728}
5729
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005730void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005731 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005732 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005733 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005734 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005735 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005736 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005737 // A forward reference was mutated into a definition. Rewrite it.
5738 // FIXME: This happens during template instantiation, should we
5739 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005740 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5741 "completed a tag from another module but not by instantiation?");
5742 DeclUpdates[RD].push_back(
5743 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005744 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005745 }
5746}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005747
Richard Smithf983f7f2015-10-13 00:23:25 +00005748static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5749 if (D->isFromASTFile())
5750 return true;
5751
Richard Smithf983f7f2015-10-13 00:23:25 +00005752 // The predefined __va_list_tag struct is imported if we imported any decls.
5753 // FIXME: This is a gross hack.
5754 return D == D->getASTContext().getVaListTagDecl();
5755}
5756
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005757void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005758 if (Chain && Chain->isProcessingUpdateRecords()) return;
5759 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005760 "Should not add lookup results to non-lookup contexts!");
5761
Vassil Vassilev632eac32016-03-16 11:17:04 +00005762 // TU is handled elsewhere.
5763 if (isa<TranslationUnitDecl>(DC))
5764 return;
5765
5766 // Namespaces are handled elsewhere, except for template instantiations of
5767 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5768 // local instantiations are added to an imported context. Only happens when
5769 // adding ADL lookup candidates, for example templated friends.
5770 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5771 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005772 return;
5773
Richard Smithf983f7f2015-10-13 00:23:25 +00005774 // We're only interested in cases where a local declaration is added to an
5775 // imported context.
5776 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5777 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005778
Richard Smith0f4e2c42015-08-06 04:23:48 +00005779 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005780 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005781 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005782 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5783 // We're adding a visible declaration to a predefined decl context. Ensure
5784 // that we write out all of its lookup results so we don't get a nasty
5785 // surprise when we try to emit its lookup table.
5786 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005787 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005788 }
Richard Smithc26d9742016-10-06 20:30:51 +00005789 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005790}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005791
5792void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005793 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005794 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005795
5796 // We're only interested in cases where a local declaration is added to an
5797 // imported context.
5798 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5799 return;
5800
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005801 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005802 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005803
5804 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005805 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005806 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005807 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005808}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005809
Richard Smith564417a2014-03-20 21:47:22 +00005810void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005811 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005812 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5813 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005814 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005815 // If we don't already know the exception specification for this redecl
5816 // chain, add an update record for it.
5817 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5818 ->getType()
5819 ->castAs<FunctionProtoType>()
5820 ->getExceptionSpecType()))
5821 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5822 });
Richard Smith564417a2014-03-20 21:47:22 +00005823}
5824
Richard Smith1fa5d642013-05-11 05:45:24 +00005825void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005826 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005827 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005828 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005829 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005830 DeclUpdates[D].push_back(
5831 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5832 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005833}
5834
Richard Smithf8134002015-03-10 01:41:22 +00005835void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
Richard Smith5b349582017-10-13 01:55:36 +00005836 const FunctionDecl *Delete,
5837 Expr *ThisArg) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005838 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005839 assert(!WritingAST && "Already writing the AST!");
5840 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005841 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005842 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005843 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5844 });
Richard Smithf8134002015-03-10 01:41:22 +00005845}
5846
Sebastian Redlab238a72011-04-24 16:28:06 +00005847void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005848 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005849 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005850 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005851 return; // Declaration not imported from PCH.
5852
Richard Smith4d235792014-08-07 18:53:08 +00005853 // Implicit function decl from a PCH was defined.
5854 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005855}
5856
Richard Smith891fc7f2017-12-05 01:31:47 +00005857void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
5858 if (Chain && Chain->isProcessingUpdateRecords()) return;
5859 assert(!WritingAST && "Already writing the AST!");
5860 if (!D->isFromASTFile())
5861 return;
5862
5863 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
5864}
5865
Richard Smithd28ac5b2014-03-22 23:33:22 +00005866void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005867 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005868 assert(!WritingAST && "Already writing the AST!");
5869 if (!D->isFromASTFile())
5870 return;
5871
Richard Smith9e2341d2015-03-23 03:25:59 +00005872 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005873}
5874
Richard Smith891fc7f2017-12-05 01:31:47 +00005875void ASTWriter::InstantiationRequested(const ValueDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005876 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005877 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005878 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005879 return;
5880
5881 // Since the actual instantiation is delayed, this really means that we need
5882 // to update the instantiation location.
Richard Smith891fc7f2017-12-05 01:31:47 +00005883 SourceLocation POI;
5884 if (auto *VD = dyn_cast<VarDecl>(D))
5885 POI = VD->getPointOfInstantiation();
5886 else
5887 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
5888 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005889}
5890
John McCall32791cc2016-01-06 22:34:54 +00005891void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005892 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005893 assert(!WritingAST && "Already writing the AST!");
5894 if (!D->isFromASTFile())
5895 return;
5896
5897 DeclUpdates[D].push_back(
5898 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5899}
5900
Richard Smith4b054b22016-08-24 21:25:37 +00005901void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5902 assert(!WritingAST && "Already writing the AST!");
5903 if (!D->isFromASTFile())
5904 return;
5905
5906 DeclUpdates[D].push_back(
5907 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5908}
5909
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005910void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5911 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005912 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005913 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005914 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005915 return; // Declaration not imported from PCH.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005916
Douglas Gregor404cdde2012-01-27 01:47:08 +00005917 assert(IFD->getDefinition() && "Category on a class without a definition?");
5918 ObjCClassesWithCategories.insert(
5919 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005920}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005921
Eli Friedman276dd182013-09-05 00:02:25 +00005922void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005923 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005924 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005925
5926 // If there is *any* declaration of the entity that's not from an AST file,
5927 // we can skip writing the update record. We make sure that isUsed() triggers
5928 // completion of the redeclaration chain of the entity.
5929 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5930 if (IsLocalDecl(Prev))
5931 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005932
Aaron Ballman4f45b712014-03-21 15:22:56 +00005933 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005934}
Alexey Bataev97720002014-11-11 04:05:39 +00005935
5936void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005937 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005938 assert(!WritingAST && "Already writing the AST!");
5939 if (!D->isFromASTFile())
5940 return;
5941
5942 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5943}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005944
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005945void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
5946 if (Chain && Chain->isProcessingUpdateRecords()) return;
5947 assert(!WritingAST && "Already writing the AST!");
5948 if (!D->isFromASTFile())
5949 return;
5950
5951 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
5952}
5953
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005954void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5955 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005956 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005957 assert(!WritingAST && "Already writing the AST!");
5958 if (!D->isFromASTFile())
5959 return;
5960
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005961 DeclUpdates[D].push_back(
5962 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005963}
5964
Richard Smith4caa4492015-05-15 02:34:32 +00005965void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005966 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005967 assert(!WritingAST && "Already writing the AST!");
5968 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005969 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005970}
Alex Denisovfde64952015-06-26 05:28:36 +00005971
5972void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5973 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005974 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00005975 assert(!WritingAST && "Already writing the AST!");
5976 if (!Record->isFromASTFile())
5977 return;
5978 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5979}
Richard Smithc26d9742016-10-06 20:30:51 +00005980
5981void ASTWriter::AddedCXXTemplateSpecialization(
5982 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
5983 assert(!WritingAST && "Already writing the AST!");
5984
5985 if (!TD->getFirstDecl()->isFromASTFile())
5986 return;
5987 if (Chain && Chain->isProcessingUpdateRecords())
5988 return;
5989
5990 DeclsToEmitEvenIfUnreferenced.push_back(D);
5991}
5992
5993void ASTWriter::AddedCXXTemplateSpecialization(
5994 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5995 assert(!WritingAST && "Already writing the AST!");
5996
5997 if (!TD->getFirstDecl()->isFromASTFile())
5998 return;
5999 if (Chain && Chain->isProcessingUpdateRecords())
6000 return;
6001
6002 DeclsToEmitEvenIfUnreferenced.push_back(D);
6003}
6004
6005void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6006 const FunctionDecl *D) {
6007 assert(!WritingAST && "Already writing the AST!");
6008
6009 if (!TD->getFirstDecl()->isFromASTFile())
6010 return;
6011 if (Chain && Chain->isProcessingUpdateRecords())
6012 return;
6013
6014 DeclsToEmitEvenIfUnreferenced.push_back(D);
6015}
Kelvin Libe286f52018-09-15 13:54:15 +00006016
6017//===----------------------------------------------------------------------===//
6018//// OMPClause Serialization
6019////===----------------------------------------------------------------------===//
6020
John McCall2ac702a2019-12-14 03:17:03 -05006021namespace {
6022
6023class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
6024 ASTRecordWriter &Record;
6025
6026public:
6027 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
6028#define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *S);
6029#include "clang/Basic/OpenMPKinds.def"
6030 void writeClause(OMPClause *C);
6031 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
6032 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
6033};
6034
6035}
6036
6037void ASTRecordWriter::writeOMPClause(OMPClause *C) {
6038 OMPClauseWriter(*this).writeClause(C);
6039}
6040
Kelvin Libe286f52018-09-15 13:54:15 +00006041void OMPClauseWriter::writeClause(OMPClause *C) {
6042 Record.push_back(C->getClauseKind());
6043 Visit(C);
6044 Record.AddSourceLocation(C->getBeginLoc());
6045 Record.AddSourceLocation(C->getEndLoc());
6046}
6047
6048void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06006049 Record.push_back(uint64_t(C->getCaptureRegion()));
Kelvin Libe286f52018-09-15 13:54:15 +00006050 Record.AddStmt(C->getPreInitStmt());
6051}
6052
6053void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6054 VisitOMPClauseWithPreInit(C);
6055 Record.AddStmt(C->getPostUpdateExpr());
6056}
6057
6058void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6059 VisitOMPClauseWithPreInit(C);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06006060 Record.push_back(uint64_t(C->getNameModifier()));
Kelvin Libe286f52018-09-15 13:54:15 +00006061 Record.AddSourceLocation(C->getNameModifierLoc());
6062 Record.AddSourceLocation(C->getColonLoc());
6063 Record.AddStmt(C->getCondition());
6064 Record.AddSourceLocation(C->getLParenLoc());
6065}
6066
6067void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
Alexey Bataev3a842ec2019-10-15 19:37:05 +00006068 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006069 Record.AddStmt(C->getCondition());
6070 Record.AddSourceLocation(C->getLParenLoc());
6071}
6072
6073void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6074 VisitOMPClauseWithPreInit(C);
6075 Record.AddStmt(C->getNumThreads());
6076 Record.AddSourceLocation(C->getLParenLoc());
6077}
6078
6079void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6080 Record.AddStmt(C->getSafelen());
6081 Record.AddSourceLocation(C->getLParenLoc());
6082}
6083
6084void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6085 Record.AddStmt(C->getSimdlen());
6086 Record.AddSourceLocation(C->getLParenLoc());
6087}
6088
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00006089void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6090 Record.AddStmt(C->getAllocator());
6091 Record.AddSourceLocation(C->getLParenLoc());
6092}
6093
Kelvin Libe286f52018-09-15 13:54:15 +00006094void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6095 Record.AddStmt(C->getNumForLoops());
6096 Record.AddSourceLocation(C->getLParenLoc());
6097}
6098
6099void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
Atmn Patel577c9b02020-02-14 21:45:49 -06006100 Record.push_back(unsigned(C->getDefaultKind()));
Kelvin Libe286f52018-09-15 13:54:15 +00006101 Record.AddSourceLocation(C->getLParenLoc());
6102 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6103}
6104
6105void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06006106 Record.push_back(unsigned(C->getProcBindKind()));
Kelvin Libe286f52018-09-15 13:54:15 +00006107 Record.AddSourceLocation(C->getLParenLoc());
6108 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6109}
6110
6111void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6112 VisitOMPClauseWithPreInit(C);
6113 Record.push_back(C->getScheduleKind());
6114 Record.push_back(C->getFirstScheduleModifier());
6115 Record.push_back(C->getSecondScheduleModifier());
6116 Record.AddStmt(C->getChunkSize());
6117 Record.AddSourceLocation(C->getLParenLoc());
6118 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6119 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6120 Record.AddSourceLocation(C->getScheduleKindLoc());
6121 Record.AddSourceLocation(C->getCommaLoc());
6122}
6123
6124void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6125 Record.push_back(C->getLoopNumIterations().size());
6126 Record.AddStmt(C->getNumForLoops());
6127 for (Expr *NumIter : C->getLoopNumIterations())
6128 Record.AddStmt(NumIter);
6129 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
Mike Rice0ed46662018-09-20 17:19:41 +00006130 Record.AddStmt(C->getLoopCounter(I));
Kelvin Libe286f52018-09-15 13:54:15 +00006131 Record.AddSourceLocation(C->getLParenLoc());
6132}
6133
6134void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6135
6136void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6137
6138void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6139
6140void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6141
6142void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6143
Alexey Bataev82f7c202020-03-03 13:22:35 -05006144void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
6145 Record.push_back(C->isExtended() ? 1 : 0);
6146 if (C->isExtended()) {
6147 Record.AddSourceLocation(C->getLParenLoc());
6148 Record.AddSourceLocation(C->getArgumentLoc());
6149 Record.writeEnum(C->getDependencyKind());
6150 }
6151}
Kelvin Libe286f52018-09-15 13:54:15 +00006152
6153void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6154
6155void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6156
Alexey Bataevea9166b2020-02-06 16:30:23 -05006157void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
6158
Alexey Bataev04a830f2020-02-10 14:30:39 -05006159void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
6160
Alexey Bataev95598342020-02-10 15:49:05 -05006161void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
6162
Alexey Bataev9a8defc2020-02-11 11:10:43 -05006163void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
6164
Kelvin Libe286f52018-09-15 13:54:15 +00006165void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6166
6167void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6168
6169void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6170
Alexey Bataev375437a2020-03-02 14:21:20 -05006171void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *) {}
6172
Kelvin Libe286f52018-09-15 13:54:15 +00006173void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6174 Record.push_back(C->varlist_size());
6175 Record.AddSourceLocation(C->getLParenLoc());
6176 for (auto *VE : C->varlists()) {
6177 Record.AddStmt(VE);
6178 }
6179 for (auto *VE : C->private_copies()) {
6180 Record.AddStmt(VE);
6181 }
6182}
6183
6184void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6185 Record.push_back(C->varlist_size());
6186 VisitOMPClauseWithPreInit(C);
6187 Record.AddSourceLocation(C->getLParenLoc());
6188 for (auto *VE : C->varlists()) {
6189 Record.AddStmt(VE);
6190 }
6191 for (auto *VE : C->private_copies()) {
6192 Record.AddStmt(VE);
6193 }
6194 for (auto *VE : C->inits()) {
6195 Record.AddStmt(VE);
6196 }
6197}
6198
6199void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6200 Record.push_back(C->varlist_size());
6201 VisitOMPClauseWithPostUpdate(C);
6202 Record.AddSourceLocation(C->getLParenLoc());
Alexey Bataev93dc40d2019-12-20 11:04:57 -05006203 Record.writeEnum(C->getKind());
6204 Record.AddSourceLocation(C->getKindLoc());
6205 Record.AddSourceLocation(C->getColonLoc());
Kelvin Libe286f52018-09-15 13:54:15 +00006206 for (auto *VE : C->varlists())
6207 Record.AddStmt(VE);
6208 for (auto *E : C->private_copies())
6209 Record.AddStmt(E);
6210 for (auto *E : C->source_exprs())
6211 Record.AddStmt(E);
6212 for (auto *E : C->destination_exprs())
6213 Record.AddStmt(E);
6214 for (auto *E : C->assignment_ops())
6215 Record.AddStmt(E);
6216}
6217
6218void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6219 Record.push_back(C->varlist_size());
6220 Record.AddSourceLocation(C->getLParenLoc());
6221 for (auto *VE : C->varlists())
6222 Record.AddStmt(VE);
6223}
6224
6225void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6226 Record.push_back(C->varlist_size());
6227 VisitOMPClauseWithPostUpdate(C);
6228 Record.AddSourceLocation(C->getLParenLoc());
6229 Record.AddSourceLocation(C->getColonLoc());
6230 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6231 Record.AddDeclarationNameInfo(C->getNameInfo());
6232 for (auto *VE : C->varlists())
6233 Record.AddStmt(VE);
6234 for (auto *VE : C->privates())
6235 Record.AddStmt(VE);
6236 for (auto *E : C->lhs_exprs())
6237 Record.AddStmt(E);
6238 for (auto *E : C->rhs_exprs())
6239 Record.AddStmt(E);
6240 for (auto *E : C->reduction_ops())
6241 Record.AddStmt(E);
6242}
6243
6244void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6245 Record.push_back(C->varlist_size());
6246 VisitOMPClauseWithPostUpdate(C);
6247 Record.AddSourceLocation(C->getLParenLoc());
6248 Record.AddSourceLocation(C->getColonLoc());
6249 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6250 Record.AddDeclarationNameInfo(C->getNameInfo());
6251 for (auto *VE : C->varlists())
6252 Record.AddStmt(VE);
6253 for (auto *VE : C->privates())
6254 Record.AddStmt(VE);
6255 for (auto *E : C->lhs_exprs())
6256 Record.AddStmt(E);
6257 for (auto *E : C->rhs_exprs())
6258 Record.AddStmt(E);
6259 for (auto *E : C->reduction_ops())
6260 Record.AddStmt(E);
6261}
6262
6263void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6264 Record.push_back(C->varlist_size());
6265 VisitOMPClauseWithPostUpdate(C);
6266 Record.AddSourceLocation(C->getLParenLoc());
6267 Record.AddSourceLocation(C->getColonLoc());
6268 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6269 Record.AddDeclarationNameInfo(C->getNameInfo());
6270 for (auto *VE : C->varlists())
6271 Record.AddStmt(VE);
6272 for (auto *VE : C->privates())
6273 Record.AddStmt(VE);
6274 for (auto *E : C->lhs_exprs())
6275 Record.AddStmt(E);
6276 for (auto *E : C->rhs_exprs())
6277 Record.AddStmt(E);
6278 for (auto *E : C->reduction_ops())
6279 Record.AddStmt(E);
6280 for (auto *E : C->taskgroup_descriptors())
6281 Record.AddStmt(E);
6282}
6283
6284void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6285 Record.push_back(C->varlist_size());
6286 VisitOMPClauseWithPostUpdate(C);
6287 Record.AddSourceLocation(C->getLParenLoc());
6288 Record.AddSourceLocation(C->getColonLoc());
6289 Record.push_back(C->getModifier());
6290 Record.AddSourceLocation(C->getModifierLoc());
6291 for (auto *VE : C->varlists()) {
6292 Record.AddStmt(VE);
6293 }
6294 for (auto *VE : C->privates()) {
6295 Record.AddStmt(VE);
6296 }
6297 for (auto *VE : C->inits()) {
6298 Record.AddStmt(VE);
6299 }
6300 for (auto *VE : C->updates()) {
6301 Record.AddStmt(VE);
6302 }
6303 for (auto *VE : C->finals()) {
6304 Record.AddStmt(VE);
6305 }
6306 Record.AddStmt(C->getStep());
6307 Record.AddStmt(C->getCalcStep());
Alexey Bataev195ae902019-08-08 13:42:45 +00006308 for (auto *VE : C->used_expressions())
6309 Record.AddStmt(VE);
Kelvin Libe286f52018-09-15 13:54:15 +00006310}
6311
6312void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6313 Record.push_back(C->varlist_size());
6314 Record.AddSourceLocation(C->getLParenLoc());
6315 Record.AddSourceLocation(C->getColonLoc());
6316 for (auto *VE : C->varlists())
6317 Record.AddStmt(VE);
6318 Record.AddStmt(C->getAlignment());
6319}
6320
6321void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6322 Record.push_back(C->varlist_size());
6323 Record.AddSourceLocation(C->getLParenLoc());
6324 for (auto *VE : C->varlists())
6325 Record.AddStmt(VE);
6326 for (auto *E : C->source_exprs())
6327 Record.AddStmt(E);
6328 for (auto *E : C->destination_exprs())
6329 Record.AddStmt(E);
6330 for (auto *E : C->assignment_ops())
6331 Record.AddStmt(E);
6332}
6333
6334void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6335 Record.push_back(C->varlist_size());
6336 Record.AddSourceLocation(C->getLParenLoc());
6337 for (auto *VE : C->varlists())
6338 Record.AddStmt(VE);
6339 for (auto *E : C->source_exprs())
6340 Record.AddStmt(E);
6341 for (auto *E : C->destination_exprs())
6342 Record.AddStmt(E);
6343 for (auto *E : C->assignment_ops())
6344 Record.AddStmt(E);
6345}
6346
6347void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6348 Record.push_back(C->varlist_size());
6349 Record.AddSourceLocation(C->getLParenLoc());
6350 for (auto *VE : C->varlists())
6351 Record.AddStmt(VE);
6352}
6353
Alexey Bataevc112e942020-02-28 09:52:15 -05006354void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
6355 Record.AddStmt(C->getDepobj());
6356 Record.AddSourceLocation(C->getLParenLoc());
6357}
6358
Kelvin Libe286f52018-09-15 13:54:15 +00006359void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6360 Record.push_back(C->varlist_size());
6361 Record.push_back(C->getNumLoops());
6362 Record.AddSourceLocation(C->getLParenLoc());
6363 Record.push_back(C->getDependencyKind());
6364 Record.AddSourceLocation(C->getDependencyLoc());
6365 Record.AddSourceLocation(C->getColonLoc());
6366 for (auto *VE : C->varlists())
6367 Record.AddStmt(VE);
6368 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6369 Record.AddStmt(C->getLoopData(I));
6370}
6371
6372void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6373 VisitOMPClauseWithPreInit(C);
6374 Record.AddStmt(C->getDevice());
6375 Record.AddSourceLocation(C->getLParenLoc());
6376}
6377
6378void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6379 Record.push_back(C->varlist_size());
6380 Record.push_back(C->getUniqueDeclarationsNum());
6381 Record.push_back(C->getTotalComponentListNum());
6382 Record.push_back(C->getTotalComponentsNum());
6383 Record.AddSourceLocation(C->getLParenLoc());
Kelvin Lief579432018-12-18 22:18:41 +00006384 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
6385 Record.push_back(C->getMapTypeModifier(I));
6386 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6387 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00006388 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6389 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006390 Record.push_back(C->getMapType());
6391 Record.AddSourceLocation(C->getMapLoc());
6392 Record.AddSourceLocation(C->getColonLoc());
6393 for (auto *E : C->varlists())
6394 Record.AddStmt(E);
Michael Kruse4304e9d2019-02-19 16:38:20 +00006395 for (auto *E : C->mapperlists())
6396 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006397 for (auto *D : C->all_decls())
6398 Record.AddDeclRef(D);
6399 for (auto N : C->all_num_lists())
6400 Record.push_back(N);
6401 for (auto N : C->all_lists_sizes())
6402 Record.push_back(N);
6403 for (auto &M : C->all_components()) {
6404 Record.AddStmt(M.getAssociatedExpression());
6405 Record.AddDeclRef(M.getAssociatedDeclaration());
6406 }
6407}
6408
Alexey Bataeve04483e2019-03-27 14:14:31 +00006409void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6410 Record.push_back(C->varlist_size());
6411 Record.AddSourceLocation(C->getLParenLoc());
6412 Record.AddSourceLocation(C->getColonLoc());
6413 Record.AddStmt(C->getAllocator());
6414 for (auto *VE : C->varlists())
6415 Record.AddStmt(VE);
6416}
6417
Kelvin Libe286f52018-09-15 13:54:15 +00006418void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6419 VisitOMPClauseWithPreInit(C);
6420 Record.AddStmt(C->getNumTeams());
6421 Record.AddSourceLocation(C->getLParenLoc());
6422}
6423
6424void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6425 VisitOMPClauseWithPreInit(C);
6426 Record.AddStmt(C->getThreadLimit());
6427 Record.AddSourceLocation(C->getLParenLoc());
6428}
6429
6430void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
Alexey Bataev31ba4762019-10-16 18:09:37 +00006431 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006432 Record.AddStmt(C->getPriority());
6433 Record.AddSourceLocation(C->getLParenLoc());
6434}
6435
6436void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
Alexey Bataevb9c55e22019-10-14 19:29:52 +00006437 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006438 Record.AddStmt(C->getGrainsize());
6439 Record.AddSourceLocation(C->getLParenLoc());
6440}
6441
6442void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
Alexey Bataevd88c7de2019-10-14 20:44:34 +00006443 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006444 Record.AddStmt(C->getNumTasks());
6445 Record.AddSourceLocation(C->getLParenLoc());
6446}
6447
6448void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
6449 Record.AddStmt(C->getHint());
6450 Record.AddSourceLocation(C->getLParenLoc());
6451}
6452
6453void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
6454 VisitOMPClauseWithPreInit(C);
6455 Record.push_back(C->getDistScheduleKind());
6456 Record.AddStmt(C->getChunkSize());
6457 Record.AddSourceLocation(C->getLParenLoc());
6458 Record.AddSourceLocation(C->getDistScheduleKindLoc());
6459 Record.AddSourceLocation(C->getCommaLoc());
6460}
6461
6462void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
6463 Record.push_back(C->getDefaultmapKind());
6464 Record.push_back(C->getDefaultmapModifier());
6465 Record.AddSourceLocation(C->getLParenLoc());
6466 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
6467 Record.AddSourceLocation(C->getDefaultmapKindLoc());
6468}
6469
6470void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
6471 Record.push_back(C->varlist_size());
6472 Record.push_back(C->getUniqueDeclarationsNum());
6473 Record.push_back(C->getTotalComponentListNum());
6474 Record.push_back(C->getTotalComponentsNum());
6475 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse01f670d2019-02-22 22:29:42 +00006476 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6477 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006478 for (auto *E : C->varlists())
6479 Record.AddStmt(E);
Michael Kruse01f670d2019-02-22 22:29:42 +00006480 for (auto *E : C->mapperlists())
6481 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006482 for (auto *D : C->all_decls())
6483 Record.AddDeclRef(D);
6484 for (auto N : C->all_num_lists())
6485 Record.push_back(N);
6486 for (auto N : C->all_lists_sizes())
6487 Record.push_back(N);
6488 for (auto &M : C->all_components()) {
6489 Record.AddStmt(M.getAssociatedExpression());
6490 Record.AddDeclRef(M.getAssociatedDeclaration());
6491 }
6492}
6493
6494void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
6495 Record.push_back(C->varlist_size());
6496 Record.push_back(C->getUniqueDeclarationsNum());
6497 Record.push_back(C->getTotalComponentListNum());
6498 Record.push_back(C->getTotalComponentsNum());
6499 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse0336c752019-02-25 20:34:15 +00006500 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6501 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006502 for (auto *E : C->varlists())
6503 Record.AddStmt(E);
Michael Kruse0336c752019-02-25 20:34:15 +00006504 for (auto *E : C->mapperlists())
6505 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006506 for (auto *D : C->all_decls())
6507 Record.AddDeclRef(D);
6508 for (auto N : C->all_num_lists())
6509 Record.push_back(N);
6510 for (auto N : C->all_lists_sizes())
6511 Record.push_back(N);
6512 for (auto &M : C->all_components()) {
6513 Record.AddStmt(M.getAssociatedExpression());
6514 Record.AddDeclRef(M.getAssociatedDeclaration());
6515 }
6516}
6517
6518void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
6519 Record.push_back(C->varlist_size());
6520 Record.push_back(C->getUniqueDeclarationsNum());
6521 Record.push_back(C->getTotalComponentListNum());
6522 Record.push_back(C->getTotalComponentsNum());
6523 Record.AddSourceLocation(C->getLParenLoc());
6524 for (auto *E : C->varlists())
6525 Record.AddStmt(E);
6526 for (auto *VE : C->private_copies())
6527 Record.AddStmt(VE);
6528 for (auto *VE : C->inits())
6529 Record.AddStmt(VE);
6530 for (auto *D : C->all_decls())
6531 Record.AddDeclRef(D);
6532 for (auto N : C->all_num_lists())
6533 Record.push_back(N);
6534 for (auto N : C->all_lists_sizes())
6535 Record.push_back(N);
6536 for (auto &M : C->all_components()) {
6537 Record.AddStmt(M.getAssociatedExpression());
6538 Record.AddDeclRef(M.getAssociatedDeclaration());
6539 }
6540}
6541
6542void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
6543 Record.push_back(C->varlist_size());
6544 Record.push_back(C->getUniqueDeclarationsNum());
6545 Record.push_back(C->getTotalComponentListNum());
6546 Record.push_back(C->getTotalComponentsNum());
6547 Record.AddSourceLocation(C->getLParenLoc());
6548 for (auto *E : C->varlists())
6549 Record.AddStmt(E);
6550 for (auto *D : C->all_decls())
6551 Record.AddDeclRef(D);
6552 for (auto N : C->all_num_lists())
6553 Record.push_back(N);
6554 for (auto N : C->all_lists_sizes())
6555 Record.push_back(N);
6556 for (auto &M : C->all_components()) {
6557 Record.AddStmt(M.getAssociatedExpression());
6558 Record.AddDeclRef(M.getAssociatedDeclaration());
6559 }
6560}
Kelvin Li1408f912018-09-26 04:28:39 +00006561
6562void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
Patrick Lyster4a370b92018-10-01 13:47:43 +00006563
6564void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
6565 OMPUnifiedSharedMemoryClause *) {}
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00006566
6567void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
Patrick Lyster3fe9e392018-10-11 14:41:10 +00006568
6569void
6570OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
6571}
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00006572
6573void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
6574 OMPAtomicDefaultMemOrderClause *C) {
6575 Record.push_back(C->getAtomicDefaultMemOrderKind());
6576 Record.AddSourceLocation(C->getLParenLoc());
6577 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
6578}
Alexey Bataevb6e70842019-12-16 15:54:17 -05006579
6580void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
6581 Record.push_back(C->varlist_size());
6582 Record.AddSourceLocation(C->getLParenLoc());
6583 for (auto *VE : C->varlists())
6584 Record.AddStmt(VE);
Alexey Bataev0860db92019-12-19 10:01:10 -05006585 for (auto *E : C->private_refs())
6586 Record.AddStmt(E);
Alexey Bataevb6e70842019-12-16 15:54:17 -05006587}
Alexey Bataevcb8e6912020-01-31 16:09:26 -05006588
6589void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
6590 Record.writeEnum(C->getKind());
6591 Record.AddSourceLocation(C->getLParenLoc());
6592 Record.AddSourceLocation(C->getKindKwLoc());
6593}
6594
Johannes Doerfertb86bf832020-02-15 18:07:42 -06006595void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo &TI) {
6596 writeUInt32(TI.Sets.size());
6597 for (const auto &Set : TI.Sets) {
Johannes Doerfert1228d422019-12-19 20:42:12 -06006598 writeEnum(Set.Kind);
6599 writeUInt32(Set.Selectors.size());
6600 for (const auto &Selector : Set.Selectors) {
6601 writeEnum(Selector.Kind);
6602 writeBool(Selector.ScoreOrCondition);
6603 if (Selector.ScoreOrCondition)
6604 writeExprRef(Selector.ScoreOrCondition);
6605 writeUInt32(Selector.Properties.size());
6606 for (const auto &Property : Selector.Properties)
6607 writeEnum(Property.Kind);
6608 }
6609 }
6610}