blob: 4c186629b59b8b7e0930134e8f66d10b6cde91c9 [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
Sebastian Redl1914c6f2010-08-18 23:56:37 +000013#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000014#include "ASTCommon.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000015#include "ASTReaderInternals.h"
16#include "MultiOnDiskHashTable.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000018#include "clang/AST/ASTUnresolvedSet.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000019#include "clang/AST/Attr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/Decl.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000021#include "clang/AST/DeclBase.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000022#include "clang/AST/DeclCXX.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000023#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000024#include "clang/AST/DeclFriend.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000025#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/AST/DeclTemplate.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000027#include "clang/AST/DeclarationName.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000028#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000029#include "clang/AST/ExprCXX.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000030#include "clang/AST/LambdaCapture.h"
31#include "clang/AST/NestedNameSpecifier.h"
32#include "clang/AST/RawCommentList.h"
33#include "clang/AST/TemplateName.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000034#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000035#include "clang/AST/TypeLocVisitor.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000036#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000037#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000038#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000039#include "clang/Basic/FileSystemOptions.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000040#include "clang/Basic/IdentifierTable.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000041#include "clang/Basic/LLVM.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000042#include "clang/Basic/Lambda.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000043#include "clang/Basic/LangOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000044#include "clang/Basic/Module.h"
45#include "clang/Basic/ObjCRuntime.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000046#include "clang/Basic/OpenCLOptions.h"
47#include "clang/Basic/SourceLocation.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000048#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000049#include "clang/Basic/SourceManagerInternals.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000050#include "clang/Basic/Specifiers.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000051#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000052#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000053#include "clang/Basic/Version.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000054#include "clang/Lex/HeaderSearch.h"
55#include "clang/Lex/HeaderSearchOptions.h"
56#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000057#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000058#include "clang/Lex/PreprocessingRecord.h"
59#include "clang/Lex/Preprocessor.h"
60#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000061#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000062#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000063#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000064#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000065#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000066#include "clang/Serialization/ASTReader.h"
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000067#include "clang/Serialization/InMemoryModuleCache.h"
Duncan P. N. Exon Smithf7170d12019-11-21 18:49:05 -080068#include "clang/Serialization/ModuleFile.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000069#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000070#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000071#include "llvm/ADT/APFloat.h"
72#include "llvm/ADT/APInt.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000073#include "llvm/ADT/APSInt.h"
74#include "llvm/ADT/ArrayRef.h"
75#include "llvm/ADT/DenseMap.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000076#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000077#include "llvm/ADT/Optional.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000078#include "llvm/ADT/PointerIntPair.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000079#include "llvm/ADT/STLExtras.h"
Ilya Biryukov45643102018-07-09 11:33:23 +000080#include "llvm/ADT/ScopeExit.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000081#include "llvm/ADT/SmallSet.h"
82#include "llvm/ADT/SmallString.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000083#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000084#include "llvm/ADT/StringMap.h"
85#include "llvm/ADT/StringRef.h"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +000086#include "llvm/Bitstream/BitCodes.h"
87#include "llvm/Bitstream/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000088#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000089#include "llvm/Support/Compression.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000090#include "llvm/Support/DJB.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000091#include "llvm/Support/Endian.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000092#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000093#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000094#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000095#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000096#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000097#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000098#include "llvm/Support/SHA1.h"
Pavel Labathd8c62902018-06-11 10:28:04 +000099#include "llvm/Support/VersionTuple.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000100#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +0000101#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000102#include <cassert>
103#include <cstdint>
104#include <cstdlib>
105#include <cstring>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000106#include <ctime>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000107#include <deque>
108#include <limits>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000109#include <memory>
110#include <queue>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000111#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +0000112#include <utility>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000113#include <vector>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000114
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +0000116using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117
Sebastian Redl3df5a082010-07-30 17:03:48 +0000118template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +0000119static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000120 if (v.empty()) return StringRef();
121 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000122 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000123}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000124
125template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000126static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000127 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000128 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000129}
130
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131//===----------------------------------------------------------------------===//
132// Type serialization
133//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000134
Richard Smith290d8012016-04-06 17:06:00 +0000135namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000136
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000137 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000138 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000139 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000141 /// Type code that corresponds to the record generated.
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000142 TypeCode Code = static_cast<TypeCode>(0);
143
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000144 /// Abbreviation to use for the record, if any.
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000145 unsigned AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146
Richard Smith290d8012016-04-06 17:06:00 +0000147 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000148 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000149 : Writer(Writer), Record(Writer, Record) {}
Richard Smith290d8012016-04-06 17:06:00 +0000150
151 uint64_t Emit() {
152 return Record.Emit(Code, AbbrevToUse);
153 }
154
155 void Visit(QualType T) {
156 if (T.hasLocalNonFastQualifiers()) {
157 Qualifiers Qs = T.getLocalQualifiers();
158 Record.AddTypeRef(T.getLocalUnqualifiedType());
159 Record.push_back(Qs.getAsOpaqueValue());
160 Code = TYPE_EXT_QUAL;
161 AbbrevToUse = Writer.TypeExtQualAbbrev;
162 } else {
163 switch (T->getTypeClass()) {
164 // For all of the concrete, non-dependent types, call the
165 // appropriate visitor function.
166#define TYPE(Class, Base) \
167 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
168#define ABSTRACT_TYPE(Class, Base)
John McCall36b12a82019-10-02 06:35:23 +0000169#include "clang/AST/TypeNodes.inc"
Richard Smith290d8012016-04-06 17:06:00 +0000170 }
171 }
172 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173
174 void VisitArrayType(const ArrayType *T);
175 void VisitFunctionType(const FunctionType *T);
176 void VisitTagType(const TagType *T);
177
178#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
179#define ABSTRACT_TYPE(Class, Base)
John McCall36b12a82019-10-02 06:35:23 +0000180#include "clang/AST/TypeNodes.inc"
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000182
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000183} // namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000186 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000187}
188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000190 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000191 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192}
193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000195 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Reid Kleckner8a365022013-06-24 17:51:48 +0000199void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000200 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000201 Code = TYPE_DECAYED;
202}
203
Reid Kleckner0503a872013-12-05 01:23:43 +0000204void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000205 Record.AddTypeRef(T->getOriginalType());
206 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000207 Code = TYPE_ADJUSTED;
208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000211 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000212 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213}
214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000216 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000217 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000218 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000222 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000227 Record.AddTypeRef(T->getPointeeType());
228 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000229 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230}
231
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000233 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000235 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236}
237
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000240 Record.AddAPInt(T->getSize());
Richard Smith772e2662019-10-04 01:25:59 +0000241 Record.AddStmt(const_cast<Expr*>(T->getSizeExpr()));
Sebastian Redl539c5062010-08-18 23:57:32 +0000242 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243}
244
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000245void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248}
249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000252 Record.AddSourceLocation(T->getLBracketLoc());
253 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000254 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000255 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256}
257
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000258void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000259 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000261 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000266 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000267 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000268}
269
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000271 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000272 FunctionType::ExtInfo C = T->getExtInfo();
273 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000274 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000275 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000276 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000277 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000278 Record.push_back(C.getProducesResult());
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000279 Record.push_back(C.getNoCallerSavedRegs());
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000280 Record.push_back(C.getNoCfCheck());
Richard Smith01b2cb42014-07-26 06:37:51 +0000281
282 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
283 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000284}
285
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000287 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000288 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000289}
290
Richard Smith290d8012016-04-06 17:06:00 +0000291static void addExceptionSpec(const FunctionProtoType *T,
292 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000293 Record.push_back(T->getExceptionSpecType());
294 if (T->getExceptionSpecType() == EST_Dynamic) {
295 Record.push_back(T->getNumExceptions());
296 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000297 Record.AddTypeRef(T->getExceptionType(I));
Richard Smitheaf11ad2018-05-03 03:58:32 +0000298 } else if (isComputedNoexcept(T->getExceptionSpecType())) {
Richard Smith290d8012016-04-06 17:06:00 +0000299 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000300 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000301 Record.AddDeclRef(T->getExceptionSpecDecl());
302 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000303 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000304 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000305 }
Richard Smith564417a2014-03-20 21:47:22 +0000306}
307
308void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
309 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000310
Richard Smith564417a2014-03-20 21:47:22 +0000311 Record.push_back(T->isVariadic());
312 Record.push_back(T->hasTrailingReturn());
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000313 Record.push_back(T->getMethodQuals().getAsOpaqueValue());
Richard Smith564417a2014-03-20 21:47:22 +0000314 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000315 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000316
317 Record.push_back(T->getNumParams());
318 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000319 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000320
John McCall18afab72016-03-01 00:49:02 +0000321 if (T->hasExtParameterInfos()) {
322 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
323 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
324 }
325
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000326 if (T->isVariadic() || T->hasTrailingReturn() || T->getMethodQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000327 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
328 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000329 AbbrevToUse = 0;
330
Sebastian Redl539c5062010-08-18 23:57:32 +0000331 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000332}
333
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000334void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000335 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000337}
John McCallb96ec562009-12-04 22:46:56 +0000338
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000339void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000340 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000341 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000342 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000343 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000344}
345
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000347 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000352 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000354}
355
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000357 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000358 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000359 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000360}
361
Alexis Hunte852b102011-05-24 22:41:36 +0000362void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000363 Record.AddTypeRef(T->getBaseType());
364 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000365 Record.push_back(T->getUTTKind());
366 Code = TYPE_UNARY_TRANSFORM;
367}
368
Richard Smith30482bc2011-02-20 03:19:35 +0000369void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000370 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000371 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000372 if (T->getDeducedType().isNull())
Richard Smithb2997f52019-05-21 20:10:50 +0000373 Record.push_back(T->containsUnexpandedParameterPack() ? 2 :
374 T->isDependentType() ? 1 : 0);
Richard Smith30482bc2011-02-20 03:19:35 +0000375 Code = TYPE_AUTO;
376}
377
Richard Smith600b5262017-01-26 20:40:47 +0000378void ASTTypeWriter::VisitDeducedTemplateSpecializationType(
379 const DeducedTemplateSpecializationType *T) {
380 Record.AddTemplateName(T->getTemplateName());
381 Record.AddTypeRef(T->getDeducedType());
382 if (T->getDeducedType().isNull())
383 Record.push_back(T->isDependentType());
384 Code = TYPE_DEDUCED_TEMPLATE_SPECIALIZATION;
385}
386
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000387void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000388 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000389 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000390 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000391 "Cannot serialize in the middle of a type definition");
392}
393
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000394void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000395 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000396 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000397}
398
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000399void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000400 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000401 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000402}
403
John McCall81904512011-01-06 01:58:22 +0000404void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000405 Record.AddTypeRef(T->getModifiedType());
406 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000407 Record.push_back(T->getAttrKind());
408 Code = TYPE_ATTRIBUTED;
409}
410
Mike Stump11289f42009-09-09 15:08:12 +0000411void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000412ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000413 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000414 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
415 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000416 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000417}
418
419void
Douglas Gregorada4b792011-01-14 02:55:32 +0000420ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
421 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000422 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
423 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000424 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
425}
426
427void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000428ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000429 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000430 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000431 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000432 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000433 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000434 Record.AddTemplateArgument(ArgI);
435 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
436 : T->isCanonicalUnqualified()
437 ? QualType()
438 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000439 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000440}
441
442void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000443ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000444 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000445 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000446 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000447 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000448}
449
450void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000451ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000452 const DependentSizedExtVectorType *T) {
Alex Lorenz00aee432017-03-22 10:04:48 +0000453 Record.AddTypeRef(T->getElementType());
454 Record.AddStmt(T->getSizeExpr());
455 Record.AddSourceLocation(T->getAttributeLoc());
456 Code = TYPE_DEPENDENT_SIZED_EXT_VECTOR;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000457}
458
Erich Keanef702b022018-07-13 19:46:04 +0000459void ASTTypeWriter::VisitDependentVectorType(const DependentVectorType *T) {
460 Record.AddTypeRef(T->getElementType());
461 Record.AddStmt(const_cast<Expr*>(T->getSizeExpr()));
462 Record.AddSourceLocation(T->getAttributeLoc());
463 Record.push_back(T->getVectorKind());
464 Code = TYPE_DEPENDENT_SIZED_VECTOR;
465}
466
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000467void
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000468ASTTypeWriter::VisitDependentAddressSpaceType(
469 const DependentAddressSpaceType *T) {
470 Record.AddTypeRef(T->getPointeeType());
471 Record.AddStmt(T->getAddrSpaceExpr());
472 Record.AddSourceLocation(T->getAttributeLoc());
473 Code = TYPE_DEPENDENT_ADDRESS_SPACE;
474}
475
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000476void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000477ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000478 Record.push_back(T->getDepth());
479 Record.push_back(T->getIndex());
480 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000481 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000482 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000483}
484
485void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000486ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000487 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000488 Record.AddNestedNameSpecifier(T->getQualifier());
489 Record.AddIdentifierRef(T->getIdentifier());
490 Record.AddTypeRef(
491 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000492 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000493}
494
495void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000496ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000497 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000498 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000499 Record.AddNestedNameSpecifier(T->getQualifier());
500 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000501 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000502 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000503 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000504 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000505}
506
Douglas Gregord2fa7662010-12-20 02:24:11 +0000507void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000508 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000509 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000510 Record.push_back(*NumExpansions + 1);
511 else
512 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000513 Code = TYPE_PACK_EXPANSION;
514}
515
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000516void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000517 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000518 Code = TYPE_PAREN;
519}
520
Leonard Chanc72aaf62019-05-07 03:20:17 +0000521void ASTTypeWriter::VisitMacroQualifiedType(const MacroQualifiedType *T) {
522 Record.AddTypeRef(T->getUnderlyingType());
523 Record.AddIdentifierRef(T->getMacroIdentifier());
524 Code = TYPE_MACRO_QUALIFIED;
525}
526
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000527void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000528 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000529 Record.AddNestedNameSpecifier(T->getQualifier());
530 Record.AddTypeRef(T->getNamedType());
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000531 Record.AddDeclRef(T->getOwnedTagDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000532 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000533}
534
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000535void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000536 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
537 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000538 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000539}
540
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000541void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000542 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000543 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000544}
545
Manman Rene6be26c2016-09-13 17:25:08 +0000546void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
547 Record.AddDeclRef(T->getDecl());
548 Record.push_back(T->getNumProtocols());
549 for (const auto *I : T->quals())
550 Record.AddDeclRef(I);
551 Code = TYPE_OBJC_TYPE_PARAM;
552}
553
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000554void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000555 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000556 Record.push_back(T->getTypeArgsAsWritten().size());
557 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000558 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000559 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000560 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000561 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000562 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000563 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000564}
565
Steve Narofffb4330f2009-06-17 22:40:22 +0000566void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000567ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000568 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000569 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000570}
571
Eli Friedman0dfb8892011-10-06 23:00:33 +0000572void
573ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000574 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000575 Code = TYPE_ATOMIC;
576}
577
Xiuli Pan9c14e282016-01-09 12:53:17 +0000578void
579ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000580 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000581 Record.push_back(T->isReadOnly());
582 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000583}
584
John McCall8f115c62009-10-16 21:56:05 +0000585namespace {
586
587class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000588 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000589
590public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000591 TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {}
John McCall8f115c62009-10-16 21:56:05 +0000592
John McCall17001972009-10-18 01:05:36 +0000593#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000594#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000595 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000596#include "clang/AST/TypeLocNodes.def"
597
John McCall17001972009-10-18 01:05:36 +0000598 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
599 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000600};
601
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000602} // namespace
John McCall8f115c62009-10-16 21:56:05 +0000603
John McCall17001972009-10-18 01:05:36 +0000604void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
605 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000606}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000607
John McCall17001972009-10-18 01:05:36 +0000608void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000609 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000610 if (TL.needsExtraLocalData()) {
Faisal Vali090da2d2018-01-01 18:23:28 +0000611 Record.push_back(TL.getWrittenTypeSpec());
612 Record.push_back(TL.getWrittenSignSpec());
613 Record.push_back(TL.getWrittenWidthSpec());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000614 Record.push_back(TL.hasModeAttr());
615 }
John McCall8f115c62009-10-16 21:56:05 +0000616}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000617
John McCall17001972009-10-18 01:05:36 +0000618void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000619 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000620}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000621
John McCall17001972009-10-18 01:05:36 +0000622void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000623 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000624}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000625
Reid Kleckner8a365022013-06-24 17:51:48 +0000626void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
627 // nothing to do
628}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000629
Reid Kleckner0503a872013-12-05 01:23:43 +0000630void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
631 // nothing to do
632}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000633
John McCall17001972009-10-18 01:05:36 +0000634void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000635 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000636}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000637
John McCall17001972009-10-18 01:05:36 +0000638void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000639 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000640}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000641
John McCall17001972009-10-18 01:05:36 +0000642void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000643 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000644}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000645
John McCall17001972009-10-18 01:05:36 +0000646void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000647 Record.AddSourceLocation(TL.getStarLoc());
648 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000649}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000650
John McCall17001972009-10-18 01:05:36 +0000651void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000652 Record.AddSourceLocation(TL.getLBracketLoc());
653 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000654 Record.push_back(TL.getSizeExpr() ? 1 : 0);
655 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000656 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000657}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000658
John McCall17001972009-10-18 01:05:36 +0000659void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
660 VisitArrayTypeLoc(TL);
661}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000662
John McCall17001972009-10-18 01:05:36 +0000663void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
664 VisitArrayTypeLoc(TL);
665}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000666
John McCall17001972009-10-18 01:05:36 +0000667void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
668 VisitArrayTypeLoc(TL);
669}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000670
John McCall17001972009-10-18 01:05:36 +0000671void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
672 DependentSizedArrayTypeLoc TL) {
673 VisitArrayTypeLoc(TL);
674}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000675
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000676void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
677 DependentAddressSpaceTypeLoc TL) {
678 Record.AddSourceLocation(TL.getAttrNameLoc());
679 SourceRange range = TL.getAttrOperandParensRange();
680 Record.AddSourceLocation(range.getBegin());
681 Record.AddSourceLocation(range.getEnd());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000682 Record.AddStmt(TL.getAttrExprOperand());
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000683}
684
John McCall17001972009-10-18 01:05:36 +0000685void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
686 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000687 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000688}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000689
John McCall17001972009-10-18 01:05:36 +0000690void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000691 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000692}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000693
Erich Keanef702b022018-07-13 19:46:04 +0000694void TypeLocWriter::VisitDependentVectorTypeLoc(
695 DependentVectorTypeLoc TL) {
696 Record.AddSourceLocation(TL.getNameLoc());
697}
698
John McCall17001972009-10-18 01:05:36 +0000699void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000700 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000701}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000702
John McCall17001972009-10-18 01:05:36 +0000703void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000704 Record.AddSourceLocation(TL.getLocalRangeBegin());
705 Record.AddSourceLocation(TL.getLParenLoc());
706 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000707 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000708 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000709 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000710 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000711}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000712
John McCall17001972009-10-18 01:05:36 +0000713void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
714 VisitFunctionTypeLoc(TL);
715}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000716
John McCall17001972009-10-18 01:05:36 +0000717void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
718 VisitFunctionTypeLoc(TL);
719}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000720
John McCallb96ec562009-12-04 22:46:56 +0000721void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000722 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000723}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000724
John McCall17001972009-10-18 01:05:36 +0000725void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000726 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000727}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000728
Manman Rene6be26c2016-09-13 17:25:08 +0000729void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
730 if (TL.getNumProtocols()) {
731 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
732 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
733 }
734 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
735 Record.AddSourceLocation(TL.getProtocolLoc(i));
736}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000737
John McCall17001972009-10-18 01:05:36 +0000738void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000739 Record.AddSourceLocation(TL.getTypeofLoc());
740 Record.AddSourceLocation(TL.getLParenLoc());
741 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000742}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000743
John McCall17001972009-10-18 01:05:36 +0000744void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000745 Record.AddSourceLocation(TL.getTypeofLoc());
746 Record.AddSourceLocation(TL.getLParenLoc());
747 Record.AddSourceLocation(TL.getRParenLoc());
748 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000749}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000750
John McCall17001972009-10-18 01:05:36 +0000751void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000752 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000753}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000754
Alexis Hunte852b102011-05-24 22:41:36 +0000755void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000756 Record.AddSourceLocation(TL.getKWLoc());
757 Record.AddSourceLocation(TL.getLParenLoc());
758 Record.AddSourceLocation(TL.getRParenLoc());
759 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000760}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000761
Richard Smith30482bc2011-02-20 03:19:35 +0000762void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000763 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000764}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000765
Richard Smith600b5262017-01-26 20:40:47 +0000766void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
767 DeducedTemplateSpecializationTypeLoc TL) {
768 Record.AddSourceLocation(TL.getTemplateNameLoc());
769}
770
John McCall17001972009-10-18 01:05:36 +0000771void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000772 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000773}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000774
John McCall17001972009-10-18 01:05:36 +0000775void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000776 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000777}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000778
John McCall81904512011-01-06 01:58:22 +0000779void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smithe43e2b32018-08-20 21:47:29 +0000780 Record.AddAttr(TL.getAttr());
John McCall81904512011-01-06 01:58:22 +0000781}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000782
John McCall17001972009-10-18 01:05:36 +0000783void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000784 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000785}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000786
John McCallcebee162009-10-18 09:09:24 +0000787void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
788 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000789 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000790}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000791
Douglas Gregorada4b792011-01-14 02:55:32 +0000792void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
793 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000794 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000795}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000796
John McCall17001972009-10-18 01:05:36 +0000797void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
798 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000799 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
800 Record.AddSourceLocation(TL.getTemplateNameLoc());
801 Record.AddSourceLocation(TL.getLAngleLoc());
802 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000803 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000804 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
805 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000806}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000807
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000808void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000809 Record.AddSourceLocation(TL.getLParenLoc());
810 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000811}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000812
Leonard Chanc72aaf62019-05-07 03:20:17 +0000813void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
814 Record.AddSourceLocation(TL.getExpansionLoc());
815}
816
Abramo Bagnara6150c882010-05-11 21:36:43 +0000817void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000818 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
819 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000820}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000821
John McCalle78aac42010-03-10 03:28:59 +0000822void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000823 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000824}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000825
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000826void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000827 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
828 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
829 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000830}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000831
John McCallc392f372010-06-11 00:33:02 +0000832void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
833 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000834 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
835 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
836 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
837 Record.AddSourceLocation(TL.getTemplateNameLoc());
838 Record.AddSourceLocation(TL.getLAngleLoc());
839 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000840 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000841 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
842 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000843}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000844
Douglas Gregord2fa7662010-12-20 02:24:11 +0000845void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000846 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000847}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000848
John McCall17001972009-10-18 01:05:36 +0000849void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000850 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000851}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000852
John McCall8b07ec22010-05-15 11:32:37 +0000853void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
854 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000855 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
856 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000857 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000858 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
859 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
860 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000861 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000862 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000863}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000864
John McCallfc93cf92009-10-22 22:37:11 +0000865void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000866 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000867}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000868
Eli Friedman0dfb8892011-10-06 23:00:33 +0000869void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000870 Record.AddSourceLocation(TL.getKWLoc());
871 Record.AddSourceLocation(TL.getLParenLoc());
872 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000873}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000874
Xiuli Pan9c14e282016-01-09 12:53:17 +0000875void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000876 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000877}
John McCall8f115c62009-10-16 21:56:05 +0000878
Richard Smith01b2cb42014-07-26 06:37:51 +0000879void ASTWriter::WriteTypeAbbrevs() {
880 using namespace llvm;
881
David Blaikieb44f0bf2017-01-04 22:36:43 +0000882 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000883
884 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000885 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000886 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
887 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
888 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000889 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000890
891 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000892 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000893 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
894 // FunctionType
895 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
896 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
897 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
898 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
899 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
900 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000901 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000902 Abv->Add(BitCodeAbbrevOp(0)); // NoCfCheck
Richard Smith01b2cb42014-07-26 06:37:51 +0000903 // FunctionProtoType
904 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
905 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
906 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
907 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
908 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
909 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
910 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
911 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000912 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000913}
914
Chris Lattner19cea4e2009-04-22 05:57:30 +0000915//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000916// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000917//===----------------------------------------------------------------------===//
918
Chris Lattner28fa4e62009-04-26 22:26:21 +0000919static void EmitBlockID(unsigned ID, const char *Name,
920 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000921 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000922 Record.clear();
923 Record.push_back(ID);
924 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
925
926 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000927 if (!Name || Name[0] == 0)
928 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000929 Record.clear();
930 while (*Name)
931 Record.push_back(*Name++);
932 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
933}
934
935static void EmitRecordID(unsigned ID, const char *Name,
936 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000937 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000938 Record.clear();
939 Record.push_back(ID);
940 while (*Name)
941 Record.push_back(*Name++);
942 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000943}
944
945static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000946 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000947#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000948 RECORD(STMT_STOP);
949 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000950 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000951 RECORD(STMT_NULL);
952 RECORD(STMT_COMPOUND);
953 RECORD(STMT_CASE);
954 RECORD(STMT_DEFAULT);
955 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000956 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000957 RECORD(STMT_IF);
958 RECORD(STMT_SWITCH);
959 RECORD(STMT_WHILE);
960 RECORD(STMT_DO);
961 RECORD(STMT_FOR);
962 RECORD(STMT_GOTO);
963 RECORD(STMT_INDIRECT_GOTO);
964 RECORD(STMT_CONTINUE);
965 RECORD(STMT_BREAK);
966 RECORD(STMT_RETURN);
967 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000968 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000969 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000970 RECORD(EXPR_PREDEFINED);
971 RECORD(EXPR_DECL_REF);
972 RECORD(EXPR_INTEGER_LITERAL);
973 RECORD(EXPR_FLOATING_LITERAL);
974 RECORD(EXPR_IMAGINARY_LITERAL);
975 RECORD(EXPR_STRING_LITERAL);
976 RECORD(EXPR_CHARACTER_LITERAL);
977 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000978 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000979 RECORD(EXPR_UNARY_OPERATOR);
980 RECORD(EXPR_SIZEOF_ALIGN_OF);
981 RECORD(EXPR_ARRAY_SUBSCRIPT);
982 RECORD(EXPR_CALL);
983 RECORD(EXPR_MEMBER);
984 RECORD(EXPR_BINARY_OPERATOR);
985 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
986 RECORD(EXPR_CONDITIONAL_OPERATOR);
987 RECORD(EXPR_IMPLICIT_CAST);
988 RECORD(EXPR_CSTYLE_CAST);
989 RECORD(EXPR_COMPOUND_LITERAL);
990 RECORD(EXPR_EXT_VECTOR_ELEMENT);
991 RECORD(EXPR_INIT_LIST);
992 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000993 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000994 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000995 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000996 RECORD(EXPR_VA_ARG);
997 RECORD(EXPR_ADDR_LABEL);
998 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000999 RECORD(EXPR_CHOOSE);
1000 RECORD(EXPR_GNU_NULL);
1001 RECORD(EXPR_SHUFFLE_VECTOR);
1002 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +00001003 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001004 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +00001005 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001006 RECORD(EXPR_OBJC_ARRAY_LITERAL);
1007 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001008 RECORD(EXPR_OBJC_ENCODE);
1009 RECORD(EXPR_OBJC_SELECTOR_EXPR);
1010 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
1011 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
1012 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
1013 RECORD(EXPR_OBJC_KVC_REF_EXPR);
1014 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001015 RECORD(STMT_OBJC_FOR_COLLECTION);
1016 RECORD(STMT_OBJC_CATCH);
1017 RECORD(STMT_OBJC_FINALLY);
1018 RECORD(STMT_OBJC_AT_TRY);
1019 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
1020 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001021 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001022 RECORD(STMT_CXX_CATCH);
1023 RECORD(STMT_CXX_TRY);
1024 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001025 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001026 RECORD(EXPR_CXX_MEMBER_CALL);
Richard Smith778dc0f2019-10-19 00:04:38 +00001027 RECORD(EXPR_CXX_REWRITTEN_BINARY_OPERATOR);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001028 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001029 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001030 RECORD(EXPR_CXX_STATIC_CAST);
1031 RECORD(EXPR_CXX_DYNAMIC_CAST);
1032 RECORD(EXPR_CXX_REINTERPRET_CAST);
1033 RECORD(EXPR_CXX_CONST_CAST);
1034 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +00001035 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +00001036 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001037 RECORD(EXPR_CXX_BOOL_LITERAL);
1038 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001039 RECORD(EXPR_CXX_TYPEID_EXPR);
1040 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001041 RECORD(EXPR_CXX_THIS);
1042 RECORD(EXPR_CXX_THROW);
1043 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +00001044 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001045 RECORD(EXPR_CXX_BIND_TEMPORARY);
1046 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
1047 RECORD(EXPR_CXX_NEW);
1048 RECORD(EXPR_CXX_DELETE);
1049 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
1050 RECORD(EXPR_EXPR_WITH_CLEANUPS);
1051 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
1052 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
1053 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
1054 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
1055 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +00001056 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001057 RECORD(EXPR_CXX_NOEXCEPT);
1058 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +00001059 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
1060 RECORD(EXPR_TYPE_TRAIT);
1061 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001062 RECORD(EXPR_PACK_EXPANSION);
1063 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001064 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001065 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001066 RECORD(EXPR_FUNCTION_PARM_PACK);
1067 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001068 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001069 RECORD(EXPR_CXX_UUIDOF_EXPR);
1070 RECORD(EXPR_CXX_UUIDOF_TYPE);
1071 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001072#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001073}
Mike Stump11289f42009-09-09 15:08:12 +00001074
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001075void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001076 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001077 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001078
Sebastian Redl539c5062010-08-18 23:57:32 +00001079#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1080#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001081
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001082 // Control Block.
1083 BLOCK(CONTROL_BLOCK);
1084 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001085 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001086 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001087 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001088 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001089 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001090 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001091 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001092 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001093
1094 BLOCK(OPTIONS_BLOCK);
1095 RECORD(LANGUAGE_OPTIONS);
1096 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001097 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001098 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001099 RECORD(PREPROCESSOR_OPTIONS);
1100
Douglas Gregor108cb222012-10-19 00:45:00 +00001101 BLOCK(INPUT_FILES_BLOCK);
1102 RECORD(INPUT_FILE);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001103 RECORD(INPUT_FILE_HASH);
Douglas Gregor108cb222012-10-19 00:45:00 +00001104
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001105 // AST Top-Level Block.
1106 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001107 RECORD(TYPE_OFFSET);
1108 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001109 RECORD(IDENTIFIER_OFFSET);
1110 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001111 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001112 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001113 RECORD(SPECIAL_TYPES);
1114 RECORD(STATISTICS);
1115 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001116 RECORD(SELECTOR_OFFSETS);
1117 RECORD(METHOD_POOL);
1118 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 RECORD(SOURCE_LOCATION_OFFSETS);
1120 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001121 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001122 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001123 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001124 RECORD(VTABLE_USES);
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00001125 RECORD(PPD_SKIPPED_RANGES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001126 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001127 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001128 RECORD(SEMA_DECL_REFS);
1129 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1130 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001131 RECORD(UPDATE_VISIBLE);
1132 RECORD(DECL_UPDATE_OFFSETS);
1133 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001134 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001135 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001136 RECORD(FP_PRAGMA_OPTIONS);
1137 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001138 RECORD(OPENCL_EXTENSION_TYPES);
1139 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001140 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001141 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001142 RECORD(MODULE_OFFSET_MAP);
1143 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001144 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001145 RECORD(FILE_SORTED_DECLS);
1146 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001147 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001148 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001149 RECORD(INTERESTING_IDENTIFIERS);
1150 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001151 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001152 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001153 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001154 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001155 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001156 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001157 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00001158 RECORD(PP_CONDITIONAL_STACK);
Douglas Gregor358cd442012-01-15 16:58:34 +00001159
Chris Lattner28fa4e62009-04-26 22:26:21 +00001160 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001161 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001162 RECORD(SM_SLOC_FILE_ENTRY);
1163 RECORD(SM_SLOC_BUFFER_ENTRY);
1164 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001165 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001166 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattner28fa4e62009-04-26 22:26:21 +00001168 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001169 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001170 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001171 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001172 RECORD(PP_MACRO_OBJECT_LIKE);
1173 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001174 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001175
Richard Smithfa715652015-08-31 22:43:10 +00001176 // Submodule Block.
1177 BLOCK(SUBMODULE_BLOCK);
1178 RECORD(SUBMODULE_METADATA);
1179 RECORD(SUBMODULE_DEFINITION);
1180 RECORD(SUBMODULE_UMBRELLA_HEADER);
1181 RECORD(SUBMODULE_HEADER);
1182 RECORD(SUBMODULE_TOPHEADER);
1183 RECORD(SUBMODULE_UMBRELLA_DIR);
1184 RECORD(SUBMODULE_IMPORTS);
1185 RECORD(SUBMODULE_EXPORTS);
1186 RECORD(SUBMODULE_REQUIRES);
1187 RECORD(SUBMODULE_EXCLUDED_HEADER);
1188 RECORD(SUBMODULE_LINK_LIBRARY);
1189 RECORD(SUBMODULE_CONFIG_MACRO);
1190 RECORD(SUBMODULE_CONFLICT);
1191 RECORD(SUBMODULE_PRIVATE_HEADER);
1192 RECORD(SUBMODULE_TEXTUAL_HEADER);
1193 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001194 RECORD(SUBMODULE_INITIALIZERS);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00001195 RECORD(SUBMODULE_EXPORT_AS);
Richard Smithfa715652015-08-31 22:43:10 +00001196
1197 // Comments Block.
1198 BLOCK(COMMENTS_BLOCK);
1199 RECORD(COMMENTS_RAW_COMMENT);
1200
Douglas Gregor12bfa382009-10-17 00:13:19 +00001201 // Decls and Types block.
1202 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001203 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001204 RECORD(TYPE_COMPLEX);
1205 RECORD(TYPE_POINTER);
1206 RECORD(TYPE_BLOCK_POINTER);
1207 RECORD(TYPE_LVALUE_REFERENCE);
1208 RECORD(TYPE_RVALUE_REFERENCE);
1209 RECORD(TYPE_MEMBER_POINTER);
1210 RECORD(TYPE_CONSTANT_ARRAY);
1211 RECORD(TYPE_INCOMPLETE_ARRAY);
1212 RECORD(TYPE_VARIABLE_ARRAY);
1213 RECORD(TYPE_VECTOR);
1214 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001215 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001216 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001217 RECORD(TYPE_TYPEDEF);
1218 RECORD(TYPE_TYPEOF_EXPR);
1219 RECORD(TYPE_TYPEOF);
1220 RECORD(TYPE_RECORD);
1221 RECORD(TYPE_ENUM);
1222 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001223 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001224 RECORD(TYPE_DECLTYPE);
1225 RECORD(TYPE_ELABORATED);
1226 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1227 RECORD(TYPE_UNRESOLVED_USING);
1228 RECORD(TYPE_INJECTED_CLASS_NAME);
1229 RECORD(TYPE_OBJC_OBJECT);
1230 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1231 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1232 RECORD(TYPE_DEPENDENT_NAME);
1233 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1234 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1235 RECORD(TYPE_PAREN);
Leonard Chanc72aaf62019-05-07 03:20:17 +00001236 RECORD(TYPE_MACRO_QUALIFIED);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001237 RECORD(TYPE_PACK_EXPANSION);
1238 RECORD(TYPE_ATTRIBUTED);
1239 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001240 RECORD(TYPE_AUTO);
1241 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001242 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001243 RECORD(TYPE_DECAYED);
1244 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001245 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001246 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001247 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001248 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001249 RECORD(DECL_ENUM);
1250 RECORD(DECL_RECORD);
1251 RECORD(DECL_ENUM_CONSTANT);
1252 RECORD(DECL_FUNCTION);
1253 RECORD(DECL_OBJC_METHOD);
1254 RECORD(DECL_OBJC_INTERFACE);
1255 RECORD(DECL_OBJC_PROTOCOL);
1256 RECORD(DECL_OBJC_IVAR);
1257 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001258 RECORD(DECL_OBJC_CATEGORY);
1259 RECORD(DECL_OBJC_CATEGORY_IMPL);
1260 RECORD(DECL_OBJC_IMPLEMENTATION);
1261 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1262 RECORD(DECL_OBJC_PROPERTY);
1263 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001264 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001265 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001266 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001267 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001268 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001269 RECORD(DECL_FILE_SCOPE_ASM);
1270 RECORD(DECL_BLOCK);
1271 RECORD(DECL_CONTEXT_LEXICAL);
1272 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001273 RECORD(DECL_NAMESPACE);
1274 RECORD(DECL_NAMESPACE_ALIAS);
1275 RECORD(DECL_USING);
1276 RECORD(DECL_USING_SHADOW);
1277 RECORD(DECL_USING_DIRECTIVE);
1278 RECORD(DECL_UNRESOLVED_USING_VALUE);
1279 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1280 RECORD(DECL_LINKAGE_SPEC);
1281 RECORD(DECL_CXX_RECORD);
1282 RECORD(DECL_CXX_METHOD);
1283 RECORD(DECL_CXX_CONSTRUCTOR);
1284 RECORD(DECL_CXX_DESTRUCTOR);
1285 RECORD(DECL_CXX_CONVERSION);
1286 RECORD(DECL_ACCESS_SPEC);
1287 RECORD(DECL_FRIEND);
1288 RECORD(DECL_FRIEND_TEMPLATE);
1289 RECORD(DECL_CLASS_TEMPLATE);
1290 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1291 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001292 RECORD(DECL_VAR_TEMPLATE);
1293 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1294 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001295 RECORD(DECL_FUNCTION_TEMPLATE);
1296 RECORD(DECL_TEMPLATE_TYPE_PARM);
1297 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1298 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Saar Razd7aae332019-07-10 21:25:49 +00001299 RECORD(DECL_CONCEPT);
Richard Smithefc884a2016-04-13 07:41:35 +00001300 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001301 RECORD(DECL_STATIC_ASSERT);
1302 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001303 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001304 RECORD(DECL_INDIRECTFIELD);
1305 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001306 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1307 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1308 RECORD(DECL_IMPORT);
1309 RECORD(DECL_OMP_THREADPRIVATE);
1310 RECORD(DECL_EMPTY);
1311 RECORD(DECL_OBJC_TYPE_PARAM);
1312 RECORD(DECL_OMP_CAPTUREDEXPR);
1313 RECORD(DECL_PRAGMA_COMMENT);
1314 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1315 RECORD(DECL_OMP_DECLARE_REDUCTION);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001316 RECORD(DECL_OMP_ALLOCATE);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001317
Douglas Gregor03412ba2011-06-03 02:27:19 +00001318 // Statements and Exprs can occur in the Decls and Types block.
1319 AddStmtsExprs(Stream, Record);
1320
Douglas Gregor92a96f52011-02-08 21:58:10 +00001321 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001322 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001323 RECORD(PPD_MACRO_DEFINITION);
1324 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001325
1326 // Decls and Types block.
1327 BLOCK(EXTENSION_BLOCK);
1328 RECORD(EXTENSION_METADATA);
1329
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001330 BLOCK(UNHASHED_CONTROL_BLOCK);
1331 RECORD(SIGNATURE);
1332 RECORD(DIAGNOSTIC_OPTIONS);
1333 RECORD(DIAG_PRAGMA_MAPPINGS);
1334
Chris Lattner28fa4e62009-04-26 22:26:21 +00001335#undef RECORD
1336#undef BLOCK
1337 Stream.ExitBlock();
1338}
1339
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001340/// Prepares a path for being written to an AST file by converting it
Richard Smith54cc3c22014-12-11 20:50:24 +00001341/// to an absolute path and removing nested './'s.
1342///
1343/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001344static bool cleanPathForOutput(FileManager &FileMgr,
1345 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001346 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001347 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001348}
1349
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001350/// Adjusts the given filename to only write out the portion of the
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001351/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001352///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001353/// \param Filename the file name to adjust.
1354///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001355/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1356/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001357///
1358/// \returns either the original filename (if it needs no adjustment) or the
1359/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001360static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001361adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001362 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001363
Richard Smith7ed1bc92014-12-05 22:42:13 +00001364 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001365 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001366
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001367 // Verify that the filename and the system root have the same prefix.
1368 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001369 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1370 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001371 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001372
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001373 // We hit the end of the filename before we hit the end of the system root.
1374 if (!Filename[Pos])
1375 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001376
Richard Smith7ed1bc92014-12-05 22:42:13 +00001377 // If there's not a path separator at the end of the base directory nor
1378 // immediately after it, then this isn't within the base directory.
1379 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1380 if (!llvm::sys::path::is_separator(BaseDir.back()))
1381 return Filename;
1382 } else {
1383 // If the file name has a '/' at the current position, skip over the '/'.
1384 // We distinguish relative paths from absolute paths by the
1385 // absence of '/' at the beginning of relative paths.
1386 //
1387 // FIXME: This is wrong. We distinguish them by asking if the path is
1388 // absolute, which isn't the same thing. And there might be multiple '/'s
1389 // in a row. Use a better mechanism to indicate whether we have emitted an
1390 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001391 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001394 return Filename + Pos;
1395}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001396
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001397ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1398 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1399 llvm::SHA1 Hasher;
1400 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1401 auto Hash = Hasher.result();
1402
1403 // Convert to an array [5*i32].
1404 ASTFileSignature Signature;
1405 auto LShift = [&](unsigned char Val, unsigned Shift) {
1406 return (uint32_t)Val << Shift;
1407 };
1408 for (int I = 0; I != 5; ++I)
1409 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1410 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1411
1412 return Signature;
1413}
1414
1415ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1416 ASTContext &Context) {
1417 // Flush first to prepare the PCM hash (signature).
1418 Stream.FlushToWord();
1419 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1420
1421 // Enter the block and prepare to write records.
1422 RecordData Record;
1423 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1424
1425 // For implicit modules, write the hash of the PCM as its signature.
1426 ASTFileSignature Signature;
1427 if (WritingModule &&
1428 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1429 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1430 Record.append(Signature.begin(), Signature.end());
1431 Stream.EmitRecord(SIGNATURE, Record);
1432 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001433 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001434
1435 // Diagnostic options.
1436 const auto &Diags = Context.getDiagnostics();
1437 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1438#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1439#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1440 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1441#include "clang/Basic/DiagnosticOptions.def"
1442 Record.push_back(DiagOpts.Warnings.size());
1443 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1444 AddString(DiagOpts.Warnings[I], Record);
1445 Record.push_back(DiagOpts.Remarks.size());
1446 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1447 AddString(DiagOpts.Remarks[I], Record);
1448 // Note: we don't serialize the log or serialization file names, because they
1449 // are generally transient files and will almost always be overridden.
1450 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1451
1452 // Write out the diagnostic/pragma mappings.
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001453 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001454
1455 // Leave the options block.
1456 Stream.ExitBlock();
1457 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001458}
1459
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001460/// Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001461void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1462 StringRef isysroot,
1463 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001464 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001465
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001466 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001467 RecordData Record;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001468
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001469 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001470 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001471 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1472 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1473 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1474 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1475 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1476 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001477 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001478 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PCHHasObjectFile
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001479 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1480 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001481 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001482 assert((!WritingModule || isysroot.empty()) &&
1483 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001484 {
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001485 RecordData::value_type Record[] = {
1486 METADATA,
1487 VERSION_MAJOR,
1488 VERSION_MINOR,
1489 CLANG_VERSION_MAJOR,
1490 CLANG_VERSION_MINOR,
1491 !isysroot.empty(),
1492 IncludeTimestamps,
1493 Context.getLangOpts().BuildingPCHWithObjectFile,
1494 ASTHasCompilerErrors};
Mehdi Amini57a41912015-09-10 01:46:39 +00001495 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1496 getClangFullRepositoryVersion());
1497 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001498
Richard Smith8b706102017-05-31 20:56:55 +00001499 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001500 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001501 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001502 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001504 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001505 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001506 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1507 }
1508
Richard Smith7ed1bc92014-12-05 22:42:13 +00001509 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001510 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001511 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001512
1513 // If the home of the module is the current working directory, then we
1514 // want to pick up the cwd of the build process loading the module, not
1515 // our cwd, when we load this module.
1516 if (!PP.getHeaderSearchInfo()
1517 .getHeaderSearchOpts()
1518 .ModuleMapFileHomeIsCwd ||
1519 WritingModule->Directory->getName() != StringRef(".")) {
1520 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001521 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001522 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1523 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001524 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001525
Mehdi Amini57a41912015-09-10 01:46:39 +00001526 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001527 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1528 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001529
1530 // Write out all other paths relative to the base directory if possible.
1531 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1532 } else if (!isysroot.empty()) {
1533 // Write out paths relative to the sysroot if possible.
1534 BaseDirectory = isysroot;
1535 }
1536
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001537 // Module map file
Richard Smithd19389a2017-07-05 07:47:11 +00001538 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001539 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001540
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001541 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001542 AddPath(WritingModule->PresumedModuleMapFile.empty()
1543 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1544 : StringRef(WritingModule->PresumedModuleMapFile),
1545 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001546
1547 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001548 if (auto *AdditionalModMaps =
1549 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001550 Record.push_back(AdditionalModMaps->size());
1551 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001552 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001553 } else {
1554 Record.push_back(0);
1555 }
1556
1557 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001558 }
1559
Douglas Gregor112b9072012-10-18 05:31:06 +00001560 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001561 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001562 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001563 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001564
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001565 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001566 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001567 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001568 continue;
1569
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001570 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1571 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001572
1573 // If we have calculated signature, there is no need to store
1574 // the size or timestamp.
1575 Record.push_back(M.Signature ? 0 : M.File->getSize());
1576 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1577
1578 for (auto I : M.Signature)
1579 Record.push_back(I);
1580
Boris Kolpackovd30446f2017-08-31 06:26:43 +00001581 AddString(M.ModuleName, Record);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001582 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001583 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001584 Stream.EmitRecord(IMPORTS, Record);
1585 }
Mike Stump11289f42009-09-09 15:08:12 +00001586
Richard Smith0516b182015-09-08 19:40:14 +00001587 // Write the options block.
1588 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1589
Douglas Gregor112b9072012-10-18 05:31:06 +00001590 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001591 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001592 const LangOptions &LangOpts = Context.getLangOpts();
1593#define LANGOPT(Name, Bits, Default, Description) \
1594 Record.push_back(LangOpts.Name);
1595#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1596 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001597#include "clang/Basic/LangOptions.def"
1598#define SANITIZER(NAME, ID) \
1599 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001600#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001601
Ben Langmuircd98cb72015-06-23 18:20:18 +00001602 Record.push_back(LangOpts.ModuleFeatures.size());
1603 for (StringRef Feature : LangOpts.ModuleFeatures)
1604 AddString(Feature, Record);
1605
Douglas Gregor112b9072012-10-18 05:31:06 +00001606 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1607 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001608
1609 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001610
1611 // Comment options.
1612 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001613 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1614 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001615 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001616 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001617
Samuel Antaoee8fb302016-01-06 13:42:12 +00001618 // OpenMP offloading options.
1619 Record.push_back(LangOpts.OMPTargetTriples.size());
1620 for (auto &T : LangOpts.OMPTargetTriples)
1621 AddString(T.getTriple(), Record);
1622
1623 AddString(LangOpts.OMPHostIRFile, Record);
1624
Douglas Gregor112b9072012-10-18 05:31:06 +00001625 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1626
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001627 // Target options.
1628 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001629 const TargetInfo &Target = Context.getTargetInfo();
1630 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001631 AddString(TargetOpts.Triple, Record);
1632 AddString(TargetOpts.CPU, Record);
1633 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001634 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1635 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1636 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1637 }
1638 Record.push_back(TargetOpts.Features.size());
1639 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1640 AddString(TargetOpts.Features[I], Record);
1641 }
1642 Stream.EmitRecord(TARGET_OPTIONS, Record);
1643
Douglas Gregorc6317db2012-10-24 15:49:58 +00001644 // File system options.
1645 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001646 const FileSystemOptions &FSOpts =
1647 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001648 AddString(FSOpts.WorkingDir, Record);
1649 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1650
Douglas Gregor2d302362012-10-24 16:50:34 +00001651 // Header search options.
1652 Record.clear();
1653 const HeaderSearchOptions &HSOpts
1654 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1655 AddString(HSOpts.Sysroot, Record);
1656
1657 // Include entries.
1658 Record.push_back(HSOpts.UserEntries.size());
1659 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1660 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1661 AddString(Entry.Path, Record);
1662 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001663 Record.push_back(Entry.IsFramework);
1664 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001665 }
1666
1667 // System header prefixes.
1668 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1669 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1670 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1671 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1672 }
1673
1674 AddString(HSOpts.ResourceDir, Record);
1675 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001676 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001677 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001678 Record.push_back(HSOpts.ImplicitModuleMaps);
1679 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001680 Record.push_back(HSOpts.UseBuiltinIncludes);
1681 Record.push_back(HSOpts.UseStandardSystemIncludes);
1682 Record.push_back(HSOpts.UseStandardCXXIncludes);
1683 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001684 // Write out the specific module cache path that contains the module files.
1685 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001686 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1687
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001688 // Preprocessor options.
1689 Record.clear();
1690 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1691
1692 // Macro definitions.
1693 Record.push_back(PPOpts.Macros.size());
1694 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1695 AddString(PPOpts.Macros[I].first, Record);
1696 Record.push_back(PPOpts.Macros[I].second);
1697 }
1698
1699 // Includes
1700 Record.push_back(PPOpts.Includes.size());
1701 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1702 AddString(PPOpts.Includes[I], Record);
1703
1704 // Macro includes
1705 Record.push_back(PPOpts.MacroIncludes.size());
1706 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1707 AddString(PPOpts.MacroIncludes[I], Record);
1708
Douglas Gregorb6368752012-10-24 23:41:50 +00001709 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001710 // Detailed record is important since it is used for the module cache hash.
1711 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001712 AddString(PPOpts.ImplicitPCHInclude, Record);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001713 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1714 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1715
Richard Smith0516b182015-09-08 19:40:14 +00001716 // Leave the options block.
1717 Stream.ExitBlock();
1718
Douglas Gregora3b20262011-05-06 21:43:30 +00001719 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001720 SourceManager &SM = Context.getSourceManager();
1721 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001722 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001723 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1724 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001725 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001726 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001727
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001728 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001729 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001730 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001731 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001732 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001733
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001734 Record.clear();
1735 Record.push_back(SM.getMainFileID().getOpaqueValue());
1736 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1737
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001738 // Original PCH directory
1739 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001740 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001741 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001743 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001744
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001745 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001746
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001747 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001748 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1749
Mehdi Amini57a41912015-09-10 01:46:39 +00001750 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001751 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1752 }
1753
Douglas Gregor49491f72013-03-15 22:15:07 +00001754 WriteInputFiles(Context.SourceMgr,
1755 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001756 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001757 Stream.ExitBlock();
1758}
1759
Douglas Gregor49491f72013-03-15 22:15:07 +00001760namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001761
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001762/// An input file.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001763struct InputFileEntry {
1764 const FileEntry *File;
1765 bool IsSystemFile;
1766 bool IsTransient;
1767 bool BufferOverridden;
1768 bool IsTopLevelModuleMap;
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001769 uint32_t ContentHash[2];
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001770};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001771
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001772} // namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001773
1774void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1775 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001776 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001777 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001778
Douglas Gregor72be3902012-10-19 00:38:02 +00001779 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001780
Douglas Gregor72be3902012-10-19 00:38:02 +00001781 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001782 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001783 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001784 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001785 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1786 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001787 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001788 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Richard Smithf3f84612017-06-29 02:19:42 +00001789 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
Douglas Gregor72be3902012-10-19 00:38:02 +00001790 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001791 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001792
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001793 // Create input file hash abbreviation.
1794 auto IFHAbbrev = std::make_shared<BitCodeAbbrev>();
1795 IFHAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_HASH));
1796 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1797 IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1798 unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
1799
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001800 // Get all ContentCache objects for files, sorted by whether the file is a
1801 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001802 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001803 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1804 // Get this source location entry.
1805 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001806 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001807
1808 // We only care about file entries that were not overridden.
1809 if (!SLoc->isFile())
1810 continue;
Richard Smithf3f84612017-06-29 02:19:42 +00001811 const SrcMgr::FileInfo &File = SLoc->getFile();
1812 const SrcMgr::ContentCache *Cache = File.getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001813 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001814 continue;
1815
Douglas Gregor49491f72013-03-15 22:15:07 +00001816 InputFileEntry Entry;
1817 Entry.File = Cache->OrigEntry;
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001818 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
Richard Smitha8cfffa2015-11-26 02:04:16 +00001819 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001820 Entry.BufferOverridden = Cache->BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001821 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1822 File.getIncludeLoc().isInvalid();
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001823
1824 auto ContentHash = hash_code(-1);
1825 if (PP->getHeaderSearchInfo()
1826 .getHeaderSearchOpts()
1827 .ValidateASTInputFilesContent) {
1828 auto *MemBuff = Cache->getRawBuffer();
1829 if (MemBuff)
1830 ContentHash = hash_value(MemBuff->getBuffer());
1831 else
1832 // FIXME: The path should be taken from the FileEntryRef.
1833 PP->Diag(SourceLocation(), diag::err_module_unable_to_hash_content)
1834 << Entry.File->getName();
1835 }
1836 auto CH = llvm::APInt(64, ContentHash);
1837 Entry.ContentHash[0] =
1838 static_cast<uint32_t>(CH.getLoBits(32).getZExtValue());
1839 Entry.ContentHash[1] =
1840 static_cast<uint32_t>(CH.getHiBits(32).getZExtValue());
1841
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001842 if (Entry.IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001843 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001844 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001845 SortedFiles.push_front(Entry);
1846 }
1847
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001848 unsigned UserFilesNum = 0;
1849 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001850 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001851 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001852 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001853 if (InputFileID != 0)
1854 continue; // already recorded this file.
1855
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001856 // Record this entry's offset.
1857 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001858
1859 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001860
Douglas Gregor49491f72013-03-15 22:15:07 +00001861 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001862 ++UserFilesNum;
1863
Douglas Gregor72be3902012-10-19 00:38:02 +00001864 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001865 // And whether this file was overridden.
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001866 {
1867 RecordData::value_type Record[] = {
1868 INPUT_FILE,
1869 InputFileOffsets.size(),
1870 (uint64_t)Entry.File->getSize(),
1871 (uint64_t)getTimestampForOutput(Entry.File),
1872 Entry.BufferOverridden,
1873 Entry.IsTransient,
1874 Entry.IsTopLevelModuleMap};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001875
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001876 // FIXME: The path should be taken from the FileEntryRef.
1877 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1878 }
1879
1880 // Emit content hash for this file.
1881 {
1882 RecordData::value_type Record[] = {INPUT_FILE_HASH, Entry.ContentHash[0],
1883 Entry.ContentHash[1]};
1884 Stream.EmitRecordWithAbbrev(IFHAbbrevCode, Record);
1885 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001886 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001887
Douglas Gregor112b9072012-10-18 05:31:06 +00001888 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001889
1890 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001891 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001892 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1893 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001894 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1895 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001896 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001897 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001898
1899 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001900 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1901 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001902 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001903}
1904
Douglas Gregora7f71a92009-04-10 03:52:48 +00001905//===----------------------------------------------------------------------===//
1906// Source Manager Serialization
1907//===----------------------------------------------------------------------===//
1908
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001909/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001910/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001911static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001912 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001913
David Blaikieb44f0bf2017-01-04 22:36:43 +00001914 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001915 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001920 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001925 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001926}
1927
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001928/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001929/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001930static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001931 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001932
David Blaikieb44f0bf2017-01-04 22:36:43 +00001933 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001934 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001940 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001941}
1942
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001943/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001944/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001945static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1946 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001947 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001948
David Blaikieb44f0bf2017-01-04 22:36:43 +00001949 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001950 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1951 : SM_SLOC_BUFFER_BLOB));
1952 if (Compressed)
1953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001955 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001956}
1957
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001958/// Create an abbreviation for the SLocEntry that refers to a macro
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001959/// expansion.
1960static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001961 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001962
David Blaikieb44f0bf2017-01-04 22:36:43 +00001963 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001964 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Richard Smithb5f81712018-04-30 05:25:48 +00001969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
Douglas Gregor83243272009-04-15 18:05:10 +00001970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001971 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001972}
1973
Douglas Gregor09b69892011-02-10 17:09:37 +00001974namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001975
Douglas Gregor09b69892011-02-10 17:09:37 +00001976 // Trait used for the on-disk hash table of header search information.
1977 class HeaderFileInfoTrait {
1978 ASTWriter &Writer;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001979
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001980 // Keep track of the framework names we've used during serialization.
1981 SmallVector<char, 128> FrameworkStringData;
1982 llvm::StringMap<unsigned> FrameworkNameOffset;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001983
Douglas Gregor09b69892011-02-10 17:09:37 +00001984 public:
Richard Smith040e1262017-06-02 01:55:39 +00001985 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1986
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001987 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001988 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001989 off_t Size;
1990 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001991 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001992 using key_type_ref = const key_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001993
1994 using UnresolvedModule =
1995 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001996
Richard Smith040e1262017-06-02 01:55:39 +00001997 struct data_type {
1998 const HeaderFileInfo &HFI;
1999 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
2000 UnresolvedModule Unresolved;
2001 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002002 using data_type_ref = const data_type &;
Richard Smith040e1262017-06-02 01:55:39 +00002003
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002004 using hash_value_type = unsigned;
2005 using offset_type = unsigned;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002006
Richard Smithe75ee0f2015-08-17 07:13:32 +00002007 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002008 // The hash is based only on size/time of the file, so that the reader can
2009 // match even when symlinking or excess path elements ("foo/../", "../")
2010 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00002011 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00002012 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002013
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002014 std::pair<unsigned, unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002015 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002016 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002017
Peter Collingbournee3f65292018-05-18 19:46:24 +00002018 endian::Writer LE(Out, little);
Mehdi Amini004b9c72016-10-10 22:52:47 +00002019 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00002020 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002021 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00002022 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00002023 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
2024 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00002025 if (Data.Unresolved.getPointer())
2026 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00002027 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002028 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00002029 }
Richard Smith040e1262017-06-02 01:55:39 +00002030
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002031 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002032 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002033
Peter Collingbournee3f65292018-05-18 19:46:24 +00002034 endian::Writer LE(Out, little);
Richard Smith040e1262017-06-02 01:55:39 +00002035 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002036 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00002037 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002038 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00002039 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00002040 }
Richard Smith040e1262017-06-02 01:55:39 +00002041
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002042 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00002043 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002044 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002045
Peter Collingbournee3f65292018-05-18 19:46:24 +00002046 endian::Writer LE(Out, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00002047 uint64_t Start = Out.tell(); (void)Start;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002048
Richard Smithf3f84612017-06-29 02:19:42 +00002049 unsigned char Flags = (Data.HFI.isImport << 5)
2050 | (Data.HFI.isPragmaOnce << 4)
Richard Smith040e1262017-06-02 01:55:39 +00002051 | (Data.HFI.DirInfo << 1)
2052 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00002053 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00002054 LE.write<uint16_t>(Data.HFI.NumIncludes);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002055
Richard Smith040e1262017-06-02 01:55:39 +00002056 if (!Data.HFI.ControllingMacro)
2057 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00002058 else
Richard Smith040e1262017-06-02 01:55:39 +00002059 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
2060
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002061 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00002062 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002063 // If this header refers into a framework, save the framework name.
2064 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00002065 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002066 if (Pos == FrameworkNameOffset.end()) {
2067 Offset = FrameworkStringData.size() + 1;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002068 FrameworkStringData.append(Data.HFI.Framework.begin(),
Richard Smith040e1262017-06-02 01:55:39 +00002069 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002070 FrameworkStringData.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002071
Richard Smith040e1262017-06-02 01:55:39 +00002072 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002073 } else
2074 Offset = Pos->second;
2075 }
Justin Bognere1c147c2014-03-28 22:03:19 +00002076 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002077
Richard Smith040e1262017-06-02 01:55:39 +00002078 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
2079 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
2080 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00002081 assert((Value >> 2) == ModID && "overflow in header module info");
2082 LE.write<uint32_t>(Value);
2083 }
Richard Smith040e1262017-06-02 01:55:39 +00002084 };
2085
2086 // FIXME: If the header is excluded, we should write out some
2087 // record of that fact.
2088 for (auto ModInfo : Data.KnownHeaders)
2089 EmitModule(ModInfo.getModule(), ModInfo.getRole());
2090 if (Data.Unresolved.getPointer())
2091 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002092
Douglas Gregor09b69892011-02-10 17:09:37 +00002093 assert(Out.tell() - Start == DataLen && "Wrong data length");
2094 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002095
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002096 const char *strings_begin() const { return FrameworkStringData.begin(); }
2097 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00002098 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002099
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002100} // namespace
Douglas Gregor09b69892011-02-10 17:09:37 +00002101
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002102/// Write the header search block for the list of files that
Douglas Gregor09b69892011-02-10 17:09:37 +00002103///
2104/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00002105void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00002106 HeaderFileInfoTrait GeneratorTrait(*this);
2107 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
2108 SmallVector<const char *, 4> SavedStrings;
2109 unsigned NumHeaderSearchEntries = 0;
2110
2111 // Find all unresolved headers for the current module. We generally will
2112 // have resolved them before we get here, but not necessarily: we might be
2113 // compiling a preprocessed module, where there is no requirement for the
2114 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00002115 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00002116 if (WritingModule) {
2117 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
2118 while (!Worklist.empty()) {
2119 Module *M = Worklist.pop_back_val();
2120 if (!M->isAvailable())
2121 continue;
2122
2123 // Map to disk files where possible, to pick up any missing stat
2124 // information. This also means we don't need to check the unresolved
2125 // headers list when emitting resolved headers in the first loop below.
2126 // FIXME: It'd be preferable to avoid doing this if we were given
2127 // sufficient stat information in the module map.
2128 HS.getModuleMap().resolveHeaderDirectives(M);
2129
2130 // If the file didn't exist, we can still create a module if we were given
2131 // enough information in the module map.
2132 for (auto U : M->MissingHeaders) {
2133 // Check that we were given enough information to build a module
2134 // without this file existing on disk.
2135 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2136 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
2137 << WritingModule->getFullModuleName() << U.Size.hasValue()
2138 << U.FileName;
2139 continue;
2140 }
2141
2142 // Form the effective relative pathname for the file.
2143 SmallString<128> Filename(M->Directory->getName());
2144 llvm::sys::path::append(Filename, U.FileName);
2145 PreparePathForOutput(Filename);
2146
2147 StringRef FilenameDup = strdup(Filename.c_str());
2148 SavedStrings.push_back(FilenameDup.data());
2149
2150 HeaderFileInfoTrait::key_type Key = {
2151 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
2152 };
2153 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00002154 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00002155 };
2156 // FIXME: Deal with cases where there are multiple unresolved header
2157 // directives in different submodules for the same header.
2158 Generator.insert(Key, Data, GeneratorTrait);
2159 ++NumHeaderSearchEntries;
2160 }
2161
2162 Worklist.append(M->submodule_begin(), M->submodule_end());
2163 }
2164 }
2165
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002166 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00002167 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002168
Douglas Gregor09b69892011-02-10 17:09:37 +00002169 if (FilesByUID.size() > HS.header_file_size())
2170 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00002171
Douglas Gregor09b69892011-02-10 17:09:37 +00002172 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2173 const FileEntry *File = FilesByUID[UID];
2174 if (!File)
2175 continue;
2176
Richard Smith386bb072015-08-18 23:42:23 +00002177 // Get the file info. This will load info from the external source if
2178 // necessary. Skip emitting this file if we have no information on it
2179 // as a header file (in which case HFI will be null) or if it hasn't
2180 // changed since it was loaded. Also skip it if it's for a modular header
2181 // from a different module; in that case, we rely on the module(s)
2182 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00002183 const HeaderFileInfo *HFI =
2184 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
2185 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00002186 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00002187
Richard Smith7ed1bc92014-12-05 22:42:13 +00002188 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002189 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00002190 SmallString<128> FilenameTmp(Filename);
2191 if (PreparePathForOutput(FilenameTmp)) {
2192 // If we performed any translation on the file name at all, we need to
2193 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002194 Filename = StringRef(strdup(FilenameTmp.c_str()));
2195 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00002196 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002197
Richard Smith040e1262017-06-02 01:55:39 +00002198 HeaderFileInfoTrait::key_type Key = {
2199 Filename, File->getSize(), getTimestampForOutput(File)
2200 };
2201 HeaderFileInfoTrait::data_type Data = {
2202 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
2203 };
2204 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002205 ++NumHeaderSearchEntries;
2206 }
Richard Smith040e1262017-06-02 01:55:39 +00002207
Douglas Gregor09b69892011-02-10 17:09:37 +00002208 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002209 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002210 uint32_t BucketOffset;
2211 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002212 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002213
Douglas Gregor09b69892011-02-10 17:09:37 +00002214 llvm::raw_svector_ostream Out(TableData);
2215 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00002216 endian::write<uint32_t>(Out, 0, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00002217 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2218 }
2219
2220 // Create a blob abbreviation
2221 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002222
David Blaikieb44f0bf2017-01-04 22:36:43 +00002223 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002224 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002229 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002230
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002231 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002232 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2233 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002234 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002235 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002236
Douglas Gregor09b69892011-02-10 17:09:37 +00002237 // Free all of the strings we had to duplicate.
2238 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002239 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002240}
2241
George Rimarc39f5492017-01-17 15:45:31 +00002242static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2243 unsigned SLocBufferBlobCompressedAbbrv,
2244 unsigned SLocBufferBlobAbbrv) {
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002245 using RecordDataType = ASTWriter::RecordData::value_type;
George Rimarc39f5492017-01-17 15:45:31 +00002246
2247 // Compress the buffer if possible. We expect that almost all PCM
2248 // consumers will not want its contents.
2249 SmallString<0> CompressedBuffer;
2250 if (llvm::zlib::isAvailable()) {
2251 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2252 if (!E) {
2253 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2254 Blob.size() - 1};
2255 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2256 CompressedBuffer);
2257 return;
2258 }
2259 llvm::consumeError(std::move(E));
2260 }
2261
2262 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2263 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2264}
2265
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002266/// Writes the block containing the serialized form of the
Douglas Gregora7f71a92009-04-10 03:52:48 +00002267/// source manager.
2268///
2269/// TODO: We should probably use an on-disk hash table (stored in a
2270/// blob), indexed based on the file name, so that we only create
2271/// entries for files that we actually need. In the common case (no
2272/// errors), we probably won't have to create file entries for any of
2273/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002274void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002275 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002276 RecordData Record;
2277
Chris Lattner0910e3b2009-04-10 17:16:57 +00002278 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002279 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002280
2281 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002282 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2283 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002284 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2285 unsigned SLocBufferBlobCompressedAbbrv =
2286 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002287 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002288
Douglas Gregor258ae542009-04-27 06:38:32 +00002289 // Write out the source location entry table. We skip the first
2290 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002291 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002292 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002293 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2294 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002295 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002296 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002297 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002298 FileID FID = FileID::get(I);
2299 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002300
Douglas Gregor258ae542009-04-27 06:38:32 +00002301 // Record the offset of this source-location entry.
2302 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2303
2304 // Figure out which record code to use.
2305 unsigned Code;
2306 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002307 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2308 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002309 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002310 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002311 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002312 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002313 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002314 Record.clear();
2315 Record.push_back(Code);
2316
Douglas Gregor925296b2011-07-19 16:10:42 +00002317 // Starting offset of this entry within this module, so skip the dummy.
2318 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002319 if (SLoc->isFile()) {
2320 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002321 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002322 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2323 Record.push_back(File.hasLineDirectives());
2324
2325 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002326 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002327 if (Content->OrigEntry) {
2328 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002329 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002330
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002331 // The source location entry is a file. Emit input file ID.
2332 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2333 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002334
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002335 Record.push_back(File.NumCreatedFIDs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002336
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002337 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002338 if (FDI != FileDeclIDs.end()) {
2339 Record.push_back(FDI->second->FirstDeclIndex);
2340 Record.push_back(FDI->second->DeclIDs.size());
2341 } else {
2342 Record.push_back(0);
2343 Record.push_back(0);
2344 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002345
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002346 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002347
Richard Smithaada85c2016-02-06 02:06:43 +00002348 if (Content->BufferOverridden || Content->IsTransient)
2349 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002350 } else {
2351 // The source location entry is a buffer. The blob associated
2352 // with this entry contains the contents of the buffer.
2353
2354 // We add one to the size so that we capture the trailing NULL
2355 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2356 // the reader side).
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00002357 const llvm::MemoryBuffer *Buffer =
2358 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002359 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002360 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002361 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002362 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002363
Mehdi Amini99d1b292016-10-01 16:38:28 +00002364 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002365 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002366 }
Richard Smithaada85c2016-02-06 02:06:43 +00002367
2368 if (EmitBlob) {
2369 // Include the implicit terminating null character in the on-disk buffer
2370 // if we're writing it uncompressed.
2371 const llvm::MemoryBuffer *Buffer =
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00002372 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Richard Smithaada85c2016-02-06 02:06:43 +00002373 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002374 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2375 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002376 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002377 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002378 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002379 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002380 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2381 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2382 AddSourceLocation(Expansion.isMacroArgExpansion()
2383 ? SourceLocation()
2384 : Expansion.getExpansionLocEnd(),
2385 Record);
Richard Smithb5f81712018-04-30 05:25:48 +00002386 Record.push_back(Expansion.isExpansionTokenRange());
Douglas Gregor258ae542009-04-27 06:38:32 +00002387
2388 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002389 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002390 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002391 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002392 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002393 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002394 }
2395 }
2396
Douglas Gregor8f45df52009-04-16 22:23:12 +00002397 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002398
2399 if (SLocEntryOffsets.empty())
2400 return;
2401
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002402 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002403 // table is used for lazily loading source-location information.
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>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002407 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002411 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002412 {
2413 RecordData::value_type Record[] = {
2414 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2415 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2416 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2417 bytes(SLocEntryOffsets));
2418 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002419 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002420 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002422
2423 // Write the line table. It depends on remapping working, so it must come
2424 // after the source location offsets.
2425 if (SourceMgr.hasLineTable()) {
2426 LineTableInfo &LineTable = SourceMgr.getLineTable();
2427
2428 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002429
2430 // Emit the needed file names.
2431 llvm::DenseMap<int, int> FilenameMap;
Hans Wennborg14487362017-12-04 22:28:45 +00002432 FilenameMap[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00002433 for (const auto &L : LineTable) {
2434 if (L.first.ID < 0)
2435 continue;
2436 for (auto &LE : L.second) {
2437 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
Hans Wennborg14487362017-12-04 22:28:45 +00002438 FilenameMap.size() - 1)).second)
Richard Smith63078492015-09-01 07:41:55 +00002439 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2440 }
2441 }
2442 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002443
2444 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002445 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002446 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002447 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002448 continue;
2449
2450 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002451 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002452
2453 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002454 Record.push_back(L.second.size());
2455 for (const auto &LE : L.second) {
2456 Record.push_back(LE.FileOffset);
2457 Record.push_back(LE.LineNo);
2458 Record.push_back(FilenameMap[LE.FilenameID]);
2459 Record.push_back((unsigned)LE.FileKind);
2460 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002461 }
2462 }
Richard Smith63078492015-09-01 07:41:55 +00002463
Douglas Gregor925296b2011-07-19 16:10:42 +00002464 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2465 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002466}
2467
Douglas Gregorc5046832009-04-27 18:38:38 +00002468//===----------------------------------------------------------------------===//
2469// Preprocessor Serialization
2470//===----------------------------------------------------------------------===//
2471
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002472static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2473 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002474 if (MacroInfo *MI = MD->getMacroInfo())
2475 if (MI->isBuiltinMacro())
2476 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002477
2478 if (IsModule) {
2479 SourceLocation Loc = MD->getLocation();
2480 if (Loc.isInvalid())
2481 return true;
2482 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2483 return true;
2484 }
2485
2486 return false;
2487}
2488
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002489/// Writes the block containing the serialized form of the
Chris Lattnereeffaef2009-04-10 17:15:23 +00002490/// preprocessor.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002491void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002492 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2493 if (PPRec)
2494 WritePreprocessorDetail(*PPRec);
2495
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002496 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002497 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002498
Chris Lattner0af3ba12009-04-13 01:29:17 +00002499 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2500 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002501 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002502 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002503 }
2504
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002505 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2506 assert(!IsModule);
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00002507 auto SkipInfo = PP.getPreambleSkipInfo();
2508 if (SkipInfo.hasValue()) {
2509 Record.push_back(true);
2510 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2511 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2512 Record.push_back(SkipInfo->FoundNonSkipPortion);
2513 Record.push_back(SkipInfo->FoundElse);
2514 AddSourceLocation(SkipInfo->ElseLoc, Record);
2515 } else {
2516 Record.push_back(false);
2517 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002518 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2519 AddSourceLocation(Cond.IfLoc, Record);
2520 Record.push_back(Cond.WasSkipping);
2521 Record.push_back(Cond.FoundNonSkip);
2522 Record.push_back(Cond.FoundElse);
2523 }
2524 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2525 Record.clear();
2526 }
2527
Douglas Gregoreda6a892009-04-26 00:07:37 +00002528 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002529 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002530
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002531 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002532 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002533 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002534 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002535
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002536 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002537 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002538
Richard Smithee977932015-05-01 21:22:17 +00002539 // Construct the list of identifiers with macro directives that need to be
2540 // serialized.
2541 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2542 for (auto &Id : PP.getIdentifierTable())
2543 if (Id.second->hadMacroDefinition() &&
2544 (!Id.second->isFromAST() ||
2545 Id.second->hasChangedSinceDeserialization()))
2546 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002547 // Sort the set of macro definitions that need to be serialized by the
2548 // name of the macro, to provide a stable ordering.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00002549 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002550
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002551 // Emit the macro directives as a list and associate the offset with the
2552 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002553 for (const IdentifierInfo *Name : MacroIdentifiers) {
2554 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002555 auto StartOffset = Stream.GetCurrentBitNo();
2556
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002557 // Emit the macro directives in reverse source order.
2558 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002559 // Once we hit an ignored macro, we're done: the rest of the chain
2560 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002561 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002562 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002563
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002564 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002565 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002566 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002567 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002568 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002569 Record.push_back(VisMD->isPublic());
2570 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002571 }
Richard Smithd7329392015-04-21 21:46:32 +00002572
Richard Smithb8b2ed62015-04-23 18:18:26 +00002573 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002574 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002575 // We write out exported module macros for PCH as well.
2576 auto Leafs = PP.getLeafModuleMacros(Name);
2577 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2578 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2579 while (!Worklist.empty()) {
2580 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002581
Manman Ren33d80292016-05-31 18:19:32 +00002582 // Emit a record indicating this submodule exports this macro.
2583 ModuleMacroRecord.push_back(
2584 getSubmoduleID(Macro->getOwningModule()));
2585 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2586 for (auto *M : Macro->overrides())
2587 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002588
Manman Ren33d80292016-05-31 18:19:32 +00002589 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2590 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002591
Manman Ren33d80292016-05-31 18:19:32 +00002592 // Enqueue overridden macros once we've visited all their ancestors.
2593 for (auto *M : Macro->overrides())
2594 if (++Visits[M] == M->getNumOverridingMacros())
2595 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002596
Manman Ren33d80292016-05-31 18:19:32 +00002597 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002598 }
Richard Smithd7329392015-04-21 21:46:32 +00002599
Richard Smithee977932015-05-01 21:22:17 +00002600 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002601 continue;
2602
Richard Smithd7329392015-04-21 21:46:32 +00002603 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002604 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2605 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002606 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002607
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002608 /// Offsets of each of the macros into the bitstream, indexed by
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002609 /// the local macro ID
2610 ///
2611 /// For each identifier that is associated with a macro, this map
2612 /// provides the offset into the bitstream where that macro is
2613 /// defined.
2614 std::vector<uint32_t> MacroOffsets;
2615
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002616 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2617 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2618 MacroInfo *MI = MacroInfosToEmit[I].MI;
2619 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002620
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002621 if (ID < FirstMacroID) {
2622 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2623 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002624 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002625
2626 // Record the local offset of this macro.
2627 unsigned Index = ID - FirstMacroID;
2628 if (Index == MacroOffsets.size())
2629 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2630 else {
2631 if (Index > MacroOffsets.size())
2632 MacroOffsets.resize(Index + 1);
2633
2634 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2635 }
2636
2637 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002638 AddSourceLocation(MI->getDefinitionLoc(), Record);
2639 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2640 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002641 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002642 unsigned Code;
2643 if (MI->isObjectLike()) {
2644 Code = PP_MACRO_OBJECT_LIKE;
2645 } else {
2646 Code = PP_MACRO_FUNCTION_LIKE;
2647
2648 Record.push_back(MI->isC99Varargs());
2649 Record.push_back(MI->isGNUVarargs());
2650 Record.push_back(MI->hasCommaPasting());
Faisal Valiac506d72017-07-17 17:18:43 +00002651 Record.push_back(MI->getNumParams());
2652 for (const IdentifierInfo *Param : MI->params())
2653 AddIdentifierRef(Param, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002654 }
2655
2656 // If we have a detailed preprocessing record, record the macro definition
2657 // ID that corresponds to this macro.
2658 if (PPRec)
2659 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2660
2661 Stream.EmitRecord(Code, Record);
2662 Record.clear();
2663
2664 // Emit the tokens array.
2665 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2666 // Note that we know that the preprocessor does not have any annotation
2667 // tokens in it because they are created by the parser, and thus can't
2668 // be in a macro definition.
2669 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002670 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002671 Stream.EmitRecord(PP_TOKEN, Record);
2672 Record.clear();
2673 }
2674 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002675 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002676
Douglas Gregor92a96f52011-02-08 21:58:10 +00002677 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002678
2679 // Write the offsets table for macro IDs.
2680 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002681
David Blaikieb44f0bf2017-01-04 22:36:43 +00002682 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002683 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2684 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2685 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2687
David Blaikieb44f0bf2017-01-04 22:36:43 +00002688 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002689 {
2690 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2691 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2692 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2693 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002694}
2695
2696void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002697 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002698 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002699
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002700 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002701
Douglas Gregor92a96f52011-02-08 21:58:10 +00002702 // Enter the preprocessor block.
2703 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002704
Douglas Gregoraae92242010-03-19 21:51:54 +00002705 // If the preprocessor has a preprocessing record, emit it.
2706 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002707 using namespace llvm;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002708
2709 // Set up the abbreviation for
Douglas Gregor92a96f52011-02-08 21:58:10 +00002710 unsigned InclusionAbbrev = 0;
2711 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002712 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002713 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2715 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2716 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002717 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002719 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002720 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002721
2722 unsigned FirstPreprocessorEntityID
2723 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002724 + NUM_PREDEF_PP_ENTITY_IDS;
2725 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002726 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002727 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2728 EEnd = PPRec.local_end();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002729 E != EEnd;
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002730 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002731 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002732
Richard Smith66a81862015-05-04 02:25:31 +00002733 PreprocessedEntityOffsets.push_back(
2734 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002735
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002736 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002737 // Record this macro definition's ID.
2738 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002739
Douglas Gregor92a96f52011-02-08 21:58:10 +00002740 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002741 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2742 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002743 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002744
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002745 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002746 Record.push_back(ME->isBuiltinMacro());
2747 if (ME->isBuiltinMacro())
2748 AddIdentifierRef(ME->getName(), Record);
2749 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002750 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002751 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002752 continue;
2753 }
2754
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002755 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002756 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002757 Record.push_back(ID->getFileName().size());
2758 Record.push_back(ID->wasInQuotes());
2759 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002760 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002761 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002762 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002763 // Check that the FileEntry is not null because it was not resolved and
2764 // we create a PCH even with compiler errors.
2765 if (ID->getFile())
2766 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002767 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002768 continue;
2769 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002770
Douglas Gregor92a96f52011-02-08 21:58:10 +00002771 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2772 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002773 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002774
Douglas Gregoraae92242010-03-19 21:51:54 +00002775 // Write the offsets table for the preprocessing record.
2776 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002777 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2778
Douglas Gregoraae92242010-03-19 21:51:54 +00002779 // Write the offsets table for identifier IDs.
2780 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002781
David Blaikieb44f0bf2017-01-04 22:36:43 +00002782 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002783 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002784 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002785 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002786 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002787
Mehdi Amini57a41912015-09-10 01:46:39 +00002788 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2789 FirstPreprocessorEntityID -
2790 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002791 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002792 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002793 }
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00002794
2795 // Write the skipped region table for the preprocessing record.
2796 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2797 if (SkippedRanges.size() > 0) {
2798 std::vector<PPSkippedRange> SerializedSkippedRanges;
2799 SerializedSkippedRanges.reserve(SkippedRanges.size());
2800 for (auto const& Range : SkippedRanges)
2801 SerializedSkippedRanges.emplace_back(Range);
2802
2803 using namespace llvm;
2804 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2805 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2807 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2808
2809 Record.clear();
2810 Record.push_back(PPD_SKIPPED_RANGES);
2811 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2812 bytes(SerializedSkippedRanges));
2813 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002814}
2815
Richard Smith386bb072015-08-18 23:42:23 +00002816unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002817 if (!Mod)
2818 return 0;
2819
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002820 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2821 if (Known != SubmoduleIDs.end())
2822 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002823
Richard Smithdc1f0422016-07-20 19:10:16 +00002824 auto *Top = Mod->getTopLevelModule();
2825 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002826 (getLangOpts().CompilingPCH ||
2827 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002828 return 0;
2829
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002830 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2831}
2832
Richard Smith386bb072015-08-18 23:42:23 +00002833unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2834 // FIXME: This can easily happen, if we have a reference to a submodule that
2835 // did not result in us loading a module file for that submodule. For
2836 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2837 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2838 assert((ID || !Mod) &&
2839 "asked for module ID for non-local, non-imported module");
2840 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002841}
2842
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002843/// Compute the number of modules within the given tree (including the
Douglas Gregor253eefe2011-12-01 00:59:36 +00002844/// given module).
2845static unsigned getNumberOfModules(Module *Mod) {
2846 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002847 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002848 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002849 ChildModules += getNumberOfModules(*Sub);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002850
Douglas Gregor253eefe2011-12-01 00:59:36 +00002851 return ChildModules + 1;
2852}
2853
Douglas Gregorde3ef502011-11-30 23:21:26 +00002854void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002855 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002856 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002857
Douglas Gregor69021972011-11-30 17:33:56 +00002858 // Write the abbreviations needed for the submodules block.
2859 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002860
David Blaikieb44f0bf2017-01-04 22:36:43 +00002861 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002862 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
Richard Smithdd8b5332017-09-04 05:37:53 +00002865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
Douglas Gregor69021972011-11-30 17:33:56 +00002866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002873 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
Douglas Gregor69021972011-11-30 17:33:56 +00002875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002876 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002877
David Blaikieb44f0bf2017-01-04 22:36:43 +00002878 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002879 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002881 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002882
David Blaikieb44f0bf2017-01-04 22:36:43 +00002883 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002884 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002886 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002887
David Blaikieb44f0bf2017-01-04 22:36:43 +00002888 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002889 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002891 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002892
David Blaikieb44f0bf2017-01-04 22:36:43 +00002893 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002894 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002896 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002897
David Blaikieb44f0bf2017-01-04 22:36:43 +00002898 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002899 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002902 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002903
David Blaikieb44f0bf2017-01-04 22:36:43 +00002904 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002905 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002907 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002908
David Blaikieb44f0bf2017-01-04 22:36:43 +00002909 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002910 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002912 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002913
David Blaikieb44f0bf2017-01-04 22:36:43 +00002914 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002915 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002917 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002918
David Blaikieb44f0bf2017-01-04 22:36:43 +00002919 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002920 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002922 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002923
David Blaikieb44f0bf2017-01-04 22:36:43 +00002924 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002925 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002928 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002929
David Blaikieb44f0bf2017-01-04 22:36:43 +00002930 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002931 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002933 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002934
David Blaikieb44f0bf2017-01-04 22:36:43 +00002935 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002936 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002939 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002940
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002941 Abbrev = std::make_shared<BitCodeAbbrev>();
2942 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2944 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2945
Douglas Gregor253eefe2011-12-01 00:59:36 +00002946 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002947 RecordData::value_type Record[] = {
2948 getNumberOfModules(WritingModule),
Richard Smithdd8b5332017-09-04 05:37:53 +00002949 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002950 Stream.EmitRecord(SUBMODULE_METADATA, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002951
Douglas Gregor69021972011-11-30 17:33:56 +00002952 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002953 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002954 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002955 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002956 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002957 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002958 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002959
2960 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002961 if (Mod->Parent) {
2962 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002963 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002964 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002965
2966 // Emit the definition of the block.
2967 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002968 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2969 ID,
2970 ParentID,
Simon Pilgrimf331a662017-09-04 10:54:39 +00002971 (RecordData::value_type)Mod->Kind,
David Blaikie9ffe5a32017-01-30 05:00:26 +00002972 Mod->IsFramework,
2973 Mod->IsExplicit,
2974 Mod->IsSystem,
2975 Mod->IsExternC,
2976 Mod->InferSubmodules,
2977 Mod->InferExplicitSubmodules,
2978 Mod->InferExportWildcard,
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002979 Mod->ConfigMacrosExhaustive,
2980 Mod->ModuleMapIsPrivate};
Mehdi Amini57a41912015-09-10 01:46:39 +00002981 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2982 }
2983
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002984 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002985 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002986 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002987 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002988 }
2989
Douglas Gregor69021972011-11-30 17:33:56 +00002990 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002991 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002992 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002993 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2994 UmbrellaHeader.NameAsWritten);
2995 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002996 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2997 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002998 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002999 }
Richard Smith306d8922014-10-22 23:50:56 +00003000
Douglas Gregor69021972011-11-30 17:33:56 +00003001 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00003002 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00003003 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00003004 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00003005 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00003006 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00003007 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
3008 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
3009 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00003010 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00003011 Module::HK_PrivateTextual},
3012 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00003013 };
3014 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003015 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00003016 for (auto &H : Mod->Headers[HL.HeaderKind])
3017 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
3018 }
3019
3020 // Emit the top headers.
3021 {
3022 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00003023 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00003024 for (auto *H : TopHeaders)
3025 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00003026 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003027
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003028 // Emit the imports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003029 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003030 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00003031 for (auto *I : Mod->Imports)
3032 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003033 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
3034 }
3035
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003036 // Emit the exports.
Douglas Gregor24bb9232011-12-02 18:58:38 +00003037 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003038 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00003039 for (const auto &E : Mod->Exports) {
3040 // FIXME: This may fail; we don't require that all exported modules
3041 // are local or imported.
3042 Record.push_back(getSubmoduleID(E.getPointer()));
3043 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00003044 }
3045 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
3046 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003047
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003048 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
3049 // Might be unnecessary as use declarations are only used to build the
3050 // module itself.
3051
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003052 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00003053 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003054 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
3055 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00003056 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003057 }
3058
Douglas Gregorfb912652013-03-20 21:10:35 +00003059 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00003060 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00003061 // FIXME: This may fail; we don't require that all conflicting modules
3062 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00003063 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
3064 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00003065 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00003066 }
3067
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003068 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00003069 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003070 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00003071 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003072 }
3073
Richard Smithdc1f0422016-07-20 19:10:16 +00003074 // Emit the initializers, if any.
3075 RecordData Inits;
3076 for (Decl *D : Context->getModuleInitializers(Mod))
3077 Inits.push_back(GetDeclRef(D));
3078 if (!Inits.empty())
3079 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3080
Douglas Gregorf0b11de2017-09-14 23:38:44 +00003081 // Emit the name of the re-exported module, if any.
3082 if (!Mod->ExportAsModule.empty()) {
3083 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3084 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3085 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003086
Douglas Gregor69021972011-11-30 17:33:56 +00003087 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00003088 for (auto *M : Mod->submodules())
3089 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00003090 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003091
Douglas Gregor69021972011-11-30 17:33:56 +00003092 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00003093
Richard Smith23d8d032015-05-14 00:45:20 +00003094 assert((NextSubmoduleID - FirstSubmoduleID ==
3095 getNumberOfModules(WritingModule)) &&
3096 "Wrong # of submodules; found a reference to a non-local, "
3097 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00003098}
3099
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00003100void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3101 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003102 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3103 DiagStateIDMap;
3104 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003105 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003106
Richard Smithe37391c2017-05-03 00:28:49 +00003107 auto EncodeDiagStateFlags =
3108 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3109 unsigned Result = (unsigned)DS->ExtBehavior;
3110 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00003111 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3112 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3113 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00003114 Result = (Result << 1) | Val;
3115 return Result;
3116 };
3117
3118 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3119 Record.push_back(Flags);
3120
Richard Smithd230de22017-01-26 01:01:01 +00003121 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3122 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00003123 // Ensure that the diagnostic state wasn't modified since it was created.
3124 // We will not correctly round-trip this information otherwise.
3125 assert(Flags == EncodeDiagStateFlags(State) &&
3126 "diag state flags vary in single AST file");
3127
Richard Smithd230de22017-01-26 01:01:01 +00003128 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003129 Record.push_back(DiagStateID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003130
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003131 if (DiagStateID == 0) {
3132 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003133
3134 // Add a placeholder for the number of mappings.
3135 auto SizeIdx = Record.size();
3136 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00003137 for (const auto &I : *State) {
3138 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003139 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00003140 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003141 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003142 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003143 // Update the placeholder.
3144 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00003145 }
3146 };
3147
3148 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00003149
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003150 // Reserve a spot for the number of locations with state transitions.
3151 auto NumLocationsIdx = Record.size();
3152 Record.emplace_back();
3153
3154 // Emit the state transitions.
3155 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00003156 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3157 if (!FileIDAndFile.first.isValid() ||
3158 !FileIDAndFile.second.HasLocalTransitions)
3159 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003160 ++NumLocations;
Richard Smith6c2b5a82018-02-09 01:15:13 +00003161
3162 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3163 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3164 AddSourceLocation(Loc, Record);
3165
Richard Smithd230de22017-01-26 01:01:01 +00003166 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3167 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3168 Record.push_back(StatePoint.Offset);
3169 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003170 }
3171 }
3172
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003173 // Backpatch the number of locations.
3174 Record[NumLocationsIdx] = NumLocations;
3175
3176 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00003177 //
3178 // This also protects against a hypothetical corner case with simulating
3179 // -Werror settings for implicit modules in the ASTReader, where reading
3180 // CurDiagState out of context could change whether warning pragmas are
3181 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003182 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3183 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3184
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00003185 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003186}
3187
Douglas Gregorc5046832009-04-27 18:38:38 +00003188//===----------------------------------------------------------------------===//
3189// Type Serialization
3190//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00003191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003192/// Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003193void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00003194 TypeIdx &IdxRef = TypeIdxs[T];
3195 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3196 IdxRef = TypeIdx(NextTypeID++);
3197 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00003198
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003199 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00003200
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003201 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00003202
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003203 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003204 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00003205 W.Visit(T);
3206 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00003207
Richard Smith290d8012016-04-06 17:06:00 +00003208 // Record the offset for this type.
3209 unsigned Index = Idx.getIndex() - FirstTypeID;
3210 if (TypeOffsets.size() == Index)
3211 TypeOffsets.push_back(Offset);
3212 else if (TypeOffsets.size() < Index) {
3213 TypeOffsets.resize(Index + 1);
3214 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00003215 } else {
Richard Smith290d8012016-04-06 17:06:00 +00003216 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003217 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003218}
3219
Douglas Gregorc5046832009-04-27 18:38:38 +00003220//===----------------------------------------------------------------------===//
3221// Declaration Serialization
3222//===----------------------------------------------------------------------===//
3223
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003224/// Write the block containing all of the declaration IDs
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003225/// lexically declared within the given DeclContext.
3226///
3227/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003228/// bitstream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003229uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003230 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003231 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003232 return 0;
3233
Douglas Gregor8f45df52009-04-16 22:23:12 +00003234 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003235 SmallVector<uint32_t, 128> KindDeclPairs;
3236 for (const auto *D : DC->decls()) {
3237 KindDeclPairs.push_back(D->getKind());
3238 KindDeclPairs.push_back(GetDeclRef(D));
3239 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003240
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003241 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00003242 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00003243 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3244 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003245 return Offset;
3246}
3247
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003248void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003249 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003250
3251 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003252 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003253 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00003255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003257 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003258 {
3259 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3260 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3261 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3262 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003263
3264 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003265 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003266 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00003268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003270 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003271 {
3272 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3273 FirstDeclID - NUM_PREDEF_DECL_IDS};
3274 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3275 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003276}
3277
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003278void ASTWriter::WriteFileDeclIDsMap() {
3279 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003280
Chandler Carruthb306d732015-03-27 00:31:20 +00003281 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3282 FileDeclIDs.begin(), FileDeclIDs.end());
Fangrui Song55fab262018-09-26 22:16:28 +00003283 llvm::sort(SortedFileDeclIDs, llvm::less_first());
Chandler Carruthb306d732015-03-27 00:31:20 +00003284
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003285 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003286 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3287 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3288 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3289 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3290 for (auto &LocDeclEntry : Info.DeclIDs)
3291 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003292 }
3293
David Blaikieb44f0bf2017-01-04 22:36:43 +00003294 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003295 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003298 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003299 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3300 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003301 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003302}
3303
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003304void ASTWriter::WriteComments() {
3305 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Ilya Biryukov45643102018-07-09 11:33:23 +00003306 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3307 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3308 return;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003309 RecordData Record;
Jan Korousf31d8df2019-08-13 18:11:44 +00003310 for (const auto &FO : Context->Comments.OrderedComments) {
3311 for (const auto &OC : FO.second) {
3312 const RawComment *I = OC.second;
3313 Record.clear();
3314 AddSourceRange(I->getSourceRange(), Record);
3315 Record.push_back(I->getKind());
3316 Record.push_back(I->isTrailingComment());
3317 Record.push_back(I->isAlmostTrailingComment());
3318 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3319 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003320 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003321}
3322
Douglas Gregorc5046832009-04-27 18:38:38 +00003323//===----------------------------------------------------------------------===//
3324// Global Method Pool and Selector Serialization
3325//===----------------------------------------------------------------------===//
3326
Douglas Gregore84a9da2009-04-20 20:36:09 +00003327namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003328
Douglas Gregorc78d3462009-04-24 21:10:55 +00003329// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003330class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003331 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003332
3333public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003334 using key_type = Selector;
3335 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003336
Sebastian Redl834bb972010-08-04 17:20:04 +00003337 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003338 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003339 ObjCMethodList Instance, Factory;
3340 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003341 using data_type_ref = const data_type &;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003342
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003343 using hash_value_type = unsigned;
3344 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003345
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003346 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
Mike Stump11289f42009-09-09 15:08:12 +00003347
Justin Bogner25463f12014-04-18 20:27:24 +00003348 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003349 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003350 }
Mike Stump11289f42009-09-09 15:08:12 +00003351
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003352 std::pair<unsigned, unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003353 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003354 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003355 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003356
Peter Collingbournee3f65292018-05-18 19:46:24 +00003357 endian::Writer LE(Out, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003358 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003359 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003360 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3361 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003362 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003363 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003364 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003365 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003366 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003367 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003368 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003369 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003370 return std::make_pair(KeyLen, DataLen);
3371 }
Mike Stump11289f42009-09-09 15:08:12 +00003372
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003373 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003374 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003375
Peter Collingbournee3f65292018-05-18 19:46:24 +00003376 endian::Writer LE(Out, little);
Mike Stump11289f42009-09-09 15:08:12 +00003377 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003378 assert((Start >> 32) == 0 && "Selector key offset too large");
3379 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003380 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003381 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003382 if (N == 0)
3383 N = 1;
3384 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003385 LE.write<uint32_t>(
3386 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003387 }
Mike Stump11289f42009-09-09 15:08:12 +00003388
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003389 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003390 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003391 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003392
Peter Collingbournee3f65292018-05-18 19:46:24 +00003393 endian::Writer LE(Out, little);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003394 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003395 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003396 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003397 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003398 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003399 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003400 ++NumInstanceMethods;
3401
3402 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003403 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003404 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003405 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003406 ++NumFactoryMethods;
3407
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003408 unsigned InstanceBits = Methods.Instance.getBits();
3409 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003410 unsigned InstanceHasMoreThanOneDeclBit =
3411 Methods.Instance.hasMoreThanOneDecl();
3412 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3413 (InstanceHasMoreThanOneDeclBit << 2) |
3414 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003415 unsigned FactoryBits = Methods.Factory.getBits();
3416 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003417 unsigned FactoryHasMoreThanOneDeclBit =
3418 Methods.Factory.hasMoreThanOneDecl();
3419 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3420 (FactoryHasMoreThanOneDeclBit << 2) |
3421 FactoryBits;
3422 LE.write<uint16_t>(FullInstanceBits);
3423 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003424 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003425 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003426 if (Method->getMethod())
3427 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003428 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003429 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003430 if (Method->getMethod())
3431 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003432
3433 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003434 }
3435};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003436
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003437} // namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +00003438
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003439/// Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003440///
3441/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003442/// in an on-disk hash table indexed by the selector. The hash table also
3443/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003444void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003445 using namespace llvm;
3446
Sebastian Redla19a67f2010-08-03 21:58:15 +00003447 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003448 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003449 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003450 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003451 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003452 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003453 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003454 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003455
Sebastian Redla19a67f2010-08-03 21:58:15 +00003456 // Create the on-disk hash table representation. We walk through every
3457 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003458 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003459 for (auto &SelectorAndID : SelectorIDs) {
3460 Selector S = SelectorAndID.first;
3461 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003462 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003463 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003464 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003465 ObjCMethodList(),
3466 ObjCMethodList()
3467 };
3468 if (F != SemaRef.MethodPool.end()) {
3469 Data.Instance = F->second.first;
3470 Data.Factory = F->second.second;
3471 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003472 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003473 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003474 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003475 // Selector already exists. Did it change?
3476 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003477 for (ObjCMethodList *M = &Data.Instance;
3478 !changed && M && M->getMethod(); M = M->getNext()) {
3479 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003480 changed = true;
3481 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003482 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003483 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003484 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003485 changed = true;
3486 }
3487 if (!changed)
3488 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003489 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003490 // A new method pool entry.
3491 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003492 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003493 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003494 }
3495
Douglas Gregorc78d3462009-04-24 21:10:55 +00003496 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003497 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003498 uint32_t BucketOffset;
3499 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003500 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003501
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003502 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003503 llvm::raw_svector_ostream Out(MethodPool);
3504 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003505 endian::write<uint32_t>(Out, 0, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003506 BucketOffset = Generator.Emit(Out, Trait);
3507 }
3508
3509 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003510 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003511 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003515 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003516
Douglas Gregor95c13f52009-04-25 17:48:32 +00003517 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003518 {
3519 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3520 NumTableEntries};
3521 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3522 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003523
3524 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003525 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003526 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003530 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003531
3532 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003533 {
3534 RecordData::value_type Record[] = {
3535 SELECTOR_OFFSETS, SelectorOffsets.size(),
3536 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3537 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3538 bytes(SelectorOffsets));
3539 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003540 }
3541}
3542
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003543/// Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003544void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003545 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003546
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003547 if (SemaRef.ReferencedSelectors.empty())
3548 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003549
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003550 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003551 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003552
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003553 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003554 // very tricky to fix, and given that @selector shouldn't really appear in
3555 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003556 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3557 Selector Sel = SelectorAndLocation.first;
3558 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003559 Writer.AddSelectorRef(Sel);
3560 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003561 }
Richard Smithe64e7452016-04-18 21:54:58 +00003562 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003563}
3564
Douglas Gregorc5046832009-04-27 18:38:38 +00003565//===----------------------------------------------------------------------===//
3566// Identifier Table Serialization
3567//===----------------------------------------------------------------------===//
3568
Richard Smithb3761912015-02-05 23:08:52 +00003569/// Determine the declaration that should be put into the name lookup table to
3570/// represent the given declaration in this module. This is usually D itself,
3571/// but if D was imported and merged into a local declaration, we want the most
3572/// recent local declaration instead. The chosen declaration will be the most
3573/// recent declaration in any module that imports this one.
3574static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3575 NamedDecl *D) {
3576 if (!LangOpts.Modules || !D->isFromASTFile())
3577 return D;
3578
3579 if (Decl *Redecl = D->getPreviousDecl()) {
3580 // For Redeclarable decls, a prior declaration might be local.
3581 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003582 // If we find a local decl, we're done.
3583 if (!Redecl->isFromASTFile()) {
3584 // Exception: in very rare cases (for injected-class-names), not all
3585 // redeclarations are in the same semantic context. Skip ones in a
3586 // different context. They don't go in this lookup table at all.
3587 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3588 D->getDeclContext()->getRedeclContext()))
3589 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003590 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003591 }
3592
Richard Smithfe620d22015-03-05 23:24:12 +00003593 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003594 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003595 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003596 break;
3597 }
3598 } else if (Decl *First = D->getCanonicalDecl()) {
3599 // For Mergeable decls, the first decl might be local.
3600 if (!First->isFromASTFile())
3601 return cast<NamedDecl>(First);
3602 }
3603
3604 // All declarations are imported. Our most recent declaration will also be
3605 // the most recent one in anyone who imports us.
3606 return D;
3607}
3608
Douglas Gregorc78d3462009-04-24 21:10:55 +00003609namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003610
Richard Smithcf97cf62015-04-19 01:34:23 +00003611class ASTIdentifierTableTrait {
3612 ASTWriter &Writer;
3613 Preprocessor &PP;
3614 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003615 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003616 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003617 ASTWriter::RecordData *InterestingIdentifierOffsets;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003618
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003619 /// Determines whether this is an "interesting" identifier that needs a
Richard Smithcf97cf62015-04-19 01:34:23 +00003620 /// full IdentifierInfo structure written into the hash table. Notably, this
3621 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3622 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003623 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003624 if (MacroOffset ||
3625 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003626 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003627 II->hasRevertedTokenIDToIdentifier() ||
Bruno Ricci366ba732018-09-21 12:53:22 +00003628 (NeedDecls && II->getFETokenInfo()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003629 return true;
3630
3631 return false;
3632 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003633
Douglas Gregore84a9da2009-04-20 20:36:09 +00003634public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003635 using key_type = IdentifierInfo *;
3636 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003637
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003638 using data_type = IdentID;
3639 using data_type_ref = data_type;
Mike Stump11289f42009-09-09 15:08:12 +00003640
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003641 using hash_value_type = unsigned;
3642 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003643
Richard Smithd7329392015-04-21 21:46:32 +00003644 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003645 IdentifierResolver &IdResolver, bool IsModule,
3646 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003647 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003648 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3649 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003650
Richard Smithd79514e2016-02-05 19:03:40 +00003651 bool needDecls() const { return NeedDecls; }
3652
Justin Bogner25463f12014-04-18 20:27:24 +00003653 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003654 return llvm::djbHash(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003655 }
Mike Stump11289f42009-09-09 15:08:12 +00003656
Richard Smith33e0f7e2015-07-22 02:08:40 +00003657 bool isInterestingIdentifier(const IdentifierInfo *II) {
3658 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3659 return isInterestingIdentifier(II, MacroOffset);
3660 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003661
Richard Smith9c254182015-07-19 21:41:12 +00003662 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3663 return isInterestingIdentifier(II, 0);
3664 }
3665
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003666 std::pair<unsigned, unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003667 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003668 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003669 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003670 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3671 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003672 DataLen += 2; // 2 bytes for builtin ID
3673 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003674 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003675 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003676
Richard Smitha534a312015-07-21 23:54:07 +00003677 if (NeedDecls) {
3678 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3679 DEnd = IdResolver.end();
3680 D != DEnd; ++D)
3681 DataLen += 4;
3682 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003683 }
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003684
Justin Bognere1c147c2014-03-28 22:03:19 +00003685 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003686
Peter Collingbournee3f65292018-05-18 19:46:24 +00003687 endian::Writer LE(Out, little);
Justin Bognere1c147c2014-03-28 22:03:19 +00003688
Richard Smithdf8a8312015-03-13 04:05:01 +00003689 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003690 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003691 // We emit the key length after the data length so that every
3692 // string is preceded by a 16-bit length. This matches the PTH
3693 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003694 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003695 return std::make_pair(KeyLen, DataLen);
3696 }
Mike Stump11289f42009-09-09 15:08:12 +00003697
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003698 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003699 unsigned KeyLen) {
3700 // Record the location of the key data. This is used when generating
3701 // the mapping from persistent IDs to strings.
3702 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003703
3704 // Emit the offset of the key/data length information to the interesting
3705 // identifiers table if necessary.
3706 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3707 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3708
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003709 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003710 }
Mike Stump11289f42009-09-09 15:08:12 +00003711
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003712 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003713 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003714 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003715
Peter Collingbournee3f65292018-05-18 19:46:24 +00003716 endian::Writer LE(Out, little);
Richard Smithcf97cf62015-04-19 01:34:23 +00003717
Richard Smithd7329392015-04-21 21:46:32 +00003718 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3719 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003720 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003721 return;
3722 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003723
Justin Bognere1c147c2014-03-28 22:03:19 +00003724 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003725 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3726 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003727 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003728 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003729 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003730 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003731 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3732 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003733 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003734 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003735 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003736 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003737
Richard Smithd7329392015-04-21 21:46:32 +00003738 if (HadMacroDefinition)
3739 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003740
Richard Smitha534a312015-07-21 23:54:07 +00003741 if (NeedDecls) {
3742 // Emit the declaration IDs in reverse order, because the
3743 // IdentifierResolver provides the declarations as they would be
3744 // visible (e.g., the function "stat" would come before the struct
3745 // "stat"), but the ASTReader adds declarations to the end of the list
3746 // (so we need to see the struct "stat" before the function "stat").
3747 // Only emit declarations that aren't from a chained PCH, though.
3748 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3749 IdResolver.end());
3750 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3751 DEnd = Decls.rend();
3752 D != DEnd; ++D)
3753 LE.write<uint32_t>(
3754 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3755 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003756 }
3757};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003758
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003759} // namespace
Douglas Gregore84a9da2009-04-20 20:36:09 +00003760
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003761/// Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003762///
3763/// The identifier table consists of a blob containing string data
3764/// (the actual identifiers themselves) and a separate "offsets" index
3765/// that maps identifier IDs to locations within the blob.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003766void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003767 IdentifierResolver &IdResolver,
3768 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003769 using namespace llvm;
3770
Richard Smith33e0f7e2015-07-22 02:08:40 +00003771 RecordData InterestingIdents;
3772
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003773 // Create and write out the blob that contains the identifier
3774 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003775 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003776 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003777 ASTIdentifierTableTrait Trait(
3778 *this, PP, IdResolver, IsModule,
3779 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003780
Douglas Gregore6648fb2009-04-28 20:33:11 +00003781 // Look for any identifiers that were named while processing the
3782 // headers, but are otherwise not needed. We add these to the hash
3783 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003784 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003785 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003786 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003787 for (const auto &ID : PP.getIdentifierTable())
3788 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003789 // Sort the identifiers lexicographically before getting them references so
3790 // that their order is stable.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00003791 llvm::sort(IIs, llvm::deref<std::less<>>());
Chandler Carruth8440c982015-03-26 23:54:15 +00003792 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003793 if (Trait.isInterestingNonMacroIdentifier(II))
3794 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003795
Sebastian Redlff4a2952010-07-23 23:49:55 +00003796 // Create the on-disk hash table representation. We only store offsets
3797 // for identifiers that appear here for the first time.
3798 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003799 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003800 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003801 IdentID ID = IdentIDPair.second;
3802 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003803 // Write out identifiers if either the ID is local or the identifier has
3804 // changed since it was loaded.
3805 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3806 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003807 (Trait.needDecls() &&
3808 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003809 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003810 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003811
Douglas Gregore84a9da2009-04-20 20:36:09 +00003812 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003813 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003814 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003815 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003816 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003817
Douglas Gregore84a9da2009-04-20 20:36:09 +00003818 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003819 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003820 endian::write<uint32_t>(Out, 0, little);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003821 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003822 }
3823
3824 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003825 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003826 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003829 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003830
3831 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003832 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003833 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003834 }
3835
3836 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003837 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003838 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003842 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003843
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003844#ifndef NDEBUG
3845 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3846 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3847#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003848
3849 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3850 IdentifierOffsets.size(),
3851 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003852 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003853 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003854
3855 // In C++, write the list of interesting identifiers (those that are
3856 // defined as macros, poisoned, or similar unusual things).
3857 if (!InterestingIdents.empty())
3858 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003859}
3860
Douglas Gregorc5046832009-04-27 18:38:38 +00003861//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003862// DeclContext's Name Lookup Table Serialization
3863//===----------------------------------------------------------------------===//
3864
3865namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003866
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003867// Trait used for the on-disk hash table used in the method pool.
3868class ASTDeclContextNameLookupTrait {
3869 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003870 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003871
3872public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003873 using key_type = DeclarationNameKey;
3874 using key_type_ref = key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003875
Richard Smithd88a7f12015-09-01 20:35:42 +00003876 /// A start and end index into DeclIDs, representing a sequence of decls.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003877 using data_type = std::pair<unsigned, unsigned>;
3878 using data_type_ref = const data_type &;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003879
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003880 using hash_value_type = unsigned;
3881 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003882
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003883 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003884
Richard Smithd88a7f12015-09-01 20:35:42 +00003885 template<typename Coll>
3886 data_type getData(const Coll &Decls) {
3887 unsigned Start = DeclIDs.size();
3888 for (NamedDecl *D : Decls) {
3889 DeclIDs.push_back(
3890 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3891 }
3892 return std::make_pair(Start, DeclIDs.size());
3893 }
3894
3895 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3896 unsigned Start = DeclIDs.size();
3897 for (auto ID : FromReader)
3898 DeclIDs.push_back(ID);
3899 return std::make_pair(Start, DeclIDs.size());
3900 }
3901
3902 static bool EqualKey(key_type_ref a, key_type_ref b) {
3903 return a == b;
3904 }
3905
Richard Smitha06c7e62015-08-26 23:55:49 +00003906 hash_value_type ComputeHash(DeclarationNameKey Name) {
3907 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003908 }
3909
Richard Smithd88a7f12015-09-01 20:35:42 +00003910 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3911 assert(Writer.hasChain() &&
3912 "have reference to loaded module file but no chain?");
3913
3914 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003915
Peter Collingbournee3f65292018-05-18 19:46:24 +00003916 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
Richard Smithd88a7f12015-09-01 20:35:42 +00003917 }
3918
Richard Smitha06c7e62015-08-26 23:55:49 +00003919 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3920 DeclarationNameKey Name,
3921 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003922 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003923
Peter Collingbournee3f65292018-05-18 19:46:24 +00003924 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003925 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003926 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003927 case DeclarationName::Identifier:
3928 case DeclarationName::ObjCZeroArgSelector:
3929 case DeclarationName::ObjCOneArgSelector:
3930 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003931 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003932 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003933 KeyLen += 4;
3934 break;
3935 case DeclarationName::CXXOperatorName:
3936 KeyLen += 1;
3937 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003938 case DeclarationName::CXXConstructorName:
3939 case DeclarationName::CXXDestructorName:
3940 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003941 case DeclarationName::CXXUsingDirective:
3942 break;
3943 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003944 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003945
Richard Smithf02662d2015-07-30 03:17:16 +00003946 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003947 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3948 assert(uint16_t(DataLen) == DataLen &&
3949 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003950 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003951
3952 return std::make_pair(KeyLen, DataLen);
3953 }
3954
Richard Smitha06c7e62015-08-26 23:55:49 +00003955 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003956 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003957
Peter Collingbournee3f65292018-05-18 19:46:24 +00003958 endian::Writer LE(Out, little);
Richard Smitha06c7e62015-08-26 23:55:49 +00003959 LE.write<uint8_t>(Name.getKind());
3960 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003961 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003962 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003963 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003964 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003965 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003966 case DeclarationName::ObjCZeroArgSelector:
3967 case DeclarationName::ObjCOneArgSelector:
3968 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003969 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003970 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003971 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003972 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003973 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003974 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003975 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003976 case DeclarationName::CXXConstructorName:
3977 case DeclarationName::CXXDestructorName:
3978 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003979 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003980 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003981 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003982
3983 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003984 }
3985
Richard Smitha06c7e62015-08-26 23:55:49 +00003986 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3987 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003988 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003989
Peter Collingbournee3f65292018-05-18 19:46:24 +00003990 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003991 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003992 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3993 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003994 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3995 }
3996};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003997
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003998} // namespace
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003999
Chandler Carruthe972c362015-03-26 03:11:40 +00004000bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
4001 DeclContext *DC) {
Erich Keanef92f31c2018-08-01 20:48:16 +00004002 return Result.hasExternalDecls() &&
4003 DC->hasNeedToReconcileExternalVisibleStorage();
Chandler Carruthe972c362015-03-26 03:11:40 +00004004}
4005
4006bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
4007 DeclContext *DC) {
4008 for (auto *D : Result.getLookupResult())
4009 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
4010 return false;
4011
4012 return true;
4013}
4014
Richard Smithd88a7f12015-09-01 20:35:42 +00004015void
Chandler Carruthe972c362015-03-26 03:11:40 +00004016ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00004017 llvm::SmallVectorImpl<char> &LookupTable) {
Erich Keanef92f31c2018-08-01 20:48:16 +00004018 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
4019 !ConstDC->hasLazyExternalLexicalLookups() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00004020 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00004021
Chandler Carruthe972c362015-03-26 03:11:40 +00004022 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004023 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00004024 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
4025
4026 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00004027 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
4028 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004029 ASTDeclContextNameLookupTrait Trait(*this);
4030
Chandler Carruthe972c362015-03-26 03:11:40 +00004031 // The first step is to collect the declaration names which we need to
4032 // serialize into the name lookup table, and to collect them in a stable
4033 // order.
4034 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004035
Chandler Carruthe972c362015-03-26 03:11:40 +00004036 // We also build up small sets of the constructor and conversion function
4037 // names which are visible.
4038 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004039
Chandler Carruthe972c362015-03-26 03:11:40 +00004040 for (auto &Lookup : *DC->buildLookup()) {
4041 auto &Name = Lookup.first;
4042 auto &Result = Lookup.second;
4043
Douglas Gregor7dd37e52015-11-03 01:20:54 +00004044 // If there are no local declarations in our lookup result, we
4045 // don't need to write an entry for the name at all. If we can't
4046 // write out a lookup set without performing more deserialization,
4047 // just skip this entry.
4048 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00004049 isLookupResultEntirelyExternal(Result, DC))
4050 continue;
4051
4052 // We also skip empty results. If any of the results could be external and
4053 // the currently available results are empty, then all of the results are
4054 // external and we skip it above. So the only way we get here with an empty
4055 // results is when no results could have been external *and* we have
4056 // external results.
4057 //
4058 // FIXME: While we might want to start emitting on-disk entries for negative
4059 // lookups into a decl context as an optimization, today we *have* to skip
4060 // them because there are names with empty lookup results in decl contexts
4061 // which we can't emit in any stable ordering: we lookup constructors and
4062 // conversion functions in the enclosing namespace scope creating empty
4063 // results for them. This in almost certainly a bug in Clang's name lookup,
4064 // but that is likely to be hard or impossible to fix and so we tolerate it
4065 // here by omitting lookups with empty results.
4066 if (Lookup.second.getLookupResult().empty())
4067 continue;
4068
4069 switch (Lookup.first.getNameKind()) {
4070 default:
4071 Names.push_back(Lookup.first);
4072 break;
4073
Richard Smithcd45dbc2014-04-19 03:48:30 +00004074 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004075 assert(isa<CXXRecordDecl>(DC) &&
4076 "Cannot have a constructor name outside of a class!");
4077 ConstructorNameSet.insert(Name);
4078 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004079
4080 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004081 assert(isa<CXXRecordDecl>(DC) &&
4082 "Cannot have a conversion function name outside of a class!");
4083 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00004084 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004085 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004086 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004087
Chandler Carruthe972c362015-03-26 03:11:40 +00004088 // Sort the names into a stable order.
Fangrui Song55fab262018-09-26 22:16:28 +00004089 llvm::sort(Names);
Chandler Carruthe972c362015-03-26 03:11:40 +00004090
Chandler Carruthffbf7052015-03-26 22:27:09 +00004091 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00004092 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00004093 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00004094
Chandler Carruthffbf7052015-03-26 22:27:09 +00004095 // First we try the easy case by forming the current context's constructor
4096 // name and adding that name first. This is a very useful optimization to
4097 // avoid walking the lexical declarations in many cases, and it also
4098 // handles the only case where a constructor name can come from some other
4099 // lexical context -- when that name is an implicit constructor merged from
4100 // another declaration in the redecl chain. Any non-implicit constructor or
4101 // conversion function which doesn't occur in all the lexical contexts
4102 // would be an ODR violation.
4103 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4104 Context->getCanonicalType(Context->getRecordType(D)));
4105 if (ConstructorNameSet.erase(ImplicitCtorName))
4106 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00004107
Chandler Carruthffbf7052015-03-26 22:27:09 +00004108 // If we still have constructors or conversion functions, we walk all the
4109 // names in the decl and add the constructors and conversion functions
4110 // which are visible in the order they lexically occur within the context.
4111 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4112 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4113 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4114 auto Name = ChildND->getDeclName();
4115 switch (Name.getNameKind()) {
4116 default:
4117 continue;
4118
4119 case DeclarationName::CXXConstructorName:
4120 if (ConstructorNameSet.erase(Name))
4121 Names.push_back(Name);
4122 break;
4123
4124 case DeclarationName::CXXConversionFunctionName:
4125 if (ConversionNameSet.erase(Name))
4126 Names.push_back(Name);
4127 break;
4128 }
4129
4130 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4131 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00004132 }
4133
Chandler Carruthe972c362015-03-26 03:11:40 +00004134 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4135 "constructors by walking all the "
4136 "lexical members of the context.");
4137 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4138 "conversion functions by walking all "
4139 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00004140 }
4141
Chandler Carruthe972c362015-03-26 03:11:40 +00004142 // Next we need to do a lookup with each name into this decl context to fully
4143 // populate any results from external sources. We don't actually use the
4144 // results of these lookups because we only want to use the results after all
4145 // results have been loaded and the pointers into them will be stable.
4146 for (auto &Name : Names)
4147 DC->lookup(Name);
4148
4149 // Now we need to insert the results for each name into the hash table. For
4150 // constructor names and conversion function names, we actually need to merge
4151 // all of the results for them into one list of results each and insert
4152 // those.
4153 SmallVector<NamedDecl *, 8> ConstructorDecls;
4154 SmallVector<NamedDecl *, 8> ConversionDecls;
4155
4156 // Now loop over the names, either inserting them or appending for the two
4157 // special cases.
4158 for (auto &Name : Names) {
4159 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4160
4161 switch (Name.getNameKind()) {
4162 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00004163 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004164 break;
4165
4166 case DeclarationName::CXXConstructorName:
4167 ConstructorDecls.append(Result.begin(), Result.end());
4168 break;
4169
4170 case DeclarationName::CXXConversionFunctionName:
4171 ConversionDecls.append(Result.begin(), Result.end());
4172 break;
4173 }
4174 }
4175
4176 // Handle our two special cases if we ended up having any. We arbitrarily use
4177 // the first declaration's name here because the name itself isn't part of
4178 // the key, only the kind of name is used.
4179 if (!ConstructorDecls.empty())
4180 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004181 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004182 if (!ConversionDecls.empty())
4183 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004184 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004185
Richard Smithd88a7f12015-09-01 20:35:42 +00004186 // Create the on-disk hash table. Also emit the existing imported and
4187 // merged table if there is one.
4188 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4189 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00004190}
4191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004192/// Write the block containing all of the declaration IDs
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004193/// visible from the given DeclContext.
4194///
4195/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00004196/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004197uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4198 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00004199 // If we imported a key declaration of this namespace, write the visible
4200 // lookup results as an update record for it rather than including them
4201 // on this declaration. We will only look at key declarations on reload.
4202 if (isa<NamespaceDecl>(DC) && Chain &&
4203 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4204 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004205 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00004206 Prev = Prev->getPreviousDecl())
4207 if (!Prev->isFromASTFile())
4208 return 0;
4209
4210 // Note that we need to emit an update record for the primary context.
4211 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4212
4213 // Make sure all visible decls are written. They will be recorded later. We
4214 // do this using a side data structure so we can sort the names into
4215 // a deterministic order.
4216 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4217 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4218 LookupResults;
4219 if (Map) {
4220 LookupResults.reserve(Map->size());
4221 for (auto &Entry : *Map)
4222 LookupResults.push_back(
4223 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4224 }
4225
Fangrui Song55fab262018-09-26 22:16:28 +00004226 llvm::sort(LookupResults, llvm::less_first());
Richard Smith5fc18a92015-07-12 23:43:21 +00004227 for (auto &NameAndResult : LookupResults) {
4228 DeclarationName Name = NameAndResult.first;
4229 DeclContext::lookup_result Result = NameAndResult.second;
4230 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4231 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4232 // We have to work around a name lookup bug here where negative lookup
4233 // results for these names get cached in namespace lookup tables (these
4234 // names should never be looked up in a namespace).
4235 assert(Result.empty() && "Cannot have a constructor or conversion "
4236 "function name in a namespace!");
4237 continue;
4238 }
4239
4240 for (NamedDecl *ND : Result)
4241 if (!ND->isFromASTFile())
4242 GetDeclRef(ND);
4243 }
4244
4245 return 0;
4246 }
4247
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004248 if (DC->getPrimaryContext() != DC)
4249 return 0;
4250
Chandler Carruthe972c362015-03-26 03:11:40 +00004251 // Skip contexts which don't support name lookup.
4252 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004253 return 0;
4254
4255 // If not in C++, we perform name lookup for the translation unit via the
4256 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004257 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004258 return 0;
4259
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004260 // Serialize the contents of the mapping used for lookup. Note that,
4261 // although we have two very different code paths, the serialized
4262 // representation is the same for both cases: a declaration name,
4263 // followed by a size, followed by references to the visible
4264 // declarations that have that name.
4265 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00004266 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004267 if (!Map || Map->empty())
4268 return 0;
4269
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004270 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004271 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004272 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004273
4274 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004275 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004276 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00004277 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004278 ++NumVisibleDeclContexts;
4279 return Offset;
4280}
4281
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004282/// Write an UPDATE_VISIBLE block for the given context.
Sebastian Redla4071b42010-08-24 00:50:09 +00004283///
4284/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4285/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00004286/// (in C++), for namespaces, and for classes with forward-declared unscoped
4287/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00004288void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00004289 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00004290 if (!Map || Map->empty())
4291 return;
4292
Sebastian Redla4071b42010-08-24 00:50:09 +00004293 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004294 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004295 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004296
Richard Smith5fc18a92015-07-12 23:43:21 +00004297 // If we're updating a namespace, select a key declaration as the key for the
4298 // update record; those are the only ones that will be checked on reload.
4299 if (isa<NamespaceDecl>(DC))
4300 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4301
Sebastian Redla4071b42010-08-24 00:50:09 +00004302 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004303 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004304 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004305}
4306
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004307/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004308void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004309 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004310 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4311}
4312
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004313/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004314void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004315 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004316 return;
4317
4318 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4319 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004320 for (const auto &I:Opts.OptMap) {
4321 AddString(I.getKey(), Record);
4322 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004323 Record.push_back(V.Supported ? 1 : 0);
4324 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004325 Record.push_back(V.Avail);
4326 Record.push_back(V.Core);
4327 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004328 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4329}
4330
Yaxun Liu5b746652016-12-18 05:18:55 +00004331void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4332 if (!SemaRef.Context.getLangOpts().OpenCL)
4333 return;
4334
Bruno Ricci2751b692019-04-18 15:13:27 +00004335 // Sort the elements of the map OpenCLTypeExtMap by TypeIDs,
4336 // without copying them.
4337 const llvm::DenseMap<const Type *, std::set<std::string>> &OpenCLTypeExtMap =
4338 SemaRef.OpenCLTypeExtMap;
4339 using ElementTy = std::pair<TypeID, const std::set<std::string> *>;
4340 llvm::SmallVector<ElementTy, 8> StableOpenCLTypeExtMap;
4341 StableOpenCLTypeExtMap.reserve(OpenCLTypeExtMap.size());
4342
4343 for (const auto &I : OpenCLTypeExtMap)
4344 StableOpenCLTypeExtMap.emplace_back(
4345 getTypeID(I.first->getCanonicalTypeInternal()), &I.second);
4346
4347 auto CompareByTypeID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
4348 return E1.first < E2.first;
4349 };
4350 llvm::sort(StableOpenCLTypeExtMap, CompareByTypeID);
4351
Yaxun Liu5b746652016-12-18 05:18:55 +00004352 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00004353 for (const ElementTy &E : StableOpenCLTypeExtMap) {
4354 Record.push_back(E.first); // TypeID
4355 const std::set<std::string> *ExtSet = E.second;
4356 Record.push_back(static_cast<unsigned>(ExtSet->size()));
4357 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00004358 AddString(Ext, Record);
4359 }
Bruno Ricci2751b692019-04-18 15:13:27 +00004360
Yaxun Liu5b746652016-12-18 05:18:55 +00004361 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4362}
4363
4364void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4365 if (!SemaRef.Context.getLangOpts().OpenCL)
4366 return;
4367
Bruno Ricci2751b692019-04-18 15:13:27 +00004368 // Sort the elements of the map OpenCLDeclExtMap by DeclIDs,
4369 // without copying them.
4370 const llvm::DenseMap<const Decl *, std::set<std::string>> &OpenCLDeclExtMap =
4371 SemaRef.OpenCLDeclExtMap;
4372 using ElementTy = std::pair<DeclID, const std::set<std::string> *>;
4373 llvm::SmallVector<ElementTy, 8> StableOpenCLDeclExtMap;
4374 StableOpenCLDeclExtMap.reserve(OpenCLDeclExtMap.size());
4375
4376 for (const auto &I : OpenCLDeclExtMap)
4377 StableOpenCLDeclExtMap.emplace_back(getDeclID(I.first), &I.second);
4378
4379 auto CompareByDeclID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
4380 return E1.first < E2.first;
4381 };
4382 llvm::sort(StableOpenCLDeclExtMap, CompareByDeclID);
4383
Yaxun Liu5b746652016-12-18 05:18:55 +00004384 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00004385 for (const ElementTy &E : StableOpenCLDeclExtMap) {
4386 Record.push_back(E.first); // DeclID
4387 const std::set<std::string> *ExtSet = E.second;
4388 Record.push_back(static_cast<unsigned>(ExtSet->size()));
4389 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00004390 AddString(Ext, Record);
4391 }
Bruno Ricci2751b692019-04-18 15:13:27 +00004392
Yaxun Liu5b746652016-12-18 05:18:55 +00004393 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4394}
4395
Justin Lebar67a78a62016-10-08 22:15:58 +00004396void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4397 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4398 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4399 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4400 }
4401}
4402
Douglas Gregor404cdde2012-01-27 01:47:08 +00004403void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004404 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004405 RecordData Categories;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004406
Douglas Gregor404cdde2012-01-27 01:47:08 +00004407 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4408 unsigned Size = 0;
4409 unsigned StartIndex = Categories.size();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004410
Douglas Gregor404cdde2012-01-27 01:47:08 +00004411 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004412
Douglas Gregor404cdde2012-01-27 01:47:08 +00004413 // Allocate space for the size.
4414 Categories.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004415
Douglas Gregor404cdde2012-01-27 01:47:08 +00004416 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004417 for (ObjCInterfaceDecl::known_categories_iterator
4418 Cat = Class->known_categories_begin(),
4419 CatEnd = Class->known_categories_end();
4420 Cat != CatEnd; ++Cat, ++Size) {
4421 assert(getDeclID(*Cat) != 0 && "Bogus category");
4422 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004423 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004424
Douglas Gregor404cdde2012-01-27 01:47:08 +00004425 // Update the size.
4426 Categories[StartIndex] = Size;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004427
Douglas Gregor404cdde2012-01-27 01:47:08 +00004428 // Record this interface -> category map.
4429 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4430 CategoriesMap.push_back(CatInfo);
4431 }
4432
4433 // Sort the categories map by the definition ID, since the reader will be
4434 // performing binary searches on this information.
4435 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4436
4437 // Emit the categories map.
4438 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004439
David Blaikieb44f0bf2017-01-04 22:36:43 +00004440 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004441 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004444 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004445
4446 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4447 Stream.EmitRecordWithBlob(AbbrevID, Record,
4448 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004449 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004450
Douglas Gregor404cdde2012-01-27 01:47:08 +00004451 // Emit the category lists.
4452 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4453}
4454
Richard Smithe40f2ba2013-08-07 21:41:30 +00004455void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4456 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4457
4458 if (LPTMap.empty())
4459 return;
4460
4461 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004462 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004463 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004464 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004465 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004466 AddDeclRef(LPT.D, Record);
4467 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004468
Justin Lebar28f09c52016-10-10 16:26:08 +00004469 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004470 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004471 }
4472 }
4473 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4474}
4475
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004476/// Write the state of 'pragma clang optimize' at the end of the module.
Dario Domizioli13a0a382014-05-23 12:13:25 +00004477void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4478 RecordData Record;
4479 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4480 AddSourceLocation(PragmaLoc, Record);
4481 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4482}
4483
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004484/// Write the state of 'pragma ms_struct' at the end of the module.
Nico Weber779355f2016-03-02 23:22:00 +00004485void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4486 RecordData Record;
4487 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4488 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4489}
4490
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004491/// Write the state of 'pragma pointers_to_members' at the end of the
Nico Weber42932312016-03-03 00:17:35 +00004492//module.
4493void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4494 RecordData Record;
4495 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4496 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4497 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4498}
4499
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004500/// Write the state of 'pragma pack' at the end of the module.
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004501void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004502 // Don't serialize pragma pack state for modules, since it should only take
4503 // effect on a per-submodule basis.
4504 if (WritingModule)
4505 return;
4506
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004507 RecordData Record;
4508 Record.push_back(SemaRef.PackStack.CurrentValue);
4509 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4510 Record.push_back(SemaRef.PackStack.Stack.size());
4511 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4512 Record.push_back(StackEntry.Value);
4513 AddSourceLocation(StackEntry.PragmaLocation, Record);
Alex Lorenz45b40142017-07-28 14:41:21 +00004514 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004515 AddString(StackEntry.StackSlotLabel, Record);
4516 }
4517 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4518}
4519
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004520void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4521 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004522 // Enter the extension block.
4523 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4524
4525 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004526 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004527 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4528 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4529 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4530 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4531 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4532 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004533 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004534
4535 // Emit the metadata record.
4536 RecordData Record;
4537 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4538 Record.push_back(EXTENSION_METADATA);
4539 Record.push_back(Metadata.MajorVersion);
4540 Record.push_back(Metadata.MinorVersion);
4541 Record.push_back(Metadata.BlockName.size());
4542 Record.push_back(Metadata.UserInfo.size());
4543 SmallString<64> Buffer;
4544 Buffer += Metadata.BlockName;
4545 Buffer += Metadata.UserInfo;
4546 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4547
4548 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004549 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004550
4551 // Exit the extension block.
4552 Stream.ExitBlock();
4553}
4554
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004555//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004556// General Serialization Routines
4557//===----------------------------------------------------------------------===//
4558
Richard Smithe43e2b32018-08-20 21:47:29 +00004559void ASTRecordWriter::AddAttr(const Attr *A) {
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004560 auto &Record = *this;
Richard Smithe43e2b32018-08-20 21:47:29 +00004561 if (!A)
4562 return Record.push_back(0);
4563 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
Erich Keane6a24e802019-09-13 17:39:31 +00004564
4565 Record.AddIdentifierRef(A->getAttrName());
4566 Record.AddIdentifierRef(A->getScopeName());
Richard Smithe43e2b32018-08-20 21:47:29 +00004567 Record.AddSourceRange(A->getRange());
Erich Keane6a24e802019-09-13 17:39:31 +00004568 Record.AddSourceLocation(A->getScopeLoc());
4569 Record.push_back(A->getParsedKind());
4570 Record.push_back(A->getSyntax());
4571 Record.push_back(A->getAttributeSpellingListIndexRaw());
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004572
4573#include "clang/Serialization/AttrPCHWrite.inc"
Richard Smithe43e2b32018-08-20 21:47:29 +00004574}
4575
4576/// Emit the list of attributes to the specified record.
4577void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4578 push_back(Attrs.size());
4579 for (const auto *A : Attrs)
4580 AddAttr(A);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004581}
4582
John McCallf413f5e2013-05-03 00:10:13 +00004583void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4584 AddSourceLocation(Tok.getLocation(), Record);
4585 Record.push_back(Tok.getLength());
4586
4587 // FIXME: When reading literal tokens, reconstruct the literal pointer
4588 // if it is needed.
4589 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4590 // FIXME: Should translate token kind to a stable encoding.
4591 Record.push_back(Tok.getKind());
4592 // FIXME: Should translate token flags to a stable encoding.
4593 Record.push_back(Tok.getFlags());
4594}
4595
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004596void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004597 Record.push_back(Str.size());
4598 Record.insert(Record.end(), Str.begin(), Str.end());
4599}
4600
Richard Smith7ed1bc92014-12-05 22:42:13 +00004601bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004602 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004603
Richard Smith54cc3c22014-12-11 20:50:24 +00004604 bool Changed =
4605 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004606
4607 // Remove a prefix to make the path relative, if relevant.
4608 const char *PathBegin = Path.data();
4609 const char *PathPtr =
4610 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4611 if (PathPtr != PathBegin) {
4612 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4613 Changed = true;
4614 }
4615
4616 return Changed;
4617}
4618
4619void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4620 SmallString<128> FilePath(Path);
4621 PreparePathForOutput(FilePath);
4622 AddString(FilePath, Record);
4623}
4624
Mehdi Amini57a41912015-09-10 01:46:39 +00004625void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004626 StringRef Path) {
4627 SmallString<128> FilePath(Path);
4628 PreparePathForOutput(FilePath);
4629 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4630}
4631
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004632void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4633 RecordDataImpl &Record) {
4634 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004635 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004636 Record.push_back(*Minor + 1);
4637 else
4638 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004639 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004640 Record.push_back(*Subminor + 1);
4641 else
4642 Record.push_back(0);
4643}
4644
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004645/// Note that the identifier II occurs at the given offset
Douglas Gregore84a9da2009-04-20 20:36:09 +00004646/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004647void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004648 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004649 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004650 // up earlier in the chain and thus don't need an offset.
4651 if (ID >= FirstIdentID)
4652 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004653}
4654
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004655/// Note that the selector Sel occurs at the given offset
Douglas Gregor95c13f52009-04-25 17:48:32 +00004656/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004657void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004658 unsigned ID = SelectorIDs[Sel];
4659 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004660 // Don't record offsets for selectors that are also available in a different
4661 // file.
4662 if (ID < FirstSelectorID)
4663 return;
4664 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004665}
4666
David Blaikie61137e12017-01-05 18:23:18 +00004667ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004668 SmallVectorImpl<char> &Buffer,
4669 InMemoryModuleCache &ModuleCache,
David Blaikie61137e12017-01-05 18:23:18 +00004670 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4671 bool IncludeTimestamps)
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004672 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004673 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004674 for (const auto &Ext : Extensions) {
4675 if (auto Writer = Ext->createExtensionWriter(*this))
4676 ModuleFileExtensionWriters.push_back(std::move(Writer));
4677 }
4678}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004679
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004680ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004681 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004682}
4683
Richard Smithb3761912015-02-05 23:08:52 +00004684const LangOptions &ASTWriter::getLangOpts() const {
4685 assert(WritingAST && "can't determine lang opts when not writing AST");
4686 return Context->getLangOpts();
4687}
4688
Richard Smithe75ee0f2015-08-17 07:13:32 +00004689time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4690 return IncludeTimestamps ? E->getModificationTime() : 0;
4691}
4692
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004693ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4694 const std::string &OutputFile,
4695 Module *WritingModule, StringRef isysroot,
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004696 bool hasErrors,
4697 bool ShouldCacheASTInMemory) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004698 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004699
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004700 ASTHasCompilerErrors = hasErrors;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004701
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004702 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004703 Stream.Emit((unsigned)'C', 8);
4704 Stream.Emit((unsigned)'P', 8);
4705 Stream.Emit((unsigned)'C', 8);
4706 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004707
Chris Lattner28fa4e62009-04-26 22:26:21 +00004708 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004709
Douglas Gregoreda8e122011-08-09 15:13:55 +00004710 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004711 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004712 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004713 ASTFileSignature Signature =
4714 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004715 Context = nullptr;
4716 PP = nullptr;
4717 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004718 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004719
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004720 WritingAST = false;
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004721 if (ShouldCacheASTInMemory) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004722 // Construct MemoryBuffer and update buffer manager.
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004723 ModuleCache.addBuiltPCM(OutputFile,
4724 llvm::MemoryBuffer::getMemBufferCopy(
4725 StringRef(Buffer.begin(), Buffer.size())));
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004726 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004727 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004728}
4729
Douglas Gregora94a1542011-07-27 21:45:57 +00004730template<typename Vector>
4731static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4732 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004733 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4734 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004735 Writer.AddDeclRef(*I, Record);
4736 }
4737}
4738
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004739ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4740 const std::string &OutputFile,
4741 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004742 using namespace llvm;
4743
Craig Toppera13603a2014-05-22 05:54:18 +00004744 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004745
Douglas Gregorcf68c582011-12-01 22:20:10 +00004746 // Make sure that the AST reader knows to finalize itself.
4747 if (Chain)
4748 Chain->finalizeForWriting();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004749
Sebastian Redl143413f2010-07-12 22:02:52 +00004750 ASTContext &Context = SemaRef.Context;
4751 Preprocessor &PP = SemaRef.PP;
4752
Douglas Gregordab42432011-08-12 00:15:20 +00004753 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004754 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4755 if (D) {
4756 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4757 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004758 }
4759 };
4760 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4761 PREDEF_DECL_TRANSLATION_UNIT_ID);
4762 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4763 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4764 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4765 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4766 PREDEF_DECL_OBJC_PROTOCOL_ID);
4767 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4768 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4769 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4770 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4771 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004772 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004773 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4774 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004775 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004776 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4777 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004778 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4779 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004780 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4781 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004782 RegisterPredefDecl(Context.TypePackElementDecl,
4783 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004784
Chris Lattner0c797362009-09-08 18:19:27 +00004785 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004786 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004787 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004788 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004789 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004790
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004791 // Build a record containing all of the file scoped decls in this file.
4792 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004793 if (!isModule)
4794 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4795 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004796
Douglas Gregor851443c2011-08-12 01:39:19 +00004797 // Build a record containing all of the delegating constructors we still need
4798 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004799 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004800 if (!isModule)
4801 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004802
Douglas Gregor851443c2011-08-12 01:39:19 +00004803 // Write the set of weak, undeclared identifiers. We always write the
4804 // entire table, since later PCH files in a PCH chain are only interested in
4805 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004806 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004807 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4808 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4809 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4810 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4811 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4812 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4813 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004814 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004815
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004816 // Build a record containing all of the ext_vector declarations.
4817 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004818 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004819
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004820 // Build a record containing all of the VTable uses information.
4821 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004822 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004823 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4824 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4825 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4826 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4827 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004828 }
4829
Nico Weber72889432014-09-06 01:25:55 +00004830 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4831 RecordData UnusedLocalTypedefNameCandidates;
4832 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4833 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4834
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004835 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004836 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004837 for (const auto &I : SemaRef.PendingInstantiations) {
4838 AddDeclRef(I.first, PendingInstantiations);
4839 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004840 }
4841 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4842 "There are local ones at end of translation unit!");
4843
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004844 // Build a record containing some declaration references.
4845 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004846 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004847 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4848 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004849 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004850 }
4851
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004852 RecordData CUDASpecialDeclRefs;
4853 if (Context.getcudaConfigureCallDecl()) {
4854 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4855 }
4856
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004857 // Build a record containing all of the known namespaces.
4858 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004859 for (const auto &I : SemaRef.KnownNamespaces) {
4860 if (!I.second)
4861 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004862 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004863
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004864 // Build a record of all used, undefined objects that require definitions.
4865 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004866
4867 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004868 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004869 for (const auto &I : Undefined) {
4870 AddDeclRef(I.first, UndefinedButUsed);
4871 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004872 }
4873
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004874 // Build a record containing all delete-expressions that we would like to
4875 // analyze later in AST.
4876 RecordData DeleteExprsToAnalyze;
4877
Richard Smithf5262c62018-06-28 01:57:04 +00004878 if (!isModule) {
4879 for (const auto &DeleteExprsInfo :
4880 SemaRef.getMismatchingDeleteExpressions()) {
4881 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4882 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4883 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4884 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4885 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4886 }
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004887 }
4888 }
4889
Douglas Gregor112b9072012-10-18 05:31:06 +00004890 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004891 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004892
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004893 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004894 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004895
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004896 // This is so that older clang versions, before the introduction
4897 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004898 {
4899 RecordData Record = {VERSION_MAJOR};
4900 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4901 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004902
Douglas Gregor851443c2011-08-12 01:39:19 +00004903 // Create a lexical update block containing all of the declarations in the
4904 // translation unit that do not come from other AST files.
4905 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004906 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4907 for (const auto *D : TU->noload_decls()) {
4908 if (!D->isFromASTFile()) {
4909 NewGlobalKindDeclPairs.push_back(D->getKind());
4910 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4911 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004912 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004913
David Blaikieb44f0bf2017-01-04 22:36:43 +00004914 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004915 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4916 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004917 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004918 {
4919 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4920 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4921 bytes(NewGlobalKindDeclPairs));
4922 }
4923
Douglas Gregor851443c2011-08-12 01:39:19 +00004924 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004925 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004926 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4927 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004928 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004929 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004930 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004931
4932 // If we have any extern "C" names, write out a visible update for them.
4933 if (Context.ExternCContext)
4934 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004935
Douglas Gregor851443c2011-08-12 01:39:19 +00004936 // If the translation unit has an anonymous namespace, and we don't already
4937 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004938 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004939 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4940 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004941 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004942 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004943 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004944
Richard Smith5652c0f2014-03-21 01:48:23 +00004945 // Add update records for all mangling numbers and static local numbers.
4946 // These aren't really update records, but this is a convenient way of
4947 // tagging this rare extra data onto the declarations.
4948 for (const auto &Number : Context.MangleNumbers)
4949 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004950 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4951 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004952 for (const auto &Number : Context.StaticLocalNumbers)
4953 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004954 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4955 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004956
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004957 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004958 // an AST file, are registered for serialization. Likewise for template
4959 // specializations added to imported templates.
4960 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004961 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004962 }
4963
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004964 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004965 // serialization, if we're storing decls with identifiers.
4966 if (!WritingModule || !getLangOpts().CPlusPlus) {
4967 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004968 for (const auto &ID : PP.getIdentifierTable()) {
4969 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004970 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4971 IIs.push_back(II);
4972 }
4973 // Sort the identifiers to visit based on their name.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00004974 llvm::sort(IIs, llvm::deref<std::less<>>());
Richard Smith79bf9202015-08-24 03:33:22 +00004975 for (const IdentifierInfo *II : IIs) {
4976 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4977 DEnd = SemaRef.IdResolver.end();
4978 D != DEnd; ++D) {
4979 GetDeclRef(*D);
4980 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004981 }
4982 }
4983
Manman Rena0f31a02016-04-29 19:04:05 +00004984 // For method pool in the module, if it contains an entry for a selector,
4985 // the entry should be complete, containing everything introduced by that
4986 // module and all modules it imports. It's possible that the entry is out of
4987 // date, so we need to pull in the new content here.
4988
4989 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4990 // safe, we copy all selectors out.
4991 llvm::SmallVector<Selector, 256> AllSelectors;
4992 for (auto &SelectorAndID : SelectorIDs)
4993 AllSelectors.push_back(SelectorAndID.first);
4994 for (auto &Selector : AllSelectors)
4995 SemaRef.updateOutOfDateSelector(Selector);
4996
Douglas Gregor5204bde2011-08-02 16:26:37 +00004997 // Form the record of special types.
4998 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004999 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00005000 AddTypeRef(Context.getFILEType(), SpecialTypes);
5001 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
5002 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
5003 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
5004 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00005005 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00005006 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00005007
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005008 if (Chain) {
5009 // Write the mapping information describing our module dependencies and how
5010 // each of those modules were mapped into our own offset/ID space, so that
5011 // the reader can build the appropriate mapping to its own offset/ID space.
5012 // The map consists solely of a blob with the following format:
Boris Kolpackovd30446f2017-08-31 06:26:43 +00005013 // *(module-kind:i8
5014 // module-name-len:i16 module-name:len*i8
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005015 // source-location-offset:i32
5016 // identifier-id:i32
5017 // preprocessed-entity-id:i32
5018 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00005019 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005020 // selector-id:i32
5021 // declaration-id:i32
5022 // c++-base-specifiers-id:i32
5023 // type-id:i32)
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005024 //
Boris Kolpackovd30446f2017-08-31 06:26:43 +00005025 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule or
5026 // MK_ExplicitModule, then the module-name is the module name. Otherwise,
5027 // it is the module file name.
David Blaikieb44f0bf2017-01-04 22:36:43 +00005028 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005029 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
5030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00005031 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005032 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005033 {
5034 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00005035 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00005036 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00005037
Peter Collingbournee3f65292018-05-18 19:46:24 +00005038 endian::Writer LE(Out, little);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00005039 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
5040 StringRef Name =
5041 M.Kind == MK_PrebuiltModule || M.Kind == MK_ExplicitModule
5042 ? M.ModuleName
5043 : M.FileName;
5044 LE.write<uint16_t>(Name.size());
5045 Out.write(Name.data(), Name.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00005046
Ben Langmuir785180e2014-10-20 16:27:30 +00005047 // Note: if a base ID was uint max, it would not be possible to load
5048 // another module after it or have more than one entity inside it.
5049 uint32_t None = std::numeric_limits<uint32_t>::max();
5050
5051 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
5052 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5053 if (ShouldWrite)
5054 LE.write<uint32_t>(BaseID);
5055 else
5056 LE.write<uint32_t>(None);
5057 };
5058
Ben Langmuirfe971d92014-08-16 04:54:18 +00005059 // These values should be unique within a chain, since they will be read
5060 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00005061 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
5062 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
5063 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5064 writeBaseIDOrNone(M.BasePreprocessedEntityID,
5065 M.NumPreprocessedEntities);
5066 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5067 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5068 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
5069 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005070 }
5071 }
Mehdi Amini57a41912015-09-10 01:46:39 +00005072 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005073 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
5074 Buffer.data(), Buffer.size());
5075 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00005076
Richard Smithb9eab6d2014-03-20 19:44:17 +00005077 RecordData DeclUpdatesOffsetsRecord;
5078
Richard Smith59442b42014-03-20 20:07:19 +00005079 // Keep writing types, declarations, and declaration update records
5080 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00005081 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5082 WriteTypeAbbrevs();
5083 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00005084 do {
5085 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5086 while (!DeclTypesToEmit.empty()) {
5087 DeclOrType DOT = DeclTypesToEmit.front();
5088 DeclTypesToEmit.pop();
5089 if (DOT.isType())
5090 WriteType(DOT.getType());
5091 else
5092 WriteDecl(Context, DOT.getDecl());
5093 }
5094 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00005095 Stream.ExitBlock();
5096
Richard Smithb9eab6d2014-03-20 19:44:17 +00005097 DoneWritingDeclsAndTypes = true;
5098
5099 // These things can only be done once we've written out decls and types.
5100 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00005101 if (!DeclUpdatesOffsetsRecord.empty())
5102 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00005103 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00005104 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00005105 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005106 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005107 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00005108 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00005109 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00005110 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005111 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00005112 WriteFPPragmaOptions(SemaRef.getFPOptions());
5113 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00005114 WriteOpenCLExtensionTypes(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00005115 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00005116
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005117 // If we're emitting a module, write out the submodule information.
Douglas Gregora89c5ac2011-12-06 01:10:29 +00005118 if (WritingModule)
5119 WriteSubmodules(WritingModule);
5120
Andrew Savonichev52d674d2018-10-29 11:14:01 +00005121 // We need to have information about submodules to correctly deserialize
5122 // decls from OpenCLExtensionDecls block
5123 WriteOpenCLExtensionDecls(SemaRef);
5124
Douglas Gregor5204bde2011-08-02 16:26:37 +00005125 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5126
Douglas Gregord4df8652009-04-22 22:02:47 +00005127 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00005128 if (!EagerlyDeserializedDecls.empty())
5129 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00005130
David Blaikief63556d2017-04-12 20:58:33 +00005131 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00005132 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5133
Douglas Gregord4df8652009-04-22 22:02:47 +00005134 // Write the record containing tentative definitions.
5135 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005136 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00005137
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00005138 // Write the record containing unused file scoped decls.
5139 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005140 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005141
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005142 // Write the record containing weak undeclared identifiers.
5143 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005144 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005145 WeakUndeclaredIdentifiers);
5146
Douglas Gregor61cac2b2009-04-27 20:06:05 +00005147 // Write the record containing ext_vector type names.
5148 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005149 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00005150
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005151 // Write the record containing VTable uses information.
5152 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005153 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005154
Nico Weber72889432014-09-06 01:25:55 +00005155 // Write the record containing potentially unused local typedefs.
5156 if (!UnusedLocalTypedefNameCandidates.empty())
5157 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5158 UnusedLocalTypedefNameCandidates);
5159
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005160 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00005161 if (!PendingInstantiations.empty())
5162 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005163
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005164 // Write the record containing declaration references of Sema.
5165 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005166 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005167
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005168 // Write the record containing CUDA-specific declaration references.
5169 if (!CUDASpecialDeclRefs.empty())
5170 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005171
Alexis Hunt27a761d2011-05-04 23:29:54 +00005172 // Write the delegating constructors.
5173 if (!DelegatingCtorDecls.empty())
5174 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005175
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005176 // Write the known namespaces.
5177 if (!KnownNamespaces.empty())
5178 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00005179
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00005180 // Write the undefined internal functions and variables, and inline functions.
5181 if (!UndefinedButUsed.empty())
5182 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00005183
5184 if (!DeleteExprsToAnalyze.empty())
5185 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5186
Douglas Gregor851443c2011-08-12 01:39:19 +00005187 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00005188 for (auto *DC : UpdatedDeclContexts)
5189 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00005190
Douglas Gregor959bb062011-12-03 01:15:29 +00005191 if (!WritingModule) {
5192 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00005193 struct ModuleInfo {
5194 uint64_t ID;
5195 Module *M;
5196 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5197 };
Richard Smith56be7542014-03-21 00:33:59 +00005198 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00005199 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00005200 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005201 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5202 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00005203 }
Richard Smith56be7542014-03-21 00:33:59 +00005204
5205 if (!Imports.empty()) {
5206 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5207 return A.ID < B.ID;
5208 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005209 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5210 return A.ID == B.ID;
5211 };
Richard Smith56be7542014-03-21 00:33:59 +00005212
5213 // Sort and deduplicate module IDs.
Fangrui Song55fab262018-09-26 22:16:28 +00005214 llvm::sort(Imports, Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005215 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00005216 Imports.end());
5217
5218 RecordData ImportedModules;
5219 for (const auto &Import : Imports) {
5220 ImportedModules.push_back(Import.ID);
5221 // FIXME: If the module has macros imported then later has declarations
5222 // imported, this location won't be the right one as a location for the
5223 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00005224 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00005225 }
5226
Douglas Gregor959bb062011-12-03 01:15:29 +00005227 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5228 }
Douglas Gregor0a839132011-12-03 00:59:55 +00005229 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005230
Douglas Gregor404cdde2012-01-27 01:47:08 +00005231 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00005232 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00005233 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005234 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00005235 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005236 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00005237 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00005238
Douglas Gregor08f01292009-04-17 22:13:46 +00005239 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00005240 RecordData::value_type Record[] = {
5241 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00005242 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00005243 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00005244
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005245 // Write the module file extension blocks.
5246 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00005247 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005248
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005249 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005250}
5251
Richard Smithb9eab6d2014-03-20 19:44:17 +00005252void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005253 if (DeclUpdates.empty())
5254 return;
5255
Richard Smith59442b42014-03-20 20:07:19 +00005256 DeclUpdateMap LocalUpdates;
5257 LocalUpdates.swap(DeclUpdates);
5258
Richard Smith6ef42932014-03-20 21:02:00 +00005259 for (auto &DeclUpdate : LocalUpdates) {
5260 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00005261
Richard Smithd28ac5b2014-03-22 23:33:22 +00005262 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00005263 RecordData RecordData;
5264 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00005265 for (auto &Update : DeclUpdate.second) {
5266 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5267
Richard Smith69c82bf2016-04-01 22:52:03 +00005268 // An updated body is emitted last, so that the reader doesn't need
5269 // to skip over the lazy body to reach statements for other records.
5270 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5271 HasUpdatedBody = true;
5272 else
5273 Record.push_back(Kind);
5274
Richard Smith6ef42932014-03-20 21:02:00 +00005275 switch (Kind) {
5276 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5277 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5278 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00005279 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00005280 Record.push_back(GetDeclRef(Update.getDecl()));
5281 break;
5282
Richard Smith4d235792014-08-07 18:53:08 +00005283 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00005284 break;
5285
Richard Smith891fc7f2017-12-05 01:31:47 +00005286 case UPD_CXX_POINT_OF_INSTANTIATION:
5287 // FIXME: Do we need to also save the template specialization kind here?
Richard Smith69c82bf2016-04-01 22:52:03 +00005288 Record.AddSourceLocation(Update.getLoc());
Richard Smith891fc7f2017-12-05 01:31:47 +00005289 break;
5290
5291 case UPD_CXX_ADDED_VAR_DEFINITION: {
5292 const VarDecl *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00005293 Record.push_back(VD->isInline());
5294 Record.push_back(VD->isInlineSpecified());
Richard Smith05a21352017-06-22 22:18:46 +00005295 if (VD->getInit()) {
5296 Record.push_back(!VD->isInitKnownICE() ? 1
5297 : (VD->isInitICE() ? 3 : 2));
5298 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
5299 } else {
5300 Record.push_back(0);
5301 }
Richard Smith4d235792014-08-07 18:53:08 +00005302 break;
Richard Smith05a21352017-06-22 22:18:46 +00005303 }
Richard Smith4d235792014-08-07 18:53:08 +00005304
John McCall32791cc2016-01-06 22:34:54 +00005305 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00005306 Record.AddStmt(const_cast<Expr *>(
5307 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00005308 break;
5309
Richard Smith4b054b22016-08-24 21:25:37 +00005310 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5311 Record.AddStmt(
5312 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5313 break;
5314
Richard Smithcd45dbc2014-04-19 03:48:30 +00005315 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5316 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005317 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00005318 Record.push_back(RD->isParamDestroyedInCallee());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00005319 Record.push_back(RD->getArgPassingRestrictions());
Richard Smith69c82bf2016-04-01 22:52:03 +00005320 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00005321 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00005322 *Context, const_cast<CXXRecordDecl *>(RD)));
5323
5324 // This state is sometimes updated by template instantiation, when we
5325 // switch from the specialization referring to the template declaration
5326 // to it referring to the template definition.
5327 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5328 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005329 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005330 } else {
5331 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5332 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005333 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00005334
5335 // The instantiation might have been resolved to a partial
5336 // specialization. If so, record which one.
5337 auto From = Spec->getInstantiatedFrom();
5338 if (auto PartialSpec =
5339 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5340 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00005341 Record.AddDeclRef(PartialSpec);
5342 Record.AddTemplateArgumentList(
5343 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00005344 } else {
5345 Record.push_back(false);
5346 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00005347 }
5348 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005349 Record.AddSourceLocation(RD->getLocation());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005350 Record.AddSourceLocation(RD->getBeginLoc());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00005351 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005352
5353 // Instantiation may change attributes; write them all out afresh.
5354 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00005355 if (D->hasAttrs())
5356 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005357
5358 // FIXME: Ensure we don't get here for explicit instantiations.
5359 break;
5360 }
5361
Richard Smithf8134002015-03-10 01:41:22 +00005362 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005363 Record.AddDeclRef(Update.getDecl());
Richard Smith5b349582017-10-13 01:55:36 +00005364 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
Richard Smithf8134002015-03-10 01:41:22 +00005365 break;
5366
Richard Smith564417a2014-03-20 21:47:22 +00005367 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
5368 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00005369 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
5370 Record);
5371 break;
5372
Richard Smith6ef42932014-03-20 21:02:00 +00005373 case UPD_CXX_DEDUCED_RETURN_TYPE:
5374 Record.push_back(GetOrCreateTypeID(Update.getType()));
5375 break;
5376
5377 case UPD_DECL_MARKED_USED:
5378 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00005379
5380 case UPD_MANGLING_NUMBER:
5381 case UPD_STATIC_LOCAL_NUMBER:
5382 Record.push_back(Update.getNumber());
5383 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005384
Alexey Bataev97720002014-11-11 04:05:39 +00005385 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005386 Record.AddSourceRange(
5387 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005388 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005389
Alexey Bataev27ef9512019-03-20 20:14:22 +00005390 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5391 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5392 Record.push_back(A->getAllocatorType());
5393 Record.AddStmt(A->getAllocator());
5394 Record.AddSourceRange(A->getRange());
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005395 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00005396 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005397
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005398 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00005399 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005400 Record.AddSourceRange(
5401 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5402 break;
5403
Richard Smith65ebb4a2015-03-26 04:09:53 +00005404 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005405 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005406 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005407
5408 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005409 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005410 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005411 }
5412 }
5413
Richard Smithd28ac5b2014-03-22 23:33:22 +00005414 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005415 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005416 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005417 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005418 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005419 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005420 }
5421
Richard Smithcd45dbc2014-04-19 03:48:30 +00005422 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005423 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005424 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005425}
5426
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005427void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005428 uint32_t Raw = Loc.getRawEncoding();
5429 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005430}
5431
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005432void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005433 AddSourceLocation(Range.getBegin(), Record);
5434 AddSourceLocation(Range.getEnd(), Record);
5435}
5436
Richard Smithf144b542016-04-08 21:54:32 +00005437void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5438 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005439 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005440 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005441}
5442
Richard Smithf144b542016-04-08 21:54:32 +00005443void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5444 Record->push_back(Value.isUnsigned());
5445 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005446}
5447
Richard Smithf144b542016-04-08 21:54:32 +00005448void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5449 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005450}
5451
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00005452static void WriteFixedPointSemantics(ASTRecordWriter &Record,
5453 FixedPointSemantics FPSema) {
5454 Record.push_back(FPSema.getWidth());
5455 Record.push_back(FPSema.getScale());
5456 Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 |
5457 FPSema.hasUnsignedPadding() << 2);
5458}
5459
5460void ASTRecordWriter::AddAPValue(const APValue &Value) {
5461 APValue::ValueKind Kind = Value.getKind();
5462 push_back(static_cast<uint64_t>(Kind));
5463 switch (Kind) {
5464 case APValue::None:
5465 case APValue::Indeterminate:
5466 return;
5467 case APValue::Int:
5468 AddAPSInt(Value.getInt());
5469 return;
5470 case APValue::Float:
5471 push_back(static_cast<uint64_t>(
5472 llvm::APFloatBase::SemanticsToEnum(Value.getFloat().getSemantics())));
5473 AddAPFloat(Value.getFloat());
5474 return;
5475 case APValue::FixedPoint: {
5476 WriteFixedPointSemantics(*this, Value.getFixedPoint().getSemantics());
5477 AddAPSInt(Value.getFixedPoint().getValue());
5478 return;
5479 }
5480 case APValue::ComplexInt: {
5481 AddAPSInt(Value.getComplexIntReal());
5482 AddAPSInt(Value.getComplexIntImag());
5483 return;
5484 }
5485 case APValue::ComplexFloat: {
5486 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5487 Value.getComplexFloatReal().getSemantics())));
5488 AddAPFloat(Value.getComplexFloatReal());
5489 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5490 Value.getComplexFloatImag().getSemantics())));
5491 AddAPFloat(Value.getComplexFloatImag());
5492 return;
5493 }
5494 case APValue::LValue:
5495 case APValue::Vector:
5496 case APValue::Array:
5497 case APValue::Struct:
5498 case APValue::Union:
5499 case APValue::MemberPointer:
5500 case APValue::AddrLabelDiff:
5501 // TODO : Handle all these APValue::ValueKind.
5502 return;
5503 }
5504 llvm_unreachable("Invalid APValue::ValueKind");
5505}
5506
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005507void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005508 Record.push_back(getIdentifierRef(II));
5509}
5510
Sebastian Redl539c5062010-08-18 23:57:32 +00005511IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005512 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005513 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005514
Sebastian Redl539c5062010-08-18 23:57:32 +00005515 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005516 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005517 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005518 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005519}
5520
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005521MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005522 // Don't emit builtin macros like __LINE__ to the AST file unless they
5523 // have been redefined by the header (in which case they are not
5524 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005525 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005526 return 0;
5527
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005528 MacroID &ID = MacroIDs[MI];
5529 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005530 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005531 MacroInfoToEmitData Info = { Name, MI, ID };
5532 MacroInfosToEmit.push_back(Info);
5533 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005534 return ID;
5535}
5536
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005537MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005538 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005539 return 0;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005540
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005541 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5542 return MacroIDs[MI];
5543}
5544
5545uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005546 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005547}
5548
Richard Smithe64e7452016-04-18 21:54:58 +00005549void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5550 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005551}
5552
Sebastian Redl539c5062010-08-18 23:57:32 +00005553SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005554 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005555 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005556 }
5557
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005558 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005559 if (SID == 0 && Chain) {
5560 // This might trigger a ReadSelector callback, which will set the ID for
5561 // this selector.
5562 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005563 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005564 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005565 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005566 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005567 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005568 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005569 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005570}
5571
Richard Smithe64e7452016-04-18 21:54:58 +00005572void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5573 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005574}
5575
Richard Smith290d8012016-04-06 17:06:00 +00005576void ASTRecordWriter::AddTemplateArgumentLocInfo(
5577 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005578 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005579 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005580 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005581 break;
5582 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005583 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005584 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005585 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005586 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5587 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005588 break;
5589 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005590 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5591 AddSourceLocation(Arg.getTemplateNameLoc());
5592 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005593 break;
John McCall0ad16662009-10-29 08:12:44 +00005594 case TemplateArgument::Null:
5595 case TemplateArgument::Integral:
5596 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005597 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005598 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005599 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005600 break;
5601 }
5602}
5603
Richard Smith290d8012016-04-06 17:06:00 +00005604void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5605 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005606
5607 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5608 bool InfoHasSameExpr
5609 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005610 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005611 if (InfoHasSameExpr)
5612 return; // Avoid storing the same expr twice.
5613 }
Richard Smith290d8012016-04-06 17:06:00 +00005614 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005615}
5616
Richard Smith290d8012016-04-06 17:06:00 +00005617void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005618 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005619 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005620 return;
5621 }
5622
Richard Smithc23d7342018-06-29 20:46:25 +00005623 AddTypeRef(TInfo->getType());
Richard Smith290d8012016-04-06 17:06:00 +00005624 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005625}
5626
Richard Smith290d8012016-04-06 17:06:00 +00005627void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
Richard Smith290d8012016-04-06 17:06:00 +00005628 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005629 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005630 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005631}
5632
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005633void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005634 Record.push_back(GetOrCreateTypeID(T));
5635}
5636
Richard Smith2095ffe2015-03-16 20:11:03 +00005637TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005638 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005639 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5640 if (T.isNull())
5641 return TypeIdx();
5642 assert(!T.getLocalFastQualifiers());
5643
5644 TypeIdx &Idx = TypeIdxs[T];
5645 if (Idx.getIndex() == 0) {
5646 if (DoneWritingDeclsAndTypes) {
5647 assert(0 && "New type seen after serializing all the types to emit!");
5648 return TypeIdx();
5649 }
5650
5651 // We haven't seen this type before. Assign it a new ID and put it
5652 // into the queue of types to emit.
5653 Idx = TypeIdx(NextTypeID++);
5654 DeclTypesToEmit.push(T);
5655 }
5656 return Idx;
5657 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005658}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005659
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005660TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005661 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005662 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5663 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005664 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005665 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005666
Richard Smith2095ffe2015-03-16 20:11:03 +00005667 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5668 assert(I != TypeIdxs.end() && "Type not emitted!");
5669 return I->second;
5670 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005671}
5672
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005673void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005674 Record.push_back(GetDeclRef(D));
5675}
5676
Sebastian Redl539c5062010-08-18 23:57:32 +00005677DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005678 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005679
5680 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005681 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005682 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005683
Douglas Gregorb3163e52012-01-05 22:33:30 +00005684 // If D comes from an AST file, its declaration ID is already known and
5685 // fixed.
5686 if (D->isFromASTFile())
5687 return D->getGlobalID();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005688
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005689 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005690 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005691 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005692 if (DoneWritingDeclsAndTypes) {
5693 assert(0 && "New decl seen after serializing all the decls to emit!");
5694 return 0;
5695 }
5696
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005697 // We haven't seen this declaration before. Give it a new ID and
5698 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005699 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005700 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005701 }
5702
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005703 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005704}
5705
Sebastian Redl539c5062010-08-18 23:57:32 +00005706DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005707 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005708 return 0;
5709
Douglas Gregorb3163e52012-01-05 22:33:30 +00005710 // If D comes from an AST file, its declaration ID is already known and
5711 // fixed.
5712 if (D->isFromASTFile())
5713 return D->getGlobalID();
5714
Douglas Gregore84a9da2009-04-20 20:36:09 +00005715 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5716 return DeclIDs[D];
5717}
5718
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005719void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005720 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005721 assert(D);
5722
5723 SourceLocation Loc = D->getLocation();
5724 if (Loc.isInvalid())
5725 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005726
5727 // We only keep track of the file-level declarations of each file.
5728 if (!D->getLexicalDeclContext()->isFileContext())
5729 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005730 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5731 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis71b74eb2018-01-26 19:26:12 +00005732 // TemplateTemplateParmDecls that are part of an alias template, should not
5733 // have TU as lexical context.
5734 if (isa<ParmVarDecl>(D) || isa<TemplateTemplateParmDecl>(D))
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005735 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005736
5737 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005738 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005739 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005740 FileID FID;
5741 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005742 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005743 if (FID.isInvalid())
5744 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005745 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005746
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005747 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005748 if (!Info)
5749 Info = new DeclIDInFileInfo();
5750
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005751 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005752 LocDeclIDsTy &Decls = Info->DeclIDs;
5753
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005754 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005755 Decls.push_back(LocDecl);
5756 return;
5757 }
5758
Benjamin Kramer45025c02013-08-24 13:22:59 +00005759 LocDeclIDsTy::iterator I =
Fangrui Song7264a472019-07-03 08:13:17 +00005760 llvm::upper_bound(Decls, LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005761
5762 Decls.insert(I, LocDecl);
5763}
5764
Richard Smithe64e7452016-04-18 21:54:58 +00005765void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005766 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005767 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005768 switch (Name.getNameKind()) {
5769 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005770 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005771 break;
5772
5773 case DeclarationName::ObjCZeroArgSelector:
5774 case DeclarationName::ObjCOneArgSelector:
5775 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005776 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005777 break;
5778
5779 case DeclarationName::CXXConstructorName:
5780 case DeclarationName::CXXDestructorName:
5781 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005782 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005783 break;
5784
Richard Smith35845152017-02-07 01:37:30 +00005785 case DeclarationName::CXXDeductionGuideName:
5786 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5787 break;
5788
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005789 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005790 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005791 break;
5792
Alexis Hunt3d221f22009-11-29 07:34:05 +00005793 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005794 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005795 break;
5796
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005797 case DeclarationName::CXXUsingDirective:
5798 // No extra data to emit
5799 break;
5800 }
5801}
Chris Lattnerca025db2010-05-07 21:43:38 +00005802
Richard Smithd08aeb62014-08-28 01:33:39 +00005803unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5804 assert(needsAnonymousDeclarationNumber(D) &&
5805 "expected an anonymous declaration");
5806
5807 // Number the anonymous declarations within this context, if we've not
5808 // already done so.
5809 auto It = AnonymousDeclarationNumbers.find(D);
5810 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005811 auto *DC = D->getLexicalDeclContext();
5812 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5813 AnonymousDeclarationNumbers[ND] = Number;
5814 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005815
5816 It = AnonymousDeclarationNumbers.find(D);
5817 assert(It != AnonymousDeclarationNumbers.end() &&
5818 "declaration not found within its lexical context");
5819 }
5820
5821 return It->second;
5822}
5823
Richard Smith290d8012016-04-06 17:06:00 +00005824void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5825 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005826 switch (Name.getNameKind()) {
5827 case DeclarationName::CXXConstructorName:
5828 case DeclarationName::CXXDestructorName:
5829 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005830 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005831 break;
5832
5833 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005834 AddSourceLocation(SourceLocation::getFromRawEncoding(
5835 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005836 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005837 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005838 break;
5839
5840 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005841 AddSourceLocation(SourceLocation::getFromRawEncoding(
5842 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005843 break;
5844
5845 case DeclarationName::Identifier:
5846 case DeclarationName::ObjCZeroArgSelector:
5847 case DeclarationName::ObjCOneArgSelector:
5848 case DeclarationName::ObjCMultiArgSelector:
5849 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005850 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005851 break;
5852 }
5853}
5854
Richard Smith290d8012016-04-06 17:06:00 +00005855void ASTRecordWriter::AddDeclarationNameInfo(
5856 const DeclarationNameInfo &NameInfo) {
5857 AddDeclarationName(NameInfo.getName());
5858 AddSourceLocation(NameInfo.getLoc());
5859 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005860}
5861
Richard Smith290d8012016-04-06 17:06:00 +00005862void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5863 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5864 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005865 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005866 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005867}
5868
Richard Smithe64e7452016-04-18 21:54:58 +00005869void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005870 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005871 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005872 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005873
5874 // Push each of the NNS's onto a stack for serialization in reverse order.
5875 while (NNS) {
5876 NestedNames.push_back(NNS);
5877 NNS = NNS->getPrefix();
5878 }
5879
Richard Smithe64e7452016-04-18 21:54:58 +00005880 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005881 while(!NestedNames.empty()) {
5882 NNS = NestedNames.pop_back_val();
5883 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005884 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005885 switch (Kind) {
5886 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005887 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005888 break;
5889
5890 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005891 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005892 break;
5893
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005894 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005895 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005896 break;
5897
Chris Lattnerca025db2010-05-07 21:43:38 +00005898 case NestedNameSpecifier::TypeSpec:
5899 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005900 AddTypeRef(QualType(NNS->getAsType(), 0));
5901 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005902 break;
5903
5904 case NestedNameSpecifier::Global:
5905 // Don't need to write an associated value.
5906 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005907
5908 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005909 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005910 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005911 }
5912 }
5913}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005914
Richard Smith290d8012016-04-06 17:06:00 +00005915void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005916 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005917 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005918 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005919
5920 // Push each of the nested-name-specifiers's onto a stack for
5921 // serialization in reverse order.
5922 while (NNS) {
5923 NestedNames.push_back(NNS);
5924 NNS = NNS.getPrefix();
5925 }
5926
Richard Smith290d8012016-04-06 17:06:00 +00005927 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005928 while(!NestedNames.empty()) {
5929 NNS = NestedNames.pop_back_val();
5930 NestedNameSpecifier::SpecifierKind Kind
5931 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005932 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005933 switch (Kind) {
5934 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005935 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5936 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005937 break;
5938
5939 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005940 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5941 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005942 break;
5943
5944 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005945 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5946 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005947 break;
5948
5949 case NestedNameSpecifier::TypeSpec:
5950 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005951 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Richard Smithc23d7342018-06-29 20:46:25 +00005952 AddTypeRef(NNS.getTypeLoc().getType());
Richard Smith290d8012016-04-06 17:06:00 +00005953 AddTypeLoc(NNS.getTypeLoc());
5954 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005955 break;
5956
5957 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005958 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005959 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005960
5961 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005962 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5963 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005964 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005965 }
5966 }
5967}
5968
Richard Smith290d8012016-04-06 17:06:00 +00005969void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005970 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005971 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005972 switch (Kind) {
5973 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005974 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005975 break;
5976
5977 case TemplateName::OverloadedTemplate: {
5978 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005979 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005980 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005981 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005982 break;
5983 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005984
Richard Smithb23c5e82019-05-09 03:31:27 +00005985 case TemplateName::AssumedTemplate: {
5986 AssumedTemplateStorage *ADLT = Name.getAsAssumedTemplateName();
5987 AddDeclarationName(ADLT->getDeclName());
5988 break;
5989 }
5990
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005991 case TemplateName::QualifiedTemplate: {
5992 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005993 AddNestedNameSpecifier(QualT->getQualifier());
5994 Record->push_back(QualT->hasTemplateKeyword());
5995 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005996 break;
5997 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005998
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005999 case TemplateName::DependentTemplate: {
6000 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00006001 AddNestedNameSpecifier(DepT->getQualifier());
6002 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006003 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00006004 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006005 else
Richard Smith290d8012016-04-06 17:06:00 +00006006 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006007 break;
6008 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006009
6010 case TemplateName::SubstTemplateTemplateParm: {
6011 SubstTemplateTemplateParmStorage *subst
6012 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00006013 AddDeclRef(subst->getParameter());
6014 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00006015 break;
6016 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006017
Douglas Gregor5590be02011-01-15 06:45:20 +00006018 case TemplateName::SubstTemplateTemplateParmPack: {
6019 SubstTemplateTemplateParmPackStorage *SubstPack
6020 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00006021 AddDeclRef(SubstPack->getParameterPack());
6022 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00006023 break;
6024 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006025 }
6026}
6027
Richard Smith290d8012016-04-06 17:06:00 +00006028void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
6029 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006030 switch (Arg.getKind()) {
6031 case TemplateArgument::Null:
6032 break;
6033 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00006034 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006035 break;
6036 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00006037 AddDeclRef(Arg.getAsDecl());
6038 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00006039 break;
6040 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00006041 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006042 break;
6043 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00006044 AddAPSInt(Arg.getAsIntegral());
6045 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006046 break;
6047 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00006048 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00006049 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006050 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00006051 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00006052 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00006053 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00006054 else
Richard Smith290d8012016-04-06 17:06:00 +00006055 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006056 break;
6057 case TemplateArgument::Expression:
6058 AddStmt(Arg.getAsExpr());
6059 break;
6060 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00006061 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00006062 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00006063 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006064 break;
6065 }
6066}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006067
Richard Smithe64e7452016-04-18 21:54:58 +00006068void ASTRecordWriter::AddTemplateParameterList(
6069 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006070 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00006071 AddSourceLocation(TemplateParams->getTemplateLoc());
6072 AddSourceLocation(TemplateParams->getLAngleLoc());
6073 AddSourceLocation(TemplateParams->getRAngleLoc());
Saar Raz0330fba2019-10-15 18:44:06 +00006074
Richard Smithe64e7452016-04-18 21:54:58 +00006075 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006076 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00006077 AddDeclRef(P);
Saar Raz0330fba2019-10-15 18:44:06 +00006078 if (const Expr *RequiresClause = TemplateParams->getRequiresClause()) {
6079 Record->push_back(true);
6080 AddStmt(const_cast<Expr*>(RequiresClause));
6081 } else {
6082 Record->push_back(false);
6083 }
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006084}
6085
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006086/// Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00006087void ASTRecordWriter::AddTemplateArgumentList(
6088 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006089 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00006090 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006091 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00006092 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006093}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006094
Richard Smith290d8012016-04-06 17:06:00 +00006095void ASTRecordWriter::AddASTTemplateArgumentListInfo(
6096 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006097 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00006098 AddSourceLocation(ASTTemplArgList->LAngleLoc);
6099 AddSourceLocation(ASTTemplArgList->RAngleLoc);
6100 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006101 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006102 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00006103 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006104}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006105
Richard Smithe64e7452016-04-18 21:54:58 +00006106void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
6107 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006108 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006109 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00006110 AddDeclRef(I.getDecl());
6111 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006112 }
6113}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006114
Richard Smith290d8012016-04-06 17:06:00 +00006115// FIXME: Move this out of the main ASTRecordWriter interface.
6116void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
6117 Record->push_back(Base.isVirtual());
6118 Record->push_back(Base.isBaseOfClass());
6119 Record->push_back(Base.getAccessSpecifierAsWritten());
6120 Record->push_back(Base.getInheritConstructors());
6121 AddTypeSourceInfo(Base.getTypeSourceInfo());
6122 AddSourceRange(Base.getSourceRange());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006123 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00006124 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006125}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006126
Richard Smith645d2cf2016-04-14 00:29:55 +00006127static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
6128 ArrayRef<CXXBaseSpecifier> Bases) {
6129 ASTWriter::RecordData Record;
6130 ASTRecordWriter Writer(W, Record);
6131 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00006132
Richard Smith645d2cf2016-04-14 00:29:55 +00006133 for (auto &Base : Bases)
6134 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00006135
Richard Smith645d2cf2016-04-14 00:29:55 +00006136 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006137}
6138
Richard Smith290d8012016-04-06 17:06:00 +00006139// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00006140void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
6141 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
6142}
6143
Richard Smithaa165cf2016-04-13 21:57:08 +00006144static uint64_t
6145EmitCXXCtorInitializers(ASTWriter &W,
6146 ArrayRef<CXXCtorInitializer *> CtorInits) {
6147 ASTWriter::RecordData Record;
6148 ASTRecordWriter Writer(W, Record);
6149 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006150
Richard Smithaa165cf2016-04-13 21:57:08 +00006151 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006152 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00006153 Writer.push_back(CTOR_INITIALIZER_BASE);
6154 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
6155 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00006156 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00006157 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
6158 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00006159 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00006160 Writer.push_back(CTOR_INITIALIZER_MEMBER);
6161 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006162 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00006163 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
6164 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006165 }
Francois Pichetd583da02010-12-04 09:14:42 +00006166
Richard Smithaa165cf2016-04-13 21:57:08 +00006167 Writer.AddSourceLocation(Init->getMemberLocation());
6168 Writer.AddStmt(Init->getInit());
6169 Writer.AddSourceLocation(Init->getLParenLoc());
6170 Writer.AddSourceLocation(Init->getRParenLoc());
6171 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00006172 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00006173 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006174 }
Richard Smithaa165cf2016-04-13 21:57:08 +00006175
6176 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006177}
6178
Richard Smithaa165cf2016-04-13 21:57:08 +00006179// FIXME: Move this out of the main ASTRecordWriter interface.
6180void ASTRecordWriter::AddCXXCtorInitializers(
6181 ArrayRef<CXXCtorInitializer *> CtorInits) {
6182 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00006183}
6184
Richard Smith290d8012016-04-06 17:06:00 +00006185void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00006186 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00006187 Record->push_back(Data.IsLambda);
Richard Smith91aeacc2019-10-11 00:29:04 +00006188
6189 #define FIELD(Name, Width, Merge) \
6190 Record->push_back(Data.Name);
6191 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
Richard Trieufd1acbb2017-04-11 21:31:00 +00006192
6193 // getODRHash will compute the ODRHash if it has not been previously computed.
6194 Record->push_back(D->getODRHash());
David Blaikief63556d2017-04-12 20:58:33 +00006195 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
6196 Writer->WritingModule && !D->isDependentType();
6197 Record->push_back(ModulesDebugInfo);
6198 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00006199 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
6200
Richard Smith561fb152012-02-25 07:33:38 +00006201 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006202
Richard Smith290d8012016-04-06 17:06:00 +00006203 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006204 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006205 AddCXXBaseSpecifiers(Data.bases());
6206
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006207 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00006208 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006209 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006210 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006211
Richard Smith290d8012016-04-06 17:06:00 +00006212 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
Richard Smith91aeacc2019-10-11 00:29:04 +00006213 Record->push_back(Data.ComputedVisibleConversions);
6214 if (Data.ComputedVisibleConversions)
6215 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006216 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00006217 AddDeclRef(D->getFirstFriend());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006218
Douglas Gregor99ae8062012-02-14 17:54:36 +00006219 // Add lambda-specific data.
6220 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00006221 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00006222 Record->push_back(Lambda.Dependent);
6223 Record->push_back(Lambda.IsGenericLambda);
6224 Record->push_back(Lambda.CaptureDefault);
6225 Record->push_back(Lambda.NumCaptures);
6226 Record->push_back(Lambda.NumExplicitCaptures);
Michael Liao243ebfb2019-10-19 00:15:19 +00006227 Record->push_back(Lambda.HasKnownInternalLinkage);
Richard Smith290d8012016-04-06 17:06:00 +00006228 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00006229 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00006230 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00006231 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00006232 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00006233 AddSourceLocation(Capture.getLocation());
6234 Record->push_back(Capture.isImplicit());
6235 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00006236 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00006237 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00006238 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00006239 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00006240 break;
6241 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00006242 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00006243 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00006244 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00006245 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00006246 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00006247 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00006248 break;
6249 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00006250 }
6251 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006252}
6253
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006254void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00006255 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00006256 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00006257 assert(FirstDeclID == NextDeclID &&
6258 FirstTypeID == NextTypeID &&
6259 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006260 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00006261 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00006262 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00006263 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00006264
Sebastian Redl07a89a82010-07-30 00:29:29 +00006265 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006266
Richard Smith7f330cd2015-03-18 01:42:29 +00006267 // Note, this will get called multiple times, once one the reader starts up
6268 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00006269 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6270 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6271 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006272 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00006273 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00006274 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006275 NextDeclID = FirstDeclID;
6276 NextTypeID = FirstTypeID;
6277 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006278 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006279 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00006280 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00006281}
6282
Sebastian Redl539c5062010-08-18 23:57:32 +00006283void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006284 // Always keep the highest ID. See \p TypeRead() for more information.
6285 IdentID &StoredID = IdentifierIDs[II];
6286 if (ID > StoredID)
6287 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00006288}
6289
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006290void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006291 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006292 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006293 if (ID > StoredID)
6294 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006295}
6296
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00006297void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006298 // Always take the highest-numbered type index. This copes with an interesting
6299 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006300 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006301 // keep the higher-numbered entry so that we can properly write it out to
6302 // the AST file.
6303 TypeIdx &StoredIdx = TypeIdxs[T];
6304 if (Idx.getIndex() >= StoredIdx.getIndex())
6305 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006306}
6307
Sebastian Redl539c5062010-08-18 23:57:32 +00006308void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006309 // Always keep the highest ID. See \p TypeRead() for more information.
6310 SelectorID &StoredID = SelectorIDs[S];
6311 if (ID > StoredID)
6312 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00006313}
Douglas Gregor91096292010-10-02 19:29:26 +00006314
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006315void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00006316 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006317 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00006318 MacroDefinitions[MD] = ID;
6319}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006320
Douglas Gregore37a85a2011-12-02 17:30:13 +00006321void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6322 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
6323 SubmoduleIDs[Mod] = ID;
6324}
6325
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006326void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006327 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00006328 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006329 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006330 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006331 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00006332 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006333 // A forward reference was mutated into a definition. Rewrite it.
6334 // FIXME: This happens during template instantiation, should we
6335 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00006336 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6337 "completed a tag from another module but not by instantiation?");
6338 DeclUpdates[RD].push_back(
6339 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006340 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006341 }
6342}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006343
Richard Smithf983f7f2015-10-13 00:23:25 +00006344static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6345 if (D->isFromASTFile())
6346 return true;
6347
Richard Smithf983f7f2015-10-13 00:23:25 +00006348 // The predefined __va_list_tag struct is imported if we imported any decls.
6349 // FIXME: This is a gross hack.
6350 return D == D->getASTContext().getVaListTagDecl();
6351}
6352
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006353void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006354 if (Chain && Chain->isProcessingUpdateRecords()) return;
6355 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00006356 "Should not add lookup results to non-lookup contexts!");
6357
Vassil Vassilev632eac32016-03-16 11:17:04 +00006358 // TU is handled elsewhere.
6359 if (isa<TranslationUnitDecl>(DC))
6360 return;
6361
6362 // Namespaces are handled elsewhere, except for template instantiations of
6363 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6364 // local instantiations are added to an imported context. Only happens when
6365 // adding ADL lookup candidates, for example templated friends.
6366 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6367 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006368 return;
6369
Richard Smithf983f7f2015-10-13 00:23:25 +00006370 // We're only interested in cases where a local declaration is added to an
6371 // imported context.
6372 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6373 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006374
Richard Smith0f4e2c42015-08-06 04:23:48 +00006375 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00006376 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00006377 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00006378 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6379 // We're adding a visible declaration to a predefined decl context. Ensure
6380 // that we write out all of its lookup results so we don't get a nasty
6381 // surprise when we try to emit its lookup table.
6382 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00006383 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00006384 }
Richard Smithc26d9742016-10-06 20:30:51 +00006385 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006386}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006387
6388void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006389 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006390 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00006391
6392 // We're only interested in cases where a local declaration is added to an
6393 // imported context.
6394 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6395 return;
6396
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006397 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00006398 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006399
6400 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00006401 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00006402 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00006403 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006404}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00006405
Richard Smith564417a2014-03-20 21:47:22 +00006406void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006407 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00006408 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6409 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006410 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006411 // If we don't already know the exception specification for this redecl
6412 // chain, add an update record for it.
6413 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6414 ->getType()
6415 ->castAs<FunctionProtoType>()
6416 ->getExceptionSpecType()))
6417 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6418 });
Richard Smith564417a2014-03-20 21:47:22 +00006419}
6420
Richard Smith1fa5d642013-05-11 05:45:24 +00006421void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006422 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00006423 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00006424 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006425 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006426 DeclUpdates[D].push_back(
6427 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6428 });
Richard Smith1fa5d642013-05-11 05:45:24 +00006429}
6430
Richard Smithf8134002015-03-10 01:41:22 +00006431void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
Richard Smith5b349582017-10-13 01:55:36 +00006432 const FunctionDecl *Delete,
6433 Expr *ThisArg) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006434 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006435 assert(!WritingAST && "Already writing the AST!");
6436 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006437 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006438 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006439 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6440 });
Richard Smithf8134002015-03-10 01:41:22 +00006441}
6442
Sebastian Redlab238a72011-04-24 16:28:06 +00006443void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006444 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006445 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006446 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006447 return; // Declaration not imported from PCH.
6448
Richard Smith4d235792014-08-07 18:53:08 +00006449 // Implicit function decl from a PCH was defined.
6450 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006451}
6452
Richard Smith891fc7f2017-12-05 01:31:47 +00006453void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6454 if (Chain && Chain->isProcessingUpdateRecords()) return;
6455 assert(!WritingAST && "Already writing the AST!");
6456 if (!D->isFromASTFile())
6457 return;
6458
6459 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6460}
6461
Richard Smithd28ac5b2014-03-22 23:33:22 +00006462void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006463 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006464 assert(!WritingAST && "Already writing the AST!");
6465 if (!D->isFromASTFile())
6466 return;
6467
Richard Smith9e2341d2015-03-23 03:25:59 +00006468 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006469}
6470
Richard Smith891fc7f2017-12-05 01:31:47 +00006471void ASTWriter::InstantiationRequested(const ValueDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006472 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006473 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006474 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006475 return;
6476
6477 // Since the actual instantiation is delayed, this really means that we need
6478 // to update the instantiation location.
Richard Smith891fc7f2017-12-05 01:31:47 +00006479 SourceLocation POI;
6480 if (auto *VD = dyn_cast<VarDecl>(D))
6481 POI = VD->getPointOfInstantiation();
6482 else
6483 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6484 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006485}
6486
John McCall32791cc2016-01-06 22:34:54 +00006487void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006488 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006489 assert(!WritingAST && "Already writing the AST!");
6490 if (!D->isFromASTFile())
6491 return;
6492
6493 DeclUpdates[D].push_back(
6494 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6495}
6496
Richard Smith4b054b22016-08-24 21:25:37 +00006497void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6498 assert(!WritingAST && "Already writing the AST!");
6499 if (!D->isFromASTFile())
6500 return;
6501
6502 DeclUpdates[D].push_back(
6503 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6504}
6505
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006506void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6507 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006508 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006509 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006510 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006511 return; // Declaration not imported from PCH.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006512
Douglas Gregor404cdde2012-01-27 01:47:08 +00006513 assert(IFD->getDefinition() && "Category on a class without a definition?");
6514 ObjCClassesWithCategories.insert(
6515 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006516}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006517
Eli Friedman276dd182013-09-05 00:02:25 +00006518void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006519 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006520 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006521
6522 // If there is *any* declaration of the entity that's not from an AST file,
6523 // we can skip writing the update record. We make sure that isUsed() triggers
6524 // completion of the redeclaration chain of the entity.
6525 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6526 if (IsLocalDecl(Prev))
6527 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006528
Aaron Ballman4f45b712014-03-21 15:22:56 +00006529 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006530}
Alexey Bataev97720002014-11-11 04:05:39 +00006531
6532void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006533 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006534 assert(!WritingAST && "Already writing the AST!");
6535 if (!D->isFromASTFile())
6536 return;
6537
6538 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6539}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006540
Alexey Bataev25ed0c02019-03-07 17:54:44 +00006541void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
6542 if (Chain && Chain->isProcessingUpdateRecords()) return;
6543 assert(!WritingAST && "Already writing the AST!");
6544 if (!D->isFromASTFile())
6545 return;
6546
6547 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
6548}
6549
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006550void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6551 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006552 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006553 assert(!WritingAST && "Already writing the AST!");
6554 if (!D->isFromASTFile())
6555 return;
6556
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006557 DeclUpdates[D].push_back(
6558 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006559}
6560
Richard Smith4caa4492015-05-15 02:34:32 +00006561void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006562 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006563 assert(!WritingAST && "Already writing the AST!");
6564 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006565 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006566}
Alex Denisovfde64952015-06-26 05:28:36 +00006567
6568void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6569 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006570 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006571 assert(!WritingAST && "Already writing the AST!");
6572 if (!Record->isFromASTFile())
6573 return;
6574 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6575}
Richard Smithc26d9742016-10-06 20:30:51 +00006576
6577void ASTWriter::AddedCXXTemplateSpecialization(
6578 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6579 assert(!WritingAST && "Already writing the AST!");
6580
6581 if (!TD->getFirstDecl()->isFromASTFile())
6582 return;
6583 if (Chain && Chain->isProcessingUpdateRecords())
6584 return;
6585
6586 DeclsToEmitEvenIfUnreferenced.push_back(D);
6587}
6588
6589void ASTWriter::AddedCXXTemplateSpecialization(
6590 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6591 assert(!WritingAST && "Already writing the AST!");
6592
6593 if (!TD->getFirstDecl()->isFromASTFile())
6594 return;
6595 if (Chain && Chain->isProcessingUpdateRecords())
6596 return;
6597
6598 DeclsToEmitEvenIfUnreferenced.push_back(D);
6599}
6600
6601void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6602 const FunctionDecl *D) {
6603 assert(!WritingAST && "Already writing the AST!");
6604
6605 if (!TD->getFirstDecl()->isFromASTFile())
6606 return;
6607 if (Chain && Chain->isProcessingUpdateRecords())
6608 return;
6609
6610 DeclsToEmitEvenIfUnreferenced.push_back(D);
6611}
Kelvin Libe286f52018-09-15 13:54:15 +00006612
6613//===----------------------------------------------------------------------===//
6614//// OMPClause Serialization
6615////===----------------------------------------------------------------------===//
6616
6617void OMPClauseWriter::writeClause(OMPClause *C) {
6618 Record.push_back(C->getClauseKind());
6619 Visit(C);
6620 Record.AddSourceLocation(C->getBeginLoc());
6621 Record.AddSourceLocation(C->getEndLoc());
6622}
6623
6624void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
6625 Record.push_back(C->getCaptureRegion());
6626 Record.AddStmt(C->getPreInitStmt());
6627}
6628
6629void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6630 VisitOMPClauseWithPreInit(C);
6631 Record.AddStmt(C->getPostUpdateExpr());
6632}
6633
6634void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6635 VisitOMPClauseWithPreInit(C);
6636 Record.push_back(C->getNameModifier());
6637 Record.AddSourceLocation(C->getNameModifierLoc());
6638 Record.AddSourceLocation(C->getColonLoc());
6639 Record.AddStmt(C->getCondition());
6640 Record.AddSourceLocation(C->getLParenLoc());
6641}
6642
6643void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
Alexey Bataev3a842ec2019-10-15 19:37:05 +00006644 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006645 Record.AddStmt(C->getCondition());
6646 Record.AddSourceLocation(C->getLParenLoc());
6647}
6648
6649void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6650 VisitOMPClauseWithPreInit(C);
6651 Record.AddStmt(C->getNumThreads());
6652 Record.AddSourceLocation(C->getLParenLoc());
6653}
6654
6655void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6656 Record.AddStmt(C->getSafelen());
6657 Record.AddSourceLocation(C->getLParenLoc());
6658}
6659
6660void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6661 Record.AddStmt(C->getSimdlen());
6662 Record.AddSourceLocation(C->getLParenLoc());
6663}
6664
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00006665void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6666 Record.AddStmt(C->getAllocator());
6667 Record.AddSourceLocation(C->getLParenLoc());
6668}
6669
Kelvin Libe286f52018-09-15 13:54:15 +00006670void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6671 Record.AddStmt(C->getNumForLoops());
6672 Record.AddSourceLocation(C->getLParenLoc());
6673}
6674
6675void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
6676 Record.push_back(C->getDefaultKind());
6677 Record.AddSourceLocation(C->getLParenLoc());
6678 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6679}
6680
6681void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
6682 Record.push_back(C->getProcBindKind());
6683 Record.AddSourceLocation(C->getLParenLoc());
6684 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6685}
6686
6687void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6688 VisitOMPClauseWithPreInit(C);
6689 Record.push_back(C->getScheduleKind());
6690 Record.push_back(C->getFirstScheduleModifier());
6691 Record.push_back(C->getSecondScheduleModifier());
6692 Record.AddStmt(C->getChunkSize());
6693 Record.AddSourceLocation(C->getLParenLoc());
6694 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6695 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6696 Record.AddSourceLocation(C->getScheduleKindLoc());
6697 Record.AddSourceLocation(C->getCommaLoc());
6698}
6699
6700void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6701 Record.push_back(C->getLoopNumIterations().size());
6702 Record.AddStmt(C->getNumForLoops());
6703 for (Expr *NumIter : C->getLoopNumIterations())
6704 Record.AddStmt(NumIter);
6705 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
Mike Rice0ed46662018-09-20 17:19:41 +00006706 Record.AddStmt(C->getLoopCounter(I));
Kelvin Libe286f52018-09-15 13:54:15 +00006707 Record.AddSourceLocation(C->getLParenLoc());
6708}
6709
6710void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6711
6712void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6713
6714void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6715
6716void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6717
6718void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6719
6720void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *) {}
6721
6722void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6723
6724void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6725
6726void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6727
6728void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6729
6730void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6731
6732void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6733 Record.push_back(C->varlist_size());
6734 Record.AddSourceLocation(C->getLParenLoc());
6735 for (auto *VE : C->varlists()) {
6736 Record.AddStmt(VE);
6737 }
6738 for (auto *VE : C->private_copies()) {
6739 Record.AddStmt(VE);
6740 }
6741}
6742
6743void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6744 Record.push_back(C->varlist_size());
6745 VisitOMPClauseWithPreInit(C);
6746 Record.AddSourceLocation(C->getLParenLoc());
6747 for (auto *VE : C->varlists()) {
6748 Record.AddStmt(VE);
6749 }
6750 for (auto *VE : C->private_copies()) {
6751 Record.AddStmt(VE);
6752 }
6753 for (auto *VE : C->inits()) {
6754 Record.AddStmt(VE);
6755 }
6756}
6757
6758void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6759 Record.push_back(C->varlist_size());
6760 VisitOMPClauseWithPostUpdate(C);
6761 Record.AddSourceLocation(C->getLParenLoc());
6762 for (auto *VE : C->varlists())
6763 Record.AddStmt(VE);
6764 for (auto *E : C->private_copies())
6765 Record.AddStmt(E);
6766 for (auto *E : C->source_exprs())
6767 Record.AddStmt(E);
6768 for (auto *E : C->destination_exprs())
6769 Record.AddStmt(E);
6770 for (auto *E : C->assignment_ops())
6771 Record.AddStmt(E);
6772}
6773
6774void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6775 Record.push_back(C->varlist_size());
6776 Record.AddSourceLocation(C->getLParenLoc());
6777 for (auto *VE : C->varlists())
6778 Record.AddStmt(VE);
6779}
6780
6781void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6782 Record.push_back(C->varlist_size());
6783 VisitOMPClauseWithPostUpdate(C);
6784 Record.AddSourceLocation(C->getLParenLoc());
6785 Record.AddSourceLocation(C->getColonLoc());
6786 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6787 Record.AddDeclarationNameInfo(C->getNameInfo());
6788 for (auto *VE : C->varlists())
6789 Record.AddStmt(VE);
6790 for (auto *VE : C->privates())
6791 Record.AddStmt(VE);
6792 for (auto *E : C->lhs_exprs())
6793 Record.AddStmt(E);
6794 for (auto *E : C->rhs_exprs())
6795 Record.AddStmt(E);
6796 for (auto *E : C->reduction_ops())
6797 Record.AddStmt(E);
6798}
6799
6800void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6801 Record.push_back(C->varlist_size());
6802 VisitOMPClauseWithPostUpdate(C);
6803 Record.AddSourceLocation(C->getLParenLoc());
6804 Record.AddSourceLocation(C->getColonLoc());
6805 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6806 Record.AddDeclarationNameInfo(C->getNameInfo());
6807 for (auto *VE : C->varlists())
6808 Record.AddStmt(VE);
6809 for (auto *VE : C->privates())
6810 Record.AddStmt(VE);
6811 for (auto *E : C->lhs_exprs())
6812 Record.AddStmt(E);
6813 for (auto *E : C->rhs_exprs())
6814 Record.AddStmt(E);
6815 for (auto *E : C->reduction_ops())
6816 Record.AddStmt(E);
6817}
6818
6819void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6820 Record.push_back(C->varlist_size());
6821 VisitOMPClauseWithPostUpdate(C);
6822 Record.AddSourceLocation(C->getLParenLoc());
6823 Record.AddSourceLocation(C->getColonLoc());
6824 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6825 Record.AddDeclarationNameInfo(C->getNameInfo());
6826 for (auto *VE : C->varlists())
6827 Record.AddStmt(VE);
6828 for (auto *VE : C->privates())
6829 Record.AddStmt(VE);
6830 for (auto *E : C->lhs_exprs())
6831 Record.AddStmt(E);
6832 for (auto *E : C->rhs_exprs())
6833 Record.AddStmt(E);
6834 for (auto *E : C->reduction_ops())
6835 Record.AddStmt(E);
6836 for (auto *E : C->taskgroup_descriptors())
6837 Record.AddStmt(E);
6838}
6839
6840void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6841 Record.push_back(C->varlist_size());
6842 VisitOMPClauseWithPostUpdate(C);
6843 Record.AddSourceLocation(C->getLParenLoc());
6844 Record.AddSourceLocation(C->getColonLoc());
6845 Record.push_back(C->getModifier());
6846 Record.AddSourceLocation(C->getModifierLoc());
6847 for (auto *VE : C->varlists()) {
6848 Record.AddStmt(VE);
6849 }
6850 for (auto *VE : C->privates()) {
6851 Record.AddStmt(VE);
6852 }
6853 for (auto *VE : C->inits()) {
6854 Record.AddStmt(VE);
6855 }
6856 for (auto *VE : C->updates()) {
6857 Record.AddStmt(VE);
6858 }
6859 for (auto *VE : C->finals()) {
6860 Record.AddStmt(VE);
6861 }
6862 Record.AddStmt(C->getStep());
6863 Record.AddStmt(C->getCalcStep());
Alexey Bataev195ae902019-08-08 13:42:45 +00006864 for (auto *VE : C->used_expressions())
6865 Record.AddStmt(VE);
Kelvin Libe286f52018-09-15 13:54:15 +00006866}
6867
6868void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6869 Record.push_back(C->varlist_size());
6870 Record.AddSourceLocation(C->getLParenLoc());
6871 Record.AddSourceLocation(C->getColonLoc());
6872 for (auto *VE : C->varlists())
6873 Record.AddStmt(VE);
6874 Record.AddStmt(C->getAlignment());
6875}
6876
6877void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6878 Record.push_back(C->varlist_size());
6879 Record.AddSourceLocation(C->getLParenLoc());
6880 for (auto *VE : C->varlists())
6881 Record.AddStmt(VE);
6882 for (auto *E : C->source_exprs())
6883 Record.AddStmt(E);
6884 for (auto *E : C->destination_exprs())
6885 Record.AddStmt(E);
6886 for (auto *E : C->assignment_ops())
6887 Record.AddStmt(E);
6888}
6889
6890void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6891 Record.push_back(C->varlist_size());
6892 Record.AddSourceLocation(C->getLParenLoc());
6893 for (auto *VE : C->varlists())
6894 Record.AddStmt(VE);
6895 for (auto *E : C->source_exprs())
6896 Record.AddStmt(E);
6897 for (auto *E : C->destination_exprs())
6898 Record.AddStmt(E);
6899 for (auto *E : C->assignment_ops())
6900 Record.AddStmt(E);
6901}
6902
6903void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6904 Record.push_back(C->varlist_size());
6905 Record.AddSourceLocation(C->getLParenLoc());
6906 for (auto *VE : C->varlists())
6907 Record.AddStmt(VE);
6908}
6909
6910void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6911 Record.push_back(C->varlist_size());
6912 Record.push_back(C->getNumLoops());
6913 Record.AddSourceLocation(C->getLParenLoc());
6914 Record.push_back(C->getDependencyKind());
6915 Record.AddSourceLocation(C->getDependencyLoc());
6916 Record.AddSourceLocation(C->getColonLoc());
6917 for (auto *VE : C->varlists())
6918 Record.AddStmt(VE);
6919 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6920 Record.AddStmt(C->getLoopData(I));
6921}
6922
6923void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6924 VisitOMPClauseWithPreInit(C);
6925 Record.AddStmt(C->getDevice());
6926 Record.AddSourceLocation(C->getLParenLoc());
6927}
6928
6929void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6930 Record.push_back(C->varlist_size());
6931 Record.push_back(C->getUniqueDeclarationsNum());
6932 Record.push_back(C->getTotalComponentListNum());
6933 Record.push_back(C->getTotalComponentsNum());
6934 Record.AddSourceLocation(C->getLParenLoc());
Kelvin Lief579432018-12-18 22:18:41 +00006935 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
6936 Record.push_back(C->getMapTypeModifier(I));
6937 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6938 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00006939 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6940 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006941 Record.push_back(C->getMapType());
6942 Record.AddSourceLocation(C->getMapLoc());
6943 Record.AddSourceLocation(C->getColonLoc());
6944 for (auto *E : C->varlists())
6945 Record.AddStmt(E);
Michael Kruse4304e9d2019-02-19 16:38:20 +00006946 for (auto *E : C->mapperlists())
6947 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006948 for (auto *D : C->all_decls())
6949 Record.AddDeclRef(D);
6950 for (auto N : C->all_num_lists())
6951 Record.push_back(N);
6952 for (auto N : C->all_lists_sizes())
6953 Record.push_back(N);
6954 for (auto &M : C->all_components()) {
6955 Record.AddStmt(M.getAssociatedExpression());
6956 Record.AddDeclRef(M.getAssociatedDeclaration());
6957 }
6958}
6959
Alexey Bataeve04483e2019-03-27 14:14:31 +00006960void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6961 Record.push_back(C->varlist_size());
6962 Record.AddSourceLocation(C->getLParenLoc());
6963 Record.AddSourceLocation(C->getColonLoc());
6964 Record.AddStmt(C->getAllocator());
6965 for (auto *VE : C->varlists())
6966 Record.AddStmt(VE);
6967}
6968
Kelvin Libe286f52018-09-15 13:54:15 +00006969void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6970 VisitOMPClauseWithPreInit(C);
6971 Record.AddStmt(C->getNumTeams());
6972 Record.AddSourceLocation(C->getLParenLoc());
6973}
6974
6975void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6976 VisitOMPClauseWithPreInit(C);
6977 Record.AddStmt(C->getThreadLimit());
6978 Record.AddSourceLocation(C->getLParenLoc());
6979}
6980
6981void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
Alexey Bataev31ba4762019-10-16 18:09:37 +00006982 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006983 Record.AddStmt(C->getPriority());
6984 Record.AddSourceLocation(C->getLParenLoc());
6985}
6986
6987void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
Alexey Bataevb9c55e22019-10-14 19:29:52 +00006988 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006989 Record.AddStmt(C->getGrainsize());
6990 Record.AddSourceLocation(C->getLParenLoc());
6991}
6992
6993void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
Alexey Bataevd88c7de2019-10-14 20:44:34 +00006994 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +00006995 Record.AddStmt(C->getNumTasks());
6996 Record.AddSourceLocation(C->getLParenLoc());
6997}
6998
6999void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
7000 Record.AddStmt(C->getHint());
7001 Record.AddSourceLocation(C->getLParenLoc());
7002}
7003
7004void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
7005 VisitOMPClauseWithPreInit(C);
7006 Record.push_back(C->getDistScheduleKind());
7007 Record.AddStmt(C->getChunkSize());
7008 Record.AddSourceLocation(C->getLParenLoc());
7009 Record.AddSourceLocation(C->getDistScheduleKindLoc());
7010 Record.AddSourceLocation(C->getCommaLoc());
7011}
7012
7013void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
7014 Record.push_back(C->getDefaultmapKind());
7015 Record.push_back(C->getDefaultmapModifier());
7016 Record.AddSourceLocation(C->getLParenLoc());
7017 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
7018 Record.AddSourceLocation(C->getDefaultmapKindLoc());
7019}
7020
7021void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
7022 Record.push_back(C->varlist_size());
7023 Record.push_back(C->getUniqueDeclarationsNum());
7024 Record.push_back(C->getTotalComponentListNum());
7025 Record.push_back(C->getTotalComponentsNum());
7026 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse01f670d2019-02-22 22:29:42 +00007027 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7028 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00007029 for (auto *E : C->varlists())
7030 Record.AddStmt(E);
Michael Kruse01f670d2019-02-22 22:29:42 +00007031 for (auto *E : C->mapperlists())
7032 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00007033 for (auto *D : C->all_decls())
7034 Record.AddDeclRef(D);
7035 for (auto N : C->all_num_lists())
7036 Record.push_back(N);
7037 for (auto N : C->all_lists_sizes())
7038 Record.push_back(N);
7039 for (auto &M : C->all_components()) {
7040 Record.AddStmt(M.getAssociatedExpression());
7041 Record.AddDeclRef(M.getAssociatedDeclaration());
7042 }
7043}
7044
7045void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
7046 Record.push_back(C->varlist_size());
7047 Record.push_back(C->getUniqueDeclarationsNum());
7048 Record.push_back(C->getTotalComponentListNum());
7049 Record.push_back(C->getTotalComponentsNum());
7050 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse0336c752019-02-25 20:34:15 +00007051 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7052 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00007053 for (auto *E : C->varlists())
7054 Record.AddStmt(E);
Michael Kruse0336c752019-02-25 20:34:15 +00007055 for (auto *E : C->mapperlists())
7056 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00007057 for (auto *D : C->all_decls())
7058 Record.AddDeclRef(D);
7059 for (auto N : C->all_num_lists())
7060 Record.push_back(N);
7061 for (auto N : C->all_lists_sizes())
7062 Record.push_back(N);
7063 for (auto &M : C->all_components()) {
7064 Record.AddStmt(M.getAssociatedExpression());
7065 Record.AddDeclRef(M.getAssociatedDeclaration());
7066 }
7067}
7068
7069void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
7070 Record.push_back(C->varlist_size());
7071 Record.push_back(C->getUniqueDeclarationsNum());
7072 Record.push_back(C->getTotalComponentListNum());
7073 Record.push_back(C->getTotalComponentsNum());
7074 Record.AddSourceLocation(C->getLParenLoc());
7075 for (auto *E : C->varlists())
7076 Record.AddStmt(E);
7077 for (auto *VE : C->private_copies())
7078 Record.AddStmt(VE);
7079 for (auto *VE : C->inits())
7080 Record.AddStmt(VE);
7081 for (auto *D : C->all_decls())
7082 Record.AddDeclRef(D);
7083 for (auto N : C->all_num_lists())
7084 Record.push_back(N);
7085 for (auto N : C->all_lists_sizes())
7086 Record.push_back(N);
7087 for (auto &M : C->all_components()) {
7088 Record.AddStmt(M.getAssociatedExpression());
7089 Record.AddDeclRef(M.getAssociatedDeclaration());
7090 }
7091}
7092
7093void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
7094 Record.push_back(C->varlist_size());
7095 Record.push_back(C->getUniqueDeclarationsNum());
7096 Record.push_back(C->getTotalComponentListNum());
7097 Record.push_back(C->getTotalComponentsNum());
7098 Record.AddSourceLocation(C->getLParenLoc());
7099 for (auto *E : C->varlists())
7100 Record.AddStmt(E);
7101 for (auto *D : C->all_decls())
7102 Record.AddDeclRef(D);
7103 for (auto N : C->all_num_lists())
7104 Record.push_back(N);
7105 for (auto N : C->all_lists_sizes())
7106 Record.push_back(N);
7107 for (auto &M : C->all_components()) {
7108 Record.AddStmt(M.getAssociatedExpression());
7109 Record.AddDeclRef(M.getAssociatedDeclaration());
7110 }
7111}
Kelvin Li1408f912018-09-26 04:28:39 +00007112
7113void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
Patrick Lyster4a370b92018-10-01 13:47:43 +00007114
7115void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7116 OMPUnifiedSharedMemoryClause *) {}
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00007117
7118void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
Patrick Lyster3fe9e392018-10-11 14:41:10 +00007119
7120void
7121OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
7122}
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00007123
7124void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7125 OMPAtomicDefaultMemOrderClause *C) {
7126 Record.push_back(C->getAtomicDefaultMemOrderKind());
7127 Record.AddSourceLocation(C->getLParenLoc());
7128 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
7129}