blob: 31d004f6c946c44a4bae0455618aaa8e58ec8a81 [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
Erich Keane5f0903e2020-04-17 10:44:19 -0700479void TypeLocWriter::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
480 Record.AddSourceLocation(TL.getNameLoc());
481}
482void TypeLocWriter::VisitDependentExtIntTypeLoc(
483 clang::DependentExtIntTypeLoc TL) {
484 Record.AddSourceLocation(TL.getNameLoc());
485}
486
Richard Smith01b2cb42014-07-26 06:37:51 +0000487void ASTWriter::WriteTypeAbbrevs() {
488 using namespace llvm;
489
David Blaikieb44f0bf2017-01-04 22:36:43 +0000490 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000491
492 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000493 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000494 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
495 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
496 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000497 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000498
499 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000500 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000501 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
502 // FunctionType
503 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
504 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
505 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
506 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
507 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
508 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000509 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000510 Abv->Add(BitCodeAbbrevOp(0)); // NoCfCheck
Momchil Velikov080d0462020-03-24 09:32:51 +0000511 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // CmseNSCall
Richard Smith01b2cb42014-07-26 06:37:51 +0000512 // FunctionProtoType
513 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
514 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
515 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
516 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
517 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
518 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
519 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
520 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000521 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000522}
523
Chris Lattner19cea4e2009-04-22 05:57:30 +0000524//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000525// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000526//===----------------------------------------------------------------------===//
527
Chris Lattner28fa4e62009-04-26 22:26:21 +0000528static void EmitBlockID(unsigned ID, const char *Name,
529 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000530 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000531 Record.clear();
532 Record.push_back(ID);
533 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
534
535 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000536 if (!Name || Name[0] == 0)
537 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538 Record.clear();
539 while (*Name)
540 Record.push_back(*Name++);
541 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
542}
543
544static void EmitRecordID(unsigned ID, const char *Name,
545 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000546 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000547 Record.clear();
548 Record.push_back(ID);
549 while (*Name)
550 Record.push_back(*Name++);
551 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000552}
553
554static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000555 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000556#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000557 RECORD(STMT_STOP);
558 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000559 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000560 RECORD(STMT_NULL);
561 RECORD(STMT_COMPOUND);
562 RECORD(STMT_CASE);
563 RECORD(STMT_DEFAULT);
564 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000565 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000566 RECORD(STMT_IF);
567 RECORD(STMT_SWITCH);
568 RECORD(STMT_WHILE);
569 RECORD(STMT_DO);
570 RECORD(STMT_FOR);
571 RECORD(STMT_GOTO);
572 RECORD(STMT_INDIRECT_GOTO);
573 RECORD(STMT_CONTINUE);
574 RECORD(STMT_BREAK);
575 RECORD(STMT_RETURN);
576 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000577 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000578 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000579 RECORD(EXPR_PREDEFINED);
580 RECORD(EXPR_DECL_REF);
581 RECORD(EXPR_INTEGER_LITERAL);
Vince Bridgers161fc1d2020-04-07 14:46:08 -0500582 RECORD(EXPR_FIXEDPOINT_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000583 RECORD(EXPR_FLOATING_LITERAL);
584 RECORD(EXPR_IMAGINARY_LITERAL);
585 RECORD(EXPR_STRING_LITERAL);
586 RECORD(EXPR_CHARACTER_LITERAL);
587 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000588 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000589 RECORD(EXPR_UNARY_OPERATOR);
590 RECORD(EXPR_SIZEOF_ALIGN_OF);
591 RECORD(EXPR_ARRAY_SUBSCRIPT);
592 RECORD(EXPR_CALL);
593 RECORD(EXPR_MEMBER);
594 RECORD(EXPR_BINARY_OPERATOR);
595 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
596 RECORD(EXPR_CONDITIONAL_OPERATOR);
597 RECORD(EXPR_IMPLICIT_CAST);
598 RECORD(EXPR_CSTYLE_CAST);
599 RECORD(EXPR_COMPOUND_LITERAL);
600 RECORD(EXPR_EXT_VECTOR_ELEMENT);
601 RECORD(EXPR_INIT_LIST);
602 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000603 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000604 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000605 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000606 RECORD(EXPR_VA_ARG);
607 RECORD(EXPR_ADDR_LABEL);
608 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000609 RECORD(EXPR_CHOOSE);
610 RECORD(EXPR_GNU_NULL);
611 RECORD(EXPR_SHUFFLE_VECTOR);
612 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000613 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000614 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000615 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000616 RECORD(EXPR_OBJC_ARRAY_LITERAL);
617 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000618 RECORD(EXPR_OBJC_ENCODE);
619 RECORD(EXPR_OBJC_SELECTOR_EXPR);
620 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
621 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
622 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
623 RECORD(EXPR_OBJC_KVC_REF_EXPR);
624 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000625 RECORD(STMT_OBJC_FOR_COLLECTION);
626 RECORD(STMT_OBJC_CATCH);
627 RECORD(STMT_OBJC_FINALLY);
628 RECORD(STMT_OBJC_AT_TRY);
629 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
630 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000631 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000632 RECORD(STMT_CXX_CATCH);
633 RECORD(STMT_CXX_TRY);
634 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000635 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000636 RECORD(EXPR_CXX_MEMBER_CALL);
Richard Smith778dc0f2019-10-19 00:04:38 +0000637 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000638 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000639 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000640 RECORD(EXPR_CXX_STATIC_CAST);
641 RECORD(EXPR_CXX_DYNAMIC_CAST);
642 RECORD(EXPR_CXX_REINTERPRET_CAST);
643 RECORD(EXPR_CXX_CONST_CAST);
644 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000645 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000646 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000647 RECORD(EXPR_CXX_BOOL_LITERAL);
648 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000649 RECORD(EXPR_CXX_TYPEID_EXPR);
650 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000651 RECORD(EXPR_CXX_THIS);
652 RECORD(EXPR_CXX_THROW);
653 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000654 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000655 RECORD(EXPR_CXX_BIND_TEMPORARY);
656 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
657 RECORD(EXPR_CXX_NEW);
658 RECORD(EXPR_CXX_DELETE);
659 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
660 RECORD(EXPR_EXPR_WITH_CLEANUPS);
661 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
662 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
663 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
664 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
665 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000666 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000667 RECORD(EXPR_CXX_NOEXCEPT);
668 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000669 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
670 RECORD(EXPR_TYPE_TRAIT);
671 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000672 RECORD(EXPR_PACK_EXPANSION);
673 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000674 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000675 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000676 RECORD(EXPR_FUNCTION_PARM_PACK);
677 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000678 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000679 RECORD(EXPR_CXX_UUIDOF_EXPR);
680 RECORD(EXPR_CXX_UUIDOF_TYPE);
681 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000682#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000683}
Mike Stump11289f42009-09-09 15:08:12 +0000684
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000685void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000686 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000687 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +0000688
Sebastian Redl539c5062010-08-18 23:57:32 +0000689#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
690#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000691
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000692 // Control Block.
693 BLOCK(CONTROL_BLOCK);
694 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000695 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +0000696 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000697 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000698 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000699 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000700 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000701 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000702 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +0000703
704 BLOCK(OPTIONS_BLOCK);
705 RECORD(LANGUAGE_OPTIONS);
706 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000707 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000708 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000709 RECORD(PREPROCESSOR_OPTIONS);
710
Douglas Gregor108cb222012-10-19 00:45:00 +0000711 BLOCK(INPUT_FILES_BLOCK);
712 RECORD(INPUT_FILE);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +0000713 RECORD(INPUT_FILE_HASH);
Douglas Gregor108cb222012-10-19 00:45:00 +0000714
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000715 // AST Top-Level Block.
716 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000717 RECORD(TYPE_OFFSET);
718 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000719 RECORD(IDENTIFIER_OFFSET);
720 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000721 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +0000722 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000723 RECORD(SPECIAL_TYPES);
724 RECORD(STATISTICS);
725 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000726 RECORD(SELECTOR_OFFSETS);
727 RECORD(METHOD_POOL);
728 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000729 RECORD(SOURCE_LOCATION_OFFSETS);
730 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000731 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +0000732 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000733 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +0000734 RECORD(VTABLE_USES);
Cameron Desrochersb60f1b62018-01-15 19:14:16 +0000735 RECORD(PPD_SKIPPED_RANGES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000736 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000737 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000738 RECORD(SEMA_DECL_REFS);
739 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
740 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000741 RECORD(UPDATE_VISIBLE);
742 RECORD(DECL_UPDATE_OFFSETS);
743 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000744 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000745 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000746 RECORD(FP_PRAGMA_OPTIONS);
747 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +0000748 RECORD(OPENCL_EXTENSION_TYPES);
749 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000750 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000751 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000752 RECORD(MODULE_OFFSET_MAP);
753 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000754 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000755 RECORD(FILE_SORTED_DECLS);
756 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000757 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000758 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +0000759 RECORD(INTERESTING_IDENTIFIERS);
760 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000761 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000762 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +0000763 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +0000764 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +0000765 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +0000766 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +0000767 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +0000768 RECORD(PP_CONDITIONAL_STACK);
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -0500769 RECORD(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS);
Douglas Gregor358cd442012-01-15 16:58:34 +0000770
Chris Lattner28fa4e62009-04-26 22:26:21 +0000771 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000772 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(SM_SLOC_FILE_ENTRY);
774 RECORD(SM_SLOC_BUFFER_ENTRY);
775 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +0000776 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000777 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattner28fa4e62009-04-26 22:26:21 +0000779 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000780 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000781 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000782 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +0000783 RECORD(PP_MACRO_OBJECT_LIKE);
784 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000785 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000786
Richard Smithfa715652015-08-31 22:43:10 +0000787 // Submodule Block.
788 BLOCK(SUBMODULE_BLOCK);
789 RECORD(SUBMODULE_METADATA);
790 RECORD(SUBMODULE_DEFINITION);
791 RECORD(SUBMODULE_UMBRELLA_HEADER);
792 RECORD(SUBMODULE_HEADER);
793 RECORD(SUBMODULE_TOPHEADER);
794 RECORD(SUBMODULE_UMBRELLA_DIR);
795 RECORD(SUBMODULE_IMPORTS);
796 RECORD(SUBMODULE_EXPORTS);
797 RECORD(SUBMODULE_REQUIRES);
798 RECORD(SUBMODULE_EXCLUDED_HEADER);
799 RECORD(SUBMODULE_LINK_LIBRARY);
800 RECORD(SUBMODULE_CONFIG_MACRO);
801 RECORD(SUBMODULE_CONFLICT);
802 RECORD(SUBMODULE_PRIVATE_HEADER);
803 RECORD(SUBMODULE_TEXTUAL_HEADER);
804 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +0000805 RECORD(SUBMODULE_INITIALIZERS);
Douglas Gregorf0b11de2017-09-14 23:38:44 +0000806 RECORD(SUBMODULE_EXPORT_AS);
Richard Smithfa715652015-08-31 22:43:10 +0000807
808 // Comments Block.
809 BLOCK(COMMENTS_BLOCK);
810 RECORD(COMMENTS_RAW_COMMENT);
811
Douglas Gregor12bfa382009-10-17 00:13:19 +0000812 // Decls and Types block.
813 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000814 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000815 RECORD(TYPE_COMPLEX);
816 RECORD(TYPE_POINTER);
817 RECORD(TYPE_BLOCK_POINTER);
818 RECORD(TYPE_LVALUE_REFERENCE);
819 RECORD(TYPE_RVALUE_REFERENCE);
820 RECORD(TYPE_MEMBER_POINTER);
821 RECORD(TYPE_CONSTANT_ARRAY);
822 RECORD(TYPE_INCOMPLETE_ARRAY);
823 RECORD(TYPE_VARIABLE_ARRAY);
824 RECORD(TYPE_VECTOR);
825 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000826 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000827 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000828 RECORD(TYPE_TYPEDEF);
829 RECORD(TYPE_TYPEOF_EXPR);
830 RECORD(TYPE_TYPEOF);
831 RECORD(TYPE_RECORD);
832 RECORD(TYPE_ENUM);
833 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000834 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000835 RECORD(TYPE_DECLTYPE);
836 RECORD(TYPE_ELABORATED);
837 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
838 RECORD(TYPE_UNRESOLVED_USING);
839 RECORD(TYPE_INJECTED_CLASS_NAME);
840 RECORD(TYPE_OBJC_OBJECT);
841 RECORD(TYPE_TEMPLATE_TYPE_PARM);
842 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
843 RECORD(TYPE_DEPENDENT_NAME);
844 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
845 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
846 RECORD(TYPE_PAREN);
Leonard Chanc72aaf62019-05-07 03:20:17 +0000847 RECORD(TYPE_MACRO_QUALIFIED);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000848 RECORD(TYPE_PACK_EXPANSION);
849 RECORD(TYPE_ATTRIBUTED);
850 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000851 RECORD(TYPE_AUTO);
852 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000853 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000854 RECORD(TYPE_DECAYED);
855 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +0000856 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +0000857 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000858 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +0000859 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000860 RECORD(DECL_ENUM);
861 RECORD(DECL_RECORD);
862 RECORD(DECL_ENUM_CONSTANT);
863 RECORD(DECL_FUNCTION);
864 RECORD(DECL_OBJC_METHOD);
865 RECORD(DECL_OBJC_INTERFACE);
866 RECORD(DECL_OBJC_PROTOCOL);
867 RECORD(DECL_OBJC_IVAR);
868 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000869 RECORD(DECL_OBJC_CATEGORY);
870 RECORD(DECL_OBJC_CATEGORY_IMPL);
871 RECORD(DECL_OBJC_IMPLEMENTATION);
872 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
873 RECORD(DECL_OBJC_PROPERTY);
874 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000875 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000876 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000877 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000878 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000879 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000880 RECORD(DECL_FILE_SCOPE_ASM);
881 RECORD(DECL_BLOCK);
882 RECORD(DECL_CONTEXT_LEXICAL);
883 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000884 RECORD(DECL_NAMESPACE);
885 RECORD(DECL_NAMESPACE_ALIAS);
886 RECORD(DECL_USING);
887 RECORD(DECL_USING_SHADOW);
888 RECORD(DECL_USING_DIRECTIVE);
889 RECORD(DECL_UNRESOLVED_USING_VALUE);
890 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
891 RECORD(DECL_LINKAGE_SPEC);
892 RECORD(DECL_CXX_RECORD);
893 RECORD(DECL_CXX_METHOD);
894 RECORD(DECL_CXX_CONSTRUCTOR);
895 RECORD(DECL_CXX_DESTRUCTOR);
896 RECORD(DECL_CXX_CONVERSION);
897 RECORD(DECL_ACCESS_SPEC);
898 RECORD(DECL_FRIEND);
899 RECORD(DECL_FRIEND_TEMPLATE);
900 RECORD(DECL_CLASS_TEMPLATE);
901 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
902 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000903 RECORD(DECL_VAR_TEMPLATE);
904 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
905 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000906 RECORD(DECL_FUNCTION_TEMPLATE);
907 RECORD(DECL_TEMPLATE_TYPE_PARM);
908 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
909 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Saar Razd7aae332019-07-10 21:25:49 +0000910 RECORD(DECL_CONCEPT);
Saar Raza0f50d72020-01-18 09:11:43 +0200911 RECORD(DECL_REQUIRES_EXPR_BODY);
Richard Smithefc884a2016-04-13 07:41:35 +0000912 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000913 RECORD(DECL_STATIC_ASSERT);
914 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +0000915 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000916 RECORD(DECL_INDIRECTFIELD);
917 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +0000918 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
919 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
920 RECORD(DECL_IMPORT);
921 RECORD(DECL_OMP_THREADPRIVATE);
922 RECORD(DECL_EMPTY);
923 RECORD(DECL_OBJC_TYPE_PARAM);
924 RECORD(DECL_OMP_CAPTUREDEXPR);
925 RECORD(DECL_PRAGMA_COMMENT);
926 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
927 RECORD(DECL_OMP_DECLARE_REDUCTION);
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000928 RECORD(DECL_OMP_ALLOCATE);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000929
Douglas Gregor03412ba2011-06-03 02:27:19 +0000930 // Statements and Exprs can occur in the Decls and Types block.
931 AddStmtsExprs(Stream, Record);
932
Douglas Gregor92a96f52011-02-08 21:58:10 +0000933 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000934 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000935 RECORD(PPD_MACRO_DEFINITION);
936 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000937
938 // Decls and Types block.
939 BLOCK(EXTENSION_BLOCK);
940 RECORD(EXTENSION_METADATA);
941
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +0000942 BLOCK(UNHASHED_CONTROL_BLOCK);
943 RECORD(SIGNATURE);
944 RECORD(DIAGNOSTIC_OPTIONS);
945 RECORD(DIAG_PRAGMA_MAPPINGS);
946
Chris Lattner28fa4e62009-04-26 22:26:21 +0000947#undef RECORD
948#undef BLOCK
949 Stream.ExitBlock();
950}
951
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000952/// Prepares a path for being written to an AST file by converting it
Richard Smith54cc3c22014-12-11 20:50:24 +0000953/// to an absolute path and removing nested './'s.
954///
955/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +0000956static bool cleanPathForOutput(FileManager &FileMgr,
957 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +0000958 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +0000959 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +0000960}
961
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000962/// Adjusts the given filename to only write out the portion of the
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000964///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000965/// \param Filename the file name to adjust.
966///
Richard Smith7ed1bc92014-12-05 22:42:13 +0000967/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
968/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000969///
970/// \returns either the original filename (if it needs no adjustment) or the
971/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000972static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +0000973adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000974 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000975
Richard Smith7ed1bc92014-12-05 22:42:13 +0000976 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000977 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000978
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000979 // Verify that the filename and the system root have the same prefix.
980 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +0000981 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
982 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000983 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000984
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000985 // We hit the end of the filename before we hit the end of the system root.
986 if (!Filename[Pos])
987 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000988
Richard Smith7ed1bc92014-12-05 22:42:13 +0000989 // If there's not a path separator at the end of the base directory nor
990 // immediately after it, then this isn't within the base directory.
991 if (!llvm::sys::path::is_separator(Filename[Pos])) {
992 if (!llvm::sys::path::is_separator(BaseDir.back()))
993 return Filename;
994 } else {
995 // If the file name has a '/' at the current position, skip over the '/'.
996 // We distinguish relative paths from absolute paths by the
997 // absence of '/' at the beginning of relative paths.
998 //
999 // FIXME: This is wrong. We distinguish them by asking if the path is
1000 // absolute, which isn't the same thing. And there might be multiple '/'s
1001 // in a row. Use a better mechanism to indicate whether we have emitted an
1002 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001003 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001006 return Filename + Pos;
1007}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001008
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001009ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1010 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1011 llvm::SHA1 Hasher;
1012 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1013 auto Hash = Hasher.result();
1014
1015 // Convert to an array [5*i32].
1016 ASTFileSignature Signature;
1017 auto LShift = [&](unsigned char Val, unsigned Shift) {
1018 return (uint32_t)Val << Shift;
1019 };
1020 for (int I = 0; I != 5; ++I)
1021 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1022 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1023
1024 return Signature;
1025}
1026
1027ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1028 ASTContext &Context) {
1029 // Flush first to prepare the PCM hash (signature).
1030 Stream.FlushToWord();
1031 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1032
1033 // Enter the block and prepare to write records.
1034 RecordData Record;
1035 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1036
1037 // For implicit modules, write the hash of the PCM as its signature.
1038 ASTFileSignature Signature;
1039 if (WritingModule &&
1040 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1041 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1042 Record.append(Signature.begin(), Signature.end());
1043 Stream.EmitRecord(SIGNATURE, Record);
1044 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001045 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001046
1047 // Diagnostic options.
1048 const auto &Diags = Context.getDiagnostics();
1049 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1050#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1051#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1052 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1053#include "clang/Basic/DiagnosticOptions.def"
1054 Record.push_back(DiagOpts.Warnings.size());
1055 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1056 AddString(DiagOpts.Warnings[I], Record);
1057 Record.push_back(DiagOpts.Remarks.size());
1058 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1059 AddString(DiagOpts.Remarks[I], Record);
1060 // Note: we don't serialize the log or serialization file names, because they
1061 // are generally transient files and will almost always be overridden.
1062 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1063
1064 // Write out the diagnostic/pragma mappings.
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001065 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001066
1067 // Leave the options block.
1068 Stream.ExitBlock();
1069 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001070}
1071
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001072/// Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001073void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1074 StringRef isysroot,
1075 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001076 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001077
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001078 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001079 RecordData Record;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001080
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001081 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001082 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001083 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1084 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1085 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1086 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1087 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1088 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001089 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001090 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PCHHasObjectFile
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001091 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1092 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001093 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001094 assert((!WritingModule || isysroot.empty()) &&
1095 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001096 {
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001097 RecordData::value_type Record[] = {
1098 METADATA,
1099 VERSION_MAJOR,
1100 VERSION_MINOR,
1101 CLANG_VERSION_MAJOR,
1102 CLANG_VERSION_MINOR,
1103 !isysroot.empty(),
1104 IncludeTimestamps,
1105 Context.getLangOpts().BuildingPCHWithObjectFile,
1106 ASTHasCompilerErrors};
Mehdi Amini57a41912015-09-10 01:46:39 +00001107 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1108 getClangFullRepositoryVersion());
1109 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001110
Richard Smith8b706102017-05-31 20:56:55 +00001111 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001112 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001113 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001114 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001116 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001117 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001118 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1119 }
1120
Richard Smith7ed1bc92014-12-05 22:42:13 +00001121 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001122 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001123 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001124
1125 // If the home of the module is the current working directory, then we
1126 // want to pick up the cwd of the build process loading the module, not
1127 // our cwd, when we load this module.
1128 if (!PP.getHeaderSearchInfo()
1129 .getHeaderSearchOpts()
1130 .ModuleMapFileHomeIsCwd ||
1131 WritingModule->Directory->getName() != StringRef(".")) {
1132 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001133 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001134 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001136 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001137
Mehdi Amini57a41912015-09-10 01:46:39 +00001138 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001139 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1140 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001141
1142 // Write out all other paths relative to the base directory if possible.
1143 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1144 } else if (!isysroot.empty()) {
1145 // Write out paths relative to the sysroot if possible.
Benjamin Krameradcd0262020-01-28 20:23:46 +01001146 BaseDirectory = std::string(isysroot);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001147 }
1148
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001149 // Module map file
Richard Smithd19389a2017-07-05 07:47:11 +00001150 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001151 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001152
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001153 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001154 AddPath(WritingModule->PresumedModuleMapFile.empty()
1155 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1156 : StringRef(WritingModule->PresumedModuleMapFile),
1157 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001158
1159 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001160 if (auto *AdditionalModMaps =
1161 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001162 Record.push_back(AdditionalModMaps->size());
1163 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001164 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001165 } else {
1166 Record.push_back(0);
1167 }
1168
1169 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001170 }
1171
Douglas Gregor112b9072012-10-18 05:31:06 +00001172 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001173 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001174 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001175 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001176
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001177 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001178 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001179 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001180 continue;
1181
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001182 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1183 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001184
1185 // If we have calculated signature, there is no need to store
1186 // the size or timestamp.
1187 Record.push_back(M.Signature ? 0 : M.File->getSize());
1188 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1189
1190 for (auto I : M.Signature)
1191 Record.push_back(I);
1192
Boris Kolpackovd30446f2017-08-31 06:26:43 +00001193 AddString(M.ModuleName, Record);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001194 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001195 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001196 Stream.EmitRecord(IMPORTS, Record);
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Richard Smith0516b182015-09-08 19:40:14 +00001199 // Write the options block.
1200 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1201
Douglas Gregor112b9072012-10-18 05:31:06 +00001202 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001203 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001204 const LangOptions &LangOpts = Context.getLangOpts();
1205#define LANGOPT(Name, Bits, Default, Description) \
1206 Record.push_back(LangOpts.Name);
1207#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1208 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001209#include "clang/Basic/LangOptions.def"
1210#define SANITIZER(NAME, ID) \
1211 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001212#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001213
Ben Langmuircd98cb72015-06-23 18:20:18 +00001214 Record.push_back(LangOpts.ModuleFeatures.size());
1215 for (StringRef Feature : LangOpts.ModuleFeatures)
1216 AddString(Feature, Record);
1217
Douglas Gregor112b9072012-10-18 05:31:06 +00001218 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1219 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001220
1221 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001222
1223 // Comment options.
1224 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001225 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1226 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001227 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001228 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001229
Samuel Antaoee8fb302016-01-06 13:42:12 +00001230 // OpenMP offloading options.
1231 Record.push_back(LangOpts.OMPTargetTriples.size());
1232 for (auto &T : LangOpts.OMPTargetTriples)
1233 AddString(T.getTriple(), Record);
1234
1235 AddString(LangOpts.OMPHostIRFile, Record);
1236
Douglas Gregor112b9072012-10-18 05:31:06 +00001237 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1238
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001239 // Target options.
1240 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001241 const TargetInfo &Target = Context.getTargetInfo();
1242 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001243 AddString(TargetOpts.Triple, Record);
1244 AddString(TargetOpts.CPU, Record);
1245 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001246 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1247 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1248 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1249 }
1250 Record.push_back(TargetOpts.Features.size());
1251 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1252 AddString(TargetOpts.Features[I], Record);
1253 }
1254 Stream.EmitRecord(TARGET_OPTIONS, Record);
1255
Douglas Gregorc6317db2012-10-24 15:49:58 +00001256 // File system options.
1257 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001258 const FileSystemOptions &FSOpts =
1259 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001260 AddString(FSOpts.WorkingDir, Record);
1261 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1262
Douglas Gregor2d302362012-10-24 16:50:34 +00001263 // Header search options.
1264 Record.clear();
1265 const HeaderSearchOptions &HSOpts
1266 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1267 AddString(HSOpts.Sysroot, Record);
1268
1269 // Include entries.
1270 Record.push_back(HSOpts.UserEntries.size());
1271 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1272 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1273 AddString(Entry.Path, Record);
1274 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001275 Record.push_back(Entry.IsFramework);
1276 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001277 }
1278
1279 // System header prefixes.
1280 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1281 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1282 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1283 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1284 }
1285
1286 AddString(HSOpts.ResourceDir, Record);
1287 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001288 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001289 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001290 Record.push_back(HSOpts.ImplicitModuleMaps);
1291 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001292 Record.push_back(HSOpts.UseBuiltinIncludes);
1293 Record.push_back(HSOpts.UseStandardSystemIncludes);
1294 Record.push_back(HSOpts.UseStandardCXXIncludes);
1295 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001296 // Write out the specific module cache path that contains the module files.
1297 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001298 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1299
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001300 // Preprocessor options.
1301 Record.clear();
1302 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1303
1304 // Macro definitions.
1305 Record.push_back(PPOpts.Macros.size());
1306 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1307 AddString(PPOpts.Macros[I].first, Record);
1308 Record.push_back(PPOpts.Macros[I].second);
1309 }
1310
1311 // Includes
1312 Record.push_back(PPOpts.Includes.size());
1313 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1314 AddString(PPOpts.Includes[I], Record);
1315
1316 // Macro includes
1317 Record.push_back(PPOpts.MacroIncludes.size());
1318 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1319 AddString(PPOpts.MacroIncludes[I], Record);
1320
Douglas Gregorb6368752012-10-24 23:41:50 +00001321 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001322 // Detailed record is important since it is used for the module cache hash.
1323 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001324 AddString(PPOpts.ImplicitPCHInclude, Record);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001325 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1326 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1327
Richard Smith0516b182015-09-08 19:40:14 +00001328 // Leave the options block.
1329 Stream.ExitBlock();
1330
Douglas Gregora3b20262011-05-06 21:43:30 +00001331 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001332 SourceManager &SM = Context.getSourceManager();
1333 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001334 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001335 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1336 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001337 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001338 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001339
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001340 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001341 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001342 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001343 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001344 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001345
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001346 Record.clear();
1347 Record.push_back(SM.getMainFileID().getOpaqueValue());
1348 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1349
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001350 // Original PCH directory
1351 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001352 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001353 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001355 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001356
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001357 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001358
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001359 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001360 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1361
Mehdi Amini57a41912015-09-10 01:46:39 +00001362 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001363 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1364 }
1365
Douglas Gregor49491f72013-03-15 22:15:07 +00001366 WriteInputFiles(Context.SourceMgr,
1367 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001368 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001369 Stream.ExitBlock();
1370}
1371
Douglas Gregor49491f72013-03-15 22:15:07 +00001372namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001373
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001374/// An input file.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001375struct InputFileEntry {
1376 const FileEntry *File;
1377 bool IsSystemFile;
1378 bool IsTransient;
1379 bool BufferOverridden;
1380 bool IsTopLevelModuleMap;
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001381 uint32_t ContentHash[2];
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001382};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001383
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001384} // namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001385
1386void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1387 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001388 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001389 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001390
Douglas Gregor72be3902012-10-19 00:38:02 +00001391 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001392
Douglas Gregor72be3902012-10-19 00:38:02 +00001393 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001394 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001395 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001396 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001397 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1398 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001399 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001400 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Richard Smithf3f84612017-06-29 02:19:42 +00001401 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
Douglas Gregor72be3902012-10-19 00:38:02 +00001402 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001403 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001404
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001405 // Create input file hash abbreviation.
1406 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1407 IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1408 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1409 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1410 unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1411
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001412 // Get all ContentCache objects for files, sorted by whether the file is a
1413 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001414 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001415 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1416 // Get this source location entry.
1417 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001418 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001419
1420 // We only care about file entries that were not overridden.
1421 if (!SLoc->isFile())
1422 continue;
Richard Smithf3f84612017-06-29 02:19:42 +00001423 const SrcMgr::FileInfo &File = SLoc->getFile();
1424 const SrcMgr::ContentCache *Cache = File.getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001425 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001426 continue;
1427
Douglas Gregor49491f72013-03-15 22:15:07 +00001428 InputFileEntry Entry;
1429 Entry.File = Cache->OrigEntry;
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001430 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
Richard Smitha8cfffa2015-11-26 02:04:16 +00001431 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001432 Entry.BufferOverridden = Cache->BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001433 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1434 File.getIncludeLoc().isInvalid();
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001435
1436 auto ContentHash = hash_code(-1);
1437 if (PP->getHeaderSearchInfo()
1438 .getHeaderSearchOpts()
1439 .ValidateASTInputFilesContent) {
1440 auto *MemBuff = Cache->getRawBuffer();
1441 if (MemBuff)
1442 ContentHash = hash_value(MemBuff->getBuffer());
1443 else
1444 // FIXME: The path should be taken from the FileEntryRef.
1445 PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1446 << Entry.File->getName();
1447 }
1448 auto CH = llvm::APInt(64, ContentHash);
1449 Entry.ContentHash[0] =
1450 static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1451 Entry.ContentHash[1] =
1452 static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1453
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001454 if (Entry.IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001455 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001456 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001457 SortedFiles.push_front(Entry);
1458 }
1459
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001460 unsigned UserFilesNum = 0;
1461 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001462 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001463 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001464 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001465 if (InputFileID != 0)
1466 continue; // already recorded this file.
1467
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001468 // Record this entry's offset.
1469 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001470
1471 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001472
Douglas Gregor49491f72013-03-15 22:15:07 +00001473 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001474 ++UserFilesNum;
1475
Douglas Gregor72be3902012-10-19 00:38:02 +00001476 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001477 // And whether this file was overridden.
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001478 {
1479 RecordData::value_type Record[] = {
1480 INPUT_FILE,
1481 InputFileOffsets.size(),
1482 (uint64_t)Entry.File->getSize(),
1483 (uint64_t)getTimestampForOutput(Entry.File),
1484 Entry.BufferOverridden,
1485 Entry.IsTransient,
1486 Entry.IsTopLevelModuleMap};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001487
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001488 // FIXME: The path should be taken from the FileEntryRef.
1489 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1490 }
1491
1492 // Emit content hash for this file.
1493 {
1494 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1495 Entry.ContentHash[1]};
1496 Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1497 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001498 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001499
Douglas Gregor112b9072012-10-18 05:31:06 +00001500 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001501
1502 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001503 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001504 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1505 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001506 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1507 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001508 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001509 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001510
1511 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001512 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1513 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001514 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001515}
1516
Douglas Gregora7f71a92009-04-10 03:52:48 +00001517//===----------------------------------------------------------------------===//
1518// Source Manager Serialization
1519//===----------------------------------------------------------------------===//
1520
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001521/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001522/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001523static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001524 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001525
David Blaikieb44f0bf2017-01-04 22:36:43 +00001526 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001527 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001532 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001537 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001538}
1539
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001540/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001541/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001542static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001543 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001544
David Blaikieb44f0bf2017-01-04 22:36:43 +00001545 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001546 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001552 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001553}
1554
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001555/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001556/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001557static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1558 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001559 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001560
David Blaikieb44f0bf2017-01-04 22:36:43 +00001561 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001562 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1563 : SM_SLOC_BUFFER_BLOB));
1564 if (Compressed)
1565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001567 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001568}
1569
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001570/// Create an abbreviation for the SLocEntry that refers to a macro
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001571/// expansion.
1572static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001574
David Blaikieb44f0bf2017-01-04 22:36:43 +00001575 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001576 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Richard Smithb5f81712018-04-30 05:25:48 +00001581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
Douglas Gregor83243272009-04-15 18:05:10 +00001582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001583 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001584}
1585
Douglas Gregor09b69892011-02-10 17:09:37 +00001586namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001587
Douglas Gregor09b69892011-02-10 17:09:37 +00001588 // Trait used for the on-disk hash table of header search information.
1589 class HeaderFileInfoTrait {
1590 ASTWriter &Writer;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001591
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001592 // Keep track of the framework names we've used during serialization.
1593 SmallVector<char, 128> FrameworkStringData;
1594 llvm::StringMap<unsigned> FrameworkNameOffset;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001595
Douglas Gregor09b69892011-02-10 17:09:37 +00001596 public:
Richard Smith040e1262017-06-02 01:55:39 +00001597 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1598
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001599 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001600 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001601 off_t Size;
1602 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001603 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001604 using key_type_ref = const key_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001605
1606 using UnresolvedModule =
1607 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001608
Richard Smith040e1262017-06-02 01:55:39 +00001609 struct data_type {
1610 const HeaderFileInfo &HFI;
1611 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1612 UnresolvedModule Unresolved;
1613 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001614 using data_type_ref = const data_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001615
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001616 using hash_value_type = unsigned;
1617 using offset_type = unsigned;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001618
Richard Smithe75ee0f2015-08-17 07:13:32 +00001619 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001620 // The hash is based only on size/time of the file, so that the reader can
1621 // match even when symlinking or excess path elements ("foo/../", "../")
1622 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00001623 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00001624 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001625
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001626 std::pair<unsigned, unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001627 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001628 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001629
Peter Collingbournee3f65292018-05-18 19:46:24 +00001630 endian::Writer LE(Out, little);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001631 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001632 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001633 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00001634 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00001635 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1636 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00001637 if (Data.Unresolved.getPointer())
1638 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00001639 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001640 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001641 }
Richard Smith040e1262017-06-02 01:55:39 +00001642
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001643 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001644 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001645
Peter Collingbournee3f65292018-05-18 19:46:24 +00001646 endian::Writer LE(Out, little);
Richard Smith040e1262017-06-02 01:55:39 +00001647 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001648 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00001649 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001650 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001651 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001652 }
Richard Smith040e1262017-06-02 01:55:39 +00001653
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001654 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001655 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001656 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001657
Peter Collingbournee3f65292018-05-18 19:46:24 +00001658 endian::Writer LE(Out, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00001659 uint64_t Start = Out.tell(); (void)Start;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001660
Richard Smithf3f84612017-06-29 02:19:42 +00001661 unsigned char Flags = (Data.HFI.isImport << 5)
1662 | (Data.HFI.isPragmaOnce << 4)
Richard Smith040e1262017-06-02 01:55:39 +00001663 | (Data.HFI.DirInfo << 1)
1664 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001665 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00001666 LE.write<uint16_t>(Data.HFI.NumIncludes);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001667
Richard Smith040e1262017-06-02 01:55:39 +00001668 if (!Data.HFI.ControllingMacro)
1669 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001670 else
Richard Smith040e1262017-06-02 01:55:39 +00001671 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1672
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001673 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00001674 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001675 // If this header refers into a framework, save the framework name.
1676 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00001677 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001678 if (Pos == FrameworkNameOffset.end()) {
1679 Offset = FrameworkStringData.size() + 1;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001680 FrameworkStringData.append(Data.HFI.Framework.begin(),
Richard Smith040e1262017-06-02 01:55:39 +00001681 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001682 FrameworkStringData.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001683
Richard Smith040e1262017-06-02 01:55:39 +00001684 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001685 } else
1686 Offset = Pos->second;
1687 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001688 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001689
Richard Smith040e1262017-06-02 01:55:39 +00001690 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1691 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1692 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00001693 assert((Value >> 2) == ModID && "overflow in header module info");
1694 LE.write<uint32_t>(Value);
1695 }
Richard Smith040e1262017-06-02 01:55:39 +00001696 };
1697
1698 // FIXME: If the header is excluded, we should write out some
1699 // record of that fact.
1700 for (auto ModInfo : Data.KnownHeaders)
1701 EmitModule(ModInfo.getModule(), ModInfo.getRole());
1702 if (Data.Unresolved.getPointer())
1703 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001704
Douglas Gregor09b69892011-02-10 17:09:37 +00001705 assert(Out.tell() - Start == DataLen && "Wrong data length");
1706 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001707
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001708 const char *strings_begin() const { return FrameworkStringData.begin(); }
1709 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001710 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001711
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001712} // namespace
Douglas Gregor09b69892011-02-10 17:09:37 +00001713
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001714/// Write the header search block for the list of files that
Douglas Gregor09b69892011-02-10 17:09:37 +00001715///
1716/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001717void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00001718 HeaderFileInfoTrait GeneratorTrait(*this);
1719 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1720 SmallVector<const char *, 4> SavedStrings;
1721 unsigned NumHeaderSearchEntries = 0;
1722
1723 // Find all unresolved headers for the current module. We generally will
1724 // have resolved them before we get here, but not necessarily: we might be
1725 // compiling a preprocessed module, where there is no requirement for the
1726 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00001727 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00001728 if (WritingModule) {
1729 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1730 while (!Worklist.empty()) {
1731 Module *M = Worklist.pop_back_val();
Richard Smith6bc75022020-04-17 20:25:15 -07001732 // We don't care about headers in unimportable submodules.
1733 if (M->isUnimportable())
Richard Smith040e1262017-06-02 01:55:39 +00001734 continue;
1735
1736 // Map to disk files where possible, to pick up any missing stat
1737 // information. This also means we don't need to check the unresolved
1738 // headers list when emitting resolved headers in the first loop below.
1739 // FIXME: It'd be preferable to avoid doing this if we were given
1740 // sufficient stat information in the module map.
1741 HS.getModuleMap().resolveHeaderDirectives(M);
1742
1743 // If the file didn't exist, we can still create a module if we were given
1744 // enough information in the module map.
1745 for (auto U : M->MissingHeaders) {
1746 // Check that we were given enough information to build a module
1747 // without this file existing on disk.
1748 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
1749 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
1750 << WritingModule->getFullModuleName() << U.Size.hasValue()
1751 << U.FileName;
1752 continue;
1753 }
1754
1755 // Form the effective relative pathname for the file.
1756 SmallString<128> Filename(M->Directory->getName());
1757 llvm::sys::path::append(Filename, U.FileName);
1758 PreparePathForOutput(Filename);
1759
1760 StringRef FilenameDup = strdup(Filename.c_str());
1761 SavedStrings.push_back(FilenameDup.data());
1762
1763 HeaderFileInfoTrait::key_type Key = {
1764 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
1765 };
1766 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00001767 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00001768 };
1769 // FIXME: Deal with cases where there are multiple unresolved header
1770 // directives in different submodules for the same header.
1771 Generator.insert(Key, Data, GeneratorTrait);
1772 ++NumHeaderSearchEntries;
1773 }
1774
1775 Worklist.append(M->submodule_begin(), M->submodule_end());
1776 }
1777 }
1778
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001779 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001780 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001781
Douglas Gregor09b69892011-02-10 17:09:37 +00001782 if (FilesByUID.size() > HS.header_file_size())
1783 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00001784
Douglas Gregor09b69892011-02-10 17:09:37 +00001785 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1786 const FileEntry *File = FilesByUID[UID];
1787 if (!File)
1788 continue;
1789
Richard Smith386bb072015-08-18 23:42:23 +00001790 // Get the file info. This will load info from the external source if
1791 // necessary. Skip emitting this file if we have no information on it
1792 // as a header file (in which case HFI will be null) or if it hasn't
1793 // changed since it was loaded. Also skip it if it's for a modular header
1794 // from a different module; in that case, we rely on the module(s)
1795 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001796 const HeaderFileInfo *HFI =
1797 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1798 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001799 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001800
Richard Smith7ed1bc92014-12-05 22:42:13 +00001801 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001802 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001803 SmallString<128> FilenameTmp(Filename);
1804 if (PreparePathForOutput(FilenameTmp)) {
1805 // If we performed any translation on the file name at all, we need to
1806 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001807 Filename = StringRef(strdup(FilenameTmp.c_str()));
1808 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001809 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001810
Richard Smith040e1262017-06-02 01:55:39 +00001811 HeaderFileInfoTrait::key_type Key = {
1812 Filename, File->getSize(), getTimestampForOutput(File)
1813 };
1814 HeaderFileInfoTrait::data_type Data = {
1815 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
1816 };
1817 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001818 ++NumHeaderSearchEntries;
1819 }
Richard Smith040e1262017-06-02 01:55:39 +00001820
Douglas Gregor09b69892011-02-10 17:09:37 +00001821 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001822 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001823 uint32_t BucketOffset;
1824 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001825 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001826
Douglas Gregor09b69892011-02-10 17:09:37 +00001827 llvm::raw_svector_ostream Out(TableData);
1828 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00001829 endian::write<uint32_t>(Out, 0, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00001830 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1831 }
1832
1833 // Create a blob abbreviation
1834 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001835
David Blaikieb44f0bf2017-01-04 22:36:43 +00001836 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00001837 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00001842 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001843
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001844 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001845 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1846 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001847 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001848 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001849
Douglas Gregor09b69892011-02-10 17:09:37 +00001850 // Free all of the strings we had to duplicate.
1851 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001852 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001853}
1854
George Rimarc39f5492017-01-17 15:45:31 +00001855static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
1856 unsigned SLocBufferBlobCompressedAbbrv,
1857 unsigned SLocBufferBlobAbbrv) {
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001858 using RecordDataType = ASTWriter::RecordData::value_type;
George Rimarc39f5492017-01-17 15:45:31 +00001859
1860 // Compress the buffer if possible. We expect that almost all PCM
1861 // consumers will not want its contents.
1862 SmallString<0> CompressedBuffer;
1863 if (llvm::zlib::isAvailable()) {
1864 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
1865 if (!E) {
1866 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
1867 Blob.size() - 1};
1868 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
1869 CompressedBuffer);
1870 return;
1871 }
1872 llvm::consumeError(std::move(E));
1873 }
1874
1875 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
1876 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
1877}
1878
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001879/// Writes the block containing the serialized form of the
Douglas Gregora7f71a92009-04-10 03:52:48 +00001880/// source manager.
1881///
1882/// TODO: We should probably use an on-disk hash table (stored in a
1883/// blob), indexed based on the file name, so that we only create
1884/// entries for files that we actually need. In the common case (no
1885/// errors), we probably won't have to create file entries for any of
1886/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001887void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001888 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001889 RecordData Record;
1890
Chris Lattner0910e3b2009-04-10 17:16:57 +00001891 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00001892 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001893
1894 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001895 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1896 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00001897 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
1898 unsigned SLocBufferBlobCompressedAbbrv =
1899 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001900 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001901
Douglas Gregor258ae542009-04-27 06:38:32 +00001902 // Write out the source location entry table. We skip the first
1903 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001904 std::vector<uint32_t> SLocEntryOffsets;
Dmitry Polukhina7afb212020-04-16 09:24:46 -07001905 uint64_t SLocEntryOffsetsBase = Stream.GetCurrentBitNo();
Douglas Gregor258ae542009-04-27 06:38:32 +00001906 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001907 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1908 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001909 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001910 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001911 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001912 FileID FID = FileID::get(I);
1913 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001914
Douglas Gregor258ae542009-04-27 06:38:32 +00001915 // Record the offset of this source-location entry.
Dmitry Polukhina7afb212020-04-16 09:24:46 -07001916 uint64_t Offset = Stream.GetCurrentBitNo() - SLocEntryOffsetsBase;
1917 assert((Offset >> 32) == 0 && "SLocEntry offset too large");
1918 SLocEntryOffsets.push_back(Offset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001919
1920 // Figure out which record code to use.
1921 unsigned Code;
1922 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001923 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1924 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001925 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001926 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001927 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001928 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001929 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001930 Record.clear();
1931 Record.push_back(Code);
1932
Douglas Gregor925296b2011-07-19 16:10:42 +00001933 // Starting offset of this entry within this module, so skip the dummy.
1934 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001935 if (SLoc->isFile()) {
1936 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00001937 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001938 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1939 Record.push_back(File.hasLineDirectives());
1940
1941 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00001942 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001943 if (Content->OrigEntry) {
1944 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001945 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001946
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001947 // The source location entry is a file. Emit input file ID.
1948 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1949 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001950
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001951 Record.push_back(File.NumCreatedFIDs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001952
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001953 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001954 if (FDI != FileDeclIDs.end()) {
1955 Record.push_back(FDI->second->FirstDeclIndex);
1956 Record.push_back(FDI->second->DeclIDs.size());
1957 } else {
1958 Record.push_back(0);
1959 Record.push_back(0);
1960 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001961
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001962 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001963
Richard Smithaada85c2016-02-06 02:06:43 +00001964 if (Content->BufferOverridden || Content->IsTransient)
1965 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00001966 } else {
1967 // The source location entry is a buffer. The blob associated
1968 // with this entry contains the contents of the buffer.
1969
1970 // We add one to the size so that we capture the trailing NULL
1971 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1972 // the reader side).
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001973 const llvm::MemoryBuffer *Buffer =
1974 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00001975 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001976 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00001977 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00001978 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00001979
Mehdi Amini99d1b292016-10-01 16:38:28 +00001980 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00001981 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001982 }
Richard Smithaada85c2016-02-06 02:06:43 +00001983
1984 if (EmitBlob) {
1985 // Include the implicit terminating null character in the on-disk buffer
1986 // if we're writing it uncompressed.
1987 const llvm::MemoryBuffer *Buffer =
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001988 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Richard Smithaada85c2016-02-06 02:06:43 +00001989 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00001990 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
1991 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00001992 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001993 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001994 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001995 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00001996 AddSourceLocation(Expansion.getSpellingLoc(), Record);
1997 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
1998 AddSourceLocation(Expansion.isMacroArgExpansion()
1999 ? SourceLocation()
2000 : Expansion.getExpansionLocEnd(),
2001 Record);
Richard Smithb5f81712018-04-30 05:25:48 +00002002 Record.push_back(Expansion.isExpansionTokenRange());
Douglas Gregor258ae542009-04-27 06:38:32 +00002003
2004 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002005 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002006 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002007 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002008 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002009 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002010 }
2011 }
2012
Douglas Gregor8f45df52009-04-16 22:23:12 +00002013 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002014
2015 if (SLocEntryOffsets.empty())
2016 return;
2017
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002018 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002019 // table is used for lazily loading source-location information.
2020 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002021
David Blaikieb44f0bf2017-01-04 22:36:43 +00002022 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002023 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
Douglas Gregor258ae542009-04-27 06:38:32 +00002027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002028 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002029 {
2030 RecordData::value_type Record[] = {
2031 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002032 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */,
2033 SLocEntryOffsetsBase};
Mehdi Amini57a41912015-09-10 01:46:39 +00002034 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2035 bytes(SLocEntryOffsets));
2036 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002037 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002038 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002039 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002040
2041 // Write the line table. It depends on remapping working, so it must come
2042 // after the source location offsets.
2043 if (SourceMgr.hasLineTable()) {
2044 LineTableInfo &LineTable = SourceMgr.getLineTable();
2045
2046 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002047
2048 // Emit the needed file names.
2049 llvm::DenseMap<int, int> FilenameMap;
Hans Wennborg14487362017-12-04 22:28:45 +00002050 FilenameMap[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00002051 for (const auto &L : LineTable) {
2052 if (L.first.ID < 0)
2053 continue;
2054 for (auto &LE : L.second) {
2055 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
Hans Wennborg14487362017-12-04 22:28:45 +00002056 FilenameMap.size() - 1)).second)
Richard Smith63078492015-09-01 07:41:55 +00002057 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2058 }
2059 }
2060 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002061
2062 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002063 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002064 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002065 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002066 continue;
2067
2068 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002069 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002070
2071 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002072 Record.push_back(L.second.size());
2073 for (const auto &LE : L.second) {
2074 Record.push_back(LE.FileOffset);
2075 Record.push_back(LE.LineNo);
2076 Record.push_back(FilenameMap[LE.FilenameID]);
2077 Record.push_back((unsigned)LE.FileKind);
2078 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002079 }
2080 }
Richard Smith63078492015-09-01 07:41:55 +00002081
Douglas Gregor925296b2011-07-19 16:10:42 +00002082 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2083 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002084}
2085
Douglas Gregorc5046832009-04-27 18:38:38 +00002086//===----------------------------------------------------------------------===//
2087// Preprocessor Serialization
2088//===----------------------------------------------------------------------===//
2089
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002090static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2091 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002092 if (MacroInfo *MI = MD->getMacroInfo())
2093 if (MI->isBuiltinMacro())
2094 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002095
2096 if (IsModule) {
2097 SourceLocation Loc = MD->getLocation();
2098 if (Loc.isInvalid())
2099 return true;
2100 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2101 return true;
2102 }
2103
2104 return false;
2105}
2106
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002107/// Writes the block containing the serialized form of the
Chris Lattnereeffaef2009-04-10 17:15:23 +00002108/// preprocessor.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002109void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002110 uint64_t MacroOffsetsBase = Stream.GetCurrentBitNo();
2111
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002112 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2113 if (PPRec)
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002114 WritePreprocessorDetail(*PPRec, MacroOffsetsBase);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002115
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002116 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002117 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002118
Chris Lattner0af3ba12009-04-13 01:29:17 +00002119 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2120 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002121 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002122 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002123 }
2124
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002125 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2126 assert(!IsModule);
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00002127 auto SkipInfo = PP.getPreambleSkipInfo();
2128 if (SkipInfo.hasValue()) {
2129 Record.push_back(true);
2130 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2131 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2132 Record.push_back(SkipInfo->FoundNonSkipPortion);
2133 Record.push_back(SkipInfo->FoundElse);
2134 AddSourceLocation(SkipInfo->ElseLoc, Record);
2135 } else {
2136 Record.push_back(false);
2137 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002138 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2139 AddSourceLocation(Cond.IfLoc, Record);
2140 Record.push_back(Cond.WasSkipping);
2141 Record.push_back(Cond.FoundNonSkip);
2142 Record.push_back(Cond.FoundElse);
2143 }
2144 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2145 Record.clear();
2146 }
2147
Douglas Gregoreda6a892009-04-26 00:07:37 +00002148 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002149 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002150
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002151 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002152 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002153 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002154 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002155
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002156 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002157 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002158
Richard Smithee977932015-05-01 21:22:17 +00002159 // Construct the list of identifiers with macro directives that need to be
2160 // serialized.
2161 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2162 for (auto &Id : PP.getIdentifierTable())
2163 if (Id.second->hadMacroDefinition() &&
2164 (!Id.second->isFromAST() ||
2165 Id.second->hasChangedSinceDeserialization()))
2166 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002167 // Sort the set of macro definitions that need to be serialized by the
2168 // name of the macro, to provide a stable ordering.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00002169 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002170
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002171 // Emit the macro directives as a list and associate the offset with the
2172 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002173 for (const IdentifierInfo *Name : MacroIdentifiers) {
2174 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002175 uint64_t StartOffset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2176 assert((StartOffset >> 32) == 0 && "Macro identifiers offset too large");
Richard Smithd7329392015-04-21 21:46:32 +00002177
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002178 // Emit the macro directives in reverse source order.
2179 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002180 // Once we hit an ignored macro, we're done: the rest of the chain
2181 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002182 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002183 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002184
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002185 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002186 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002187 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002188 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002189 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002190 Record.push_back(VisMD->isPublic());
2191 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002192 }
Richard Smithd7329392015-04-21 21:46:32 +00002193
Richard Smithb8b2ed62015-04-23 18:18:26 +00002194 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002195 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002196 // We write out exported module macros for PCH as well.
2197 auto Leafs = PP.getLeafModuleMacros(Name);
2198 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2199 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2200 while (!Worklist.empty()) {
2201 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002202
Manman Ren33d80292016-05-31 18:19:32 +00002203 // Emit a record indicating this submodule exports this macro.
2204 ModuleMacroRecord.push_back(
2205 getSubmoduleID(Macro->getOwningModule()));
2206 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2207 for (auto *M : Macro->overrides())
2208 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002209
Manman Ren33d80292016-05-31 18:19:32 +00002210 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2211 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002212
Manman Ren33d80292016-05-31 18:19:32 +00002213 // Enqueue overridden macros once we've visited all their ancestors.
2214 for (auto *M : Macro->overrides())
2215 if (++Visits[M] == M->getNumOverridingMacros())
2216 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002217
Manman Ren33d80292016-05-31 18:19:32 +00002218 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002219 }
Richard Smithd7329392015-04-21 21:46:32 +00002220
Richard Smithee977932015-05-01 21:22:17 +00002221 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002222 continue;
2223
Richard Smithd7329392015-04-21 21:46:32 +00002224 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002225 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2226 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002227 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002229 /// Offsets of each of the macros into the bitstream, indexed by
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002230 /// the local macro ID
2231 ///
2232 /// For each identifier that is associated with a macro, this map
2233 /// provides the offset into the bitstream where that macro is
2234 /// defined.
2235 std::vector<uint32_t> MacroOffsets;
2236
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002237 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2238 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2239 MacroInfo *MI = MacroInfosToEmit[I].MI;
2240 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002241
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002242 if (ID < FirstMacroID) {
2243 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2244 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002245 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002246
2247 // Record the local offset of this macro.
2248 unsigned Index = ID - FirstMacroID;
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002249 if (Index >= MacroOffsets.size())
2250 MacroOffsets.resize(Index + 1);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002251
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002252 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2253 assert((Offset >> 32) == 0 && "Macro offset too large");
2254 MacroOffsets[Index] = Offset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002255
2256 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002257 AddSourceLocation(MI->getDefinitionLoc(), Record);
2258 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2259 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002260 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002261 unsigned Code;
2262 if (MI->isObjectLike()) {
2263 Code = PP_MACRO_OBJECT_LIKE;
2264 } else {
2265 Code = PP_MACRO_FUNCTION_LIKE;
2266
2267 Record.push_back(MI->isC99Varargs());
2268 Record.push_back(MI->isGNUVarargs());
2269 Record.push_back(MI->hasCommaPasting());
Faisal Valiac506d72017-07-17 17:18:43 +00002270 Record.push_back(MI->getNumParams());
2271 for (const IdentifierInfo *Param : MI->params())
2272 AddIdentifierRef(Param, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002273 }
2274
2275 // If we have a detailed preprocessing record, record the macro definition
2276 // ID that corresponds to this macro.
2277 if (PPRec)
2278 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2279
2280 Stream.EmitRecord(Code, Record);
2281 Record.clear();
2282
2283 // Emit the tokens array.
2284 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2285 // Note that we know that the preprocessor does not have any annotation
2286 // tokens in it because they are created by the parser, and thus can't
2287 // be in a macro definition.
2288 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002289 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002290 Stream.EmitRecord(PP_TOKEN, Record);
2291 Record.clear();
2292 }
2293 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002294 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002295
Douglas Gregor92a96f52011-02-08 21:58:10 +00002296 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002297
2298 // Write the offsets table for macro IDs.
2299 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002300
David Blaikieb44f0bf2017-01-04 22:36:43 +00002301 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002302 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // base offset
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2307
David Blaikieb44f0bf2017-01-04 22:36:43 +00002308 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002309 {
2310 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002311 FirstMacroID - NUM_PREDEF_MACRO_IDS,
2312 MacroOffsetsBase};
Mehdi Amini57a41912015-09-10 01:46:39 +00002313 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2314 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002315}
2316
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002317void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
2318 uint64_t MacroOffsetsBase) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002319 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002320 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002321
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002322 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002323
Douglas Gregor92a96f52011-02-08 21:58:10 +00002324 // Enter the preprocessor block.
2325 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002326
Douglas Gregoraae92242010-03-19 21:51:54 +00002327 // If the preprocessor has a preprocessing record, emit it.
2328 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002329 using namespace llvm;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002330
2331 // Set up the abbreviation for
Douglas Gregor92a96f52011-02-08 21:58:10 +00002332 unsigned InclusionAbbrev = 0;
2333 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002334 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002335 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002341 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002342 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002343
2344 unsigned FirstPreprocessorEntityID
2345 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002346 + NUM_PREDEF_PP_ENTITY_IDS;
2347 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002348 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002349 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2350 EEnd = PPRec.local_end();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002351 E != EEnd;
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002352 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002353 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002354
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002355 uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
2356 assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
Richard Smith66a81862015-05-04 02:25:31 +00002357 PreprocessedEntityOffsets.push_back(
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002358 PPEntityOffset((*E)->getSourceRange(), Offset));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002359
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002360 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002361 // Record this macro definition's ID.
2362 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002363
Douglas Gregor92a96f52011-02-08 21:58:10 +00002364 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002365 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2366 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002367 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002368
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002369 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002370 Record.push_back(ME->isBuiltinMacro());
2371 if (ME->isBuiltinMacro())
2372 AddIdentifierRef(ME->getName(), Record);
2373 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002374 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002375 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002376 continue;
2377 }
2378
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002379 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002380 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002381 Record.push_back(ID->getFileName().size());
2382 Record.push_back(ID->wasInQuotes());
2383 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002384 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002385 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002386 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002387 // Check that the FileEntry is not null because it was not resolved and
2388 // we create a PCH even with compiler errors.
2389 if (ID->getFile())
2390 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002391 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002392 continue;
2393 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002394
Douglas Gregor92a96f52011-02-08 21:58:10 +00002395 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2396 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002397 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002398
Douglas Gregoraae92242010-03-19 21:51:54 +00002399 // Write the offsets table for the preprocessing record.
2400 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002401 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2402
Douglas Gregoraae92242010-03-19 21:51:54 +00002403 // Write the offsets table for identifier IDs.
2404 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002405
David Blaikieb44f0bf2017-01-04 22:36:43 +00002406 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002407 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002410 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002411
Mehdi Amini57a41912015-09-10 01:46:39 +00002412 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2413 FirstPreprocessorEntityID -
2414 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002415 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002416 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002417 }
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00002418
2419 // Write the skipped region table for the preprocessing record.
2420 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2421 if (SkippedRanges.size() > 0) {
2422 std::vector<PPSkippedRange> SerializedSkippedRanges;
2423 SerializedSkippedRanges.reserve(SkippedRanges.size());
2424 for (auto const& Range : SkippedRanges)
2425 SerializedSkippedRanges.emplace_back(Range);
2426
2427 using namespace llvm;
2428 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2429 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2431 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2432
2433 Record.clear();
2434 Record.push_back(PPD_SKIPPED_RANGES);
2435 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2436 bytes(SerializedSkippedRanges));
2437 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002438}
2439
Richard Smith386bb072015-08-18 23:42:23 +00002440unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002441 if (!Mod)
2442 return 0;
2443
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002444 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2445 if (Known != SubmoduleIDs.end())
2446 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002447
Richard Smithdc1f0422016-07-20 19:10:16 +00002448 auto *Top = Mod->getTopLevelModule();
2449 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002450 (getLangOpts().CompilingPCH ||
2451 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002452 return 0;
2453
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002454 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2455}
2456
Richard Smith386bb072015-08-18 23:42:23 +00002457unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2458 // FIXME: This can easily happen, if we have a reference to a submodule that
2459 // did not result in us loading a module file for that submodule. For
2460 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2461 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2462 assert((ID || !Mod) &&
2463 "asked for module ID for non-local, non-imported module");
2464 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002465}
2466
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002467/// Compute the number of modules within the given tree (including the
Douglas Gregor253eefe2011-12-01 00:59:36 +00002468/// given module).
2469static unsigned getNumberOfModules(Module *Mod) {
2470 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002471 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002472 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002473 ChildModules += getNumberOfModules(*Sub);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002474
Douglas Gregor253eefe2011-12-01 00:59:36 +00002475 return ChildModules + 1;
2476}
2477
Douglas Gregorde3ef502011-11-30 23:21:26 +00002478void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002479 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002480 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002481
Douglas Gregor69021972011-11-30 17:33:56 +00002482 // Write the abbreviations needed for the submodules block.
2483 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002484
David Blaikieb44f0bf2017-01-04 22:36:43 +00002485 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002486 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002487 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
Richard Smithdd8b5332017-09-04 05:37:53 +00002489 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
Douglas Gregor69021972011-11-30 17:33:56 +00002490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2491 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2493 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
Douglas Gregor69021972011-11-30 17:33:56 +00002499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002500 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002501
David Blaikieb44f0bf2017-01-04 22:36:43 +00002502 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002503 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002505 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002506
David Blaikieb44f0bf2017-01-04 22:36:43 +00002507 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002508 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002510 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002511
David Blaikieb44f0bf2017-01-04 22:36:43 +00002512 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002513 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002515 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002516
David Blaikieb44f0bf2017-01-04 22:36:43 +00002517 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002518 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002520 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002521
David Blaikieb44f0bf2017-01-04 22:36:43 +00002522 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002523 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002524 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002526 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002527
David Blaikieb44f0bf2017-01-04 22:36:43 +00002528 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002529 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002531 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002532
David Blaikieb44f0bf2017-01-04 22:36:43 +00002533 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002534 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002536 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002537
David Blaikieb44f0bf2017-01-04 22:36:43 +00002538 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002539 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002541 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002542
David Blaikieb44f0bf2017-01-04 22:36:43 +00002543 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002544 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002546 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002547
David Blaikieb44f0bf2017-01-04 22:36:43 +00002548 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002549 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002552 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002553
David Blaikieb44f0bf2017-01-04 22:36:43 +00002554 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002555 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002557 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002558
David Blaikieb44f0bf2017-01-04 22:36:43 +00002559 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002560 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002563 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002564
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002565 Abbrev = std::make_shared<BitCodeAbbrev>();
2566 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2568 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2569
Douglas Gregor253eefe2011-12-01 00:59:36 +00002570 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002571 RecordData::value_type Record[] = {
2572 getNumberOfModules(WritingModule),
Richard Smithdd8b5332017-09-04 05:37:53 +00002573 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002574 Stream.EmitRecord(SUBMODULE_METADATA, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002575
Douglas Gregor69021972011-11-30 17:33:56 +00002576 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002577 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002578 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002579 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002580 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002581 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002582 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002583
2584 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002585 if (Mod->Parent) {
2586 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002587 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002588 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002589
2590 // Emit the definition of the block.
2591 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002592 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2593 ID,
2594 ParentID,
Simon Pilgrimf331a662017-09-04 10:54:39 +00002595 (RecordData::value_type)Mod->Kind,
David Blaikie9ffe5a32017-01-30 05:00:26 +00002596 Mod->IsFramework,
2597 Mod->IsExplicit,
2598 Mod->IsSystem,
2599 Mod->IsExternC,
2600 Mod->InferSubmodules,
2601 Mod->InferExplicitSubmodules,
2602 Mod->InferExportWildcard,
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002603 Mod->ConfigMacrosExhaustive,
2604 Mod->ModuleMapIsPrivate};
Mehdi Amini57a41912015-09-10 01:46:39 +00002605 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2606 }
2607
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002608 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002609 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002610 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002611 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002612 }
2613
Douglas Gregor69021972011-11-30 17:33:56 +00002614 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002615 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002616 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002617 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2618 UmbrellaHeader.NameAsWritten);
2619 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002620 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2621 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002622 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002623 }
Richard Smith306d8922014-10-22 23:50:56 +00002624
Douglas Gregor69021972011-11-30 17:33:56 +00002625 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002626 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002627 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002628 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002629 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002630 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002631 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2632 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2633 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002634 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002635 Module::HK_PrivateTextual},
2636 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002637 };
2638 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002639 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002640 for (auto &H : Mod->Headers[HL.HeaderKind])
2641 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2642 }
2643
2644 // Emit the top headers.
2645 {
2646 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002647 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002648 for (auto *H : TopHeaders)
2649 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002650 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002651
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002652 // Emit the imports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002653 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002654 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002655 for (auto *I : Mod->Imports)
2656 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002657 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2658 }
2659
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002660 // Emit the exports.
Douglas Gregor24bb9232011-12-02 18:58:38 +00002661 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002662 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002663 for (const auto &E : Mod->Exports) {
2664 // FIXME: This may fail; we don't require that all exported modules
2665 // are local or imported.
2666 Record.push_back(getSubmoduleID(E.getPointer()));
2667 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002668 }
2669 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2670 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002671
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002672 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2673 // Might be unnecessary as use declarations are only used to build the
2674 // module itself.
2675
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002676 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002677 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002678 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2679 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002680 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002681 }
2682
Douglas Gregorfb912652013-03-20 21:10:35 +00002683 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002684 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002685 // FIXME: This may fail; we don't require that all conflicting modules
2686 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002687 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2688 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002689 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002690 }
2691
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002692 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002693 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002694 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002695 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002696 }
2697
Richard Smithdc1f0422016-07-20 19:10:16 +00002698 // Emit the initializers, if any.
2699 RecordData Inits;
2700 for (Decl *D : Context->getModuleInitializers(Mod))
2701 Inits.push_back(GetDeclRef(D));
2702 if (!Inits.empty())
2703 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2704
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002705 // Emit the name of the re-exported module, if any.
2706 if (!Mod->ExportAsModule.empty()) {
2707 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
2708 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
2709 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002710
Douglas Gregor69021972011-11-30 17:33:56 +00002711 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002712 for (auto *M : Mod->submodules())
2713 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002714 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002715
Douglas Gregor69021972011-11-30 17:33:56 +00002716 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002717
Richard Smith23d8d032015-05-14 00:45:20 +00002718 assert((NextSubmoduleID - FirstSubmoduleID ==
2719 getNumberOfModules(WritingModule)) &&
2720 "Wrong # of submodules; found a reference to a non-local, "
2721 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002722}
2723
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002724void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2725 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002726 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2727 DiagStateIDMap;
2728 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002729 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002730
Richard Smithe37391c2017-05-03 00:28:49 +00002731 auto EncodeDiagStateFlags =
2732 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
2733 unsigned Result = (unsigned)DS->ExtBehavior;
2734 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00002735 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
2736 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
2737 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00002738 Result = (Result << 1) | Val;
2739 return Result;
2740 };
2741
2742 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
2743 Record.push_back(Flags);
2744
Richard Smithd230de22017-01-26 01:01:01 +00002745 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2746 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00002747 // Ensure that the diagnostic state wasn't modified since it was created.
2748 // We will not correctly round-trip this information otherwise.
2749 assert(Flags == EncodeDiagStateFlags(State) &&
2750 "diag state flags vary in single AST file");
2751
Richard Smithd230de22017-01-26 01:01:01 +00002752 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002753 Record.push_back(DiagStateID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002754
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002755 if (DiagStateID == 0) {
2756 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002757
2758 // Add a placeholder for the number of mappings.
2759 auto SizeIdx = Record.size();
2760 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00002761 for (const auto &I : *State) {
2762 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002763 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00002764 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002765 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002766 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002767 // Update the placeholder.
2768 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00002769 }
2770 };
2771
2772 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00002773
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002774 // Reserve a spot for the number of locations with state transitions.
2775 auto NumLocationsIdx = Record.size();
2776 Record.emplace_back();
2777
2778 // Emit the state transitions.
2779 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00002780 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2781 if (!FileIDAndFile.first.isValid() ||
2782 !FileIDAndFile.second.HasLocalTransitions)
2783 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002784 ++NumLocations;
Richard Smith6c2b5a82018-02-09 01:15:13 +00002785
2786 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
2787 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
2788 AddSourceLocation(Loc, Record);
2789
Richard Smithd230de22017-01-26 01:01:01 +00002790 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2791 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2792 Record.push_back(StatePoint.Offset);
2793 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002794 }
2795 }
2796
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002797 // Backpatch the number of locations.
2798 Record[NumLocationsIdx] = NumLocations;
2799
2800 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00002801 //
2802 // This also protects against a hypothetical corner case with simulating
2803 // -Werror settings for implicit modules in the ASTReader, where reading
2804 // CurDiagState out of context could change whether warning pragmas are
2805 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002806 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2807 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2808
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00002809 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002810}
2811
Douglas Gregorc5046832009-04-27 18:38:38 +00002812//===----------------------------------------------------------------------===//
2813// Type Serialization
2814//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002815
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002816/// Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002817void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002818 TypeIdx &IdxRef = TypeIdxs[T];
2819 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2820 IdxRef = TypeIdx(NextTypeID++);
2821 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002822
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002823 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002824
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002825 // Emit the type's representation.
John McCalld505e572019-12-13 21:54:44 -05002826 uint64_t Offset = ASTTypeWriter(*this).write(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002827
Richard Smith290d8012016-04-06 17:06:00 +00002828 // Record the offset for this type.
2829 unsigned Index = Idx.getIndex() - FirstTypeID;
2830 if (TypeOffsets.size() == Index)
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002831 TypeOffsets.emplace_back(Offset);
Richard Smith290d8012016-04-06 17:06:00 +00002832 else if (TypeOffsets.size() < Index) {
2833 TypeOffsets.resize(Index + 1);
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002834 TypeOffsets[Index].setBitOffset(Offset);
John McCall8ccfcb52009-09-24 19:53:00 +00002835 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002836 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002837 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002838}
2839
Douglas Gregorc5046832009-04-27 18:38:38 +00002840//===----------------------------------------------------------------------===//
2841// Declaration Serialization
2842//===----------------------------------------------------------------------===//
2843
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002844/// Write the block containing all of the declaration IDs
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002845/// lexically declared within the given DeclContext.
2846///
2847/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002848/// bitstream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002849uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002850 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002851 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002852 return 0;
2853
Douglas Gregor8f45df52009-04-16 22:23:12 +00002854 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002855 SmallVector<uint32_t, 128> KindDeclPairs;
2856 for (const auto *D : DC->decls()) {
2857 KindDeclPairs.push_back(D->getKind());
2858 KindDeclPairs.push_back(GetDeclRef(D));
2859 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002860
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002861 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002862 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002863 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2864 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002865 return Offset;
2866}
2867
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002868void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002869 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002870
2871 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002872 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002873 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002877 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002878 {
2879 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2880 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2881 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2882 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002883
2884 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002885 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002886 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002890 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002891 {
2892 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2893 FirstDeclID - NUM_PREDEF_DECL_IDS};
2894 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2895 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002896}
2897
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002898void ASTWriter::WriteFileDeclIDsMap() {
2899 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002900
Chandler Carruthb306d732015-03-27 00:31:20 +00002901 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2902 FileDeclIDs.begin(), FileDeclIDs.end());
Fangrui Song55fab262018-09-26 22:16:28 +00002903 llvm::sort(SortedFileDeclIDs, llvm::less_first());
Chandler Carruthb306d732015-03-27 00:31:20 +00002904
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002905 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002906 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2907 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2908 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2909 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2910 for (auto &LocDeclEntry : Info.DeclIDs)
2911 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002912 }
2913
David Blaikieb44f0bf2017-01-04 22:36:43 +00002914 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002915 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002918 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002919 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2920 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002921 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002922}
2923
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002924void ASTWriter::WriteComments() {
2925 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Ilya Biryukov45643102018-07-09 11:33:23 +00002926 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
2927 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
2928 return;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002929 RecordData Record;
Jan Korousf31d8df2019-08-13 18:11:44 +00002930 for (const auto &FO : Context->Comments.OrderedComments) {
2931 for (const auto &OC : FO.second) {
2932 const RawComment *I = OC.second;
2933 Record.clear();
2934 AddSourceRange(I->getSourceRange(), Record);
2935 Record.push_back(I->getKind());
2936 Record.push_back(I->isTrailingComment());
2937 Record.push_back(I->isAlmostTrailingComment());
2938 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2939 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002940 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002941}
2942
Douglas Gregorc5046832009-04-27 18:38:38 +00002943//===----------------------------------------------------------------------===//
2944// Global Method Pool and Selector Serialization
2945//===----------------------------------------------------------------------===//
2946
Douglas Gregore84a9da2009-04-20 20:36:09 +00002947namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002948
Douglas Gregorc78d3462009-04-24 21:10:55 +00002949// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002950class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002951 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002952
2953public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002954 using key_type = Selector;
2955 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00002956
Sebastian Redl834bb972010-08-04 17:20:04 +00002957 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002958 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002959 ObjCMethodList Instance, Factory;
2960 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002961 using data_type_ref = const data_type &;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002962
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002963 using hash_value_type = unsigned;
2964 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00002965
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002966 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
Mike Stump11289f42009-09-09 15:08:12 +00002967
Justin Bogner25463f12014-04-18 20:27:24 +00002968 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002969 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002970 }
Mike Stump11289f42009-09-09 15:08:12 +00002971
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002972 std::pair<unsigned, unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002973 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002974 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002975 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002976
Peter Collingbournee3f65292018-05-18 19:46:24 +00002977 endian::Writer LE(Out, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002978 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002979 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002980 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2981 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002982 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002983 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002984 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002985 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002986 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002987 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002988 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002989 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002990 return std::make_pair(KeyLen, DataLen);
2991 }
Mike Stump11289f42009-09-09 15:08:12 +00002992
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002993 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002994 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002995
Peter Collingbournee3f65292018-05-18 19:46:24 +00002996 endian::Writer LE(Out, little);
Mike Stump11289f42009-09-09 15:08:12 +00002997 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002998 assert((Start >> 32) == 0 && "Selector key offset too large");
2999 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003000 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003001 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003002 if (N == 0)
3003 N = 1;
3004 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003005 LE.write<uint32_t>(
3006 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003007 }
Mike Stump11289f42009-09-09 15:08:12 +00003008
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003009 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003010 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003011 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003012
Peter Collingbournee3f65292018-05-18 19:46:24 +00003013 endian::Writer LE(Out, little);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003014 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003015 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003016 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003017 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003018 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003019 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003020 ++NumInstanceMethods;
3021
3022 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003023 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003024 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003025 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003026 ++NumFactoryMethods;
3027
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003028 unsigned InstanceBits = Methods.Instance.getBits();
3029 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003030 unsigned InstanceHasMoreThanOneDeclBit =
3031 Methods.Instance.hasMoreThanOneDecl();
3032 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3033 (InstanceHasMoreThanOneDeclBit << 2) |
3034 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003035 unsigned FactoryBits = Methods.Factory.getBits();
3036 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003037 unsigned FactoryHasMoreThanOneDeclBit =
3038 Methods.Factory.hasMoreThanOneDecl();
3039 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3040 (FactoryHasMoreThanOneDeclBit << 2) |
3041 FactoryBits;
3042 LE.write<uint16_t>(FullInstanceBits);
3043 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003044 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003045 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003046 if (Method->getMethod())
3047 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003048 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003049 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003050 if (Method->getMethod())
3051 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003052
3053 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003054 }
3055};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003056
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003057} // namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +00003058
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003059/// Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003060///
3061/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003062/// in an on-disk hash table indexed by the selector. The hash table also
3063/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003064void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003065 using namespace llvm;
3066
Sebastian Redla19a67f2010-08-03 21:58:15 +00003067 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003068 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003069 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003070 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003071 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003072 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003073 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003074 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003075
Sebastian Redla19a67f2010-08-03 21:58:15 +00003076 // Create the on-disk hash table representation. We walk through every
3077 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003078 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003079 for (auto &SelectorAndID : SelectorIDs) {
3080 Selector S = SelectorAndID.first;
3081 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003082 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003083 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003084 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003085 ObjCMethodList(),
3086 ObjCMethodList()
3087 };
3088 if (F != SemaRef.MethodPool.end()) {
3089 Data.Instance = F->second.first;
3090 Data.Factory = F->second.second;
3091 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003092 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003093 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003094 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003095 // Selector already exists. Did it change?
3096 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003097 for (ObjCMethodList *M = &Data.Instance;
3098 !changed && M && M->getMethod(); M = M->getNext()) {
3099 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003100 changed = true;
3101 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003102 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003103 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003104 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003105 changed = true;
3106 }
3107 if (!changed)
3108 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003109 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003110 // A new method pool entry.
3111 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003112 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003113 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003114 }
3115
Douglas Gregorc78d3462009-04-24 21:10:55 +00003116 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003117 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003118 uint32_t BucketOffset;
3119 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003120 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003122 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003123 llvm::raw_svector_ostream Out(MethodPool);
3124 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003125 endian::write<uint32_t>(Out, 0, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003126 BucketOffset = Generator.Emit(Out, Trait);
3127 }
3128
3129 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003130 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003131 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003135 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003136
Douglas Gregor95c13f52009-04-25 17:48:32 +00003137 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003138 {
3139 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3140 NumTableEntries};
3141 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3142 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003143
3144 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003145 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003146 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003150 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003151
3152 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003153 {
3154 RecordData::value_type Record[] = {
3155 SELECTOR_OFFSETS, SelectorOffsets.size(),
3156 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3157 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3158 bytes(SelectorOffsets));
3159 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003160 }
3161}
3162
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003163/// Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003164void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003165 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003166
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003167 if (SemaRef.ReferencedSelectors.empty())
3168 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003169
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003170 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003171 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003173 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003174 // very tricky to fix, and given that @selector shouldn't really appear in
3175 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003176 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3177 Selector Sel = SelectorAndLocation.first;
3178 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003179 Writer.AddSelectorRef(Sel);
3180 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003181 }
Richard Smithe64e7452016-04-18 21:54:58 +00003182 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003183}
3184
Douglas Gregorc5046832009-04-27 18:38:38 +00003185//===----------------------------------------------------------------------===//
3186// Identifier Table Serialization
3187//===----------------------------------------------------------------------===//
3188
Richard Smithb3761912015-02-05 23:08:52 +00003189/// Determine the declaration that should be put into the name lookup table to
3190/// represent the given declaration in this module. This is usually D itself,
3191/// but if D was imported and merged into a local declaration, we want the most
3192/// recent local declaration instead. The chosen declaration will be the most
3193/// recent declaration in any module that imports this one.
3194static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3195 NamedDecl *D) {
3196 if (!LangOpts.Modules || !D->isFromASTFile())
3197 return D;
3198
3199 if (Decl *Redecl = D->getPreviousDecl()) {
3200 // For Redeclarable decls, a prior declaration might be local.
3201 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003202 // If we find a local decl, we're done.
3203 if (!Redecl->isFromASTFile()) {
3204 // Exception: in very rare cases (for injected-class-names), not all
3205 // redeclarations are in the same semantic context. Skip ones in a
3206 // different context. They don't go in this lookup table at all.
3207 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3208 D->getDeclContext()->getRedeclContext()))
3209 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003210 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003211 }
3212
Richard Smithfe620d22015-03-05 23:24:12 +00003213 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003214 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003215 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003216 break;
3217 }
3218 } else if (Decl *First = D->getCanonicalDecl()) {
3219 // For Mergeable decls, the first decl might be local.
3220 if (!First->isFromASTFile())
3221 return cast<NamedDecl>(First);
3222 }
3223
3224 // All declarations are imported. Our most recent declaration will also be
3225 // the most recent one in anyone who imports us.
3226 return D;
3227}
3228
Douglas Gregorc78d3462009-04-24 21:10:55 +00003229namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003230
Richard Smithcf97cf62015-04-19 01:34:23 +00003231class ASTIdentifierTableTrait {
3232 ASTWriter &Writer;
3233 Preprocessor &PP;
3234 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003235 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003236 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003237 ASTWriter::RecordData *InterestingIdentifierOffsets;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003238
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003239 /// Determines whether this is an "interesting" identifier that needs a
Richard Smithcf97cf62015-04-19 01:34:23 +00003240 /// full IdentifierInfo structure written into the hash table. Notably, this
3241 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3242 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003243 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003244 if (MacroOffset ||
3245 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003246 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003247 II->hasRevertedTokenIDToIdentifier() ||
Bruno Ricci366ba732018-09-21 12:53:22 +00003248 (NeedDecls && II->getFETokenInfo()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003249 return true;
3250
3251 return false;
3252 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003253
Douglas Gregore84a9da2009-04-20 20:36:09 +00003254public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003255 using key_type = IdentifierInfo *;
3256 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003257
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003258 using data_type = IdentID;
3259 using data_type_ref = data_type;
Mike Stump11289f42009-09-09 15:08:12 +00003260
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003261 using hash_value_type = unsigned;
3262 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003263
Richard Smithd7329392015-04-21 21:46:32 +00003264 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003265 IdentifierResolver &IdResolver, bool IsModule,
3266 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003267 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003268 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3269 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003270
Richard Smithd79514e2016-02-05 19:03:40 +00003271 bool needDecls() const { return NeedDecls; }
3272
Justin Bogner25463f12014-04-18 20:27:24 +00003273 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003274 return llvm::djbHash(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003275 }
Mike Stump11289f42009-09-09 15:08:12 +00003276
Richard Smith33e0f7e2015-07-22 02:08:40 +00003277 bool isInterestingIdentifier(const IdentifierInfo *II) {
3278 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3279 return isInterestingIdentifier(II, MacroOffset);
3280 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003281
Richard Smith9c254182015-07-19 21:41:12 +00003282 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3283 return isInterestingIdentifier(II, 0);
3284 }
3285
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003286 std::pair<unsigned, unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003287 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003288 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003289 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003290 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3291 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003292 DataLen += 2; // 2 bytes for builtin ID
3293 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003294 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003295 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003296
Richard Smitha534a312015-07-21 23:54:07 +00003297 if (NeedDecls) {
3298 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3299 DEnd = IdResolver.end();
3300 D != DEnd; ++D)
3301 DataLen += 4;
3302 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003303 }
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003304
Justin Bognere1c147c2014-03-28 22:03:19 +00003305 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003306
Peter Collingbournee3f65292018-05-18 19:46:24 +00003307 endian::Writer LE(Out, little);
Justin Bognere1c147c2014-03-28 22:03:19 +00003308
Richard Smithdf8a8312015-03-13 04:05:01 +00003309 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003310 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003311 // We emit the key length after the data length so that every
3312 // string is preceded by a 16-bit length. This matches the PTH
3313 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003314 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003315 return std::make_pair(KeyLen, DataLen);
3316 }
Mike Stump11289f42009-09-09 15:08:12 +00003317
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003318 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003319 unsigned KeyLen) {
3320 // Record the location of the key data. This is used when generating
3321 // the mapping from persistent IDs to strings.
3322 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003323
3324 // Emit the offset of the key/data length information to the interesting
3325 // identifiers table if necessary.
3326 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3327 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3328
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003329 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003330 }
Mike Stump11289f42009-09-09 15:08:12 +00003331
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003332 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003333 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003334 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003335
Peter Collingbournee3f65292018-05-18 19:46:24 +00003336 endian::Writer LE(Out, little);
Richard Smithcf97cf62015-04-19 01:34:23 +00003337
Richard Smithd7329392015-04-21 21:46:32 +00003338 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3339 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003340 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003341 return;
3342 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003343
Justin Bognere1c147c2014-03-28 22:03:19 +00003344 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003345 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3346 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003347 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003348 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003349 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003350 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003351 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3352 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003353 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003354 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003355 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003356 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003357
Richard Smithd7329392015-04-21 21:46:32 +00003358 if (HadMacroDefinition)
3359 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003360
Richard Smitha534a312015-07-21 23:54:07 +00003361 if (NeedDecls) {
3362 // Emit the declaration IDs in reverse order, because the
3363 // IdentifierResolver provides the declarations as they would be
3364 // visible (e.g., the function "stat" would come before the struct
3365 // "stat"), but the ASTReader adds declarations to the end of the list
3366 // (so we need to see the struct "stat" before the function "stat").
3367 // Only emit declarations that aren't from a chained PCH, though.
3368 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3369 IdResolver.end());
3370 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3371 DEnd = Decls.rend();
3372 D != DEnd; ++D)
3373 LE.write<uint32_t>(
3374 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3375 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003376 }
3377};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003378
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003379} // namespace
Douglas Gregore84a9da2009-04-20 20:36:09 +00003380
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003381/// Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003382///
3383/// The identifier table consists of a blob containing string data
3384/// (the actual identifiers themselves) and a separate "offsets" index
3385/// that maps identifier IDs to locations within the blob.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003386void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003387 IdentifierResolver &IdResolver,
3388 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003389 using namespace llvm;
3390
Richard Smith33e0f7e2015-07-22 02:08:40 +00003391 RecordData InterestingIdents;
3392
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003393 // Create and write out the blob that contains the identifier
3394 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003395 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003396 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003397 ASTIdentifierTableTrait Trait(
3398 *this, PP, IdResolver, IsModule,
3399 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003400
Douglas Gregore6648fb2009-04-28 20:33:11 +00003401 // Look for any identifiers that were named while processing the
3402 // headers, but are otherwise not needed. We add these to the hash
3403 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003404 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003405 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003406 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003407 for (const auto &ID : PP.getIdentifierTable())
3408 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003409 // Sort the identifiers lexicographically before getting them references so
3410 // that their order is stable.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00003411 llvm::sort(IIs, llvm::deref<std::less<>>());
Chandler Carruth8440c982015-03-26 23:54:15 +00003412 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003413 if (Trait.isInterestingNonMacroIdentifier(II))
3414 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003415
Sebastian Redlff4a2952010-07-23 23:49:55 +00003416 // Create the on-disk hash table representation. We only store offsets
3417 // for identifiers that appear here for the first time.
3418 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003419 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003420 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003421 IdentID ID = IdentIDPair.second;
3422 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003423 // Write out identifiers if either the ID is local or the identifier has
3424 // changed since it was loaded.
3425 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3426 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003427 (Trait.needDecls() &&
3428 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003429 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003430 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003431
Douglas Gregore84a9da2009-04-20 20:36:09 +00003432 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003433 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003434 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003435 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003436 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003437
Douglas Gregore84a9da2009-04-20 20:36:09 +00003438 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003439 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003440 endian::write<uint32_t>(Out, 0, little);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003441 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003442 }
3443
3444 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003445 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003446 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003449 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003450
3451 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003452 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003453 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003454 }
3455
3456 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003457 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003458 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003462 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003463
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003464#ifndef NDEBUG
3465 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3466 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3467#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003468
3469 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3470 IdentifierOffsets.size(),
3471 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003472 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003473 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003474
3475 // In C++, write the list of interesting identifiers (those that are
3476 // defined as macros, poisoned, or similar unusual things).
3477 if (!InterestingIdents.empty())
3478 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003479}
3480
Douglas Gregorc5046832009-04-27 18:38:38 +00003481//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003482// DeclContext's Name Lookup Table Serialization
3483//===----------------------------------------------------------------------===//
3484
3485namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003486
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003487// Trait used for the on-disk hash table used in the method pool.
3488class ASTDeclContextNameLookupTrait {
3489 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003490 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003491
3492public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003493 using key_type = DeclarationNameKey;
3494 using key_type_ref = key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003495
Richard Smithd88a7f12015-09-01 20:35:42 +00003496 /// A start and end index into DeclIDs, representing a sequence of decls.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003497 using data_type = std::pair<unsigned, unsigned>;
3498 using data_type_ref = const data_type &;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003499
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003500 using hash_value_type = unsigned;
3501 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003502
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003503 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003504
Richard Smithd88a7f12015-09-01 20:35:42 +00003505 template<typename Coll>
3506 data_type getData(const Coll &Decls) {
3507 unsigned Start = DeclIDs.size();
3508 for (NamedDecl *D : Decls) {
3509 DeclIDs.push_back(
3510 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3511 }
3512 return std::make_pair(Start, DeclIDs.size());
3513 }
3514
3515 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3516 unsigned Start = DeclIDs.size();
3517 for (auto ID : FromReader)
3518 DeclIDs.push_back(ID);
3519 return std::make_pair(Start, DeclIDs.size());
3520 }
3521
3522 static bool EqualKey(key_type_ref a, key_type_ref b) {
3523 return a == b;
3524 }
3525
Richard Smitha06c7e62015-08-26 23:55:49 +00003526 hash_value_type ComputeHash(DeclarationNameKey Name) {
3527 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003528 }
3529
Richard Smithd88a7f12015-09-01 20:35:42 +00003530 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3531 assert(Writer.hasChain() &&
3532 "have reference to loaded module file but no chain?");
3533
3534 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003535
Peter Collingbournee3f65292018-05-18 19:46:24 +00003536 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
Richard Smithd88a7f12015-09-01 20:35:42 +00003537 }
3538
Richard Smitha06c7e62015-08-26 23:55:49 +00003539 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3540 DeclarationNameKey Name,
3541 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003542 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003543
Peter Collingbournee3f65292018-05-18 19:46:24 +00003544 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003545 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003546 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003547 case DeclarationName::Identifier:
3548 case DeclarationName::ObjCZeroArgSelector:
3549 case DeclarationName::ObjCOneArgSelector:
3550 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003551 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003552 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003553 KeyLen += 4;
3554 break;
3555 case DeclarationName::CXXOperatorName:
3556 KeyLen += 1;
3557 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003558 case DeclarationName::CXXConstructorName:
3559 case DeclarationName::CXXDestructorName:
3560 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003561 case DeclarationName::CXXUsingDirective:
3562 break;
3563 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003564 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003565
Richard Smithf02662d2015-07-30 03:17:16 +00003566 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003567 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3568 assert(uint16_t(DataLen) == DataLen &&
3569 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003570 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003571
3572 return std::make_pair(KeyLen, DataLen);
3573 }
3574
Richard Smitha06c7e62015-08-26 23:55:49 +00003575 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003576 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003577
Peter Collingbournee3f65292018-05-18 19:46:24 +00003578 endian::Writer LE(Out, little);
Richard Smitha06c7e62015-08-26 23:55:49 +00003579 LE.write<uint8_t>(Name.getKind());
3580 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003581 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003582 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003583 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003584 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003585 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003586 case DeclarationName::ObjCZeroArgSelector:
3587 case DeclarationName::ObjCOneArgSelector:
3588 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003589 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003590 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003591 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003592 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003593 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003594 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003595 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003596 case DeclarationName::CXXConstructorName:
3597 case DeclarationName::CXXDestructorName:
3598 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003599 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003600 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003601 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003602
3603 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003604 }
3605
Richard Smitha06c7e62015-08-26 23:55:49 +00003606 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3607 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003608 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003609
Peter Collingbournee3f65292018-05-18 19:46:24 +00003610 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003611 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003612 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3613 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003614 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3615 }
3616};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003617
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003618} // namespace
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003619
Chandler Carruthe972c362015-03-26 03:11:40 +00003620bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3621 DeclContext *DC) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003622 return Result.hasExternalDecls() &&
3623 DC->hasNeedToReconcileExternalVisibleStorage();
Chandler Carruthe972c362015-03-26 03:11:40 +00003624}
3625
3626bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3627 DeclContext *DC) {
3628 for (auto *D : Result.getLookupResult())
3629 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3630 return false;
3631
3632 return true;
3633}
3634
Richard Smithd88a7f12015-09-01 20:35:42 +00003635void
Chandler Carruthe972c362015-03-26 03:11:40 +00003636ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003637 llvm::SmallVectorImpl<char> &LookupTable) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003638 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3639 !ConstDC->hasLazyExternalLexicalLookups() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003640 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003641
Chandler Carruthe972c362015-03-26 03:11:40 +00003642 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003643 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003644 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3645
3646 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003647 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3648 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003649 ASTDeclContextNameLookupTrait Trait(*this);
3650
Chandler Carruthe972c362015-03-26 03:11:40 +00003651 // The first step is to collect the declaration names which we need to
3652 // serialize into the name lookup table, and to collect them in a stable
3653 // order.
3654 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003655
Chandler Carruthe972c362015-03-26 03:11:40 +00003656 // We also build up small sets of the constructor and conversion function
3657 // names which are visible.
3658 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003659
Chandler Carruthe972c362015-03-26 03:11:40 +00003660 for (auto &Lookup : *DC->buildLookup()) {
3661 auto &Name = Lookup.first;
3662 auto &Result = Lookup.second;
3663
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003664 // If there are no local declarations in our lookup result, we
3665 // don't need to write an entry for the name at all. If we can't
3666 // write out a lookup set without performing more deserialization,
3667 // just skip this entry.
3668 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003669 isLookupResultEntirelyExternal(Result, DC))
3670 continue;
3671
3672 // We also skip empty results. If any of the results could be external and
3673 // the currently available results are empty, then all of the results are
3674 // external and we skip it above. So the only way we get here with an empty
3675 // results is when no results could have been external *and* we have
3676 // external results.
3677 //
3678 // FIXME: While we might want to start emitting on-disk entries for negative
3679 // lookups into a decl context as an optimization, today we *have* to skip
3680 // them because there are names with empty lookup results in decl contexts
3681 // which we can't emit in any stable ordering: we lookup constructors and
3682 // conversion functions in the enclosing namespace scope creating empty
3683 // results for them. This in almost certainly a bug in Clang's name lookup,
3684 // but that is likely to be hard or impossible to fix and so we tolerate it
3685 // here by omitting lookups with empty results.
3686 if (Lookup.second.getLookupResult().empty())
3687 continue;
3688
3689 switch (Lookup.first.getNameKind()) {
3690 default:
3691 Names.push_back(Lookup.first);
3692 break;
3693
Richard Smithcd45dbc2014-04-19 03:48:30 +00003694 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003695 assert(isa<CXXRecordDecl>(DC) &&
3696 "Cannot have a constructor name outside of a class!");
3697 ConstructorNameSet.insert(Name);
3698 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003699
3700 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003701 assert(isa<CXXRecordDecl>(DC) &&
3702 "Cannot have a conversion function name outside of a class!");
3703 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003704 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003705 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003706 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003707
Chandler Carruthe972c362015-03-26 03:11:40 +00003708 // Sort the names into a stable order.
Fangrui Song55fab262018-09-26 22:16:28 +00003709 llvm::sort(Names);
Chandler Carruthe972c362015-03-26 03:11:40 +00003710
Chandler Carruthffbf7052015-03-26 22:27:09 +00003711 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003712 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003713 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003714
Chandler Carruthffbf7052015-03-26 22:27:09 +00003715 // First we try the easy case by forming the current context's constructor
3716 // name and adding that name first. This is a very useful optimization to
3717 // avoid walking the lexical declarations in many cases, and it also
3718 // handles the only case where a constructor name can come from some other
3719 // lexical context -- when that name is an implicit constructor merged from
3720 // another declaration in the redecl chain. Any non-implicit constructor or
3721 // conversion function which doesn't occur in all the lexical contexts
3722 // would be an ODR violation.
3723 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3724 Context->getCanonicalType(Context->getRecordType(D)));
3725 if (ConstructorNameSet.erase(ImplicitCtorName))
3726 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003727
Chandler Carruthffbf7052015-03-26 22:27:09 +00003728 // If we still have constructors or conversion functions, we walk all the
3729 // names in the decl and add the constructors and conversion functions
3730 // which are visible in the order they lexically occur within the context.
3731 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3732 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3733 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3734 auto Name = ChildND->getDeclName();
3735 switch (Name.getNameKind()) {
3736 default:
3737 continue;
3738
3739 case DeclarationName::CXXConstructorName:
3740 if (ConstructorNameSet.erase(Name))
3741 Names.push_back(Name);
3742 break;
3743
3744 case DeclarationName::CXXConversionFunctionName:
3745 if (ConversionNameSet.erase(Name))
3746 Names.push_back(Name);
3747 break;
3748 }
3749
3750 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3751 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003752 }
3753
Chandler Carruthe972c362015-03-26 03:11:40 +00003754 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3755 "constructors by walking all the "
3756 "lexical members of the context.");
3757 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3758 "conversion functions by walking all "
3759 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003760 }
3761
Chandler Carruthe972c362015-03-26 03:11:40 +00003762 // Next we need to do a lookup with each name into this decl context to fully
3763 // populate any results from external sources. We don't actually use the
3764 // results of these lookups because we only want to use the results after all
3765 // results have been loaded and the pointers into them will be stable.
3766 for (auto &Name : Names)
3767 DC->lookup(Name);
3768
3769 // Now we need to insert the results for each name into the hash table. For
3770 // constructor names and conversion function names, we actually need to merge
3771 // all of the results for them into one list of results each and insert
3772 // those.
3773 SmallVector<NamedDecl *, 8> ConstructorDecls;
3774 SmallVector<NamedDecl *, 8> ConversionDecls;
3775
3776 // Now loop over the names, either inserting them or appending for the two
3777 // special cases.
3778 for (auto &Name : Names) {
3779 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3780
3781 switch (Name.getNameKind()) {
3782 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003783 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003784 break;
3785
3786 case DeclarationName::CXXConstructorName:
3787 ConstructorDecls.append(Result.begin(), Result.end());
3788 break;
3789
3790 case DeclarationName::CXXConversionFunctionName:
3791 ConversionDecls.append(Result.begin(), Result.end());
3792 break;
3793 }
3794 }
3795
3796 // Handle our two special cases if we ended up having any. We arbitrarily use
3797 // the first declaration's name here because the name itself isn't part of
3798 // the key, only the kind of name is used.
3799 if (!ConstructorDecls.empty())
3800 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003801 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003802 if (!ConversionDecls.empty())
3803 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003804 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003805
Richard Smithd88a7f12015-09-01 20:35:42 +00003806 // Create the on-disk hash table. Also emit the existing imported and
3807 // merged table if there is one.
3808 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3809 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003810}
3811
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003812/// Write the block containing all of the declaration IDs
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003813/// visible from the given DeclContext.
3814///
3815/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003816/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003817uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3818 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003819 // If we imported a key declaration of this namespace, write the visible
3820 // lookup results as an update record for it rather than including them
3821 // on this declaration. We will only look at key declarations on reload.
3822 if (isa<NamespaceDecl>(DC) && Chain &&
3823 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3824 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003825 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003826 Prev = Prev->getPreviousDecl())
3827 if (!Prev->isFromASTFile())
3828 return 0;
3829
3830 // Note that we need to emit an update record for the primary context.
3831 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3832
3833 // Make sure all visible decls are written. They will be recorded later. We
3834 // do this using a side data structure so we can sort the names into
3835 // a deterministic order.
3836 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3837 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3838 LookupResults;
3839 if (Map) {
3840 LookupResults.reserve(Map->size());
3841 for (auto &Entry : *Map)
3842 LookupResults.push_back(
3843 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3844 }
3845
Fangrui Song55fab262018-09-26 22:16:28 +00003846 llvm::sort(LookupResults, llvm::less_first());
Richard Smith5fc18a92015-07-12 23:43:21 +00003847 for (auto &NameAndResult : LookupResults) {
3848 DeclarationName Name = NameAndResult.first;
3849 DeclContext::lookup_result Result = NameAndResult.second;
3850 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3851 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3852 // We have to work around a name lookup bug here where negative lookup
3853 // results for these names get cached in namespace lookup tables (these
3854 // names should never be looked up in a namespace).
3855 assert(Result.empty() && "Cannot have a constructor or conversion "
3856 "function name in a namespace!");
3857 continue;
3858 }
3859
3860 for (NamedDecl *ND : Result)
3861 if (!ND->isFromASTFile())
3862 GetDeclRef(ND);
3863 }
3864
3865 return 0;
3866 }
3867
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003868 if (DC->getPrimaryContext() != DC)
3869 return 0;
3870
Chandler Carruthe972c362015-03-26 03:11:40 +00003871 // Skip contexts which don't support name lookup.
3872 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003873 return 0;
3874
3875 // If not in C++, we perform name lookup for the translation unit via the
3876 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003877 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003878 return 0;
3879
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003880 // Serialize the contents of the mapping used for lookup. Note that,
3881 // although we have two very different code paths, the serialized
3882 // representation is the same for both cases: a declaration name,
3883 // followed by a size, followed by references to the visible
3884 // declarations that have that name.
3885 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003886 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003887 if (!Map || Map->empty())
3888 return 0;
3889
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +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);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003893
3894 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003895 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003896 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003897 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003898 ++NumVisibleDeclContexts;
3899 return Offset;
3900}
3901
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003902/// Write an UPDATE_VISIBLE block for the given context.
Sebastian Redla4071b42010-08-24 00:50:09 +00003903///
3904/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3905/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003906/// (in C++), for namespaces, and for classes with forward-declared unscoped
3907/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003908void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003909 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003910 if (!Map || Map->empty())
3911 return;
3912
Sebastian Redla4071b42010-08-24 00:50:09 +00003913 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003914 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003915 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003916
Richard Smith5fc18a92015-07-12 23:43:21 +00003917 // If we're updating a namespace, select a key declaration as the key for the
3918 // update record; those are the only ones that will be checked on reload.
3919 if (isa<NamespaceDecl>(DC))
3920 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3921
Sebastian Redla4071b42010-08-24 00:50:09 +00003922 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003923 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003924 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003925}
3926
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003927/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003928void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07003929 RecordData::value_type Record[] = {Opts.getAsOpaqueInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003930 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3931}
3932
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003933/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003934void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003935 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003936 return;
3937
3938 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3939 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00003940 for (const auto &I:Opts.OptMap) {
3941 AddString(I.getKey(), Record);
3942 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00003943 Record.push_back(V.Supported ? 1 : 0);
3944 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00003945 Record.push_back(V.Avail);
3946 Record.push_back(V.Core);
3947 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003948 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3949}
3950
Yaxun Liu5b746652016-12-18 05:18:55 +00003951void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
3952 if (!SemaRef.Context.getLangOpts().OpenCL)
3953 return;
3954
Bruno Ricci2751b692019-04-18 15:13:27 +00003955 // Sort the elements of the map OpenCLTypeExtMap by TypeIDs,
3956 // without copying them.
3957 const llvm::DenseMap<const Type *, std::set<std::string>> &OpenCLTypeExtMap =
3958 SemaRef.OpenCLTypeExtMap;
3959 using ElementTy = std::pair<TypeID, const std::set<std::string> *>;
3960 llvm::SmallVector<ElementTy, 8> StableOpenCLTypeExtMap;
3961 StableOpenCLTypeExtMap.reserve(OpenCLTypeExtMap.size());
3962
3963 for (const auto &I : OpenCLTypeExtMap)
3964 StableOpenCLTypeExtMap.emplace_back(
3965 getTypeID(I.first->getCanonicalTypeInternal()), &I.second);
3966
3967 auto CompareByTypeID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
3968 return E1.first < E2.first;
3969 };
3970 llvm::sort(StableOpenCLTypeExtMap, CompareByTypeID);
3971
Yaxun Liu5b746652016-12-18 05:18:55 +00003972 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00003973 for (const ElementTy &E : StableOpenCLTypeExtMap) {
3974 Record.push_back(E.first); // TypeID
3975 const std::set<std::string> *ExtSet = E.second;
3976 Record.push_back(static_cast<unsigned>(ExtSet->size()));
3977 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00003978 AddString(Ext, Record);
3979 }
Bruno Ricci2751b692019-04-18 15:13:27 +00003980
Yaxun Liu5b746652016-12-18 05:18:55 +00003981 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
3982}
3983
3984void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
3985 if (!SemaRef.Context.getLangOpts().OpenCL)
3986 return;
3987
Bruno Ricci2751b692019-04-18 15:13:27 +00003988 // Sort the elements of the map OpenCLDeclExtMap by DeclIDs,
3989 // without copying them.
3990 const llvm::DenseMap<const Decl *, std::set<std::string>> &OpenCLDeclExtMap =
3991 SemaRef.OpenCLDeclExtMap;
3992 using ElementTy = std::pair<DeclID, const std::set<std::string> *>;
3993 llvm::SmallVector<ElementTy, 8> StableOpenCLDeclExtMap;
3994 StableOpenCLDeclExtMap.reserve(OpenCLDeclExtMap.size());
3995
3996 for (const auto &I : OpenCLDeclExtMap)
3997 StableOpenCLDeclExtMap.emplace_back(getDeclID(I.first), &I.second);
3998
3999 auto CompareByDeclID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
4000 return E1.first < E2.first;
4001 };
4002 llvm::sort(StableOpenCLDeclExtMap, CompareByDeclID);
4003
Yaxun Liu5b746652016-12-18 05:18:55 +00004004 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00004005 for (const ElementTy &E : StableOpenCLDeclExtMap) {
4006 Record.push_back(E.first); // DeclID
4007 const std::set<std::string> *ExtSet = E.second;
4008 Record.push_back(static_cast<unsigned>(ExtSet->size()));
4009 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00004010 AddString(Ext, Record);
4011 }
Bruno Ricci2751b692019-04-18 15:13:27 +00004012
Yaxun Liu5b746652016-12-18 05:18:55 +00004013 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4014}
4015
Justin Lebar67a78a62016-10-08 22:15:58 +00004016void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4017 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4018 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4019 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4020 }
4021}
4022
Douglas Gregor404cdde2012-01-27 01:47:08 +00004023void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004024 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004025 RecordData Categories;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004026
Douglas Gregor404cdde2012-01-27 01:47:08 +00004027 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4028 unsigned Size = 0;
4029 unsigned StartIndex = Categories.size();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004030
Douglas Gregor404cdde2012-01-27 01:47:08 +00004031 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004032
Douglas Gregor404cdde2012-01-27 01:47:08 +00004033 // Allocate space for the size.
4034 Categories.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004035
Douglas Gregor404cdde2012-01-27 01:47:08 +00004036 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004037 for (ObjCInterfaceDecl::known_categories_iterator
4038 Cat = Class->known_categories_begin(),
4039 CatEnd = Class->known_categories_end();
4040 Cat != CatEnd; ++Cat, ++Size) {
4041 assert(getDeclID(*Cat) != 0 && "Bogus category");
4042 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004043 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004044
Douglas Gregor404cdde2012-01-27 01:47:08 +00004045 // Update the size.
4046 Categories[StartIndex] = Size;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004047
Douglas Gregor404cdde2012-01-27 01:47:08 +00004048 // Record this interface -> category map.
4049 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4050 CategoriesMap.push_back(CatInfo);
4051 }
4052
4053 // Sort the categories map by the definition ID, since the reader will be
4054 // performing binary searches on this information.
4055 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4056
4057 // Emit the categories map.
4058 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004059
David Blaikieb44f0bf2017-01-04 22:36:43 +00004060 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004061 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004064 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004065
4066 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4067 Stream.EmitRecordWithBlob(AbbrevID, Record,
4068 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004069 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004070
Douglas Gregor404cdde2012-01-27 01:47:08 +00004071 // Emit the category lists.
4072 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4073}
4074
Richard Smithe40f2ba2013-08-07 21:41:30 +00004075void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4076 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4077
4078 if (LPTMap.empty())
4079 return;
4080
4081 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004082 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004083 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004084 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004085 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004086 AddDeclRef(LPT.D, Record);
4087 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004088
Justin Lebar28f09c52016-10-10 16:26:08 +00004089 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004090 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004091 }
4092 }
4093 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4094}
4095
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004096/// Write the state of 'pragma clang optimize' at the end of the module.
Dario Domizioli13a0a382014-05-23 12:13:25 +00004097void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4098 RecordData Record;
4099 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4100 AddSourceLocation(PragmaLoc, Record);
4101 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4102}
4103
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004104/// Write the state of 'pragma ms_struct' at the end of the module.
Nico Weber779355f2016-03-02 23:22:00 +00004105void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4106 RecordData Record;
4107 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4108 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4109}
4110
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004111/// Write the state of 'pragma pointers_to_members' at the end of the
Nico Weber42932312016-03-03 00:17:35 +00004112//module.
4113void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4114 RecordData Record;
4115 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4116 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4117 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4118}
4119
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004120/// Write the state of 'pragma pack' at the end of the module.
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004121void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004122 // Don't serialize pragma pack state for modules, since it should only take
4123 // effect on a per-submodule basis.
4124 if (WritingModule)
4125 return;
4126
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004127 RecordData Record;
4128 Record.push_back(SemaRef.PackStack.CurrentValue);
4129 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4130 Record.push_back(SemaRef.PackStack.Stack.size());
4131 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4132 Record.push_back(StackEntry.Value);
4133 AddSourceLocation(StackEntry.PragmaLocation, Record);
Alex Lorenz45b40142017-07-28 14:41:21 +00004134 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004135 AddString(StackEntry.StackSlotLabel, Record);
4136 }
4137 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4138}
4139
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004140void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4141 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004142 // Enter the extension block.
4143 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4144
4145 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004146 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004147 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4148 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4149 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4150 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4151 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4152 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004153 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004154
4155 // Emit the metadata record.
4156 RecordData Record;
4157 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4158 Record.push_back(EXTENSION_METADATA);
4159 Record.push_back(Metadata.MajorVersion);
4160 Record.push_back(Metadata.MinorVersion);
4161 Record.push_back(Metadata.BlockName.size());
4162 Record.push_back(Metadata.UserInfo.size());
4163 SmallString<64> Buffer;
4164 Buffer += Metadata.BlockName;
4165 Buffer += Metadata.UserInfo;
4166 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4167
4168 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004169 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004170
4171 // Exit the extension block.
4172 Stream.ExitBlock();
4173}
4174
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004175//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004176// General Serialization Routines
4177//===----------------------------------------------------------------------===//
4178
Richard Smithe43e2b32018-08-20 21:47:29 +00004179void ASTRecordWriter::AddAttr(const Attr *A) {
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004180 auto &Record = *this;
Richard Smithe43e2b32018-08-20 21:47:29 +00004181 if (!A)
4182 return Record.push_back(0);
4183 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
Erich Keane6a24e802019-09-13 17:39:31 +00004184
4185 Record.AddIdentifierRef(A->getAttrName());
4186 Record.AddIdentifierRef(A->getScopeName());
Richard Smithe43e2b32018-08-20 21:47:29 +00004187 Record.AddSourceRange(A->getRange());
Erich Keane6a24e802019-09-13 17:39:31 +00004188 Record.AddSourceLocation(A->getScopeLoc());
4189 Record.push_back(A->getParsedKind());
4190 Record.push_back(A->getSyntax());
4191 Record.push_back(A->getAttributeSpellingListIndexRaw());
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004192
4193#include "clang/Serialization/AttrPCHWrite.inc"
Richard Smithe43e2b32018-08-20 21:47:29 +00004194}
4195
4196/// Emit the list of attributes to the specified record.
4197void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4198 push_back(Attrs.size());
4199 for (const auto *A : Attrs)
4200 AddAttr(A);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004201}
4202
John McCallf413f5e2013-05-03 00:10:13 +00004203void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4204 AddSourceLocation(Tok.getLocation(), Record);
4205 Record.push_back(Tok.getLength());
4206
4207 // FIXME: When reading literal tokens, reconstruct the literal pointer
4208 // if it is needed.
4209 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4210 // FIXME: Should translate token kind to a stable encoding.
4211 Record.push_back(Tok.getKind());
4212 // FIXME: Should translate token flags to a stable encoding.
4213 Record.push_back(Tok.getFlags());
4214}
4215
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004216void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004217 Record.push_back(Str.size());
4218 Record.insert(Record.end(), Str.begin(), Str.end());
4219}
4220
Richard Smith7ed1bc92014-12-05 22:42:13 +00004221bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004222 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004223
Richard Smith54cc3c22014-12-11 20:50:24 +00004224 bool Changed =
4225 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004226
4227 // Remove a prefix to make the path relative, if relevant.
4228 const char *PathBegin = Path.data();
4229 const char *PathPtr =
4230 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4231 if (PathPtr != PathBegin) {
4232 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4233 Changed = true;
4234 }
4235
4236 return Changed;
4237}
4238
4239void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4240 SmallString<128> FilePath(Path);
4241 PreparePathForOutput(FilePath);
4242 AddString(FilePath, Record);
4243}
4244
Mehdi Amini57a41912015-09-10 01:46:39 +00004245void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004246 StringRef Path) {
4247 SmallString<128> FilePath(Path);
4248 PreparePathForOutput(FilePath);
4249 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4250}
4251
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004252void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4253 RecordDataImpl &Record) {
4254 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004255 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004256 Record.push_back(*Minor + 1);
4257 else
4258 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004259 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004260 Record.push_back(*Subminor + 1);
4261 else
4262 Record.push_back(0);
4263}
4264
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004265/// Note that the identifier II occurs at the given offset
Douglas Gregore84a9da2009-04-20 20:36:09 +00004266/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004267void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004268 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004269 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004270 // up earlier in the chain and thus don't need an offset.
4271 if (ID >= FirstIdentID)
4272 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004273}
4274
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004275/// Note that the selector Sel occurs at the given offset
Douglas Gregor95c13f52009-04-25 17:48:32 +00004276/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004277void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004278 unsigned ID = SelectorIDs[Sel];
4279 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004280 // Don't record offsets for selectors that are also available in a different
4281 // file.
4282 if (ID < FirstSelectorID)
4283 return;
4284 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004285}
4286
David Blaikie61137e12017-01-05 18:23:18 +00004287ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004288 SmallVectorImpl<char> &Buffer,
4289 InMemoryModuleCache &ModuleCache,
David Blaikie61137e12017-01-05 18:23:18 +00004290 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4291 bool IncludeTimestamps)
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004292 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004293 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004294 for (const auto &Ext : Extensions) {
4295 if (auto Writer = Ext->createExtensionWriter(*this))
4296 ModuleFileExtensionWriters.push_back(std::move(Writer));
4297 }
4298}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004299
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004300ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004301 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004302}
4303
Richard Smithb3761912015-02-05 23:08:52 +00004304const LangOptions &ASTWriter::getLangOpts() const {
4305 assert(WritingAST && "can't determine lang opts when not writing AST");
4306 return Context->getLangOpts();
4307}
4308
Richard Smithe75ee0f2015-08-17 07:13:32 +00004309time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4310 return IncludeTimestamps ? E->getModificationTime() : 0;
4311}
4312
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004313ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4314 const std::string &OutputFile,
4315 Module *WritingModule, StringRef isysroot,
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004316 bool hasErrors,
4317 bool ShouldCacheASTInMemory) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004318 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004319
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004320 ASTHasCompilerErrors = hasErrors;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004321
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004322 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004323 Stream.Emit((unsigned)'C', 8);
4324 Stream.Emit((unsigned)'P', 8);
4325 Stream.Emit((unsigned)'C', 8);
4326 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004327
Chris Lattner28fa4e62009-04-26 22:26:21 +00004328 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004329
Douglas Gregoreda8e122011-08-09 15:13:55 +00004330 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004331 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004332 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004333 ASTFileSignature Signature =
4334 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004335 Context = nullptr;
4336 PP = nullptr;
4337 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004338 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004339
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004340 WritingAST = false;
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004341 if (ShouldCacheASTInMemory) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004342 // Construct MemoryBuffer and update buffer manager.
Rumeet Dhindsa57a2eaf2020-03-10 10:59:26 -07004343 ModuleCache.addBuiltPCM(OutputFile,
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004344 llvm::MemoryBuffer::getMemBufferCopy(
4345 StringRef(Buffer.begin(), Buffer.size())));
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004346 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004347 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004348}
4349
Douglas Gregora94a1542011-07-27 21:45:57 +00004350template<typename Vector>
4351static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4352 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004353 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4354 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004355 Writer.AddDeclRef(*I, Record);
4356 }
4357}
4358
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004359ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4360 const std::string &OutputFile,
4361 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004362 using namespace llvm;
4363
Craig Toppera13603a2014-05-22 05:54:18 +00004364 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004365
Douglas Gregorcf68c582011-12-01 22:20:10 +00004366 // Make sure that the AST reader knows to finalize itself.
4367 if (Chain)
4368 Chain->finalizeForWriting();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004369
Sebastian Redl143413f2010-07-12 22:02:52 +00004370 ASTContext &Context = SemaRef.Context;
4371 Preprocessor &PP = SemaRef.PP;
4372
Douglas Gregordab42432011-08-12 00:15:20 +00004373 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004374 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4375 if (D) {
4376 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4377 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004378 }
4379 };
4380 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4381 PREDEF_DECL_TRANSLATION_UNIT_ID);
4382 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4383 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4384 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4385 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4386 PREDEF_DECL_OBJC_PROTOCOL_ID);
4387 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4388 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4389 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4390 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4391 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004392 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004393 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4394 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smithbab6df82020-04-11 22:15:29 -07004395 RegisterPredefDecl(Context.MSGuidTagDecl,
4396 PREDEF_DECL_BUILTIN_MS_GUID_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004397 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004398 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4399 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004400 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4401 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004402 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4403 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004404 RegisterPredefDecl(Context.TypePackElementDecl,
4405 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004406
Chris Lattner0c797362009-09-08 18:19:27 +00004407 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004408 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004409 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004410 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004411 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004412
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004413 // Build a record containing all of the file scoped decls in this file.
4414 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004415 if (!isModule)
4416 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4417 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004418
Douglas Gregor851443c2011-08-12 01:39:19 +00004419 // Build a record containing all of the delegating constructors we still need
4420 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004421 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004422 if (!isModule)
4423 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004424
Douglas Gregor851443c2011-08-12 01:39:19 +00004425 // Write the set of weak, undeclared identifiers. We always write the
4426 // entire table, since later PCH files in a PCH chain are only interested in
4427 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004428 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004429 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4430 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4431 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4432 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4433 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4434 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4435 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004436 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004437
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004438 // Build a record containing all of the ext_vector declarations.
4439 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004440 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004441
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004442 // Build a record containing all of the VTable uses information.
4443 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004444 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004445 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4446 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4447 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4448 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4449 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004450 }
4451
Nico Weber72889432014-09-06 01:25:55 +00004452 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4453 RecordData UnusedLocalTypedefNameCandidates;
4454 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4455 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4456
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004457 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004458 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004459 for (const auto &I : SemaRef.PendingInstantiations) {
4460 AddDeclRef(I.first, PendingInstantiations);
4461 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004462 }
4463 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4464 "There are local ones at end of translation unit!");
4465
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004466 // Build a record containing some declaration references.
4467 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004468 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004469 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4470 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004471 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004472 }
4473
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004474 RecordData CUDASpecialDeclRefs;
4475 if (Context.getcudaConfigureCallDecl()) {
4476 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4477 }
4478
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004479 // Build a record containing all of the known namespaces.
4480 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004481 for (const auto &I : SemaRef.KnownNamespaces) {
4482 if (!I.second)
4483 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004484 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004485
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004486 // Build a record of all used, undefined objects that require definitions.
4487 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004488
4489 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004490 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004491 for (const auto &I : Undefined) {
4492 AddDeclRef(I.first, UndefinedButUsed);
4493 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004494 }
4495
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004496 // Build a record containing all delete-expressions that we would like to
4497 // analyze later in AST.
4498 RecordData DeleteExprsToAnalyze;
4499
Richard Smithf5262c62018-06-28 01:57:04 +00004500 if (!isModule) {
4501 for (const auto &DeleteExprsInfo :
4502 SemaRef.getMismatchingDeleteExpressions()) {
4503 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4504 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4505 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4506 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4507 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4508 }
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004509 }
4510 }
4511
Douglas Gregor112b9072012-10-18 05:31:06 +00004512 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004513 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004514
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004515 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004516 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004517
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004518 // This is so that older clang versions, before the introduction
4519 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004520 {
4521 RecordData Record = {VERSION_MAJOR};
4522 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4523 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004524
Douglas Gregor851443c2011-08-12 01:39:19 +00004525 // Create a lexical update block containing all of the declarations in the
4526 // translation unit that do not come from other AST files.
4527 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004528 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4529 for (const auto *D : TU->noload_decls()) {
4530 if (!D->isFromASTFile()) {
4531 NewGlobalKindDeclPairs.push_back(D->getKind());
4532 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4533 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004534 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004535
David Blaikieb44f0bf2017-01-04 22:36:43 +00004536 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004537 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4538 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004539 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004540 {
4541 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4542 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4543 bytes(NewGlobalKindDeclPairs));
4544 }
4545
Douglas Gregor851443c2011-08-12 01:39:19 +00004546 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004547 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004548 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4549 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004550 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004551 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004552 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004553
4554 // If we have any extern "C" names, write out a visible update for them.
4555 if (Context.ExternCContext)
4556 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004557
Douglas Gregor851443c2011-08-12 01:39:19 +00004558 // If the translation unit has an anonymous namespace, and we don't already
4559 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004560 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004561 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4562 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004563 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004564 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004565 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004566
Richard Smith5652c0f2014-03-21 01:48:23 +00004567 // Add update records for all mangling numbers and static local numbers.
4568 // These aren't really update records, but this is a convenient way of
4569 // tagging this rare extra data onto the declarations.
4570 for (const auto &Number : Context.MangleNumbers)
4571 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004572 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4573 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004574 for (const auto &Number : Context.StaticLocalNumbers)
4575 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004576 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4577 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004578
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004579 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004580 // an AST file, are registered for serialization. Likewise for template
4581 // specializations added to imported templates.
4582 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004583 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004584 }
4585
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004586 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004587 // serialization, if we're storing decls with identifiers.
4588 if (!WritingModule || !getLangOpts().CPlusPlus) {
4589 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004590 for (const auto &ID : PP.getIdentifierTable()) {
4591 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004592 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4593 IIs.push_back(II);
4594 }
4595 // Sort the identifiers to visit based on their name.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00004596 llvm::sort(IIs, llvm::deref<std::less<>>());
Richard Smith79bf9202015-08-24 03:33:22 +00004597 for (const IdentifierInfo *II : IIs) {
4598 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4599 DEnd = SemaRef.IdResolver.end();
4600 D != DEnd; ++D) {
4601 GetDeclRef(*D);
4602 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004603 }
4604 }
4605
Manman Rena0f31a02016-04-29 19:04:05 +00004606 // For method pool in the module, if it contains an entry for a selector,
4607 // the entry should be complete, containing everything introduced by that
4608 // module and all modules it imports. It's possible that the entry is out of
4609 // date, so we need to pull in the new content here.
4610
4611 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4612 // safe, we copy all selectors out.
4613 llvm::SmallVector<Selector, 256> AllSelectors;
4614 for (auto &SelectorAndID : SelectorIDs)
4615 AllSelectors.push_back(SelectorAndID.first);
4616 for (auto &Selector : AllSelectors)
4617 SemaRef.updateOutOfDateSelector(Selector);
4618
Douglas Gregor5204bde2011-08-02 16:26:37 +00004619 // Form the record of special types.
4620 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004621 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004622 AddTypeRef(Context.getFILEType(), SpecialTypes);
4623 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4624 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4625 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4626 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004627 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004628 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004629
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004630 if (Chain) {
4631 // Write the mapping information describing our module dependencies and how
4632 // each of those modules were mapped into our own offset/ID space, so that
4633 // the reader can build the appropriate mapping to its own offset/ID space.
4634 // The map consists solely of a blob with the following format:
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004635 // *(module-kind:i8
4636 // module-name-len:i16 module-name:len*i8
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004637 // source-location-offset:i32
4638 // identifier-id:i32
4639 // preprocessed-entity-id:i32
4640 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004641 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004642 // selector-id:i32
4643 // declaration-id:i32
4644 // c++-base-specifiers-id:i32
4645 // type-id:i32)
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004646 //
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004647 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule or
4648 // MK_ExplicitModule, then the module-name is the module name. Otherwise,
4649 // it is the module file name.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004650 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004651 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004653 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004654 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004655 {
4656 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004657 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004658 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00004659
Peter Collingbournee3f65292018-05-18 19:46:24 +00004660 endian::Writer LE(Out, little);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004661 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
4662 StringRef Name =
4663 M.Kind == MK_PrebuiltModule || M.Kind == MK_ExplicitModule
4664 ? M.ModuleName
4665 : M.FileName;
4666 LE.write<uint16_t>(Name.size());
4667 Out.write(Name.data(), Name.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004668
Ben Langmuir785180e2014-10-20 16:27:30 +00004669 // Note: if a base ID was uint max, it would not be possible to load
4670 // another module after it or have more than one entity inside it.
4671 uint32_t None = std::numeric_limits<uint32_t>::max();
4672
4673 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4674 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4675 if (ShouldWrite)
4676 LE.write<uint32_t>(BaseID);
4677 else
4678 LE.write<uint32_t>(None);
4679 };
4680
Ben Langmuirfe971d92014-08-16 04:54:18 +00004681 // These values should be unique within a chain, since they will be read
4682 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004683 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4684 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4685 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4686 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4687 M.NumPreprocessedEntities);
4688 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4689 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4690 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4691 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004692 }
4693 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004694 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004695 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4696 Buffer.data(), Buffer.size());
4697 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004698
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05004699 // Build a record containing all of the DeclsToCheckForDeferredDiags.
4700 RecordData DeclsToCheckForDeferredDiags;
4701 for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
4702 AddDeclRef(D, DeclsToCheckForDeferredDiags);
4703
Richard Smithb9eab6d2014-03-20 19:44:17 +00004704 RecordData DeclUpdatesOffsetsRecord;
4705
Richard Smith59442b42014-03-20 20:07:19 +00004706 // Keep writing types, declarations, and declaration update records
4707 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004708 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4709 WriteTypeAbbrevs();
4710 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004711 do {
4712 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4713 while (!DeclTypesToEmit.empty()) {
4714 DeclOrType DOT = DeclTypesToEmit.front();
4715 DeclTypesToEmit.pop();
4716 if (DOT.isType())
4717 WriteType(DOT.getType());
4718 else
4719 WriteDecl(Context, DOT.getDecl());
4720 }
4721 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004722 Stream.ExitBlock();
4723
Richard Smithb9eab6d2014-03-20 19:44:17 +00004724 DoneWritingDeclsAndTypes = true;
4725
4726 // These things can only be done once we've written out decls and types.
4727 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004728 if (!DeclUpdatesOffsetsRecord.empty())
4729 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004730 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004731 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004732 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004733 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004734 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004735 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004736 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004737 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004738 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Melanie Blower8812b0c2020-04-16 08:45:26 -07004739 WriteFPPragmaOptions(SemaRef.getCurFPFeatures());
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004740 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004741 WriteOpenCLExtensionTypes(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004742 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004743
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004744 // If we're emitting a module, write out the submodule information.
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004745 if (WritingModule)
4746 WriteSubmodules(WritingModule);
4747
Andrew Savonichev52d674d2018-10-29 11:14:01 +00004748 // We need to have information about submodules to correctly deserialize
4749 // decls from OpenCLExtensionDecls block
4750 WriteOpenCLExtensionDecls(SemaRef);
4751
Douglas Gregor5204bde2011-08-02 16:26:37 +00004752 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4753
Douglas Gregord4df8652009-04-22 22:02:47 +00004754 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004755 if (!EagerlyDeserializedDecls.empty())
4756 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004757
David Blaikief63556d2017-04-12 20:58:33 +00004758 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00004759 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4760
Douglas Gregord4df8652009-04-22 22:02:47 +00004761 // Write the record containing tentative definitions.
4762 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004763 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004764
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004765 // Write the record containing unused file scoped decls.
4766 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004767 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004768
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004769 // Write the record containing weak undeclared identifiers.
4770 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004771 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004772 WeakUndeclaredIdentifiers);
4773
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004774 // Write the record containing ext_vector type names.
4775 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004776 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004777
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004778 // Write the record containing VTable uses information.
4779 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004780 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004781
Nico Weber72889432014-09-06 01:25:55 +00004782 // Write the record containing potentially unused local typedefs.
4783 if (!UnusedLocalTypedefNameCandidates.empty())
4784 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4785 UnusedLocalTypedefNameCandidates);
4786
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004787 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004788 if (!PendingInstantiations.empty())
4789 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004790
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004791 // Write the record containing declaration references of Sema.
4792 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004793 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004794
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05004795 // Write the record containing decls to be checked for deferred diags.
4796 if (!DeclsToCheckForDeferredDiags.empty())
4797 Stream.EmitRecord(DECLS_TO_CHECK_FOR_DEFERRED_DIAGS,
4798 DeclsToCheckForDeferredDiags);
4799
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004800 // Write the record containing CUDA-specific declaration references.
4801 if (!CUDASpecialDeclRefs.empty())
4802 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004803
Alexis Hunt27a761d2011-05-04 23:29:54 +00004804 // Write the delegating constructors.
4805 if (!DelegatingCtorDecls.empty())
4806 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004807
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004808 // Write the known namespaces.
4809 if (!KnownNamespaces.empty())
4810 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004811
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004812 // Write the undefined internal functions and variables, and inline functions.
4813 if (!UndefinedButUsed.empty())
4814 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004815
4816 if (!DeleteExprsToAnalyze.empty())
4817 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4818
Douglas Gregor851443c2011-08-12 01:39:19 +00004819 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004820 for (auto *DC : UpdatedDeclContexts)
4821 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004822
Douglas Gregor959bb062011-12-03 01:15:29 +00004823 if (!WritingModule) {
4824 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004825 struct ModuleInfo {
4826 uint64_t ID;
4827 Module *M;
4828 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4829 };
Richard Smith56be7542014-03-21 00:33:59 +00004830 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004831 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004832 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004833 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4834 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004835 }
Richard Smith56be7542014-03-21 00:33:59 +00004836
4837 if (!Imports.empty()) {
4838 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4839 return A.ID < B.ID;
4840 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004841 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4842 return A.ID == B.ID;
4843 };
Richard Smith56be7542014-03-21 00:33:59 +00004844
4845 // Sort and deduplicate module IDs.
Fangrui Song55fab262018-09-26 22:16:28 +00004846 llvm::sort(Imports, Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004847 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004848 Imports.end());
4849
4850 RecordData ImportedModules;
4851 for (const auto &Import : Imports) {
4852 ImportedModules.push_back(Import.ID);
4853 // FIXME: If the module has macros imported then later has declarations
4854 // imported, this location won't be the right one as a location for the
4855 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004856 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004857 }
4858
Douglas Gregor959bb062011-12-03 01:15:29 +00004859 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4860 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004861 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004862
Douglas Gregor404cdde2012-01-27 01:47:08 +00004863 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004864 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004865 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004866 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004867 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004868 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004869 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004870
Douglas Gregor08f01292009-04-17 22:13:46 +00004871 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004872 RecordData::value_type Record[] = {
4873 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004874 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004875 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004876
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004877 // Write the module file extension blocks.
4878 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004879 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004880
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004881 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004882}
4883
Richard Smithb9eab6d2014-03-20 19:44:17 +00004884void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004885 if (DeclUpdates.empty())
4886 return;
4887
Richard Smith59442b42014-03-20 20:07:19 +00004888 DeclUpdateMap LocalUpdates;
4889 LocalUpdates.swap(DeclUpdates);
4890
Richard Smith6ef42932014-03-20 21:02:00 +00004891 for (auto &DeclUpdate : LocalUpdates) {
4892 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004893
Richard Smithd28ac5b2014-03-22 23:33:22 +00004894 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004895 RecordData RecordData;
4896 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004897 for (auto &Update : DeclUpdate.second) {
4898 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4899
Richard Smith69c82bf2016-04-01 22:52:03 +00004900 // An updated body is emitted last, so that the reader doesn't need
4901 // to skip over the lazy body to reach statements for other records.
4902 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4903 HasUpdatedBody = true;
4904 else
4905 Record.push_back(Kind);
4906
Richard Smith6ef42932014-03-20 21:02:00 +00004907 switch (Kind) {
4908 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4909 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4910 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004911 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004912 Record.push_back(GetDeclRef(Update.getDecl()));
4913 break;
4914
Richard Smith4d235792014-08-07 18:53:08 +00004915 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004916 break;
4917
Richard Smith891fc7f2017-12-05 01:31:47 +00004918 case UPD_CXX_POINT_OF_INSTANTIATION:
4919 // FIXME: Do we need to also save the template specialization kind here?
Richard Smith69c82bf2016-04-01 22:52:03 +00004920 Record.AddSourceLocation(Update.getLoc());
Richard Smith891fc7f2017-12-05 01:31:47 +00004921 break;
4922
4923 case UPD_CXX_ADDED_VAR_DEFINITION: {
4924 const VarDecl *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004925 Record.push_back(VD->isInline());
4926 Record.push_back(VD->isInlineSpecified());
Richard Smith05a21352017-06-22 22:18:46 +00004927 if (VD->getInit()) {
4928 Record.push_back(!VD->isInitKnownICE() ? 1
4929 : (VD->isInitICE() ? 3 : 2));
4930 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
4931 } else {
4932 Record.push_back(0);
4933 }
Richard Smith4d235792014-08-07 18:53:08 +00004934 break;
Richard Smith05a21352017-06-22 22:18:46 +00004935 }
Richard Smith4d235792014-08-07 18:53:08 +00004936
John McCall32791cc2016-01-06 22:34:54 +00004937 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004938 Record.AddStmt(const_cast<Expr *>(
4939 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004940 break;
4941
Richard Smith4b054b22016-08-24 21:25:37 +00004942 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4943 Record.AddStmt(
4944 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4945 break;
4946
Richard Smithcd45dbc2014-04-19 03:48:30 +00004947 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4948 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004949 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004950 Record.push_back(RD->isParamDestroyedInCallee());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004951 Record.push_back(RD->getArgPassingRestrictions());
Richard Smith69c82bf2016-04-01 22:52:03 +00004952 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004953 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004954 *Context, const_cast<CXXRecordDecl *>(RD)));
4955
4956 // This state is sometimes updated by template instantiation, when we
4957 // switch from the specialization referring to the template declaration
4958 // to it referring to the template definition.
4959 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4960 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004961 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004962 } else {
4963 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4964 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004965 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004966
4967 // The instantiation might have been resolved to a partial
4968 // specialization. If so, record which one.
4969 auto From = Spec->getInstantiatedFrom();
4970 if (auto PartialSpec =
4971 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4972 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004973 Record.AddDeclRef(PartialSpec);
4974 Record.AddTemplateArgumentList(
4975 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004976 } else {
4977 Record.push_back(false);
4978 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004979 }
4980 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004981 Record.AddSourceLocation(RD->getLocation());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004982 Record.AddSourceLocation(RD->getBeginLoc());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004983 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004984
4985 // Instantiation may change attributes; write them all out afresh.
4986 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004987 if (D->hasAttrs())
4988 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004989
4990 // FIXME: Ensure we don't get here for explicit instantiations.
4991 break;
4992 }
4993
Richard Smithf8134002015-03-10 01:41:22 +00004994 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004995 Record.AddDeclRef(Update.getDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004996 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
Richard Smithf8134002015-03-10 01:41:22 +00004997 break;
4998
John McCalld505e572019-12-13 21:54:44 -05004999 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
5000 auto prototype =
5001 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>();
5002 Record.writeExceptionSpecInfo(prototype->getExceptionSpecInfo());
Richard Smith564417a2014-03-20 21:47:22 +00005003 break;
John McCalld505e572019-12-13 21:54:44 -05005004 }
Richard Smith564417a2014-03-20 21:47:22 +00005005
Richard Smith6ef42932014-03-20 21:02:00 +00005006 case UPD_CXX_DEDUCED_RETURN_TYPE:
5007 Record.push_back(GetOrCreateTypeID(Update.getType()));
5008 break;
5009
5010 case UPD_DECL_MARKED_USED:
5011 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00005012
5013 case UPD_MANGLING_NUMBER:
5014 case UPD_STATIC_LOCAL_NUMBER:
5015 Record.push_back(Update.getNumber());
5016 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005017
Alexey Bataev97720002014-11-11 04:05:39 +00005018 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005019 Record.AddSourceRange(
5020 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005021 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005022
Alexey Bataev27ef9512019-03-20 20:14:22 +00005023 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5024 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5025 Record.push_back(A->getAllocatorType());
5026 Record.AddStmt(A->getAllocator());
5027 Record.AddSourceRange(A->getRange());
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005028 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00005029 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005030
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005031 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00005032 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005033 Record.AddSourceRange(
5034 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5035 break;
5036
Richard Smith65ebb4a2015-03-26 04:09:53 +00005037 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005038 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005039 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005040
5041 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005042 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005043 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005044 }
5045 }
5046
Richard Smithd28ac5b2014-03-22 23:33:22 +00005047 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005048 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005049 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005050 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005051 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005052 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005053 }
5054
Richard Smithcd45dbc2014-04-19 03:48:30 +00005055 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005056 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005057 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005058}
5059
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005060void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005061 uint32_t Raw = Loc.getRawEncoding();
5062 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005063}
5064
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005065void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005066 AddSourceLocation(Range.getBegin(), Record);
5067 AddSourceLocation(Range.getEnd(), Record);
5068}
5069
Richard Smithf144b542016-04-08 21:54:32 +00005070void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5071 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005072}
5073
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00005074static void WriteFixedPointSemantics(ASTRecordWriter &Record,
5075 FixedPointSemantics FPSema) {
5076 Record.push_back(FPSema.getWidth());
5077 Record.push_back(FPSema.getScale());
5078 Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 |
5079 FPSema.hasUnsignedPadding() << 2);
5080}
5081
5082void ASTRecordWriter::AddAPValue(const APValue &Value) {
5083 APValue::ValueKind Kind = Value.getKind();
5084 push_back(static_cast<uint64_t>(Kind));
5085 switch (Kind) {
5086 case APValue::None:
5087 case APValue::Indeterminate:
5088 return;
5089 case APValue::Int:
5090 AddAPSInt(Value.getInt());
5091 return;
5092 case APValue::Float:
5093 push_back(static_cast<uint64_t>(
5094 llvm::APFloatBase::SemanticsToEnum(Value.getFloat().getSemantics())));
5095 AddAPFloat(Value.getFloat());
5096 return;
5097 case APValue::FixedPoint: {
5098 WriteFixedPointSemantics(*this, Value.getFixedPoint().getSemantics());
5099 AddAPSInt(Value.getFixedPoint().getValue());
5100 return;
5101 }
5102 case APValue::ComplexInt: {
5103 AddAPSInt(Value.getComplexIntReal());
5104 AddAPSInt(Value.getComplexIntImag());
5105 return;
5106 }
5107 case APValue::ComplexFloat: {
5108 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5109 Value.getComplexFloatReal().getSemantics())));
5110 AddAPFloat(Value.getComplexFloatReal());
5111 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5112 Value.getComplexFloatImag().getSemantics())));
5113 AddAPFloat(Value.getComplexFloatImag());
5114 return;
5115 }
5116 case APValue::LValue:
5117 case APValue::Vector:
5118 case APValue::Array:
5119 case APValue::Struct:
5120 case APValue::Union:
5121 case APValue::MemberPointer:
5122 case APValue::AddrLabelDiff:
5123 // TODO : Handle all these APValue::ValueKind.
5124 return;
5125 }
5126 llvm_unreachable("Invalid APValue::ValueKind");
5127}
5128
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005129void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005130 Record.push_back(getIdentifierRef(II));
5131}
5132
Sebastian Redl539c5062010-08-18 23:57:32 +00005133IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005134 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005135 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005136
Sebastian Redl539c5062010-08-18 23:57:32 +00005137 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005138 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005139 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005140 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005141}
5142
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005143MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005144 // Don't emit builtin macros like __LINE__ to the AST file unless they
5145 // have been redefined by the header (in which case they are not
5146 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005147 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005148 return 0;
5149
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005150 MacroID &ID = MacroIDs[MI];
5151 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005152 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005153 MacroInfoToEmitData Info = { Name, MI, ID };
5154 MacroInfosToEmit.push_back(Info);
5155 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005156 return ID;
5157}
5158
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005159MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005160 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005161 return 0;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005162
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005163 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5164 return MacroIDs[MI];
5165}
5166
Dmitry Polukhina7afb212020-04-16 09:24:46 -07005167uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005168 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005169}
5170
Richard Smithe64e7452016-04-18 21:54:58 +00005171void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5172 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005173}
5174
Sebastian Redl539c5062010-08-18 23:57:32 +00005175SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005176 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005177 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005178 }
5179
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005180 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005181 if (SID == 0 && Chain) {
5182 // This might trigger a ReadSelector callback, which will set the ID for
5183 // this selector.
5184 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005185 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005186 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005187 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005188 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005189 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005190 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005191 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005192}
5193
Richard Smithe64e7452016-04-18 21:54:58 +00005194void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5195 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005196}
5197
Richard Smith290d8012016-04-06 17:06:00 +00005198void ASTRecordWriter::AddTemplateArgumentLocInfo(
5199 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005200 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005201 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005202 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005203 break;
5204 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005205 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005206 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005207 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005208 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5209 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005210 break;
5211 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005212 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5213 AddSourceLocation(Arg.getTemplateNameLoc());
5214 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005215 break;
John McCall0ad16662009-10-29 08:12:44 +00005216 case TemplateArgument::Null:
5217 case TemplateArgument::Integral:
5218 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005219 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005220 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005221 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005222 break;
5223 }
5224}
5225
Richard Smith290d8012016-04-06 17:06:00 +00005226void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5227 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005228
5229 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5230 bool InfoHasSameExpr
5231 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005232 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005233 if (InfoHasSameExpr)
5234 return; // Avoid storing the same expr twice.
5235 }
Richard Smith290d8012016-04-06 17:06:00 +00005236 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005237}
5238
Richard Smith290d8012016-04-06 17:06:00 +00005239void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005240 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005241 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005242 return;
5243 }
5244
Richard Smithc23d7342018-06-29 20:46:25 +00005245 AddTypeRef(TInfo->getType());
Richard Smith290d8012016-04-06 17:06:00 +00005246 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005247}
5248
Richard Smith290d8012016-04-06 17:06:00 +00005249void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
Richard Smith290d8012016-04-06 17:06:00 +00005250 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005251 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005252 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005253}
5254
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005255void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005256 Record.push_back(GetOrCreateTypeID(T));
5257}
5258
Richard Smith2095ffe2015-03-16 20:11:03 +00005259TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005260 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005261 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5262 if (T.isNull())
5263 return TypeIdx();
5264 assert(!T.getLocalFastQualifiers());
5265
5266 TypeIdx &Idx = TypeIdxs[T];
5267 if (Idx.getIndex() == 0) {
5268 if (DoneWritingDeclsAndTypes) {
5269 assert(0 && "New type seen after serializing all the types to emit!");
5270 return TypeIdx();
5271 }
5272
5273 // We haven't seen this type before. Assign it a new ID and put it
5274 // into the queue of types to emit.
5275 Idx = TypeIdx(NextTypeID++);
5276 DeclTypesToEmit.push(T);
5277 }
5278 return Idx;
5279 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005280}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005281
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005282TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005283 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005284 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5285 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005286 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005287 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005288
Richard Smith2095ffe2015-03-16 20:11:03 +00005289 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5290 assert(I != TypeIdxs.end() && "Type not emitted!");
5291 return I->second;
5292 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005293}
5294
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005295void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005296 Record.push_back(GetDeclRef(D));
5297}
5298
Sebastian Redl539c5062010-08-18 23:57:32 +00005299DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005300 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005301
5302 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005303 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005304 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005305
Douglas Gregorb3163e52012-01-05 22:33:30 +00005306 // If D comes from an AST file, its declaration ID is already known and
5307 // fixed.
5308 if (D->isFromASTFile())
5309 return D->getGlobalID();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005310
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005311 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005312 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005313 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005314 if (DoneWritingDeclsAndTypes) {
5315 assert(0 && "New decl seen after serializing all the decls to emit!");
5316 return 0;
5317 }
5318
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005319 // We haven't seen this declaration before. Give it a new ID and
5320 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005321 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005322 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005323 }
5324
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005325 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005326}
5327
Sebastian Redl539c5062010-08-18 23:57:32 +00005328DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005329 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005330 return 0;
5331
Douglas Gregorb3163e52012-01-05 22:33:30 +00005332 // If D comes from an AST file, its declaration ID is already known and
5333 // fixed.
5334 if (D->isFromASTFile())
5335 return D->getGlobalID();
5336
Douglas Gregore84a9da2009-04-20 20:36:09 +00005337 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5338 return DeclIDs[D];
5339}
5340
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005341void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005342 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005343 assert(D);
5344
5345 SourceLocation Loc = D->getLocation();
5346 if (Loc.isInvalid())
5347 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005348
5349 // We only keep track of the file-level declarations of each file.
5350 if (!D->getLexicalDeclContext()->isFileContext())
5351 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005352 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5353 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis71b74eb2018-01-26 19:26:12 +00005354 // TemplateTemplateParmDecls that are part of an alias template, should not
5355 // have TU as lexical context.
5356 if (isa<ParmVarDecl>(D) || isa<TemplateTemplateParmDecl>(D))
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005357 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005358
5359 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005360 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005361 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005362 FileID FID;
5363 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005364 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005365 if (FID.isInvalid())
5366 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005367 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005368
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005369 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005370 if (!Info)
5371 Info = new DeclIDInFileInfo();
5372
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005373 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005374 LocDeclIDsTy &Decls = Info->DeclIDs;
5375
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005376 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005377 Decls.push_back(LocDecl);
5378 return;
5379 }
5380
Benjamin Kramer45025c02013-08-24 13:22:59 +00005381 LocDeclIDsTy::iterator I =
Fangrui Song7264a472019-07-03 08:13:17 +00005382 llvm::upper_bound(Decls, LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005383
5384 Decls.insert(I, LocDecl);
5385}
5386
Richard Smithd08aeb62014-08-28 01:33:39 +00005387unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5388 assert(needsAnonymousDeclarationNumber(D) &&
5389 "expected an anonymous declaration");
5390
5391 // Number the anonymous declarations within this context, if we've not
5392 // already done so.
5393 auto It = AnonymousDeclarationNumbers.find(D);
5394 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005395 auto *DC = D->getLexicalDeclContext();
5396 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5397 AnonymousDeclarationNumbers[ND] = Number;
5398 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005399
5400 It = AnonymousDeclarationNumbers.find(D);
5401 assert(It != AnonymousDeclarationNumbers.end() &&
5402 "declaration not found within its lexical context");
5403 }
5404
5405 return It->second;
5406}
5407
Richard Smith290d8012016-04-06 17:06:00 +00005408void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5409 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005410 switch (Name.getNameKind()) {
5411 case DeclarationName::CXXConstructorName:
5412 case DeclarationName::CXXDestructorName:
5413 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005414 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005415 break;
5416
5417 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005418 AddSourceLocation(SourceLocation::getFromRawEncoding(
5419 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005420 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005421 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005422 break;
5423
5424 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005425 AddSourceLocation(SourceLocation::getFromRawEncoding(
5426 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005427 break;
5428
5429 case DeclarationName::Identifier:
5430 case DeclarationName::ObjCZeroArgSelector:
5431 case DeclarationName::ObjCOneArgSelector:
5432 case DeclarationName::ObjCMultiArgSelector:
5433 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005434 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005435 break;
5436 }
5437}
5438
Richard Smith290d8012016-04-06 17:06:00 +00005439void ASTRecordWriter::AddDeclarationNameInfo(
5440 const DeclarationNameInfo &NameInfo) {
5441 AddDeclarationName(NameInfo.getName());
5442 AddSourceLocation(NameInfo.getLoc());
5443 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005444}
5445
Richard Smith290d8012016-04-06 17:06:00 +00005446void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5447 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5448 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005449 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005450 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005451}
5452
Richard Smith290d8012016-04-06 17:06:00 +00005453void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005454 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005455 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005456 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005457
5458 // Push each of the nested-name-specifiers's onto a stack for
5459 // serialization in reverse order.
5460 while (NNS) {
5461 NestedNames.push_back(NNS);
5462 NNS = NNS.getPrefix();
5463 }
5464
Richard Smith290d8012016-04-06 17:06:00 +00005465 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005466 while(!NestedNames.empty()) {
5467 NNS = NestedNames.pop_back_val();
5468 NestedNameSpecifier::SpecifierKind Kind
5469 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005470 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005471 switch (Kind) {
5472 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005473 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5474 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005475 break;
5476
5477 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005478 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5479 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005480 break;
5481
5482 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005483 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5484 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005485 break;
5486
5487 case NestedNameSpecifier::TypeSpec:
5488 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005489 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Richard Smithc23d7342018-06-29 20:46:25 +00005490 AddTypeRef(NNS.getTypeLoc().getType());
Richard Smith290d8012016-04-06 17:06:00 +00005491 AddTypeLoc(NNS.getTypeLoc());
5492 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005493 break;
5494
5495 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005496 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005497 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005498
5499 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005500 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5501 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005502 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005503 }
5504 }
5505}
5506
Richard Smithe64e7452016-04-18 21:54:58 +00005507void ASTRecordWriter::AddTemplateParameterList(
5508 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005509 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005510 AddSourceLocation(TemplateParams->getTemplateLoc());
5511 AddSourceLocation(TemplateParams->getLAngleLoc());
5512 AddSourceLocation(TemplateParams->getRAngleLoc());
Saar Raz0330fba2019-10-15 18:44:06 +00005513
Richard Smithe64e7452016-04-18 21:54:58 +00005514 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005515 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005516 AddDeclRef(P);
Saar Raz0330fba2019-10-15 18:44:06 +00005517 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
5518 Record->push_back(true);
5519 AddStmt(const_cast<Expr*>(RequiresClause));
5520 } else {
5521 Record->push_back(false);
5522 }
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005523}
5524
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005525/// Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005526void ASTRecordWriter::AddTemplateArgumentList(
5527 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005528 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005529 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005530 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005531 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005532}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005533
Richard Smith290d8012016-04-06 17:06:00 +00005534void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5535 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005536 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005537 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5538 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5539 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005540 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005541 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005542 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005543}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005544
Richard Smithe64e7452016-04-18 21:54:58 +00005545void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5546 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005547 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005548 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005549 AddDeclRef(I.getDecl());
5550 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005551 }
5552}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005553
Richard Smith290d8012016-04-06 17:06:00 +00005554// FIXME: Move this out of the main ASTRecordWriter interface.
5555void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5556 Record->push_back(Base.isVirtual());
5557 Record->push_back(Base.isBaseOfClass());
5558 Record->push_back(Base.getAccessSpecifierAsWritten());
5559 Record->push_back(Base.getInheritConstructors());
5560 AddTypeSourceInfo(Base.getTypeSourceInfo());
5561 AddSourceRange(Base.getSourceRange());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005562 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005563 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005564}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005565
Richard Smith645d2cf2016-04-14 00:29:55 +00005566static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5567 ArrayRef<CXXBaseSpecifier> Bases) {
5568 ASTWriter::RecordData Record;
5569 ASTRecordWriter Writer(W, Record);
5570 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005571
Richard Smith645d2cf2016-04-14 00:29:55 +00005572 for (auto &Base : Bases)
5573 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005574
Richard Smith645d2cf2016-04-14 00:29:55 +00005575 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005576}
5577
Richard Smith290d8012016-04-06 17:06:00 +00005578// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005579void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5580 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5581}
5582
Richard Smithaa165cf2016-04-13 21:57:08 +00005583static uint64_t
5584EmitCXXCtorInitializers(ASTWriter &W,
5585 ArrayRef<CXXCtorInitializer *> CtorInits) {
5586 ASTWriter::RecordData Record;
5587 ASTRecordWriter Writer(W, Record);
5588 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005589
Richard Smithaa165cf2016-04-13 21:57:08 +00005590 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005591 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005592 Writer.push_back(CTOR_INITIALIZER_BASE);
5593 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5594 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005595 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005596 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5597 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005598 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005599 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5600 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005601 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005602 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5603 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005604 }
Francois Pichetd583da02010-12-04 09:14:42 +00005605
Richard Smithaa165cf2016-04-13 21:57:08 +00005606 Writer.AddSourceLocation(Init->getMemberLocation());
5607 Writer.AddStmt(Init->getInit());
5608 Writer.AddSourceLocation(Init->getLParenLoc());
5609 Writer.AddSourceLocation(Init->getRParenLoc());
5610 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005611 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005612 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005613 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005614
5615 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005616}
5617
Richard Smithaa165cf2016-04-13 21:57:08 +00005618// FIXME: Move this out of the main ASTRecordWriter interface.
5619void ASTRecordWriter::AddCXXCtorInitializers(
5620 ArrayRef<CXXCtorInitializer *> CtorInits) {
5621 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005622}
5623
Richard Smith290d8012016-04-06 17:06:00 +00005624void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005625 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005626 Record->push_back(Data.IsLambda);
Richard Smith91aeacc2019-10-11 00:29:04 +00005627
5628 #define FIELD(Name, Width, Merge) \
5629 Record->push_back(Data.Name);
5630 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
Richard Trieufd1acbb2017-04-11 21:31:00 +00005631
5632 // getODRHash will compute the ODRHash if it has not been previously computed.
5633 Record->push_back(D->getODRHash());
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +01005634 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
5635 Writer->WritingModule && !D->isDependentType();
David Blaikief63556d2017-04-12 20:58:33 +00005636 Record->push_back(ModulesDebugInfo);
5637 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00005638 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
5639
Richard Smith561fb152012-02-25 07:33:38 +00005640 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005641
Richard Smith290d8012016-04-06 17:06:00 +00005642 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005643 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005644 AddCXXBaseSpecifiers(Data.bases());
5645
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005646 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005647 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005648 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005649 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005650
Richard Smith290d8012016-04-06 17:06:00 +00005651 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
Richard Smith91aeacc2019-10-11 00:29:04 +00005652 Record->push_back(Data.ComputedVisibleConversions);
5653 if (Data.ComputedVisibleConversions)
5654 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005655 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005656 AddDeclRef(D->getFirstFriend());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005657
Douglas Gregor99ae8062012-02-14 17:54:36 +00005658 // Add lambda-specific data.
5659 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005660 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005661 Record->push_back(Lambda.Dependent);
5662 Record->push_back(Lambda.IsGenericLambda);
5663 Record->push_back(Lambda.CaptureDefault);
5664 Record->push_back(Lambda.NumCaptures);
5665 Record->push_back(Lambda.NumExplicitCaptures);
Michael Liao243ebfb2019-10-19 00:15:19 +00005666 Record->push_back(Lambda.HasKnownInternalLinkage);
Richard Smith290d8012016-04-06 17:06:00 +00005667 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005668 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005669 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005670 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005671 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005672 AddSourceLocation(Capture.getLocation());
5673 Record->push_back(Capture.isImplicit());
5674 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005675 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005676 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005677 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005678 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005679 break;
5680 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005681 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005682 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005683 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005684 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005685 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005686 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005687 break;
5688 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005689 }
5690 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005691}
5692
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005693void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005694 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005695 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005696 assert(FirstDeclID == NextDeclID &&
5697 FirstTypeID == NextTypeID &&
5698 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005699 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005700 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005701 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005702 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005703
Sebastian Redl07a89a82010-07-30 00:29:29 +00005704 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005705
Richard Smith7f330cd2015-03-18 01:42:29 +00005706 // Note, this will get called multiple times, once one the reader starts up
5707 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005708 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5709 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5710 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005711 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005712 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005713 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005714 NextDeclID = FirstDeclID;
5715 NextTypeID = FirstTypeID;
5716 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005717 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005718 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005719 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005720}
5721
Sebastian Redl539c5062010-08-18 23:57:32 +00005722void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005723 // Always keep the highest ID. See \p TypeRead() for more information.
5724 IdentID &StoredID = IdentifierIDs[II];
5725 if (ID > StoredID)
5726 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005727}
5728
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005729void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005730 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005731 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005732 if (ID > StoredID)
5733 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005734}
5735
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005736void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005737 // Always take the highest-numbered type index. This copes with an interesting
5738 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005739 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005740 // keep the higher-numbered entry so that we can properly write it out to
5741 // the AST file.
5742 TypeIdx &StoredIdx = TypeIdxs[T];
5743 if (Idx.getIndex() >= StoredIdx.getIndex())
5744 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005745}
5746
Sebastian Redl539c5062010-08-18 23:57:32 +00005747void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005748 // Always keep the highest ID. See \p TypeRead() for more information.
5749 SelectorID &StoredID = SelectorIDs[S];
5750 if (ID > StoredID)
5751 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005752}
Douglas Gregor91096292010-10-02 19:29:26 +00005753
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005754void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005755 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005756 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005757 MacroDefinitions[MD] = ID;
5758}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005759
Douglas Gregore37a85a2011-12-02 17:30:13 +00005760void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5761 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5762 SubmoduleIDs[Mod] = ID;
5763}
5764
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005765void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005766 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005767 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005768 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005769 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005770 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005771 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005772 // A forward reference was mutated into a definition. Rewrite it.
5773 // FIXME: This happens during template instantiation, should we
5774 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005775 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5776 "completed a tag from another module but not by instantiation?");
5777 DeclUpdates[RD].push_back(
5778 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005779 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005780 }
5781}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005782
Richard Smithf983f7f2015-10-13 00:23:25 +00005783static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5784 if (D->isFromASTFile())
5785 return true;
5786
Richard Smithf983f7f2015-10-13 00:23:25 +00005787 // The predefined __va_list_tag struct is imported if we imported any decls.
5788 // FIXME: This is a gross hack.
5789 return D == D->getASTContext().getVaListTagDecl();
5790}
5791
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005792void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005793 if (Chain && Chain->isProcessingUpdateRecords()) return;
5794 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005795 "Should not add lookup results to non-lookup contexts!");
5796
Vassil Vassilev632eac32016-03-16 11:17:04 +00005797 // TU is handled elsewhere.
5798 if (isa<TranslationUnitDecl>(DC))
5799 return;
5800
5801 // Namespaces are handled elsewhere, except for template instantiations of
5802 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5803 // local instantiations are added to an imported context. Only happens when
5804 // adding ADL lookup candidates, for example templated friends.
5805 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5806 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005807 return;
5808
Richard Smithf983f7f2015-10-13 00:23:25 +00005809 // We're only interested in cases where a local declaration is added to an
5810 // imported context.
5811 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5812 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005813
Richard Smith0f4e2c42015-08-06 04:23:48 +00005814 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005815 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005816 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005817 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5818 // We're adding a visible declaration to a predefined decl context. Ensure
5819 // that we write out all of its lookup results so we don't get a nasty
5820 // surprise when we try to emit its lookup table.
5821 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005822 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005823 }
Richard Smithc26d9742016-10-06 20:30:51 +00005824 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005825}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005826
5827void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005828 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005829 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005830
5831 // We're only interested in cases where a local declaration is added to an
5832 // imported context.
5833 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5834 return;
5835
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005836 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005837 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005838
5839 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005840 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005841 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005842 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005843}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005844
Richard Smith564417a2014-03-20 21:47:22 +00005845void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005846 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005847 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5848 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005849 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005850 // If we don't already know the exception specification for this redecl
5851 // chain, add an update record for it.
5852 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5853 ->getType()
5854 ->castAs<FunctionProtoType>()
5855 ->getExceptionSpecType()))
5856 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5857 });
Richard Smith564417a2014-03-20 21:47:22 +00005858}
5859
Richard Smith1fa5d642013-05-11 05:45:24 +00005860void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005861 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005862 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005863 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005864 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005865 DeclUpdates[D].push_back(
5866 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5867 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005868}
5869
Richard Smithf8134002015-03-10 01:41:22 +00005870void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
Richard Smith5b349582017-10-13 01:55:36 +00005871 const FunctionDecl *Delete,
5872 Expr *ThisArg) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005873 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005874 assert(!WritingAST && "Already writing the AST!");
5875 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005876 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005877 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005878 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5879 });
Richard Smithf8134002015-03-10 01:41:22 +00005880}
5881
Sebastian Redlab238a72011-04-24 16:28:06 +00005882void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005883 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005884 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005885 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005886 return; // Declaration not imported from PCH.
5887
Richard Smith4d235792014-08-07 18:53:08 +00005888 // Implicit function decl from a PCH was defined.
5889 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005890}
5891
Richard Smith891fc7f2017-12-05 01:31:47 +00005892void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
5893 if (Chain && Chain->isProcessingUpdateRecords()) return;
5894 assert(!WritingAST && "Already writing the AST!");
5895 if (!D->isFromASTFile())
5896 return;
5897
5898 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
5899}
5900
Richard Smithd28ac5b2014-03-22 23:33:22 +00005901void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005902 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005903 assert(!WritingAST && "Already writing the AST!");
5904 if (!D->isFromASTFile())
5905 return;
5906
Richard Smith9e2341d2015-03-23 03:25:59 +00005907 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005908}
5909
Richard Smith891fc7f2017-12-05 01:31:47 +00005910void ASTWriter::InstantiationRequested(const ValueDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005911 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005912 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005913 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005914 return;
5915
5916 // Since the actual instantiation is delayed, this really means that we need
5917 // to update the instantiation location.
Richard Smith891fc7f2017-12-05 01:31:47 +00005918 SourceLocation POI;
5919 if (auto *VD = dyn_cast<VarDecl>(D))
5920 POI = VD->getPointOfInstantiation();
5921 else
5922 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
5923 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005924}
5925
John McCall32791cc2016-01-06 22:34:54 +00005926void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005927 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005928 assert(!WritingAST && "Already writing the AST!");
5929 if (!D->isFromASTFile())
5930 return;
5931
5932 DeclUpdates[D].push_back(
5933 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5934}
5935
Richard Smith4b054b22016-08-24 21:25:37 +00005936void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5937 assert(!WritingAST && "Already writing the AST!");
5938 if (!D->isFromASTFile())
5939 return;
5940
5941 DeclUpdates[D].push_back(
5942 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5943}
5944
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005945void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5946 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005947 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005948 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005949 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005950 return; // Declaration not imported from PCH.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005951
Douglas Gregor404cdde2012-01-27 01:47:08 +00005952 assert(IFD->getDefinition() && "Category on a class without a definition?");
5953 ObjCClassesWithCategories.insert(
5954 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005955}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005956
Eli Friedman276dd182013-09-05 00:02:25 +00005957void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005958 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005959 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005960
5961 // If there is *any* declaration of the entity that's not from an AST file,
5962 // we can skip writing the update record. We make sure that isUsed() triggers
5963 // completion of the redeclaration chain of the entity.
5964 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5965 if (IsLocalDecl(Prev))
5966 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005967
Aaron Ballman4f45b712014-03-21 15:22:56 +00005968 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005969}
Alexey Bataev97720002014-11-11 04:05:39 +00005970
5971void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005972 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005973 assert(!WritingAST && "Already writing the AST!");
5974 if (!D->isFromASTFile())
5975 return;
5976
5977 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5978}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005979
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005980void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
5981 if (Chain && Chain->isProcessingUpdateRecords()) return;
5982 assert(!WritingAST && "Already writing the AST!");
5983 if (!D->isFromASTFile())
5984 return;
5985
5986 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
5987}
5988
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005989void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5990 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005991 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005992 assert(!WritingAST && "Already writing the AST!");
5993 if (!D->isFromASTFile())
5994 return;
5995
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005996 DeclUpdates[D].push_back(
5997 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005998}
5999
Richard Smith4caa4492015-05-15 02:34:32 +00006000void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006001 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006002 assert(!WritingAST && "Already writing the AST!");
6003 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006004 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006005}
Alex Denisovfde64952015-06-26 05:28:36 +00006006
6007void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6008 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006009 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006010 assert(!WritingAST && "Already writing the AST!");
6011 if (!Record->isFromASTFile())
6012 return;
6013 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6014}
Richard Smithc26d9742016-10-06 20:30:51 +00006015
6016void ASTWriter::AddedCXXTemplateSpecialization(
6017 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6018 assert(!WritingAST && "Already writing the AST!");
6019
6020 if (!TD->getFirstDecl()->isFromASTFile())
6021 return;
6022 if (Chain && Chain->isProcessingUpdateRecords())
6023 return;
6024
6025 DeclsToEmitEvenIfUnreferenced.push_back(D);
6026}
6027
6028void ASTWriter::AddedCXXTemplateSpecialization(
6029 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6030 assert(!WritingAST && "Already writing the AST!");
6031
6032 if (!TD->getFirstDecl()->isFromASTFile())
6033 return;
6034 if (Chain && Chain->isProcessingUpdateRecords())
6035 return;
6036
6037 DeclsToEmitEvenIfUnreferenced.push_back(D);
6038}
6039
6040void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6041 const FunctionDecl *D) {
6042 assert(!WritingAST && "Already writing the AST!");
6043
6044 if (!TD->getFirstDecl()->isFromASTFile())
6045 return;
6046 if (Chain && Chain->isProcessingUpdateRecords())
6047 return;
6048
6049 DeclsToEmitEvenIfUnreferenced.push_back(D);
6050}
Kelvin Libe286f52018-09-15 13:54:15 +00006051
6052//===----------------------------------------------------------------------===//
6053//// OMPClause Serialization
6054////===----------------------------------------------------------------------===//
6055
John McCall2ac702a2019-12-14 03:17:03 -05006056namespace {
6057
6058class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
6059 ASTRecordWriter &Record;
6060
6061public:
6062 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
Johannes Doerfert419a5592020-03-30 19:58:40 -05006063#define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *S);
6064#include "llvm/Frontend/OpenMP/OMPKinds.def"
John McCall2ac702a2019-12-14 03:17:03 -05006065 void writeClause(OMPClause *C);
6066 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
6067 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
6068};
6069
6070}
6071
6072void ASTRecordWriter::writeOMPClause(OMPClause *C) {
6073 OMPClauseWriter(*this).writeClause(C);
6074}
6075
Kelvin Libe286f52018-09-15 13:54:15 +00006076void OMPClauseWriter::writeClause(OMPClause *C) {
Johannes Doerfert419a5592020-03-30 19:58:40 -05006077 Record.push_back(unsigned(C->getClauseKind()));
Kelvin Libe286f52018-09-15 13:54:15 +00006078 Visit(C);
6079 Record.AddSourceLocation(C->getBeginLoc());
6080 Record.AddSourceLocation(C->getEndLoc());
6081}
6082
6083void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06006084 Record.push_back(uint64_t(C->getCaptureRegion()));
Kelvin Libe286f52018-09-15 13:54:15 +00006085 Record.AddStmt(C->getPreInitStmt());
6086}
6087
6088void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6089 VisitOMPClauseWithPreInit(C);
6090 Record.AddStmt(C->getPostUpdateExpr());
6091}
6092
6093void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6094 VisitOMPClauseWithPreInit(C);
Johannes Doerferteb3e81f2019-11-04 22:00:49 -06006095 Record.push_back(uint64_t(C->getNameModifier()));
Kelvin Libe286f52018-09-15 13:54:15 +00006096 Record.AddSourceLocation(C->getNameModifierLoc());
6097 Record.AddSourceLocation(C->getColonLoc());
6098 Record.AddStmt(C->getCondition());
6099 Record.AddSourceLocation(C->getLParenLoc());
6100}
6101
6102void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
Alexey Bataev3a842ec2019-10-15 19:37:05 +00006103 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006104 Record.AddStmt(C->getCondition());
6105 Record.AddSourceLocation(C->getLParenLoc());
6106}
6107
6108void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6109 VisitOMPClauseWithPreInit(C);
6110 Record.AddStmt(C->getNumThreads());
6111 Record.AddSourceLocation(C->getLParenLoc());
6112}
6113
6114void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6115 Record.AddStmt(C->getSafelen());
6116 Record.AddSourceLocation(C->getLParenLoc());
6117}
6118
6119void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6120 Record.AddStmt(C->getSimdlen());
6121 Record.AddSourceLocation(C->getLParenLoc());
6122}
6123
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00006124void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6125 Record.AddStmt(C->getAllocator());
6126 Record.AddSourceLocation(C->getLParenLoc());
6127}
6128
Kelvin Libe286f52018-09-15 13:54:15 +00006129void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6130 Record.AddStmt(C->getNumForLoops());
6131 Record.AddSourceLocation(C->getLParenLoc());
6132}
6133
Alexey Bataev0f0564b2020-03-17 09:17:42 -04006134void OMPClauseWriter::VisitOMPDetachClause(OMPDetachClause *C) {
6135 Record.AddStmt(C->getEventHandler());
6136 Record.AddSourceLocation(C->getLParenLoc());
6137}
6138
Kelvin Libe286f52018-09-15 13:54:15 +00006139void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
Atmn Patel577c9b02020-02-14 21:45:49 -06006140 Record.push_back(unsigned(C->getDefaultKind()));
Kelvin Libe286f52018-09-15 13:54:15 +00006141 Record.AddSourceLocation(C->getLParenLoc());
6142 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6143}
6144
6145void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06006146 Record.push_back(unsigned(C->getProcBindKind()));
Kelvin Libe286f52018-09-15 13:54:15 +00006147 Record.AddSourceLocation(C->getLParenLoc());
6148 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6149}
6150
6151void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6152 VisitOMPClauseWithPreInit(C);
6153 Record.push_back(C->getScheduleKind());
6154 Record.push_back(C->getFirstScheduleModifier());
6155 Record.push_back(C->getSecondScheduleModifier());
6156 Record.AddStmt(C->getChunkSize());
6157 Record.AddSourceLocation(C->getLParenLoc());
6158 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6159 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6160 Record.AddSourceLocation(C->getScheduleKindLoc());
6161 Record.AddSourceLocation(C->getCommaLoc());
6162}
6163
6164void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6165 Record.push_back(C->getLoopNumIterations().size());
6166 Record.AddStmt(C->getNumForLoops());
6167 for (Expr *NumIter : C->getLoopNumIterations())
6168 Record.AddStmt(NumIter);
6169 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
Mike Rice0ed46662018-09-20 17:19:41 +00006170 Record.AddStmt(C->getLoopCounter(I));
Kelvin Libe286f52018-09-15 13:54:15 +00006171 Record.AddSourceLocation(C->getLParenLoc());
6172}
6173
6174void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6175
6176void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6177
6178void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6179
6180void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6181
6182void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6183
Alexey Bataev82f7c202020-03-03 13:22:35 -05006184void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *C) {
6185 Record.push_back(C->isExtended() ? 1 : 0);
6186 if (C->isExtended()) {
6187 Record.AddSourceLocation(C->getLParenLoc());
6188 Record.AddSourceLocation(C->getArgumentLoc());
6189 Record.writeEnum(C->getDependencyKind());
6190 }
6191}
Kelvin Libe286f52018-09-15 13:54:15 +00006192
6193void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6194
6195void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6196
Alexey Bataevea9166b2020-02-06 16:30:23 -05006197void OMPClauseWriter::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
6198
Alexey Bataev04a830f2020-02-10 14:30:39 -05006199void OMPClauseWriter::VisitOMPAcquireClause(OMPAcquireClause *) {}
6200
Alexey Bataev95598342020-02-10 15:49:05 -05006201void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
6202
Alexey Bataev9a8defc2020-02-11 11:10:43 -05006203void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
6204
Kelvin Libe286f52018-09-15 13:54:15 +00006205void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6206
6207void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6208
6209void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6210
Alexey Bataev375437a2020-03-02 14:21:20 -05006211void OMPClauseWriter::VisitOMPDestroyClause(OMPDestroyClause *) {}
6212
Kelvin Libe286f52018-09-15 13:54:15 +00006213void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6214 Record.push_back(C->varlist_size());
6215 Record.AddSourceLocation(C->getLParenLoc());
6216 for (auto *VE : C->varlists()) {
6217 Record.AddStmt(VE);
6218 }
6219 for (auto *VE : C->private_copies()) {
6220 Record.AddStmt(VE);
6221 }
6222}
6223
6224void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6225 Record.push_back(C->varlist_size());
6226 VisitOMPClauseWithPreInit(C);
6227 Record.AddSourceLocation(C->getLParenLoc());
6228 for (auto *VE : C->varlists()) {
6229 Record.AddStmt(VE);
6230 }
6231 for (auto *VE : C->private_copies()) {
6232 Record.AddStmt(VE);
6233 }
6234 for (auto *VE : C->inits()) {
6235 Record.AddStmt(VE);
6236 }
6237}
6238
6239void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6240 Record.push_back(C->varlist_size());
6241 VisitOMPClauseWithPostUpdate(C);
6242 Record.AddSourceLocation(C->getLParenLoc());
Alexey Bataev93dc40d2019-12-20 11:04:57 -05006243 Record.writeEnum(C->getKind());
6244 Record.AddSourceLocation(C->getKindLoc());
6245 Record.AddSourceLocation(C->getColonLoc());
Kelvin Libe286f52018-09-15 13:54:15 +00006246 for (auto *VE : C->varlists())
6247 Record.AddStmt(VE);
6248 for (auto *E : C->private_copies())
6249 Record.AddStmt(E);
6250 for (auto *E : C->source_exprs())
6251 Record.AddStmt(E);
6252 for (auto *E : C->destination_exprs())
6253 Record.AddStmt(E);
6254 for (auto *E : C->assignment_ops())
6255 Record.AddStmt(E);
6256}
6257
6258void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6259 Record.push_back(C->varlist_size());
6260 Record.AddSourceLocation(C->getLParenLoc());
6261 for (auto *VE : C->varlists())
6262 Record.AddStmt(VE);
6263}
6264
6265void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6266 Record.push_back(C->varlist_size());
6267 VisitOMPClauseWithPostUpdate(C);
6268 Record.AddSourceLocation(C->getLParenLoc());
Alexey Bataev1236eb62020-03-23 17:30:38 -04006269 Record.AddSourceLocation(C->getModifierLoc());
Kelvin Libe286f52018-09-15 13:54:15 +00006270 Record.AddSourceLocation(C->getColonLoc());
Alexey Bataev1236eb62020-03-23 17:30:38 -04006271 Record.writeEnum(C->getModifier());
Kelvin Libe286f52018-09-15 13:54:15 +00006272 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6273 Record.AddDeclarationNameInfo(C->getNameInfo());
6274 for (auto *VE : C->varlists())
6275 Record.AddStmt(VE);
6276 for (auto *VE : C->privates())
6277 Record.AddStmt(VE);
6278 for (auto *E : C->lhs_exprs())
6279 Record.AddStmt(E);
6280 for (auto *E : C->rhs_exprs())
6281 Record.AddStmt(E);
6282 for (auto *E : C->reduction_ops())
6283 Record.AddStmt(E);
6284}
6285
6286void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6287 Record.push_back(C->varlist_size());
6288 VisitOMPClauseWithPostUpdate(C);
6289 Record.AddSourceLocation(C->getLParenLoc());
6290 Record.AddSourceLocation(C->getColonLoc());
6291 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6292 Record.AddDeclarationNameInfo(C->getNameInfo());
6293 for (auto *VE : C->varlists())
6294 Record.AddStmt(VE);
6295 for (auto *VE : C->privates())
6296 Record.AddStmt(VE);
6297 for (auto *E : C->lhs_exprs())
6298 Record.AddStmt(E);
6299 for (auto *E : C->rhs_exprs())
6300 Record.AddStmt(E);
6301 for (auto *E : C->reduction_ops())
6302 Record.AddStmt(E);
6303}
6304
6305void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6306 Record.push_back(C->varlist_size());
6307 VisitOMPClauseWithPostUpdate(C);
6308 Record.AddSourceLocation(C->getLParenLoc());
6309 Record.AddSourceLocation(C->getColonLoc());
6310 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6311 Record.AddDeclarationNameInfo(C->getNameInfo());
6312 for (auto *VE : C->varlists())
6313 Record.AddStmt(VE);
6314 for (auto *VE : C->privates())
6315 Record.AddStmt(VE);
6316 for (auto *E : C->lhs_exprs())
6317 Record.AddStmt(E);
6318 for (auto *E : C->rhs_exprs())
6319 Record.AddStmt(E);
6320 for (auto *E : C->reduction_ops())
6321 Record.AddStmt(E);
6322 for (auto *E : C->taskgroup_descriptors())
6323 Record.AddStmt(E);
6324}
6325
6326void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6327 Record.push_back(C->varlist_size());
6328 VisitOMPClauseWithPostUpdate(C);
6329 Record.AddSourceLocation(C->getLParenLoc());
6330 Record.AddSourceLocation(C->getColonLoc());
6331 Record.push_back(C->getModifier());
6332 Record.AddSourceLocation(C->getModifierLoc());
6333 for (auto *VE : C->varlists()) {
6334 Record.AddStmt(VE);
6335 }
6336 for (auto *VE : C->privates()) {
6337 Record.AddStmt(VE);
6338 }
6339 for (auto *VE : C->inits()) {
6340 Record.AddStmt(VE);
6341 }
6342 for (auto *VE : C->updates()) {
6343 Record.AddStmt(VE);
6344 }
6345 for (auto *VE : C->finals()) {
6346 Record.AddStmt(VE);
6347 }
6348 Record.AddStmt(C->getStep());
6349 Record.AddStmt(C->getCalcStep());
Alexey Bataev195ae902019-08-08 13:42:45 +00006350 for (auto *VE : C->used_expressions())
6351 Record.AddStmt(VE);
Kelvin Libe286f52018-09-15 13:54:15 +00006352}
6353
6354void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6355 Record.push_back(C->varlist_size());
6356 Record.AddSourceLocation(C->getLParenLoc());
6357 Record.AddSourceLocation(C->getColonLoc());
6358 for (auto *VE : C->varlists())
6359 Record.AddStmt(VE);
6360 Record.AddStmt(C->getAlignment());
6361}
6362
6363void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6364 Record.push_back(C->varlist_size());
6365 Record.AddSourceLocation(C->getLParenLoc());
6366 for (auto *VE : C->varlists())
6367 Record.AddStmt(VE);
6368 for (auto *E : C->source_exprs())
6369 Record.AddStmt(E);
6370 for (auto *E : C->destination_exprs())
6371 Record.AddStmt(E);
6372 for (auto *E : C->assignment_ops())
6373 Record.AddStmt(E);
6374}
6375
6376void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6377 Record.push_back(C->varlist_size());
6378 Record.AddSourceLocation(C->getLParenLoc());
6379 for (auto *VE : C->varlists())
6380 Record.AddStmt(VE);
6381 for (auto *E : C->source_exprs())
6382 Record.AddStmt(E);
6383 for (auto *E : C->destination_exprs())
6384 Record.AddStmt(E);
6385 for (auto *E : C->assignment_ops())
6386 Record.AddStmt(E);
6387}
6388
6389void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6390 Record.push_back(C->varlist_size());
6391 Record.AddSourceLocation(C->getLParenLoc());
6392 for (auto *VE : C->varlists())
6393 Record.AddStmt(VE);
6394}
6395
Alexey Bataevc112e942020-02-28 09:52:15 -05006396void OMPClauseWriter::VisitOMPDepobjClause(OMPDepobjClause *C) {
6397 Record.AddStmt(C->getDepobj());
6398 Record.AddSourceLocation(C->getLParenLoc());
6399}
6400
Kelvin Libe286f52018-09-15 13:54:15 +00006401void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6402 Record.push_back(C->varlist_size());
6403 Record.push_back(C->getNumLoops());
6404 Record.AddSourceLocation(C->getLParenLoc());
Alexey Bataev13a15042020-04-01 15:06:38 -04006405 Record.AddStmt(C->getModifier());
Kelvin Libe286f52018-09-15 13:54:15 +00006406 Record.push_back(C->getDependencyKind());
6407 Record.AddSourceLocation(C->getDependencyLoc());
6408 Record.AddSourceLocation(C->getColonLoc());
6409 for (auto *VE : C->varlists())
6410 Record.AddStmt(VE);
6411 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6412 Record.AddStmt(C->getLoopData(I));
6413}
6414
6415void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6416 VisitOMPClauseWithPreInit(C);
Alexey Bataev2f8894a2020-03-18 15:01:15 -04006417 Record.writeEnum(C->getModifier());
Kelvin Libe286f52018-09-15 13:54:15 +00006418 Record.AddStmt(C->getDevice());
Alexey Bataev2f8894a2020-03-18 15:01:15 -04006419 Record.AddSourceLocation(C->getModifierLoc());
Kelvin Libe286f52018-09-15 13:54:15 +00006420 Record.AddSourceLocation(C->getLParenLoc());
6421}
6422
6423void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6424 Record.push_back(C->varlist_size());
6425 Record.push_back(C->getUniqueDeclarationsNum());
6426 Record.push_back(C->getTotalComponentListNum());
6427 Record.push_back(C->getTotalComponentsNum());
6428 Record.AddSourceLocation(C->getLParenLoc());
Reid Klecknerba1ffd22020-04-03 12:35:30 -07006429 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
Kelvin Lief579432018-12-18 22:18:41 +00006430 Record.push_back(C->getMapTypeModifier(I));
6431 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6432 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00006433 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6434 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006435 Record.push_back(C->getMapType());
6436 Record.AddSourceLocation(C->getMapLoc());
6437 Record.AddSourceLocation(C->getColonLoc());
6438 for (auto *E : C->varlists())
6439 Record.AddStmt(E);
Michael Kruse4304e9d2019-02-19 16:38:20 +00006440 for (auto *E : C->mapperlists())
6441 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006442 for (auto *D : C->all_decls())
6443 Record.AddDeclRef(D);
6444 for (auto N : C->all_num_lists())
6445 Record.push_back(N);
6446 for (auto N : C->all_lists_sizes())
6447 Record.push_back(N);
6448 for (auto &M : C->all_components()) {
6449 Record.AddStmt(M.getAssociatedExpression());
6450 Record.AddDeclRef(M.getAssociatedDeclaration());
6451 }
6452}
6453
Alexey Bataeve04483e2019-03-27 14:14:31 +00006454void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6455 Record.push_back(C->varlist_size());
6456 Record.AddSourceLocation(C->getLParenLoc());
6457 Record.AddSourceLocation(C->getColonLoc());
6458 Record.AddStmt(C->getAllocator());
6459 for (auto *VE : C->varlists())
6460 Record.AddStmt(VE);
6461}
6462
Kelvin Libe286f52018-09-15 13:54:15 +00006463void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6464 VisitOMPClauseWithPreInit(C);
6465 Record.AddStmt(C->getNumTeams());
6466 Record.AddSourceLocation(C->getLParenLoc());
6467}
6468
6469void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6470 VisitOMPClauseWithPreInit(C);
6471 Record.AddStmt(C->getThreadLimit());
6472 Record.AddSourceLocation(C->getLParenLoc());
6473}
6474
6475void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
Alexey Bataev31ba4762019-10-16 18:09:37 +00006476 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006477 Record.AddStmt(C->getPriority());
6478 Record.AddSourceLocation(C->getLParenLoc());
6479}
6480
6481void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
Alexey Bataevb9c55e22019-10-14 19:29:52 +00006482 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006483 Record.AddStmt(C->getGrainsize());
6484 Record.AddSourceLocation(C->getLParenLoc());
6485}
6486
6487void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
Alexey Bataevd88c7de2019-10-14 20:44:34 +00006488 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006489 Record.AddStmt(C->getNumTasks());
6490 Record.AddSourceLocation(C->getLParenLoc());
6491}
6492
6493void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
6494 Record.AddStmt(C->getHint());
6495 Record.AddSourceLocation(C->getLParenLoc());
6496}
6497
6498void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
6499 VisitOMPClauseWithPreInit(C);
6500 Record.push_back(C->getDistScheduleKind());
6501 Record.AddStmt(C->getChunkSize());
6502 Record.AddSourceLocation(C->getLParenLoc());
6503 Record.AddSourceLocation(C->getDistScheduleKindLoc());
6504 Record.AddSourceLocation(C->getCommaLoc());
6505}
6506
6507void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
6508 Record.push_back(C->getDefaultmapKind());
6509 Record.push_back(C->getDefaultmapModifier());
6510 Record.AddSourceLocation(C->getLParenLoc());
6511 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
6512 Record.AddSourceLocation(C->getDefaultmapKindLoc());
6513}
6514
6515void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
6516 Record.push_back(C->varlist_size());
6517 Record.push_back(C->getUniqueDeclarationsNum());
6518 Record.push_back(C->getTotalComponentListNum());
6519 Record.push_back(C->getTotalComponentsNum());
6520 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse01f670d2019-02-22 22:29:42 +00006521 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6522 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006523 for (auto *E : C->varlists())
6524 Record.AddStmt(E);
Michael Kruse01f670d2019-02-22 22:29:42 +00006525 for (auto *E : C->mapperlists())
6526 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006527 for (auto *D : C->all_decls())
6528 Record.AddDeclRef(D);
6529 for (auto N : C->all_num_lists())
6530 Record.push_back(N);
6531 for (auto N : C->all_lists_sizes())
6532 Record.push_back(N);
6533 for (auto &M : C->all_components()) {
6534 Record.AddStmt(M.getAssociatedExpression());
6535 Record.AddDeclRef(M.getAssociatedDeclaration());
6536 }
6537}
6538
6539void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
6540 Record.push_back(C->varlist_size());
6541 Record.push_back(C->getUniqueDeclarationsNum());
6542 Record.push_back(C->getTotalComponentListNum());
6543 Record.push_back(C->getTotalComponentsNum());
6544 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse0336c752019-02-25 20:34:15 +00006545 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6546 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006547 for (auto *E : C->varlists())
6548 Record.AddStmt(E);
Michael Kruse0336c752019-02-25 20:34:15 +00006549 for (auto *E : C->mapperlists())
6550 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006551 for (auto *D : C->all_decls())
6552 Record.AddDeclRef(D);
6553 for (auto N : C->all_num_lists())
6554 Record.push_back(N);
6555 for (auto N : C->all_lists_sizes())
6556 Record.push_back(N);
6557 for (auto &M : C->all_components()) {
6558 Record.AddStmt(M.getAssociatedExpression());
6559 Record.AddDeclRef(M.getAssociatedDeclaration());
6560 }
6561}
6562
6563void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
6564 Record.push_back(C->varlist_size());
6565 Record.push_back(C->getUniqueDeclarationsNum());
6566 Record.push_back(C->getTotalComponentListNum());
6567 Record.push_back(C->getTotalComponentsNum());
6568 Record.AddSourceLocation(C->getLParenLoc());
6569 for (auto *E : C->varlists())
6570 Record.AddStmt(E);
6571 for (auto *VE : C->private_copies())
6572 Record.AddStmt(VE);
6573 for (auto *VE : C->inits())
6574 Record.AddStmt(VE);
6575 for (auto *D : C->all_decls())
6576 Record.AddDeclRef(D);
6577 for (auto N : C->all_num_lists())
6578 Record.push_back(N);
6579 for (auto N : C->all_lists_sizes())
6580 Record.push_back(N);
6581 for (auto &M : C->all_components()) {
6582 Record.AddStmt(M.getAssociatedExpression());
6583 Record.AddDeclRef(M.getAssociatedDeclaration());
6584 }
6585}
6586
6587void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
6588 Record.push_back(C->varlist_size());
6589 Record.push_back(C->getUniqueDeclarationsNum());
6590 Record.push_back(C->getTotalComponentListNum());
6591 Record.push_back(C->getTotalComponentsNum());
6592 Record.AddSourceLocation(C->getLParenLoc());
6593 for (auto *E : C->varlists())
6594 Record.AddStmt(E);
6595 for (auto *D : C->all_decls())
6596 Record.AddDeclRef(D);
6597 for (auto N : C->all_num_lists())
6598 Record.push_back(N);
6599 for (auto N : C->all_lists_sizes())
6600 Record.push_back(N);
6601 for (auto &M : C->all_components()) {
6602 Record.AddStmt(M.getAssociatedExpression());
6603 Record.AddDeclRef(M.getAssociatedDeclaration());
6604 }
6605}
Kelvin Li1408f912018-09-26 04:28:39 +00006606
6607void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
Patrick Lyster4a370b92018-10-01 13:47:43 +00006608
6609void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
6610 OMPUnifiedSharedMemoryClause *) {}
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00006611
6612void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
Patrick Lyster3fe9e392018-10-11 14:41:10 +00006613
6614void
6615OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
6616}
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00006617
6618void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
6619 OMPAtomicDefaultMemOrderClause *C) {
6620 Record.push_back(C->getAtomicDefaultMemOrderKind());
6621 Record.AddSourceLocation(C->getLParenLoc());
6622 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
6623}
Alexey Bataevb6e70842019-12-16 15:54:17 -05006624
6625void OMPClauseWriter::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
6626 Record.push_back(C->varlist_size());
6627 Record.AddSourceLocation(C->getLParenLoc());
6628 for (auto *VE : C->varlists())
6629 Record.AddStmt(VE);
Alexey Bataev0860db92019-12-19 10:01:10 -05006630 for (auto *E : C->private_refs())
6631 Record.AddStmt(E);
Alexey Bataevb6e70842019-12-16 15:54:17 -05006632}
Alexey Bataevcb8e6912020-01-31 16:09:26 -05006633
Alexey Bataev06dea732020-03-20 09:41:22 -04006634void OMPClauseWriter::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
6635 Record.push_back(C->varlist_size());
6636 Record.AddSourceLocation(C->getLParenLoc());
6637 for (auto *VE : C->varlists())
6638 Record.AddStmt(VE);
6639}
6640
Alexey Bataev63828a32020-03-23 10:41:08 -04006641void OMPClauseWriter::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
6642 Record.push_back(C->varlist_size());
6643 Record.AddSourceLocation(C->getLParenLoc());
6644 for (auto *VE : C->varlists())
6645 Record.AddStmt(VE);
6646}
6647
Alexey Bataevcb8e6912020-01-31 16:09:26 -05006648void OMPClauseWriter::VisitOMPOrderClause(OMPOrderClause *C) {
6649 Record.writeEnum(C->getKind());
6650 Record.AddSourceLocation(C->getLParenLoc());
6651 Record.AddSourceLocation(C->getKindKwLoc());
6652}
6653
Johannes Doerfert55eca282020-03-13 23:42:05 -05006654void ASTRecordWriter::writeOMPTraitInfo(const OMPTraitInfo *TI) {
6655 writeUInt32(TI->Sets.size());
6656 for (const auto &Set : TI->Sets) {
Johannes Doerfert1228d422019-12-19 20:42:12 -06006657 writeEnum(Set.Kind);
6658 writeUInt32(Set.Selectors.size());
6659 for (const auto &Selector : Set.Selectors) {
6660 writeEnum(Selector.Kind);
6661 writeBool(Selector.ScoreOrCondition);
6662 if (Selector.ScoreOrCondition)
6663 writeExprRef(Selector.ScoreOrCondition);
6664 writeUInt32(Selector.Properties.size());
6665 for (const auto &Property : Selector.Properties)
6666 writeEnum(Property.Kind);
6667 }
6668 }
6669}