blob: e1f3d50456b11f2a17d91afa062c95d65b22abc6 [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"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000068#include "clang/Serialization/Module.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());
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000246 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000247}
248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000251 Record.AddSourceLocation(T->getLBracketLoc());
252 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000253 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000254 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255}
256
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000258 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000259 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000260 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000261 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262}
263
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000264void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000265 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000266 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000267}
268
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000269void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000270 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000271 FunctionType::ExtInfo C = T->getExtInfo();
272 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000273 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000274 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000275 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000276 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000277 Record.push_back(C.getProducesResult());
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000278 Record.push_back(C.getNoCallerSavedRegs());
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000279 Record.push_back(C.getNoCfCheck());
Richard Smith01b2cb42014-07-26 06:37:51 +0000280
281 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
282 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000283}
284
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000285void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000286 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000287 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000288}
289
Richard Smith290d8012016-04-06 17:06:00 +0000290static void addExceptionSpec(const FunctionProtoType *T,
291 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000292 Record.push_back(T->getExceptionSpecType());
293 if (T->getExceptionSpecType() == EST_Dynamic) {
294 Record.push_back(T->getNumExceptions());
295 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000296 Record.AddTypeRef(T->getExceptionType(I));
Richard Smitheaf11ad2018-05-03 03:58:32 +0000297 } else if (isComputedNoexcept(T->getExceptionSpecType())) {
Richard Smith290d8012016-04-06 17:06:00 +0000298 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000299 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000300 Record.AddDeclRef(T->getExceptionSpecDecl());
301 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000302 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000303 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000304 }
Richard Smith564417a2014-03-20 21:47:22 +0000305}
306
307void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
308 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000309
Richard Smith564417a2014-03-20 21:47:22 +0000310 Record.push_back(T->isVariadic());
311 Record.push_back(T->hasTrailingReturn());
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000312 Record.push_back(T->getMethodQuals().getAsOpaqueValue());
Richard Smith564417a2014-03-20 21:47:22 +0000313 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000314 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000315
316 Record.push_back(T->getNumParams());
317 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000318 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000319
John McCall18afab72016-03-01 00:49:02 +0000320 if (T->hasExtParameterInfos()) {
321 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
322 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
323 }
324
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000325 if (T->isVariadic() || T->hasTrailingReturn() || T->getMethodQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000326 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
327 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000328 AbbrevToUse = 0;
329
Sebastian Redl539c5062010-08-18 23:57:32 +0000330 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000331}
332
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000333void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000334 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000335 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000336}
John McCallb96ec562009-12-04 22:46:56 +0000337
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000338void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000339 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000340 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000341 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000343}
344
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000345void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000346 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000347 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000348}
349
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000350void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000351 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000352 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000353}
354
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000355void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000356 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000357 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000358 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000359}
360
Alexis Hunte852b102011-05-24 22:41:36 +0000361void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000362 Record.AddTypeRef(T->getBaseType());
363 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000364 Record.push_back(T->getUTTKind());
365 Code = TYPE_UNARY_TRANSFORM;
366}
367
Richard Smith30482bc2011-02-20 03:19:35 +0000368void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000369 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000370 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000371 if (T->getDeducedType().isNull())
Richard Smithb2997f52019-05-21 20:10:50 +0000372 Record.push_back(T->containsUnexpandedParameterPack() ? 2 :
373 T->isDependentType() ? 1 : 0);
Richard Smith30482bc2011-02-20 03:19:35 +0000374 Code = TYPE_AUTO;
375}
376
Richard Smith600b5262017-01-26 20:40:47 +0000377void ASTTypeWriter::VisitDeducedTemplateSpecializationType(
378 const DeducedTemplateSpecializationType *T) {
379 Record.AddTemplateName(T->getTemplateName());
380 Record.AddTypeRef(T->getDeducedType());
381 if (T->getDeducedType().isNull())
382 Record.push_back(T->isDependentType());
383 Code = TYPE_DEDUCED_TEMPLATE_SPECIALIZATION;
384}
385
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000386void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000387 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000388 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000389 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000390 "Cannot serialize in the middle of a type definition");
391}
392
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000393void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000394 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000395 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000396}
397
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000398void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000399 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000400 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000401}
402
John McCall81904512011-01-06 01:58:22 +0000403void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000404 Record.AddTypeRef(T->getModifiedType());
405 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000406 Record.push_back(T->getAttrKind());
407 Code = TYPE_ATTRIBUTED;
408}
409
Mike Stump11289f42009-09-09 15:08:12 +0000410void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000411ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000412 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000413 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
414 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000415 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000416}
417
418void
Douglas Gregorada4b792011-01-14 02:55:32 +0000419ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
420 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000421 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
422 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000423 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
424}
425
426void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000427ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000428 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000429 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000430 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000431 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000432 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000433 Record.AddTemplateArgument(ArgI);
434 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
435 : T->isCanonicalUnqualified()
436 ? QualType()
437 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000438 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000439}
440
441void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000442ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000443 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000444 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000445 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000446 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000447}
448
449void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000450ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000451 const DependentSizedExtVectorType *T) {
Alex Lorenz00aee432017-03-22 10:04:48 +0000452 Record.AddTypeRef(T->getElementType());
453 Record.AddStmt(T->getSizeExpr());
454 Record.AddSourceLocation(T->getAttributeLoc());
455 Code = TYPE_DEPENDENT_SIZED_EXT_VECTOR;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000456}
457
Erich Keanef702b022018-07-13 19:46:04 +0000458void ASTTypeWriter::VisitDependentVectorType(const DependentVectorType *T) {
459 Record.AddTypeRef(T->getElementType());
460 Record.AddStmt(const_cast<Expr*>(T->getSizeExpr()));
461 Record.AddSourceLocation(T->getAttributeLoc());
462 Record.push_back(T->getVectorKind());
463 Code = TYPE_DEPENDENT_SIZED_VECTOR;
464}
465
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000466void
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000467ASTTypeWriter::VisitDependentAddressSpaceType(
468 const DependentAddressSpaceType *T) {
469 Record.AddTypeRef(T->getPointeeType());
470 Record.AddStmt(T->getAddrSpaceExpr());
471 Record.AddSourceLocation(T->getAttributeLoc());
472 Code = TYPE_DEPENDENT_ADDRESS_SPACE;
473}
474
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000475void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000476ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000477 Record.push_back(T->getDepth());
478 Record.push_back(T->getIndex());
479 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000480 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000481 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000482}
483
484void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000485ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000486 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000487 Record.AddNestedNameSpecifier(T->getQualifier());
488 Record.AddIdentifierRef(T->getIdentifier());
489 Record.AddTypeRef(
490 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000491 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000492}
493
494void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000495ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000496 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000497 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000498 Record.AddNestedNameSpecifier(T->getQualifier());
499 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000500 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000501 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000502 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000503 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000504}
505
Douglas Gregord2fa7662010-12-20 02:24:11 +0000506void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000507 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000508 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000509 Record.push_back(*NumExpansions + 1);
510 else
511 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000512 Code = TYPE_PACK_EXPANSION;
513}
514
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000515void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000516 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000517 Code = TYPE_PAREN;
518}
519
Leonard Chanc72aaf62019-05-07 03:20:17 +0000520void ASTTypeWriter::VisitMacroQualifiedType(const MacroQualifiedType *T) {
521 Record.AddTypeRef(T->getUnderlyingType());
522 Record.AddIdentifierRef(T->getMacroIdentifier());
523 Code = TYPE_MACRO_QUALIFIED;
524}
525
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000526void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000527 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000528 Record.AddNestedNameSpecifier(T->getQualifier());
529 Record.AddTypeRef(T->getNamedType());
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000530 Record.AddDeclRef(T->getOwnedTagDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000531 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000532}
533
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000534void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000535 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
536 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000537 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000538}
539
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000540void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000541 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000542 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000543}
544
Manman Rene6be26c2016-09-13 17:25:08 +0000545void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
546 Record.AddDeclRef(T->getDecl());
547 Record.push_back(T->getNumProtocols());
548 for (const auto *I : T->quals())
549 Record.AddDeclRef(I);
550 Code = TYPE_OBJC_TYPE_PARAM;
551}
552
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000553void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000554 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000555 Record.push_back(T->getTypeArgsAsWritten().size());
556 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000557 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000558 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000559 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000560 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000561 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000562 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000563}
564
Steve Narofffb4330f2009-06-17 22:40:22 +0000565void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000566ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000567 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000568 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000569}
570
Eli Friedman0dfb8892011-10-06 23:00:33 +0000571void
572ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000573 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000574 Code = TYPE_ATOMIC;
575}
576
Xiuli Pan9c14e282016-01-09 12:53:17 +0000577void
578ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000579 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000580 Record.push_back(T->isReadOnly());
581 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000582}
583
John McCall8f115c62009-10-16 21:56:05 +0000584namespace {
585
586class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000587 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000588
589public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000590 TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {}
John McCall8f115c62009-10-16 21:56:05 +0000591
John McCall17001972009-10-18 01:05:36 +0000592#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000593#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000594 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000595#include "clang/AST/TypeLocNodes.def"
596
John McCall17001972009-10-18 01:05:36 +0000597 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
598 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000599};
600
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000601} // namespace
John McCall8f115c62009-10-16 21:56:05 +0000602
John McCall17001972009-10-18 01:05:36 +0000603void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
604 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000605}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000606
John McCall17001972009-10-18 01:05:36 +0000607void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000608 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000609 if (TL.needsExtraLocalData()) {
Faisal Vali090da2d2018-01-01 18:23:28 +0000610 Record.push_back(TL.getWrittenTypeSpec());
611 Record.push_back(TL.getWrittenSignSpec());
612 Record.push_back(TL.getWrittenWidthSpec());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000613 Record.push_back(TL.hasModeAttr());
614 }
John McCall8f115c62009-10-16 21:56:05 +0000615}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000616
John McCall17001972009-10-18 01:05:36 +0000617void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000618 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000619}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000620
John McCall17001972009-10-18 01:05:36 +0000621void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000622 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000623}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000624
Reid Kleckner8a365022013-06-24 17:51:48 +0000625void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
626 // nothing to do
627}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000628
Reid Kleckner0503a872013-12-05 01:23:43 +0000629void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
630 // nothing to do
631}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000632
John McCall17001972009-10-18 01:05:36 +0000633void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000634 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000635}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000636
John McCall17001972009-10-18 01:05:36 +0000637void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000638 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000639}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000640
John McCall17001972009-10-18 01:05:36 +0000641void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000642 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000643}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000644
John McCall17001972009-10-18 01:05:36 +0000645void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000646 Record.AddSourceLocation(TL.getStarLoc());
647 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000648}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000649
John McCall17001972009-10-18 01:05:36 +0000650void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000651 Record.AddSourceLocation(TL.getLBracketLoc());
652 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000653 Record.push_back(TL.getSizeExpr() ? 1 : 0);
654 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000655 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000656}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000657
John McCall17001972009-10-18 01:05:36 +0000658void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
659 VisitArrayTypeLoc(TL);
660}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000661
John McCall17001972009-10-18 01:05:36 +0000662void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
663 VisitArrayTypeLoc(TL);
664}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000665
John McCall17001972009-10-18 01:05:36 +0000666void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
667 VisitArrayTypeLoc(TL);
668}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000669
John McCall17001972009-10-18 01:05:36 +0000670void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
671 DependentSizedArrayTypeLoc TL) {
672 VisitArrayTypeLoc(TL);
673}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000674
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000675void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
676 DependentAddressSpaceTypeLoc TL) {
677 Record.AddSourceLocation(TL.getAttrNameLoc());
678 SourceRange range = TL.getAttrOperandParensRange();
679 Record.AddSourceLocation(range.getBegin());
680 Record.AddSourceLocation(range.getEnd());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000681 Record.AddStmt(TL.getAttrExprOperand());
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000682}
683
John McCall17001972009-10-18 01:05:36 +0000684void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
685 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000686 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000687}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000688
John McCall17001972009-10-18 01:05:36 +0000689void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000690 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000691}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000692
Erich Keanef702b022018-07-13 19:46:04 +0000693void TypeLocWriter::VisitDependentVectorTypeLoc(
694 DependentVectorTypeLoc TL) {
695 Record.AddSourceLocation(TL.getNameLoc());
696}
697
John McCall17001972009-10-18 01:05:36 +0000698void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000699 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000700}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000701
John McCall17001972009-10-18 01:05:36 +0000702void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000703 Record.AddSourceLocation(TL.getLocalRangeBegin());
704 Record.AddSourceLocation(TL.getLParenLoc());
705 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000706 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000707 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000708 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000709 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000710}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000711
John McCall17001972009-10-18 01:05:36 +0000712void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
713 VisitFunctionTypeLoc(TL);
714}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000715
John McCall17001972009-10-18 01:05:36 +0000716void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
717 VisitFunctionTypeLoc(TL);
718}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000719
John McCallb96ec562009-12-04 22:46:56 +0000720void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000721 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000722}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000723
John McCall17001972009-10-18 01:05:36 +0000724void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000725 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000726}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000727
Manman Rene6be26c2016-09-13 17:25:08 +0000728void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
729 if (TL.getNumProtocols()) {
730 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
731 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
732 }
733 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
734 Record.AddSourceLocation(TL.getProtocolLoc(i));
735}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000736
John McCall17001972009-10-18 01:05:36 +0000737void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000738 Record.AddSourceLocation(TL.getTypeofLoc());
739 Record.AddSourceLocation(TL.getLParenLoc());
740 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000741}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000742
John McCall17001972009-10-18 01:05:36 +0000743void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000744 Record.AddSourceLocation(TL.getTypeofLoc());
745 Record.AddSourceLocation(TL.getLParenLoc());
746 Record.AddSourceLocation(TL.getRParenLoc());
747 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000748}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000749
John McCall17001972009-10-18 01:05:36 +0000750void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000751 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000752}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000753
Alexis Hunte852b102011-05-24 22:41:36 +0000754void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000755 Record.AddSourceLocation(TL.getKWLoc());
756 Record.AddSourceLocation(TL.getLParenLoc());
757 Record.AddSourceLocation(TL.getRParenLoc());
758 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000759}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000760
Richard Smith30482bc2011-02-20 03:19:35 +0000761void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000762 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000763}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000764
Richard Smith600b5262017-01-26 20:40:47 +0000765void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
766 DeducedTemplateSpecializationTypeLoc TL) {
767 Record.AddSourceLocation(TL.getTemplateNameLoc());
768}
769
John McCall17001972009-10-18 01:05:36 +0000770void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000771 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000772}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000773
John McCall17001972009-10-18 01:05:36 +0000774void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000775 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000776}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000777
John McCall81904512011-01-06 01:58:22 +0000778void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smithe43e2b32018-08-20 21:47:29 +0000779 Record.AddAttr(TL.getAttr());
John McCall81904512011-01-06 01:58:22 +0000780}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000781
John McCall17001972009-10-18 01:05:36 +0000782void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000783 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000784}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000785
John McCallcebee162009-10-18 09:09:24 +0000786void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
787 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000788 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000789}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000790
Douglas Gregorada4b792011-01-14 02:55:32 +0000791void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
792 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000793 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000794}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000795
John McCall17001972009-10-18 01:05:36 +0000796void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
797 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000798 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
799 Record.AddSourceLocation(TL.getTemplateNameLoc());
800 Record.AddSourceLocation(TL.getLAngleLoc());
801 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000802 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000803 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
804 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000805}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000806
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000807void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000808 Record.AddSourceLocation(TL.getLParenLoc());
809 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000810}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000811
Leonard Chanc72aaf62019-05-07 03:20:17 +0000812void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
813 Record.AddSourceLocation(TL.getExpansionLoc());
814}
815
Abramo Bagnara6150c882010-05-11 21:36:43 +0000816void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000817 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
818 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000819}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000820
John McCalle78aac42010-03-10 03:28:59 +0000821void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000822 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000823}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000824
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000825void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000826 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
827 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
828 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000829}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000830
John McCallc392f372010-06-11 00:33:02 +0000831void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
832 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000833 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
834 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
835 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
836 Record.AddSourceLocation(TL.getTemplateNameLoc());
837 Record.AddSourceLocation(TL.getLAngleLoc());
838 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000839 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000840 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
841 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000842}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000843
Douglas Gregord2fa7662010-12-20 02:24:11 +0000844void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000845 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000846}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000847
John McCall17001972009-10-18 01:05:36 +0000848void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000849 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000850}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000851
John McCall8b07ec22010-05-15 11:32:37 +0000852void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
853 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000854 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
855 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000856 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000857 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
858 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
859 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000860 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000861 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000862}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000863
John McCallfc93cf92009-10-22 22:37:11 +0000864void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000865 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000866}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000867
Eli Friedman0dfb8892011-10-06 23:00:33 +0000868void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000869 Record.AddSourceLocation(TL.getKWLoc());
870 Record.AddSourceLocation(TL.getLParenLoc());
871 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000872}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000873
Xiuli Pan9c14e282016-01-09 12:53:17 +0000874void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000875 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000876}
John McCall8f115c62009-10-16 21:56:05 +0000877
Richard Smith01b2cb42014-07-26 06:37:51 +0000878void ASTWriter::WriteTypeAbbrevs() {
879 using namespace llvm;
880
David Blaikieb44f0bf2017-01-04 22:36:43 +0000881 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000882
883 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000884 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000885 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
886 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
887 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000888 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000889
890 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000891 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000892 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
893 // FunctionType
894 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
895 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
896 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
897 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
898 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
899 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000900 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000901 Abv->Add(BitCodeAbbrevOp(0)); // NoCfCheck
Richard Smith01b2cb42014-07-26 06:37:51 +0000902 // FunctionProtoType
903 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
904 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
905 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
906 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
907 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
908 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
909 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
910 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000911 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000912}
913
Chris Lattner19cea4e2009-04-22 05:57:30 +0000914//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000915// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000916//===----------------------------------------------------------------------===//
917
Chris Lattner28fa4e62009-04-26 22:26:21 +0000918static void EmitBlockID(unsigned ID, const char *Name,
919 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000920 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000921 Record.clear();
922 Record.push_back(ID);
923 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
924
925 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000926 if (!Name || Name[0] == 0)
927 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000928 Record.clear();
929 while (*Name)
930 Record.push_back(*Name++);
931 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
932}
933
934static void EmitRecordID(unsigned ID, const char *Name,
935 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000936 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000937 Record.clear();
938 Record.push_back(ID);
939 while (*Name)
940 Record.push_back(*Name++);
941 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000942}
943
944static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000945 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000946#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000947 RECORD(STMT_STOP);
948 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000949 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000950 RECORD(STMT_NULL);
951 RECORD(STMT_COMPOUND);
952 RECORD(STMT_CASE);
953 RECORD(STMT_DEFAULT);
954 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000955 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000956 RECORD(STMT_IF);
957 RECORD(STMT_SWITCH);
958 RECORD(STMT_WHILE);
959 RECORD(STMT_DO);
960 RECORD(STMT_FOR);
961 RECORD(STMT_GOTO);
962 RECORD(STMT_INDIRECT_GOTO);
963 RECORD(STMT_CONTINUE);
964 RECORD(STMT_BREAK);
965 RECORD(STMT_RETURN);
966 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000967 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000968 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000969 RECORD(EXPR_PREDEFINED);
970 RECORD(EXPR_DECL_REF);
971 RECORD(EXPR_INTEGER_LITERAL);
972 RECORD(EXPR_FLOATING_LITERAL);
973 RECORD(EXPR_IMAGINARY_LITERAL);
974 RECORD(EXPR_STRING_LITERAL);
975 RECORD(EXPR_CHARACTER_LITERAL);
976 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000977 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000978 RECORD(EXPR_UNARY_OPERATOR);
979 RECORD(EXPR_SIZEOF_ALIGN_OF);
980 RECORD(EXPR_ARRAY_SUBSCRIPT);
981 RECORD(EXPR_CALL);
982 RECORD(EXPR_MEMBER);
983 RECORD(EXPR_BINARY_OPERATOR);
984 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
985 RECORD(EXPR_CONDITIONAL_OPERATOR);
986 RECORD(EXPR_IMPLICIT_CAST);
987 RECORD(EXPR_CSTYLE_CAST);
988 RECORD(EXPR_COMPOUND_LITERAL);
989 RECORD(EXPR_EXT_VECTOR_ELEMENT);
990 RECORD(EXPR_INIT_LIST);
991 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000992 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000993 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000994 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000995 RECORD(EXPR_VA_ARG);
996 RECORD(EXPR_ADDR_LABEL);
997 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000998 RECORD(EXPR_CHOOSE);
999 RECORD(EXPR_GNU_NULL);
1000 RECORD(EXPR_SHUFFLE_VECTOR);
1001 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +00001002 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001003 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +00001004 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001005 RECORD(EXPR_OBJC_ARRAY_LITERAL);
1006 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001007 RECORD(EXPR_OBJC_ENCODE);
1008 RECORD(EXPR_OBJC_SELECTOR_EXPR);
1009 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
1010 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
1011 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
1012 RECORD(EXPR_OBJC_KVC_REF_EXPR);
1013 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001014 RECORD(STMT_OBJC_FOR_COLLECTION);
1015 RECORD(STMT_OBJC_CATCH);
1016 RECORD(STMT_OBJC_FINALLY);
1017 RECORD(STMT_OBJC_AT_TRY);
1018 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
1019 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001020 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001021 RECORD(STMT_CXX_CATCH);
1022 RECORD(STMT_CXX_TRY);
1023 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001024 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001025 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001026 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001027 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001028 RECORD(EXPR_CXX_STATIC_CAST);
1029 RECORD(EXPR_CXX_DYNAMIC_CAST);
1030 RECORD(EXPR_CXX_REINTERPRET_CAST);
1031 RECORD(EXPR_CXX_CONST_CAST);
1032 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +00001033 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +00001034 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001035 RECORD(EXPR_CXX_BOOL_LITERAL);
1036 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001037 RECORD(EXPR_CXX_TYPEID_EXPR);
1038 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001039 RECORD(EXPR_CXX_THIS);
1040 RECORD(EXPR_CXX_THROW);
1041 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +00001042 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001043 RECORD(EXPR_CXX_BIND_TEMPORARY);
1044 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
1045 RECORD(EXPR_CXX_NEW);
1046 RECORD(EXPR_CXX_DELETE);
1047 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
1048 RECORD(EXPR_EXPR_WITH_CLEANUPS);
1049 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
1050 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
1051 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
1052 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
1053 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +00001054 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001055 RECORD(EXPR_CXX_NOEXCEPT);
1056 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +00001057 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
1058 RECORD(EXPR_TYPE_TRAIT);
1059 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001060 RECORD(EXPR_PACK_EXPANSION);
1061 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001062 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001063 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001064 RECORD(EXPR_FUNCTION_PARM_PACK);
1065 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001066 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001067 RECORD(EXPR_CXX_UUIDOF_EXPR);
1068 RECORD(EXPR_CXX_UUIDOF_TYPE);
1069 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001070#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001071}
Mike Stump11289f42009-09-09 15:08:12 +00001072
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001073void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001074 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001075 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001076
Sebastian Redl539c5062010-08-18 23:57:32 +00001077#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1078#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001079
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001080 // Control Block.
1081 BLOCK(CONTROL_BLOCK);
1082 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001083 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001084 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001085 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001086 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001087 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001088 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001089 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001090 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001091
1092 BLOCK(OPTIONS_BLOCK);
1093 RECORD(LANGUAGE_OPTIONS);
1094 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001095 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001096 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001097 RECORD(PREPROCESSOR_OPTIONS);
1098
Douglas Gregor108cb222012-10-19 00:45:00 +00001099 BLOCK(INPUT_FILES_BLOCK);
1100 RECORD(INPUT_FILE);
1101
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001102 // AST Top-Level Block.
1103 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001104 RECORD(TYPE_OFFSET);
1105 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001106 RECORD(IDENTIFIER_OFFSET);
1107 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001108 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001109 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001110 RECORD(SPECIAL_TYPES);
1111 RECORD(STATISTICS);
1112 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001113 RECORD(SELECTOR_OFFSETS);
1114 RECORD(METHOD_POOL);
1115 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001116 RECORD(SOURCE_LOCATION_OFFSETS);
1117 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001118 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001119 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001120 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001121 RECORD(VTABLE_USES);
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00001122 RECORD(PPD_SKIPPED_RANGES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001123 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001124 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001125 RECORD(SEMA_DECL_REFS);
1126 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1127 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001128 RECORD(UPDATE_VISIBLE);
1129 RECORD(DECL_UPDATE_OFFSETS);
1130 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001131 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001132 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001133 RECORD(FP_PRAGMA_OPTIONS);
1134 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001135 RECORD(OPENCL_EXTENSION_TYPES);
1136 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001137 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001138 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001139 RECORD(MODULE_OFFSET_MAP);
1140 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001141 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001142 RECORD(FILE_SORTED_DECLS);
1143 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001144 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001145 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001146 RECORD(INTERESTING_IDENTIFIERS);
1147 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001148 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001149 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001150 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001151 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001152 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001153 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001154 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00001155 RECORD(PP_CONDITIONAL_STACK);
Douglas Gregor358cd442012-01-15 16:58:34 +00001156
Chris Lattner28fa4e62009-04-26 22:26:21 +00001157 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001158 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001159 RECORD(SM_SLOC_FILE_ENTRY);
1160 RECORD(SM_SLOC_BUFFER_ENTRY);
1161 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001162 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001163 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001164
Chris Lattner28fa4e62009-04-26 22:26:21 +00001165 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001166 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001167 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001168 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001169 RECORD(PP_MACRO_OBJECT_LIKE);
1170 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001171 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001172
Richard Smithfa715652015-08-31 22:43:10 +00001173 // Submodule Block.
1174 BLOCK(SUBMODULE_BLOCK);
1175 RECORD(SUBMODULE_METADATA);
1176 RECORD(SUBMODULE_DEFINITION);
1177 RECORD(SUBMODULE_UMBRELLA_HEADER);
1178 RECORD(SUBMODULE_HEADER);
1179 RECORD(SUBMODULE_TOPHEADER);
1180 RECORD(SUBMODULE_UMBRELLA_DIR);
1181 RECORD(SUBMODULE_IMPORTS);
1182 RECORD(SUBMODULE_EXPORTS);
1183 RECORD(SUBMODULE_REQUIRES);
1184 RECORD(SUBMODULE_EXCLUDED_HEADER);
1185 RECORD(SUBMODULE_LINK_LIBRARY);
1186 RECORD(SUBMODULE_CONFIG_MACRO);
1187 RECORD(SUBMODULE_CONFLICT);
1188 RECORD(SUBMODULE_PRIVATE_HEADER);
1189 RECORD(SUBMODULE_TEXTUAL_HEADER);
1190 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001191 RECORD(SUBMODULE_INITIALIZERS);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00001192 RECORD(SUBMODULE_EXPORT_AS);
Richard Smithfa715652015-08-31 22:43:10 +00001193
1194 // Comments Block.
1195 BLOCK(COMMENTS_BLOCK);
1196 RECORD(COMMENTS_RAW_COMMENT);
1197
Douglas Gregor12bfa382009-10-17 00:13:19 +00001198 // Decls and Types block.
1199 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001200 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001201 RECORD(TYPE_COMPLEX);
1202 RECORD(TYPE_POINTER);
1203 RECORD(TYPE_BLOCK_POINTER);
1204 RECORD(TYPE_LVALUE_REFERENCE);
1205 RECORD(TYPE_RVALUE_REFERENCE);
1206 RECORD(TYPE_MEMBER_POINTER);
1207 RECORD(TYPE_CONSTANT_ARRAY);
1208 RECORD(TYPE_INCOMPLETE_ARRAY);
1209 RECORD(TYPE_VARIABLE_ARRAY);
1210 RECORD(TYPE_VECTOR);
1211 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001212 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001213 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001214 RECORD(TYPE_TYPEDEF);
1215 RECORD(TYPE_TYPEOF_EXPR);
1216 RECORD(TYPE_TYPEOF);
1217 RECORD(TYPE_RECORD);
1218 RECORD(TYPE_ENUM);
1219 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001220 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001221 RECORD(TYPE_DECLTYPE);
1222 RECORD(TYPE_ELABORATED);
1223 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1224 RECORD(TYPE_UNRESOLVED_USING);
1225 RECORD(TYPE_INJECTED_CLASS_NAME);
1226 RECORD(TYPE_OBJC_OBJECT);
1227 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1228 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1229 RECORD(TYPE_DEPENDENT_NAME);
1230 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1231 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1232 RECORD(TYPE_PAREN);
Leonard Chanc72aaf62019-05-07 03:20:17 +00001233 RECORD(TYPE_MACRO_QUALIFIED);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001234 RECORD(TYPE_PACK_EXPANSION);
1235 RECORD(TYPE_ATTRIBUTED);
1236 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001237 RECORD(TYPE_AUTO);
1238 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001239 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001240 RECORD(TYPE_DECAYED);
1241 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001242 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001243 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001244 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001245 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001246 RECORD(DECL_ENUM);
1247 RECORD(DECL_RECORD);
1248 RECORD(DECL_ENUM_CONSTANT);
1249 RECORD(DECL_FUNCTION);
1250 RECORD(DECL_OBJC_METHOD);
1251 RECORD(DECL_OBJC_INTERFACE);
1252 RECORD(DECL_OBJC_PROTOCOL);
1253 RECORD(DECL_OBJC_IVAR);
1254 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001255 RECORD(DECL_OBJC_CATEGORY);
1256 RECORD(DECL_OBJC_CATEGORY_IMPL);
1257 RECORD(DECL_OBJC_IMPLEMENTATION);
1258 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1259 RECORD(DECL_OBJC_PROPERTY);
1260 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001261 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001262 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001263 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001264 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001265 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001266 RECORD(DECL_FILE_SCOPE_ASM);
1267 RECORD(DECL_BLOCK);
1268 RECORD(DECL_CONTEXT_LEXICAL);
1269 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001270 RECORD(DECL_NAMESPACE);
1271 RECORD(DECL_NAMESPACE_ALIAS);
1272 RECORD(DECL_USING);
1273 RECORD(DECL_USING_SHADOW);
1274 RECORD(DECL_USING_DIRECTIVE);
1275 RECORD(DECL_UNRESOLVED_USING_VALUE);
1276 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1277 RECORD(DECL_LINKAGE_SPEC);
1278 RECORD(DECL_CXX_RECORD);
1279 RECORD(DECL_CXX_METHOD);
1280 RECORD(DECL_CXX_CONSTRUCTOR);
1281 RECORD(DECL_CXX_DESTRUCTOR);
1282 RECORD(DECL_CXX_CONVERSION);
1283 RECORD(DECL_ACCESS_SPEC);
1284 RECORD(DECL_FRIEND);
1285 RECORD(DECL_FRIEND_TEMPLATE);
1286 RECORD(DECL_CLASS_TEMPLATE);
1287 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1288 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001289 RECORD(DECL_VAR_TEMPLATE);
1290 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1291 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001292 RECORD(DECL_FUNCTION_TEMPLATE);
1293 RECORD(DECL_TEMPLATE_TYPE_PARM);
1294 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1295 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Saar Razd7aae332019-07-10 21:25:49 +00001296 RECORD(DECL_CONCEPT);
Richard Smithefc884a2016-04-13 07:41:35 +00001297 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001298 RECORD(DECL_STATIC_ASSERT);
1299 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001300 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001301 RECORD(DECL_INDIRECTFIELD);
1302 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001303 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1304 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1305 RECORD(DECL_IMPORT);
1306 RECORD(DECL_OMP_THREADPRIVATE);
1307 RECORD(DECL_EMPTY);
1308 RECORD(DECL_OBJC_TYPE_PARAM);
1309 RECORD(DECL_OMP_CAPTUREDEXPR);
1310 RECORD(DECL_PRAGMA_COMMENT);
1311 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1312 RECORD(DECL_OMP_DECLARE_REDUCTION);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001313 RECORD(DECL_OMP_ALLOCATE);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001314
Douglas Gregor03412ba2011-06-03 02:27:19 +00001315 // Statements and Exprs can occur in the Decls and Types block.
1316 AddStmtsExprs(Stream, Record);
1317
Douglas Gregor92a96f52011-02-08 21:58:10 +00001318 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001319 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001320 RECORD(PPD_MACRO_DEFINITION);
1321 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001322
1323 // Decls and Types block.
1324 BLOCK(EXTENSION_BLOCK);
1325 RECORD(EXTENSION_METADATA);
1326
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001327 BLOCK(UNHASHED_CONTROL_BLOCK);
1328 RECORD(SIGNATURE);
1329 RECORD(DIAGNOSTIC_OPTIONS);
1330 RECORD(DIAG_PRAGMA_MAPPINGS);
1331
Chris Lattner28fa4e62009-04-26 22:26:21 +00001332#undef RECORD
1333#undef BLOCK
1334 Stream.ExitBlock();
1335}
1336
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001337/// Prepares a path for being written to an AST file by converting it
Richard Smith54cc3c22014-12-11 20:50:24 +00001338/// to an absolute path and removing nested './'s.
1339///
1340/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001341static bool cleanPathForOutput(FileManager &FileMgr,
1342 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001343 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001344 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001345}
1346
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001347/// Adjusts the given filename to only write out the portion of the
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001348/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001349///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001350/// \param Filename the file name to adjust.
1351///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001352/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1353/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001354///
1355/// \returns either the original filename (if it needs no adjustment) or the
1356/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001357static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001358adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001359 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001360
Richard Smith7ed1bc92014-12-05 22:42:13 +00001361 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001362 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001363
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001364 // Verify that the filename and the system root have the same prefix.
1365 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001366 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1367 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001368 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001370 // We hit the end of the filename before we hit the end of the system root.
1371 if (!Filename[Pos])
1372 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001373
Richard Smith7ed1bc92014-12-05 22:42:13 +00001374 // If there's not a path separator at the end of the base directory nor
1375 // immediately after it, then this isn't within the base directory.
1376 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1377 if (!llvm::sys::path::is_separator(BaseDir.back()))
1378 return Filename;
1379 } else {
1380 // If the file name has a '/' at the current position, skip over the '/'.
1381 // We distinguish relative paths from absolute paths by the
1382 // absence of '/' at the beginning of relative paths.
1383 //
1384 // FIXME: This is wrong. We distinguish them by asking if the path is
1385 // absolute, which isn't the same thing. And there might be multiple '/'s
1386 // in a row. Use a better mechanism to indicate whether we have emitted an
1387 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001388 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001389 }
Mike Stump11289f42009-09-09 15:08:12 +00001390
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001391 return Filename + Pos;
1392}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001393
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001394ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1395 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1396 llvm::SHA1 Hasher;
1397 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1398 auto Hash = Hasher.result();
1399
1400 // Convert to an array [5*i32].
1401 ASTFileSignature Signature;
1402 auto LShift = [&](unsigned char Val, unsigned Shift) {
1403 return (uint32_t)Val << Shift;
1404 };
1405 for (int I = 0; I != 5; ++I)
1406 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1407 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1408
1409 return Signature;
1410}
1411
1412ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1413 ASTContext &Context) {
1414 // Flush first to prepare the PCM hash (signature).
1415 Stream.FlushToWord();
1416 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1417
1418 // Enter the block and prepare to write records.
1419 RecordData Record;
1420 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1421
1422 // For implicit modules, write the hash of the PCM as its signature.
1423 ASTFileSignature Signature;
1424 if (WritingModule &&
1425 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1426 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1427 Record.append(Signature.begin(), Signature.end());
1428 Stream.EmitRecord(SIGNATURE, Record);
1429 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001430 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001431
1432 // Diagnostic options.
1433 const auto &Diags = Context.getDiagnostics();
1434 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1435#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1436#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1437 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1438#include "clang/Basic/DiagnosticOptions.def"
1439 Record.push_back(DiagOpts.Warnings.size());
1440 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1441 AddString(DiagOpts.Warnings[I], Record);
1442 Record.push_back(DiagOpts.Remarks.size());
1443 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1444 AddString(DiagOpts.Remarks[I], Record);
1445 // Note: we don't serialize the log or serialization file names, because they
1446 // are generally transient files and will almost always be overridden.
1447 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1448
1449 // Write out the diagnostic/pragma mappings.
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001450 WritePragmaDiagnosticMappings(Diags, /* isModule = */ WritingModule);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001451
1452 // Leave the options block.
1453 Stream.ExitBlock();
1454 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001455}
1456
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001457/// Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001458void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1459 StringRef isysroot,
1460 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001461 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001462
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001463 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001464 RecordData Record;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001465
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001466 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001467 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001468 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1469 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1470 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1471 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1472 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1473 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001474 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001475 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PCHHasObjectFile
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001476 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1477 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001478 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001479 assert((!WritingModule || isysroot.empty()) &&
1480 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001481 {
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001482 RecordData::value_type Record[] = {
1483 METADATA,
1484 VERSION_MAJOR,
1485 VERSION_MINOR,
1486 CLANG_VERSION_MAJOR,
1487 CLANG_VERSION_MINOR,
1488 !isysroot.empty(),
1489 IncludeTimestamps,
1490 Context.getLangOpts().BuildingPCHWithObjectFile,
1491 ASTHasCompilerErrors};
Mehdi Amini57a41912015-09-10 01:46:39 +00001492 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1493 getClangFullRepositoryVersion());
1494 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001495
Richard Smith8b706102017-05-31 20:56:55 +00001496 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001497 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001498 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001499 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001501 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001502 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001503 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1504 }
1505
Richard Smith7ed1bc92014-12-05 22:42:13 +00001506 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001507 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001508 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001509
1510 // If the home of the module is the current working directory, then we
1511 // want to pick up the cwd of the build process loading the module, not
1512 // our cwd, when we load this module.
1513 if (!PP.getHeaderSearchInfo()
1514 .getHeaderSearchOpts()
1515 .ModuleMapFileHomeIsCwd ||
1516 WritingModule->Directory->getName() != StringRef(".")) {
1517 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001518 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001519 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001521 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001522
Mehdi Amini57a41912015-09-10 01:46:39 +00001523 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001524 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1525 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001526
1527 // Write out all other paths relative to the base directory if possible.
1528 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1529 } else if (!isysroot.empty()) {
1530 // Write out paths relative to the sysroot if possible.
1531 BaseDirectory = isysroot;
1532 }
1533
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001534 // Module map file
Richard Smithd19389a2017-07-05 07:47:11 +00001535 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001536 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001537
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001538 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001539 AddPath(WritingModule->PresumedModuleMapFile.empty()
1540 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1541 : StringRef(WritingModule->PresumedModuleMapFile),
1542 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001543
1544 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001545 if (auto *AdditionalModMaps =
1546 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001547 Record.push_back(AdditionalModMaps->size());
1548 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001549 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001550 } else {
1551 Record.push_back(0);
1552 }
1553
1554 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001555 }
1556
Douglas Gregor112b9072012-10-18 05:31:06 +00001557 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001558 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001559 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001560 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001561
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001562 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001563 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001564 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001565 continue;
1566
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001567 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1568 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001569
1570 // If we have calculated signature, there is no need to store
1571 // the size or timestamp.
1572 Record.push_back(M.Signature ? 0 : M.File->getSize());
1573 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1574
1575 for (auto I : M.Signature)
1576 Record.push_back(I);
1577
Boris Kolpackovd30446f2017-08-31 06:26:43 +00001578 AddString(M.ModuleName, Record);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001579 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001580 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001581 Stream.EmitRecord(IMPORTS, Record);
1582 }
Mike Stump11289f42009-09-09 15:08:12 +00001583
Richard Smith0516b182015-09-08 19:40:14 +00001584 // Write the options block.
1585 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1586
Douglas Gregor112b9072012-10-18 05:31:06 +00001587 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001588 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001589 const LangOptions &LangOpts = Context.getLangOpts();
1590#define LANGOPT(Name, Bits, Default, Description) \
1591 Record.push_back(LangOpts.Name);
1592#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1593 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001594#include "clang/Basic/LangOptions.def"
1595#define SANITIZER(NAME, ID) \
1596 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001597#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001598
Ben Langmuircd98cb72015-06-23 18:20:18 +00001599 Record.push_back(LangOpts.ModuleFeatures.size());
1600 for (StringRef Feature : LangOpts.ModuleFeatures)
1601 AddString(Feature, Record);
1602
Douglas Gregor112b9072012-10-18 05:31:06 +00001603 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1604 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001605
1606 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001607
1608 // Comment options.
1609 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001610 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1611 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001612 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001613 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001614
Samuel Antaoee8fb302016-01-06 13:42:12 +00001615 // OpenMP offloading options.
1616 Record.push_back(LangOpts.OMPTargetTriples.size());
1617 for (auto &T : LangOpts.OMPTargetTriples)
1618 AddString(T.getTriple(), Record);
1619
1620 AddString(LangOpts.OMPHostIRFile, Record);
1621
Douglas Gregor112b9072012-10-18 05:31:06 +00001622 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1623
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001624 // Target options.
1625 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001626 const TargetInfo &Target = Context.getTargetInfo();
1627 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001628 AddString(TargetOpts.Triple, Record);
1629 AddString(TargetOpts.CPU, Record);
1630 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001631 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1632 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1633 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1634 }
1635 Record.push_back(TargetOpts.Features.size());
1636 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1637 AddString(TargetOpts.Features[I], Record);
1638 }
1639 Stream.EmitRecord(TARGET_OPTIONS, Record);
1640
Douglas Gregorc6317db2012-10-24 15:49:58 +00001641 // File system options.
1642 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001643 const FileSystemOptions &FSOpts =
1644 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001645 AddString(FSOpts.WorkingDir, Record);
1646 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1647
Douglas Gregor2d302362012-10-24 16:50:34 +00001648 // Header search options.
1649 Record.clear();
1650 const HeaderSearchOptions &HSOpts
1651 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1652 AddString(HSOpts.Sysroot, Record);
1653
1654 // Include entries.
1655 Record.push_back(HSOpts.UserEntries.size());
1656 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1657 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1658 AddString(Entry.Path, Record);
1659 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001660 Record.push_back(Entry.IsFramework);
1661 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001662 }
1663
1664 // System header prefixes.
1665 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1666 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1667 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1668 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1669 }
1670
1671 AddString(HSOpts.ResourceDir, Record);
1672 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001673 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001674 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001675 Record.push_back(HSOpts.ImplicitModuleMaps);
1676 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001677 Record.push_back(HSOpts.UseBuiltinIncludes);
1678 Record.push_back(HSOpts.UseStandardSystemIncludes);
1679 Record.push_back(HSOpts.UseStandardCXXIncludes);
1680 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001681 // Write out the specific module cache path that contains the module files.
1682 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001683 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1684
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001685 // Preprocessor options.
1686 Record.clear();
1687 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1688
1689 // Macro definitions.
1690 Record.push_back(PPOpts.Macros.size());
1691 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1692 AddString(PPOpts.Macros[I].first, Record);
1693 Record.push_back(PPOpts.Macros[I].second);
1694 }
1695
1696 // Includes
1697 Record.push_back(PPOpts.Includes.size());
1698 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1699 AddString(PPOpts.Includes[I], Record);
1700
1701 // Macro includes
1702 Record.push_back(PPOpts.MacroIncludes.size());
1703 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1704 AddString(PPOpts.MacroIncludes[I], Record);
1705
Douglas Gregorb6368752012-10-24 23:41:50 +00001706 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001707 // Detailed record is important since it is used for the module cache hash.
1708 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001709 AddString(PPOpts.ImplicitPCHInclude, Record);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001710 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1711 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1712
Richard Smith0516b182015-09-08 19:40:14 +00001713 // Leave the options block.
1714 Stream.ExitBlock();
1715
Douglas Gregora3b20262011-05-06 21:43:30 +00001716 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001717 SourceManager &SM = Context.getSourceManager();
1718 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001719 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001720 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1721 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001722 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001723 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001724
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001725 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001726 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001727 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001728 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001729 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001730
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001731 Record.clear();
1732 Record.push_back(SM.getMainFileID().getOpaqueValue());
1733 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1734
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001735 // Original PCH directory
1736 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001737 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001738 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001740 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001741
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001742 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001743
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001744 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001745 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1746
Mehdi Amini57a41912015-09-10 01:46:39 +00001747 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001748 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1749 }
1750
Douglas Gregor49491f72013-03-15 22:15:07 +00001751 WriteInputFiles(Context.SourceMgr,
1752 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001753 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001754 Stream.ExitBlock();
1755}
1756
Douglas Gregor49491f72013-03-15 22:15:07 +00001757namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001758
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001759/// An input file.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001760struct InputFileEntry {
1761 const FileEntry *File;
1762 bool IsSystemFile;
1763 bool IsTransient;
1764 bool BufferOverridden;
1765 bool IsTopLevelModuleMap;
1766};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001767
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001768} // namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001769
1770void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1771 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001772 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001773 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001774
Douglas Gregor72be3902012-10-19 00:38:02 +00001775 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001776
Douglas Gregor72be3902012-10-19 00:38:02 +00001777 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001778 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001779 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001780 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001781 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1782 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001783 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001784 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Richard Smithf3f84612017-06-29 02:19:42 +00001785 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
Douglas Gregor72be3902012-10-19 00:38:02 +00001786 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001787 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001788
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001789 // Get all ContentCache objects for files, sorted by whether the file is a
1790 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001791 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001792 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1793 // Get this source location entry.
1794 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001795 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001796
1797 // We only care about file entries that were not overridden.
1798 if (!SLoc->isFile())
1799 continue;
Richard Smithf3f84612017-06-29 02:19:42 +00001800 const SrcMgr::FileInfo &File = SLoc->getFile();
1801 const SrcMgr::ContentCache *Cache = File.getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001802 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001803 continue;
1804
Douglas Gregor49491f72013-03-15 22:15:07 +00001805 InputFileEntry Entry;
1806 Entry.File = Cache->OrigEntry;
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001807 Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
Richard Smitha8cfffa2015-11-26 02:04:16 +00001808 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001809 Entry.BufferOverridden = Cache->BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001810 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1811 File.getIncludeLoc().isInvalid();
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00001812 if (Entry.IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001813 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001814 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001815 SortedFiles.push_front(Entry);
1816 }
1817
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001818 unsigned UserFilesNum = 0;
1819 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001820 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001821 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001822 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001823 if (InputFileID != 0)
1824 continue; // already recorded this file.
1825
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001826 // Record this entry's offset.
1827 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001828
1829 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001830
Douglas Gregor49491f72013-03-15 22:15:07 +00001831 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001832 ++UserFilesNum;
1833
Douglas Gregor72be3902012-10-19 00:38:02 +00001834 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001835 // And whether this file was overridden.
1836 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001837 INPUT_FILE,
1838 InputFileOffsets.size(),
1839 (uint64_t)Entry.File->getSize(),
1840 (uint64_t)getTimestampForOutput(Entry.File),
1841 Entry.BufferOverridden,
Richard Smithf3f84612017-06-29 02:19:42 +00001842 Entry.IsTransient,
1843 Entry.IsTopLevelModuleMap};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001844
Alex Lorenz4dc55732019-08-22 18:15:50 +00001845 // FIXME: The path should be taken from the FileEntryRef.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001846 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1847 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001848
Douglas Gregor112b9072012-10-18 05:31:06 +00001849 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001850
1851 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001852 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001853 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1854 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001855 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1856 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001857 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001858 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001859
1860 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001861 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1862 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001863 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001864}
1865
Douglas Gregora7f71a92009-04-10 03:52:48 +00001866//===----------------------------------------------------------------------===//
1867// Source Manager Serialization
1868//===----------------------------------------------------------------------===//
1869
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001870/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001871/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001872static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001873 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001874
David Blaikieb44f0bf2017-01-04 22:36:43 +00001875 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001876 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001881 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001886 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001887}
1888
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001889/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001890/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001891static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001892 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001893
David Blaikieb44f0bf2017-01-04 22:36:43 +00001894 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001895 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001901 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001902}
1903
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001904/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001905/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001906static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1907 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001908 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001909
David Blaikieb44f0bf2017-01-04 22:36:43 +00001910 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001911 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1912 : SM_SLOC_BUFFER_BLOB));
1913 if (Compressed)
1914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001916 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001917}
1918
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001919/// Create an abbreviation for the SLocEntry that refers to a macro
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001920/// expansion.
1921static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001922 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001923
David Blaikieb44f0bf2017-01-04 22:36:43 +00001924 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001925 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Richard Smithb5f81712018-04-30 05:25:48 +00001930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
Douglas Gregor83243272009-04-15 18:05:10 +00001931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001932 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001933}
1934
Douglas Gregor09b69892011-02-10 17:09:37 +00001935namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001936
Douglas Gregor09b69892011-02-10 17:09:37 +00001937 // Trait used for the on-disk hash table of header search information.
1938 class HeaderFileInfoTrait {
1939 ASTWriter &Writer;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001940
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001941 // Keep track of the framework names we've used during serialization.
1942 SmallVector<char, 128> FrameworkStringData;
1943 llvm::StringMap<unsigned> FrameworkNameOffset;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001944
Douglas Gregor09b69892011-02-10 17:09:37 +00001945 public:
Richard Smith040e1262017-06-02 01:55:39 +00001946 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1947
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001948 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001949 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001950 off_t Size;
1951 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001952 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001953 using key_type_ref = const key_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001954
1955 using UnresolvedModule =
1956 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001957
Richard Smith040e1262017-06-02 01:55:39 +00001958 struct data_type {
1959 const HeaderFileInfo &HFI;
1960 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1961 UnresolvedModule Unresolved;
1962 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001963 using data_type_ref = const data_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001964
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001965 using hash_value_type = unsigned;
1966 using offset_type = unsigned;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001967
Richard Smithe75ee0f2015-08-17 07:13:32 +00001968 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001969 // The hash is based only on size/time of the file, so that the reader can
1970 // match even when symlinking or excess path elements ("foo/../", "../")
1971 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00001972 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00001973 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001974
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001975 std::pair<unsigned, unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001976 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001977 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001978
Peter Collingbournee3f65292018-05-18 19:46:24 +00001979 endian::Writer LE(Out, little);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001980 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001981 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001982 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00001983 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00001984 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1985 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00001986 if (Data.Unresolved.getPointer())
1987 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00001988 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001989 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001990 }
Richard Smith040e1262017-06-02 01:55:39 +00001991
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001992 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001993 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001994
Peter Collingbournee3f65292018-05-18 19:46:24 +00001995 endian::Writer LE(Out, little);
Richard Smith040e1262017-06-02 01:55:39 +00001996 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001997 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00001998 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001999 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00002000 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00002001 }
Richard Smith040e1262017-06-02 01:55:39 +00002002
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002003 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00002004 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002005 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002006
Peter Collingbournee3f65292018-05-18 19:46:24 +00002007 endian::Writer LE(Out, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00002008 uint64_t Start = Out.tell(); (void)Start;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002009
Richard Smithf3f84612017-06-29 02:19:42 +00002010 unsigned char Flags = (Data.HFI.isImport << 5)
2011 | (Data.HFI.isPragmaOnce << 4)
Richard Smith040e1262017-06-02 01:55:39 +00002012 | (Data.HFI.DirInfo << 1)
2013 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00002014 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00002015 LE.write<uint16_t>(Data.HFI.NumIncludes);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002016
Richard Smith040e1262017-06-02 01:55:39 +00002017 if (!Data.HFI.ControllingMacro)
2018 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00002019 else
Richard Smith040e1262017-06-02 01:55:39 +00002020 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
2021
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002022 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00002023 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002024 // If this header refers into a framework, save the framework name.
2025 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00002026 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002027 if (Pos == FrameworkNameOffset.end()) {
2028 Offset = FrameworkStringData.size() + 1;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002029 FrameworkStringData.append(Data.HFI.Framework.begin(),
Richard Smith040e1262017-06-02 01:55:39 +00002030 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002031 FrameworkStringData.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002032
Richard Smith040e1262017-06-02 01:55:39 +00002033 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002034 } else
2035 Offset = Pos->second;
2036 }
Justin Bognere1c147c2014-03-28 22:03:19 +00002037 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002038
Richard Smith040e1262017-06-02 01:55:39 +00002039 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
2040 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
2041 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00002042 assert((Value >> 2) == ModID && "overflow in header module info");
2043 LE.write<uint32_t>(Value);
2044 }
Richard Smith040e1262017-06-02 01:55:39 +00002045 };
2046
2047 // FIXME: If the header is excluded, we should write out some
2048 // record of that fact.
2049 for (auto ModInfo : Data.KnownHeaders)
2050 EmitModule(ModInfo.getModule(), ModInfo.getRole());
2051 if (Data.Unresolved.getPointer())
2052 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002053
Douglas Gregor09b69892011-02-10 17:09:37 +00002054 assert(Out.tell() - Start == DataLen && "Wrong data length");
2055 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002056
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002057 const char *strings_begin() const { return FrameworkStringData.begin(); }
2058 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00002059 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002060
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002061} // namespace
Douglas Gregor09b69892011-02-10 17:09:37 +00002062
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002063/// Write the header search block for the list of files that
Douglas Gregor09b69892011-02-10 17:09:37 +00002064///
2065/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00002066void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00002067 HeaderFileInfoTrait GeneratorTrait(*this);
2068 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
2069 SmallVector<const char *, 4> SavedStrings;
2070 unsigned NumHeaderSearchEntries = 0;
2071
2072 // Find all unresolved headers for the current module. We generally will
2073 // have resolved them before we get here, but not necessarily: we might be
2074 // compiling a preprocessed module, where there is no requirement for the
2075 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00002076 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00002077 if (WritingModule) {
2078 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
2079 while (!Worklist.empty()) {
2080 Module *M = Worklist.pop_back_val();
2081 if (!M->isAvailable())
2082 continue;
2083
2084 // Map to disk files where possible, to pick up any missing stat
2085 // information. This also means we don't need to check the unresolved
2086 // headers list when emitting resolved headers in the first loop below.
2087 // FIXME: It'd be preferable to avoid doing this if we were given
2088 // sufficient stat information in the module map.
2089 HS.getModuleMap().resolveHeaderDirectives(M);
2090
2091 // If the file didn't exist, we can still create a module if we were given
2092 // enough information in the module map.
2093 for (auto U : M->MissingHeaders) {
2094 // Check that we were given enough information to build a module
2095 // without this file existing on disk.
2096 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2097 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
2098 << WritingModule->getFullModuleName() << U.Size.hasValue()
2099 << U.FileName;
2100 continue;
2101 }
2102
2103 // Form the effective relative pathname for the file.
2104 SmallString<128> Filename(M->Directory->getName());
2105 llvm::sys::path::append(Filename, U.FileName);
2106 PreparePathForOutput(Filename);
2107
2108 StringRef FilenameDup = strdup(Filename.c_str());
2109 SavedStrings.push_back(FilenameDup.data());
2110
2111 HeaderFileInfoTrait::key_type Key = {
2112 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
2113 };
2114 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00002115 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00002116 };
2117 // FIXME: Deal with cases where there are multiple unresolved header
2118 // directives in different submodules for the same header.
2119 Generator.insert(Key, Data, GeneratorTrait);
2120 ++NumHeaderSearchEntries;
2121 }
2122
2123 Worklist.append(M->submodule_begin(), M->submodule_end());
2124 }
2125 }
2126
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002127 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00002128 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002129
Douglas Gregor09b69892011-02-10 17:09:37 +00002130 if (FilesByUID.size() > HS.header_file_size())
2131 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00002132
Douglas Gregor09b69892011-02-10 17:09:37 +00002133 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2134 const FileEntry *File = FilesByUID[UID];
2135 if (!File)
2136 continue;
2137
Richard Smith386bb072015-08-18 23:42:23 +00002138 // Get the file info. This will load info from the external source if
2139 // necessary. Skip emitting this file if we have no information on it
2140 // as a header file (in which case HFI will be null) or if it hasn't
2141 // changed since it was loaded. Also skip it if it's for a modular header
2142 // from a different module; in that case, we rely on the module(s)
2143 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00002144 const HeaderFileInfo *HFI =
2145 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
2146 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00002147 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00002148
Richard Smith7ed1bc92014-12-05 22:42:13 +00002149 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002150 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00002151 SmallString<128> FilenameTmp(Filename);
2152 if (PreparePathForOutput(FilenameTmp)) {
2153 // If we performed any translation on the file name at all, we need to
2154 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002155 Filename = StringRef(strdup(FilenameTmp.c_str()));
2156 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00002157 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002158
Richard Smith040e1262017-06-02 01:55:39 +00002159 HeaderFileInfoTrait::key_type Key = {
2160 Filename, File->getSize(), getTimestampForOutput(File)
2161 };
2162 HeaderFileInfoTrait::data_type Data = {
2163 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
2164 };
2165 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002166 ++NumHeaderSearchEntries;
2167 }
Richard Smith040e1262017-06-02 01:55:39 +00002168
Douglas Gregor09b69892011-02-10 17:09:37 +00002169 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002170 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002171 uint32_t BucketOffset;
2172 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002173 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002174
Douglas Gregor09b69892011-02-10 17:09:37 +00002175 llvm::raw_svector_ostream Out(TableData);
2176 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00002177 endian::write<uint32_t>(Out, 0, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00002178 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2179 }
2180
2181 // Create a blob abbreviation
2182 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002183
David Blaikieb44f0bf2017-01-04 22:36:43 +00002184 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002185 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002190 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002191
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002192 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002193 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2194 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002195 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002196 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002197
Douglas Gregor09b69892011-02-10 17:09:37 +00002198 // Free all of the strings we had to duplicate.
2199 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002200 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002201}
2202
George Rimarc39f5492017-01-17 15:45:31 +00002203static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2204 unsigned SLocBufferBlobCompressedAbbrv,
2205 unsigned SLocBufferBlobAbbrv) {
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002206 using RecordDataType = ASTWriter::RecordData::value_type;
George Rimarc39f5492017-01-17 15:45:31 +00002207
2208 // Compress the buffer if possible. We expect that almost all PCM
2209 // consumers will not want its contents.
2210 SmallString<0> CompressedBuffer;
2211 if (llvm::zlib::isAvailable()) {
2212 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2213 if (!E) {
2214 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2215 Blob.size() - 1};
2216 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2217 CompressedBuffer);
2218 return;
2219 }
2220 llvm::consumeError(std::move(E));
2221 }
2222
2223 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2224 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2225}
2226
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002227/// Writes the block containing the serialized form of the
Douglas Gregora7f71a92009-04-10 03:52:48 +00002228/// source manager.
2229///
2230/// TODO: We should probably use an on-disk hash table (stored in a
2231/// blob), indexed based on the file name, so that we only create
2232/// entries for files that we actually need. In the common case (no
2233/// errors), we probably won't have to create file entries for any of
2234/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002235void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002236 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002237 RecordData Record;
2238
Chris Lattner0910e3b2009-04-10 17:16:57 +00002239 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002240 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002241
2242 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002243 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2244 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002245 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2246 unsigned SLocBufferBlobCompressedAbbrv =
2247 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002248 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002249
Douglas Gregor258ae542009-04-27 06:38:32 +00002250 // Write out the source location entry table. We skip the first
2251 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002252 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002253 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002254 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2255 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002256 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002257 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002258 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002259 FileID FID = FileID::get(I);
2260 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002261
Douglas Gregor258ae542009-04-27 06:38:32 +00002262 // Record the offset of this source-location entry.
2263 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2264
2265 // Figure out which record code to use.
2266 unsigned Code;
2267 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002268 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2269 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002271 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002272 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002273 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002274 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002275 Record.clear();
2276 Record.push_back(Code);
2277
Douglas Gregor925296b2011-07-19 16:10:42 +00002278 // Starting offset of this entry within this module, so skip the dummy.
2279 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002280 if (SLoc->isFile()) {
2281 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002282 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002283 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2284 Record.push_back(File.hasLineDirectives());
2285
2286 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002287 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002288 if (Content->OrigEntry) {
2289 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002290 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002291
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002292 // The source location entry is a file. Emit input file ID.
2293 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2294 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002295
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002296 Record.push_back(File.NumCreatedFIDs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002297
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002298 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002299 if (FDI != FileDeclIDs.end()) {
2300 Record.push_back(FDI->second->FirstDeclIndex);
2301 Record.push_back(FDI->second->DeclIDs.size());
2302 } else {
2303 Record.push_back(0);
2304 Record.push_back(0);
2305 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002306
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002307 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002308
Richard Smithaada85c2016-02-06 02:06:43 +00002309 if (Content->BufferOverridden || Content->IsTransient)
2310 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002311 } else {
2312 // The source location entry is a buffer. The blob associated
2313 // with this entry contains the contents of the buffer.
2314
2315 // We add one to the size so that we capture the trailing NULL
2316 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2317 // the reader side).
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00002318 const llvm::MemoryBuffer *Buffer =
2319 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002320 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002321 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002322 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002323 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002324
Mehdi Amini99d1b292016-10-01 16:38:28 +00002325 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002326 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002327 }
Richard Smithaada85c2016-02-06 02:06:43 +00002328
2329 if (EmitBlob) {
2330 // Include the implicit terminating null character in the on-disk buffer
2331 // if we're writing it uncompressed.
2332 const llvm::MemoryBuffer *Buffer =
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00002333 Content->getBuffer(PP.getDiagnostics(), PP.getFileManager());
Richard Smithaada85c2016-02-06 02:06:43 +00002334 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002335 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2336 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002337 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002338 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002339 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002340 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002341 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2342 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2343 AddSourceLocation(Expansion.isMacroArgExpansion()
2344 ? SourceLocation()
2345 : Expansion.getExpansionLocEnd(),
2346 Record);
Richard Smithb5f81712018-04-30 05:25:48 +00002347 Record.push_back(Expansion.isExpansionTokenRange());
Douglas Gregor258ae542009-04-27 06:38:32 +00002348
2349 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002350 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002351 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002352 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002353 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002354 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002355 }
2356 }
2357
Douglas Gregor8f45df52009-04-16 22:23:12 +00002358 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002359
2360 if (SLocEntryOffsets.empty())
2361 return;
2362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002363 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002364 // table is used for lazily loading source-location information.
2365 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002366
David Blaikieb44f0bf2017-01-04 22:36:43 +00002367 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002368 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002372 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002373 {
2374 RecordData::value_type Record[] = {
2375 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2376 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2377 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2378 bytes(SLocEntryOffsets));
2379 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002380 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002381 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002382 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002383
2384 // Write the line table. It depends on remapping working, so it must come
2385 // after the source location offsets.
2386 if (SourceMgr.hasLineTable()) {
2387 LineTableInfo &LineTable = SourceMgr.getLineTable();
2388
2389 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002390
2391 // Emit the needed file names.
2392 llvm::DenseMap<int, int> FilenameMap;
Hans Wennborg14487362017-12-04 22:28:45 +00002393 FilenameMap[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00002394 for (const auto &L : LineTable) {
2395 if (L.first.ID < 0)
2396 continue;
2397 for (auto &LE : L.second) {
2398 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
Hans Wennborg14487362017-12-04 22:28:45 +00002399 FilenameMap.size() - 1)).second)
Richard Smith63078492015-09-01 07:41:55 +00002400 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2401 }
2402 }
2403 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002404
2405 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002406 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002407 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002408 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002409 continue;
2410
2411 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002412 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002413
2414 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002415 Record.push_back(L.second.size());
2416 for (const auto &LE : L.second) {
2417 Record.push_back(LE.FileOffset);
2418 Record.push_back(LE.LineNo);
2419 Record.push_back(FilenameMap[LE.FilenameID]);
2420 Record.push_back((unsigned)LE.FileKind);
2421 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002422 }
2423 }
Richard Smith63078492015-09-01 07:41:55 +00002424
Douglas Gregor925296b2011-07-19 16:10:42 +00002425 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2426 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002427}
2428
Douglas Gregorc5046832009-04-27 18:38:38 +00002429//===----------------------------------------------------------------------===//
2430// Preprocessor Serialization
2431//===----------------------------------------------------------------------===//
2432
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002433static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2434 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002435 if (MacroInfo *MI = MD->getMacroInfo())
2436 if (MI->isBuiltinMacro())
2437 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002438
2439 if (IsModule) {
2440 SourceLocation Loc = MD->getLocation();
2441 if (Loc.isInvalid())
2442 return true;
2443 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2444 return true;
2445 }
2446
2447 return false;
2448}
2449
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002450/// Writes the block containing the serialized form of the
Chris Lattnereeffaef2009-04-10 17:15:23 +00002451/// preprocessor.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002452void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002453 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2454 if (PPRec)
2455 WritePreprocessorDetail(*PPRec);
2456
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002457 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002458 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002459
Chris Lattner0af3ba12009-04-13 01:29:17 +00002460 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2461 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002462 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002463 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002464 }
2465
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002466 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2467 assert(!IsModule);
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00002468 auto SkipInfo = PP.getPreambleSkipInfo();
2469 if (SkipInfo.hasValue()) {
2470 Record.push_back(true);
2471 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2472 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2473 Record.push_back(SkipInfo->FoundNonSkipPortion);
2474 Record.push_back(SkipInfo->FoundElse);
2475 AddSourceLocation(SkipInfo->ElseLoc, Record);
2476 } else {
2477 Record.push_back(false);
2478 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002479 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2480 AddSourceLocation(Cond.IfLoc, Record);
2481 Record.push_back(Cond.WasSkipping);
2482 Record.push_back(Cond.FoundNonSkip);
2483 Record.push_back(Cond.FoundElse);
2484 }
2485 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2486 Record.clear();
2487 }
2488
Douglas Gregoreda6a892009-04-26 00:07:37 +00002489 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002490 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002491
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002492 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002493 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002494 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002495 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002496
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002497 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002498 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002499
Richard Smithee977932015-05-01 21:22:17 +00002500 // Construct the list of identifiers with macro directives that need to be
2501 // serialized.
2502 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2503 for (auto &Id : PP.getIdentifierTable())
2504 if (Id.second->hadMacroDefinition() &&
2505 (!Id.second->isFromAST() ||
2506 Id.second->hasChangedSinceDeserialization()))
2507 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002508 // Sort the set of macro definitions that need to be serialized by the
2509 // name of the macro, to provide a stable ordering.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00002510 llvm::sort(MacroIdentifiers, llvm::deref<std::less<>>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002511
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002512 // Emit the macro directives as a list and associate the offset with the
2513 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002514 for (const IdentifierInfo *Name : MacroIdentifiers) {
2515 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002516 auto StartOffset = Stream.GetCurrentBitNo();
2517
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002518 // Emit the macro directives in reverse source order.
2519 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002520 // Once we hit an ignored macro, we're done: the rest of the chain
2521 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002522 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002523 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002524
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002525 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002526 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002527 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002528 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002529 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002530 Record.push_back(VisMD->isPublic());
2531 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002532 }
Richard Smithd7329392015-04-21 21:46:32 +00002533
Richard Smithb8b2ed62015-04-23 18:18:26 +00002534 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002535 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002536 // We write out exported module macros for PCH as well.
2537 auto Leafs = PP.getLeafModuleMacros(Name);
2538 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2539 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2540 while (!Worklist.empty()) {
2541 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002542
Manman Ren33d80292016-05-31 18:19:32 +00002543 // Emit a record indicating this submodule exports this macro.
2544 ModuleMacroRecord.push_back(
2545 getSubmoduleID(Macro->getOwningModule()));
2546 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2547 for (auto *M : Macro->overrides())
2548 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002549
Manman Ren33d80292016-05-31 18:19:32 +00002550 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2551 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002552
Manman Ren33d80292016-05-31 18:19:32 +00002553 // Enqueue overridden macros once we've visited all their ancestors.
2554 for (auto *M : Macro->overrides())
2555 if (++Visits[M] == M->getNumOverridingMacros())
2556 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002557
Manman Ren33d80292016-05-31 18:19:32 +00002558 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002559 }
Richard Smithd7329392015-04-21 21:46:32 +00002560
Richard Smithee977932015-05-01 21:22:17 +00002561 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002562 continue;
2563
Richard Smithd7329392015-04-21 21:46:32 +00002564 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002565 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2566 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002567 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002568
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002569 /// Offsets of each of the macros into the bitstream, indexed by
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002570 /// the local macro ID
2571 ///
2572 /// For each identifier that is associated with a macro, this map
2573 /// provides the offset into the bitstream where that macro is
2574 /// defined.
2575 std::vector<uint32_t> MacroOffsets;
2576
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002577 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2578 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2579 MacroInfo *MI = MacroInfosToEmit[I].MI;
2580 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002581
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002582 if (ID < FirstMacroID) {
2583 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2584 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002585 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002586
2587 // Record the local offset of this macro.
2588 unsigned Index = ID - FirstMacroID;
2589 if (Index == MacroOffsets.size())
2590 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2591 else {
2592 if (Index > MacroOffsets.size())
2593 MacroOffsets.resize(Index + 1);
2594
2595 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2596 }
2597
2598 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002599 AddSourceLocation(MI->getDefinitionLoc(), Record);
2600 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2601 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002602 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002603 unsigned Code;
2604 if (MI->isObjectLike()) {
2605 Code = PP_MACRO_OBJECT_LIKE;
2606 } else {
2607 Code = PP_MACRO_FUNCTION_LIKE;
2608
2609 Record.push_back(MI->isC99Varargs());
2610 Record.push_back(MI->isGNUVarargs());
2611 Record.push_back(MI->hasCommaPasting());
Faisal Valiac506d72017-07-17 17:18:43 +00002612 Record.push_back(MI->getNumParams());
2613 for (const IdentifierInfo *Param : MI->params())
2614 AddIdentifierRef(Param, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002615 }
2616
2617 // If we have a detailed preprocessing record, record the macro definition
2618 // ID that corresponds to this macro.
2619 if (PPRec)
2620 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2621
2622 Stream.EmitRecord(Code, Record);
2623 Record.clear();
2624
2625 // Emit the tokens array.
2626 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2627 // Note that we know that the preprocessor does not have any annotation
2628 // tokens in it because they are created by the parser, and thus can't
2629 // be in a macro definition.
2630 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002631 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002632 Stream.EmitRecord(PP_TOKEN, Record);
2633 Record.clear();
2634 }
2635 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002636 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002637
Douglas Gregor92a96f52011-02-08 21:58:10 +00002638 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002639
2640 // Write the offsets table for macro IDs.
2641 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002642
David Blaikieb44f0bf2017-01-04 22:36:43 +00002643 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002644 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2647 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2648
David Blaikieb44f0bf2017-01-04 22:36:43 +00002649 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002650 {
2651 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2652 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2653 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2654 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002655}
2656
2657void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002658 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002659 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002660
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002661 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002662
Douglas Gregor92a96f52011-02-08 21:58:10 +00002663 // Enter the preprocessor block.
2664 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002665
Douglas Gregoraae92242010-03-19 21:51:54 +00002666 // If the preprocessor has a preprocessing record, emit it.
2667 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002668 using namespace llvm;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002669
2670 // Set up the abbreviation for
Douglas Gregor92a96f52011-02-08 21:58:10 +00002671 unsigned InclusionAbbrev = 0;
2672 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002673 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002674 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002678 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002679 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002680 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002681 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002682
2683 unsigned FirstPreprocessorEntityID
2684 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002685 + NUM_PREDEF_PP_ENTITY_IDS;
2686 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002687 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002688 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2689 EEnd = PPRec.local_end();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002690 E != EEnd;
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002691 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002692 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002693
Richard Smith66a81862015-05-04 02:25:31 +00002694 PreprocessedEntityOffsets.push_back(
2695 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002696
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002697 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002698 // Record this macro definition's ID.
2699 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002700
Douglas Gregor92a96f52011-02-08 21:58:10 +00002701 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002702 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2703 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002704 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002705
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002706 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002707 Record.push_back(ME->isBuiltinMacro());
2708 if (ME->isBuiltinMacro())
2709 AddIdentifierRef(ME->getName(), Record);
2710 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002711 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002712 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002713 continue;
2714 }
2715
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002716 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002717 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002718 Record.push_back(ID->getFileName().size());
2719 Record.push_back(ID->wasInQuotes());
2720 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002721 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002722 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002723 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002724 // Check that the FileEntry is not null because it was not resolved and
2725 // we create a PCH even with compiler errors.
2726 if (ID->getFile())
2727 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002728 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002729 continue;
2730 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002731
Douglas Gregor92a96f52011-02-08 21:58:10 +00002732 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2733 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002734 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002735
Douglas Gregoraae92242010-03-19 21:51:54 +00002736 // Write the offsets table for the preprocessing record.
2737 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002738 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2739
Douglas Gregoraae92242010-03-19 21:51:54 +00002740 // Write the offsets table for identifier IDs.
2741 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002742
David Blaikieb44f0bf2017-01-04 22:36:43 +00002743 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002744 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002747 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002748
Mehdi Amini57a41912015-09-10 01:46:39 +00002749 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2750 FirstPreprocessorEntityID -
2751 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002752 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002753 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002754 }
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00002755
2756 // Write the skipped region table for the preprocessing record.
2757 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2758 if (SkippedRanges.size() > 0) {
2759 std::vector<PPSkippedRange> SerializedSkippedRanges;
2760 SerializedSkippedRanges.reserve(SkippedRanges.size());
2761 for (auto const& Range : SkippedRanges)
2762 SerializedSkippedRanges.emplace_back(Range);
2763
2764 using namespace llvm;
2765 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2766 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2768 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2769
2770 Record.clear();
2771 Record.push_back(PPD_SKIPPED_RANGES);
2772 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2773 bytes(SerializedSkippedRanges));
2774 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002775}
2776
Richard Smith386bb072015-08-18 23:42:23 +00002777unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002778 if (!Mod)
2779 return 0;
2780
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002781 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2782 if (Known != SubmoduleIDs.end())
2783 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002784
Richard Smithdc1f0422016-07-20 19:10:16 +00002785 auto *Top = Mod->getTopLevelModule();
2786 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002787 (getLangOpts().CompilingPCH ||
2788 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002789 return 0;
2790
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002791 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2792}
2793
Richard Smith386bb072015-08-18 23:42:23 +00002794unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2795 // FIXME: This can easily happen, if we have a reference to a submodule that
2796 // did not result in us loading a module file for that submodule. For
2797 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2798 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2799 assert((ID || !Mod) &&
2800 "asked for module ID for non-local, non-imported module");
2801 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002802}
2803
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002804/// Compute the number of modules within the given tree (including the
Douglas Gregor253eefe2011-12-01 00:59:36 +00002805/// given module).
2806static unsigned getNumberOfModules(Module *Mod) {
2807 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002808 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002809 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002810 ChildModules += getNumberOfModules(*Sub);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002811
Douglas Gregor253eefe2011-12-01 00:59:36 +00002812 return ChildModules + 1;
2813}
2814
Douglas Gregorde3ef502011-11-30 23:21:26 +00002815void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002816 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002817 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002818
Douglas Gregor69021972011-11-30 17:33:56 +00002819 // Write the abbreviations needed for the submodules block.
2820 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002821
David Blaikieb44f0bf2017-01-04 22:36:43 +00002822 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002823 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002824 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
Richard Smithdd8b5332017-09-04 05:37:53 +00002826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
Douglas Gregor69021972011-11-30 17:33:56 +00002827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002834 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
Douglas Gregor69021972011-11-30 17:33:56 +00002836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002837 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002838
David Blaikieb44f0bf2017-01-04 22:36:43 +00002839 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002840 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002842 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002843
David Blaikieb44f0bf2017-01-04 22:36:43 +00002844 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002845 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002847 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002848
David Blaikieb44f0bf2017-01-04 22:36:43 +00002849 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002850 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002852 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002853
David Blaikieb44f0bf2017-01-04 22:36:43 +00002854 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002855 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002857 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002858
David Blaikieb44f0bf2017-01-04 22:36:43 +00002859 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002860 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002861 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2862 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002863 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002864
David Blaikieb44f0bf2017-01-04 22:36:43 +00002865 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002866 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002868 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002869
David Blaikieb44f0bf2017-01-04 22:36:43 +00002870 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002871 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002873 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002874
David Blaikieb44f0bf2017-01-04 22:36:43 +00002875 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002876 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002878 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002879
David Blaikieb44f0bf2017-01-04 22:36:43 +00002880 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002881 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002883 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002884
David Blaikieb44f0bf2017-01-04 22:36:43 +00002885 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002886 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002889 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002890
David Blaikieb44f0bf2017-01-04 22:36:43 +00002891 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002892 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002894 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002895
David Blaikieb44f0bf2017-01-04 22:36:43 +00002896 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002897 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002900 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002901
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002902 Abbrev = std::make_shared<BitCodeAbbrev>();
2903 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2905 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2906
Douglas Gregor253eefe2011-12-01 00:59:36 +00002907 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002908 RecordData::value_type Record[] = {
2909 getNumberOfModules(WritingModule),
Richard Smithdd8b5332017-09-04 05:37:53 +00002910 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002911 Stream.EmitRecord(SUBMODULE_METADATA, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002912
Douglas Gregor69021972011-11-30 17:33:56 +00002913 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002914 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002915 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002916 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002917 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002918 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002919 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002920
2921 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002922 if (Mod->Parent) {
2923 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002924 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002925 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002926
2927 // Emit the definition of the block.
2928 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002929 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2930 ID,
2931 ParentID,
Simon Pilgrimf331a662017-09-04 10:54:39 +00002932 (RecordData::value_type)Mod->Kind,
David Blaikie9ffe5a32017-01-30 05:00:26 +00002933 Mod->IsFramework,
2934 Mod->IsExplicit,
2935 Mod->IsSystem,
2936 Mod->IsExternC,
2937 Mod->InferSubmodules,
2938 Mod->InferExplicitSubmodules,
2939 Mod->InferExportWildcard,
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002940 Mod->ConfigMacrosExhaustive,
2941 Mod->ModuleMapIsPrivate};
Mehdi Amini57a41912015-09-10 01:46:39 +00002942 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2943 }
2944
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002945 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002946 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002947 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002948 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002949 }
2950
Douglas Gregor69021972011-11-30 17:33:56 +00002951 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002952 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002953 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002954 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2955 UmbrellaHeader.NameAsWritten);
2956 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002957 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2958 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002959 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002960 }
Richard Smith306d8922014-10-22 23:50:56 +00002961
Douglas Gregor69021972011-11-30 17:33:56 +00002962 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002963 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002964 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002965 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002966 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002967 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002968 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2969 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2970 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002971 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002972 Module::HK_PrivateTextual},
2973 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002974 };
2975 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002976 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002977 for (auto &H : Mod->Headers[HL.HeaderKind])
2978 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2979 }
2980
2981 // Emit the top headers.
2982 {
2983 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002984 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002985 for (auto *H : TopHeaders)
2986 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002987 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002988
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002989 // Emit the imports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002990 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002991 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002992 for (auto *I : Mod->Imports)
2993 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002994 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2995 }
2996
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002997 // Emit the exports.
Douglas Gregor24bb9232011-12-02 18:58:38 +00002998 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002999 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00003000 for (const auto &E : Mod->Exports) {
3001 // FIXME: This may fail; we don't require that all exported modules
3002 // are local or imported.
3003 Record.push_back(getSubmoduleID(E.getPointer()));
3004 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00003005 }
3006 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
3007 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003008
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003009 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
3010 // Might be unnecessary as use declarations are only used to build the
3011 // module itself.
3012
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003013 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00003014 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003015 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
3016 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00003017 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003018 }
3019
Douglas Gregorfb912652013-03-20 21:10:35 +00003020 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00003021 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00003022 // FIXME: This may fail; we don't require that all conflicting modules
3023 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00003024 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
3025 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00003026 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00003027 }
3028
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003029 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00003030 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003031 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00003032 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003033 }
3034
Richard Smithdc1f0422016-07-20 19:10:16 +00003035 // Emit the initializers, if any.
3036 RecordData Inits;
3037 for (Decl *D : Context->getModuleInitializers(Mod))
3038 Inits.push_back(GetDeclRef(D));
3039 if (!Inits.empty())
3040 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3041
Douglas Gregorf0b11de2017-09-14 23:38:44 +00003042 // Emit the name of the re-exported module, if any.
3043 if (!Mod->ExportAsModule.empty()) {
3044 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3045 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3046 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003047
Douglas Gregor69021972011-11-30 17:33:56 +00003048 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00003049 for (auto *M : Mod->submodules())
3050 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00003051 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003052
Douglas Gregor69021972011-11-30 17:33:56 +00003053 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00003054
Richard Smith23d8d032015-05-14 00:45:20 +00003055 assert((NextSubmoduleID - FirstSubmoduleID ==
3056 getNumberOfModules(WritingModule)) &&
3057 "Wrong # of submodules; found a reference to a non-local, "
3058 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00003059}
3060
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00003061void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3062 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003063 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3064 DiagStateIDMap;
3065 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003066 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003067
Richard Smithe37391c2017-05-03 00:28:49 +00003068 auto EncodeDiagStateFlags =
3069 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3070 unsigned Result = (unsigned)DS->ExtBehavior;
3071 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00003072 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3073 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3074 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00003075 Result = (Result << 1) | Val;
3076 return Result;
3077 };
3078
3079 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3080 Record.push_back(Flags);
3081
Richard Smithd230de22017-01-26 01:01:01 +00003082 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3083 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00003084 // Ensure that the diagnostic state wasn't modified since it was created.
3085 // We will not correctly round-trip this information otherwise.
3086 assert(Flags == EncodeDiagStateFlags(State) &&
3087 "diag state flags vary in single AST file");
3088
Richard Smithd230de22017-01-26 01:01:01 +00003089 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003090 Record.push_back(DiagStateID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003091
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003092 if (DiagStateID == 0) {
3093 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003094
3095 // Add a placeholder for the number of mappings.
3096 auto SizeIdx = Record.size();
3097 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00003098 for (const auto &I : *State) {
3099 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003100 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00003101 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003102 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003103 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003104 // Update the placeholder.
3105 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00003106 }
3107 };
3108
3109 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00003110
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003111 // Reserve a spot for the number of locations with state transitions.
3112 auto NumLocationsIdx = Record.size();
3113 Record.emplace_back();
3114
3115 // Emit the state transitions.
3116 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00003117 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3118 if (!FileIDAndFile.first.isValid() ||
3119 !FileIDAndFile.second.HasLocalTransitions)
3120 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003121 ++NumLocations;
Richard Smith6c2b5a82018-02-09 01:15:13 +00003122
3123 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3124 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3125 AddSourceLocation(Loc, Record);
3126
Richard Smithd230de22017-01-26 01:01:01 +00003127 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3128 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3129 Record.push_back(StatePoint.Offset);
3130 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003131 }
3132 }
3133
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003134 // Backpatch the number of locations.
3135 Record[NumLocationsIdx] = NumLocations;
3136
3137 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00003138 //
3139 // This also protects against a hypothetical corner case with simulating
3140 // -Werror settings for implicit modules in the ASTReader, where reading
3141 // CurDiagState out of context could change whether warning pragmas are
3142 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003143 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3144 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3145
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00003146 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003147}
3148
Douglas Gregorc5046832009-04-27 18:38:38 +00003149//===----------------------------------------------------------------------===//
3150// Type Serialization
3151//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00003152
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003153/// Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003154void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00003155 TypeIdx &IdxRef = TypeIdxs[T];
3156 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3157 IdxRef = TypeIdx(NextTypeID++);
3158 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00003159
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003160 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00003161
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003162 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00003163
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003164 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003165 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00003166 W.Visit(T);
3167 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00003168
Richard Smith290d8012016-04-06 17:06:00 +00003169 // Record the offset for this type.
3170 unsigned Index = Idx.getIndex() - FirstTypeID;
3171 if (TypeOffsets.size() == Index)
3172 TypeOffsets.push_back(Offset);
3173 else if (TypeOffsets.size() < Index) {
3174 TypeOffsets.resize(Index + 1);
3175 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00003176 } else {
Richard Smith290d8012016-04-06 17:06:00 +00003177 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003178 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003179}
3180
Douglas Gregorc5046832009-04-27 18:38:38 +00003181//===----------------------------------------------------------------------===//
3182// Declaration Serialization
3183//===----------------------------------------------------------------------===//
3184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003185/// Write the block containing all of the declaration IDs
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003186/// lexically declared within the given DeclContext.
3187///
3188/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003189/// bitstream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003190uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003191 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003192 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003193 return 0;
3194
Douglas Gregor8f45df52009-04-16 22:23:12 +00003195 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003196 SmallVector<uint32_t, 128> KindDeclPairs;
3197 for (const auto *D : DC->decls()) {
3198 KindDeclPairs.push_back(D->getKind());
3199 KindDeclPairs.push_back(GetDeclRef(D));
3200 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003201
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003202 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00003203 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00003204 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3205 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003206 return Offset;
3207}
3208
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003209void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003210 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003211
3212 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003213 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003214 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00003216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003218 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003219 {
3220 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3221 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3222 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3223 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003224
3225 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003226 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00003229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003231 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003232 {
3233 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3234 FirstDeclID - NUM_PREDEF_DECL_IDS};
3235 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3236 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003237}
3238
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003239void ASTWriter::WriteFileDeclIDsMap() {
3240 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003241
Chandler Carruthb306d732015-03-27 00:31:20 +00003242 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3243 FileDeclIDs.begin(), FileDeclIDs.end());
Fangrui Song55fab262018-09-26 22:16:28 +00003244 llvm::sort(SortedFileDeclIDs, llvm::less_first());
Chandler Carruthb306d732015-03-27 00:31:20 +00003245
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003246 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003247 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3248 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3249 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3250 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3251 for (auto &LocDeclEntry : Info.DeclIDs)
3252 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003253 }
3254
David Blaikieb44f0bf2017-01-04 22:36:43 +00003255 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003256 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003259 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003260 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3261 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003262 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003263}
3264
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003265void ASTWriter::WriteComments() {
3266 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Ilya Biryukov45643102018-07-09 11:33:23 +00003267 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3268 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3269 return;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003270 RecordData Record;
Jan Korousf31d8df2019-08-13 18:11:44 +00003271 for (const auto &FO : Context->Comments.OrderedComments) {
3272 for (const auto &OC : FO.second) {
3273 const RawComment *I = OC.second;
3274 Record.clear();
3275 AddSourceRange(I->getSourceRange(), Record);
3276 Record.push_back(I->getKind());
3277 Record.push_back(I->isTrailingComment());
3278 Record.push_back(I->isAlmostTrailingComment());
3279 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3280 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003281 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003282}
3283
Douglas Gregorc5046832009-04-27 18:38:38 +00003284//===----------------------------------------------------------------------===//
3285// Global Method Pool and Selector Serialization
3286//===----------------------------------------------------------------------===//
3287
Douglas Gregore84a9da2009-04-20 20:36:09 +00003288namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003289
Douglas Gregorc78d3462009-04-24 21:10:55 +00003290// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003291class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003292 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003293
3294public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003295 using key_type = Selector;
3296 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003297
Sebastian Redl834bb972010-08-04 17:20:04 +00003298 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003299 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003300 ObjCMethodList Instance, Factory;
3301 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003302 using data_type_ref = const data_type &;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003303
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003304 using hash_value_type = unsigned;
3305 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003306
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003307 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
Mike Stump11289f42009-09-09 15:08:12 +00003308
Justin Bogner25463f12014-04-18 20:27:24 +00003309 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003310 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003311 }
Mike Stump11289f42009-09-09 15:08:12 +00003312
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003313 std::pair<unsigned, unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003314 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003315 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003316 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003317
Peter Collingbournee3f65292018-05-18 19:46:24 +00003318 endian::Writer LE(Out, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003319 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003320 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003321 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3322 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003323 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003324 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003325 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003326 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003327 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003328 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003329 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003330 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003331 return std::make_pair(KeyLen, DataLen);
3332 }
Mike Stump11289f42009-09-09 15:08:12 +00003333
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003334 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003335 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003336
Peter Collingbournee3f65292018-05-18 19:46:24 +00003337 endian::Writer LE(Out, little);
Mike Stump11289f42009-09-09 15:08:12 +00003338 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003339 assert((Start >> 32) == 0 && "Selector key offset too large");
3340 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003341 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003342 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003343 if (N == 0)
3344 N = 1;
3345 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003346 LE.write<uint32_t>(
3347 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003348 }
Mike Stump11289f42009-09-09 15:08:12 +00003349
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003350 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003351 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003352 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003353
Peter Collingbournee3f65292018-05-18 19:46:24 +00003354 endian::Writer LE(Out, little);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003355 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003356 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003357 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003358 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003359 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003360 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003361 ++NumInstanceMethods;
3362
3363 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003364 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003365 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003366 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003367 ++NumFactoryMethods;
3368
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003369 unsigned InstanceBits = Methods.Instance.getBits();
3370 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003371 unsigned InstanceHasMoreThanOneDeclBit =
3372 Methods.Instance.hasMoreThanOneDecl();
3373 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3374 (InstanceHasMoreThanOneDeclBit << 2) |
3375 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003376 unsigned FactoryBits = Methods.Factory.getBits();
3377 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003378 unsigned FactoryHasMoreThanOneDeclBit =
3379 Methods.Factory.hasMoreThanOneDecl();
3380 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3381 (FactoryHasMoreThanOneDeclBit << 2) |
3382 FactoryBits;
3383 LE.write<uint16_t>(FullInstanceBits);
3384 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003385 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003386 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003387 if (Method->getMethod())
3388 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003389 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003390 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003391 if (Method->getMethod())
3392 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003393
3394 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003395 }
3396};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003397
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003398} // namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +00003399
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003400/// Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003401///
3402/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003403/// in an on-disk hash table indexed by the selector. The hash table also
3404/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003405void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003406 using namespace llvm;
3407
Sebastian Redla19a67f2010-08-03 21:58:15 +00003408 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003409 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003410 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003411 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003412 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003413 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003414 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003415 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003416
Sebastian Redla19a67f2010-08-03 21:58:15 +00003417 // Create the on-disk hash table representation. We walk through every
3418 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003419 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003420 for (auto &SelectorAndID : SelectorIDs) {
3421 Selector S = SelectorAndID.first;
3422 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003423 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003424 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003425 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003426 ObjCMethodList(),
3427 ObjCMethodList()
3428 };
3429 if (F != SemaRef.MethodPool.end()) {
3430 Data.Instance = F->second.first;
3431 Data.Factory = F->second.second;
3432 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003433 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003434 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003435 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003436 // Selector already exists. Did it change?
3437 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003438 for (ObjCMethodList *M = &Data.Instance;
3439 !changed && M && M->getMethod(); M = M->getNext()) {
3440 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003441 changed = true;
3442 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003443 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003444 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003445 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003446 changed = true;
3447 }
3448 if (!changed)
3449 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003450 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003451 // A new method pool entry.
3452 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003453 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003454 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003455 }
3456
Douglas Gregorc78d3462009-04-24 21:10:55 +00003457 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003458 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003459 uint32_t BucketOffset;
3460 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003461 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003462
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003463 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003464 llvm::raw_svector_ostream Out(MethodPool);
3465 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003466 endian::write<uint32_t>(Out, 0, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003467 BucketOffset = Generator.Emit(Out, Trait);
3468 }
3469
3470 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003471 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003472 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003473 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003475 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003476 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003477
Douglas Gregor95c13f52009-04-25 17:48:32 +00003478 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003479 {
3480 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3481 NumTableEntries};
3482 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3483 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003484
3485 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003486 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003487 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003489 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003491 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003492
3493 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003494 {
3495 RecordData::value_type Record[] = {
3496 SELECTOR_OFFSETS, SelectorOffsets.size(),
3497 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3498 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3499 bytes(SelectorOffsets));
3500 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003501 }
3502}
3503
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003504/// Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003505void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003506 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003507
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003508 if (SemaRef.ReferencedSelectors.empty())
3509 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003510
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003511 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003512 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003513
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003514 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003515 // very tricky to fix, and given that @selector shouldn't really appear in
3516 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003517 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3518 Selector Sel = SelectorAndLocation.first;
3519 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003520 Writer.AddSelectorRef(Sel);
3521 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003522 }
Richard Smithe64e7452016-04-18 21:54:58 +00003523 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003524}
3525
Douglas Gregorc5046832009-04-27 18:38:38 +00003526//===----------------------------------------------------------------------===//
3527// Identifier Table Serialization
3528//===----------------------------------------------------------------------===//
3529
Richard Smithb3761912015-02-05 23:08:52 +00003530/// Determine the declaration that should be put into the name lookup table to
3531/// represent the given declaration in this module. This is usually D itself,
3532/// but if D was imported and merged into a local declaration, we want the most
3533/// recent local declaration instead. The chosen declaration will be the most
3534/// recent declaration in any module that imports this one.
3535static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3536 NamedDecl *D) {
3537 if (!LangOpts.Modules || !D->isFromASTFile())
3538 return D;
3539
3540 if (Decl *Redecl = D->getPreviousDecl()) {
3541 // For Redeclarable decls, a prior declaration might be local.
3542 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003543 // If we find a local decl, we're done.
3544 if (!Redecl->isFromASTFile()) {
3545 // Exception: in very rare cases (for injected-class-names), not all
3546 // redeclarations are in the same semantic context. Skip ones in a
3547 // different context. They don't go in this lookup table at all.
3548 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3549 D->getDeclContext()->getRedeclContext()))
3550 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003551 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003552 }
3553
Richard Smithfe620d22015-03-05 23:24:12 +00003554 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003555 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003556 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003557 break;
3558 }
3559 } else if (Decl *First = D->getCanonicalDecl()) {
3560 // For Mergeable decls, the first decl might be local.
3561 if (!First->isFromASTFile())
3562 return cast<NamedDecl>(First);
3563 }
3564
3565 // All declarations are imported. Our most recent declaration will also be
3566 // the most recent one in anyone who imports us.
3567 return D;
3568}
3569
Douglas Gregorc78d3462009-04-24 21:10:55 +00003570namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003571
Richard Smithcf97cf62015-04-19 01:34:23 +00003572class ASTIdentifierTableTrait {
3573 ASTWriter &Writer;
3574 Preprocessor &PP;
3575 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003576 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003577 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003578 ASTWriter::RecordData *InterestingIdentifierOffsets;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003579
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003580 /// Determines whether this is an "interesting" identifier that needs a
Richard Smithcf97cf62015-04-19 01:34:23 +00003581 /// full IdentifierInfo structure written into the hash table. Notably, this
3582 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3583 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003584 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003585 if (MacroOffset ||
3586 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003587 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003588 II->hasRevertedTokenIDToIdentifier() ||
Bruno Ricci366ba732018-09-21 12:53:22 +00003589 (NeedDecls && II->getFETokenInfo()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003590 return true;
3591
3592 return false;
3593 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003594
Douglas Gregore84a9da2009-04-20 20:36:09 +00003595public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003596 using key_type = IdentifierInfo *;
3597 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003598
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003599 using data_type = IdentID;
3600 using data_type_ref = data_type;
Mike Stump11289f42009-09-09 15:08:12 +00003601
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003602 using hash_value_type = unsigned;
3603 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003604
Richard Smithd7329392015-04-21 21:46:32 +00003605 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003606 IdentifierResolver &IdResolver, bool IsModule,
3607 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003608 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003609 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3610 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003611
Richard Smithd79514e2016-02-05 19:03:40 +00003612 bool needDecls() const { return NeedDecls; }
3613
Justin Bogner25463f12014-04-18 20:27:24 +00003614 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003615 return llvm::djbHash(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003616 }
Mike Stump11289f42009-09-09 15:08:12 +00003617
Richard Smith33e0f7e2015-07-22 02:08:40 +00003618 bool isInterestingIdentifier(const IdentifierInfo *II) {
3619 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3620 return isInterestingIdentifier(II, MacroOffset);
3621 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003622
Richard Smith9c254182015-07-19 21:41:12 +00003623 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3624 return isInterestingIdentifier(II, 0);
3625 }
3626
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003627 std::pair<unsigned, unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003628 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003629 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003630 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003631 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3632 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003633 DataLen += 2; // 2 bytes for builtin ID
3634 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003635 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003636 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003637
Richard Smitha534a312015-07-21 23:54:07 +00003638 if (NeedDecls) {
3639 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3640 DEnd = IdResolver.end();
3641 D != DEnd; ++D)
3642 DataLen += 4;
3643 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003644 }
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003645
Justin Bognere1c147c2014-03-28 22:03:19 +00003646 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003647
Peter Collingbournee3f65292018-05-18 19:46:24 +00003648 endian::Writer LE(Out, little);
Justin Bognere1c147c2014-03-28 22:03:19 +00003649
Richard Smithdf8a8312015-03-13 04:05:01 +00003650 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003651 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003652 // We emit the key length after the data length so that every
3653 // string is preceded by a 16-bit length. This matches the PTH
3654 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003655 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003656 return std::make_pair(KeyLen, DataLen);
3657 }
Mike Stump11289f42009-09-09 15:08:12 +00003658
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003659 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003660 unsigned KeyLen) {
3661 // Record the location of the key data. This is used when generating
3662 // the mapping from persistent IDs to strings.
3663 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003664
3665 // Emit the offset of the key/data length information to the interesting
3666 // identifiers table if necessary.
3667 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3668 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3669
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003670 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003671 }
Mike Stump11289f42009-09-09 15:08:12 +00003672
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003673 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003674 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003675 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003676
Peter Collingbournee3f65292018-05-18 19:46:24 +00003677 endian::Writer LE(Out, little);
Richard Smithcf97cf62015-04-19 01:34:23 +00003678
Richard Smithd7329392015-04-21 21:46:32 +00003679 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3680 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003681 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003682 return;
3683 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003684
Justin Bognere1c147c2014-03-28 22:03:19 +00003685 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003686 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3687 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003688 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003689 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003690 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003691 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003692 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3693 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003694 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003695 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003696 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003697 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003698
Richard Smithd7329392015-04-21 21:46:32 +00003699 if (HadMacroDefinition)
3700 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003701
Richard Smitha534a312015-07-21 23:54:07 +00003702 if (NeedDecls) {
3703 // Emit the declaration IDs in reverse order, because the
3704 // IdentifierResolver provides the declarations as they would be
3705 // visible (e.g., the function "stat" would come before the struct
3706 // "stat"), but the ASTReader adds declarations to the end of the list
3707 // (so we need to see the struct "stat" before the function "stat").
3708 // Only emit declarations that aren't from a chained PCH, though.
3709 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3710 IdResolver.end());
3711 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3712 DEnd = Decls.rend();
3713 D != DEnd; ++D)
3714 LE.write<uint32_t>(
3715 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3716 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003717 }
3718};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003719
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003720} // namespace
Douglas Gregore84a9da2009-04-20 20:36:09 +00003721
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003722/// Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003723///
3724/// The identifier table consists of a blob containing string data
3725/// (the actual identifiers themselves) and a separate "offsets" index
3726/// that maps identifier IDs to locations within the blob.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003727void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003728 IdentifierResolver &IdResolver,
3729 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003730 using namespace llvm;
3731
Richard Smith33e0f7e2015-07-22 02:08:40 +00003732 RecordData InterestingIdents;
3733
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003734 // Create and write out the blob that contains the identifier
3735 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003736 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003737 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003738 ASTIdentifierTableTrait Trait(
3739 *this, PP, IdResolver, IsModule,
3740 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003741
Douglas Gregore6648fb2009-04-28 20:33:11 +00003742 // Look for any identifiers that were named while processing the
3743 // headers, but are otherwise not needed. We add these to the hash
3744 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003745 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003746 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003747 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003748 for (const auto &ID : PP.getIdentifierTable())
3749 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003750 // Sort the identifiers lexicographically before getting them references so
3751 // that their order is stable.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00003752 llvm::sort(IIs, llvm::deref<std::less<>>());
Chandler Carruth8440c982015-03-26 23:54:15 +00003753 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003754 if (Trait.isInterestingNonMacroIdentifier(II))
3755 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003756
Sebastian Redlff4a2952010-07-23 23:49:55 +00003757 // Create the on-disk hash table representation. We only store offsets
3758 // for identifiers that appear here for the first time.
3759 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003760 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003761 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003762 IdentID ID = IdentIDPair.second;
3763 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003764 // Write out identifiers if either the ID is local or the identifier has
3765 // changed since it was loaded.
3766 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3767 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003768 (Trait.needDecls() &&
3769 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003770 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003771 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003772
Douglas Gregore84a9da2009-04-20 20:36:09 +00003773 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003774 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003775 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003776 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003777 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003778
Douglas Gregore84a9da2009-04-20 20:36:09 +00003779 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003780 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003781 endian::write<uint32_t>(Out, 0, little);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003782 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003783 }
3784
3785 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003786 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003787 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003790 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003791
3792 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003793 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003794 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003795 }
3796
3797 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003798 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003799 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003801 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003802 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003803 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003804
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003805#ifndef NDEBUG
3806 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3807 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3808#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003809
3810 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3811 IdentifierOffsets.size(),
3812 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003813 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003814 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003815
3816 // In C++, write the list of interesting identifiers (those that are
3817 // defined as macros, poisoned, or similar unusual things).
3818 if (!InterestingIdents.empty())
3819 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003820}
3821
Douglas Gregorc5046832009-04-27 18:38:38 +00003822//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003823// DeclContext's Name Lookup Table Serialization
3824//===----------------------------------------------------------------------===//
3825
3826namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003827
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003828// Trait used for the on-disk hash table used in the method pool.
3829class ASTDeclContextNameLookupTrait {
3830 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003831 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003832
3833public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003834 using key_type = DeclarationNameKey;
3835 using key_type_ref = key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003836
Richard Smithd88a7f12015-09-01 20:35:42 +00003837 /// A start and end index into DeclIDs, representing a sequence of decls.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003838 using data_type = std::pair<unsigned, unsigned>;
3839 using data_type_ref = const data_type &;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003840
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003841 using hash_value_type = unsigned;
3842 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003843
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003844 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003845
Richard Smithd88a7f12015-09-01 20:35:42 +00003846 template<typename Coll>
3847 data_type getData(const Coll &Decls) {
3848 unsigned Start = DeclIDs.size();
3849 for (NamedDecl *D : Decls) {
3850 DeclIDs.push_back(
3851 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3852 }
3853 return std::make_pair(Start, DeclIDs.size());
3854 }
3855
3856 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3857 unsigned Start = DeclIDs.size();
3858 for (auto ID : FromReader)
3859 DeclIDs.push_back(ID);
3860 return std::make_pair(Start, DeclIDs.size());
3861 }
3862
3863 static bool EqualKey(key_type_ref a, key_type_ref b) {
3864 return a == b;
3865 }
3866
Richard Smitha06c7e62015-08-26 23:55:49 +00003867 hash_value_type ComputeHash(DeclarationNameKey Name) {
3868 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003869 }
3870
Richard Smithd88a7f12015-09-01 20:35:42 +00003871 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3872 assert(Writer.hasChain() &&
3873 "have reference to loaded module file but no chain?");
3874
3875 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003876
Peter Collingbournee3f65292018-05-18 19:46:24 +00003877 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
Richard Smithd88a7f12015-09-01 20:35:42 +00003878 }
3879
Richard Smitha06c7e62015-08-26 23:55:49 +00003880 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3881 DeclarationNameKey Name,
3882 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003883 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003884
Peter Collingbournee3f65292018-05-18 19:46:24 +00003885 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003886 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003887 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003888 case DeclarationName::Identifier:
3889 case DeclarationName::ObjCZeroArgSelector:
3890 case DeclarationName::ObjCOneArgSelector:
3891 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003892 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003893 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003894 KeyLen += 4;
3895 break;
3896 case DeclarationName::CXXOperatorName:
3897 KeyLen += 1;
3898 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003899 case DeclarationName::CXXConstructorName:
3900 case DeclarationName::CXXDestructorName:
3901 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003902 case DeclarationName::CXXUsingDirective:
3903 break;
3904 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003905 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003906
Richard Smithf02662d2015-07-30 03:17:16 +00003907 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003908 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3909 assert(uint16_t(DataLen) == DataLen &&
3910 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003911 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003912
3913 return std::make_pair(KeyLen, DataLen);
3914 }
3915
Richard Smitha06c7e62015-08-26 23:55:49 +00003916 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003917 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003918
Peter Collingbournee3f65292018-05-18 19:46:24 +00003919 endian::Writer LE(Out, little);
Richard Smitha06c7e62015-08-26 23:55:49 +00003920 LE.write<uint8_t>(Name.getKind());
3921 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003922 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003923 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003924 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003925 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003926 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003927 case DeclarationName::ObjCZeroArgSelector:
3928 case DeclarationName::ObjCOneArgSelector:
3929 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003930 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003931 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003932 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003933 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003934 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003935 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003936 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003937 case DeclarationName::CXXConstructorName:
3938 case DeclarationName::CXXDestructorName:
3939 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003940 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003941 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003942 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003943
3944 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003945 }
3946
Richard Smitha06c7e62015-08-26 23:55:49 +00003947 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3948 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003949 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003950
Peter Collingbournee3f65292018-05-18 19:46:24 +00003951 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003952 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003953 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3954 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003955 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3956 }
3957};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003958
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003959} // namespace
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003960
Chandler Carruthe972c362015-03-26 03:11:40 +00003961bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3962 DeclContext *DC) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003963 return Result.hasExternalDecls() &&
3964 DC->hasNeedToReconcileExternalVisibleStorage();
Chandler Carruthe972c362015-03-26 03:11:40 +00003965}
3966
3967bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3968 DeclContext *DC) {
3969 for (auto *D : Result.getLookupResult())
3970 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3971 return false;
3972
3973 return true;
3974}
3975
Richard Smithd88a7f12015-09-01 20:35:42 +00003976void
Chandler Carruthe972c362015-03-26 03:11:40 +00003977ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003978 llvm::SmallVectorImpl<char> &LookupTable) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003979 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3980 !ConstDC->hasLazyExternalLexicalLookups() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003981 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003982
Chandler Carruthe972c362015-03-26 03:11:40 +00003983 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003984 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003985 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3986
3987 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003988 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3989 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003990 ASTDeclContextNameLookupTrait Trait(*this);
3991
Chandler Carruthe972c362015-03-26 03:11:40 +00003992 // The first step is to collect the declaration names which we need to
3993 // serialize into the name lookup table, and to collect them in a stable
3994 // order.
3995 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003996
Chandler Carruthe972c362015-03-26 03:11:40 +00003997 // We also build up small sets of the constructor and conversion function
3998 // names which are visible.
3999 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004000
Chandler Carruthe972c362015-03-26 03:11:40 +00004001 for (auto &Lookup : *DC->buildLookup()) {
4002 auto &Name = Lookup.first;
4003 auto &Result = Lookup.second;
4004
Douglas Gregor7dd37e52015-11-03 01:20:54 +00004005 // If there are no local declarations in our lookup result, we
4006 // don't need to write an entry for the name at all. If we can't
4007 // write out a lookup set without performing more deserialization,
4008 // just skip this entry.
4009 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00004010 isLookupResultEntirelyExternal(Result, DC))
4011 continue;
4012
4013 // We also skip empty results. If any of the results could be external and
4014 // the currently available results are empty, then all of the results are
4015 // external and we skip it above. So the only way we get here with an empty
4016 // results is when no results could have been external *and* we have
4017 // external results.
4018 //
4019 // FIXME: While we might want to start emitting on-disk entries for negative
4020 // lookups into a decl context as an optimization, today we *have* to skip
4021 // them because there are names with empty lookup results in decl contexts
4022 // which we can't emit in any stable ordering: we lookup constructors and
4023 // conversion functions in the enclosing namespace scope creating empty
4024 // results for them. This in almost certainly a bug in Clang's name lookup,
4025 // but that is likely to be hard or impossible to fix and so we tolerate it
4026 // here by omitting lookups with empty results.
4027 if (Lookup.second.getLookupResult().empty())
4028 continue;
4029
4030 switch (Lookup.first.getNameKind()) {
4031 default:
4032 Names.push_back(Lookup.first);
4033 break;
4034
Richard Smithcd45dbc2014-04-19 03:48:30 +00004035 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004036 assert(isa<CXXRecordDecl>(DC) &&
4037 "Cannot have a constructor name outside of a class!");
4038 ConstructorNameSet.insert(Name);
4039 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004040
4041 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004042 assert(isa<CXXRecordDecl>(DC) &&
4043 "Cannot have a conversion function name outside of a class!");
4044 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00004045 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004046 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004047 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004048
Chandler Carruthe972c362015-03-26 03:11:40 +00004049 // Sort the names into a stable order.
Fangrui Song55fab262018-09-26 22:16:28 +00004050 llvm::sort(Names);
Chandler Carruthe972c362015-03-26 03:11:40 +00004051
Chandler Carruthffbf7052015-03-26 22:27:09 +00004052 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00004053 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00004054 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00004055
Chandler Carruthffbf7052015-03-26 22:27:09 +00004056 // First we try the easy case by forming the current context's constructor
4057 // name and adding that name first. This is a very useful optimization to
4058 // avoid walking the lexical declarations in many cases, and it also
4059 // handles the only case where a constructor name can come from some other
4060 // lexical context -- when that name is an implicit constructor merged from
4061 // another declaration in the redecl chain. Any non-implicit constructor or
4062 // conversion function which doesn't occur in all the lexical contexts
4063 // would be an ODR violation.
4064 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4065 Context->getCanonicalType(Context->getRecordType(D)));
4066 if (ConstructorNameSet.erase(ImplicitCtorName))
4067 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00004068
Chandler Carruthffbf7052015-03-26 22:27:09 +00004069 // If we still have constructors or conversion functions, we walk all the
4070 // names in the decl and add the constructors and conversion functions
4071 // which are visible in the order they lexically occur within the context.
4072 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4073 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4074 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4075 auto Name = ChildND->getDeclName();
4076 switch (Name.getNameKind()) {
4077 default:
4078 continue;
4079
4080 case DeclarationName::CXXConstructorName:
4081 if (ConstructorNameSet.erase(Name))
4082 Names.push_back(Name);
4083 break;
4084
4085 case DeclarationName::CXXConversionFunctionName:
4086 if (ConversionNameSet.erase(Name))
4087 Names.push_back(Name);
4088 break;
4089 }
4090
4091 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4092 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00004093 }
4094
Chandler Carruthe972c362015-03-26 03:11:40 +00004095 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4096 "constructors by walking all the "
4097 "lexical members of the context.");
4098 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4099 "conversion functions by walking all "
4100 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00004101 }
4102
Chandler Carruthe972c362015-03-26 03:11:40 +00004103 // Next we need to do a lookup with each name into this decl context to fully
4104 // populate any results from external sources. We don't actually use the
4105 // results of these lookups because we only want to use the results after all
4106 // results have been loaded and the pointers into them will be stable.
4107 for (auto &Name : Names)
4108 DC->lookup(Name);
4109
4110 // Now we need to insert the results for each name into the hash table. For
4111 // constructor names and conversion function names, we actually need to merge
4112 // all of the results for them into one list of results each and insert
4113 // those.
4114 SmallVector<NamedDecl *, 8> ConstructorDecls;
4115 SmallVector<NamedDecl *, 8> ConversionDecls;
4116
4117 // Now loop over the names, either inserting them or appending for the two
4118 // special cases.
4119 for (auto &Name : Names) {
4120 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4121
4122 switch (Name.getNameKind()) {
4123 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00004124 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004125 break;
4126
4127 case DeclarationName::CXXConstructorName:
4128 ConstructorDecls.append(Result.begin(), Result.end());
4129 break;
4130
4131 case DeclarationName::CXXConversionFunctionName:
4132 ConversionDecls.append(Result.begin(), Result.end());
4133 break;
4134 }
4135 }
4136
4137 // Handle our two special cases if we ended up having any. We arbitrarily use
4138 // the first declaration's name here because the name itself isn't part of
4139 // the key, only the kind of name is used.
4140 if (!ConstructorDecls.empty())
4141 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004142 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004143 if (!ConversionDecls.empty())
4144 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004145 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004146
Richard Smithd88a7f12015-09-01 20:35:42 +00004147 // Create the on-disk hash table. Also emit the existing imported and
4148 // merged table if there is one.
4149 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4150 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00004151}
4152
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004153/// Write the block containing all of the declaration IDs
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004154/// visible from the given DeclContext.
4155///
4156/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00004157/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004158uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4159 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00004160 // If we imported a key declaration of this namespace, write the visible
4161 // lookup results as an update record for it rather than including them
4162 // on this declaration. We will only look at key declarations on reload.
4163 if (isa<NamespaceDecl>(DC) && Chain &&
4164 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4165 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004166 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00004167 Prev = Prev->getPreviousDecl())
4168 if (!Prev->isFromASTFile())
4169 return 0;
4170
4171 // Note that we need to emit an update record for the primary context.
4172 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4173
4174 // Make sure all visible decls are written. They will be recorded later. We
4175 // do this using a side data structure so we can sort the names into
4176 // a deterministic order.
4177 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4178 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4179 LookupResults;
4180 if (Map) {
4181 LookupResults.reserve(Map->size());
4182 for (auto &Entry : *Map)
4183 LookupResults.push_back(
4184 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4185 }
4186
Fangrui Song55fab262018-09-26 22:16:28 +00004187 llvm::sort(LookupResults, llvm::less_first());
Richard Smith5fc18a92015-07-12 23:43:21 +00004188 for (auto &NameAndResult : LookupResults) {
4189 DeclarationName Name = NameAndResult.first;
4190 DeclContext::lookup_result Result = NameAndResult.second;
4191 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4192 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4193 // We have to work around a name lookup bug here where negative lookup
4194 // results for these names get cached in namespace lookup tables (these
4195 // names should never be looked up in a namespace).
4196 assert(Result.empty() && "Cannot have a constructor or conversion "
4197 "function name in a namespace!");
4198 continue;
4199 }
4200
4201 for (NamedDecl *ND : Result)
4202 if (!ND->isFromASTFile())
4203 GetDeclRef(ND);
4204 }
4205
4206 return 0;
4207 }
4208
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004209 if (DC->getPrimaryContext() != DC)
4210 return 0;
4211
Chandler Carruthe972c362015-03-26 03:11:40 +00004212 // Skip contexts which don't support name lookup.
4213 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004214 return 0;
4215
4216 // If not in C++, we perform name lookup for the translation unit via the
4217 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004218 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004219 return 0;
4220
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004221 // Serialize the contents of the mapping used for lookup. Note that,
4222 // although we have two very different code paths, the serialized
4223 // representation is the same for both cases: a declaration name,
4224 // followed by a size, followed by references to the visible
4225 // declarations that have that name.
4226 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00004227 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004228 if (!Map || Map->empty())
4229 return 0;
4230
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004231 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004232 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004233 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004234
4235 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004236 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004237 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00004238 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004239 ++NumVisibleDeclContexts;
4240 return Offset;
4241}
4242
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004243/// Write an UPDATE_VISIBLE block for the given context.
Sebastian Redla4071b42010-08-24 00:50:09 +00004244///
4245/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4246/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00004247/// (in C++), for namespaces, and for classes with forward-declared unscoped
4248/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00004249void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00004250 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00004251 if (!Map || Map->empty())
4252 return;
4253
Sebastian Redla4071b42010-08-24 00:50:09 +00004254 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004255 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004256 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004257
Richard Smith5fc18a92015-07-12 23:43:21 +00004258 // If we're updating a namespace, select a key declaration as the key for the
4259 // update record; those are the only ones that will be checked on reload.
4260 if (isa<NamespaceDecl>(DC))
4261 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4262
Sebastian Redla4071b42010-08-24 00:50:09 +00004263 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004264 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004265 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004266}
4267
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004268/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004269void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004270 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004271 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4272}
4273
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004274/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004275void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004276 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004277 return;
4278
4279 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4280 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004281 for (const auto &I:Opts.OptMap) {
4282 AddString(I.getKey(), Record);
4283 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004284 Record.push_back(V.Supported ? 1 : 0);
4285 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004286 Record.push_back(V.Avail);
4287 Record.push_back(V.Core);
4288 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004289 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4290}
4291
Yaxun Liu5b746652016-12-18 05:18:55 +00004292void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4293 if (!SemaRef.Context.getLangOpts().OpenCL)
4294 return;
4295
Bruno Ricci2751b692019-04-18 15:13:27 +00004296 // Sort the elements of the map OpenCLTypeExtMap by TypeIDs,
4297 // without copying them.
4298 const llvm::DenseMap<const Type *, std::set<std::string>> &OpenCLTypeExtMap =
4299 SemaRef.OpenCLTypeExtMap;
4300 using ElementTy = std::pair<TypeID, const std::set<std::string> *>;
4301 llvm::SmallVector<ElementTy, 8> StableOpenCLTypeExtMap;
4302 StableOpenCLTypeExtMap.reserve(OpenCLTypeExtMap.size());
4303
4304 for (const auto &I : OpenCLTypeExtMap)
4305 StableOpenCLTypeExtMap.emplace_back(
4306 getTypeID(I.first->getCanonicalTypeInternal()), &I.second);
4307
4308 auto CompareByTypeID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
4309 return E1.first < E2.first;
4310 };
4311 llvm::sort(StableOpenCLTypeExtMap, CompareByTypeID);
4312
Yaxun Liu5b746652016-12-18 05:18:55 +00004313 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00004314 for (const ElementTy &E : StableOpenCLTypeExtMap) {
4315 Record.push_back(E.first); // TypeID
4316 const std::set<std::string> *ExtSet = E.second;
4317 Record.push_back(static_cast<unsigned>(ExtSet->size()));
4318 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00004319 AddString(Ext, Record);
4320 }
Bruno Ricci2751b692019-04-18 15:13:27 +00004321
Yaxun Liu5b746652016-12-18 05:18:55 +00004322 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4323}
4324
4325void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4326 if (!SemaRef.Context.getLangOpts().OpenCL)
4327 return;
4328
Bruno Ricci2751b692019-04-18 15:13:27 +00004329 // Sort the elements of the map OpenCLDeclExtMap by DeclIDs,
4330 // without copying them.
4331 const llvm::DenseMap<const Decl *, std::set<std::string>> &OpenCLDeclExtMap =
4332 SemaRef.OpenCLDeclExtMap;
4333 using ElementTy = std::pair<DeclID, const std::set<std::string> *>;
4334 llvm::SmallVector<ElementTy, 8> StableOpenCLDeclExtMap;
4335 StableOpenCLDeclExtMap.reserve(OpenCLDeclExtMap.size());
4336
4337 for (const auto &I : OpenCLDeclExtMap)
4338 StableOpenCLDeclExtMap.emplace_back(getDeclID(I.first), &I.second);
4339
4340 auto CompareByDeclID = [](const ElementTy &E1, const ElementTy &E2) -> bool {
4341 return E1.first < E2.first;
4342 };
4343 llvm::sort(StableOpenCLDeclExtMap, CompareByDeclID);
4344
Yaxun Liu5b746652016-12-18 05:18:55 +00004345 RecordData Record;
Bruno Ricci2751b692019-04-18 15:13:27 +00004346 for (const ElementTy &E : StableOpenCLDeclExtMap) {
4347 Record.push_back(E.first); // DeclID
4348 const std::set<std::string> *ExtSet = E.second;
4349 Record.push_back(static_cast<unsigned>(ExtSet->size()));
4350 for (const std::string &Ext : *ExtSet)
Yaxun Liu5b746652016-12-18 05:18:55 +00004351 AddString(Ext, Record);
4352 }
Bruno Ricci2751b692019-04-18 15:13:27 +00004353
Yaxun Liu5b746652016-12-18 05:18:55 +00004354 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4355}
4356
Justin Lebar67a78a62016-10-08 22:15:58 +00004357void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4358 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4359 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4360 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4361 }
4362}
4363
Douglas Gregor404cdde2012-01-27 01:47:08 +00004364void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004365 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004366 RecordData Categories;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004367
Douglas Gregor404cdde2012-01-27 01:47:08 +00004368 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4369 unsigned Size = 0;
4370 unsigned StartIndex = Categories.size();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004371
Douglas Gregor404cdde2012-01-27 01:47:08 +00004372 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004373
Douglas Gregor404cdde2012-01-27 01:47:08 +00004374 // Allocate space for the size.
4375 Categories.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004376
Douglas Gregor404cdde2012-01-27 01:47:08 +00004377 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004378 for (ObjCInterfaceDecl::known_categories_iterator
4379 Cat = Class->known_categories_begin(),
4380 CatEnd = Class->known_categories_end();
4381 Cat != CatEnd; ++Cat, ++Size) {
4382 assert(getDeclID(*Cat) != 0 && "Bogus category");
4383 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004384 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004385
Douglas Gregor404cdde2012-01-27 01:47:08 +00004386 // Update the size.
4387 Categories[StartIndex] = Size;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004388
Douglas Gregor404cdde2012-01-27 01:47:08 +00004389 // Record this interface -> category map.
4390 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4391 CategoriesMap.push_back(CatInfo);
4392 }
4393
4394 // Sort the categories map by the definition ID, since the reader will be
4395 // performing binary searches on this information.
4396 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4397
4398 // Emit the categories map.
4399 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004400
David Blaikieb44f0bf2017-01-04 22:36:43 +00004401 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004402 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004405 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004406
4407 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4408 Stream.EmitRecordWithBlob(AbbrevID, Record,
4409 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004410 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004411
Douglas Gregor404cdde2012-01-27 01:47:08 +00004412 // Emit the category lists.
4413 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4414}
4415
Richard Smithe40f2ba2013-08-07 21:41:30 +00004416void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4417 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4418
4419 if (LPTMap.empty())
4420 return;
4421
4422 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004423 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004424 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004425 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004426 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004427 AddDeclRef(LPT.D, Record);
4428 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004429
Justin Lebar28f09c52016-10-10 16:26:08 +00004430 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004431 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004432 }
4433 }
4434 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4435}
4436
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004437/// Write the state of 'pragma clang optimize' at the end of the module.
Dario Domizioli13a0a382014-05-23 12:13:25 +00004438void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4439 RecordData Record;
4440 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4441 AddSourceLocation(PragmaLoc, Record);
4442 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4443}
4444
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004445/// Write the state of 'pragma ms_struct' at the end of the module.
Nico Weber779355f2016-03-02 23:22:00 +00004446void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4447 RecordData Record;
4448 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4449 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4450}
4451
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004452/// Write the state of 'pragma pointers_to_members' at the end of the
Nico Weber42932312016-03-03 00:17:35 +00004453//module.
4454void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4455 RecordData Record;
4456 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4457 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4458 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4459}
4460
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004461/// Write the state of 'pragma pack' at the end of the module.
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004462void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004463 // Don't serialize pragma pack state for modules, since it should only take
4464 // effect on a per-submodule basis.
4465 if (WritingModule)
4466 return;
4467
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004468 RecordData Record;
4469 Record.push_back(SemaRef.PackStack.CurrentValue);
4470 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4471 Record.push_back(SemaRef.PackStack.Stack.size());
4472 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4473 Record.push_back(StackEntry.Value);
4474 AddSourceLocation(StackEntry.PragmaLocation, Record);
Alex Lorenz45b40142017-07-28 14:41:21 +00004475 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004476 AddString(StackEntry.StackSlotLabel, Record);
4477 }
4478 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4479}
4480
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004481void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4482 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004483 // Enter the extension block.
4484 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4485
4486 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004487 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004488 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4489 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4490 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4491 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4492 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4493 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004494 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004495
4496 // Emit the metadata record.
4497 RecordData Record;
4498 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4499 Record.push_back(EXTENSION_METADATA);
4500 Record.push_back(Metadata.MajorVersion);
4501 Record.push_back(Metadata.MinorVersion);
4502 Record.push_back(Metadata.BlockName.size());
4503 Record.push_back(Metadata.UserInfo.size());
4504 SmallString<64> Buffer;
4505 Buffer += Metadata.BlockName;
4506 Buffer += Metadata.UserInfo;
4507 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4508
4509 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004510 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004511
4512 // Exit the extension block.
4513 Stream.ExitBlock();
4514}
4515
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004516//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004517// General Serialization Routines
4518//===----------------------------------------------------------------------===//
4519
Richard Smithe43e2b32018-08-20 21:47:29 +00004520void ASTRecordWriter::AddAttr(const Attr *A) {
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004521 auto &Record = *this;
Richard Smithe43e2b32018-08-20 21:47:29 +00004522 if (!A)
4523 return Record.push_back(0);
4524 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
Erich Keane6a24e802019-09-13 17:39:31 +00004525
4526 Record.AddIdentifierRef(A->getAttrName());
4527 Record.AddIdentifierRef(A->getScopeName());
Richard Smithe43e2b32018-08-20 21:47:29 +00004528 Record.AddSourceRange(A->getRange());
Erich Keane6a24e802019-09-13 17:39:31 +00004529 Record.AddSourceLocation(A->getScopeLoc());
4530 Record.push_back(A->getParsedKind());
4531 Record.push_back(A->getSyntax());
4532 Record.push_back(A->getAttributeSpellingListIndexRaw());
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004533
4534#include "clang/Serialization/AttrPCHWrite.inc"
Richard Smithe43e2b32018-08-20 21:47:29 +00004535}
4536
4537/// Emit the list of attributes to the specified record.
4538void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4539 push_back(Attrs.size());
4540 for (const auto *A : Attrs)
4541 AddAttr(A);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004542}
4543
John McCallf413f5e2013-05-03 00:10:13 +00004544void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4545 AddSourceLocation(Tok.getLocation(), Record);
4546 Record.push_back(Tok.getLength());
4547
4548 // FIXME: When reading literal tokens, reconstruct the literal pointer
4549 // if it is needed.
4550 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4551 // FIXME: Should translate token kind to a stable encoding.
4552 Record.push_back(Tok.getKind());
4553 // FIXME: Should translate token flags to a stable encoding.
4554 Record.push_back(Tok.getFlags());
4555}
4556
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004557void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004558 Record.push_back(Str.size());
4559 Record.insert(Record.end(), Str.begin(), Str.end());
4560}
4561
Richard Smith7ed1bc92014-12-05 22:42:13 +00004562bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004563 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004564
Richard Smith54cc3c22014-12-11 20:50:24 +00004565 bool Changed =
4566 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004567
4568 // Remove a prefix to make the path relative, if relevant.
4569 const char *PathBegin = Path.data();
4570 const char *PathPtr =
4571 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4572 if (PathPtr != PathBegin) {
4573 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4574 Changed = true;
4575 }
4576
4577 return Changed;
4578}
4579
4580void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4581 SmallString<128> FilePath(Path);
4582 PreparePathForOutput(FilePath);
4583 AddString(FilePath, Record);
4584}
4585
Mehdi Amini57a41912015-09-10 01:46:39 +00004586void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004587 StringRef Path) {
4588 SmallString<128> FilePath(Path);
4589 PreparePathForOutput(FilePath);
4590 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4591}
4592
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004593void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4594 RecordDataImpl &Record) {
4595 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004596 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004597 Record.push_back(*Minor + 1);
4598 else
4599 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004600 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004601 Record.push_back(*Subminor + 1);
4602 else
4603 Record.push_back(0);
4604}
4605
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004606/// Note that the identifier II occurs at the given offset
Douglas Gregore84a9da2009-04-20 20:36:09 +00004607/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004608void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004609 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004610 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004611 // up earlier in the chain and thus don't need an offset.
4612 if (ID >= FirstIdentID)
4613 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004614}
4615
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004616/// Note that the selector Sel occurs at the given offset
Douglas Gregor95c13f52009-04-25 17:48:32 +00004617/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004618void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004619 unsigned ID = SelectorIDs[Sel];
4620 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004621 // Don't record offsets for selectors that are also available in a different
4622 // file.
4623 if (ID < FirstSelectorID)
4624 return;
4625 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004626}
4627
David Blaikie61137e12017-01-05 18:23:18 +00004628ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004629 SmallVectorImpl<char> &Buffer,
4630 InMemoryModuleCache &ModuleCache,
David Blaikie61137e12017-01-05 18:23:18 +00004631 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4632 bool IncludeTimestamps)
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004633 : Stream(Stream), Buffer(Buffer), ModuleCache(ModuleCache),
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004634 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004635 for (const auto &Ext : Extensions) {
4636 if (auto Writer = Ext->createExtensionWriter(*this))
4637 ModuleFileExtensionWriters.push_back(std::move(Writer));
4638 }
4639}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004640
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004641ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004642 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004643}
4644
Richard Smithb3761912015-02-05 23:08:52 +00004645const LangOptions &ASTWriter::getLangOpts() const {
4646 assert(WritingAST && "can't determine lang opts when not writing AST");
4647 return Context->getLangOpts();
4648}
4649
Richard Smithe75ee0f2015-08-17 07:13:32 +00004650time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4651 return IncludeTimestamps ? E->getModificationTime() : 0;
4652}
4653
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004654ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4655 const std::string &OutputFile,
4656 Module *WritingModule, StringRef isysroot,
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004657 bool hasErrors,
4658 bool ShouldCacheASTInMemory) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004659 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004660
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004661 ASTHasCompilerErrors = hasErrors;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004662
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004663 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004664 Stream.Emit((unsigned)'C', 8);
4665 Stream.Emit((unsigned)'P', 8);
4666 Stream.Emit((unsigned)'C', 8);
4667 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004668
Chris Lattner28fa4e62009-04-26 22:26:21 +00004669 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004670
Douglas Gregoreda8e122011-08-09 15:13:55 +00004671 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004672 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004673 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004674 ASTFileSignature Signature =
4675 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004676 Context = nullptr;
4677 PP = nullptr;
4678 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004679 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004680
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004681 WritingAST = false;
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +00004682 if (ShouldCacheASTInMemory) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004683 // Construct MemoryBuffer and update buffer manager.
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004684 ModuleCache.addBuiltPCM(OutputFile,
4685 llvm::MemoryBuffer::getMemBufferCopy(
4686 StringRef(Buffer.begin(), Buffer.size())));
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004687 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004688 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004689}
4690
Douglas Gregora94a1542011-07-27 21:45:57 +00004691template<typename Vector>
4692static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4693 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004694 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4695 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004696 Writer.AddDeclRef(*I, Record);
4697 }
4698}
4699
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004700ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4701 const std::string &OutputFile,
4702 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004703 using namespace llvm;
4704
Craig Toppera13603a2014-05-22 05:54:18 +00004705 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004706
Douglas Gregorcf68c582011-12-01 22:20:10 +00004707 // Make sure that the AST reader knows to finalize itself.
4708 if (Chain)
4709 Chain->finalizeForWriting();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004710
Sebastian Redl143413f2010-07-12 22:02:52 +00004711 ASTContext &Context = SemaRef.Context;
4712 Preprocessor &PP = SemaRef.PP;
4713
Douglas Gregordab42432011-08-12 00:15:20 +00004714 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004715 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4716 if (D) {
4717 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4718 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004719 }
4720 };
4721 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4722 PREDEF_DECL_TRANSLATION_UNIT_ID);
4723 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4724 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4725 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4726 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4727 PREDEF_DECL_OBJC_PROTOCOL_ID);
4728 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4729 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4730 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4731 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4732 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004733 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004734 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4735 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004736 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004737 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4738 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004739 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4740 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004741 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4742 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004743 RegisterPredefDecl(Context.TypePackElementDecl,
4744 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004745
Chris Lattner0c797362009-09-08 18:19:27 +00004746 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004747 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004748 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004749 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004750 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004751
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004752 // Build a record containing all of the file scoped decls in this file.
4753 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004754 if (!isModule)
4755 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4756 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004757
Douglas Gregor851443c2011-08-12 01:39:19 +00004758 // Build a record containing all of the delegating constructors we still need
4759 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004760 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004761 if (!isModule)
4762 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004763
Douglas Gregor851443c2011-08-12 01:39:19 +00004764 // Write the set of weak, undeclared identifiers. We always write the
4765 // entire table, since later PCH files in a PCH chain are only interested in
4766 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004767 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004768 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4769 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4770 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4771 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4772 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4773 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4774 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004775 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004776
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004777 // Build a record containing all of the ext_vector declarations.
4778 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004779 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004780
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004781 // Build a record containing all of the VTable uses information.
4782 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004783 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004784 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4785 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4786 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4787 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4788 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004789 }
4790
Nico Weber72889432014-09-06 01:25:55 +00004791 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4792 RecordData UnusedLocalTypedefNameCandidates;
4793 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4794 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4795
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004796 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004797 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004798 for (const auto &I : SemaRef.PendingInstantiations) {
4799 AddDeclRef(I.first, PendingInstantiations);
4800 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004801 }
4802 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4803 "There are local ones at end of translation unit!");
4804
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004805 // Build a record containing some declaration references.
4806 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004807 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004808 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4809 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004810 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004811 }
4812
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004813 RecordData CUDASpecialDeclRefs;
4814 if (Context.getcudaConfigureCallDecl()) {
4815 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4816 }
4817
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004818 // Build a record containing all of the known namespaces.
4819 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004820 for (const auto &I : SemaRef.KnownNamespaces) {
4821 if (!I.second)
4822 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004823 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004824
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004825 // Build a record of all used, undefined objects that require definitions.
4826 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004827
4828 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004829 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004830 for (const auto &I : Undefined) {
4831 AddDeclRef(I.first, UndefinedButUsed);
4832 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004833 }
4834
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004835 // Build a record containing all delete-expressions that we would like to
4836 // analyze later in AST.
4837 RecordData DeleteExprsToAnalyze;
4838
Richard Smithf5262c62018-06-28 01:57:04 +00004839 if (!isModule) {
4840 for (const auto &DeleteExprsInfo :
4841 SemaRef.getMismatchingDeleteExpressions()) {
4842 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4843 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4844 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4845 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4846 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4847 }
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004848 }
4849 }
4850
Douglas Gregor112b9072012-10-18 05:31:06 +00004851 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004852 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004853
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004854 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004855 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004856
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004857 // This is so that older clang versions, before the introduction
4858 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004859 {
4860 RecordData Record = {VERSION_MAJOR};
4861 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4862 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004863
Douglas Gregor851443c2011-08-12 01:39:19 +00004864 // Create a lexical update block containing all of the declarations in the
4865 // translation unit that do not come from other AST files.
4866 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004867 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4868 for (const auto *D : TU->noload_decls()) {
4869 if (!D->isFromASTFile()) {
4870 NewGlobalKindDeclPairs.push_back(D->getKind());
4871 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4872 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004873 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004874
David Blaikieb44f0bf2017-01-04 22:36:43 +00004875 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004876 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4877 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004878 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004879 {
4880 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4881 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4882 bytes(NewGlobalKindDeclPairs));
4883 }
4884
Douglas Gregor851443c2011-08-12 01:39:19 +00004885 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004886 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004887 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4888 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004889 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004890 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004891 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004892
4893 // If we have any extern "C" names, write out a visible update for them.
4894 if (Context.ExternCContext)
4895 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004896
Douglas Gregor851443c2011-08-12 01:39:19 +00004897 // If the translation unit has an anonymous namespace, and we don't already
4898 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004899 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004900 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4901 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004902 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004903 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004904 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004905
Richard Smith5652c0f2014-03-21 01:48:23 +00004906 // Add update records for all mangling numbers and static local numbers.
4907 // These aren't really update records, but this is a convenient way of
4908 // tagging this rare extra data onto the declarations.
4909 for (const auto &Number : Context.MangleNumbers)
4910 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004911 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4912 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004913 for (const auto &Number : Context.StaticLocalNumbers)
4914 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004915 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4916 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004917
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004918 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004919 // an AST file, are registered for serialization. Likewise for template
4920 // specializations added to imported templates.
4921 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004922 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004923 }
4924
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004925 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004926 // serialization, if we're storing decls with identifiers.
4927 if (!WritingModule || !getLangOpts().CPlusPlus) {
4928 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004929 for (const auto &ID : PP.getIdentifierTable()) {
4930 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004931 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4932 IIs.push_back(II);
4933 }
4934 // Sort the identifiers to visit based on their name.
Benjamin Kramercd2bae32019-08-22 17:32:16 +00004935 llvm::sort(IIs, llvm::deref<std::less<>>());
Richard Smith79bf9202015-08-24 03:33:22 +00004936 for (const IdentifierInfo *II : IIs) {
4937 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4938 DEnd = SemaRef.IdResolver.end();
4939 D != DEnd; ++D) {
4940 GetDeclRef(*D);
4941 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004942 }
4943 }
4944
Manman Rena0f31a02016-04-29 19:04:05 +00004945 // For method pool in the module, if it contains an entry for a selector,
4946 // the entry should be complete, containing everything introduced by that
4947 // module and all modules it imports. It's possible that the entry is out of
4948 // date, so we need to pull in the new content here.
4949
4950 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4951 // safe, we copy all selectors out.
4952 llvm::SmallVector<Selector, 256> AllSelectors;
4953 for (auto &SelectorAndID : SelectorIDs)
4954 AllSelectors.push_back(SelectorAndID.first);
4955 for (auto &Selector : AllSelectors)
4956 SemaRef.updateOutOfDateSelector(Selector);
4957
Douglas Gregor5204bde2011-08-02 16:26:37 +00004958 // Form the record of special types.
4959 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004960 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004961 AddTypeRef(Context.getFILEType(), SpecialTypes);
4962 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4963 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4964 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4965 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004966 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004967 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004968
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004969 if (Chain) {
4970 // Write the mapping information describing our module dependencies and how
4971 // each of those modules were mapped into our own offset/ID space, so that
4972 // the reader can build the appropriate mapping to its own offset/ID space.
4973 // The map consists solely of a blob with the following format:
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004974 // *(module-kind:i8
4975 // module-name-len:i16 module-name:len*i8
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004976 // source-location-offset:i32
4977 // identifier-id:i32
4978 // preprocessed-entity-id:i32
4979 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004980 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004981 // selector-id:i32
4982 // declaration-id:i32
4983 // c++-base-specifiers-id:i32
4984 // type-id:i32)
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004985 //
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004986 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule or
4987 // MK_ExplicitModule, then the module-name is the module name. Otherwise,
4988 // it is the module file name.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004989 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004990 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004992 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004993 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004994 {
4995 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004996 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004997 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00004998
Peter Collingbournee3f65292018-05-18 19:46:24 +00004999 endian::Writer LE(Out, little);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00005000 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
5001 StringRef Name =
5002 M.Kind == MK_PrebuiltModule || M.Kind == MK_ExplicitModule
5003 ? M.ModuleName
5004 : M.FileName;
5005 LE.write<uint16_t>(Name.size());
5006 Out.write(Name.data(), Name.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00005007
Ben Langmuir785180e2014-10-20 16:27:30 +00005008 // Note: if a base ID was uint max, it would not be possible to load
5009 // another module after it or have more than one entity inside it.
5010 uint32_t None = std::numeric_limits<uint32_t>::max();
5011
5012 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
5013 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
5014 if (ShouldWrite)
5015 LE.write<uint32_t>(BaseID);
5016 else
5017 LE.write<uint32_t>(None);
5018 };
5019
Ben Langmuirfe971d92014-08-16 04:54:18 +00005020 // These values should be unique within a chain, since they will be read
5021 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00005022 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
5023 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
5024 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
5025 writeBaseIDOrNone(M.BasePreprocessedEntityID,
5026 M.NumPreprocessedEntities);
5027 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
5028 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
5029 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
5030 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005031 }
5032 }
Mehdi Amini57a41912015-09-10 01:46:39 +00005033 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005034 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
5035 Buffer.data(), Buffer.size());
5036 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00005037
Richard Smithb9eab6d2014-03-20 19:44:17 +00005038 RecordData DeclUpdatesOffsetsRecord;
5039
Richard Smith59442b42014-03-20 20:07:19 +00005040 // Keep writing types, declarations, and declaration update records
5041 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00005042 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5043 WriteTypeAbbrevs();
5044 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00005045 do {
5046 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5047 while (!DeclTypesToEmit.empty()) {
5048 DeclOrType DOT = DeclTypesToEmit.front();
5049 DeclTypesToEmit.pop();
5050 if (DOT.isType())
5051 WriteType(DOT.getType());
5052 else
5053 WriteDecl(Context, DOT.getDecl());
5054 }
5055 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00005056 Stream.ExitBlock();
5057
Richard Smithb9eab6d2014-03-20 19:44:17 +00005058 DoneWritingDeclsAndTypes = true;
5059
5060 // These things can only be done once we've written out decls and types.
5061 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00005062 if (!DeclUpdatesOffsetsRecord.empty())
5063 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00005064 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00005065 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00005066 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005067 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005068 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00005069 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00005070 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00005071 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005072 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00005073 WriteFPPragmaOptions(SemaRef.getFPOptions());
5074 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00005075 WriteOpenCLExtensionTypes(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00005076 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00005077
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005078 // If we're emitting a module, write out the submodule information.
Douglas Gregora89c5ac2011-12-06 01:10:29 +00005079 if (WritingModule)
5080 WriteSubmodules(WritingModule);
5081
Andrew Savonichev52d674d2018-10-29 11:14:01 +00005082 // We need to have information about submodules to correctly deserialize
5083 // decls from OpenCLExtensionDecls block
5084 WriteOpenCLExtensionDecls(SemaRef);
5085
Douglas Gregor5204bde2011-08-02 16:26:37 +00005086 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5087
Douglas Gregord4df8652009-04-22 22:02:47 +00005088 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00005089 if (!EagerlyDeserializedDecls.empty())
5090 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00005091
David Blaikief63556d2017-04-12 20:58:33 +00005092 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00005093 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5094
Douglas Gregord4df8652009-04-22 22:02:47 +00005095 // Write the record containing tentative definitions.
5096 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005097 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00005098
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00005099 // Write the record containing unused file scoped decls.
5100 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005101 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005102
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005103 // Write the record containing weak undeclared identifiers.
5104 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005105 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005106 WeakUndeclaredIdentifiers);
5107
Douglas Gregor61cac2b2009-04-27 20:06:05 +00005108 // Write the record containing ext_vector type names.
5109 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005110 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00005111
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005112 // Write the record containing VTable uses information.
5113 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005114 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005115
Nico Weber72889432014-09-06 01:25:55 +00005116 // Write the record containing potentially unused local typedefs.
5117 if (!UnusedLocalTypedefNameCandidates.empty())
5118 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5119 UnusedLocalTypedefNameCandidates);
5120
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005121 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00005122 if (!PendingInstantiations.empty())
5123 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005124
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005125 // Write the record containing declaration references of Sema.
5126 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005127 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005128
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005129 // Write the record containing CUDA-specific declaration references.
5130 if (!CUDASpecialDeclRefs.empty())
5131 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005132
Alexis Hunt27a761d2011-05-04 23:29:54 +00005133 // Write the delegating constructors.
5134 if (!DelegatingCtorDecls.empty())
5135 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005136
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005137 // Write the known namespaces.
5138 if (!KnownNamespaces.empty())
5139 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00005140
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00005141 // Write the undefined internal functions and variables, and inline functions.
5142 if (!UndefinedButUsed.empty())
5143 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00005144
5145 if (!DeleteExprsToAnalyze.empty())
5146 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5147
Douglas Gregor851443c2011-08-12 01:39:19 +00005148 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00005149 for (auto *DC : UpdatedDeclContexts)
5150 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00005151
Douglas Gregor959bb062011-12-03 01:15:29 +00005152 if (!WritingModule) {
5153 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00005154 struct ModuleInfo {
5155 uint64_t ID;
5156 Module *M;
5157 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5158 };
Richard Smith56be7542014-03-21 00:33:59 +00005159 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00005160 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00005161 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005162 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5163 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00005164 }
Richard Smith56be7542014-03-21 00:33:59 +00005165
5166 if (!Imports.empty()) {
5167 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5168 return A.ID < B.ID;
5169 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005170 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5171 return A.ID == B.ID;
5172 };
Richard Smith56be7542014-03-21 00:33:59 +00005173
5174 // Sort and deduplicate module IDs.
Fangrui Song55fab262018-09-26 22:16:28 +00005175 llvm::sort(Imports, Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005176 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00005177 Imports.end());
5178
5179 RecordData ImportedModules;
5180 for (const auto &Import : Imports) {
5181 ImportedModules.push_back(Import.ID);
5182 // FIXME: If the module has macros imported then later has declarations
5183 // imported, this location won't be the right one as a location for the
5184 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00005185 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00005186 }
5187
Douglas Gregor959bb062011-12-03 01:15:29 +00005188 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5189 }
Douglas Gregor0a839132011-12-03 00:59:55 +00005190 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005191
Douglas Gregor404cdde2012-01-27 01:47:08 +00005192 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00005193 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00005194 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005195 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00005196 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005197 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00005198 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00005199
Douglas Gregor08f01292009-04-17 22:13:46 +00005200 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00005201 RecordData::value_type Record[] = {
5202 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00005203 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00005204 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00005205
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005206 // Write the module file extension blocks.
5207 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00005208 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005209
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005210 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005211}
5212
Richard Smithb9eab6d2014-03-20 19:44:17 +00005213void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005214 if (DeclUpdates.empty())
5215 return;
5216
Richard Smith59442b42014-03-20 20:07:19 +00005217 DeclUpdateMap LocalUpdates;
5218 LocalUpdates.swap(DeclUpdates);
5219
Richard Smith6ef42932014-03-20 21:02:00 +00005220 for (auto &DeclUpdate : LocalUpdates) {
5221 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00005222
Richard Smithd28ac5b2014-03-22 23:33:22 +00005223 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00005224 RecordData RecordData;
5225 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00005226 for (auto &Update : DeclUpdate.second) {
5227 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5228
Richard Smith69c82bf2016-04-01 22:52:03 +00005229 // An updated body is emitted last, so that the reader doesn't need
5230 // to skip over the lazy body to reach statements for other records.
5231 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5232 HasUpdatedBody = true;
5233 else
5234 Record.push_back(Kind);
5235
Richard Smith6ef42932014-03-20 21:02:00 +00005236 switch (Kind) {
5237 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5238 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5239 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00005240 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00005241 Record.push_back(GetDeclRef(Update.getDecl()));
5242 break;
5243
Richard Smith4d235792014-08-07 18:53:08 +00005244 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00005245 break;
5246
Richard Smith891fc7f2017-12-05 01:31:47 +00005247 case UPD_CXX_POINT_OF_INSTANTIATION:
5248 // FIXME: Do we need to also save the template specialization kind here?
Richard Smith69c82bf2016-04-01 22:52:03 +00005249 Record.AddSourceLocation(Update.getLoc());
Richard Smith891fc7f2017-12-05 01:31:47 +00005250 break;
5251
5252 case UPD_CXX_ADDED_VAR_DEFINITION: {
5253 const VarDecl *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00005254 Record.push_back(VD->isInline());
5255 Record.push_back(VD->isInlineSpecified());
Richard Smith05a21352017-06-22 22:18:46 +00005256 if (VD->getInit()) {
5257 Record.push_back(!VD->isInitKnownICE() ? 1
5258 : (VD->isInitICE() ? 3 : 2));
5259 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
5260 } else {
5261 Record.push_back(0);
5262 }
Richard Smith4d235792014-08-07 18:53:08 +00005263 break;
Richard Smith05a21352017-06-22 22:18:46 +00005264 }
Richard Smith4d235792014-08-07 18:53:08 +00005265
John McCall32791cc2016-01-06 22:34:54 +00005266 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00005267 Record.AddStmt(const_cast<Expr *>(
5268 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00005269 break;
5270
Richard Smith4b054b22016-08-24 21:25:37 +00005271 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5272 Record.AddStmt(
5273 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5274 break;
5275
Richard Smithcd45dbc2014-04-19 03:48:30 +00005276 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5277 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005278 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00005279 Record.push_back(RD->isParamDestroyedInCallee());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00005280 Record.push_back(RD->getArgPassingRestrictions());
Richard Smith69c82bf2016-04-01 22:52:03 +00005281 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00005282 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00005283 *Context, const_cast<CXXRecordDecl *>(RD)));
5284
5285 // This state is sometimes updated by template instantiation, when we
5286 // switch from the specialization referring to the template declaration
5287 // to it referring to the template definition.
5288 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5289 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005290 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005291 } else {
5292 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5293 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005294 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00005295
5296 // The instantiation might have been resolved to a partial
5297 // specialization. If so, record which one.
5298 auto From = Spec->getInstantiatedFrom();
5299 if (auto PartialSpec =
5300 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5301 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00005302 Record.AddDeclRef(PartialSpec);
5303 Record.AddTemplateArgumentList(
5304 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00005305 } else {
5306 Record.push_back(false);
5307 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00005308 }
5309 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005310 Record.AddSourceLocation(RD->getLocation());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005311 Record.AddSourceLocation(RD->getBeginLoc());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00005312 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005313
5314 // Instantiation may change attributes; write them all out afresh.
5315 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00005316 if (D->hasAttrs())
5317 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005318
5319 // FIXME: Ensure we don't get here for explicit instantiations.
5320 break;
5321 }
5322
Richard Smithf8134002015-03-10 01:41:22 +00005323 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005324 Record.AddDeclRef(Update.getDecl());
Richard Smith5b349582017-10-13 01:55:36 +00005325 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
Richard Smithf8134002015-03-10 01:41:22 +00005326 break;
5327
Richard Smith564417a2014-03-20 21:47:22 +00005328 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
5329 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00005330 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
5331 Record);
5332 break;
5333
Richard Smith6ef42932014-03-20 21:02:00 +00005334 case UPD_CXX_DEDUCED_RETURN_TYPE:
5335 Record.push_back(GetOrCreateTypeID(Update.getType()));
5336 break;
5337
5338 case UPD_DECL_MARKED_USED:
5339 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00005340
5341 case UPD_MANGLING_NUMBER:
5342 case UPD_STATIC_LOCAL_NUMBER:
5343 Record.push_back(Update.getNumber());
5344 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005345
Alexey Bataev97720002014-11-11 04:05:39 +00005346 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005347 Record.AddSourceRange(
5348 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005349 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005350
Alexey Bataev27ef9512019-03-20 20:14:22 +00005351 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
5352 auto *A = D->getAttr<OMPAllocateDeclAttr>();
5353 Record.push_back(A->getAllocatorType());
5354 Record.AddStmt(A->getAllocator());
5355 Record.AddSourceRange(A->getRange());
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005356 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00005357 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00005358
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005359 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00005360 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005361 Record.AddSourceRange(
5362 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5363 break;
5364
Richard Smith65ebb4a2015-03-26 04:09:53 +00005365 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005366 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005367 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005368
5369 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005370 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005371 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005372 }
5373 }
5374
Richard Smithd28ac5b2014-03-22 23:33:22 +00005375 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005376 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005377 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005378 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005379 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005380 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005381 }
5382
Richard Smithcd45dbc2014-04-19 03:48:30 +00005383 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005384 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005385 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005386}
5387
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005388void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005389 uint32_t Raw = Loc.getRawEncoding();
5390 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005391}
5392
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005393void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005394 AddSourceLocation(Range.getBegin(), Record);
5395 AddSourceLocation(Range.getEnd(), Record);
5396}
5397
Richard Smithf144b542016-04-08 21:54:32 +00005398void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5399 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005400 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005401 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005402}
5403
Richard Smithf144b542016-04-08 21:54:32 +00005404void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5405 Record->push_back(Value.isUnsigned());
5406 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005407}
5408
Richard Smithf144b542016-04-08 21:54:32 +00005409void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5410 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005411}
5412
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00005413static void WriteFixedPointSemantics(ASTRecordWriter &Record,
5414 FixedPointSemantics FPSema) {
5415 Record.push_back(FPSema.getWidth());
5416 Record.push_back(FPSema.getScale());
5417 Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 |
5418 FPSema.hasUnsignedPadding() << 2);
5419}
5420
5421void ASTRecordWriter::AddAPValue(const APValue &Value) {
5422 APValue::ValueKind Kind = Value.getKind();
5423 push_back(static_cast<uint64_t>(Kind));
5424 switch (Kind) {
5425 case APValue::None:
5426 case APValue::Indeterminate:
5427 return;
5428 case APValue::Int:
5429 AddAPSInt(Value.getInt());
5430 return;
5431 case APValue::Float:
5432 push_back(static_cast<uint64_t>(
5433 llvm::APFloatBase::SemanticsToEnum(Value.getFloat().getSemantics())));
5434 AddAPFloat(Value.getFloat());
5435 return;
5436 case APValue::FixedPoint: {
5437 WriteFixedPointSemantics(*this, Value.getFixedPoint().getSemantics());
5438 AddAPSInt(Value.getFixedPoint().getValue());
5439 return;
5440 }
5441 case APValue::ComplexInt: {
5442 AddAPSInt(Value.getComplexIntReal());
5443 AddAPSInt(Value.getComplexIntImag());
5444 return;
5445 }
5446 case APValue::ComplexFloat: {
5447 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5448 Value.getComplexFloatReal().getSemantics())));
5449 AddAPFloat(Value.getComplexFloatReal());
5450 push_back(static_cast<uint64_t>(llvm::APFloatBase::SemanticsToEnum(
5451 Value.getComplexFloatImag().getSemantics())));
5452 AddAPFloat(Value.getComplexFloatImag());
5453 return;
5454 }
5455 case APValue::LValue:
5456 case APValue::Vector:
5457 case APValue::Array:
5458 case APValue::Struct:
5459 case APValue::Union:
5460 case APValue::MemberPointer:
5461 case APValue::AddrLabelDiff:
5462 // TODO : Handle all these APValue::ValueKind.
5463 return;
5464 }
5465 llvm_unreachable("Invalid APValue::ValueKind");
5466}
5467
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005468void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005469 Record.push_back(getIdentifierRef(II));
5470}
5471
Sebastian Redl539c5062010-08-18 23:57:32 +00005472IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005473 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005474 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005475
Sebastian Redl539c5062010-08-18 23:57:32 +00005476 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005477 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005478 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005479 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005480}
5481
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005482MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005483 // Don't emit builtin macros like __LINE__ to the AST file unless they
5484 // have been redefined by the header (in which case they are not
5485 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005486 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005487 return 0;
5488
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005489 MacroID &ID = MacroIDs[MI];
5490 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005491 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005492 MacroInfoToEmitData Info = { Name, MI, ID };
5493 MacroInfosToEmit.push_back(Info);
5494 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005495 return ID;
5496}
5497
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005498MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005499 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005500 return 0;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005501
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005502 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5503 return MacroIDs[MI];
5504}
5505
5506uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005507 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005508}
5509
Richard Smithe64e7452016-04-18 21:54:58 +00005510void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5511 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005512}
5513
Sebastian Redl539c5062010-08-18 23:57:32 +00005514SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005515 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005516 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005517 }
5518
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005519 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005520 if (SID == 0 && Chain) {
5521 // This might trigger a ReadSelector callback, which will set the ID for
5522 // this selector.
5523 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005524 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005525 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005526 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005527 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005528 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005529 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005530 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005531}
5532
Richard Smithe64e7452016-04-18 21:54:58 +00005533void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5534 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005535}
5536
Richard Smith290d8012016-04-06 17:06:00 +00005537void ASTRecordWriter::AddTemplateArgumentLocInfo(
5538 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005539 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005540 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005541 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005542 break;
5543 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005544 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005545 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005546 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005547 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5548 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005549 break;
5550 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005551 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5552 AddSourceLocation(Arg.getTemplateNameLoc());
5553 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005554 break;
John McCall0ad16662009-10-29 08:12:44 +00005555 case TemplateArgument::Null:
5556 case TemplateArgument::Integral:
5557 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005558 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005559 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005560 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005561 break;
5562 }
5563}
5564
Richard Smith290d8012016-04-06 17:06:00 +00005565void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5566 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005567
5568 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5569 bool InfoHasSameExpr
5570 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005571 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005572 if (InfoHasSameExpr)
5573 return; // Avoid storing the same expr twice.
5574 }
Richard Smith290d8012016-04-06 17:06:00 +00005575 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005576}
5577
Richard Smith290d8012016-04-06 17:06:00 +00005578void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005579 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005580 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005581 return;
5582 }
5583
Richard Smithc23d7342018-06-29 20:46:25 +00005584 AddTypeRef(TInfo->getType());
Richard Smith290d8012016-04-06 17:06:00 +00005585 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005586}
5587
Richard Smith290d8012016-04-06 17:06:00 +00005588void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
Richard Smith290d8012016-04-06 17:06:00 +00005589 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005590 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005591 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005592}
5593
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005594void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005595 Record.push_back(GetOrCreateTypeID(T));
5596}
5597
Richard Smith2095ffe2015-03-16 20:11:03 +00005598TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005599 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005600 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5601 if (T.isNull())
5602 return TypeIdx();
5603 assert(!T.getLocalFastQualifiers());
5604
5605 TypeIdx &Idx = TypeIdxs[T];
5606 if (Idx.getIndex() == 0) {
5607 if (DoneWritingDeclsAndTypes) {
5608 assert(0 && "New type seen after serializing all the types to emit!");
5609 return TypeIdx();
5610 }
5611
5612 // We haven't seen this type before. Assign it a new ID and put it
5613 // into the queue of types to emit.
5614 Idx = TypeIdx(NextTypeID++);
5615 DeclTypesToEmit.push(T);
5616 }
5617 return Idx;
5618 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005619}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005620
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005621TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005622 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005623 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5624 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005625 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005626 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005627
Richard Smith2095ffe2015-03-16 20:11:03 +00005628 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5629 assert(I != TypeIdxs.end() && "Type not emitted!");
5630 return I->second;
5631 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005632}
5633
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005634void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005635 Record.push_back(GetDeclRef(D));
5636}
5637
Sebastian Redl539c5062010-08-18 23:57:32 +00005638DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005639 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005640
5641 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005642 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005643 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005644
Douglas Gregorb3163e52012-01-05 22:33:30 +00005645 // If D comes from an AST file, its declaration ID is already known and
5646 // fixed.
5647 if (D->isFromASTFile())
5648 return D->getGlobalID();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005649
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005650 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005651 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005652 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005653 if (DoneWritingDeclsAndTypes) {
5654 assert(0 && "New decl seen after serializing all the decls to emit!");
5655 return 0;
5656 }
5657
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005658 // We haven't seen this declaration before. Give it a new ID and
5659 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005660 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005661 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005662 }
5663
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005664 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005665}
5666
Sebastian Redl539c5062010-08-18 23:57:32 +00005667DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005668 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005669 return 0;
5670
Douglas Gregorb3163e52012-01-05 22:33:30 +00005671 // If D comes from an AST file, its declaration ID is already known and
5672 // fixed.
5673 if (D->isFromASTFile())
5674 return D->getGlobalID();
5675
Douglas Gregore84a9da2009-04-20 20:36:09 +00005676 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5677 return DeclIDs[D];
5678}
5679
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005680void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005681 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005682 assert(D);
5683
5684 SourceLocation Loc = D->getLocation();
5685 if (Loc.isInvalid())
5686 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005687
5688 // We only keep track of the file-level declarations of each file.
5689 if (!D->getLexicalDeclContext()->isFileContext())
5690 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005691 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5692 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis71b74eb2018-01-26 19:26:12 +00005693 // TemplateTemplateParmDecls that are part of an alias template, should not
5694 // have TU as lexical context.
5695 if (isa<ParmVarDecl>(D) || isa<TemplateTemplateParmDecl>(D))
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005696 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005697
5698 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005699 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005700 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005701 FileID FID;
5702 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005703 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005704 if (FID.isInvalid())
5705 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005706 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005707
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005708 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005709 if (!Info)
5710 Info = new DeclIDInFileInfo();
5711
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005712 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005713 LocDeclIDsTy &Decls = Info->DeclIDs;
5714
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005715 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005716 Decls.push_back(LocDecl);
5717 return;
5718 }
5719
Benjamin Kramer45025c02013-08-24 13:22:59 +00005720 LocDeclIDsTy::iterator I =
Fangrui Song7264a472019-07-03 08:13:17 +00005721 llvm::upper_bound(Decls, LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005722
5723 Decls.insert(I, LocDecl);
5724}
5725
Richard Smithe64e7452016-04-18 21:54:58 +00005726void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005727 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005728 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005729 switch (Name.getNameKind()) {
5730 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005731 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005732 break;
5733
5734 case DeclarationName::ObjCZeroArgSelector:
5735 case DeclarationName::ObjCOneArgSelector:
5736 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005737 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005738 break;
5739
5740 case DeclarationName::CXXConstructorName:
5741 case DeclarationName::CXXDestructorName:
5742 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005743 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005744 break;
5745
Richard Smith35845152017-02-07 01:37:30 +00005746 case DeclarationName::CXXDeductionGuideName:
5747 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5748 break;
5749
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005750 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005751 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005752 break;
5753
Alexis Hunt3d221f22009-11-29 07:34:05 +00005754 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005755 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005756 break;
5757
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005758 case DeclarationName::CXXUsingDirective:
5759 // No extra data to emit
5760 break;
5761 }
5762}
Chris Lattnerca025db2010-05-07 21:43:38 +00005763
Richard Smithd08aeb62014-08-28 01:33:39 +00005764unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5765 assert(needsAnonymousDeclarationNumber(D) &&
5766 "expected an anonymous declaration");
5767
5768 // Number the anonymous declarations within this context, if we've not
5769 // already done so.
5770 auto It = AnonymousDeclarationNumbers.find(D);
5771 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005772 auto *DC = D->getLexicalDeclContext();
5773 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5774 AnonymousDeclarationNumbers[ND] = Number;
5775 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005776
5777 It = AnonymousDeclarationNumbers.find(D);
5778 assert(It != AnonymousDeclarationNumbers.end() &&
5779 "declaration not found within its lexical context");
5780 }
5781
5782 return It->second;
5783}
5784
Richard Smith290d8012016-04-06 17:06:00 +00005785void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5786 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005787 switch (Name.getNameKind()) {
5788 case DeclarationName::CXXConstructorName:
5789 case DeclarationName::CXXDestructorName:
5790 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005791 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005792 break;
5793
5794 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005795 AddSourceLocation(SourceLocation::getFromRawEncoding(
5796 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005797 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005798 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005799 break;
5800
5801 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005802 AddSourceLocation(SourceLocation::getFromRawEncoding(
5803 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005804 break;
5805
5806 case DeclarationName::Identifier:
5807 case DeclarationName::ObjCZeroArgSelector:
5808 case DeclarationName::ObjCOneArgSelector:
5809 case DeclarationName::ObjCMultiArgSelector:
5810 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005811 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005812 break;
5813 }
5814}
5815
Richard Smith290d8012016-04-06 17:06:00 +00005816void ASTRecordWriter::AddDeclarationNameInfo(
5817 const DeclarationNameInfo &NameInfo) {
5818 AddDeclarationName(NameInfo.getName());
5819 AddSourceLocation(NameInfo.getLoc());
5820 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005821}
5822
Richard Smith290d8012016-04-06 17:06:00 +00005823void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5824 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5825 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005826 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005827 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005828}
5829
Richard Smithe64e7452016-04-18 21:54:58 +00005830void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005831 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005832 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005833 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005834
5835 // Push each of the NNS's onto a stack for serialization in reverse order.
5836 while (NNS) {
5837 NestedNames.push_back(NNS);
5838 NNS = NNS->getPrefix();
5839 }
5840
Richard Smithe64e7452016-04-18 21:54:58 +00005841 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005842 while(!NestedNames.empty()) {
5843 NNS = NestedNames.pop_back_val();
5844 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005845 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005846 switch (Kind) {
5847 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005848 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005849 break;
5850
5851 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005852 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005853 break;
5854
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005855 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005856 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005857 break;
5858
Chris Lattnerca025db2010-05-07 21:43:38 +00005859 case NestedNameSpecifier::TypeSpec:
5860 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005861 AddTypeRef(QualType(NNS->getAsType(), 0));
5862 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005863 break;
5864
5865 case NestedNameSpecifier::Global:
5866 // Don't need to write an associated value.
5867 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005868
5869 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005870 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005871 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005872 }
5873 }
5874}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005875
Richard Smith290d8012016-04-06 17:06:00 +00005876void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005877 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005878 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005879 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005880
5881 // Push each of the nested-name-specifiers's onto a stack for
5882 // serialization in reverse order.
5883 while (NNS) {
5884 NestedNames.push_back(NNS);
5885 NNS = NNS.getPrefix();
5886 }
5887
Richard Smith290d8012016-04-06 17:06:00 +00005888 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005889 while(!NestedNames.empty()) {
5890 NNS = NestedNames.pop_back_val();
5891 NestedNameSpecifier::SpecifierKind Kind
5892 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005893 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005894 switch (Kind) {
5895 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005896 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5897 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005898 break;
5899
5900 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005901 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5902 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005903 break;
5904
5905 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005906 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5907 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005908 break;
5909
5910 case NestedNameSpecifier::TypeSpec:
5911 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005912 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Richard Smithc23d7342018-06-29 20:46:25 +00005913 AddTypeRef(NNS.getTypeLoc().getType());
Richard Smith290d8012016-04-06 17:06:00 +00005914 AddTypeLoc(NNS.getTypeLoc());
5915 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005916 break;
5917
5918 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005919 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005920 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005921
5922 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005923 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5924 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005925 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005926 }
5927 }
5928}
5929
Richard Smith290d8012016-04-06 17:06:00 +00005930void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005931 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005932 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005933 switch (Kind) {
5934 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005935 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005936 break;
5937
5938 case TemplateName::OverloadedTemplate: {
5939 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005940 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005941 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005942 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005943 break;
5944 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005945
Richard Smithb23c5e82019-05-09 03:31:27 +00005946 case TemplateName::AssumedTemplate: {
5947 AssumedTemplateStorage *ADLT = Name.getAsAssumedTemplateName();
5948 AddDeclarationName(ADLT->getDeclName());
5949 break;
5950 }
5951
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005952 case TemplateName::QualifiedTemplate: {
5953 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005954 AddNestedNameSpecifier(QualT->getQualifier());
5955 Record->push_back(QualT->hasTemplateKeyword());
5956 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005957 break;
5958 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005959
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005960 case TemplateName::DependentTemplate: {
5961 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005962 AddNestedNameSpecifier(DepT->getQualifier());
5963 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005964 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005965 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005966 else
Richard Smith290d8012016-04-06 17:06:00 +00005967 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005968 break;
5969 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005970
5971 case TemplateName::SubstTemplateTemplateParm: {
5972 SubstTemplateTemplateParmStorage *subst
5973 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005974 AddDeclRef(subst->getParameter());
5975 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005976 break;
5977 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005978
Douglas Gregor5590be02011-01-15 06:45:20 +00005979 case TemplateName::SubstTemplateTemplateParmPack: {
5980 SubstTemplateTemplateParmPackStorage *SubstPack
5981 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005982 AddDeclRef(SubstPack->getParameterPack());
5983 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005984 break;
5985 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005986 }
5987}
5988
Richard Smith290d8012016-04-06 17:06:00 +00005989void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5990 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005991 switch (Arg.getKind()) {
5992 case TemplateArgument::Null:
5993 break;
5994 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005995 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005996 break;
5997 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005998 AddDeclRef(Arg.getAsDecl());
5999 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00006000 break;
6001 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00006002 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006003 break;
6004 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00006005 AddAPSInt(Arg.getAsIntegral());
6006 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006007 break;
6008 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00006009 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00006010 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006011 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00006012 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00006013 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00006014 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00006015 else
Richard Smith290d8012016-04-06 17:06:00 +00006016 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006017 break;
6018 case TemplateArgument::Expression:
6019 AddStmt(Arg.getAsExpr());
6020 break;
6021 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00006022 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00006023 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00006024 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00006025 break;
6026 }
6027}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006028
Richard Smithe64e7452016-04-18 21:54:58 +00006029void ASTRecordWriter::AddTemplateParameterList(
6030 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006031 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00006032 AddSourceLocation(TemplateParams->getTemplateLoc());
6033 AddSourceLocation(TemplateParams->getLAngleLoc());
6034 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00006035 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00006036 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006037 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00006038 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006039}
6040
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006041/// Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00006042void ASTRecordWriter::AddTemplateArgumentList(
6043 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006044 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00006045 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006046 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00006047 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006048}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006049
Richard Smith290d8012016-04-06 17:06:00 +00006050void ASTRecordWriter::AddASTTemplateArgumentListInfo(
6051 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006052 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00006053 AddSourceLocation(ASTTemplArgList->LAngleLoc);
6054 AddSourceLocation(ASTTemplArgList->RAngleLoc);
6055 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006056 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006057 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00006058 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006059}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006060
Richard Smithe64e7452016-04-18 21:54:58 +00006061void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
6062 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006063 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006064 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00006065 AddDeclRef(I.getDecl());
6066 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006067 }
6068}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006069
Richard Smith290d8012016-04-06 17:06:00 +00006070// FIXME: Move this out of the main ASTRecordWriter interface.
6071void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
6072 Record->push_back(Base.isVirtual());
6073 Record->push_back(Base.isBaseOfClass());
6074 Record->push_back(Base.getAccessSpecifierAsWritten());
6075 Record->push_back(Base.getInheritConstructors());
6076 AddTypeSourceInfo(Base.getTypeSourceInfo());
6077 AddSourceRange(Base.getSourceRange());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006078 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00006079 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006080}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006081
Richard Smith645d2cf2016-04-14 00:29:55 +00006082static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
6083 ArrayRef<CXXBaseSpecifier> Bases) {
6084 ASTWriter::RecordData Record;
6085 ASTRecordWriter Writer(W, Record);
6086 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00006087
Richard Smith645d2cf2016-04-14 00:29:55 +00006088 for (auto &Base : Bases)
6089 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00006090
Richard Smith645d2cf2016-04-14 00:29:55 +00006091 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006092}
6093
Richard Smith290d8012016-04-06 17:06:00 +00006094// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00006095void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
6096 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
6097}
6098
Richard Smithaa165cf2016-04-13 21:57:08 +00006099static uint64_t
6100EmitCXXCtorInitializers(ASTWriter &W,
6101 ArrayRef<CXXCtorInitializer *> CtorInits) {
6102 ASTWriter::RecordData Record;
6103 ASTRecordWriter Writer(W, Record);
6104 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006105
Richard Smithaa165cf2016-04-13 21:57:08 +00006106 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006107 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00006108 Writer.push_back(CTOR_INITIALIZER_BASE);
6109 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
6110 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00006111 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00006112 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
6113 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00006114 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00006115 Writer.push_back(CTOR_INITIALIZER_MEMBER);
6116 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006117 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00006118 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
6119 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006120 }
Francois Pichetd583da02010-12-04 09:14:42 +00006121
Richard Smithaa165cf2016-04-13 21:57:08 +00006122 Writer.AddSourceLocation(Init->getMemberLocation());
6123 Writer.AddStmt(Init->getInit());
6124 Writer.AddSourceLocation(Init->getLParenLoc());
6125 Writer.AddSourceLocation(Init->getRParenLoc());
6126 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00006127 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00006128 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006129 }
Richard Smithaa165cf2016-04-13 21:57:08 +00006130
6131 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006132}
6133
Richard Smithaa165cf2016-04-13 21:57:08 +00006134// FIXME: Move this out of the main ASTRecordWriter interface.
6135void ASTRecordWriter::AddCXXCtorInitializers(
6136 ArrayRef<CXXCtorInitializer *> CtorInits) {
6137 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00006138}
6139
Richard Smith290d8012016-04-06 17:06:00 +00006140void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00006141 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00006142 Record->push_back(Data.IsLambda);
6143 Record->push_back(Data.UserDeclaredConstructor);
6144 Record->push_back(Data.UserDeclaredSpecialMembers);
6145 Record->push_back(Data.Aggregate);
6146 Record->push_back(Data.PlainOldData);
6147 Record->push_back(Data.Empty);
6148 Record->push_back(Data.Polymorphic);
6149 Record->push_back(Data.Abstract);
6150 Record->push_back(Data.IsStandardLayout);
Richard Smithb6070db2018-04-05 18:55:37 +00006151 Record->push_back(Data.IsCXX11StandardLayout);
6152 Record->push_back(Data.HasBasesWithFields);
6153 Record->push_back(Data.HasBasesWithNonStaticDataMembers);
Richard Smith290d8012016-04-06 17:06:00 +00006154 Record->push_back(Data.HasPrivateFields);
6155 Record->push_back(Data.HasProtectedFields);
6156 Record->push_back(Data.HasPublicFields);
6157 Record->push_back(Data.HasMutableFields);
6158 Record->push_back(Data.HasVariantMembers);
6159 Record->push_back(Data.HasOnlyCMembers);
6160 Record->push_back(Data.HasInClassInitializer);
6161 Record->push_back(Data.HasUninitializedReferenceMember);
6162 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00006163 Record->push_back(Data.HasInheritedConstructor);
6164 Record->push_back(Data.HasInheritedAssignment);
Richard Smith96cd6712017-08-16 01:49:53 +00006165 Record->push_back(Data.NeedOverloadResolutionForCopyConstructor);
Richard Smith290d8012016-04-06 17:06:00 +00006166 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
6167 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
6168 Record->push_back(Data.NeedOverloadResolutionForDestructor);
Richard Smith96cd6712017-08-16 01:49:53 +00006169 Record->push_back(Data.DefaultedCopyConstructorIsDeleted);
Richard Smith290d8012016-04-06 17:06:00 +00006170 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
6171 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
6172 Record->push_back(Data.DefaultedDestructorIsDeleted);
6173 Record->push_back(Data.HasTrivialSpecialMembers);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006174 Record->push_back(Data.HasTrivialSpecialMembersForCall);
Richard Smith290d8012016-04-06 17:06:00 +00006175 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006176 Record->push_back(Data.DeclaredNonTrivialSpecialMembersForCall);
Richard Smith290d8012016-04-06 17:06:00 +00006177 Record->push_back(Data.HasIrrelevantDestructor);
6178 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
6179 Record->push_back(Data.HasDefaultedDefaultConstructor);
6180 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
6181 Record->push_back(Data.HasConstexprDefaultConstructor);
6182 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
6183 Record->push_back(Data.ComputedVisibleConversions);
6184 Record->push_back(Data.UserProvidedDefaultConstructor);
6185 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00006186 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
6187 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00006188 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
6189 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
6190 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieufd1acbb2017-04-11 21:31:00 +00006191
6192 // getODRHash will compute the ODRHash if it has not been previously computed.
6193 Record->push_back(D->getODRHash());
David Blaikief63556d2017-04-12 20:58:33 +00006194 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
6195 Writer->WritingModule && !D->isDependentType();
6196 Record->push_back(ModulesDebugInfo);
6197 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00006198 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
6199
Richard Smith561fb152012-02-25 07:33:38 +00006200 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006201
Richard Smith290d8012016-04-06 17:06:00 +00006202 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006203 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006204 AddCXXBaseSpecifiers(Data.bases());
6205
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006206 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00006207 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006208 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006209 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006210
Richard Smith290d8012016-04-06 17:06:00 +00006211 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
6212 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006213 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00006214 AddDeclRef(D->getFirstFriend());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006215
Douglas Gregor99ae8062012-02-14 17:54:36 +00006216 // Add lambda-specific data.
6217 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00006218 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00006219 Record->push_back(Lambda.Dependent);
6220 Record->push_back(Lambda.IsGenericLambda);
6221 Record->push_back(Lambda.CaptureDefault);
6222 Record->push_back(Lambda.NumCaptures);
6223 Record->push_back(Lambda.NumExplicitCaptures);
6224 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00006225 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00006226 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00006227 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00006228 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00006229 AddSourceLocation(Capture.getLocation());
6230 Record->push_back(Capture.isImplicit());
6231 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00006232 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00006233 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00006234 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00006235 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00006236 break;
6237 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00006238 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00006239 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00006240 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00006241 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00006242 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00006243 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00006244 break;
6245 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00006246 }
6247 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006248}
6249
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006250void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00006251 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00006252 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00006253 assert(FirstDeclID == NextDeclID &&
6254 FirstTypeID == NextTypeID &&
6255 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006256 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00006257 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00006258 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00006259 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00006260
Sebastian Redl07a89a82010-07-30 00:29:29 +00006261 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006262
Richard Smith7f330cd2015-03-18 01:42:29 +00006263 // Note, this will get called multiple times, once one the reader starts up
6264 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00006265 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6266 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6267 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006268 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00006269 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00006270 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006271 NextDeclID = FirstDeclID;
6272 NextTypeID = FirstTypeID;
6273 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006274 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006275 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00006276 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00006277}
6278
Sebastian Redl539c5062010-08-18 23:57:32 +00006279void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006280 // Always keep the highest ID. See \p TypeRead() for more information.
6281 IdentID &StoredID = IdentifierIDs[II];
6282 if (ID > StoredID)
6283 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00006284}
6285
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006286void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006287 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006288 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006289 if (ID > StoredID)
6290 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006291}
6292
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00006293void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006294 // Always take the highest-numbered type index. This copes with an interesting
6295 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006296 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006297 // keep the higher-numbered entry so that we can properly write it out to
6298 // the AST file.
6299 TypeIdx &StoredIdx = TypeIdxs[T];
6300 if (Idx.getIndex() >= StoredIdx.getIndex())
6301 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006302}
6303
Sebastian Redl539c5062010-08-18 23:57:32 +00006304void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006305 // Always keep the highest ID. See \p TypeRead() for more information.
6306 SelectorID &StoredID = SelectorIDs[S];
6307 if (ID > StoredID)
6308 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00006309}
Douglas Gregor91096292010-10-02 19:29:26 +00006310
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006311void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00006312 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006313 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00006314 MacroDefinitions[MD] = ID;
6315}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006316
Douglas Gregore37a85a2011-12-02 17:30:13 +00006317void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6318 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
6319 SubmoduleIDs[Mod] = ID;
6320}
6321
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006322void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006323 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00006324 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006325 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006326 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006327 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00006328 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006329 // A forward reference was mutated into a definition. Rewrite it.
6330 // FIXME: This happens during template instantiation, should we
6331 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00006332 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6333 "completed a tag from another module but not by instantiation?");
6334 DeclUpdates[RD].push_back(
6335 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006336 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006337 }
6338}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006339
Richard Smithf983f7f2015-10-13 00:23:25 +00006340static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6341 if (D->isFromASTFile())
6342 return true;
6343
Richard Smithf983f7f2015-10-13 00:23:25 +00006344 // The predefined __va_list_tag struct is imported if we imported any decls.
6345 // FIXME: This is a gross hack.
6346 return D == D->getASTContext().getVaListTagDecl();
6347}
6348
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006349void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006350 if (Chain && Chain->isProcessingUpdateRecords()) return;
6351 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00006352 "Should not add lookup results to non-lookup contexts!");
6353
Vassil Vassilev632eac32016-03-16 11:17:04 +00006354 // TU is handled elsewhere.
6355 if (isa<TranslationUnitDecl>(DC))
6356 return;
6357
6358 // Namespaces are handled elsewhere, except for template instantiations of
6359 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6360 // local instantiations are added to an imported context. Only happens when
6361 // adding ADL lookup candidates, for example templated friends.
6362 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6363 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006364 return;
6365
Richard Smithf983f7f2015-10-13 00:23:25 +00006366 // We're only interested in cases where a local declaration is added to an
6367 // imported context.
6368 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6369 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006370
Richard Smith0f4e2c42015-08-06 04:23:48 +00006371 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00006372 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00006373 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00006374 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6375 // We're adding a visible declaration to a predefined decl context. Ensure
6376 // that we write out all of its lookup results so we don't get a nasty
6377 // surprise when we try to emit its lookup table.
6378 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00006379 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00006380 }
Richard Smithc26d9742016-10-06 20:30:51 +00006381 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006382}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006383
6384void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006385 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006386 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00006387
6388 // We're only interested in cases where a local declaration is added to an
6389 // imported context.
6390 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6391 return;
6392
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006393 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00006394 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006395
6396 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00006397 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00006398 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00006399 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006400}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00006401
Richard Smith564417a2014-03-20 21:47:22 +00006402void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006403 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00006404 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6405 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006406 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006407 // If we don't already know the exception specification for this redecl
6408 // chain, add an update record for it.
6409 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6410 ->getType()
6411 ->castAs<FunctionProtoType>()
6412 ->getExceptionSpecType()))
6413 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6414 });
Richard Smith564417a2014-03-20 21:47:22 +00006415}
6416
Richard Smith1fa5d642013-05-11 05:45:24 +00006417void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006418 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00006419 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00006420 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006421 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006422 DeclUpdates[D].push_back(
6423 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6424 });
Richard Smith1fa5d642013-05-11 05:45:24 +00006425}
6426
Richard Smithf8134002015-03-10 01:41:22 +00006427void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
Richard Smith5b349582017-10-13 01:55:36 +00006428 const FunctionDecl *Delete,
6429 Expr *ThisArg) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006430 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006431 assert(!WritingAST && "Already writing the AST!");
6432 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006433 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006434 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006435 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6436 });
Richard Smithf8134002015-03-10 01:41:22 +00006437}
6438
Sebastian Redlab238a72011-04-24 16:28:06 +00006439void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006440 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006441 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006442 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006443 return; // Declaration not imported from PCH.
6444
Richard Smith4d235792014-08-07 18:53:08 +00006445 // Implicit function decl from a PCH was defined.
6446 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006447}
6448
Richard Smith891fc7f2017-12-05 01:31:47 +00006449void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6450 if (Chain && Chain->isProcessingUpdateRecords()) return;
6451 assert(!WritingAST && "Already writing the AST!");
6452 if (!D->isFromASTFile())
6453 return;
6454
6455 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6456}
6457
Richard Smithd28ac5b2014-03-22 23:33:22 +00006458void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006459 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006460 assert(!WritingAST && "Already writing the AST!");
6461 if (!D->isFromASTFile())
6462 return;
6463
Richard Smith9e2341d2015-03-23 03:25:59 +00006464 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006465}
6466
Richard Smith891fc7f2017-12-05 01:31:47 +00006467void ASTWriter::InstantiationRequested(const ValueDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006468 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006469 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006470 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006471 return;
6472
6473 // Since the actual instantiation is delayed, this really means that we need
6474 // to update the instantiation location.
Richard Smith891fc7f2017-12-05 01:31:47 +00006475 SourceLocation POI;
6476 if (auto *VD = dyn_cast<VarDecl>(D))
6477 POI = VD->getPointOfInstantiation();
6478 else
6479 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6480 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006481}
6482
John McCall32791cc2016-01-06 22:34:54 +00006483void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006484 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006485 assert(!WritingAST && "Already writing the AST!");
6486 if (!D->isFromASTFile())
6487 return;
6488
6489 DeclUpdates[D].push_back(
6490 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6491}
6492
Richard Smith4b054b22016-08-24 21:25:37 +00006493void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6494 assert(!WritingAST && "Already writing the AST!");
6495 if (!D->isFromASTFile())
6496 return;
6497
6498 DeclUpdates[D].push_back(
6499 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6500}
6501
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006502void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6503 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006504 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006505 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006506 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006507 return; // Declaration not imported from PCH.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006508
Douglas Gregor404cdde2012-01-27 01:47:08 +00006509 assert(IFD->getDefinition() && "Category on a class without a definition?");
6510 ObjCClassesWithCategories.insert(
6511 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006512}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006513
Eli Friedman276dd182013-09-05 00:02:25 +00006514void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006515 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006516 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006517
6518 // If there is *any* declaration of the entity that's not from an AST file,
6519 // we can skip writing the update record. We make sure that isUsed() triggers
6520 // completion of the redeclaration chain of the entity.
6521 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6522 if (IsLocalDecl(Prev))
6523 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006524
Aaron Ballman4f45b712014-03-21 15:22:56 +00006525 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006526}
Alexey Bataev97720002014-11-11 04:05:39 +00006527
6528void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006529 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006530 assert(!WritingAST && "Already writing the AST!");
6531 if (!D->isFromASTFile())
6532 return;
6533
6534 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6535}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006536
Alexey Bataev25ed0c02019-03-07 17:54:44 +00006537void ASTWriter::DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) {
6538 if (Chain && Chain->isProcessingUpdateRecords()) return;
6539 assert(!WritingAST && "Already writing the AST!");
6540 if (!D->isFromASTFile())
6541 return;
6542
6543 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_ALLOCATE, A));
6544}
6545
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006546void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6547 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006548 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006549 assert(!WritingAST && "Already writing the AST!");
6550 if (!D->isFromASTFile())
6551 return;
6552
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006553 DeclUpdates[D].push_back(
6554 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006555}
6556
Richard Smith4caa4492015-05-15 02:34:32 +00006557void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006558 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006559 assert(!WritingAST && "Already writing the AST!");
6560 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006561 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006562}
Alex Denisovfde64952015-06-26 05:28:36 +00006563
6564void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6565 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006566 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006567 assert(!WritingAST && "Already writing the AST!");
6568 if (!Record->isFromASTFile())
6569 return;
6570 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6571}
Richard Smithc26d9742016-10-06 20:30:51 +00006572
6573void ASTWriter::AddedCXXTemplateSpecialization(
6574 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6575 assert(!WritingAST && "Already writing the AST!");
6576
6577 if (!TD->getFirstDecl()->isFromASTFile())
6578 return;
6579 if (Chain && Chain->isProcessingUpdateRecords())
6580 return;
6581
6582 DeclsToEmitEvenIfUnreferenced.push_back(D);
6583}
6584
6585void ASTWriter::AddedCXXTemplateSpecialization(
6586 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6587 assert(!WritingAST && "Already writing the AST!");
6588
6589 if (!TD->getFirstDecl()->isFromASTFile())
6590 return;
6591 if (Chain && Chain->isProcessingUpdateRecords())
6592 return;
6593
6594 DeclsToEmitEvenIfUnreferenced.push_back(D);
6595}
6596
6597void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6598 const FunctionDecl *D) {
6599 assert(!WritingAST && "Already writing the AST!");
6600
6601 if (!TD->getFirstDecl()->isFromASTFile())
6602 return;
6603 if (Chain && Chain->isProcessingUpdateRecords())
6604 return;
6605
6606 DeclsToEmitEvenIfUnreferenced.push_back(D);
6607}
Kelvin Libe286f52018-09-15 13:54:15 +00006608
6609//===----------------------------------------------------------------------===//
6610//// OMPClause Serialization
6611////===----------------------------------------------------------------------===//
6612
6613void OMPClauseWriter::writeClause(OMPClause *C) {
6614 Record.push_back(C->getClauseKind());
6615 Visit(C);
6616 Record.AddSourceLocation(C->getBeginLoc());
6617 Record.AddSourceLocation(C->getEndLoc());
6618}
6619
6620void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
6621 Record.push_back(C->getCaptureRegion());
6622 Record.AddStmt(C->getPreInitStmt());
6623}
6624
6625void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6626 VisitOMPClauseWithPreInit(C);
6627 Record.AddStmt(C->getPostUpdateExpr());
6628}
6629
6630void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6631 VisitOMPClauseWithPreInit(C);
6632 Record.push_back(C->getNameModifier());
6633 Record.AddSourceLocation(C->getNameModifierLoc());
6634 Record.AddSourceLocation(C->getColonLoc());
6635 Record.AddStmt(C->getCondition());
6636 Record.AddSourceLocation(C->getLParenLoc());
6637}
6638
6639void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
6640 Record.AddStmt(C->getCondition());
6641 Record.AddSourceLocation(C->getLParenLoc());
6642}
6643
6644void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6645 VisitOMPClauseWithPreInit(C);
6646 Record.AddStmt(C->getNumThreads());
6647 Record.AddSourceLocation(C->getLParenLoc());
6648}
6649
6650void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6651 Record.AddStmt(C->getSafelen());
6652 Record.AddSourceLocation(C->getLParenLoc());
6653}
6654
6655void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6656 Record.AddStmt(C->getSimdlen());
6657 Record.AddSourceLocation(C->getLParenLoc());
6658}
6659
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00006660void OMPClauseWriter::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
6661 Record.AddStmt(C->getAllocator());
6662 Record.AddSourceLocation(C->getLParenLoc());
6663}
6664
Kelvin Libe286f52018-09-15 13:54:15 +00006665void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6666 Record.AddStmt(C->getNumForLoops());
6667 Record.AddSourceLocation(C->getLParenLoc());
6668}
6669
6670void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
6671 Record.push_back(C->getDefaultKind());
6672 Record.AddSourceLocation(C->getLParenLoc());
6673 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6674}
6675
6676void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
6677 Record.push_back(C->getProcBindKind());
6678 Record.AddSourceLocation(C->getLParenLoc());
6679 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6680}
6681
6682void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6683 VisitOMPClauseWithPreInit(C);
6684 Record.push_back(C->getScheduleKind());
6685 Record.push_back(C->getFirstScheduleModifier());
6686 Record.push_back(C->getSecondScheduleModifier());
6687 Record.AddStmt(C->getChunkSize());
6688 Record.AddSourceLocation(C->getLParenLoc());
6689 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6690 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6691 Record.AddSourceLocation(C->getScheduleKindLoc());
6692 Record.AddSourceLocation(C->getCommaLoc());
6693}
6694
6695void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6696 Record.push_back(C->getLoopNumIterations().size());
6697 Record.AddStmt(C->getNumForLoops());
6698 for (Expr *NumIter : C->getLoopNumIterations())
6699 Record.AddStmt(NumIter);
6700 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
Mike Rice0ed46662018-09-20 17:19:41 +00006701 Record.AddStmt(C->getLoopCounter(I));
Kelvin Libe286f52018-09-15 13:54:15 +00006702 Record.AddSourceLocation(C->getLParenLoc());
6703}
6704
6705void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6706
6707void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6708
6709void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6710
6711void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6712
6713void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6714
6715void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *) {}
6716
6717void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6718
6719void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6720
6721void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6722
6723void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6724
6725void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6726
6727void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6728 Record.push_back(C->varlist_size());
6729 Record.AddSourceLocation(C->getLParenLoc());
6730 for (auto *VE : C->varlists()) {
6731 Record.AddStmt(VE);
6732 }
6733 for (auto *VE : C->private_copies()) {
6734 Record.AddStmt(VE);
6735 }
6736}
6737
6738void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6739 Record.push_back(C->varlist_size());
6740 VisitOMPClauseWithPreInit(C);
6741 Record.AddSourceLocation(C->getLParenLoc());
6742 for (auto *VE : C->varlists()) {
6743 Record.AddStmt(VE);
6744 }
6745 for (auto *VE : C->private_copies()) {
6746 Record.AddStmt(VE);
6747 }
6748 for (auto *VE : C->inits()) {
6749 Record.AddStmt(VE);
6750 }
6751}
6752
6753void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6754 Record.push_back(C->varlist_size());
6755 VisitOMPClauseWithPostUpdate(C);
6756 Record.AddSourceLocation(C->getLParenLoc());
6757 for (auto *VE : C->varlists())
6758 Record.AddStmt(VE);
6759 for (auto *E : C->private_copies())
6760 Record.AddStmt(E);
6761 for (auto *E : C->source_exprs())
6762 Record.AddStmt(E);
6763 for (auto *E : C->destination_exprs())
6764 Record.AddStmt(E);
6765 for (auto *E : C->assignment_ops())
6766 Record.AddStmt(E);
6767}
6768
6769void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6770 Record.push_back(C->varlist_size());
6771 Record.AddSourceLocation(C->getLParenLoc());
6772 for (auto *VE : C->varlists())
6773 Record.AddStmt(VE);
6774}
6775
6776void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6777 Record.push_back(C->varlist_size());
6778 VisitOMPClauseWithPostUpdate(C);
6779 Record.AddSourceLocation(C->getLParenLoc());
6780 Record.AddSourceLocation(C->getColonLoc());
6781 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6782 Record.AddDeclarationNameInfo(C->getNameInfo());
6783 for (auto *VE : C->varlists())
6784 Record.AddStmt(VE);
6785 for (auto *VE : C->privates())
6786 Record.AddStmt(VE);
6787 for (auto *E : C->lhs_exprs())
6788 Record.AddStmt(E);
6789 for (auto *E : C->rhs_exprs())
6790 Record.AddStmt(E);
6791 for (auto *E : C->reduction_ops())
6792 Record.AddStmt(E);
6793}
6794
6795void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6796 Record.push_back(C->varlist_size());
6797 VisitOMPClauseWithPostUpdate(C);
6798 Record.AddSourceLocation(C->getLParenLoc());
6799 Record.AddSourceLocation(C->getColonLoc());
6800 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6801 Record.AddDeclarationNameInfo(C->getNameInfo());
6802 for (auto *VE : C->varlists())
6803 Record.AddStmt(VE);
6804 for (auto *VE : C->privates())
6805 Record.AddStmt(VE);
6806 for (auto *E : C->lhs_exprs())
6807 Record.AddStmt(E);
6808 for (auto *E : C->rhs_exprs())
6809 Record.AddStmt(E);
6810 for (auto *E : C->reduction_ops())
6811 Record.AddStmt(E);
6812}
6813
6814void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6815 Record.push_back(C->varlist_size());
6816 VisitOMPClauseWithPostUpdate(C);
6817 Record.AddSourceLocation(C->getLParenLoc());
6818 Record.AddSourceLocation(C->getColonLoc());
6819 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6820 Record.AddDeclarationNameInfo(C->getNameInfo());
6821 for (auto *VE : C->varlists())
6822 Record.AddStmt(VE);
6823 for (auto *VE : C->privates())
6824 Record.AddStmt(VE);
6825 for (auto *E : C->lhs_exprs())
6826 Record.AddStmt(E);
6827 for (auto *E : C->rhs_exprs())
6828 Record.AddStmt(E);
6829 for (auto *E : C->reduction_ops())
6830 Record.AddStmt(E);
6831 for (auto *E : C->taskgroup_descriptors())
6832 Record.AddStmt(E);
6833}
6834
6835void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6836 Record.push_back(C->varlist_size());
6837 VisitOMPClauseWithPostUpdate(C);
6838 Record.AddSourceLocation(C->getLParenLoc());
6839 Record.AddSourceLocation(C->getColonLoc());
6840 Record.push_back(C->getModifier());
6841 Record.AddSourceLocation(C->getModifierLoc());
6842 for (auto *VE : C->varlists()) {
6843 Record.AddStmt(VE);
6844 }
6845 for (auto *VE : C->privates()) {
6846 Record.AddStmt(VE);
6847 }
6848 for (auto *VE : C->inits()) {
6849 Record.AddStmt(VE);
6850 }
6851 for (auto *VE : C->updates()) {
6852 Record.AddStmt(VE);
6853 }
6854 for (auto *VE : C->finals()) {
6855 Record.AddStmt(VE);
6856 }
6857 Record.AddStmt(C->getStep());
6858 Record.AddStmt(C->getCalcStep());
Alexey Bataev195ae902019-08-08 13:42:45 +00006859 for (auto *VE : C->used_expressions())
6860 Record.AddStmt(VE);
Kelvin Libe286f52018-09-15 13:54:15 +00006861}
6862
6863void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6864 Record.push_back(C->varlist_size());
6865 Record.AddSourceLocation(C->getLParenLoc());
6866 Record.AddSourceLocation(C->getColonLoc());
6867 for (auto *VE : C->varlists())
6868 Record.AddStmt(VE);
6869 Record.AddStmt(C->getAlignment());
6870}
6871
6872void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6873 Record.push_back(C->varlist_size());
6874 Record.AddSourceLocation(C->getLParenLoc());
6875 for (auto *VE : C->varlists())
6876 Record.AddStmt(VE);
6877 for (auto *E : C->source_exprs())
6878 Record.AddStmt(E);
6879 for (auto *E : C->destination_exprs())
6880 Record.AddStmt(E);
6881 for (auto *E : C->assignment_ops())
6882 Record.AddStmt(E);
6883}
6884
6885void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6886 Record.push_back(C->varlist_size());
6887 Record.AddSourceLocation(C->getLParenLoc());
6888 for (auto *VE : C->varlists())
6889 Record.AddStmt(VE);
6890 for (auto *E : C->source_exprs())
6891 Record.AddStmt(E);
6892 for (auto *E : C->destination_exprs())
6893 Record.AddStmt(E);
6894 for (auto *E : C->assignment_ops())
6895 Record.AddStmt(E);
6896}
6897
6898void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6899 Record.push_back(C->varlist_size());
6900 Record.AddSourceLocation(C->getLParenLoc());
6901 for (auto *VE : C->varlists())
6902 Record.AddStmt(VE);
6903}
6904
6905void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6906 Record.push_back(C->varlist_size());
6907 Record.push_back(C->getNumLoops());
6908 Record.AddSourceLocation(C->getLParenLoc());
6909 Record.push_back(C->getDependencyKind());
6910 Record.AddSourceLocation(C->getDependencyLoc());
6911 Record.AddSourceLocation(C->getColonLoc());
6912 for (auto *VE : C->varlists())
6913 Record.AddStmt(VE);
6914 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6915 Record.AddStmt(C->getLoopData(I));
6916}
6917
6918void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6919 VisitOMPClauseWithPreInit(C);
6920 Record.AddStmt(C->getDevice());
6921 Record.AddSourceLocation(C->getLParenLoc());
6922}
6923
6924void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6925 Record.push_back(C->varlist_size());
6926 Record.push_back(C->getUniqueDeclarationsNum());
6927 Record.push_back(C->getTotalComponentListNum());
6928 Record.push_back(C->getTotalComponentsNum());
6929 Record.AddSourceLocation(C->getLParenLoc());
Kelvin Lief579432018-12-18 22:18:41 +00006930 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
6931 Record.push_back(C->getMapTypeModifier(I));
6932 Record.AddSourceLocation(C->getMapTypeModifierLoc(I));
6933 }
Michael Kruse4304e9d2019-02-19 16:38:20 +00006934 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
6935 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00006936 Record.push_back(C->getMapType());
6937 Record.AddSourceLocation(C->getMapLoc());
6938 Record.AddSourceLocation(C->getColonLoc());
6939 for (auto *E : C->varlists())
6940 Record.AddStmt(E);
Michael Kruse4304e9d2019-02-19 16:38:20 +00006941 for (auto *E : C->mapperlists())
6942 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00006943 for (auto *D : C->all_decls())
6944 Record.AddDeclRef(D);
6945 for (auto N : C->all_num_lists())
6946 Record.push_back(N);
6947 for (auto N : C->all_lists_sizes())
6948 Record.push_back(N);
6949 for (auto &M : C->all_components()) {
6950 Record.AddStmt(M.getAssociatedExpression());
6951 Record.AddDeclRef(M.getAssociatedDeclaration());
6952 }
6953}
6954
Alexey Bataeve04483e2019-03-27 14:14:31 +00006955void OMPClauseWriter::VisitOMPAllocateClause(OMPAllocateClause *C) {
6956 Record.push_back(C->varlist_size());
6957 Record.AddSourceLocation(C->getLParenLoc());
6958 Record.AddSourceLocation(C->getColonLoc());
6959 Record.AddStmt(C->getAllocator());
6960 for (auto *VE : C->varlists())
6961 Record.AddStmt(VE);
6962}
6963
Kelvin Libe286f52018-09-15 13:54:15 +00006964void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6965 VisitOMPClauseWithPreInit(C);
6966 Record.AddStmt(C->getNumTeams());
6967 Record.AddSourceLocation(C->getLParenLoc());
6968}
6969
6970void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6971 VisitOMPClauseWithPreInit(C);
6972 Record.AddStmt(C->getThreadLimit());
6973 Record.AddSourceLocation(C->getLParenLoc());
6974}
6975
6976void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
6977 Record.AddStmt(C->getPriority());
6978 Record.AddSourceLocation(C->getLParenLoc());
6979}
6980
6981void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
6982 Record.AddStmt(C->getGrainsize());
6983 Record.AddSourceLocation(C->getLParenLoc());
6984}
6985
6986void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
6987 Record.AddStmt(C->getNumTasks());
6988 Record.AddSourceLocation(C->getLParenLoc());
6989}
6990
6991void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
6992 Record.AddStmt(C->getHint());
6993 Record.AddSourceLocation(C->getLParenLoc());
6994}
6995
6996void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
6997 VisitOMPClauseWithPreInit(C);
6998 Record.push_back(C->getDistScheduleKind());
6999 Record.AddStmt(C->getChunkSize());
7000 Record.AddSourceLocation(C->getLParenLoc());
7001 Record.AddSourceLocation(C->getDistScheduleKindLoc());
7002 Record.AddSourceLocation(C->getCommaLoc());
7003}
7004
7005void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
7006 Record.push_back(C->getDefaultmapKind());
7007 Record.push_back(C->getDefaultmapModifier());
7008 Record.AddSourceLocation(C->getLParenLoc());
7009 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
7010 Record.AddSourceLocation(C->getDefaultmapKindLoc());
7011}
7012
7013void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
7014 Record.push_back(C->varlist_size());
7015 Record.push_back(C->getUniqueDeclarationsNum());
7016 Record.push_back(C->getTotalComponentListNum());
7017 Record.push_back(C->getTotalComponentsNum());
7018 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse01f670d2019-02-22 22:29:42 +00007019 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7020 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00007021 for (auto *E : C->varlists())
7022 Record.AddStmt(E);
Michael Kruse01f670d2019-02-22 22:29:42 +00007023 for (auto *E : C->mapperlists())
7024 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00007025 for (auto *D : C->all_decls())
7026 Record.AddDeclRef(D);
7027 for (auto N : C->all_num_lists())
7028 Record.push_back(N);
7029 for (auto N : C->all_lists_sizes())
7030 Record.push_back(N);
7031 for (auto &M : C->all_components()) {
7032 Record.AddStmt(M.getAssociatedExpression());
7033 Record.AddDeclRef(M.getAssociatedDeclaration());
7034 }
7035}
7036
7037void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
7038 Record.push_back(C->varlist_size());
7039 Record.push_back(C->getUniqueDeclarationsNum());
7040 Record.push_back(C->getTotalComponentListNum());
7041 Record.push_back(C->getTotalComponentsNum());
7042 Record.AddSourceLocation(C->getLParenLoc());
Michael Kruse0336c752019-02-25 20:34:15 +00007043 Record.AddNestedNameSpecifierLoc(C->getMapperQualifierLoc());
7044 Record.AddDeclarationNameInfo(C->getMapperIdInfo());
Kelvin Libe286f52018-09-15 13:54:15 +00007045 for (auto *E : C->varlists())
7046 Record.AddStmt(E);
Michael Kruse0336c752019-02-25 20:34:15 +00007047 for (auto *E : C->mapperlists())
7048 Record.AddStmt(E);
Kelvin Libe286f52018-09-15 13:54:15 +00007049 for (auto *D : C->all_decls())
7050 Record.AddDeclRef(D);
7051 for (auto N : C->all_num_lists())
7052 Record.push_back(N);
7053 for (auto N : C->all_lists_sizes())
7054 Record.push_back(N);
7055 for (auto &M : C->all_components()) {
7056 Record.AddStmt(M.getAssociatedExpression());
7057 Record.AddDeclRef(M.getAssociatedDeclaration());
7058 }
7059}
7060
7061void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
7062 Record.push_back(C->varlist_size());
7063 Record.push_back(C->getUniqueDeclarationsNum());
7064 Record.push_back(C->getTotalComponentListNum());
7065 Record.push_back(C->getTotalComponentsNum());
7066 Record.AddSourceLocation(C->getLParenLoc());
7067 for (auto *E : C->varlists())
7068 Record.AddStmt(E);
7069 for (auto *VE : C->private_copies())
7070 Record.AddStmt(VE);
7071 for (auto *VE : C->inits())
7072 Record.AddStmt(VE);
7073 for (auto *D : C->all_decls())
7074 Record.AddDeclRef(D);
7075 for (auto N : C->all_num_lists())
7076 Record.push_back(N);
7077 for (auto N : C->all_lists_sizes())
7078 Record.push_back(N);
7079 for (auto &M : C->all_components()) {
7080 Record.AddStmt(M.getAssociatedExpression());
7081 Record.AddDeclRef(M.getAssociatedDeclaration());
7082 }
7083}
7084
7085void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
7086 Record.push_back(C->varlist_size());
7087 Record.push_back(C->getUniqueDeclarationsNum());
7088 Record.push_back(C->getTotalComponentListNum());
7089 Record.push_back(C->getTotalComponentsNum());
7090 Record.AddSourceLocation(C->getLParenLoc());
7091 for (auto *E : C->varlists())
7092 Record.AddStmt(E);
7093 for (auto *D : C->all_decls())
7094 Record.AddDeclRef(D);
7095 for (auto N : C->all_num_lists())
7096 Record.push_back(N);
7097 for (auto N : C->all_lists_sizes())
7098 Record.push_back(N);
7099 for (auto &M : C->all_components()) {
7100 Record.AddStmt(M.getAssociatedExpression());
7101 Record.AddDeclRef(M.getAssociatedDeclaration());
7102 }
7103}
Kelvin Li1408f912018-09-26 04:28:39 +00007104
7105void OMPClauseWriter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
Patrick Lyster4a370b92018-10-01 13:47:43 +00007106
7107void OMPClauseWriter::VisitOMPUnifiedSharedMemoryClause(
7108 OMPUnifiedSharedMemoryClause *) {}
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00007109
7110void OMPClauseWriter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
Patrick Lyster3fe9e392018-10-11 14:41:10 +00007111
7112void
7113OMPClauseWriter::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
7114}
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00007115
7116void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
7117 OMPAtomicDefaultMemOrderClause *C) {
7118 Record.push_back(C->getAtomicDefaultMemOrderKind());
7119 Record.AddSourceLocation(C->getLParenLoc());
7120 Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
7121}