blob: 29c3c0ba918aad9ff5e3e8882191004df65d2a08 [file] [log] [blame]
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001//===- ASTWriter.cpp - AST File Writer ------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000016#include "ASTReaderInternals.h"
17#include "MultiOnDiskHashTable.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000019#include "clang/AST/ASTUnresolvedSet.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000020#include "clang/AST/Attr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Decl.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000022#include "clang/AST/DeclBase.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000023#include "clang/AST/DeclCXX.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000024#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000025#include "clang/AST/DeclFriend.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000026#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "clang/AST/DeclTemplate.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000028#include "clang/AST/DeclarationName.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000029#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000030#include "clang/AST/ExprCXX.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000031#include "clang/AST/LambdaCapture.h"
32#include "clang/AST/NestedNameSpecifier.h"
33#include "clang/AST/RawCommentList.h"
34#include "clang/AST/TemplateName.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000036#include "clang/AST/TypeLocVisitor.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000037#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000038#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000039#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000040#include "clang/Basic/FileSystemOptions.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000041#include "clang/Basic/IdentifierTable.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000042#include "clang/Basic/LLVM.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000043#include "clang/Basic/Lambda.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000044#include "clang/Basic/LangOptions.h"
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000045#include "clang/Basic/MemoryBufferCache.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000046#include "clang/Basic/Module.h"
47#include "clang/Basic/ObjCRuntime.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000048#include "clang/Basic/OpenCLOptions.h"
49#include "clang/Basic/SourceLocation.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000050#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000051#include "clang/Basic/SourceManagerInternals.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000052#include "clang/Basic/Specifiers.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000053#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000054#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000055#include "clang/Basic/Version.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000056#include "clang/Lex/HeaderSearch.h"
57#include "clang/Lex/HeaderSearchOptions.h"
58#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000059#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000060#include "clang/Lex/PreprocessingRecord.h"
61#include "clang/Lex/Preprocessor.h"
62#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000063#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000064#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000065#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000066#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000067#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000068#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000069#include "clang/Serialization/Module.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000070#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000071#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000072#include "llvm/ADT/APFloat.h"
73#include "llvm/ADT/APInt.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000074#include "llvm/ADT/APSInt.h"
75#include "llvm/ADT/ArrayRef.h"
76#include "llvm/ADT/DenseMap.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000077#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000078#include "llvm/ADT/Optional.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000079#include "llvm/ADT/PointerIntPair.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000080#include "llvm/ADT/STLExtras.h"
Ilya Biryukov45643102018-07-09 11:33:23 +000081#include "llvm/ADT/ScopeExit.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000082#include "llvm/ADT/SmallSet.h"
83#include "llvm/ADT/SmallString.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000084#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000085#include "llvm/ADT/StringMap.h"
86#include "llvm/ADT/StringRef.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000087#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000089#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000090#include "llvm/Support/Compression.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000091#include "llvm/Support/DJB.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000092#include "llvm/Support/Endian.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000093#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000094#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000095#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000096#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000097#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000098#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000099#include "llvm/Support/SHA1.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000100#include "llvm/Support/VersionTuple.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000101#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +0000102#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000103#include <cassert>
104#include <cstdint>
105#include <cstdlib>
106#include <cstring>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000107#include <ctime>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000108#include <deque>
109#include <limits>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000110#include <memory>
111#include <queue>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000112#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +0000113#include <utility>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000114#include <vector>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000115
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +0000117using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000118
Sebastian Redl3df5a082010-07-30 17:03:48 +0000119template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +0000120static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000121 if (v.empty()) return StringRef();
122 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000123 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000124}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000125
126template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000127static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000128 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000129 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000130}
131
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132//===----------------------------------------------------------------------===//
133// Type serialization
134//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000135
Richard Smith290d8012016-04-06 17:06:00 +0000136namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000139 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000140 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000142 /// Type code that corresponds to the record generated.
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000143 TypeCode Code = static_cast<TypeCode>(0);
144
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000145 /// Abbreviation to use for the record, if any.
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000146 unsigned AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147
Richard Smith290d8012016-04-06 17:06:00 +0000148 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000149 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000150 : Writer(Writer), Record(Writer, Record) {}
Richard Smith290d8012016-04-06 17:06:00 +0000151
152 uint64_t Emit() {
153 return Record.Emit(Code, AbbrevToUse);
154 }
155
156 void Visit(QualType T) {
157 if (T.hasLocalNonFastQualifiers()) {
158 Qualifiers Qs = T.getLocalQualifiers();
159 Record.AddTypeRef(T.getLocalUnqualifiedType());
160 Record.push_back(Qs.getAsOpaqueValue());
161 Code = TYPE_EXT_QUAL;
162 AbbrevToUse = Writer.TypeExtQualAbbrev;
163 } else {
164 switch (T->getTypeClass()) {
165 // For all of the concrete, non-dependent types, call the
166 // appropriate visitor function.
167#define TYPE(Class, Base) \
168 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
169#define ABSTRACT_TYPE(Class, Base)
170#include "clang/AST/TypeNodes.def"
171 }
172 }
173 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174
175 void VisitArrayType(const ArrayType *T);
176 void VisitFunctionType(const FunctionType *T);
177 void VisitTagType(const TagType *T);
178
179#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
180#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181#include "clang/AST/TypeNodes.def"
182 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000183
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000184} // namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000187 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000188}
189
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000190void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000191 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000192 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193}
194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000196 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000197 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198}
199
Reid Kleckner8a365022013-06-24 17:51:48 +0000200void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000201 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000202 Code = TYPE_DECAYED;
203}
204
Reid Kleckner0503a872013-12-05 01:23:43 +0000205void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000206 Record.AddTypeRef(T->getOriginalType());
207 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000208 Code = TYPE_ADJUSTED;
209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000212 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000217 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000218 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000223 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000228 Record.AddTypeRef(T->getPointeeType());
229 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231}
232
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000234 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000236 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237}
238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000241 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000242 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243}
244
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000245void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248}
249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000252 Record.AddSourceLocation(T->getLBracketLoc());
253 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000254 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000255 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256}
257
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000258void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000259 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000261 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000266 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000267 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000268}
269
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000271 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000272 FunctionType::ExtInfo C = T->getExtInfo();
273 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000274 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000275 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000276 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000277 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000278 Record.push_back(C.getProducesResult());
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000279 Record.push_back(C.getNoCallerSavedRegs());
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000280 Record.push_back(C.getNoCfCheck());
Richard Smith01b2cb42014-07-26 06:37:51 +0000281
282 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
283 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000284}
285
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000287 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000288 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000289}
290
Richard Smith290d8012016-04-06 17:06:00 +0000291static void addExceptionSpec(const FunctionProtoType *T,
292 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000293 Record.push_back(T->getExceptionSpecType());
294 if (T->getExceptionSpecType() == EST_Dynamic) {
295 Record.push_back(T->getNumExceptions());
296 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000297 Record.AddTypeRef(T->getExceptionType(I));
Richard Smitheaf11ad2018-05-03 03:58:32 +0000298 } else if (isComputedNoexcept(T->getExceptionSpecType())) {
Richard Smith290d8012016-04-06 17:06:00 +0000299 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000300 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000301 Record.AddDeclRef(T->getExceptionSpecDecl());
302 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000303 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000304 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000305 }
Richard Smith564417a2014-03-20 21:47:22 +0000306}
307
308void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
309 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000310
Richard Smith564417a2014-03-20 21:47:22 +0000311 Record.push_back(T->isVariadic());
312 Record.push_back(T->hasTrailingReturn());
313 Record.push_back(T->getTypeQuals());
314 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000315 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000316
317 Record.push_back(T->getNumParams());
318 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000319 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000320
John McCall18afab72016-03-01 00:49:02 +0000321 if (T->hasExtParameterInfos()) {
322 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
323 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
324 }
325
Richard Smith01b2cb42014-07-26 06:37:51 +0000326 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000327 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
328 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000329 AbbrevToUse = 0;
330
Sebastian Redl539c5062010-08-18 23:57:32 +0000331 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000332}
333
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000334void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000335 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000337}
John McCallb96ec562009-12-04 22:46:56 +0000338
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000339void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000340 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000341 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000342 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000343 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000344}
345
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000347 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000352 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000354}
355
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000357 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000358 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000359 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000360}
361
Alexis Hunte852b102011-05-24 22:41:36 +0000362void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000363 Record.AddTypeRef(T->getBaseType());
364 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000365 Record.push_back(T->getUTTKind());
366 Code = TYPE_UNARY_TRANSFORM;
367}
368
Richard Smith30482bc2011-02-20 03:19:35 +0000369void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000370 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000371 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000372 if (T->getDeducedType().isNull())
373 Record.push_back(T->isDependentType());
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
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000520void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000521 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000522 Record.AddNestedNameSpecifier(T->getQualifier());
523 Record.AddTypeRef(T->getNamedType());
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000524 Record.AddDeclRef(T->getOwnedTagDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000525 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000526}
527
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000528void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000529 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
530 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000531 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000532}
533
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000534void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000535 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000536 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000537}
538
Manman Rene6be26c2016-09-13 17:25:08 +0000539void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
540 Record.AddDeclRef(T->getDecl());
541 Record.push_back(T->getNumProtocols());
542 for (const auto *I : T->quals())
543 Record.AddDeclRef(I);
544 Code = TYPE_OBJC_TYPE_PARAM;
545}
546
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000547void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000548 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000549 Record.push_back(T->getTypeArgsAsWritten().size());
550 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000551 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000552 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000553 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000554 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000555 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000556 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000557}
558
Steve Narofffb4330f2009-06-17 22:40:22 +0000559void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000560ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000561 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000562 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000563}
564
Eli Friedman0dfb8892011-10-06 23:00:33 +0000565void
566ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000567 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000568 Code = TYPE_ATOMIC;
569}
570
Xiuli Pan9c14e282016-01-09 12:53:17 +0000571void
572ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000573 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000574 Record.push_back(T->isReadOnly());
575 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000576}
577
John McCall8f115c62009-10-16 21:56:05 +0000578namespace {
579
580class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000581 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000582
583public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000584 TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {}
John McCall8f115c62009-10-16 21:56:05 +0000585
John McCall17001972009-10-18 01:05:36 +0000586#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000587#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000588 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000589#include "clang/AST/TypeLocNodes.def"
590
John McCall17001972009-10-18 01:05:36 +0000591 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
592 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000593};
594
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000595} // namespace
John McCall8f115c62009-10-16 21:56:05 +0000596
John McCall17001972009-10-18 01:05:36 +0000597void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
598 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000599}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000600
John McCall17001972009-10-18 01:05:36 +0000601void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000602 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000603 if (TL.needsExtraLocalData()) {
Faisal Vali090da2d2018-01-01 18:23:28 +0000604 Record.push_back(TL.getWrittenTypeSpec());
605 Record.push_back(TL.getWrittenSignSpec());
606 Record.push_back(TL.getWrittenWidthSpec());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000607 Record.push_back(TL.hasModeAttr());
608 }
John McCall8f115c62009-10-16 21:56:05 +0000609}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000610
John McCall17001972009-10-18 01:05:36 +0000611void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000612 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000613}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000614
John McCall17001972009-10-18 01:05:36 +0000615void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000616 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000617}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000618
Reid Kleckner8a365022013-06-24 17:51:48 +0000619void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
620 // nothing to do
621}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000622
Reid Kleckner0503a872013-12-05 01:23:43 +0000623void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
624 // nothing to do
625}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000626
John McCall17001972009-10-18 01:05:36 +0000627void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000628 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000629}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000630
John McCall17001972009-10-18 01:05:36 +0000631void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000632 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000633}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000634
John McCall17001972009-10-18 01:05:36 +0000635void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000636 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000637}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000638
John McCall17001972009-10-18 01:05:36 +0000639void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000640 Record.AddSourceLocation(TL.getStarLoc());
641 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000642}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000643
John McCall17001972009-10-18 01:05:36 +0000644void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000645 Record.AddSourceLocation(TL.getLBracketLoc());
646 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000647 Record.push_back(TL.getSizeExpr() ? 1 : 0);
648 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000649 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000650}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000651
John McCall17001972009-10-18 01:05:36 +0000652void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
653 VisitArrayTypeLoc(TL);
654}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000655
John McCall17001972009-10-18 01:05:36 +0000656void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
657 VisitArrayTypeLoc(TL);
658}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000659
John McCall17001972009-10-18 01:05:36 +0000660void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
661 VisitArrayTypeLoc(TL);
662}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000663
John McCall17001972009-10-18 01:05:36 +0000664void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
665 DependentSizedArrayTypeLoc TL) {
666 VisitArrayTypeLoc(TL);
667}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000668
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000669void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
670 DependentAddressSpaceTypeLoc TL) {
671 Record.AddSourceLocation(TL.getAttrNameLoc());
672 SourceRange range = TL.getAttrOperandParensRange();
673 Record.AddSourceLocation(range.getBegin());
674 Record.AddSourceLocation(range.getEnd());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000675 Record.AddStmt(TL.getAttrExprOperand());
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000676}
677
John McCall17001972009-10-18 01:05:36 +0000678void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
679 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000680 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000681}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000682
John McCall17001972009-10-18 01:05:36 +0000683void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000684 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000685}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000686
Erich Keanef702b022018-07-13 19:46:04 +0000687void TypeLocWriter::VisitDependentVectorTypeLoc(
688 DependentVectorTypeLoc TL) {
689 Record.AddSourceLocation(TL.getNameLoc());
690}
691
John McCall17001972009-10-18 01:05:36 +0000692void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000693 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000694}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000695
John McCall17001972009-10-18 01:05:36 +0000696void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000697 Record.AddSourceLocation(TL.getLocalRangeBegin());
698 Record.AddSourceLocation(TL.getLParenLoc());
699 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000700 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000701 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000702 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000703 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000704}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000705
John McCall17001972009-10-18 01:05:36 +0000706void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
707 VisitFunctionTypeLoc(TL);
708}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000709
John McCall17001972009-10-18 01:05:36 +0000710void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
711 VisitFunctionTypeLoc(TL);
712}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000713
John McCallb96ec562009-12-04 22:46:56 +0000714void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000715 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000716}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000717
John McCall17001972009-10-18 01:05:36 +0000718void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000719 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000720}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000721
Manman Rene6be26c2016-09-13 17:25:08 +0000722void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
723 if (TL.getNumProtocols()) {
724 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
725 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
726 }
727 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
728 Record.AddSourceLocation(TL.getProtocolLoc(i));
729}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000730
John McCall17001972009-10-18 01:05:36 +0000731void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000732 Record.AddSourceLocation(TL.getTypeofLoc());
733 Record.AddSourceLocation(TL.getLParenLoc());
734 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000735}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000736
John McCall17001972009-10-18 01:05:36 +0000737void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000738 Record.AddSourceLocation(TL.getTypeofLoc());
739 Record.AddSourceLocation(TL.getLParenLoc());
740 Record.AddSourceLocation(TL.getRParenLoc());
741 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000742}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000743
John McCall17001972009-10-18 01:05:36 +0000744void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000745 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000746}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000747
Alexis Hunte852b102011-05-24 22:41:36 +0000748void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000749 Record.AddSourceLocation(TL.getKWLoc());
750 Record.AddSourceLocation(TL.getLParenLoc());
751 Record.AddSourceLocation(TL.getRParenLoc());
752 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000753}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000754
Richard Smith30482bc2011-02-20 03:19:35 +0000755void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000756 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000757}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000758
Richard Smith600b5262017-01-26 20:40:47 +0000759void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
760 DeducedTemplateSpecializationTypeLoc TL) {
761 Record.AddSourceLocation(TL.getTemplateNameLoc());
762}
763
John McCall17001972009-10-18 01:05:36 +0000764void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000765 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000766}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000767
John McCall17001972009-10-18 01:05:36 +0000768void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000769 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000770}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000771
John McCall81904512011-01-06 01:58:22 +0000772void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smithe43e2b32018-08-20 21:47:29 +0000773 Record.AddAttr(TL.getAttr());
John McCall81904512011-01-06 01:58:22 +0000774}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000775
John McCall17001972009-10-18 01:05:36 +0000776void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000777 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000778}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000779
John McCallcebee162009-10-18 09:09:24 +0000780void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
781 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000782 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000783}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000784
Douglas Gregorada4b792011-01-14 02:55:32 +0000785void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
786 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000787 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000788}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000789
John McCall17001972009-10-18 01:05:36 +0000790void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
791 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000792 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
793 Record.AddSourceLocation(TL.getTemplateNameLoc());
794 Record.AddSourceLocation(TL.getLAngleLoc());
795 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000796 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000797 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
798 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000799}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000800
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000801void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000802 Record.AddSourceLocation(TL.getLParenLoc());
803 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000804}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000805
Abramo Bagnara6150c882010-05-11 21:36:43 +0000806void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000807 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
808 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000809}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000810
John McCalle78aac42010-03-10 03:28:59 +0000811void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000812 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000813}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000814
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000815void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000816 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
817 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
818 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000819}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000820
John McCallc392f372010-06-11 00:33:02 +0000821void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
822 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000823 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
824 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
825 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
826 Record.AddSourceLocation(TL.getTemplateNameLoc());
827 Record.AddSourceLocation(TL.getLAngleLoc());
828 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000829 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000830 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
831 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000832}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000833
Douglas Gregord2fa7662010-12-20 02:24:11 +0000834void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000835 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000836}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000837
John McCall17001972009-10-18 01:05:36 +0000838void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000839 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000840}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000841
John McCall8b07ec22010-05-15 11:32:37 +0000842void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
843 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000844 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
845 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000846 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000847 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
848 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
849 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000850 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000851 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000852}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000853
John McCallfc93cf92009-10-22 22:37:11 +0000854void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000855 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000856}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000857
Eli Friedman0dfb8892011-10-06 23:00:33 +0000858void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000859 Record.AddSourceLocation(TL.getKWLoc());
860 Record.AddSourceLocation(TL.getLParenLoc());
861 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000862}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000863
Xiuli Pan9c14e282016-01-09 12:53:17 +0000864void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000865 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000866}
John McCall8f115c62009-10-16 21:56:05 +0000867
Richard Smith01b2cb42014-07-26 06:37:51 +0000868void ASTWriter::WriteTypeAbbrevs() {
869 using namespace llvm;
870
David Blaikieb44f0bf2017-01-04 22:36:43 +0000871 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000872
873 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000874 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000875 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
876 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
877 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000878 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000879
880 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000881 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000882 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
883 // FunctionType
884 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
885 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
886 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
887 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
888 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
889 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000890 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000891 Abv->Add(BitCodeAbbrevOp(0)); // NoCfCheck
Richard Smith01b2cb42014-07-26 06:37:51 +0000892 // FunctionProtoType
893 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
894 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
895 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
896 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
897 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
898 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
899 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
900 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000901 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000902}
903
Chris Lattner19cea4e2009-04-22 05:57:30 +0000904//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000905// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000906//===----------------------------------------------------------------------===//
907
Chris Lattner28fa4e62009-04-26 22:26:21 +0000908static void EmitBlockID(unsigned ID, const char *Name,
909 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000910 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000911 Record.clear();
912 Record.push_back(ID);
913 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
914
915 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000916 if (!Name || Name[0] == 0)
917 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000918 Record.clear();
919 while (*Name)
920 Record.push_back(*Name++);
921 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
922}
923
924static void EmitRecordID(unsigned ID, const char *Name,
925 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000926 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000927 Record.clear();
928 Record.push_back(ID);
929 while (*Name)
930 Record.push_back(*Name++);
931 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000932}
933
934static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000935 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000936#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000937 RECORD(STMT_STOP);
938 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000939 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000940 RECORD(STMT_NULL);
941 RECORD(STMT_COMPOUND);
942 RECORD(STMT_CASE);
943 RECORD(STMT_DEFAULT);
944 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000945 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000946 RECORD(STMT_IF);
947 RECORD(STMT_SWITCH);
948 RECORD(STMT_WHILE);
949 RECORD(STMT_DO);
950 RECORD(STMT_FOR);
951 RECORD(STMT_GOTO);
952 RECORD(STMT_INDIRECT_GOTO);
953 RECORD(STMT_CONTINUE);
954 RECORD(STMT_BREAK);
955 RECORD(STMT_RETURN);
956 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000957 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000958 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000959 RECORD(EXPR_PREDEFINED);
960 RECORD(EXPR_DECL_REF);
961 RECORD(EXPR_INTEGER_LITERAL);
962 RECORD(EXPR_FLOATING_LITERAL);
963 RECORD(EXPR_IMAGINARY_LITERAL);
964 RECORD(EXPR_STRING_LITERAL);
965 RECORD(EXPR_CHARACTER_LITERAL);
966 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000967 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000968 RECORD(EXPR_UNARY_OPERATOR);
969 RECORD(EXPR_SIZEOF_ALIGN_OF);
970 RECORD(EXPR_ARRAY_SUBSCRIPT);
971 RECORD(EXPR_CALL);
972 RECORD(EXPR_MEMBER);
973 RECORD(EXPR_BINARY_OPERATOR);
974 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
975 RECORD(EXPR_CONDITIONAL_OPERATOR);
976 RECORD(EXPR_IMPLICIT_CAST);
977 RECORD(EXPR_CSTYLE_CAST);
978 RECORD(EXPR_COMPOUND_LITERAL);
979 RECORD(EXPR_EXT_VECTOR_ELEMENT);
980 RECORD(EXPR_INIT_LIST);
981 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000982 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000983 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000984 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000985 RECORD(EXPR_VA_ARG);
986 RECORD(EXPR_ADDR_LABEL);
987 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000988 RECORD(EXPR_CHOOSE);
989 RECORD(EXPR_GNU_NULL);
990 RECORD(EXPR_SHUFFLE_VECTOR);
991 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000992 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000993 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000994 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000995 RECORD(EXPR_OBJC_ARRAY_LITERAL);
996 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000997 RECORD(EXPR_OBJC_ENCODE);
998 RECORD(EXPR_OBJC_SELECTOR_EXPR);
999 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
1000 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
1001 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
1002 RECORD(EXPR_OBJC_KVC_REF_EXPR);
1003 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001004 RECORD(STMT_OBJC_FOR_COLLECTION);
1005 RECORD(STMT_OBJC_CATCH);
1006 RECORD(STMT_OBJC_FINALLY);
1007 RECORD(STMT_OBJC_AT_TRY);
1008 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
1009 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001010 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001011 RECORD(STMT_CXX_CATCH);
1012 RECORD(STMT_CXX_TRY);
1013 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001014 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001015 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001016 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001017 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001018 RECORD(EXPR_CXX_STATIC_CAST);
1019 RECORD(EXPR_CXX_DYNAMIC_CAST);
1020 RECORD(EXPR_CXX_REINTERPRET_CAST);
1021 RECORD(EXPR_CXX_CONST_CAST);
1022 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +00001023 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +00001024 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001025 RECORD(EXPR_CXX_BOOL_LITERAL);
1026 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001027 RECORD(EXPR_CXX_TYPEID_EXPR);
1028 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001029 RECORD(EXPR_CXX_THIS);
1030 RECORD(EXPR_CXX_THROW);
1031 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +00001032 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001033 RECORD(EXPR_CXX_BIND_TEMPORARY);
1034 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
1035 RECORD(EXPR_CXX_NEW);
1036 RECORD(EXPR_CXX_DELETE);
1037 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
1038 RECORD(EXPR_EXPR_WITH_CLEANUPS);
1039 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
1040 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
1041 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
1042 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
1043 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +00001044 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001045 RECORD(EXPR_CXX_NOEXCEPT);
1046 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +00001047 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
1048 RECORD(EXPR_TYPE_TRAIT);
1049 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001050 RECORD(EXPR_PACK_EXPANSION);
1051 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001052 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001053 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001054 RECORD(EXPR_FUNCTION_PARM_PACK);
1055 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001056 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001057 RECORD(EXPR_CXX_UUIDOF_EXPR);
1058 RECORD(EXPR_CXX_UUIDOF_TYPE);
1059 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001060#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001061}
Mike Stump11289f42009-09-09 15:08:12 +00001062
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001063void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001064 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001065 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001066
Sebastian Redl539c5062010-08-18 23:57:32 +00001067#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1068#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001069
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001070 // Control Block.
1071 BLOCK(CONTROL_BLOCK);
1072 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001073 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001074 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001075 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001076 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001077 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001078 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001079 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001080 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001081
1082 BLOCK(OPTIONS_BLOCK);
1083 RECORD(LANGUAGE_OPTIONS);
1084 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001085 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001086 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001087 RECORD(PREPROCESSOR_OPTIONS);
1088
Douglas Gregor108cb222012-10-19 00:45:00 +00001089 BLOCK(INPUT_FILES_BLOCK);
1090 RECORD(INPUT_FILE);
1091
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001092 // AST Top-Level Block.
1093 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001094 RECORD(TYPE_OFFSET);
1095 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001096 RECORD(IDENTIFIER_OFFSET);
1097 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001098 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001099 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001100 RECORD(SPECIAL_TYPES);
1101 RECORD(STATISTICS);
1102 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001103 RECORD(SELECTOR_OFFSETS);
1104 RECORD(METHOD_POOL);
1105 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001106 RECORD(SOURCE_LOCATION_OFFSETS);
1107 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001108 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001109 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001110 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001111 RECORD(VTABLE_USES);
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00001112 RECORD(PPD_SKIPPED_RANGES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001113 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001114 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001115 RECORD(SEMA_DECL_REFS);
1116 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1117 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001118 RECORD(UPDATE_VISIBLE);
1119 RECORD(DECL_UPDATE_OFFSETS);
1120 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001121 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001122 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001123 RECORD(FP_PRAGMA_OPTIONS);
1124 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001125 RECORD(OPENCL_EXTENSION_TYPES);
1126 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001127 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001128 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001129 RECORD(MODULE_OFFSET_MAP);
1130 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001131 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001132 RECORD(FILE_SORTED_DECLS);
1133 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001134 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001135 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001136 RECORD(INTERESTING_IDENTIFIERS);
1137 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001138 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001139 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001140 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001141 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001142 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001143 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001144 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00001145 RECORD(PP_CONDITIONAL_STACK);
Douglas Gregor358cd442012-01-15 16:58:34 +00001146
Chris Lattner28fa4e62009-04-26 22:26:21 +00001147 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001148 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001149 RECORD(SM_SLOC_FILE_ENTRY);
1150 RECORD(SM_SLOC_BUFFER_ENTRY);
1151 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001152 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001153 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001154
Chris Lattner28fa4e62009-04-26 22:26:21 +00001155 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001156 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001157 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001158 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001159 RECORD(PP_MACRO_OBJECT_LIKE);
1160 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001161 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001162
Richard Smithfa715652015-08-31 22:43:10 +00001163 // Submodule Block.
1164 BLOCK(SUBMODULE_BLOCK);
1165 RECORD(SUBMODULE_METADATA);
1166 RECORD(SUBMODULE_DEFINITION);
1167 RECORD(SUBMODULE_UMBRELLA_HEADER);
1168 RECORD(SUBMODULE_HEADER);
1169 RECORD(SUBMODULE_TOPHEADER);
1170 RECORD(SUBMODULE_UMBRELLA_DIR);
1171 RECORD(SUBMODULE_IMPORTS);
1172 RECORD(SUBMODULE_EXPORTS);
1173 RECORD(SUBMODULE_REQUIRES);
1174 RECORD(SUBMODULE_EXCLUDED_HEADER);
1175 RECORD(SUBMODULE_LINK_LIBRARY);
1176 RECORD(SUBMODULE_CONFIG_MACRO);
1177 RECORD(SUBMODULE_CONFLICT);
1178 RECORD(SUBMODULE_PRIVATE_HEADER);
1179 RECORD(SUBMODULE_TEXTUAL_HEADER);
1180 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001181 RECORD(SUBMODULE_INITIALIZERS);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00001182 RECORD(SUBMODULE_EXPORT_AS);
Richard Smithfa715652015-08-31 22:43:10 +00001183
1184 // Comments Block.
1185 BLOCK(COMMENTS_BLOCK);
1186 RECORD(COMMENTS_RAW_COMMENT);
1187
Douglas Gregor12bfa382009-10-17 00:13:19 +00001188 // Decls and Types block.
1189 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001190 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001191 RECORD(TYPE_COMPLEX);
1192 RECORD(TYPE_POINTER);
1193 RECORD(TYPE_BLOCK_POINTER);
1194 RECORD(TYPE_LVALUE_REFERENCE);
1195 RECORD(TYPE_RVALUE_REFERENCE);
1196 RECORD(TYPE_MEMBER_POINTER);
1197 RECORD(TYPE_CONSTANT_ARRAY);
1198 RECORD(TYPE_INCOMPLETE_ARRAY);
1199 RECORD(TYPE_VARIABLE_ARRAY);
1200 RECORD(TYPE_VECTOR);
1201 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001202 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001203 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001204 RECORD(TYPE_TYPEDEF);
1205 RECORD(TYPE_TYPEOF_EXPR);
1206 RECORD(TYPE_TYPEOF);
1207 RECORD(TYPE_RECORD);
1208 RECORD(TYPE_ENUM);
1209 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001210 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001211 RECORD(TYPE_DECLTYPE);
1212 RECORD(TYPE_ELABORATED);
1213 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1214 RECORD(TYPE_UNRESOLVED_USING);
1215 RECORD(TYPE_INJECTED_CLASS_NAME);
1216 RECORD(TYPE_OBJC_OBJECT);
1217 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1218 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1219 RECORD(TYPE_DEPENDENT_NAME);
1220 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1221 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1222 RECORD(TYPE_PAREN);
1223 RECORD(TYPE_PACK_EXPANSION);
1224 RECORD(TYPE_ATTRIBUTED);
1225 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001226 RECORD(TYPE_AUTO);
1227 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001228 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001229 RECORD(TYPE_DECAYED);
1230 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001231 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001232 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001233 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001234 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001235 RECORD(DECL_ENUM);
1236 RECORD(DECL_RECORD);
1237 RECORD(DECL_ENUM_CONSTANT);
1238 RECORD(DECL_FUNCTION);
1239 RECORD(DECL_OBJC_METHOD);
1240 RECORD(DECL_OBJC_INTERFACE);
1241 RECORD(DECL_OBJC_PROTOCOL);
1242 RECORD(DECL_OBJC_IVAR);
1243 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001244 RECORD(DECL_OBJC_CATEGORY);
1245 RECORD(DECL_OBJC_CATEGORY_IMPL);
1246 RECORD(DECL_OBJC_IMPLEMENTATION);
1247 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1248 RECORD(DECL_OBJC_PROPERTY);
1249 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001250 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001251 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001252 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001253 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001254 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001255 RECORD(DECL_FILE_SCOPE_ASM);
1256 RECORD(DECL_BLOCK);
1257 RECORD(DECL_CONTEXT_LEXICAL);
1258 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001259 RECORD(DECL_NAMESPACE);
1260 RECORD(DECL_NAMESPACE_ALIAS);
1261 RECORD(DECL_USING);
1262 RECORD(DECL_USING_SHADOW);
1263 RECORD(DECL_USING_DIRECTIVE);
1264 RECORD(DECL_UNRESOLVED_USING_VALUE);
1265 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1266 RECORD(DECL_LINKAGE_SPEC);
1267 RECORD(DECL_CXX_RECORD);
1268 RECORD(DECL_CXX_METHOD);
1269 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001270 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001271 RECORD(DECL_CXX_DESTRUCTOR);
1272 RECORD(DECL_CXX_CONVERSION);
1273 RECORD(DECL_ACCESS_SPEC);
1274 RECORD(DECL_FRIEND);
1275 RECORD(DECL_FRIEND_TEMPLATE);
1276 RECORD(DECL_CLASS_TEMPLATE);
1277 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1278 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001279 RECORD(DECL_VAR_TEMPLATE);
1280 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1281 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001282 RECORD(DECL_FUNCTION_TEMPLATE);
1283 RECORD(DECL_TEMPLATE_TYPE_PARM);
1284 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1285 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001286 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001287 RECORD(DECL_STATIC_ASSERT);
1288 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001289 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001290 RECORD(DECL_INDIRECTFIELD);
1291 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001292 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1293 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1294 RECORD(DECL_IMPORT);
1295 RECORD(DECL_OMP_THREADPRIVATE);
1296 RECORD(DECL_EMPTY);
1297 RECORD(DECL_OBJC_TYPE_PARAM);
1298 RECORD(DECL_OMP_CAPTUREDEXPR);
1299 RECORD(DECL_PRAGMA_COMMENT);
1300 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1301 RECORD(DECL_OMP_DECLARE_REDUCTION);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001302
Douglas Gregor03412ba2011-06-03 02:27:19 +00001303 // Statements and Exprs can occur in the Decls and Types block.
1304 AddStmtsExprs(Stream, Record);
1305
Douglas Gregor92a96f52011-02-08 21:58:10 +00001306 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001307 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001308 RECORD(PPD_MACRO_DEFINITION);
1309 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001310
1311 // Decls and Types block.
1312 BLOCK(EXTENSION_BLOCK);
1313 RECORD(EXTENSION_METADATA);
1314
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001315 BLOCK(UNHASHED_CONTROL_BLOCK);
1316 RECORD(SIGNATURE);
1317 RECORD(DIAGNOSTIC_OPTIONS);
1318 RECORD(DIAG_PRAGMA_MAPPINGS);
1319
Chris Lattner28fa4e62009-04-26 22:26:21 +00001320#undef RECORD
1321#undef BLOCK
1322 Stream.ExitBlock();
1323}
1324
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001325/// Prepares a path for being written to an AST file by converting it
Richard Smith54cc3c22014-12-11 20:50:24 +00001326/// to an absolute path and removing nested './'s.
1327///
1328/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001329static bool cleanPathForOutput(FileManager &FileMgr,
1330 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001331 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001332 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001333}
1334
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001335/// Adjusts the given filename to only write out the portion of the
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001336/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001337///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001338/// \param Filename the file name to adjust.
1339///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001340/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1341/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001342///
1343/// \returns either the original filename (if it needs no adjustment) or the
1344/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001345static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001346adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001347 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001348
Richard Smith7ed1bc92014-12-05 22:42:13 +00001349 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001350 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001351
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001352 // Verify that the filename and the system root have the same prefix.
1353 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001354 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1355 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001356 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001357
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001358 // We hit the end of the filename before we hit the end of the system root.
1359 if (!Filename[Pos])
1360 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001361
Richard Smith7ed1bc92014-12-05 22:42:13 +00001362 // If there's not a path separator at the end of the base directory nor
1363 // immediately after it, then this isn't within the base directory.
1364 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1365 if (!llvm::sys::path::is_separator(BaseDir.back()))
1366 return Filename;
1367 } else {
1368 // If the file name has a '/' at the current position, skip over the '/'.
1369 // We distinguish relative paths from absolute paths by the
1370 // absence of '/' at the beginning of relative paths.
1371 //
1372 // FIXME: This is wrong. We distinguish them by asking if the path is
1373 // absolute, which isn't the same thing. And there might be multiple '/'s
1374 // in a row. Use a better mechanism to indicate whether we have emitted an
1375 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001376 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001379 return Filename + Pos;
1380}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001381
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001382ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1383 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1384 llvm::SHA1 Hasher;
1385 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1386 auto Hash = Hasher.result();
1387
1388 // Convert to an array [5*i32].
1389 ASTFileSignature Signature;
1390 auto LShift = [&](unsigned char Val, unsigned Shift) {
1391 return (uint32_t)Val << Shift;
1392 };
1393 for (int I = 0; I != 5; ++I)
1394 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1395 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1396
1397 return Signature;
1398}
1399
1400ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1401 ASTContext &Context) {
1402 // Flush first to prepare the PCM hash (signature).
1403 Stream.FlushToWord();
1404 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1405
1406 // Enter the block and prepare to write records.
1407 RecordData Record;
1408 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1409
1410 // For implicit modules, write the hash of the PCM as its signature.
1411 ASTFileSignature Signature;
1412 if (WritingModule &&
1413 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1414 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1415 Record.append(Signature.begin(), Signature.end());
1416 Stream.EmitRecord(SIGNATURE, Record);
1417 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001418 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001419
1420 // Diagnostic options.
1421 const auto &Diags = Context.getDiagnostics();
1422 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1423#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1424#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1425 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1426#include "clang/Basic/DiagnosticOptions.def"
1427 Record.push_back(DiagOpts.Warnings.size());
1428 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1429 AddString(DiagOpts.Warnings[I], Record);
1430 Record.push_back(DiagOpts.Remarks.size());
1431 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1432 AddString(DiagOpts.Remarks[I], Record);
1433 // Note: we don't serialize the log or serialization file names, because they
1434 // are generally transient files and will almost always be overridden.
1435 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1436
1437 // Write out the diagnostic/pragma mappings.
1438 WritePragmaDiagnosticMappings(Diags, /* IsModule = */ WritingModule);
1439
1440 // Leave the options block.
1441 Stream.ExitBlock();
1442 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001443}
1444
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001445/// Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001446void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1447 StringRef isysroot,
1448 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001449 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001450
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001451 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001452 RecordData Record;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001453
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001454 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001455 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001456 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1457 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1458 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1459 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1460 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1461 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001462 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001463 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PCHHasObjectFile
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001464 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1465 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001466 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001467 assert((!WritingModule || isysroot.empty()) &&
1468 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001469 {
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001470 RecordData::value_type Record[] = {
1471 METADATA,
1472 VERSION_MAJOR,
1473 VERSION_MINOR,
1474 CLANG_VERSION_MAJOR,
1475 CLANG_VERSION_MINOR,
1476 !isysroot.empty(),
1477 IncludeTimestamps,
1478 Context.getLangOpts().BuildingPCHWithObjectFile,
1479 ASTHasCompilerErrors};
Mehdi Amini57a41912015-09-10 01:46:39 +00001480 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1481 getClangFullRepositoryVersion());
1482 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001483
Richard Smith8b706102017-05-31 20:56:55 +00001484 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001485 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001486 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001487 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001489 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001490 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001491 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1492 }
1493
Richard Smith7ed1bc92014-12-05 22:42:13 +00001494 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001495 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001496 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001497
1498 // If the home of the module is the current working directory, then we
1499 // want to pick up the cwd of the build process loading the module, not
1500 // our cwd, when we load this module.
1501 if (!PP.getHeaderSearchInfo()
1502 .getHeaderSearchOpts()
1503 .ModuleMapFileHomeIsCwd ||
1504 WritingModule->Directory->getName() != StringRef(".")) {
1505 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001506 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001507 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1508 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001509 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001510
Mehdi Amini57a41912015-09-10 01:46:39 +00001511 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001512 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1513 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001514
1515 // Write out all other paths relative to the base directory if possible.
1516 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1517 } else if (!isysroot.empty()) {
1518 // Write out paths relative to the sysroot if possible.
1519 BaseDirectory = isysroot;
1520 }
1521
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001522 // Module map file
Richard Smithd19389a2017-07-05 07:47:11 +00001523 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001524 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001525
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001526 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001527 AddPath(WritingModule->PresumedModuleMapFile.empty()
1528 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1529 : StringRef(WritingModule->PresumedModuleMapFile),
1530 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001531
1532 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001533 if (auto *AdditionalModMaps =
1534 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001535 Record.push_back(AdditionalModMaps->size());
1536 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001537 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001538 } else {
1539 Record.push_back(0);
1540 }
1541
1542 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001543 }
1544
Douglas Gregor112b9072012-10-18 05:31:06 +00001545 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001546 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001547 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001548 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001549
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001550 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001551 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001552 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001553 continue;
1554
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001555 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1556 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001557
1558 // If we have calculated signature, there is no need to store
1559 // the size or timestamp.
1560 Record.push_back(M.Signature ? 0 : M.File->getSize());
1561 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1562
1563 for (auto I : M.Signature)
1564 Record.push_back(I);
1565
Boris Kolpackovd30446f2017-08-31 06:26:43 +00001566 AddString(M.ModuleName, Record);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001567 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001568 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001569 Stream.EmitRecord(IMPORTS, Record);
1570 }
Mike Stump11289f42009-09-09 15:08:12 +00001571
Richard Smith0516b182015-09-08 19:40:14 +00001572 // Write the options block.
1573 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1574
Douglas Gregor112b9072012-10-18 05:31:06 +00001575 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001576 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001577 const LangOptions &LangOpts = Context.getLangOpts();
1578#define LANGOPT(Name, Bits, Default, Description) \
1579 Record.push_back(LangOpts.Name);
1580#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1581 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001582#include "clang/Basic/LangOptions.def"
1583#define SANITIZER(NAME, ID) \
1584 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001585#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001586
Ben Langmuircd98cb72015-06-23 18:20:18 +00001587 Record.push_back(LangOpts.ModuleFeatures.size());
1588 for (StringRef Feature : LangOpts.ModuleFeatures)
1589 AddString(Feature, Record);
1590
Douglas Gregor112b9072012-10-18 05:31:06 +00001591 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1592 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001593
1594 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001595
1596 // Comment options.
1597 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001598 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1599 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001600 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001601 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001602
Samuel Antaoee8fb302016-01-06 13:42:12 +00001603 // OpenMP offloading options.
1604 Record.push_back(LangOpts.OMPTargetTriples.size());
1605 for (auto &T : LangOpts.OMPTargetTriples)
1606 AddString(T.getTriple(), Record);
1607
1608 AddString(LangOpts.OMPHostIRFile, Record);
1609
Douglas Gregor112b9072012-10-18 05:31:06 +00001610 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1611
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001612 // Target options.
1613 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001614 const TargetInfo &Target = Context.getTargetInfo();
1615 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001616 AddString(TargetOpts.Triple, Record);
1617 AddString(TargetOpts.CPU, Record);
1618 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001619 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1620 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1621 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1622 }
1623 Record.push_back(TargetOpts.Features.size());
1624 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1625 AddString(TargetOpts.Features[I], Record);
1626 }
1627 Stream.EmitRecord(TARGET_OPTIONS, Record);
1628
Douglas Gregorc6317db2012-10-24 15:49:58 +00001629 // File system options.
1630 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001631 const FileSystemOptions &FSOpts =
1632 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001633 AddString(FSOpts.WorkingDir, Record);
1634 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1635
Douglas Gregor2d302362012-10-24 16:50:34 +00001636 // Header search options.
1637 Record.clear();
1638 const HeaderSearchOptions &HSOpts
1639 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1640 AddString(HSOpts.Sysroot, Record);
1641
1642 // Include entries.
1643 Record.push_back(HSOpts.UserEntries.size());
1644 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1645 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1646 AddString(Entry.Path, Record);
1647 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001648 Record.push_back(Entry.IsFramework);
1649 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001650 }
1651
1652 // System header prefixes.
1653 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1654 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1655 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1656 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1657 }
1658
1659 AddString(HSOpts.ResourceDir, Record);
1660 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001661 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001662 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001663 Record.push_back(HSOpts.ImplicitModuleMaps);
1664 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001665 Record.push_back(HSOpts.UseBuiltinIncludes);
1666 Record.push_back(HSOpts.UseStandardSystemIncludes);
1667 Record.push_back(HSOpts.UseStandardCXXIncludes);
1668 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001669 // Write out the specific module cache path that contains the module files.
1670 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001671 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1672
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001673 // Preprocessor options.
1674 Record.clear();
1675 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1676
1677 // Macro definitions.
1678 Record.push_back(PPOpts.Macros.size());
1679 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1680 AddString(PPOpts.Macros[I].first, Record);
1681 Record.push_back(PPOpts.Macros[I].second);
1682 }
1683
1684 // Includes
1685 Record.push_back(PPOpts.Includes.size());
1686 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1687 AddString(PPOpts.Includes[I], Record);
1688
1689 // Macro includes
1690 Record.push_back(PPOpts.MacroIncludes.size());
1691 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1692 AddString(PPOpts.MacroIncludes[I], Record);
1693
Douglas Gregorb6368752012-10-24 23:41:50 +00001694 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001695 // Detailed record is important since it is used for the module cache hash.
1696 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001697 AddString(PPOpts.ImplicitPCHInclude, Record);
1698 AddString(PPOpts.ImplicitPTHInclude, Record);
1699 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1700 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1701
Richard Smith0516b182015-09-08 19:40:14 +00001702 // Leave the options block.
1703 Stream.ExitBlock();
1704
Douglas Gregora3b20262011-05-06 21:43:30 +00001705 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001706 SourceManager &SM = Context.getSourceManager();
1707 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001708 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001709 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1710 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001711 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001712 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001713
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001714 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001715 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001716 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001717 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001718 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001719
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001720 Record.clear();
1721 Record.push_back(SM.getMainFileID().getOpaqueValue());
1722 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1723
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001724 // Original PCH directory
1725 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001726 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001727 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001729 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001730
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001731 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001732
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001733 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001734 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1735
Mehdi Amini57a41912015-09-10 01:46:39 +00001736 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001737 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1738 }
1739
Douglas Gregor49491f72013-03-15 22:15:07 +00001740 WriteInputFiles(Context.SourceMgr,
1741 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001742 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001743 Stream.ExitBlock();
1744}
1745
Douglas Gregor49491f72013-03-15 22:15:07 +00001746namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001747
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001748/// An input file.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001749struct InputFileEntry {
1750 const FileEntry *File;
1751 bool IsSystemFile;
1752 bool IsTransient;
1753 bool BufferOverridden;
1754 bool IsTopLevelModuleMap;
1755};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001756
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001757} // namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001758
1759void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1760 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001761 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001762 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001763
Douglas Gregor72be3902012-10-19 00:38:02 +00001764 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001765
Douglas Gregor72be3902012-10-19 00:38:02 +00001766 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001767 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001768 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001769 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001770 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1771 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001772 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001773 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Richard Smithf3f84612017-06-29 02:19:42 +00001774 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
Douglas Gregor72be3902012-10-19 00:38:02 +00001775 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001776 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001777
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001778 // Get all ContentCache objects for files, sorted by whether the file is a
1779 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001780 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001781 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1782 // Get this source location entry.
1783 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001784 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001785
1786 // We only care about file entries that were not overridden.
1787 if (!SLoc->isFile())
1788 continue;
Richard Smithf3f84612017-06-29 02:19:42 +00001789 const SrcMgr::FileInfo &File = SLoc->getFile();
1790 const SrcMgr::ContentCache *Cache = File.getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001791 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001792 continue;
1793
Douglas Gregor49491f72013-03-15 22:15:07 +00001794 InputFileEntry Entry;
1795 Entry.File = Cache->OrigEntry;
1796 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001797 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001798 Entry.BufferOverridden = Cache->BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001799 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1800 File.getIncludeLoc().isInvalid();
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001801 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001802 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001803 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001804 SortedFiles.push_front(Entry);
1805 }
1806
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001807 unsigned UserFilesNum = 0;
1808 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001809 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001810 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001811 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001812 if (InputFileID != 0)
1813 continue; // already recorded this file.
1814
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001815 // Record this entry's offset.
1816 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001817
1818 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001819
Douglas Gregor49491f72013-03-15 22:15:07 +00001820 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001821 ++UserFilesNum;
1822
Douglas Gregor72be3902012-10-19 00:38:02 +00001823 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001824 // And whether this file was overridden.
1825 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001826 INPUT_FILE,
1827 InputFileOffsets.size(),
1828 (uint64_t)Entry.File->getSize(),
1829 (uint64_t)getTimestampForOutput(Entry.File),
1830 Entry.BufferOverridden,
Richard Smithf3f84612017-06-29 02:19:42 +00001831 Entry.IsTransient,
1832 Entry.IsTopLevelModuleMap};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001833
Richard Smith7ed1bc92014-12-05 22:42:13 +00001834 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1835 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001836
Douglas Gregor112b9072012-10-18 05:31:06 +00001837 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001838
1839 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001840 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001841 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1842 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001843 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1844 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001845 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001846 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001847
1848 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001849 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1850 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001851 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001852}
1853
Douglas Gregora7f71a92009-04-10 03:52:48 +00001854//===----------------------------------------------------------------------===//
1855// Source Manager Serialization
1856//===----------------------------------------------------------------------===//
1857
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001858/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001859/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001860static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001861 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001862
David Blaikieb44f0bf2017-01-04 22:36:43 +00001863 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001864 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001869 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1873 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001874 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001875}
1876
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001877/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001878/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001879static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001880 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001881
David Blaikieb44f0bf2017-01-04 22:36:43 +00001882 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001883 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001889 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001890}
1891
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001892/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001893/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001894static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1895 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001896 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001897
David Blaikieb44f0bf2017-01-04 22:36:43 +00001898 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001899 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1900 : SM_SLOC_BUFFER_BLOB));
1901 if (Compressed)
1902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001904 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001905}
1906
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001907/// Create an abbreviation for the SLocEntry that refers to a macro
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001908/// expansion.
1909static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001910 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001911
David Blaikieb44f0bf2017-01-04 22:36:43 +00001912 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001913 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Richard Smithb5f81712018-04-30 05:25:48 +00001918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
Douglas Gregor83243272009-04-15 18:05:10 +00001919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001920 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001921}
1922
Douglas Gregor09b69892011-02-10 17:09:37 +00001923namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001924
Douglas Gregor09b69892011-02-10 17:09:37 +00001925 // Trait used for the on-disk hash table of header search information.
1926 class HeaderFileInfoTrait {
1927 ASTWriter &Writer;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001928
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001929 // Keep track of the framework names we've used during serialization.
1930 SmallVector<char, 128> FrameworkStringData;
1931 llvm::StringMap<unsigned> FrameworkNameOffset;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001932
Douglas Gregor09b69892011-02-10 17:09:37 +00001933 public:
Richard Smith040e1262017-06-02 01:55:39 +00001934 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1935
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001936 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001937 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001938 off_t Size;
1939 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001940 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001941 using key_type_ref = const key_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001942
1943 using UnresolvedModule =
1944 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001945
Richard Smith040e1262017-06-02 01:55:39 +00001946 struct data_type {
1947 const HeaderFileInfo &HFI;
1948 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1949 UnresolvedModule Unresolved;
1950 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001951 using data_type_ref = const data_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001952
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001953 using hash_value_type = unsigned;
1954 using offset_type = unsigned;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001955
Richard Smithe75ee0f2015-08-17 07:13:32 +00001956 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001957 // The hash is based only on size/time of the file, so that the reader can
1958 // match even when symlinking or excess path elements ("foo/../", "../")
1959 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00001960 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00001961 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001962
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001963 std::pair<unsigned, unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001964 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001965 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001966
Peter Collingbournee3f65292018-05-18 19:46:24 +00001967 endian::Writer LE(Out, little);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001968 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001969 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001970 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00001971 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00001972 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1973 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00001974 if (Data.Unresolved.getPointer())
1975 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00001976 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001977 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001978 }
Richard Smith040e1262017-06-02 01:55:39 +00001979
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001980 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001981 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001982
Peter Collingbournee3f65292018-05-18 19:46:24 +00001983 endian::Writer LE(Out, little);
Richard Smith040e1262017-06-02 01:55:39 +00001984 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001985 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00001986 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001987 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001988 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001989 }
Richard Smith040e1262017-06-02 01:55:39 +00001990
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001991 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001992 data_type_ref Data, unsigned DataLen) {
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);
Douglas Gregor09b69892011-02-10 17:09:37 +00001996 uint64_t Start = Out.tell(); (void)Start;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001997
Richard Smithf3f84612017-06-29 02:19:42 +00001998 unsigned char Flags = (Data.HFI.isImport << 5)
1999 | (Data.HFI.isPragmaOnce << 4)
Richard Smith040e1262017-06-02 01:55:39 +00002000 | (Data.HFI.DirInfo << 1)
2001 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00002002 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00002003 LE.write<uint16_t>(Data.HFI.NumIncludes);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002004
Richard Smith040e1262017-06-02 01:55:39 +00002005 if (!Data.HFI.ControllingMacro)
2006 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00002007 else
Richard Smith040e1262017-06-02 01:55:39 +00002008 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
2009
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002010 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00002011 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002012 // If this header refers into a framework, save the framework name.
2013 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00002014 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002015 if (Pos == FrameworkNameOffset.end()) {
2016 Offset = FrameworkStringData.size() + 1;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002017 FrameworkStringData.append(Data.HFI.Framework.begin(),
Richard Smith040e1262017-06-02 01:55:39 +00002018 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002019 FrameworkStringData.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002020
Richard Smith040e1262017-06-02 01:55:39 +00002021 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002022 } else
2023 Offset = Pos->second;
2024 }
Justin Bognere1c147c2014-03-28 22:03:19 +00002025 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002026
Richard Smith040e1262017-06-02 01:55:39 +00002027 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
2028 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
2029 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00002030 assert((Value >> 2) == ModID && "overflow in header module info");
2031 LE.write<uint32_t>(Value);
2032 }
Richard Smith040e1262017-06-02 01:55:39 +00002033 };
2034
2035 // FIXME: If the header is excluded, we should write out some
2036 // record of that fact.
2037 for (auto ModInfo : Data.KnownHeaders)
2038 EmitModule(ModInfo.getModule(), ModInfo.getRole());
2039 if (Data.Unresolved.getPointer())
2040 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002041
Douglas Gregor09b69892011-02-10 17:09:37 +00002042 assert(Out.tell() - Start == DataLen && "Wrong data length");
2043 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002044
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002045 const char *strings_begin() const { return FrameworkStringData.begin(); }
2046 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00002047 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002048
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002049} // namespace
Douglas Gregor09b69892011-02-10 17:09:37 +00002050
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002051/// Write the header search block for the list of files that
Douglas Gregor09b69892011-02-10 17:09:37 +00002052///
2053/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00002054void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00002055 HeaderFileInfoTrait GeneratorTrait(*this);
2056 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
2057 SmallVector<const char *, 4> SavedStrings;
2058 unsigned NumHeaderSearchEntries = 0;
2059
2060 // Find all unresolved headers for the current module. We generally will
2061 // have resolved them before we get here, but not necessarily: we might be
2062 // compiling a preprocessed module, where there is no requirement for the
2063 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00002064 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00002065 if (WritingModule) {
2066 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
2067 while (!Worklist.empty()) {
2068 Module *M = Worklist.pop_back_val();
2069 if (!M->isAvailable())
2070 continue;
2071
2072 // Map to disk files where possible, to pick up any missing stat
2073 // information. This also means we don't need to check the unresolved
2074 // headers list when emitting resolved headers in the first loop below.
2075 // FIXME: It'd be preferable to avoid doing this if we were given
2076 // sufficient stat information in the module map.
2077 HS.getModuleMap().resolveHeaderDirectives(M);
2078
2079 // If the file didn't exist, we can still create a module if we were given
2080 // enough information in the module map.
2081 for (auto U : M->MissingHeaders) {
2082 // Check that we were given enough information to build a module
2083 // without this file existing on disk.
2084 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2085 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
2086 << WritingModule->getFullModuleName() << U.Size.hasValue()
2087 << U.FileName;
2088 continue;
2089 }
2090
2091 // Form the effective relative pathname for the file.
2092 SmallString<128> Filename(M->Directory->getName());
2093 llvm::sys::path::append(Filename, U.FileName);
2094 PreparePathForOutput(Filename);
2095
2096 StringRef FilenameDup = strdup(Filename.c_str());
2097 SavedStrings.push_back(FilenameDup.data());
2098
2099 HeaderFileInfoTrait::key_type Key = {
2100 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
2101 };
2102 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00002103 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00002104 };
2105 // FIXME: Deal with cases where there are multiple unresolved header
2106 // directives in different submodules for the same header.
2107 Generator.insert(Key, Data, GeneratorTrait);
2108 ++NumHeaderSearchEntries;
2109 }
2110
2111 Worklist.append(M->submodule_begin(), M->submodule_end());
2112 }
2113 }
2114
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002115 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00002116 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002117
Douglas Gregor09b69892011-02-10 17:09:37 +00002118 if (FilesByUID.size() > HS.header_file_size())
2119 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00002120
Douglas Gregor09b69892011-02-10 17:09:37 +00002121 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2122 const FileEntry *File = FilesByUID[UID];
2123 if (!File)
2124 continue;
2125
Richard Smith386bb072015-08-18 23:42:23 +00002126 // Get the file info. This will load info from the external source if
2127 // necessary. Skip emitting this file if we have no information on it
2128 // as a header file (in which case HFI will be null) or if it hasn't
2129 // changed since it was loaded. Also skip it if it's for a modular header
2130 // from a different module; in that case, we rely on the module(s)
2131 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00002132 const HeaderFileInfo *HFI =
2133 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
2134 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00002135 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00002136
Richard Smith7ed1bc92014-12-05 22:42:13 +00002137 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002138 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00002139 SmallString<128> FilenameTmp(Filename);
2140 if (PreparePathForOutput(FilenameTmp)) {
2141 // If we performed any translation on the file name at all, we need to
2142 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002143 Filename = StringRef(strdup(FilenameTmp.c_str()));
2144 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00002145 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002146
Richard Smith040e1262017-06-02 01:55:39 +00002147 HeaderFileInfoTrait::key_type Key = {
2148 Filename, File->getSize(), getTimestampForOutput(File)
2149 };
2150 HeaderFileInfoTrait::data_type Data = {
2151 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
2152 };
2153 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002154 ++NumHeaderSearchEntries;
2155 }
Richard Smith040e1262017-06-02 01:55:39 +00002156
Douglas Gregor09b69892011-02-10 17:09:37 +00002157 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002158 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002159 uint32_t BucketOffset;
2160 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002161 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002162
Douglas Gregor09b69892011-02-10 17:09:37 +00002163 llvm::raw_svector_ostream Out(TableData);
2164 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00002165 endian::write<uint32_t>(Out, 0, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00002166 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2167 }
2168
2169 // Create a blob abbreviation
2170 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002171
David Blaikieb44f0bf2017-01-04 22:36:43 +00002172 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002173 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002178 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002179
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002180 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002181 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2182 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002183 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002184 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002185
Douglas Gregor09b69892011-02-10 17:09:37 +00002186 // Free all of the strings we had to duplicate.
2187 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002188 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002189}
2190
George Rimarc39f5492017-01-17 15:45:31 +00002191static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2192 unsigned SLocBufferBlobCompressedAbbrv,
2193 unsigned SLocBufferBlobAbbrv) {
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002194 using RecordDataType = ASTWriter::RecordData::value_type;
George Rimarc39f5492017-01-17 15:45:31 +00002195
2196 // Compress the buffer if possible. We expect that almost all PCM
2197 // consumers will not want its contents.
2198 SmallString<0> CompressedBuffer;
2199 if (llvm::zlib::isAvailable()) {
2200 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2201 if (!E) {
2202 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2203 Blob.size() - 1};
2204 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2205 CompressedBuffer);
2206 return;
2207 }
2208 llvm::consumeError(std::move(E));
2209 }
2210
2211 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2212 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2213}
2214
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002215/// Writes the block containing the serialized form of the
Douglas Gregora7f71a92009-04-10 03:52:48 +00002216/// source manager.
2217///
2218/// TODO: We should probably use an on-disk hash table (stored in a
2219/// blob), indexed based on the file name, so that we only create
2220/// entries for files that we actually need. In the common case (no
2221/// errors), we probably won't have to create file entries for any of
2222/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002223void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002224 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002225 RecordData Record;
2226
Chris Lattner0910e3b2009-04-10 17:16:57 +00002227 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002228 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002229
2230 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002231 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2232 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002233 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2234 unsigned SLocBufferBlobCompressedAbbrv =
2235 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002236 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002237
Douglas Gregor258ae542009-04-27 06:38:32 +00002238 // Write out the source location entry table. We skip the first
2239 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002240 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002241 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002242 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2243 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002244 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002245 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002246 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002247 FileID FID = FileID::get(I);
2248 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002249
Douglas Gregor258ae542009-04-27 06:38:32 +00002250 // Record the offset of this source-location entry.
2251 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2252
2253 // Figure out which record code to use.
2254 unsigned Code;
2255 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002256 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2257 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002258 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002259 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002260 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002261 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002262 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002263 Record.clear();
2264 Record.push_back(Code);
2265
Douglas Gregor925296b2011-07-19 16:10:42 +00002266 // Starting offset of this entry within this module, so skip the dummy.
2267 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002268 if (SLoc->isFile()) {
2269 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002270 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002271 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2272 Record.push_back(File.hasLineDirectives());
2273
2274 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002275 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002276 if (Content->OrigEntry) {
2277 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002278 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002279
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002280 // The source location entry is a file. Emit input file ID.
2281 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2282 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002283
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002284 Record.push_back(File.NumCreatedFIDs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002285
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002286 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002287 if (FDI != FileDeclIDs.end()) {
2288 Record.push_back(FDI->second->FirstDeclIndex);
2289 Record.push_back(FDI->second->DeclIDs.size());
2290 } else {
2291 Record.push_back(0);
2292 Record.push_back(0);
2293 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002294
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002295 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002296
Richard Smithaada85c2016-02-06 02:06:43 +00002297 if (Content->BufferOverridden || Content->IsTransient)
2298 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002299 } else {
2300 // The source location entry is a buffer. The blob associated
2301 // with this entry contains the contents of the buffer.
2302
2303 // We add one to the size so that we capture the trailing NULL
2304 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2305 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002306 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002307 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002308 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002309 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002310 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002311 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002312
Mehdi Amini99d1b292016-10-01 16:38:28 +00002313 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002314 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002315 }
Richard Smithaada85c2016-02-06 02:06:43 +00002316
2317 if (EmitBlob) {
2318 // Include the implicit terminating null character in the on-disk buffer
2319 // if we're writing it uncompressed.
2320 const llvm::MemoryBuffer *Buffer =
2321 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2322 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002323 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2324 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002325 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002326 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002327 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002328 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002329 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2330 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2331 AddSourceLocation(Expansion.isMacroArgExpansion()
2332 ? SourceLocation()
2333 : Expansion.getExpansionLocEnd(),
2334 Record);
Richard Smithb5f81712018-04-30 05:25:48 +00002335 Record.push_back(Expansion.isExpansionTokenRange());
Douglas Gregor258ae542009-04-27 06:38:32 +00002336
2337 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002338 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002339 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002340 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002341 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002342 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002343 }
2344 }
2345
Douglas Gregor8f45df52009-04-16 22:23:12 +00002346 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002347
2348 if (SLocEntryOffsets.empty())
2349 return;
2350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002351 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002352 // table is used for lazily loading source-location information.
2353 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002354
David Blaikieb44f0bf2017-01-04 22:36:43 +00002355 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002356 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002360 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002361 {
2362 RecordData::value_type Record[] = {
2363 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2364 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2365 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2366 bytes(SLocEntryOffsets));
2367 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002368 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002369 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002370 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002371
2372 // Write the line table. It depends on remapping working, so it must come
2373 // after the source location offsets.
2374 if (SourceMgr.hasLineTable()) {
2375 LineTableInfo &LineTable = SourceMgr.getLineTable();
2376
2377 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002378
2379 // Emit the needed file names.
2380 llvm::DenseMap<int, int> FilenameMap;
Hans Wennborg14487362017-12-04 22:28:45 +00002381 FilenameMap[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00002382 for (const auto &L : LineTable) {
2383 if (L.first.ID < 0)
2384 continue;
2385 for (auto &LE : L.second) {
2386 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
Hans Wennborg14487362017-12-04 22:28:45 +00002387 FilenameMap.size() - 1)).second)
Richard Smith63078492015-09-01 07:41:55 +00002388 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2389 }
2390 }
2391 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002392
2393 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002394 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002395 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002396 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002397 continue;
2398
2399 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002400 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002401
2402 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002403 Record.push_back(L.second.size());
2404 for (const auto &LE : L.second) {
2405 Record.push_back(LE.FileOffset);
2406 Record.push_back(LE.LineNo);
2407 Record.push_back(FilenameMap[LE.FilenameID]);
2408 Record.push_back((unsigned)LE.FileKind);
2409 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002410 }
2411 }
Richard Smith63078492015-09-01 07:41:55 +00002412
Douglas Gregor925296b2011-07-19 16:10:42 +00002413 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2414 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002415}
2416
Douglas Gregorc5046832009-04-27 18:38:38 +00002417//===----------------------------------------------------------------------===//
2418// Preprocessor Serialization
2419//===----------------------------------------------------------------------===//
2420
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002421static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2422 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002423 if (MacroInfo *MI = MD->getMacroInfo())
2424 if (MI->isBuiltinMacro())
2425 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002426
2427 if (IsModule) {
2428 SourceLocation Loc = MD->getLocation();
2429 if (Loc.isInvalid())
2430 return true;
2431 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2432 return true;
2433 }
2434
2435 return false;
2436}
2437
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002438/// Writes the block containing the serialized form of the
Chris Lattnereeffaef2009-04-10 17:15:23 +00002439/// preprocessor.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002440void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002441 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2442 if (PPRec)
2443 WritePreprocessorDetail(*PPRec);
2444
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002445 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002446 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002447
Chris Lattner0af3ba12009-04-13 01:29:17 +00002448 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2449 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002450 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002451 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002452 }
2453
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002454 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2455 assert(!IsModule);
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00002456 auto SkipInfo = PP.getPreambleSkipInfo();
2457 if (SkipInfo.hasValue()) {
2458 Record.push_back(true);
2459 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2460 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2461 Record.push_back(SkipInfo->FoundNonSkipPortion);
2462 Record.push_back(SkipInfo->FoundElse);
2463 AddSourceLocation(SkipInfo->ElseLoc, Record);
2464 } else {
2465 Record.push_back(false);
2466 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002467 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2468 AddSourceLocation(Cond.IfLoc, Record);
2469 Record.push_back(Cond.WasSkipping);
2470 Record.push_back(Cond.FoundNonSkip);
2471 Record.push_back(Cond.FoundElse);
2472 }
2473 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2474 Record.clear();
2475 }
2476
Douglas Gregoreda6a892009-04-26 00:07:37 +00002477 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002478 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002479
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002480 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002481 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002482 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002483 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002484
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002485 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002486 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002487
Richard Smithee977932015-05-01 21:22:17 +00002488 // Construct the list of identifiers with macro directives that need to be
2489 // serialized.
2490 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2491 for (auto &Id : PP.getIdentifierTable())
2492 if (Id.second->hadMacroDefinition() &&
2493 (!Id.second->isFromAST() ||
2494 Id.second->hasChangedSinceDeserialization()))
2495 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002496 // Sort the set of macro definitions that need to be serialized by the
2497 // name of the macro, to provide a stable ordering.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00002498 llvm::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2499 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002500
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002501 // Emit the macro directives as a list and associate the offset with the
2502 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002503 for (const IdentifierInfo *Name : MacroIdentifiers) {
2504 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002505 auto StartOffset = Stream.GetCurrentBitNo();
2506
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002507 // Emit the macro directives in reverse source order.
2508 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002509 // Once we hit an ignored macro, we're done: the rest of the chain
2510 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002511 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002512 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002513
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002514 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002515 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002516 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002517 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002518 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002519 Record.push_back(VisMD->isPublic());
2520 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002521 }
Richard Smithd7329392015-04-21 21:46:32 +00002522
Richard Smithb8b2ed62015-04-23 18:18:26 +00002523 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002524 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002525 // We write out exported module macros for PCH as well.
2526 auto Leafs = PP.getLeafModuleMacros(Name);
2527 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2528 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2529 while (!Worklist.empty()) {
2530 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002531
Manman Ren33d80292016-05-31 18:19:32 +00002532 // Emit a record indicating this submodule exports this macro.
2533 ModuleMacroRecord.push_back(
2534 getSubmoduleID(Macro->getOwningModule()));
2535 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2536 for (auto *M : Macro->overrides())
2537 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002538
Manman Ren33d80292016-05-31 18:19:32 +00002539 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2540 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002541
Manman Ren33d80292016-05-31 18:19:32 +00002542 // Enqueue overridden macros once we've visited all their ancestors.
2543 for (auto *M : Macro->overrides())
2544 if (++Visits[M] == M->getNumOverridingMacros())
2545 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002546
Manman Ren33d80292016-05-31 18:19:32 +00002547 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002548 }
Richard Smithd7329392015-04-21 21:46:32 +00002549
Richard Smithee977932015-05-01 21:22:17 +00002550 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002551 continue;
2552
Richard Smithd7329392015-04-21 21:46:32 +00002553 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002554 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2555 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002556 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002557
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002558 /// Offsets of each of the macros into the bitstream, indexed by
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002559 /// the local macro ID
2560 ///
2561 /// For each identifier that is associated with a macro, this map
2562 /// provides the offset into the bitstream where that macro is
2563 /// defined.
2564 std::vector<uint32_t> MacroOffsets;
2565
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002566 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2567 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2568 MacroInfo *MI = MacroInfosToEmit[I].MI;
2569 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002570
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002571 if (ID < FirstMacroID) {
2572 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2573 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002574 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002575
2576 // Record the local offset of this macro.
2577 unsigned Index = ID - FirstMacroID;
2578 if (Index == MacroOffsets.size())
2579 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2580 else {
2581 if (Index > MacroOffsets.size())
2582 MacroOffsets.resize(Index + 1);
2583
2584 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2585 }
2586
2587 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002588 AddSourceLocation(MI->getDefinitionLoc(), Record);
2589 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2590 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002591 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002592 unsigned Code;
2593 if (MI->isObjectLike()) {
2594 Code = PP_MACRO_OBJECT_LIKE;
2595 } else {
2596 Code = PP_MACRO_FUNCTION_LIKE;
2597
2598 Record.push_back(MI->isC99Varargs());
2599 Record.push_back(MI->isGNUVarargs());
2600 Record.push_back(MI->hasCommaPasting());
Faisal Valiac506d72017-07-17 17:18:43 +00002601 Record.push_back(MI->getNumParams());
2602 for (const IdentifierInfo *Param : MI->params())
2603 AddIdentifierRef(Param, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002604 }
2605
2606 // If we have a detailed preprocessing record, record the macro definition
2607 // ID that corresponds to this macro.
2608 if (PPRec)
2609 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2610
2611 Stream.EmitRecord(Code, Record);
2612 Record.clear();
2613
2614 // Emit the tokens array.
2615 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2616 // Note that we know that the preprocessor does not have any annotation
2617 // tokens in it because they are created by the parser, and thus can't
2618 // be in a macro definition.
2619 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002620 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002621 Stream.EmitRecord(PP_TOKEN, Record);
2622 Record.clear();
2623 }
2624 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002625 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002626
Douglas Gregor92a96f52011-02-08 21:58:10 +00002627 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002628
2629 // Write the offsets table for macro IDs.
2630 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002631
David Blaikieb44f0bf2017-01-04 22:36:43 +00002632 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002633 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2637
David Blaikieb44f0bf2017-01-04 22:36:43 +00002638 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002639 {
2640 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2641 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2642 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2643 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002644}
2645
2646void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002647 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002648 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002649
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002650 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002651
Douglas Gregor92a96f52011-02-08 21:58:10 +00002652 // Enter the preprocessor block.
2653 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002654
Douglas Gregoraae92242010-03-19 21:51:54 +00002655 // If the preprocessor has a preprocessing record, emit it.
2656 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002657 using namespace llvm;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002658
2659 // Set up the abbreviation for
Douglas Gregor92a96f52011-02-08 21:58:10 +00002660 unsigned InclusionAbbrev = 0;
2661 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002662 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002663 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002668 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002669 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002670 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002671
2672 unsigned FirstPreprocessorEntityID
2673 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002674 + NUM_PREDEF_PP_ENTITY_IDS;
2675 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002676 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002677 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2678 EEnd = PPRec.local_end();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002679 E != EEnd;
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002680 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002681 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002682
Richard Smith66a81862015-05-04 02:25:31 +00002683 PreprocessedEntityOffsets.push_back(
2684 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002685
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002686 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002687 // Record this macro definition's ID.
2688 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002689
Douglas Gregor92a96f52011-02-08 21:58:10 +00002690 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002691 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2692 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002693 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002694
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002695 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002696 Record.push_back(ME->isBuiltinMacro());
2697 if (ME->isBuiltinMacro())
2698 AddIdentifierRef(ME->getName(), Record);
2699 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002700 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002701 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002702 continue;
2703 }
2704
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002705 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002706 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002707 Record.push_back(ID->getFileName().size());
2708 Record.push_back(ID->wasInQuotes());
2709 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002710 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002711 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002712 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002713 // Check that the FileEntry is not null because it was not resolved and
2714 // we create a PCH even with compiler errors.
2715 if (ID->getFile())
2716 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002717 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002718 continue;
2719 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002720
Douglas Gregor92a96f52011-02-08 21:58:10 +00002721 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2722 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002723 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002724
Douglas Gregoraae92242010-03-19 21:51:54 +00002725 // Write the offsets table for the preprocessing record.
2726 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002727 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2728
Douglas Gregoraae92242010-03-19 21:51:54 +00002729 // Write the offsets table for identifier IDs.
2730 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002731
David Blaikieb44f0bf2017-01-04 22:36:43 +00002732 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002733 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002736 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002737
Mehdi Amini57a41912015-09-10 01:46:39 +00002738 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2739 FirstPreprocessorEntityID -
2740 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002741 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002742 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002743 }
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00002744
2745 // Write the skipped region table for the preprocessing record.
2746 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2747 if (SkippedRanges.size() > 0) {
2748 std::vector<PPSkippedRange> SerializedSkippedRanges;
2749 SerializedSkippedRanges.reserve(SkippedRanges.size());
2750 for (auto const& Range : SkippedRanges)
2751 SerializedSkippedRanges.emplace_back(Range);
2752
2753 using namespace llvm;
2754 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2755 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2757 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2758
2759 Record.clear();
2760 Record.push_back(PPD_SKIPPED_RANGES);
2761 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2762 bytes(SerializedSkippedRanges));
2763 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002764}
2765
Richard Smith386bb072015-08-18 23:42:23 +00002766unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002767 if (!Mod)
2768 return 0;
2769
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002770 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2771 if (Known != SubmoduleIDs.end())
2772 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002773
Richard Smithdc1f0422016-07-20 19:10:16 +00002774 auto *Top = Mod->getTopLevelModule();
2775 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002776 (getLangOpts().CompilingPCH ||
2777 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002778 return 0;
2779
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002780 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2781}
2782
Richard Smith386bb072015-08-18 23:42:23 +00002783unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2784 // FIXME: This can easily happen, if we have a reference to a submodule that
2785 // did not result in us loading a module file for that submodule. For
2786 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2787 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2788 assert((ID || !Mod) &&
2789 "asked for module ID for non-local, non-imported module");
2790 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002791}
2792
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002793/// Compute the number of modules within the given tree (including the
Douglas Gregor253eefe2011-12-01 00:59:36 +00002794/// given module).
2795static unsigned getNumberOfModules(Module *Mod) {
2796 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002797 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002798 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002799 ChildModules += getNumberOfModules(*Sub);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002800
Douglas Gregor253eefe2011-12-01 00:59:36 +00002801 return ChildModules + 1;
2802}
2803
Douglas Gregorde3ef502011-11-30 23:21:26 +00002804void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002805 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002806 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002807
Douglas Gregor69021972011-11-30 17:33:56 +00002808 // Write the abbreviations needed for the submodules block.
2809 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002810
David Blaikieb44f0bf2017-01-04 22:36:43 +00002811 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002812 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
Richard Smithdd8b5332017-09-04 05:37:53 +00002815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
Douglas Gregor69021972011-11-30 17:33:56 +00002816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002818 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2819 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002820 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002821 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002822 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002823 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002824 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
Douglas Gregor69021972011-11-30 17:33:56 +00002825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002826 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002827
David Blaikieb44f0bf2017-01-04 22:36:43 +00002828 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002829 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002831 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002832
David Blaikieb44f0bf2017-01-04 22:36:43 +00002833 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002834 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002836 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002837
David Blaikieb44f0bf2017-01-04 22:36:43 +00002838 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002839 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002841 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002842
David Blaikieb44f0bf2017-01-04 22:36:43 +00002843 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002844 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002846 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002847
David Blaikieb44f0bf2017-01-04 22:36:43 +00002848 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002849 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002852 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002853
David Blaikieb44f0bf2017-01-04 22:36:43 +00002854 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002855 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002857 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002858
David Blaikieb44f0bf2017-01-04 22:36:43 +00002859 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002860 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2861 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002862 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002863
David Blaikieb44f0bf2017-01-04 22:36:43 +00002864 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002865 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002867 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002868
David Blaikieb44f0bf2017-01-04 22:36:43 +00002869 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002870 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002872 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002873
David Blaikieb44f0bf2017-01-04 22:36:43 +00002874 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002875 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002878 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002879
David Blaikieb44f0bf2017-01-04 22:36:43 +00002880 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002881 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002883 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002884
David Blaikieb44f0bf2017-01-04 22:36:43 +00002885 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002886 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002889 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002890
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002891 Abbrev = std::make_shared<BitCodeAbbrev>();
2892 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2894 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2895
Douglas Gregor253eefe2011-12-01 00:59:36 +00002896 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002897 RecordData::value_type Record[] = {
2898 getNumberOfModules(WritingModule),
Richard Smithdd8b5332017-09-04 05:37:53 +00002899 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002900 Stream.EmitRecord(SUBMODULE_METADATA, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002901
Douglas Gregor69021972011-11-30 17:33:56 +00002902 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002903 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002904 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002905 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002906 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002907 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002908 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002909
2910 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002911 if (Mod->Parent) {
2912 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002913 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002914 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002915
2916 // Emit the definition of the block.
2917 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002918 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2919 ID,
2920 ParentID,
Simon Pilgrimf331a662017-09-04 10:54:39 +00002921 (RecordData::value_type)Mod->Kind,
David Blaikie9ffe5a32017-01-30 05:00:26 +00002922 Mod->IsFramework,
2923 Mod->IsExplicit,
2924 Mod->IsSystem,
2925 Mod->IsExternC,
2926 Mod->InferSubmodules,
2927 Mod->InferExplicitSubmodules,
2928 Mod->InferExportWildcard,
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002929 Mod->ConfigMacrosExhaustive,
2930 Mod->ModuleMapIsPrivate};
Mehdi Amini57a41912015-09-10 01:46:39 +00002931 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2932 }
2933
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002934 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002935 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002936 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002937 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002938 }
2939
Douglas Gregor69021972011-11-30 17:33:56 +00002940 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002941 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002942 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002943 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2944 UmbrellaHeader.NameAsWritten);
2945 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002946 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2947 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002948 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002949 }
Richard Smith306d8922014-10-22 23:50:56 +00002950
Douglas Gregor69021972011-11-30 17:33:56 +00002951 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002952 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002953 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002954 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002955 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002956 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002957 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2958 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2959 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002960 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002961 Module::HK_PrivateTextual},
2962 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002963 };
2964 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002965 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002966 for (auto &H : Mod->Headers[HL.HeaderKind])
2967 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2968 }
2969
2970 // Emit the top headers.
2971 {
2972 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002973 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002974 for (auto *H : TopHeaders)
2975 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002976 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002977
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002978 // Emit the imports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002979 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002980 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002981 for (auto *I : Mod->Imports)
2982 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002983 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2984 }
2985
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002986 // Emit the exports.
Douglas Gregor24bb9232011-12-02 18:58:38 +00002987 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002988 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002989 for (const auto &E : Mod->Exports) {
2990 // FIXME: This may fail; we don't require that all exported modules
2991 // are local or imported.
2992 Record.push_back(getSubmoduleID(E.getPointer()));
2993 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002994 }
2995 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2996 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002997
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002998 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2999 // Might be unnecessary as use declarations are only used to build the
3000 // module itself.
3001
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003002 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00003003 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003004 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
3005 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00003006 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003007 }
3008
Douglas Gregorfb912652013-03-20 21:10:35 +00003009 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00003010 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00003011 // FIXME: This may fail; we don't require that all conflicting modules
3012 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00003013 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
3014 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00003015 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00003016 }
3017
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003018 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00003019 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003020 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00003021 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003022 }
3023
Richard Smithdc1f0422016-07-20 19:10:16 +00003024 // Emit the initializers, if any.
3025 RecordData Inits;
3026 for (Decl *D : Context->getModuleInitializers(Mod))
3027 Inits.push_back(GetDeclRef(D));
3028 if (!Inits.empty())
3029 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3030
Douglas Gregorf0b11de2017-09-14 23:38:44 +00003031 // Emit the name of the re-exported module, if any.
3032 if (!Mod->ExportAsModule.empty()) {
3033 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3034 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3035 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003036
Douglas Gregor69021972011-11-30 17:33:56 +00003037 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00003038 for (auto *M : Mod->submodules())
3039 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00003040 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003041
Douglas Gregor69021972011-11-30 17:33:56 +00003042 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00003043
Richard Smith23d8d032015-05-14 00:45:20 +00003044 assert((NextSubmoduleID - FirstSubmoduleID ==
3045 getNumberOfModules(WritingModule)) &&
3046 "Wrong # of submodules; found a reference to a non-local, "
3047 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00003048}
3049
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00003050void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3051 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003052 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3053 DiagStateIDMap;
3054 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003055 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003056
Richard Smithe37391c2017-05-03 00:28:49 +00003057 auto EncodeDiagStateFlags =
3058 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3059 unsigned Result = (unsigned)DS->ExtBehavior;
3060 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00003061 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3062 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3063 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00003064 Result = (Result << 1) | Val;
3065 return Result;
3066 };
3067
3068 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3069 Record.push_back(Flags);
3070
Richard Smithd230de22017-01-26 01:01:01 +00003071 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3072 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00003073 // Ensure that the diagnostic state wasn't modified since it was created.
3074 // We will not correctly round-trip this information otherwise.
3075 assert(Flags == EncodeDiagStateFlags(State) &&
3076 "diag state flags vary in single AST file");
3077
Richard Smithd230de22017-01-26 01:01:01 +00003078 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003079 Record.push_back(DiagStateID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003080
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003081 if (DiagStateID == 0) {
3082 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003083
3084 // Add a placeholder for the number of mappings.
3085 auto SizeIdx = Record.size();
3086 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00003087 for (const auto &I : *State) {
3088 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003089 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00003090 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003091 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003092 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003093 // Update the placeholder.
3094 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00003095 }
3096 };
3097
3098 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00003099
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003100 // Reserve a spot for the number of locations with state transitions.
3101 auto NumLocationsIdx = Record.size();
3102 Record.emplace_back();
3103
3104 // Emit the state transitions.
3105 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00003106 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3107 if (!FileIDAndFile.first.isValid() ||
3108 !FileIDAndFile.second.HasLocalTransitions)
3109 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003110 ++NumLocations;
Richard Smith6c2b5a82018-02-09 01:15:13 +00003111
3112 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3113 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3114 AddSourceLocation(Loc, Record);
3115
Richard Smithd230de22017-01-26 01:01:01 +00003116 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3117 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3118 Record.push_back(StatePoint.Offset);
3119 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003120 }
3121 }
3122
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003123 // Backpatch the number of locations.
3124 Record[NumLocationsIdx] = NumLocations;
3125
3126 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00003127 //
3128 // This also protects against a hypothetical corner case with simulating
3129 // -Werror settings for implicit modules in the ASTReader, where reading
3130 // CurDiagState out of context could change whether warning pragmas are
3131 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003132 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3133 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3134
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00003135 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003136}
3137
Douglas Gregorc5046832009-04-27 18:38:38 +00003138//===----------------------------------------------------------------------===//
3139// Type Serialization
3140//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00003141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003142/// Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003143void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00003144 TypeIdx &IdxRef = TypeIdxs[T];
3145 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3146 IdxRef = TypeIdx(NextTypeID++);
3147 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00003148
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003149 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00003150
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003151 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00003152
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003153 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003154 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00003155 W.Visit(T);
3156 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00003157
Richard Smith290d8012016-04-06 17:06:00 +00003158 // Record the offset for this type.
3159 unsigned Index = Idx.getIndex() - FirstTypeID;
3160 if (TypeOffsets.size() == Index)
3161 TypeOffsets.push_back(Offset);
3162 else if (TypeOffsets.size() < Index) {
3163 TypeOffsets.resize(Index + 1);
3164 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00003165 } else {
Richard Smith290d8012016-04-06 17:06:00 +00003166 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003167 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003168}
3169
Douglas Gregorc5046832009-04-27 18:38:38 +00003170//===----------------------------------------------------------------------===//
3171// Declaration Serialization
3172//===----------------------------------------------------------------------===//
3173
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003174/// Write the block containing all of the declaration IDs
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003175/// lexically declared within the given DeclContext.
3176///
3177/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003178/// bitstream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003179uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003180 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003181 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003182 return 0;
3183
Douglas Gregor8f45df52009-04-16 22:23:12 +00003184 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003185 SmallVector<uint32_t, 128> KindDeclPairs;
3186 for (const auto *D : DC->decls()) {
3187 KindDeclPairs.push_back(D->getKind());
3188 KindDeclPairs.push_back(GetDeclRef(D));
3189 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003190
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003191 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00003192 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00003193 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3194 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003195 return Offset;
3196}
3197
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003198void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003199 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003200
3201 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003202 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003203 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00003205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003207 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003208 {
3209 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3210 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3211 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3212 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003213
3214 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003215 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003216 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00003218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003220 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003221 {
3222 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3223 FirstDeclID - NUM_PREDEF_DECL_IDS};
3224 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3225 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003226}
3227
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003228void ASTWriter::WriteFileDeclIDsMap() {
3229 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003230
Chandler Carruthb306d732015-03-27 00:31:20 +00003231 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3232 FileDeclIDs.begin(), FileDeclIDs.end());
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00003233 llvm::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3234 llvm::less_first());
Chandler Carruthb306d732015-03-27 00:31:20 +00003235
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003236 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003237 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3238 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3239 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3240 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3241 for (auto &LocDeclEntry : Info.DeclIDs)
3242 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003243 }
3244
David Blaikieb44f0bf2017-01-04 22:36:43 +00003245 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003246 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003249 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003250 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3251 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003252 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003253}
3254
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003255void ASTWriter::WriteComments() {
3256 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Ilya Biryukov45643102018-07-09 11:33:23 +00003257 auto _ = llvm::make_scope_exit([this] { Stream.ExitBlock(); });
3258 if (!PP->getPreprocessorOpts().WriteCommentListToPCH)
3259 return;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003260 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003261 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003262 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003263 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003264 AddSourceRange(I->getSourceRange(), Record);
3265 Record.push_back(I->getKind());
3266 Record.push_back(I->isTrailingComment());
3267 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003268 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3269 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003270}
3271
Douglas Gregorc5046832009-04-27 18:38:38 +00003272//===----------------------------------------------------------------------===//
3273// Global Method Pool and Selector Serialization
3274//===----------------------------------------------------------------------===//
3275
Douglas Gregore84a9da2009-04-20 20:36:09 +00003276namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003277
Douglas Gregorc78d3462009-04-24 21:10:55 +00003278// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003279class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003280 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003281
3282public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003283 using key_type = Selector;
3284 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003285
Sebastian Redl834bb972010-08-04 17:20:04 +00003286 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003287 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003288 ObjCMethodList Instance, Factory;
3289 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003290 using data_type_ref = const data_type &;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003291
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003292 using hash_value_type = unsigned;
3293 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003294
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003295 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
Mike Stump11289f42009-09-09 15:08:12 +00003296
Justin Bogner25463f12014-04-18 20:27:24 +00003297 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003298 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003299 }
Mike Stump11289f42009-09-09 15:08:12 +00003300
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003301 std::pair<unsigned, unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003302 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003303 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003304 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003305
Peter Collingbournee3f65292018-05-18 19:46:24 +00003306 endian::Writer LE(Out, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003307 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003308 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003309 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3310 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003311 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003312 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003313 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003314 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003315 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003316 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003317 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003318 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003319 return std::make_pair(KeyLen, DataLen);
3320 }
Mike Stump11289f42009-09-09 15:08:12 +00003321
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003322 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003323 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003324
Peter Collingbournee3f65292018-05-18 19:46:24 +00003325 endian::Writer LE(Out, little);
Mike Stump11289f42009-09-09 15:08:12 +00003326 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003327 assert((Start >> 32) == 0 && "Selector key offset too large");
3328 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003329 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003330 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003331 if (N == 0)
3332 N = 1;
3333 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003334 LE.write<uint32_t>(
3335 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003336 }
Mike Stump11289f42009-09-09 15:08:12 +00003337
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003338 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003339 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003340 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003341
Peter Collingbournee3f65292018-05-18 19:46:24 +00003342 endian::Writer LE(Out, little);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003343 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003344 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003345 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003346 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003347 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003348 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003349 ++NumInstanceMethods;
3350
3351 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003352 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003353 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003354 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003355 ++NumFactoryMethods;
3356
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003357 unsigned InstanceBits = Methods.Instance.getBits();
3358 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003359 unsigned InstanceHasMoreThanOneDeclBit =
3360 Methods.Instance.hasMoreThanOneDecl();
3361 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3362 (InstanceHasMoreThanOneDeclBit << 2) |
3363 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003364 unsigned FactoryBits = Methods.Factory.getBits();
3365 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003366 unsigned FactoryHasMoreThanOneDeclBit =
3367 Methods.Factory.hasMoreThanOneDecl();
3368 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3369 (FactoryHasMoreThanOneDeclBit << 2) |
3370 FactoryBits;
3371 LE.write<uint16_t>(FullInstanceBits);
3372 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003373 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003374 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003375 if (Method->getMethod())
3376 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003377 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003378 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003379 if (Method->getMethod())
3380 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003381
3382 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003383 }
3384};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003385
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003386} // namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +00003387
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003388/// Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003389///
3390/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003391/// in an on-disk hash table indexed by the selector. The hash table also
3392/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003393void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003394 using namespace llvm;
3395
Sebastian Redla19a67f2010-08-03 21:58:15 +00003396 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003397 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003398 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003399 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003400 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003401 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003402 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003403 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003404
Sebastian Redla19a67f2010-08-03 21:58:15 +00003405 // Create the on-disk hash table representation. We walk through every
3406 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003407 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003408 for (auto &SelectorAndID : SelectorIDs) {
3409 Selector S = SelectorAndID.first;
3410 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003411 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003412 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003413 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003414 ObjCMethodList(),
3415 ObjCMethodList()
3416 };
3417 if (F != SemaRef.MethodPool.end()) {
3418 Data.Instance = F->second.first;
3419 Data.Factory = F->second.second;
3420 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003421 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003422 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003423 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003424 // Selector already exists. Did it change?
3425 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003426 for (ObjCMethodList *M = &Data.Instance;
3427 !changed && M && M->getMethod(); M = M->getNext()) {
3428 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003429 changed = true;
3430 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003431 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003432 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003433 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003434 changed = true;
3435 }
3436 if (!changed)
3437 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003438 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003439 // A new method pool entry.
3440 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003441 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003442 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003443 }
3444
Douglas Gregorc78d3462009-04-24 21:10:55 +00003445 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003446 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003447 uint32_t BucketOffset;
3448 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003449 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003450
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003451 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003452 llvm::raw_svector_ostream Out(MethodPool);
3453 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003454 endian::write<uint32_t>(Out, 0, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003455 BucketOffset = Generator.Emit(Out, Trait);
3456 }
3457
3458 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003459 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003460 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003463 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003464 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003465
Douglas Gregor95c13f52009-04-25 17:48:32 +00003466 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003467 {
3468 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3469 NumTableEntries};
3470 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3471 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003472
3473 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003474 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003475 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003479 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003480
3481 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003482 {
3483 RecordData::value_type Record[] = {
3484 SELECTOR_OFFSETS, SelectorOffsets.size(),
3485 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3486 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3487 bytes(SelectorOffsets));
3488 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003489 }
3490}
3491
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003492/// Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003493void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003494 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003495
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003496 if (SemaRef.ReferencedSelectors.empty())
3497 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003498
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003499 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003500 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003501
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003502 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003503 // very tricky to fix, and given that @selector shouldn't really appear in
3504 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003505 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3506 Selector Sel = SelectorAndLocation.first;
3507 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003508 Writer.AddSelectorRef(Sel);
3509 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003510 }
Richard Smithe64e7452016-04-18 21:54:58 +00003511 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003512}
3513
Douglas Gregorc5046832009-04-27 18:38:38 +00003514//===----------------------------------------------------------------------===//
3515// Identifier Table Serialization
3516//===----------------------------------------------------------------------===//
3517
Richard Smithb3761912015-02-05 23:08:52 +00003518/// Determine the declaration that should be put into the name lookup table to
3519/// represent the given declaration in this module. This is usually D itself,
3520/// but if D was imported and merged into a local declaration, we want the most
3521/// recent local declaration instead. The chosen declaration will be the most
3522/// recent declaration in any module that imports this one.
3523static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3524 NamedDecl *D) {
3525 if (!LangOpts.Modules || !D->isFromASTFile())
3526 return D;
3527
3528 if (Decl *Redecl = D->getPreviousDecl()) {
3529 // For Redeclarable decls, a prior declaration might be local.
3530 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003531 // If we find a local decl, we're done.
3532 if (!Redecl->isFromASTFile()) {
3533 // Exception: in very rare cases (for injected-class-names), not all
3534 // redeclarations are in the same semantic context. Skip ones in a
3535 // different context. They don't go in this lookup table at all.
3536 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3537 D->getDeclContext()->getRedeclContext()))
3538 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003539 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003540 }
3541
Richard Smithfe620d22015-03-05 23:24:12 +00003542 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003543 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003544 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003545 break;
3546 }
3547 } else if (Decl *First = D->getCanonicalDecl()) {
3548 // For Mergeable decls, the first decl might be local.
3549 if (!First->isFromASTFile())
3550 return cast<NamedDecl>(First);
3551 }
3552
3553 // All declarations are imported. Our most recent declaration will also be
3554 // the most recent one in anyone who imports us.
3555 return D;
3556}
3557
Douglas Gregorc78d3462009-04-24 21:10:55 +00003558namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003559
Richard Smithcf97cf62015-04-19 01:34:23 +00003560class ASTIdentifierTableTrait {
3561 ASTWriter &Writer;
3562 Preprocessor &PP;
3563 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003564 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003565 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003566 ASTWriter::RecordData *InterestingIdentifierOffsets;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003567
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003568 /// Determines whether this is an "interesting" identifier that needs a
Richard Smithcf97cf62015-04-19 01:34:23 +00003569 /// full IdentifierInfo structure written into the hash table. Notably, this
3570 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3571 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003572 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003573 if (MacroOffset ||
3574 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003575 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003576 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003577 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003578 return true;
3579
3580 return false;
3581 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003582
Douglas Gregore84a9da2009-04-20 20:36:09 +00003583public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003584 using key_type = IdentifierInfo *;
3585 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003586
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003587 using data_type = IdentID;
3588 using data_type_ref = data_type;
Mike Stump11289f42009-09-09 15:08:12 +00003589
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003590 using hash_value_type = unsigned;
3591 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003592
Richard Smithd7329392015-04-21 21:46:32 +00003593 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003594 IdentifierResolver &IdResolver, bool IsModule,
3595 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003596 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003597 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3598 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003599
Richard Smithd79514e2016-02-05 19:03:40 +00003600 bool needDecls() const { return NeedDecls; }
3601
Justin Bogner25463f12014-04-18 20:27:24 +00003602 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003603 return llvm::djbHash(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003604 }
Mike Stump11289f42009-09-09 15:08:12 +00003605
Richard Smith33e0f7e2015-07-22 02:08:40 +00003606 bool isInterestingIdentifier(const IdentifierInfo *II) {
3607 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3608 return isInterestingIdentifier(II, MacroOffset);
3609 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003610
Richard Smith9c254182015-07-19 21:41:12 +00003611 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3612 return isInterestingIdentifier(II, 0);
3613 }
3614
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003615 std::pair<unsigned, unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003616 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003617 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003618 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003619 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3620 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003621 DataLen += 2; // 2 bytes for builtin ID
3622 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003623 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003624 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003625
Richard Smitha534a312015-07-21 23:54:07 +00003626 if (NeedDecls) {
3627 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3628 DEnd = IdResolver.end();
3629 D != DEnd; ++D)
3630 DataLen += 4;
3631 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003632 }
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003633
Justin Bognere1c147c2014-03-28 22:03:19 +00003634 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003635
Peter Collingbournee3f65292018-05-18 19:46:24 +00003636 endian::Writer LE(Out, little);
Justin Bognere1c147c2014-03-28 22:03:19 +00003637
Richard Smithdf8a8312015-03-13 04:05:01 +00003638 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003639 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003640 // We emit the key length after the data length so that every
3641 // string is preceded by a 16-bit length. This matches the PTH
3642 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003643 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003644 return std::make_pair(KeyLen, DataLen);
3645 }
Mike Stump11289f42009-09-09 15:08:12 +00003646
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003647 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003648 unsigned KeyLen) {
3649 // Record the location of the key data. This is used when generating
3650 // the mapping from persistent IDs to strings.
3651 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003652
3653 // Emit the offset of the key/data length information to the interesting
3654 // identifiers table if necessary.
3655 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3656 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3657
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003658 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003659 }
Mike Stump11289f42009-09-09 15:08:12 +00003660
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003661 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003662 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003663 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003664
Peter Collingbournee3f65292018-05-18 19:46:24 +00003665 endian::Writer LE(Out, little);
Richard Smithcf97cf62015-04-19 01:34:23 +00003666
Richard Smithd7329392015-04-21 21:46:32 +00003667 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3668 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003669 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003670 return;
3671 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003672
Justin Bognere1c147c2014-03-28 22:03:19 +00003673 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003674 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3675 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003676 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003677 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003678 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003679 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003680 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3681 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003682 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003683 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003684 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003685 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003686
Richard Smithd7329392015-04-21 21:46:32 +00003687 if (HadMacroDefinition)
3688 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003689
Richard Smitha534a312015-07-21 23:54:07 +00003690 if (NeedDecls) {
3691 // Emit the declaration IDs in reverse order, because the
3692 // IdentifierResolver provides the declarations as they would be
3693 // visible (e.g., the function "stat" would come before the struct
3694 // "stat"), but the ASTReader adds declarations to the end of the list
3695 // (so we need to see the struct "stat" before the function "stat").
3696 // Only emit declarations that aren't from a chained PCH, though.
3697 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3698 IdResolver.end());
3699 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3700 DEnd = Decls.rend();
3701 D != DEnd; ++D)
3702 LE.write<uint32_t>(
3703 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3704 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003705 }
3706};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003707
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003708} // namespace
Douglas Gregore84a9da2009-04-20 20:36:09 +00003709
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003710/// Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003711///
3712/// The identifier table consists of a blob containing string data
3713/// (the actual identifiers themselves) and a separate "offsets" index
3714/// that maps identifier IDs to locations within the blob.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003715void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003716 IdentifierResolver &IdResolver,
3717 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003718 using namespace llvm;
3719
Richard Smith33e0f7e2015-07-22 02:08:40 +00003720 RecordData InterestingIdents;
3721
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003722 // Create and write out the blob that contains the identifier
3723 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003724 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003725 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003726 ASTIdentifierTableTrait Trait(
3727 *this, PP, IdResolver, IsModule,
3728 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003729
Douglas Gregore6648fb2009-04-28 20:33:11 +00003730 // Look for any identifiers that were named while processing the
3731 // headers, but are otherwise not needed. We add these to the hash
3732 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003733 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003734 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003735 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003736 for (const auto &ID : PP.getIdentifierTable())
3737 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003738 // Sort the identifiers lexicographically before getting them references so
3739 // that their order is stable.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00003740 llvm::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
Chandler Carruth8440c982015-03-26 23:54:15 +00003741 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003742 if (Trait.isInterestingNonMacroIdentifier(II))
3743 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003744
Sebastian Redlff4a2952010-07-23 23:49:55 +00003745 // Create the on-disk hash table representation. We only store offsets
3746 // for identifiers that appear here for the first time.
3747 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003748 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003749 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003750 IdentID ID = IdentIDPair.second;
3751 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003752 // Write out identifiers if either the ID is local or the identifier has
3753 // changed since it was loaded.
3754 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3755 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003756 (Trait.needDecls() &&
3757 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003758 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003759 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003760
Douglas Gregore84a9da2009-04-20 20:36:09 +00003761 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003762 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003763 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003764 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003765 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003766
Douglas Gregore84a9da2009-04-20 20:36:09 +00003767 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003768 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003769 endian::write<uint32_t>(Out, 0, little);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003770 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003771 }
3772
3773 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003774 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003775 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003778 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003779
3780 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003781 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003782 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003783 }
3784
3785 // Write the offsets table for identifier IDs.
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_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003791 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003792
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003793#ifndef NDEBUG
3794 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3795 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3796#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003797
3798 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3799 IdentifierOffsets.size(),
3800 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003801 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003802 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003803
3804 // In C++, write the list of interesting identifiers (those that are
3805 // defined as macros, poisoned, or similar unusual things).
3806 if (!InterestingIdents.empty())
3807 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003808}
3809
Douglas Gregorc5046832009-04-27 18:38:38 +00003810//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003811// DeclContext's Name Lookup Table Serialization
3812//===----------------------------------------------------------------------===//
3813
3814namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003815
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003816// Trait used for the on-disk hash table used in the method pool.
3817class ASTDeclContextNameLookupTrait {
3818 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003819 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003820
3821public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003822 using key_type = DeclarationNameKey;
3823 using key_type_ref = key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003824
Richard Smithd88a7f12015-09-01 20:35:42 +00003825 /// A start and end index into DeclIDs, representing a sequence of decls.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003826 using data_type = std::pair<unsigned, unsigned>;
3827 using data_type_ref = const data_type &;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003828
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003829 using hash_value_type = unsigned;
3830 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003831
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003832 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003833
Richard Smithd88a7f12015-09-01 20:35:42 +00003834 template<typename Coll>
3835 data_type getData(const Coll &Decls) {
3836 unsigned Start = DeclIDs.size();
3837 for (NamedDecl *D : Decls) {
3838 DeclIDs.push_back(
3839 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3840 }
3841 return std::make_pair(Start, DeclIDs.size());
3842 }
3843
3844 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3845 unsigned Start = DeclIDs.size();
3846 for (auto ID : FromReader)
3847 DeclIDs.push_back(ID);
3848 return std::make_pair(Start, DeclIDs.size());
3849 }
3850
3851 static bool EqualKey(key_type_ref a, key_type_ref b) {
3852 return a == b;
3853 }
3854
Richard Smitha06c7e62015-08-26 23:55:49 +00003855 hash_value_type ComputeHash(DeclarationNameKey Name) {
3856 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003857 }
3858
Richard Smithd88a7f12015-09-01 20:35:42 +00003859 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3860 assert(Writer.hasChain() &&
3861 "have reference to loaded module file but no chain?");
3862
3863 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003864
Peter Collingbournee3f65292018-05-18 19:46:24 +00003865 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
Richard Smithd88a7f12015-09-01 20:35:42 +00003866 }
3867
Richard Smitha06c7e62015-08-26 23:55:49 +00003868 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3869 DeclarationNameKey Name,
3870 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003871 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003872
Peter Collingbournee3f65292018-05-18 19:46:24 +00003873 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003874 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003875 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003876 case DeclarationName::Identifier:
3877 case DeclarationName::ObjCZeroArgSelector:
3878 case DeclarationName::ObjCOneArgSelector:
3879 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003880 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003881 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003882 KeyLen += 4;
3883 break;
3884 case DeclarationName::CXXOperatorName:
3885 KeyLen += 1;
3886 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003887 case DeclarationName::CXXConstructorName:
3888 case DeclarationName::CXXDestructorName:
3889 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003890 case DeclarationName::CXXUsingDirective:
3891 break;
3892 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003893 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003894
Richard Smithf02662d2015-07-30 03:17:16 +00003895 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003896 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3897 assert(uint16_t(DataLen) == DataLen &&
3898 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003899 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003900
3901 return std::make_pair(KeyLen, DataLen);
3902 }
3903
Richard Smitha06c7e62015-08-26 23:55:49 +00003904 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003905 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003906
Peter Collingbournee3f65292018-05-18 19:46:24 +00003907 endian::Writer LE(Out, little);
Richard Smitha06c7e62015-08-26 23:55:49 +00003908 LE.write<uint8_t>(Name.getKind());
3909 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003910 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003911 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003912 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003913 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003914 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003915 case DeclarationName::ObjCZeroArgSelector:
3916 case DeclarationName::ObjCOneArgSelector:
3917 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003918 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003919 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003920 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003921 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003922 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003923 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003924 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003925 case DeclarationName::CXXConstructorName:
3926 case DeclarationName::CXXDestructorName:
3927 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003928 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003929 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003930 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003931
3932 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003933 }
3934
Richard Smitha06c7e62015-08-26 23:55:49 +00003935 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3936 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003937 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003938
Peter Collingbournee3f65292018-05-18 19:46:24 +00003939 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003940 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003941 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3942 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003943 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3944 }
3945};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003946
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003947} // namespace
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003948
Chandler Carruthe972c362015-03-26 03:11:40 +00003949bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3950 DeclContext *DC) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003951 return Result.hasExternalDecls() &&
3952 DC->hasNeedToReconcileExternalVisibleStorage();
Chandler Carruthe972c362015-03-26 03:11:40 +00003953}
3954
3955bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3956 DeclContext *DC) {
3957 for (auto *D : Result.getLookupResult())
3958 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3959 return false;
3960
3961 return true;
3962}
3963
Richard Smithd88a7f12015-09-01 20:35:42 +00003964void
Chandler Carruthe972c362015-03-26 03:11:40 +00003965ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003966 llvm::SmallVectorImpl<char> &LookupTable) {
Erich Keanef92f31c2018-08-01 20:48:16 +00003967 assert(!ConstDC->hasLazyLocalLexicalLookups() &&
3968 !ConstDC->hasLazyExternalLexicalLookups() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003969 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003970
Chandler Carruthe972c362015-03-26 03:11:40 +00003971 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003972 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003973 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3974
3975 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003976 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3977 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003978 ASTDeclContextNameLookupTrait Trait(*this);
3979
Chandler Carruthe972c362015-03-26 03:11:40 +00003980 // The first step is to collect the declaration names which we need to
3981 // serialize into the name lookup table, and to collect them in a stable
3982 // order.
3983 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003984
Chandler Carruthe972c362015-03-26 03:11:40 +00003985 // We also build up small sets of the constructor and conversion function
3986 // names which are visible.
3987 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003988
Chandler Carruthe972c362015-03-26 03:11:40 +00003989 for (auto &Lookup : *DC->buildLookup()) {
3990 auto &Name = Lookup.first;
3991 auto &Result = Lookup.second;
3992
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003993 // If there are no local declarations in our lookup result, we
3994 // don't need to write an entry for the name at all. If we can't
3995 // write out a lookup set without performing more deserialization,
3996 // just skip this entry.
3997 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003998 isLookupResultEntirelyExternal(Result, DC))
3999 continue;
4000
4001 // We also skip empty results. If any of the results could be external and
4002 // the currently available results are empty, then all of the results are
4003 // external and we skip it above. So the only way we get here with an empty
4004 // results is when no results could have been external *and* we have
4005 // external results.
4006 //
4007 // FIXME: While we might want to start emitting on-disk entries for negative
4008 // lookups into a decl context as an optimization, today we *have* to skip
4009 // them because there are names with empty lookup results in decl contexts
4010 // which we can't emit in any stable ordering: we lookup constructors and
4011 // conversion functions in the enclosing namespace scope creating empty
4012 // results for them. This in almost certainly a bug in Clang's name lookup,
4013 // but that is likely to be hard or impossible to fix and so we tolerate it
4014 // here by omitting lookups with empty results.
4015 if (Lookup.second.getLookupResult().empty())
4016 continue;
4017
4018 switch (Lookup.first.getNameKind()) {
4019 default:
4020 Names.push_back(Lookup.first);
4021 break;
4022
Richard Smithcd45dbc2014-04-19 03:48:30 +00004023 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004024 assert(isa<CXXRecordDecl>(DC) &&
4025 "Cannot have a constructor name outside of a class!");
4026 ConstructorNameSet.insert(Name);
4027 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004028
4029 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004030 assert(isa<CXXRecordDecl>(DC) &&
4031 "Cannot have a conversion function name outside of a class!");
4032 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00004033 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004034 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004035 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004036
Chandler Carruthe972c362015-03-26 03:11:40 +00004037 // Sort the names into a stable order.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004038 llvm::sort(Names.begin(), Names.end());
Chandler Carruthe972c362015-03-26 03:11:40 +00004039
Chandler Carruthffbf7052015-03-26 22:27:09 +00004040 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00004041 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00004042 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00004043
Chandler Carruthffbf7052015-03-26 22:27:09 +00004044 // First we try the easy case by forming the current context's constructor
4045 // name and adding that name first. This is a very useful optimization to
4046 // avoid walking the lexical declarations in many cases, and it also
4047 // handles the only case where a constructor name can come from some other
4048 // lexical context -- when that name is an implicit constructor merged from
4049 // another declaration in the redecl chain. Any non-implicit constructor or
4050 // conversion function which doesn't occur in all the lexical contexts
4051 // would be an ODR violation.
4052 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4053 Context->getCanonicalType(Context->getRecordType(D)));
4054 if (ConstructorNameSet.erase(ImplicitCtorName))
4055 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00004056
Chandler Carruthffbf7052015-03-26 22:27:09 +00004057 // If we still have constructors or conversion functions, we walk all the
4058 // names in the decl and add the constructors and conversion functions
4059 // which are visible in the order they lexically occur within the context.
4060 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4061 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4062 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4063 auto Name = ChildND->getDeclName();
4064 switch (Name.getNameKind()) {
4065 default:
4066 continue;
4067
4068 case DeclarationName::CXXConstructorName:
4069 if (ConstructorNameSet.erase(Name))
4070 Names.push_back(Name);
4071 break;
4072
4073 case DeclarationName::CXXConversionFunctionName:
4074 if (ConversionNameSet.erase(Name))
4075 Names.push_back(Name);
4076 break;
4077 }
4078
4079 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4080 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00004081 }
4082
Chandler Carruthe972c362015-03-26 03:11:40 +00004083 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4084 "constructors by walking all the "
4085 "lexical members of the context.");
4086 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4087 "conversion functions by walking all "
4088 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00004089 }
4090
Chandler Carruthe972c362015-03-26 03:11:40 +00004091 // Next we need to do a lookup with each name into this decl context to fully
4092 // populate any results from external sources. We don't actually use the
4093 // results of these lookups because we only want to use the results after all
4094 // results have been loaded and the pointers into them will be stable.
4095 for (auto &Name : Names)
4096 DC->lookup(Name);
4097
4098 // Now we need to insert the results for each name into the hash table. For
4099 // constructor names and conversion function names, we actually need to merge
4100 // all of the results for them into one list of results each and insert
4101 // those.
4102 SmallVector<NamedDecl *, 8> ConstructorDecls;
4103 SmallVector<NamedDecl *, 8> ConversionDecls;
4104
4105 // Now loop over the names, either inserting them or appending for the two
4106 // special cases.
4107 for (auto &Name : Names) {
4108 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4109
4110 switch (Name.getNameKind()) {
4111 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00004112 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004113 break;
4114
4115 case DeclarationName::CXXConstructorName:
4116 ConstructorDecls.append(Result.begin(), Result.end());
4117 break;
4118
4119 case DeclarationName::CXXConversionFunctionName:
4120 ConversionDecls.append(Result.begin(), Result.end());
4121 break;
4122 }
4123 }
4124
4125 // Handle our two special cases if we ended up having any. We arbitrarily use
4126 // the first declaration's name here because the name itself isn't part of
4127 // the key, only the kind of name is used.
4128 if (!ConstructorDecls.empty())
4129 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004130 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004131 if (!ConversionDecls.empty())
4132 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004133 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004134
Richard Smithd88a7f12015-09-01 20:35:42 +00004135 // Create the on-disk hash table. Also emit the existing imported and
4136 // merged table if there is one.
4137 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4138 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00004139}
4140
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004141/// Write the block containing all of the declaration IDs
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004142/// visible from the given DeclContext.
4143///
4144/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00004145/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004146uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4147 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00004148 // If we imported a key declaration of this namespace, write the visible
4149 // lookup results as an update record for it rather than including them
4150 // on this declaration. We will only look at key declarations on reload.
4151 if (isa<NamespaceDecl>(DC) && Chain &&
4152 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4153 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004154 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00004155 Prev = Prev->getPreviousDecl())
4156 if (!Prev->isFromASTFile())
4157 return 0;
4158
4159 // Note that we need to emit an update record for the primary context.
4160 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4161
4162 // Make sure all visible decls are written. They will be recorded later. We
4163 // do this using a side data structure so we can sort the names into
4164 // a deterministic order.
4165 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4166 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4167 LookupResults;
4168 if (Map) {
4169 LookupResults.reserve(Map->size());
4170 for (auto &Entry : *Map)
4171 LookupResults.push_back(
4172 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4173 }
4174
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004175 llvm::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
Richard Smith5fc18a92015-07-12 23:43:21 +00004176 for (auto &NameAndResult : LookupResults) {
4177 DeclarationName Name = NameAndResult.first;
4178 DeclContext::lookup_result Result = NameAndResult.second;
4179 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4180 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4181 // We have to work around a name lookup bug here where negative lookup
4182 // results for these names get cached in namespace lookup tables (these
4183 // names should never be looked up in a namespace).
4184 assert(Result.empty() && "Cannot have a constructor or conversion "
4185 "function name in a namespace!");
4186 continue;
4187 }
4188
4189 for (NamedDecl *ND : Result)
4190 if (!ND->isFromASTFile())
4191 GetDeclRef(ND);
4192 }
4193
4194 return 0;
4195 }
4196
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004197 if (DC->getPrimaryContext() != DC)
4198 return 0;
4199
Chandler Carruthe972c362015-03-26 03:11:40 +00004200 // Skip contexts which don't support name lookup.
4201 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004202 return 0;
4203
4204 // If not in C++, we perform name lookup for the translation unit via the
4205 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004206 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004207 return 0;
4208
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004209 // Serialize the contents of the mapping used for lookup. Note that,
4210 // although we have two very different code paths, the serialized
4211 // representation is the same for both cases: a declaration name,
4212 // followed by a size, followed by references to the visible
4213 // declarations that have that name.
4214 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00004215 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004216 if (!Map || Map->empty())
4217 return 0;
4218
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004219 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004220 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004221 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004222
4223 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004224 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004225 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00004226 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004227 ++NumVisibleDeclContexts;
4228 return Offset;
4229}
4230
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004231/// Write an UPDATE_VISIBLE block for the given context.
Sebastian Redla4071b42010-08-24 00:50:09 +00004232///
4233/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4234/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00004235/// (in C++), for namespaces, and for classes with forward-declared unscoped
4236/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00004237void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00004238 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00004239 if (!Map || Map->empty())
4240 return;
4241
Sebastian Redla4071b42010-08-24 00:50:09 +00004242 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004243 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004244 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004245
Richard Smith5fc18a92015-07-12 23:43:21 +00004246 // If we're updating a namespace, select a key declaration as the key for the
4247 // update record; those are the only ones that will be checked on reload.
4248 if (isa<NamespaceDecl>(DC))
4249 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4250
Sebastian Redla4071b42010-08-24 00:50:09 +00004251 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004252 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004253 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004254}
4255
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004256/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004257void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004258 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004259 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4260}
4261
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004262/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004263void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004264 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004265 return;
4266
4267 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4268 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004269 for (const auto &I:Opts.OptMap) {
4270 AddString(I.getKey(), Record);
4271 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004272 Record.push_back(V.Supported ? 1 : 0);
4273 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004274 Record.push_back(V.Avail);
4275 Record.push_back(V.Core);
4276 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004277 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4278}
4279
Yaxun Liu5b746652016-12-18 05:18:55 +00004280void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4281 if (!SemaRef.Context.getLangOpts().OpenCL)
4282 return;
4283
4284 RecordData Record;
4285 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4286 Record.push_back(
4287 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4288 Record.push_back(I.second.size());
4289 for (auto Ext : I.second)
4290 AddString(Ext, Record);
4291 }
4292 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4293}
4294
4295void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4296 if (!SemaRef.Context.getLangOpts().OpenCL)
4297 return;
4298
4299 RecordData Record;
4300 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4301 Record.push_back(getDeclID(I.first));
4302 Record.push_back(static_cast<unsigned>(I.second.size()));
4303 for (auto Ext : I.second)
4304 AddString(Ext, Record);
4305 }
4306 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4307}
4308
Justin Lebar67a78a62016-10-08 22:15:58 +00004309void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4310 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4311 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4312 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4313 }
4314}
4315
Douglas Gregor404cdde2012-01-27 01:47:08 +00004316void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004317 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004318 RecordData Categories;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004319
Douglas Gregor404cdde2012-01-27 01:47:08 +00004320 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4321 unsigned Size = 0;
4322 unsigned StartIndex = Categories.size();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004323
Douglas Gregor404cdde2012-01-27 01:47:08 +00004324 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004325
Douglas Gregor404cdde2012-01-27 01:47:08 +00004326 // Allocate space for the size.
4327 Categories.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004328
Douglas Gregor404cdde2012-01-27 01:47:08 +00004329 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004330 for (ObjCInterfaceDecl::known_categories_iterator
4331 Cat = Class->known_categories_begin(),
4332 CatEnd = Class->known_categories_end();
4333 Cat != CatEnd; ++Cat, ++Size) {
4334 assert(getDeclID(*Cat) != 0 && "Bogus category");
4335 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004336 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004337
Douglas Gregor404cdde2012-01-27 01:47:08 +00004338 // Update the size.
4339 Categories[StartIndex] = Size;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004340
Douglas Gregor404cdde2012-01-27 01:47:08 +00004341 // Record this interface -> category map.
4342 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4343 CategoriesMap.push_back(CatInfo);
4344 }
4345
4346 // Sort the categories map by the definition ID, since the reader will be
4347 // performing binary searches on this information.
4348 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4349
4350 // Emit the categories map.
4351 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004352
David Blaikieb44f0bf2017-01-04 22:36:43 +00004353 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004354 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004357 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004358
4359 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4360 Stream.EmitRecordWithBlob(AbbrevID, Record,
4361 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004362 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004363
Douglas Gregor404cdde2012-01-27 01:47:08 +00004364 // Emit the category lists.
4365 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4366}
4367
Richard Smithe40f2ba2013-08-07 21:41:30 +00004368void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4369 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4370
4371 if (LPTMap.empty())
4372 return;
4373
4374 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004375 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004376 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004377 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004378 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004379 AddDeclRef(LPT.D, Record);
4380 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004381
Justin Lebar28f09c52016-10-10 16:26:08 +00004382 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004383 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004384 }
4385 }
4386 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4387}
4388
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004389/// Write the state of 'pragma clang optimize' at the end of the module.
Dario Domizioli13a0a382014-05-23 12:13:25 +00004390void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4391 RecordData Record;
4392 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4393 AddSourceLocation(PragmaLoc, Record);
4394 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4395}
4396
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004397/// Write the state of 'pragma ms_struct' at the end of the module.
Nico Weber779355f2016-03-02 23:22:00 +00004398void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4399 RecordData Record;
4400 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4401 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4402}
4403
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004404/// Write the state of 'pragma pointers_to_members' at the end of the
Nico Weber42932312016-03-03 00:17:35 +00004405//module.
4406void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4407 RecordData Record;
4408 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4409 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4410 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4411}
4412
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004413/// Write the state of 'pragma pack' at the end of the module.
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004414void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004415 // Don't serialize pragma pack state for modules, since it should only take
4416 // effect on a per-submodule basis.
4417 if (WritingModule)
4418 return;
4419
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004420 RecordData Record;
4421 Record.push_back(SemaRef.PackStack.CurrentValue);
4422 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4423 Record.push_back(SemaRef.PackStack.Stack.size());
4424 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4425 Record.push_back(StackEntry.Value);
4426 AddSourceLocation(StackEntry.PragmaLocation, Record);
Alex Lorenz45b40142017-07-28 14:41:21 +00004427 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004428 AddString(StackEntry.StackSlotLabel, Record);
4429 }
4430 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4431}
4432
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004433void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4434 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004435 // Enter the extension block.
4436 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4437
4438 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004439 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004440 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4441 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4442 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4443 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4444 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4445 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004446 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004447
4448 // Emit the metadata record.
4449 RecordData Record;
4450 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4451 Record.push_back(EXTENSION_METADATA);
4452 Record.push_back(Metadata.MajorVersion);
4453 Record.push_back(Metadata.MinorVersion);
4454 Record.push_back(Metadata.BlockName.size());
4455 Record.push_back(Metadata.UserInfo.size());
4456 SmallString<64> Buffer;
4457 Buffer += Metadata.BlockName;
4458 Buffer += Metadata.UserInfo;
4459 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4460
4461 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004462 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004463
4464 // Exit the extension block.
4465 Stream.ExitBlock();
4466}
4467
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004468//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004469// General Serialization Routines
4470//===----------------------------------------------------------------------===//
4471
Richard Smithe43e2b32018-08-20 21:47:29 +00004472void ASTRecordWriter::AddAttr(const Attr *A) {
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004473 auto &Record = *this;
Richard Smithe43e2b32018-08-20 21:47:29 +00004474 if (!A)
4475 return Record.push_back(0);
4476 Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
4477 Record.AddSourceRange(A->getRange());
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00004478
4479#include "clang/Serialization/AttrPCHWrite.inc"
Richard Smithe43e2b32018-08-20 21:47:29 +00004480}
4481
4482/// Emit the list of attributes to the specified record.
4483void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4484 push_back(Attrs.size());
4485 for (const auto *A : Attrs)
4486 AddAttr(A);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004487}
4488
John McCallf413f5e2013-05-03 00:10:13 +00004489void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4490 AddSourceLocation(Tok.getLocation(), Record);
4491 Record.push_back(Tok.getLength());
4492
4493 // FIXME: When reading literal tokens, reconstruct the literal pointer
4494 // if it is needed.
4495 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4496 // FIXME: Should translate token kind to a stable encoding.
4497 Record.push_back(Tok.getKind());
4498 // FIXME: Should translate token flags to a stable encoding.
4499 Record.push_back(Tok.getFlags());
4500}
4501
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004502void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004503 Record.push_back(Str.size());
4504 Record.insert(Record.end(), Str.begin(), Str.end());
4505}
4506
Richard Smith7ed1bc92014-12-05 22:42:13 +00004507bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004508 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004509
Richard Smith54cc3c22014-12-11 20:50:24 +00004510 bool Changed =
4511 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004512
4513 // Remove a prefix to make the path relative, if relevant.
4514 const char *PathBegin = Path.data();
4515 const char *PathPtr =
4516 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4517 if (PathPtr != PathBegin) {
4518 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4519 Changed = true;
4520 }
4521
4522 return Changed;
4523}
4524
4525void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4526 SmallString<128> FilePath(Path);
4527 PreparePathForOutput(FilePath);
4528 AddString(FilePath, Record);
4529}
4530
Mehdi Amini57a41912015-09-10 01:46:39 +00004531void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004532 StringRef Path) {
4533 SmallString<128> FilePath(Path);
4534 PreparePathForOutput(FilePath);
4535 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4536}
4537
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004538void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4539 RecordDataImpl &Record) {
4540 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004541 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004542 Record.push_back(*Minor + 1);
4543 else
4544 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004545 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004546 Record.push_back(*Subminor + 1);
4547 else
4548 Record.push_back(0);
4549}
4550
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004551/// Note that the identifier II occurs at the given offset
Douglas Gregore84a9da2009-04-20 20:36:09 +00004552/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004553void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004554 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004555 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004556 // up earlier in the chain and thus don't need an offset.
4557 if (ID >= FirstIdentID)
4558 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004559}
4560
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004561/// Note that the selector Sel occurs at the given offset
Douglas Gregor95c13f52009-04-25 17:48:32 +00004562/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004563void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004564 unsigned ID = SelectorIDs[Sel];
4565 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004566 // Don't record offsets for selectors that are also available in a different
4567 // file.
4568 if (ID < FirstSelectorID)
4569 return;
4570 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004571}
4572
David Blaikie61137e12017-01-05 18:23:18 +00004573ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004574 SmallVectorImpl<char> &Buffer, MemoryBufferCache &PCMCache,
David Blaikie61137e12017-01-05 18:23:18 +00004575 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4576 bool IncludeTimestamps)
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004577 : Stream(Stream), Buffer(Buffer), PCMCache(PCMCache),
4578 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004579 for (const auto &Ext : Extensions) {
4580 if (auto Writer = Ext->createExtensionWriter(*this))
4581 ModuleFileExtensionWriters.push_back(std::move(Writer));
4582 }
4583}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004584
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004585ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004586 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004587}
4588
Richard Smithb3761912015-02-05 23:08:52 +00004589const LangOptions &ASTWriter::getLangOpts() const {
4590 assert(WritingAST && "can't determine lang opts when not writing AST");
4591 return Context->getLangOpts();
4592}
4593
Richard Smithe75ee0f2015-08-17 07:13:32 +00004594time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4595 return IncludeTimestamps ? E->getModificationTime() : 0;
4596}
4597
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004598ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4599 const std::string &OutputFile,
4600 Module *WritingModule, StringRef isysroot,
4601 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004602 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004603
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004604 ASTHasCompilerErrors = hasErrors;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004605
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004606 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004607 Stream.Emit((unsigned)'C', 8);
4608 Stream.Emit((unsigned)'P', 8);
4609 Stream.Emit((unsigned)'C', 8);
4610 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004611
Chris Lattner28fa4e62009-04-26 22:26:21 +00004612 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004613
Douglas Gregoreda8e122011-08-09 15:13:55 +00004614 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004615 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004616 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004617 ASTFileSignature Signature =
4618 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004619 Context = nullptr;
4620 PP = nullptr;
4621 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004622 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004623
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004624 WritingAST = false;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004625 if (SemaRef.Context.getLangOpts().ImplicitModules && WritingModule) {
4626 // Construct MemoryBuffer and update buffer manager.
4627 PCMCache.addBuffer(OutputFile,
4628 llvm::MemoryBuffer::getMemBufferCopy(
4629 StringRef(Buffer.begin(), Buffer.size())));
4630 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004631 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004632}
4633
Douglas Gregora94a1542011-07-27 21:45:57 +00004634template<typename Vector>
4635static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4636 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004637 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4638 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004639 Writer.AddDeclRef(*I, Record);
4640 }
4641}
4642
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004643ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4644 const std::string &OutputFile,
4645 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004646 using namespace llvm;
4647
Craig Toppera13603a2014-05-22 05:54:18 +00004648 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004649
Douglas Gregorcf68c582011-12-01 22:20:10 +00004650 // Make sure that the AST reader knows to finalize itself.
4651 if (Chain)
4652 Chain->finalizeForWriting();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004653
Sebastian Redl143413f2010-07-12 22:02:52 +00004654 ASTContext &Context = SemaRef.Context;
4655 Preprocessor &PP = SemaRef.PP;
4656
Douglas Gregordab42432011-08-12 00:15:20 +00004657 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004658 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4659 if (D) {
4660 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4661 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004662 }
4663 };
4664 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4665 PREDEF_DECL_TRANSLATION_UNIT_ID);
4666 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4667 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4668 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4669 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4670 PREDEF_DECL_OBJC_PROTOCOL_ID);
4671 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4672 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4673 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4674 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4675 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004676 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004677 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4678 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004679 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004680 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4681 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004682 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4683 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004684 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4685 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004686 RegisterPredefDecl(Context.TypePackElementDecl,
4687 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004688
Chris Lattner0c797362009-09-08 18:19:27 +00004689 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004690 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004691 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004692 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004693 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004694
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004695 // Build a record containing all of the file scoped decls in this file.
4696 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004697 if (!isModule)
4698 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4699 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004700
Douglas Gregor851443c2011-08-12 01:39:19 +00004701 // Build a record containing all of the delegating constructors we still need
4702 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004703 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004704 if (!isModule)
4705 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004706
Douglas Gregor851443c2011-08-12 01:39:19 +00004707 // Write the set of weak, undeclared identifiers. We always write the
4708 // entire table, since later PCH files in a PCH chain are only interested in
4709 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004710 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004711 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4712 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4713 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4714 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4715 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4716 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4717 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004718 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004719
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004720 // Build a record containing all of the ext_vector declarations.
4721 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004722 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004723
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004724 // Build a record containing all of the VTable uses information.
4725 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004726 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004727 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4728 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4729 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4730 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4731 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004732 }
4733
Nico Weber72889432014-09-06 01:25:55 +00004734 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4735 RecordData UnusedLocalTypedefNameCandidates;
4736 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4737 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4738
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004739 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004740 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004741 for (const auto &I : SemaRef.PendingInstantiations) {
4742 AddDeclRef(I.first, PendingInstantiations);
4743 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004744 }
4745 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4746 "There are local ones at end of translation unit!");
4747
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004748 // Build a record containing some declaration references.
4749 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004750 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004751 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4752 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004753 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004754 }
4755
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004756 RecordData CUDASpecialDeclRefs;
4757 if (Context.getcudaConfigureCallDecl()) {
4758 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4759 }
4760
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004761 // Build a record containing all of the known namespaces.
4762 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004763 for (const auto &I : SemaRef.KnownNamespaces) {
4764 if (!I.second)
4765 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004766 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004767
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004768 // Build a record of all used, undefined objects that require definitions.
4769 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004770
4771 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004772 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004773 for (const auto &I : Undefined) {
4774 AddDeclRef(I.first, UndefinedButUsed);
4775 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004776 }
4777
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004778 // Build a record containing all delete-expressions that we would like to
4779 // analyze later in AST.
4780 RecordData DeleteExprsToAnalyze;
4781
Richard Smithf5262c62018-06-28 01:57:04 +00004782 if (!isModule) {
4783 for (const auto &DeleteExprsInfo :
4784 SemaRef.getMismatchingDeleteExpressions()) {
4785 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4786 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4787 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4788 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4789 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4790 }
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004791 }
4792 }
4793
Douglas Gregor112b9072012-10-18 05:31:06 +00004794 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004795 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004796
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004797 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004798 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004799
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004800 // This is so that older clang versions, before the introduction
4801 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004802 {
4803 RecordData Record = {VERSION_MAJOR};
4804 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4805 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004806
Douglas Gregor851443c2011-08-12 01:39:19 +00004807 // Create a lexical update block containing all of the declarations in the
4808 // translation unit that do not come from other AST files.
4809 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004810 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4811 for (const auto *D : TU->noload_decls()) {
4812 if (!D->isFromASTFile()) {
4813 NewGlobalKindDeclPairs.push_back(D->getKind());
4814 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4815 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004816 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004817
David Blaikieb44f0bf2017-01-04 22:36:43 +00004818 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004819 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4820 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004821 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004822 {
4823 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4824 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4825 bytes(NewGlobalKindDeclPairs));
4826 }
4827
Douglas Gregor851443c2011-08-12 01:39:19 +00004828 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004829 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004830 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4831 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004832 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004833 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004834 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004835
4836 // If we have any extern "C" names, write out a visible update for them.
4837 if (Context.ExternCContext)
4838 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004839
Douglas Gregor851443c2011-08-12 01:39:19 +00004840 // If the translation unit has an anonymous namespace, and we don't already
4841 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004842 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004843 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4844 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004845 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004846 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004847 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004848
Richard Smith5652c0f2014-03-21 01:48:23 +00004849 // Add update records for all mangling numbers and static local numbers.
4850 // These aren't really update records, but this is a convenient way of
4851 // tagging this rare extra data onto the declarations.
4852 for (const auto &Number : Context.MangleNumbers)
4853 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004854 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4855 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004856 for (const auto &Number : Context.StaticLocalNumbers)
4857 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004858 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4859 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004860
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004861 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004862 // an AST file, are registered for serialization. Likewise for template
4863 // specializations added to imported templates.
4864 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004865 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004866 }
4867
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004868 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004869 // serialization, if we're storing decls with identifiers.
4870 if (!WritingModule || !getLangOpts().CPlusPlus) {
4871 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004872 for (const auto &ID : PP.getIdentifierTable()) {
4873 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004874 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4875 IIs.push_back(II);
4876 }
4877 // Sort the identifiers to visit based on their name.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004878 llvm::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
Richard Smith79bf9202015-08-24 03:33:22 +00004879 for (const IdentifierInfo *II : IIs) {
4880 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4881 DEnd = SemaRef.IdResolver.end();
4882 D != DEnd; ++D) {
4883 GetDeclRef(*D);
4884 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004885 }
4886 }
4887
Manman Rena0f31a02016-04-29 19:04:05 +00004888 // For method pool in the module, if it contains an entry for a selector,
4889 // the entry should be complete, containing everything introduced by that
4890 // module and all modules it imports. It's possible that the entry is out of
4891 // date, so we need to pull in the new content here.
4892
4893 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4894 // safe, we copy all selectors out.
4895 llvm::SmallVector<Selector, 256> AllSelectors;
4896 for (auto &SelectorAndID : SelectorIDs)
4897 AllSelectors.push_back(SelectorAndID.first);
4898 for (auto &Selector : AllSelectors)
4899 SemaRef.updateOutOfDateSelector(Selector);
4900
Douglas Gregor5204bde2011-08-02 16:26:37 +00004901 // Form the record of special types.
4902 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004903 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004904 AddTypeRef(Context.getFILEType(), SpecialTypes);
4905 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4906 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4907 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4908 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004909 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004910 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004911
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004912 if (Chain) {
4913 // Write the mapping information describing our module dependencies and how
4914 // each of those modules were mapped into our own offset/ID space, so that
4915 // the reader can build the appropriate mapping to its own offset/ID space.
4916 // The map consists solely of a blob with the following format:
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004917 // *(module-kind:i8
4918 // module-name-len:i16 module-name:len*i8
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004919 // source-location-offset:i32
4920 // identifier-id:i32
4921 // preprocessed-entity-id:i32
4922 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004923 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004924 // selector-id:i32
4925 // declaration-id:i32
4926 // c++-base-specifiers-id:i32
4927 // type-id:i32)
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004928 //
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004929 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule or
4930 // MK_ExplicitModule, then the module-name is the module name. Otherwise,
4931 // it is the module file name.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004932 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004933 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004935 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004936 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004937 {
4938 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004939 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004940 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00004941
Peter Collingbournee3f65292018-05-18 19:46:24 +00004942 endian::Writer LE(Out, little);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004943 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
4944 StringRef Name =
4945 M.Kind == MK_PrebuiltModule || M.Kind == MK_ExplicitModule
4946 ? M.ModuleName
4947 : M.FileName;
4948 LE.write<uint16_t>(Name.size());
4949 Out.write(Name.data(), Name.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004950
Ben Langmuir785180e2014-10-20 16:27:30 +00004951 // Note: if a base ID was uint max, it would not be possible to load
4952 // another module after it or have more than one entity inside it.
4953 uint32_t None = std::numeric_limits<uint32_t>::max();
4954
4955 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4956 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4957 if (ShouldWrite)
4958 LE.write<uint32_t>(BaseID);
4959 else
4960 LE.write<uint32_t>(None);
4961 };
4962
Ben Langmuirfe971d92014-08-16 04:54:18 +00004963 // These values should be unique within a chain, since they will be read
4964 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004965 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4966 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4967 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4968 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4969 M.NumPreprocessedEntities);
4970 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4971 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4972 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4973 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004974 }
4975 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004976 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004977 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4978 Buffer.data(), Buffer.size());
4979 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004980
Richard Smithb9eab6d2014-03-20 19:44:17 +00004981 RecordData DeclUpdatesOffsetsRecord;
4982
Richard Smith59442b42014-03-20 20:07:19 +00004983 // Keep writing types, declarations, and declaration update records
4984 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004985 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4986 WriteTypeAbbrevs();
4987 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004988 do {
4989 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4990 while (!DeclTypesToEmit.empty()) {
4991 DeclOrType DOT = DeclTypesToEmit.front();
4992 DeclTypesToEmit.pop();
4993 if (DOT.isType())
4994 WriteType(DOT.getType());
4995 else
4996 WriteDecl(Context, DOT.getDecl());
4997 }
4998 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004999 Stream.ExitBlock();
5000
Richard Smithb9eab6d2014-03-20 19:44:17 +00005001 DoneWritingDeclsAndTypes = true;
5002
5003 // These things can only be done once we've written out decls and types.
5004 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00005005 if (!DeclUpdatesOffsetsRecord.empty())
5006 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00005007 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00005008 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00005009 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005010 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005011 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00005012 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00005013 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00005014 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005015 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00005016 WriteFPPragmaOptions(SemaRef.getFPOptions());
5017 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00005018 WriteOpenCLExtensionTypes(SemaRef);
5019 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00005020 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00005021
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005022 // If we're emitting a module, write out the submodule information.
Douglas Gregora89c5ac2011-12-06 01:10:29 +00005023 if (WritingModule)
5024 WriteSubmodules(WritingModule);
5025
Douglas Gregor5204bde2011-08-02 16:26:37 +00005026 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5027
Douglas Gregord4df8652009-04-22 22:02:47 +00005028 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00005029 if (!EagerlyDeserializedDecls.empty())
5030 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00005031
David Blaikief63556d2017-04-12 20:58:33 +00005032 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00005033 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5034
Douglas Gregord4df8652009-04-22 22:02:47 +00005035 // Write the record containing tentative definitions.
5036 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005037 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00005038
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00005039 // Write the record containing unused file scoped decls.
5040 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005041 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005042
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005043 // Write the record containing weak undeclared identifiers.
5044 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005045 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005046 WeakUndeclaredIdentifiers);
5047
Douglas Gregor61cac2b2009-04-27 20:06:05 +00005048 // Write the record containing ext_vector type names.
5049 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005050 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00005051
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005052 // Write the record containing VTable uses information.
5053 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005054 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005055
Nico Weber72889432014-09-06 01:25:55 +00005056 // Write the record containing potentially unused local typedefs.
5057 if (!UnusedLocalTypedefNameCandidates.empty())
5058 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5059 UnusedLocalTypedefNameCandidates);
5060
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005061 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00005062 if (!PendingInstantiations.empty())
5063 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005064
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005065 // Write the record containing declaration references of Sema.
5066 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005067 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005068
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005069 // Write the record containing CUDA-specific declaration references.
5070 if (!CUDASpecialDeclRefs.empty())
5071 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005072
Alexis Hunt27a761d2011-05-04 23:29:54 +00005073 // Write the delegating constructors.
5074 if (!DelegatingCtorDecls.empty())
5075 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005076
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005077 // Write the known namespaces.
5078 if (!KnownNamespaces.empty())
5079 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00005080
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00005081 // Write the undefined internal functions and variables, and inline functions.
5082 if (!UndefinedButUsed.empty())
5083 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00005084
5085 if (!DeleteExprsToAnalyze.empty())
5086 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5087
Douglas Gregor851443c2011-08-12 01:39:19 +00005088 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00005089 for (auto *DC : UpdatedDeclContexts)
5090 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00005091
Douglas Gregor959bb062011-12-03 01:15:29 +00005092 if (!WritingModule) {
5093 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00005094 struct ModuleInfo {
5095 uint64_t ID;
5096 Module *M;
5097 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5098 };
Richard Smith56be7542014-03-21 00:33:59 +00005099 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00005100 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00005101 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005102 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5103 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00005104 }
Richard Smith56be7542014-03-21 00:33:59 +00005105
5106 if (!Imports.empty()) {
5107 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5108 return A.ID < B.ID;
5109 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005110 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5111 return A.ID == B.ID;
5112 };
Richard Smith56be7542014-03-21 00:33:59 +00005113
5114 // Sort and deduplicate module IDs.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00005115 llvm::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005116 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00005117 Imports.end());
5118
5119 RecordData ImportedModules;
5120 for (const auto &Import : Imports) {
5121 ImportedModules.push_back(Import.ID);
5122 // FIXME: If the module has macros imported then later has declarations
5123 // imported, this location won't be the right one as a location for the
5124 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00005125 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00005126 }
5127
Douglas Gregor959bb062011-12-03 01:15:29 +00005128 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5129 }
Douglas Gregor0a839132011-12-03 00:59:55 +00005130 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005131
Douglas Gregor404cdde2012-01-27 01:47:08 +00005132 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00005133 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00005134 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005135 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00005136 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005137 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00005138 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00005139
Douglas Gregor08f01292009-04-17 22:13:46 +00005140 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00005141 RecordData::value_type Record[] = {
5142 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00005143 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00005144 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00005145
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005146 // Write the module file extension blocks.
5147 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00005148 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005149
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005150 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005151}
5152
Richard Smithb9eab6d2014-03-20 19:44:17 +00005153void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005154 if (DeclUpdates.empty())
5155 return;
5156
Richard Smith59442b42014-03-20 20:07:19 +00005157 DeclUpdateMap LocalUpdates;
5158 LocalUpdates.swap(DeclUpdates);
5159
Richard Smith6ef42932014-03-20 21:02:00 +00005160 for (auto &DeclUpdate : LocalUpdates) {
5161 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00005162
Richard Smithd28ac5b2014-03-22 23:33:22 +00005163 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00005164 RecordData RecordData;
5165 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00005166 for (auto &Update : DeclUpdate.second) {
5167 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5168
Richard Smith69c82bf2016-04-01 22:52:03 +00005169 // An updated body is emitted last, so that the reader doesn't need
5170 // to skip over the lazy body to reach statements for other records.
5171 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5172 HasUpdatedBody = true;
5173 else
5174 Record.push_back(Kind);
5175
Richard Smith6ef42932014-03-20 21:02:00 +00005176 switch (Kind) {
5177 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5178 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5179 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00005180 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00005181 Record.push_back(GetDeclRef(Update.getDecl()));
5182 break;
5183
Richard Smith4d235792014-08-07 18:53:08 +00005184 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00005185 break;
5186
Richard Smith891fc7f2017-12-05 01:31:47 +00005187 case UPD_CXX_POINT_OF_INSTANTIATION:
5188 // FIXME: Do we need to also save the template specialization kind here?
Richard Smith69c82bf2016-04-01 22:52:03 +00005189 Record.AddSourceLocation(Update.getLoc());
Richard Smith891fc7f2017-12-05 01:31:47 +00005190 break;
5191
5192 case UPD_CXX_ADDED_VAR_DEFINITION: {
5193 const VarDecl *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00005194 Record.push_back(VD->isInline());
5195 Record.push_back(VD->isInlineSpecified());
Richard Smith05a21352017-06-22 22:18:46 +00005196 if (VD->getInit()) {
5197 Record.push_back(!VD->isInitKnownICE() ? 1
5198 : (VD->isInitICE() ? 3 : 2));
5199 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
5200 } else {
5201 Record.push_back(0);
5202 }
Richard Smith4d235792014-08-07 18:53:08 +00005203 break;
Richard Smith05a21352017-06-22 22:18:46 +00005204 }
Richard Smith4d235792014-08-07 18:53:08 +00005205
John McCall32791cc2016-01-06 22:34:54 +00005206 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00005207 Record.AddStmt(const_cast<Expr *>(
5208 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00005209 break;
5210
Richard Smith4b054b22016-08-24 21:25:37 +00005211 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5212 Record.AddStmt(
5213 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5214 break;
5215
Richard Smithcd45dbc2014-04-19 03:48:30 +00005216 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5217 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005218 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00005219 Record.push_back(RD->isParamDestroyedInCallee());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00005220 Record.push_back(RD->getArgPassingRestrictions());
Richard Smith69c82bf2016-04-01 22:52:03 +00005221 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00005222 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00005223 *Context, const_cast<CXXRecordDecl *>(RD)));
5224
5225 // This state is sometimes updated by template instantiation, when we
5226 // switch from the specialization referring to the template declaration
5227 // to it referring to the template definition.
5228 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5229 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005230 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005231 } else {
5232 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5233 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005234 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00005235
5236 // The instantiation might have been resolved to a partial
5237 // specialization. If so, record which one.
5238 auto From = Spec->getInstantiatedFrom();
5239 if (auto PartialSpec =
5240 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5241 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00005242 Record.AddDeclRef(PartialSpec);
5243 Record.AddTemplateArgumentList(
5244 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00005245 } else {
5246 Record.push_back(false);
5247 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00005248 }
5249 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005250 Record.AddSourceLocation(RD->getLocation());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005251 Record.AddSourceLocation(RD->getBeginLoc());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00005252 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005253
5254 // Instantiation may change attributes; write them all out afresh.
5255 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00005256 if (D->hasAttrs())
5257 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005258
5259 // FIXME: Ensure we don't get here for explicit instantiations.
5260 break;
5261 }
5262
Richard Smithf8134002015-03-10 01:41:22 +00005263 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005264 Record.AddDeclRef(Update.getDecl());
Richard Smith5b349582017-10-13 01:55:36 +00005265 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
Richard Smithf8134002015-03-10 01:41:22 +00005266 break;
5267
Richard Smith564417a2014-03-20 21:47:22 +00005268 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
5269 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00005270 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
5271 Record);
5272 break;
5273
Richard Smith6ef42932014-03-20 21:02:00 +00005274 case UPD_CXX_DEDUCED_RETURN_TYPE:
5275 Record.push_back(GetOrCreateTypeID(Update.getType()));
5276 break;
5277
5278 case UPD_DECL_MARKED_USED:
5279 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00005280
5281 case UPD_MANGLING_NUMBER:
5282 case UPD_STATIC_LOCAL_NUMBER:
5283 Record.push_back(Update.getNumber());
5284 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005285
Alexey Bataev97720002014-11-11 04:05:39 +00005286 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005287 Record.AddSourceRange(
5288 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005289 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005290
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005291 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00005292 Record.push_back(D->getAttr<OMPDeclareTargetDeclAttr>()->getMapType());
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005293 Record.AddSourceRange(
5294 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5295 break;
5296
Richard Smith65ebb4a2015-03-26 04:09:53 +00005297 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005298 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005299 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005300
5301 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005302 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005303 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005304 }
5305 }
5306
Richard Smithd28ac5b2014-03-22 23:33:22 +00005307 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005308 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005309 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005310 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005311 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005312 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005313 }
5314
Richard Smithcd45dbc2014-04-19 03:48:30 +00005315 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005316 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005317 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005318}
5319
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005320void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005321 uint32_t Raw = Loc.getRawEncoding();
5322 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005323}
5324
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005325void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005326 AddSourceLocation(Range.getBegin(), Record);
5327 AddSourceLocation(Range.getEnd(), Record);
5328}
5329
Richard Smithf144b542016-04-08 21:54:32 +00005330void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5331 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005332 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005333 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005334}
5335
Richard Smithf144b542016-04-08 21:54:32 +00005336void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5337 Record->push_back(Value.isUnsigned());
5338 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005339}
5340
Richard Smithf144b542016-04-08 21:54:32 +00005341void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5342 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005343}
5344
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005345void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005346 Record.push_back(getIdentifierRef(II));
5347}
5348
Sebastian Redl539c5062010-08-18 23:57:32 +00005349IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005350 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005351 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005352
Sebastian Redl539c5062010-08-18 23:57:32 +00005353 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005354 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005355 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005356 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005357}
5358
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005359MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005360 // Don't emit builtin macros like __LINE__ to the AST file unless they
5361 // have been redefined by the header (in which case they are not
5362 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005363 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005364 return 0;
5365
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005366 MacroID &ID = MacroIDs[MI];
5367 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005368 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005369 MacroInfoToEmitData Info = { Name, MI, ID };
5370 MacroInfosToEmit.push_back(Info);
5371 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005372 return ID;
5373}
5374
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005375MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005376 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005377 return 0;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005378
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005379 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5380 return MacroIDs[MI];
5381}
5382
5383uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005384 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005385}
5386
Richard Smithe64e7452016-04-18 21:54:58 +00005387void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5388 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005389}
5390
Sebastian Redl539c5062010-08-18 23:57:32 +00005391SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005392 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005393 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005394 }
5395
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005396 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005397 if (SID == 0 && Chain) {
5398 // This might trigger a ReadSelector callback, which will set the ID for
5399 // this selector.
5400 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005401 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005402 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005403 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005404 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005405 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005406 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005407 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005408}
5409
Richard Smithe64e7452016-04-18 21:54:58 +00005410void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5411 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005412}
5413
Richard Smith290d8012016-04-06 17:06:00 +00005414void ASTRecordWriter::AddTemplateArgumentLocInfo(
5415 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005416 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005417 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005418 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005419 break;
5420 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005421 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005422 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005423 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005424 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5425 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005426 break;
5427 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005428 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5429 AddSourceLocation(Arg.getTemplateNameLoc());
5430 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005431 break;
John McCall0ad16662009-10-29 08:12:44 +00005432 case TemplateArgument::Null:
5433 case TemplateArgument::Integral:
5434 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005435 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005436 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005437 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005438 break;
5439 }
5440}
5441
Richard Smith290d8012016-04-06 17:06:00 +00005442void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5443 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005444
5445 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5446 bool InfoHasSameExpr
5447 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005448 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005449 if (InfoHasSameExpr)
5450 return; // Avoid storing the same expr twice.
5451 }
Richard Smith290d8012016-04-06 17:06:00 +00005452 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005453}
5454
Richard Smith290d8012016-04-06 17:06:00 +00005455void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005456 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005457 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005458 return;
5459 }
5460
Richard Smithc23d7342018-06-29 20:46:25 +00005461 AddTypeRef(TInfo->getType());
Richard Smith290d8012016-04-06 17:06:00 +00005462 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005463}
5464
Richard Smith290d8012016-04-06 17:06:00 +00005465void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
Richard Smith290d8012016-04-06 17:06:00 +00005466 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005467 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005468 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005469}
5470
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005471void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005472 Record.push_back(GetOrCreateTypeID(T));
5473}
5474
Richard Smith2095ffe2015-03-16 20:11:03 +00005475TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005476 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005477 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5478 if (T.isNull())
5479 return TypeIdx();
5480 assert(!T.getLocalFastQualifiers());
5481
5482 TypeIdx &Idx = TypeIdxs[T];
5483 if (Idx.getIndex() == 0) {
5484 if (DoneWritingDeclsAndTypes) {
5485 assert(0 && "New type seen after serializing all the types to emit!");
5486 return TypeIdx();
5487 }
5488
5489 // We haven't seen this type before. Assign it a new ID and put it
5490 // into the queue of types to emit.
5491 Idx = TypeIdx(NextTypeID++);
5492 DeclTypesToEmit.push(T);
5493 }
5494 return Idx;
5495 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005496}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005497
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005498TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005499 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005500 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5501 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005502 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005503 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005504
Richard Smith2095ffe2015-03-16 20:11:03 +00005505 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5506 assert(I != TypeIdxs.end() && "Type not emitted!");
5507 return I->second;
5508 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005509}
5510
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005511void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005512 Record.push_back(GetDeclRef(D));
5513}
5514
Sebastian Redl539c5062010-08-18 23:57:32 +00005515DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005516 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005517
5518 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005519 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005520 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005521
Douglas Gregorb3163e52012-01-05 22:33:30 +00005522 // If D comes from an AST file, its declaration ID is already known and
5523 // fixed.
5524 if (D->isFromASTFile())
5525 return D->getGlobalID();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005526
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005527 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005528 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005529 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005530 if (DoneWritingDeclsAndTypes) {
5531 assert(0 && "New decl seen after serializing all the decls to emit!");
5532 return 0;
5533 }
5534
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005535 // We haven't seen this declaration before. Give it a new ID and
5536 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005537 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005538 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005539 }
5540
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005541 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005542}
5543
Sebastian Redl539c5062010-08-18 23:57:32 +00005544DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005545 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005546 return 0;
5547
Douglas Gregorb3163e52012-01-05 22:33:30 +00005548 // If D comes from an AST file, its declaration ID is already known and
5549 // fixed.
5550 if (D->isFromASTFile())
5551 return D->getGlobalID();
5552
Douglas Gregore84a9da2009-04-20 20:36:09 +00005553 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5554 return DeclIDs[D];
5555}
5556
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005557void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005558 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005559 assert(D);
5560
5561 SourceLocation Loc = D->getLocation();
5562 if (Loc.isInvalid())
5563 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005564
5565 // We only keep track of the file-level declarations of each file.
5566 if (!D->getLexicalDeclContext()->isFileContext())
5567 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005568 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5569 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis71b74eb2018-01-26 19:26:12 +00005570 // TemplateTemplateParmDecls that are part of an alias template, should not
5571 // have TU as lexical context.
5572 if (isa<ParmVarDecl>(D) || isa<TemplateTemplateParmDecl>(D))
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005573 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005574
5575 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005576 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005577 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005578 FileID FID;
5579 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005580 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005581 if (FID.isInvalid())
5582 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005583 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005584
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005585 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005586 if (!Info)
5587 Info = new DeclIDInFileInfo();
5588
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005589 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005590 LocDeclIDsTy &Decls = Info->DeclIDs;
5591
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005592 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005593 Decls.push_back(LocDecl);
5594 return;
5595 }
5596
Benjamin Kramer45025c02013-08-24 13:22:59 +00005597 LocDeclIDsTy::iterator I =
5598 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005599
5600 Decls.insert(I, LocDecl);
5601}
5602
Richard Smithe64e7452016-04-18 21:54:58 +00005603void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005604 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005605 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005606 switch (Name.getNameKind()) {
5607 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005608 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005609 break;
5610
5611 case DeclarationName::ObjCZeroArgSelector:
5612 case DeclarationName::ObjCOneArgSelector:
5613 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005614 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005615 break;
5616
5617 case DeclarationName::CXXConstructorName:
5618 case DeclarationName::CXXDestructorName:
5619 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005620 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005621 break;
5622
Richard Smith35845152017-02-07 01:37:30 +00005623 case DeclarationName::CXXDeductionGuideName:
5624 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5625 break;
5626
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005627 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005628 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005629 break;
5630
Alexis Hunt3d221f22009-11-29 07:34:05 +00005631 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005632 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005633 break;
5634
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005635 case DeclarationName::CXXUsingDirective:
5636 // No extra data to emit
5637 break;
5638 }
5639}
Chris Lattnerca025db2010-05-07 21:43:38 +00005640
Richard Smithd08aeb62014-08-28 01:33:39 +00005641unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5642 assert(needsAnonymousDeclarationNumber(D) &&
5643 "expected an anonymous declaration");
5644
5645 // Number the anonymous declarations within this context, if we've not
5646 // already done so.
5647 auto It = AnonymousDeclarationNumbers.find(D);
5648 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005649 auto *DC = D->getLexicalDeclContext();
5650 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5651 AnonymousDeclarationNumbers[ND] = Number;
5652 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005653
5654 It = AnonymousDeclarationNumbers.find(D);
5655 assert(It != AnonymousDeclarationNumbers.end() &&
5656 "declaration not found within its lexical context");
5657 }
5658
5659 return It->second;
5660}
5661
Richard Smith290d8012016-04-06 17:06:00 +00005662void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5663 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005664 switch (Name.getNameKind()) {
5665 case DeclarationName::CXXConstructorName:
5666 case DeclarationName::CXXDestructorName:
5667 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005668 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005669 break;
5670
5671 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005672 AddSourceLocation(SourceLocation::getFromRawEncoding(
5673 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005674 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005675 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005676 break;
5677
5678 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005679 AddSourceLocation(SourceLocation::getFromRawEncoding(
5680 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005681 break;
5682
5683 case DeclarationName::Identifier:
5684 case DeclarationName::ObjCZeroArgSelector:
5685 case DeclarationName::ObjCOneArgSelector:
5686 case DeclarationName::ObjCMultiArgSelector:
5687 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005688 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005689 break;
5690 }
5691}
5692
Richard Smith290d8012016-04-06 17:06:00 +00005693void ASTRecordWriter::AddDeclarationNameInfo(
5694 const DeclarationNameInfo &NameInfo) {
5695 AddDeclarationName(NameInfo.getName());
5696 AddSourceLocation(NameInfo.getLoc());
5697 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005698}
5699
Richard Smith290d8012016-04-06 17:06:00 +00005700void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5701 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5702 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005703 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005704 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005705}
5706
Richard Smithe64e7452016-04-18 21:54:58 +00005707void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005708 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005709 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005710 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005711
5712 // Push each of the NNS's onto a stack for serialization in reverse order.
5713 while (NNS) {
5714 NestedNames.push_back(NNS);
5715 NNS = NNS->getPrefix();
5716 }
5717
Richard Smithe64e7452016-04-18 21:54:58 +00005718 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005719 while(!NestedNames.empty()) {
5720 NNS = NestedNames.pop_back_val();
5721 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005722 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005723 switch (Kind) {
5724 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005725 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005726 break;
5727
5728 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005729 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005730 break;
5731
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005732 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005733 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005734 break;
5735
Chris Lattnerca025db2010-05-07 21:43:38 +00005736 case NestedNameSpecifier::TypeSpec:
5737 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005738 AddTypeRef(QualType(NNS->getAsType(), 0));
5739 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005740 break;
5741
5742 case NestedNameSpecifier::Global:
5743 // Don't need to write an associated value.
5744 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005745
5746 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005747 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005748 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005749 }
5750 }
5751}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005752
Richard Smith290d8012016-04-06 17:06:00 +00005753void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005754 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005755 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005756 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005757
5758 // Push each of the nested-name-specifiers's onto a stack for
5759 // serialization in reverse order.
5760 while (NNS) {
5761 NestedNames.push_back(NNS);
5762 NNS = NNS.getPrefix();
5763 }
5764
Richard Smith290d8012016-04-06 17:06:00 +00005765 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005766 while(!NestedNames.empty()) {
5767 NNS = NestedNames.pop_back_val();
5768 NestedNameSpecifier::SpecifierKind Kind
5769 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005770 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005771 switch (Kind) {
5772 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005773 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5774 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005775 break;
5776
5777 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005778 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5779 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005780 break;
5781
5782 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005783 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5784 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005785 break;
5786
5787 case NestedNameSpecifier::TypeSpec:
5788 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005789 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Richard Smithc23d7342018-06-29 20:46:25 +00005790 AddTypeRef(NNS.getTypeLoc().getType());
Richard Smith290d8012016-04-06 17:06:00 +00005791 AddTypeLoc(NNS.getTypeLoc());
5792 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005793 break;
5794
5795 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005796 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005797 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005798
5799 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005800 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5801 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005802 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005803 }
5804 }
5805}
5806
Richard Smith290d8012016-04-06 17:06:00 +00005807void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005808 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005809 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005810 switch (Kind) {
5811 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005812 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005813 break;
5814
5815 case TemplateName::OverloadedTemplate: {
5816 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005817 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005818 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005819 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005820 break;
5821 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005822
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005823 case TemplateName::QualifiedTemplate: {
5824 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005825 AddNestedNameSpecifier(QualT->getQualifier());
5826 Record->push_back(QualT->hasTemplateKeyword());
5827 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005828 break;
5829 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005830
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005831 case TemplateName::DependentTemplate: {
5832 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005833 AddNestedNameSpecifier(DepT->getQualifier());
5834 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005835 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005836 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005837 else
Richard Smith290d8012016-04-06 17:06:00 +00005838 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005839 break;
5840 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005841
5842 case TemplateName::SubstTemplateTemplateParm: {
5843 SubstTemplateTemplateParmStorage *subst
5844 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005845 AddDeclRef(subst->getParameter());
5846 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005847 break;
5848 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005849
Douglas Gregor5590be02011-01-15 06:45:20 +00005850 case TemplateName::SubstTemplateTemplateParmPack: {
5851 SubstTemplateTemplateParmPackStorage *SubstPack
5852 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005853 AddDeclRef(SubstPack->getParameterPack());
5854 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005855 break;
5856 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005857 }
5858}
5859
Richard Smith290d8012016-04-06 17:06:00 +00005860void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5861 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005862 switch (Arg.getKind()) {
5863 case TemplateArgument::Null:
5864 break;
5865 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005866 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005867 break;
5868 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005869 AddDeclRef(Arg.getAsDecl());
5870 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005871 break;
5872 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005873 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005874 break;
5875 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005876 AddAPSInt(Arg.getAsIntegral());
5877 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005878 break;
5879 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005880 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005881 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005882 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005883 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005884 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005885 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005886 else
Richard Smith290d8012016-04-06 17:06:00 +00005887 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005888 break;
5889 case TemplateArgument::Expression:
5890 AddStmt(Arg.getAsExpr());
5891 break;
5892 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005893 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005894 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005895 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005896 break;
5897 }
5898}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005899
Richard Smithe64e7452016-04-18 21:54:58 +00005900void ASTRecordWriter::AddTemplateParameterList(
5901 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005902 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005903 AddSourceLocation(TemplateParams->getTemplateLoc());
5904 AddSourceLocation(TemplateParams->getLAngleLoc());
5905 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005906 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005907 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005908 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005909 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005910}
5911
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005912/// Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005913void ASTRecordWriter::AddTemplateArgumentList(
5914 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005915 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005916 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005917 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005918 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005919}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005920
Richard Smith290d8012016-04-06 17:06:00 +00005921void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5922 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005923 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005924 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5925 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5926 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005927 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005928 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005929 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005930}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005931
Richard Smithe64e7452016-04-18 21:54:58 +00005932void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5933 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005934 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005935 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005936 AddDeclRef(I.getDecl());
5937 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005938 }
5939}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005940
Richard Smith290d8012016-04-06 17:06:00 +00005941// FIXME: Move this out of the main ASTRecordWriter interface.
5942void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5943 Record->push_back(Base.isVirtual());
5944 Record->push_back(Base.isBaseOfClass());
5945 Record->push_back(Base.getAccessSpecifierAsWritten());
5946 Record->push_back(Base.getInheritConstructors());
5947 AddTypeSourceInfo(Base.getTypeSourceInfo());
5948 AddSourceRange(Base.getSourceRange());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005949 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005950 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005951}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005952
Richard Smith645d2cf2016-04-14 00:29:55 +00005953static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5954 ArrayRef<CXXBaseSpecifier> Bases) {
5955 ASTWriter::RecordData Record;
5956 ASTRecordWriter Writer(W, Record);
5957 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005958
Richard Smith645d2cf2016-04-14 00:29:55 +00005959 for (auto &Base : Bases)
5960 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005961
Richard Smith645d2cf2016-04-14 00:29:55 +00005962 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005963}
5964
Richard Smith290d8012016-04-06 17:06:00 +00005965// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005966void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5967 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5968}
5969
Richard Smithaa165cf2016-04-13 21:57:08 +00005970static uint64_t
5971EmitCXXCtorInitializers(ASTWriter &W,
5972 ArrayRef<CXXCtorInitializer *> CtorInits) {
5973 ASTWriter::RecordData Record;
5974 ASTRecordWriter Writer(W, Record);
5975 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005976
Richard Smithaa165cf2016-04-13 21:57:08 +00005977 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005978 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005979 Writer.push_back(CTOR_INITIALIZER_BASE);
5980 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5981 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005982 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005983 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5984 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005985 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005986 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5987 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005988 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005989 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5990 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005991 }
Francois Pichetd583da02010-12-04 09:14:42 +00005992
Richard Smithaa165cf2016-04-13 21:57:08 +00005993 Writer.AddSourceLocation(Init->getMemberLocation());
5994 Writer.AddStmt(Init->getInit());
5995 Writer.AddSourceLocation(Init->getLParenLoc());
5996 Writer.AddSourceLocation(Init->getRParenLoc());
5997 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005998 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005999 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006000 }
Richard Smithaa165cf2016-04-13 21:57:08 +00006001
6002 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006003}
6004
Richard Smithaa165cf2016-04-13 21:57:08 +00006005// FIXME: Move this out of the main ASTRecordWriter interface.
6006void ASTRecordWriter::AddCXXCtorInitializers(
6007 ArrayRef<CXXCtorInitializer *> CtorInits) {
6008 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00006009}
6010
Richard Smith290d8012016-04-06 17:06:00 +00006011void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00006012 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00006013 Record->push_back(Data.IsLambda);
6014 Record->push_back(Data.UserDeclaredConstructor);
6015 Record->push_back(Data.UserDeclaredSpecialMembers);
6016 Record->push_back(Data.Aggregate);
6017 Record->push_back(Data.PlainOldData);
6018 Record->push_back(Data.Empty);
6019 Record->push_back(Data.Polymorphic);
6020 Record->push_back(Data.Abstract);
6021 Record->push_back(Data.IsStandardLayout);
Richard Smithb6070db2018-04-05 18:55:37 +00006022 Record->push_back(Data.IsCXX11StandardLayout);
6023 Record->push_back(Data.HasBasesWithFields);
6024 Record->push_back(Data.HasBasesWithNonStaticDataMembers);
Richard Smith290d8012016-04-06 17:06:00 +00006025 Record->push_back(Data.HasPrivateFields);
6026 Record->push_back(Data.HasProtectedFields);
6027 Record->push_back(Data.HasPublicFields);
6028 Record->push_back(Data.HasMutableFields);
6029 Record->push_back(Data.HasVariantMembers);
6030 Record->push_back(Data.HasOnlyCMembers);
6031 Record->push_back(Data.HasInClassInitializer);
6032 Record->push_back(Data.HasUninitializedReferenceMember);
6033 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00006034 Record->push_back(Data.HasInheritedConstructor);
6035 Record->push_back(Data.HasInheritedAssignment);
Richard Smith96cd6712017-08-16 01:49:53 +00006036 Record->push_back(Data.NeedOverloadResolutionForCopyConstructor);
Richard Smith290d8012016-04-06 17:06:00 +00006037 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
6038 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
6039 Record->push_back(Data.NeedOverloadResolutionForDestructor);
Richard Smith96cd6712017-08-16 01:49:53 +00006040 Record->push_back(Data.DefaultedCopyConstructorIsDeleted);
Richard Smith290d8012016-04-06 17:06:00 +00006041 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
6042 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
6043 Record->push_back(Data.DefaultedDestructorIsDeleted);
6044 Record->push_back(Data.HasTrivialSpecialMembers);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006045 Record->push_back(Data.HasTrivialSpecialMembersForCall);
Richard Smith290d8012016-04-06 17:06:00 +00006046 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006047 Record->push_back(Data.DeclaredNonTrivialSpecialMembersForCall);
Richard Smith290d8012016-04-06 17:06:00 +00006048 Record->push_back(Data.HasIrrelevantDestructor);
6049 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
6050 Record->push_back(Data.HasDefaultedDefaultConstructor);
6051 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
6052 Record->push_back(Data.HasConstexprDefaultConstructor);
6053 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
6054 Record->push_back(Data.ComputedVisibleConversions);
6055 Record->push_back(Data.UserProvidedDefaultConstructor);
6056 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00006057 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
6058 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00006059 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
6060 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
6061 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieufd1acbb2017-04-11 21:31:00 +00006062
6063 // getODRHash will compute the ODRHash if it has not been previously computed.
6064 Record->push_back(D->getODRHash());
David Blaikief63556d2017-04-12 20:58:33 +00006065 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
6066 Writer->WritingModule && !D->isDependentType();
6067 Record->push_back(ModulesDebugInfo);
6068 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00006069 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
6070
Richard Smith561fb152012-02-25 07:33:38 +00006071 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006072
Richard Smith290d8012016-04-06 17:06:00 +00006073 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006074 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006075 AddCXXBaseSpecifiers(Data.bases());
6076
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006077 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00006078 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006079 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006080 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006081
Richard Smith290d8012016-04-06 17:06:00 +00006082 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
6083 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006084 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00006085 AddDeclRef(D->getFirstFriend());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006086
Douglas Gregor99ae8062012-02-14 17:54:36 +00006087 // Add lambda-specific data.
6088 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00006089 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00006090 Record->push_back(Lambda.Dependent);
6091 Record->push_back(Lambda.IsGenericLambda);
6092 Record->push_back(Lambda.CaptureDefault);
6093 Record->push_back(Lambda.NumCaptures);
6094 Record->push_back(Lambda.NumExplicitCaptures);
6095 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00006096 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00006097 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00006098 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00006099 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00006100 AddSourceLocation(Capture.getLocation());
6101 Record->push_back(Capture.isImplicit());
6102 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00006103 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00006104 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00006105 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00006106 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00006107 break;
6108 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00006109 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00006110 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00006111 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00006112 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00006113 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00006114 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00006115 break;
6116 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00006117 }
6118 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006119}
6120
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006121void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00006122 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00006123 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00006124 assert(FirstDeclID == NextDeclID &&
6125 FirstTypeID == NextTypeID &&
6126 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006127 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00006128 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00006129 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00006130 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00006131
Sebastian Redl07a89a82010-07-30 00:29:29 +00006132 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006133
Richard Smith7f330cd2015-03-18 01:42:29 +00006134 // Note, this will get called multiple times, once one the reader starts up
6135 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00006136 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6137 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6138 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006139 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00006140 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00006141 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006142 NextDeclID = FirstDeclID;
6143 NextTypeID = FirstTypeID;
6144 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006145 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006146 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00006147 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00006148}
6149
Sebastian Redl539c5062010-08-18 23:57:32 +00006150void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006151 // Always keep the highest ID. See \p TypeRead() for more information.
6152 IdentID &StoredID = IdentifierIDs[II];
6153 if (ID > StoredID)
6154 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00006155}
6156
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006157void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006158 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006159 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006160 if (ID > StoredID)
6161 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006162}
6163
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00006164void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006165 // Always take the highest-numbered type index. This copes with an interesting
6166 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006167 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006168 // keep the higher-numbered entry so that we can properly write it out to
6169 // the AST file.
6170 TypeIdx &StoredIdx = TypeIdxs[T];
6171 if (Idx.getIndex() >= StoredIdx.getIndex())
6172 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006173}
6174
Sebastian Redl539c5062010-08-18 23:57:32 +00006175void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006176 // Always keep the highest ID. See \p TypeRead() for more information.
6177 SelectorID &StoredID = SelectorIDs[S];
6178 if (ID > StoredID)
6179 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00006180}
Douglas Gregor91096292010-10-02 19:29:26 +00006181
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006182void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00006183 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006184 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00006185 MacroDefinitions[MD] = ID;
6186}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006187
Douglas Gregore37a85a2011-12-02 17:30:13 +00006188void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6189 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
6190 SubmoduleIDs[Mod] = ID;
6191}
6192
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006193void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006194 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00006195 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006196 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006197 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006198 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00006199 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006200 // A forward reference was mutated into a definition. Rewrite it.
6201 // FIXME: This happens during template instantiation, should we
6202 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00006203 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6204 "completed a tag from another module but not by instantiation?");
6205 DeclUpdates[RD].push_back(
6206 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006207 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006208 }
6209}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006210
Richard Smithf983f7f2015-10-13 00:23:25 +00006211static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6212 if (D->isFromASTFile())
6213 return true;
6214
Richard Smithf983f7f2015-10-13 00:23:25 +00006215 // The predefined __va_list_tag struct is imported if we imported any decls.
6216 // FIXME: This is a gross hack.
6217 return D == D->getASTContext().getVaListTagDecl();
6218}
6219
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006220void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006221 if (Chain && Chain->isProcessingUpdateRecords()) return;
6222 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00006223 "Should not add lookup results to non-lookup contexts!");
6224
Vassil Vassilev632eac32016-03-16 11:17:04 +00006225 // TU is handled elsewhere.
6226 if (isa<TranslationUnitDecl>(DC))
6227 return;
6228
6229 // Namespaces are handled elsewhere, except for template instantiations of
6230 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6231 // local instantiations are added to an imported context. Only happens when
6232 // adding ADL lookup candidates, for example templated friends.
6233 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6234 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006235 return;
6236
Richard Smithf983f7f2015-10-13 00:23:25 +00006237 // We're only interested in cases where a local declaration is added to an
6238 // imported context.
6239 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6240 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006241
Richard Smith0f4e2c42015-08-06 04:23:48 +00006242 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00006243 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00006244 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00006245 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6246 // We're adding a visible declaration to a predefined decl context. Ensure
6247 // that we write out all of its lookup results so we don't get a nasty
6248 // surprise when we try to emit its lookup table.
6249 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00006250 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00006251 }
Richard Smithc26d9742016-10-06 20:30:51 +00006252 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006253}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006254
6255void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006256 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006257 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00006258
6259 // We're only interested in cases where a local declaration is added to an
6260 // imported context.
6261 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6262 return;
6263
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006264 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00006265 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006266
6267 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00006268 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00006269 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00006270 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006271}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00006272
Richard Smith564417a2014-03-20 21:47:22 +00006273void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006274 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00006275 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6276 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006277 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006278 // If we don't already know the exception specification for this redecl
6279 // chain, add an update record for it.
6280 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6281 ->getType()
6282 ->castAs<FunctionProtoType>()
6283 ->getExceptionSpecType()))
6284 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6285 });
Richard Smith564417a2014-03-20 21:47:22 +00006286}
6287
Richard Smith1fa5d642013-05-11 05:45:24 +00006288void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006289 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00006290 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00006291 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006292 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006293 DeclUpdates[D].push_back(
6294 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6295 });
Richard Smith1fa5d642013-05-11 05:45:24 +00006296}
6297
Richard Smithf8134002015-03-10 01:41:22 +00006298void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
Richard Smith5b349582017-10-13 01:55:36 +00006299 const FunctionDecl *Delete,
6300 Expr *ThisArg) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006301 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006302 assert(!WritingAST && "Already writing the AST!");
6303 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006304 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006305 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006306 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6307 });
Richard Smithf8134002015-03-10 01:41:22 +00006308}
6309
Sebastian Redlab238a72011-04-24 16:28:06 +00006310void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006311 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006312 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006313 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006314 return; // Declaration not imported from PCH.
6315
Richard Smith4d235792014-08-07 18:53:08 +00006316 // Implicit function decl from a PCH was defined.
6317 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006318}
6319
Richard Smith891fc7f2017-12-05 01:31:47 +00006320void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6321 if (Chain && Chain->isProcessingUpdateRecords()) return;
6322 assert(!WritingAST && "Already writing the AST!");
6323 if (!D->isFromASTFile())
6324 return;
6325
6326 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6327}
6328
Richard Smithd28ac5b2014-03-22 23:33:22 +00006329void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006330 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006331 assert(!WritingAST && "Already writing the AST!");
6332 if (!D->isFromASTFile())
6333 return;
6334
Richard Smith9e2341d2015-03-23 03:25:59 +00006335 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006336}
6337
Richard Smith891fc7f2017-12-05 01:31:47 +00006338void ASTWriter::InstantiationRequested(const ValueDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006339 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006340 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006341 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006342 return;
6343
6344 // Since the actual instantiation is delayed, this really means that we need
6345 // to update the instantiation location.
Richard Smith891fc7f2017-12-05 01:31:47 +00006346 SourceLocation POI;
6347 if (auto *VD = dyn_cast<VarDecl>(D))
6348 POI = VD->getPointOfInstantiation();
6349 else
6350 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6351 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006352}
6353
John McCall32791cc2016-01-06 22:34:54 +00006354void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006355 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006356 assert(!WritingAST && "Already writing the AST!");
6357 if (!D->isFromASTFile())
6358 return;
6359
6360 DeclUpdates[D].push_back(
6361 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6362}
6363
Richard Smith4b054b22016-08-24 21:25:37 +00006364void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6365 assert(!WritingAST && "Already writing the AST!");
6366 if (!D->isFromASTFile())
6367 return;
6368
6369 DeclUpdates[D].push_back(
6370 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6371}
6372
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006373void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6374 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006375 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006376 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006377 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006378 return; // Declaration not imported from PCH.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006379
Douglas Gregor404cdde2012-01-27 01:47:08 +00006380 assert(IFD->getDefinition() && "Category on a class without a definition?");
6381 ObjCClassesWithCategories.insert(
6382 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006383}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006384
Eli Friedman276dd182013-09-05 00:02:25 +00006385void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006386 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006387 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006388
6389 // If there is *any* declaration of the entity that's not from an AST file,
6390 // we can skip writing the update record. We make sure that isUsed() triggers
6391 // completion of the redeclaration chain of the entity.
6392 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6393 if (IsLocalDecl(Prev))
6394 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006395
Aaron Ballman4f45b712014-03-21 15:22:56 +00006396 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006397}
Alexey Bataev97720002014-11-11 04:05:39 +00006398
6399void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006400 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006401 assert(!WritingAST && "Already writing the AST!");
6402 if (!D->isFromASTFile())
6403 return;
6404
6405 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6406}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006407
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006408void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6409 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006410 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006411 assert(!WritingAST && "Already writing the AST!");
6412 if (!D->isFromASTFile())
6413 return;
6414
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006415 DeclUpdates[D].push_back(
6416 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006417}
6418
Richard Smith4caa4492015-05-15 02:34:32 +00006419void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006420 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006421 assert(!WritingAST && "Already writing the AST!");
6422 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006423 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006424}
Alex Denisovfde64952015-06-26 05:28:36 +00006425
6426void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6427 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006428 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006429 assert(!WritingAST && "Already writing the AST!");
6430 if (!Record->isFromASTFile())
6431 return;
6432 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6433}
Richard Smithc26d9742016-10-06 20:30:51 +00006434
6435void ASTWriter::AddedCXXTemplateSpecialization(
6436 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6437 assert(!WritingAST && "Already writing the AST!");
6438
6439 if (!TD->getFirstDecl()->isFromASTFile())
6440 return;
6441 if (Chain && Chain->isProcessingUpdateRecords())
6442 return;
6443
6444 DeclsToEmitEvenIfUnreferenced.push_back(D);
6445}
6446
6447void ASTWriter::AddedCXXTemplateSpecialization(
6448 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6449 assert(!WritingAST && "Already writing the AST!");
6450
6451 if (!TD->getFirstDecl()->isFromASTFile())
6452 return;
6453 if (Chain && Chain->isProcessingUpdateRecords())
6454 return;
6455
6456 DeclsToEmitEvenIfUnreferenced.push_back(D);
6457}
6458
6459void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6460 const FunctionDecl *D) {
6461 assert(!WritingAST && "Already writing the AST!");
6462
6463 if (!TD->getFirstDecl()->isFromASTFile())
6464 return;
6465 if (Chain && Chain->isProcessingUpdateRecords())
6466 return;
6467
6468 DeclsToEmitEvenIfUnreferenced.push_back(D);
6469}
Kelvin Libe286f52018-09-15 13:54:15 +00006470
6471//===----------------------------------------------------------------------===//
6472//// OMPClause Serialization
6473////===----------------------------------------------------------------------===//
6474
6475void OMPClauseWriter::writeClause(OMPClause *C) {
6476 Record.push_back(C->getClauseKind());
6477 Visit(C);
6478 Record.AddSourceLocation(C->getBeginLoc());
6479 Record.AddSourceLocation(C->getEndLoc());
6480}
6481
6482void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
6483 Record.push_back(C->getCaptureRegion());
6484 Record.AddStmt(C->getPreInitStmt());
6485}
6486
6487void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
6488 VisitOMPClauseWithPreInit(C);
6489 Record.AddStmt(C->getPostUpdateExpr());
6490}
6491
6492void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
6493 VisitOMPClauseWithPreInit(C);
6494 Record.push_back(C->getNameModifier());
6495 Record.AddSourceLocation(C->getNameModifierLoc());
6496 Record.AddSourceLocation(C->getColonLoc());
6497 Record.AddStmt(C->getCondition());
6498 Record.AddSourceLocation(C->getLParenLoc());
6499}
6500
6501void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
6502 Record.AddStmt(C->getCondition());
6503 Record.AddSourceLocation(C->getLParenLoc());
6504}
6505
6506void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
6507 VisitOMPClauseWithPreInit(C);
6508 Record.AddStmt(C->getNumThreads());
6509 Record.AddSourceLocation(C->getLParenLoc());
6510}
6511
6512void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
6513 Record.AddStmt(C->getSafelen());
6514 Record.AddSourceLocation(C->getLParenLoc());
6515}
6516
6517void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
6518 Record.AddStmt(C->getSimdlen());
6519 Record.AddSourceLocation(C->getLParenLoc());
6520}
6521
6522void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
6523 Record.AddStmt(C->getNumForLoops());
6524 Record.AddSourceLocation(C->getLParenLoc());
6525}
6526
6527void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
6528 Record.push_back(C->getDefaultKind());
6529 Record.AddSourceLocation(C->getLParenLoc());
6530 Record.AddSourceLocation(C->getDefaultKindKwLoc());
6531}
6532
6533void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
6534 Record.push_back(C->getProcBindKind());
6535 Record.AddSourceLocation(C->getLParenLoc());
6536 Record.AddSourceLocation(C->getProcBindKindKwLoc());
6537}
6538
6539void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
6540 VisitOMPClauseWithPreInit(C);
6541 Record.push_back(C->getScheduleKind());
6542 Record.push_back(C->getFirstScheduleModifier());
6543 Record.push_back(C->getSecondScheduleModifier());
6544 Record.AddStmt(C->getChunkSize());
6545 Record.AddSourceLocation(C->getLParenLoc());
6546 Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
6547 Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
6548 Record.AddSourceLocation(C->getScheduleKindLoc());
6549 Record.AddSourceLocation(C->getCommaLoc());
6550}
6551
6552void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
6553 Record.push_back(C->getLoopNumIterations().size());
6554 Record.AddStmt(C->getNumForLoops());
6555 for (Expr *NumIter : C->getLoopNumIterations())
6556 Record.AddStmt(NumIter);
6557 for (unsigned I = 0, E = C->getLoopNumIterations().size(); I <E; ++I)
6558 Record.AddStmt(C->getLoopCunter(I));
6559 Record.AddSourceLocation(C->getLParenLoc());
6560}
6561
6562void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
6563
6564void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
6565
6566void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
6567
6568void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
6569
6570void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
6571
6572void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *) {}
6573
6574void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
6575
6576void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
6577
6578void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
6579
6580void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
6581
6582void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
6583
6584void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
6585 Record.push_back(C->varlist_size());
6586 Record.AddSourceLocation(C->getLParenLoc());
6587 for (auto *VE : C->varlists()) {
6588 Record.AddStmt(VE);
6589 }
6590 for (auto *VE : C->private_copies()) {
6591 Record.AddStmt(VE);
6592 }
6593}
6594
6595void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
6596 Record.push_back(C->varlist_size());
6597 VisitOMPClauseWithPreInit(C);
6598 Record.AddSourceLocation(C->getLParenLoc());
6599 for (auto *VE : C->varlists()) {
6600 Record.AddStmt(VE);
6601 }
6602 for (auto *VE : C->private_copies()) {
6603 Record.AddStmt(VE);
6604 }
6605 for (auto *VE : C->inits()) {
6606 Record.AddStmt(VE);
6607 }
6608}
6609
6610void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
6611 Record.push_back(C->varlist_size());
6612 VisitOMPClauseWithPostUpdate(C);
6613 Record.AddSourceLocation(C->getLParenLoc());
6614 for (auto *VE : C->varlists())
6615 Record.AddStmt(VE);
6616 for (auto *E : C->private_copies())
6617 Record.AddStmt(E);
6618 for (auto *E : C->source_exprs())
6619 Record.AddStmt(E);
6620 for (auto *E : C->destination_exprs())
6621 Record.AddStmt(E);
6622 for (auto *E : C->assignment_ops())
6623 Record.AddStmt(E);
6624}
6625
6626void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
6627 Record.push_back(C->varlist_size());
6628 Record.AddSourceLocation(C->getLParenLoc());
6629 for (auto *VE : C->varlists())
6630 Record.AddStmt(VE);
6631}
6632
6633void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
6634 Record.push_back(C->varlist_size());
6635 VisitOMPClauseWithPostUpdate(C);
6636 Record.AddSourceLocation(C->getLParenLoc());
6637 Record.AddSourceLocation(C->getColonLoc());
6638 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6639 Record.AddDeclarationNameInfo(C->getNameInfo());
6640 for (auto *VE : C->varlists())
6641 Record.AddStmt(VE);
6642 for (auto *VE : C->privates())
6643 Record.AddStmt(VE);
6644 for (auto *E : C->lhs_exprs())
6645 Record.AddStmt(E);
6646 for (auto *E : C->rhs_exprs())
6647 Record.AddStmt(E);
6648 for (auto *E : C->reduction_ops())
6649 Record.AddStmt(E);
6650}
6651
6652void OMPClauseWriter::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
6653 Record.push_back(C->varlist_size());
6654 VisitOMPClauseWithPostUpdate(C);
6655 Record.AddSourceLocation(C->getLParenLoc());
6656 Record.AddSourceLocation(C->getColonLoc());
6657 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6658 Record.AddDeclarationNameInfo(C->getNameInfo());
6659 for (auto *VE : C->varlists())
6660 Record.AddStmt(VE);
6661 for (auto *VE : C->privates())
6662 Record.AddStmt(VE);
6663 for (auto *E : C->lhs_exprs())
6664 Record.AddStmt(E);
6665 for (auto *E : C->rhs_exprs())
6666 Record.AddStmt(E);
6667 for (auto *E : C->reduction_ops())
6668 Record.AddStmt(E);
6669}
6670
6671void OMPClauseWriter::VisitOMPInReductionClause(OMPInReductionClause *C) {
6672 Record.push_back(C->varlist_size());
6673 VisitOMPClauseWithPostUpdate(C);
6674 Record.AddSourceLocation(C->getLParenLoc());
6675 Record.AddSourceLocation(C->getColonLoc());
6676 Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
6677 Record.AddDeclarationNameInfo(C->getNameInfo());
6678 for (auto *VE : C->varlists())
6679 Record.AddStmt(VE);
6680 for (auto *VE : C->privates())
6681 Record.AddStmt(VE);
6682 for (auto *E : C->lhs_exprs())
6683 Record.AddStmt(E);
6684 for (auto *E : C->rhs_exprs())
6685 Record.AddStmt(E);
6686 for (auto *E : C->reduction_ops())
6687 Record.AddStmt(E);
6688 for (auto *E : C->taskgroup_descriptors())
6689 Record.AddStmt(E);
6690}
6691
6692void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
6693 Record.push_back(C->varlist_size());
6694 VisitOMPClauseWithPostUpdate(C);
6695 Record.AddSourceLocation(C->getLParenLoc());
6696 Record.AddSourceLocation(C->getColonLoc());
6697 Record.push_back(C->getModifier());
6698 Record.AddSourceLocation(C->getModifierLoc());
6699 for (auto *VE : C->varlists()) {
6700 Record.AddStmt(VE);
6701 }
6702 for (auto *VE : C->privates()) {
6703 Record.AddStmt(VE);
6704 }
6705 for (auto *VE : C->inits()) {
6706 Record.AddStmt(VE);
6707 }
6708 for (auto *VE : C->updates()) {
6709 Record.AddStmt(VE);
6710 }
6711 for (auto *VE : C->finals()) {
6712 Record.AddStmt(VE);
6713 }
6714 Record.AddStmt(C->getStep());
6715 Record.AddStmt(C->getCalcStep());
6716}
6717
6718void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
6719 Record.push_back(C->varlist_size());
6720 Record.AddSourceLocation(C->getLParenLoc());
6721 Record.AddSourceLocation(C->getColonLoc());
6722 for (auto *VE : C->varlists())
6723 Record.AddStmt(VE);
6724 Record.AddStmt(C->getAlignment());
6725}
6726
6727void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
6728 Record.push_back(C->varlist_size());
6729 Record.AddSourceLocation(C->getLParenLoc());
6730 for (auto *VE : C->varlists())
6731 Record.AddStmt(VE);
6732 for (auto *E : C->source_exprs())
6733 Record.AddStmt(E);
6734 for (auto *E : C->destination_exprs())
6735 Record.AddStmt(E);
6736 for (auto *E : C->assignment_ops())
6737 Record.AddStmt(E);
6738}
6739
6740void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
6741 Record.push_back(C->varlist_size());
6742 Record.AddSourceLocation(C->getLParenLoc());
6743 for (auto *VE : C->varlists())
6744 Record.AddStmt(VE);
6745 for (auto *E : C->source_exprs())
6746 Record.AddStmt(E);
6747 for (auto *E : C->destination_exprs())
6748 Record.AddStmt(E);
6749 for (auto *E : C->assignment_ops())
6750 Record.AddStmt(E);
6751}
6752
6753void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
6754 Record.push_back(C->varlist_size());
6755 Record.AddSourceLocation(C->getLParenLoc());
6756 for (auto *VE : C->varlists())
6757 Record.AddStmt(VE);
6758}
6759
6760void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
6761 Record.push_back(C->varlist_size());
6762 Record.push_back(C->getNumLoops());
6763 Record.AddSourceLocation(C->getLParenLoc());
6764 Record.push_back(C->getDependencyKind());
6765 Record.AddSourceLocation(C->getDependencyLoc());
6766 Record.AddSourceLocation(C->getColonLoc());
6767 for (auto *VE : C->varlists())
6768 Record.AddStmt(VE);
6769 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
6770 Record.AddStmt(C->getLoopData(I));
6771}
6772
6773void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
6774 VisitOMPClauseWithPreInit(C);
6775 Record.AddStmt(C->getDevice());
6776 Record.AddSourceLocation(C->getLParenLoc());
6777}
6778
6779void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
6780 Record.push_back(C->varlist_size());
6781 Record.push_back(C->getUniqueDeclarationsNum());
6782 Record.push_back(C->getTotalComponentListNum());
6783 Record.push_back(C->getTotalComponentsNum());
6784 Record.AddSourceLocation(C->getLParenLoc());
6785 Record.push_back(C->getMapTypeModifier());
6786 Record.push_back(C->getMapType());
6787 Record.AddSourceLocation(C->getMapLoc());
6788 Record.AddSourceLocation(C->getColonLoc());
6789 for (auto *E : C->varlists())
6790 Record.AddStmt(E);
6791 for (auto *D : C->all_decls())
6792 Record.AddDeclRef(D);
6793 for (auto N : C->all_num_lists())
6794 Record.push_back(N);
6795 for (auto N : C->all_lists_sizes())
6796 Record.push_back(N);
6797 for (auto &M : C->all_components()) {
6798 Record.AddStmt(M.getAssociatedExpression());
6799 Record.AddDeclRef(M.getAssociatedDeclaration());
6800 }
6801}
6802
6803void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
6804 VisitOMPClauseWithPreInit(C);
6805 Record.AddStmt(C->getNumTeams());
6806 Record.AddSourceLocation(C->getLParenLoc());
6807}
6808
6809void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
6810 VisitOMPClauseWithPreInit(C);
6811 Record.AddStmt(C->getThreadLimit());
6812 Record.AddSourceLocation(C->getLParenLoc());
6813}
6814
6815void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
6816 Record.AddStmt(C->getPriority());
6817 Record.AddSourceLocation(C->getLParenLoc());
6818}
6819
6820void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
6821 Record.AddStmt(C->getGrainsize());
6822 Record.AddSourceLocation(C->getLParenLoc());
6823}
6824
6825void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
6826 Record.AddStmt(C->getNumTasks());
6827 Record.AddSourceLocation(C->getLParenLoc());
6828}
6829
6830void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
6831 Record.AddStmt(C->getHint());
6832 Record.AddSourceLocation(C->getLParenLoc());
6833}
6834
6835void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
6836 VisitOMPClauseWithPreInit(C);
6837 Record.push_back(C->getDistScheduleKind());
6838 Record.AddStmt(C->getChunkSize());
6839 Record.AddSourceLocation(C->getLParenLoc());
6840 Record.AddSourceLocation(C->getDistScheduleKindLoc());
6841 Record.AddSourceLocation(C->getCommaLoc());
6842}
6843
6844void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
6845 Record.push_back(C->getDefaultmapKind());
6846 Record.push_back(C->getDefaultmapModifier());
6847 Record.AddSourceLocation(C->getLParenLoc());
6848 Record.AddSourceLocation(C->getDefaultmapModifierLoc());
6849 Record.AddSourceLocation(C->getDefaultmapKindLoc());
6850}
6851
6852void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
6853 Record.push_back(C->varlist_size());
6854 Record.push_back(C->getUniqueDeclarationsNum());
6855 Record.push_back(C->getTotalComponentListNum());
6856 Record.push_back(C->getTotalComponentsNum());
6857 Record.AddSourceLocation(C->getLParenLoc());
6858 for (auto *E : C->varlists())
6859 Record.AddStmt(E);
6860 for (auto *D : C->all_decls())
6861 Record.AddDeclRef(D);
6862 for (auto N : C->all_num_lists())
6863 Record.push_back(N);
6864 for (auto N : C->all_lists_sizes())
6865 Record.push_back(N);
6866 for (auto &M : C->all_components()) {
6867 Record.AddStmt(M.getAssociatedExpression());
6868 Record.AddDeclRef(M.getAssociatedDeclaration());
6869 }
6870}
6871
6872void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
6873 Record.push_back(C->varlist_size());
6874 Record.push_back(C->getUniqueDeclarationsNum());
6875 Record.push_back(C->getTotalComponentListNum());
6876 Record.push_back(C->getTotalComponentsNum());
6877 Record.AddSourceLocation(C->getLParenLoc());
6878 for (auto *E : C->varlists())
6879 Record.AddStmt(E);
6880 for (auto *D : C->all_decls())
6881 Record.AddDeclRef(D);
6882 for (auto N : C->all_num_lists())
6883 Record.push_back(N);
6884 for (auto N : C->all_lists_sizes())
6885 Record.push_back(N);
6886 for (auto &M : C->all_components()) {
6887 Record.AddStmt(M.getAssociatedExpression());
6888 Record.AddDeclRef(M.getAssociatedDeclaration());
6889 }
6890}
6891
6892void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
6893 Record.push_back(C->varlist_size());
6894 Record.push_back(C->getUniqueDeclarationsNum());
6895 Record.push_back(C->getTotalComponentListNum());
6896 Record.push_back(C->getTotalComponentsNum());
6897 Record.AddSourceLocation(C->getLParenLoc());
6898 for (auto *E : C->varlists())
6899 Record.AddStmt(E);
6900 for (auto *VE : C->private_copies())
6901 Record.AddStmt(VE);
6902 for (auto *VE : C->inits())
6903 Record.AddStmt(VE);
6904 for (auto *D : C->all_decls())
6905 Record.AddDeclRef(D);
6906 for (auto N : C->all_num_lists())
6907 Record.push_back(N);
6908 for (auto N : C->all_lists_sizes())
6909 Record.push_back(N);
6910 for (auto &M : C->all_components()) {
6911 Record.AddStmt(M.getAssociatedExpression());
6912 Record.AddDeclRef(M.getAssociatedDeclaration());
6913 }
6914}
6915
6916void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
6917 Record.push_back(C->varlist_size());
6918 Record.push_back(C->getUniqueDeclarationsNum());
6919 Record.push_back(C->getTotalComponentListNum());
6920 Record.push_back(C->getTotalComponentsNum());
6921 Record.AddSourceLocation(C->getLParenLoc());
6922 for (auto *E : C->varlists())
6923 Record.AddStmt(E);
6924 for (auto *D : C->all_decls())
6925 Record.AddDeclRef(D);
6926 for (auto N : C->all_num_lists())
6927 Record.push_back(N);
6928 for (auto N : C->all_lists_sizes())
6929 Record.push_back(N);
6930 for (auto &M : C->all_components()) {
6931 Record.AddStmt(M.getAssociatedExpression());
6932 Record.AddDeclRef(M.getAssociatedDeclaration());
6933 }
6934}