blob: 1c838ae2c0dbc76eb978e7a6539e522be20f31ca [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"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000081#include "llvm/ADT/SmallSet.h"
82#include "llvm/ADT/SmallString.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000083#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000084#include "llvm/ADT/StringMap.h"
85#include "llvm/ADT/StringRef.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000086#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000088#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000089#include "llvm/Support/Compression.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +000090#include "llvm/Support/DJB.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000091#include "llvm/Support/Endian.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000092#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000093#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000094#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000095#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000096#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000097#include "llvm/Support/Path.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000098#include "llvm/Support/SHA1.h"
Pavel Labathd8c62902018-06-11 10:28:04 +000099#include "llvm/Support/VersionTuple.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000100#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +0000101#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000102#include <cassert>
103#include <cstdint>
104#include <cstdlib>
105#include <cstring>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000106#include <ctime>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000107#include <deque>
108#include <limits>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000109#include <memory>
110#include <queue>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000111#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +0000112#include <utility>
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000113#include <vector>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000114
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +0000116using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117
Sebastian Redl3df5a082010-07-30 17:03:48 +0000118template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +0000119static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000120 if (v.empty()) return StringRef();
121 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000122 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000123}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000124
125template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000126static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000127 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000128 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000129}
130
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131//===----------------------------------------------------------------------===//
132// Type serialization
133//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000134
Richard Smith290d8012016-04-06 17:06:00 +0000135namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000136
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000137 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000138 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000139 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000141 /// Type code that corresponds to the record generated.
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000142 TypeCode Code = static_cast<TypeCode>(0);
143
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000144 /// Abbreviation to use for the record, if any.
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000145 unsigned AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146
Richard Smith290d8012016-04-06 17:06:00 +0000147 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000148 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000149 : Writer(Writer), Record(Writer, Record) {}
Richard Smith290d8012016-04-06 17:06:00 +0000150
151 uint64_t Emit() {
152 return Record.Emit(Code, AbbrevToUse);
153 }
154
155 void Visit(QualType T) {
156 if (T.hasLocalNonFastQualifiers()) {
157 Qualifiers Qs = T.getLocalQualifiers();
158 Record.AddTypeRef(T.getLocalUnqualifiedType());
159 Record.push_back(Qs.getAsOpaqueValue());
160 Code = TYPE_EXT_QUAL;
161 AbbrevToUse = Writer.TypeExtQualAbbrev;
162 } else {
163 switch (T->getTypeClass()) {
164 // For all of the concrete, non-dependent types, call the
165 // appropriate visitor function.
166#define TYPE(Class, Base) \
167 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
168#define ABSTRACT_TYPE(Class, Base)
169#include "clang/AST/TypeNodes.def"
170 }
171 }
172 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173
174 void VisitArrayType(const ArrayType *T);
175 void VisitFunctionType(const FunctionType *T);
176 void VisitTagType(const TagType *T);
177
178#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
179#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180#include "clang/AST/TypeNodes.def"
181 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000182
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000183} // namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000186 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000187}
188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000190 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000191 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192}
193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000195 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Reid Kleckner8a365022013-06-24 17:51:48 +0000199void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000200 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000201 Code = TYPE_DECAYED;
202}
203
Reid Kleckner0503a872013-12-05 01:23:43 +0000204void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000205 Record.AddTypeRef(T->getOriginalType());
206 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000207 Code = TYPE_ADJUSTED;
208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000211 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000212 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213}
214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000216 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000217 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000218 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000222 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000227 Record.AddTypeRef(T->getPointeeType());
228 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000229 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230}
231
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000233 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000235 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236}
237
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000240 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000246 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000247}
248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000251 Record.AddSourceLocation(T->getLBracketLoc());
252 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000253 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000254 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255}
256
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000258 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000259 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000260 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000261 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262}
263
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000264void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000265 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000266 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000267}
268
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000269void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000270 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000271 FunctionType::ExtInfo C = T->getExtInfo();
272 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000273 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000274 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000275 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000276 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000277 Record.push_back(C.getProducesResult());
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000278 Record.push_back(C.getNoCallerSavedRegs());
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000279 Record.push_back(C.getNoCfCheck());
Richard Smith01b2cb42014-07-26 06:37:51 +0000280
281 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
282 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000283}
284
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000285void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000286 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000287 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000288}
289
Richard Smith290d8012016-04-06 17:06:00 +0000290static void addExceptionSpec(const FunctionProtoType *T,
291 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000292 Record.push_back(T->getExceptionSpecType());
293 if (T->getExceptionSpecType() == EST_Dynamic) {
294 Record.push_back(T->getNumExceptions());
295 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000296 Record.AddTypeRef(T->getExceptionType(I));
Richard Smitheaf11ad2018-05-03 03:58:32 +0000297 } else if (isComputedNoexcept(T->getExceptionSpecType())) {
Richard Smith290d8012016-04-06 17:06:00 +0000298 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000299 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000300 Record.AddDeclRef(T->getExceptionSpecDecl());
301 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000302 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000303 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000304 }
Richard Smith564417a2014-03-20 21:47:22 +0000305}
306
307void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
308 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000309
Richard Smith564417a2014-03-20 21:47:22 +0000310 Record.push_back(T->isVariadic());
311 Record.push_back(T->hasTrailingReturn());
312 Record.push_back(T->getTypeQuals());
313 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000314 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000315
316 Record.push_back(T->getNumParams());
317 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000318 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000319
John McCall18afab72016-03-01 00:49:02 +0000320 if (T->hasExtParameterInfos()) {
321 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
322 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
323 }
324
Richard Smith01b2cb42014-07-26 06:37:51 +0000325 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000326 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
327 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000328 AbbrevToUse = 0;
329
Sebastian Redl539c5062010-08-18 23:57:32 +0000330 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000331}
332
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000333void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000334 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000335 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000336}
John McCallb96ec562009-12-04 22:46:56 +0000337
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000338void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000339 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000340 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000341 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000343}
344
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000345void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000346 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000347 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000348}
349
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000350void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000351 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000352 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000353}
354
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000355void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000356 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000357 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000358 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000359}
360
Alexis Hunte852b102011-05-24 22:41:36 +0000361void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000362 Record.AddTypeRef(T->getBaseType());
363 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000364 Record.push_back(T->getUTTKind());
365 Code = TYPE_UNARY_TRANSFORM;
366}
367
Richard Smith30482bc2011-02-20 03:19:35 +0000368void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000369 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000370 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000371 if (T->getDeducedType().isNull())
372 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000373 Code = TYPE_AUTO;
374}
375
Richard Smith600b5262017-01-26 20:40:47 +0000376void ASTTypeWriter::VisitDeducedTemplateSpecializationType(
377 const DeducedTemplateSpecializationType *T) {
378 Record.AddTemplateName(T->getTemplateName());
379 Record.AddTypeRef(T->getDeducedType());
380 if (T->getDeducedType().isNull())
381 Record.push_back(T->isDependentType());
382 Code = TYPE_DEDUCED_TEMPLATE_SPECIALIZATION;
383}
384
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000385void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000386 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000387 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000388 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000389 "Cannot serialize in the middle of a type definition");
390}
391
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000392void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000393 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000394 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000395}
396
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000397void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000398 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000399 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000400}
401
John McCall81904512011-01-06 01:58:22 +0000402void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000403 Record.AddTypeRef(T->getModifiedType());
404 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000405 Record.push_back(T->getAttrKind());
406 Code = TYPE_ATTRIBUTED;
407}
408
Mike Stump11289f42009-09-09 15:08:12 +0000409void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000410ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000411 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000412 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
413 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000414 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000415}
416
417void
Douglas Gregorada4b792011-01-14 02:55:32 +0000418ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
419 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000420 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
421 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000422 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
423}
424
425void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000426ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000427 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000428 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000429 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000430 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000431 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000432 Record.AddTemplateArgument(ArgI);
433 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
434 : T->isCanonicalUnqualified()
435 ? QualType()
436 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000437 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000438}
439
440void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000441ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000442 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000443 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000444 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000445 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000446}
447
448void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000449ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000450 const DependentSizedExtVectorType *T) {
Alex Lorenz00aee432017-03-22 10:04:48 +0000451 Record.AddTypeRef(T->getElementType());
452 Record.AddStmt(T->getSizeExpr());
453 Record.AddSourceLocation(T->getAttributeLoc());
454 Code = TYPE_DEPENDENT_SIZED_EXT_VECTOR;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000455}
456
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000457void
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000458ASTTypeWriter::VisitDependentAddressSpaceType(
459 const DependentAddressSpaceType *T) {
460 Record.AddTypeRef(T->getPointeeType());
461 Record.AddStmt(T->getAddrSpaceExpr());
462 Record.AddSourceLocation(T->getAttributeLoc());
463 Code = TYPE_DEPENDENT_ADDRESS_SPACE;
464}
465
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000466void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000467ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000468 Record.push_back(T->getDepth());
469 Record.push_back(T->getIndex());
470 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000471 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000472 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000473}
474
475void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000476ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000477 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000478 Record.AddNestedNameSpecifier(T->getQualifier());
479 Record.AddIdentifierRef(T->getIdentifier());
480 Record.AddTypeRef(
481 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000482 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000483}
484
485void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000486ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000487 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000488 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000489 Record.AddNestedNameSpecifier(T->getQualifier());
490 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000491 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000492 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000493 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000494 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000495}
496
Douglas Gregord2fa7662010-12-20 02:24:11 +0000497void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000498 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000499 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000500 Record.push_back(*NumExpansions + 1);
501 else
502 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000503 Code = TYPE_PACK_EXPANSION;
504}
505
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000506void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000507 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000508 Code = TYPE_PAREN;
509}
510
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000511void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000512 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000513 Record.AddNestedNameSpecifier(T->getQualifier());
514 Record.AddTypeRef(T->getNamedType());
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000515 Record.AddDeclRef(T->getOwnedTagDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000516 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000517}
518
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000519void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000520 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
521 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000522 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000523}
524
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000525void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000526 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000527 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000528}
529
Manman Rene6be26c2016-09-13 17:25:08 +0000530void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
531 Record.AddDeclRef(T->getDecl());
532 Record.push_back(T->getNumProtocols());
533 for (const auto *I : T->quals())
534 Record.AddDeclRef(I);
535 Code = TYPE_OBJC_TYPE_PARAM;
536}
537
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000538void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000539 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000540 Record.push_back(T->getTypeArgsAsWritten().size());
541 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000542 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000543 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000544 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000545 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000546 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000547 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000548}
549
Steve Narofffb4330f2009-06-17 22:40:22 +0000550void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000551ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000552 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000553 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000554}
555
Eli Friedman0dfb8892011-10-06 23:00:33 +0000556void
557ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000558 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000559 Code = TYPE_ATOMIC;
560}
561
Xiuli Pan9c14e282016-01-09 12:53:17 +0000562void
563ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000564 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000565 Record.push_back(T->isReadOnly());
566 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000567}
568
John McCall8f115c62009-10-16 21:56:05 +0000569namespace {
570
571class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000572 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000573
574public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000575 TypeLocWriter(ASTRecordWriter &Record) : Record(Record) {}
John McCall8f115c62009-10-16 21:56:05 +0000576
John McCall17001972009-10-18 01:05:36 +0000577#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000578#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000579 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000580#include "clang/AST/TypeLocNodes.def"
581
John McCall17001972009-10-18 01:05:36 +0000582 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
583 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000584};
585
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000586} // namespace
John McCall8f115c62009-10-16 21:56:05 +0000587
John McCall17001972009-10-18 01:05:36 +0000588void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
589 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000590}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000591
John McCall17001972009-10-18 01:05:36 +0000592void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000593 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000594 if (TL.needsExtraLocalData()) {
Faisal Vali090da2d2018-01-01 18:23:28 +0000595 Record.push_back(TL.getWrittenTypeSpec());
596 Record.push_back(TL.getWrittenSignSpec());
597 Record.push_back(TL.getWrittenWidthSpec());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000598 Record.push_back(TL.hasModeAttr());
599 }
John McCall8f115c62009-10-16 21:56:05 +0000600}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000601
John McCall17001972009-10-18 01:05:36 +0000602void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000603 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000604}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000605
John McCall17001972009-10-18 01:05:36 +0000606void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000607 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000608}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000609
Reid Kleckner8a365022013-06-24 17:51:48 +0000610void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
611 // nothing to do
612}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000613
Reid Kleckner0503a872013-12-05 01:23:43 +0000614void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
615 // nothing to do
616}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000617
John McCall17001972009-10-18 01:05:36 +0000618void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000619 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000620}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000621
John McCall17001972009-10-18 01:05:36 +0000622void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000623 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000624}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000625
John McCall17001972009-10-18 01:05:36 +0000626void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000627 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000628}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000629
John McCall17001972009-10-18 01:05:36 +0000630void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000631 Record.AddSourceLocation(TL.getStarLoc());
632 Record.AddTypeSourceInfo(TL.getClassTInfo());
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::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000636 Record.AddSourceLocation(TL.getLBracketLoc());
637 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000638 Record.push_back(TL.getSizeExpr() ? 1 : 0);
639 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000640 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000641}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000642
John McCall17001972009-10-18 01:05:36 +0000643void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
644 VisitArrayTypeLoc(TL);
645}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000646
John McCall17001972009-10-18 01:05:36 +0000647void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
648 VisitArrayTypeLoc(TL);
649}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000650
John McCall17001972009-10-18 01:05:36 +0000651void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
652 VisitArrayTypeLoc(TL);
653}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000654
John McCall17001972009-10-18 01:05:36 +0000655void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
656 DependentSizedArrayTypeLoc TL) {
657 VisitArrayTypeLoc(TL);
658}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000659
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000660void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
661 DependentAddressSpaceTypeLoc TL) {
662 Record.AddSourceLocation(TL.getAttrNameLoc());
663 SourceRange range = TL.getAttrOperandParensRange();
664 Record.AddSourceLocation(range.getBegin());
665 Record.AddSourceLocation(range.getEnd());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000666 Record.AddStmt(TL.getAttrExprOperand());
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000667}
668
John McCall17001972009-10-18 01:05:36 +0000669void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
670 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000671 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000672}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000673
John McCall17001972009-10-18 01:05:36 +0000674void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000675 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000676}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000677
John McCall17001972009-10-18 01:05:36 +0000678void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000679 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000680}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000681
John McCall17001972009-10-18 01:05:36 +0000682void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000683 Record.AddSourceLocation(TL.getLocalRangeBegin());
684 Record.AddSourceLocation(TL.getLParenLoc());
685 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000686 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000687 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000688 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000689 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000690}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000691
John McCall17001972009-10-18 01:05:36 +0000692void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
693 VisitFunctionTypeLoc(TL);
694}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000695
John McCall17001972009-10-18 01:05:36 +0000696void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
697 VisitFunctionTypeLoc(TL);
698}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000699
John McCallb96ec562009-12-04 22:46:56 +0000700void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000701 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000702}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000703
John McCall17001972009-10-18 01:05:36 +0000704void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000705 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000706}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000707
Manman Rene6be26c2016-09-13 17:25:08 +0000708void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
709 if (TL.getNumProtocols()) {
710 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
711 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
712 }
713 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
714 Record.AddSourceLocation(TL.getProtocolLoc(i));
715}
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000716
John McCall17001972009-10-18 01:05:36 +0000717void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000718 Record.AddSourceLocation(TL.getTypeofLoc());
719 Record.AddSourceLocation(TL.getLParenLoc());
720 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000721}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000722
John McCall17001972009-10-18 01:05:36 +0000723void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000724 Record.AddSourceLocation(TL.getTypeofLoc());
725 Record.AddSourceLocation(TL.getLParenLoc());
726 Record.AddSourceLocation(TL.getRParenLoc());
727 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000728}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000729
John McCall17001972009-10-18 01:05:36 +0000730void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000731 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000732}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000733
Alexis Hunte852b102011-05-24 22:41:36 +0000734void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000735 Record.AddSourceLocation(TL.getKWLoc());
736 Record.AddSourceLocation(TL.getLParenLoc());
737 Record.AddSourceLocation(TL.getRParenLoc());
738 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000739}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000740
Richard Smith30482bc2011-02-20 03:19:35 +0000741void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000742 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000743}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000744
Richard Smith600b5262017-01-26 20:40:47 +0000745void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
746 DeducedTemplateSpecializationTypeLoc TL) {
747 Record.AddSourceLocation(TL.getTemplateNameLoc());
748}
749
John McCall17001972009-10-18 01:05:36 +0000750void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000751 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000752}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000753
John McCall17001972009-10-18 01:05:36 +0000754void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000755 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000756}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000757
John McCall81904512011-01-06 01:58:22 +0000758void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000759 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000760 if (TL.hasAttrOperand()) {
761 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000762 Record.AddSourceLocation(range.getBegin());
763 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000764 }
765 if (TL.hasAttrExprOperand()) {
766 Expr *operand = TL.getAttrExprOperand();
767 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000768 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000769 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000770 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000771 }
772}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000773
John McCall17001972009-10-18 01:05:36 +0000774void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000775 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000776}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000777
John McCallcebee162009-10-18 09:09:24 +0000778void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
779 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000780 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000781}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000782
Douglas Gregorada4b792011-01-14 02:55:32 +0000783void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
784 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000785 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000786}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000787
John McCall17001972009-10-18 01:05:36 +0000788void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
789 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000790 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
791 Record.AddSourceLocation(TL.getTemplateNameLoc());
792 Record.AddSourceLocation(TL.getLAngleLoc());
793 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000794 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000795 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
796 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000797}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000798
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000799void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000800 Record.AddSourceLocation(TL.getLParenLoc());
801 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000802}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000803
Abramo Bagnara6150c882010-05-11 21:36:43 +0000804void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000805 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
806 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000807}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000808
John McCalle78aac42010-03-10 03:28:59 +0000809void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000810 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000811}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000812
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000813void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000814 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
815 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
816 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000817}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000818
John McCallc392f372010-06-11 00:33:02 +0000819void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
820 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000821 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
822 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
823 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
824 Record.AddSourceLocation(TL.getTemplateNameLoc());
825 Record.AddSourceLocation(TL.getLAngleLoc());
826 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000827 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000828 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
829 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000830}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000831
Douglas Gregord2fa7662010-12-20 02:24:11 +0000832void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000833 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000834}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000835
John McCall17001972009-10-18 01:05:36 +0000836void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000837 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000838}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000839
John McCall8b07ec22010-05-15 11:32:37 +0000840void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
841 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000842 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
843 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000844 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000845 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
846 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
847 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000848 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000849 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000850}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000851
John McCallfc93cf92009-10-22 22:37:11 +0000852void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000853 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000854}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000855
Eli Friedman0dfb8892011-10-06 23:00:33 +0000856void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000857 Record.AddSourceLocation(TL.getKWLoc());
858 Record.AddSourceLocation(TL.getLParenLoc());
859 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000860}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000861
Xiuli Pan9c14e282016-01-09 12:53:17 +0000862void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000863 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000864}
John McCall8f115c62009-10-16 21:56:05 +0000865
Richard Smith01b2cb42014-07-26 06:37:51 +0000866void ASTWriter::WriteTypeAbbrevs() {
867 using namespace llvm;
868
David Blaikieb44f0bf2017-01-04 22:36:43 +0000869 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000870
871 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000872 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000873 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
874 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
875 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000876 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000877
878 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000879 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000880 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
881 // FunctionType
882 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
883 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
884 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
885 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
886 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
887 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000888 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Oren Ben Simhon220671a2018-03-17 13:31:35 +0000889 Abv->Add(BitCodeAbbrevOp(0)); // NoCfCheck
Richard Smith01b2cb42014-07-26 06:37:51 +0000890 // FunctionProtoType
891 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
892 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
893 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
894 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
895 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
896 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
897 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
898 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000899 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000900}
901
Chris Lattner19cea4e2009-04-22 05:57:30 +0000902//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000903// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000904//===----------------------------------------------------------------------===//
905
Chris Lattner28fa4e62009-04-26 22:26:21 +0000906static void EmitBlockID(unsigned ID, const char *Name,
907 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000908 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000909 Record.clear();
910 Record.push_back(ID);
911 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
912
913 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000914 if (!Name || Name[0] == 0)
915 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000916 Record.clear();
917 while (*Name)
918 Record.push_back(*Name++);
919 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
920}
921
922static void EmitRecordID(unsigned ID, const char *Name,
923 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000924 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000925 Record.clear();
926 Record.push_back(ID);
927 while (*Name)
928 Record.push_back(*Name++);
929 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000930}
931
932static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000933 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000934#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000935 RECORD(STMT_STOP);
936 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000937 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000938 RECORD(STMT_NULL);
939 RECORD(STMT_COMPOUND);
940 RECORD(STMT_CASE);
941 RECORD(STMT_DEFAULT);
942 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000943 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000944 RECORD(STMT_IF);
945 RECORD(STMT_SWITCH);
946 RECORD(STMT_WHILE);
947 RECORD(STMT_DO);
948 RECORD(STMT_FOR);
949 RECORD(STMT_GOTO);
950 RECORD(STMT_INDIRECT_GOTO);
951 RECORD(STMT_CONTINUE);
952 RECORD(STMT_BREAK);
953 RECORD(STMT_RETURN);
954 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000955 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000956 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000957 RECORD(EXPR_PREDEFINED);
958 RECORD(EXPR_DECL_REF);
959 RECORD(EXPR_INTEGER_LITERAL);
960 RECORD(EXPR_FLOATING_LITERAL);
961 RECORD(EXPR_IMAGINARY_LITERAL);
962 RECORD(EXPR_STRING_LITERAL);
963 RECORD(EXPR_CHARACTER_LITERAL);
964 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000965 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000966 RECORD(EXPR_UNARY_OPERATOR);
967 RECORD(EXPR_SIZEOF_ALIGN_OF);
968 RECORD(EXPR_ARRAY_SUBSCRIPT);
969 RECORD(EXPR_CALL);
970 RECORD(EXPR_MEMBER);
971 RECORD(EXPR_BINARY_OPERATOR);
972 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
973 RECORD(EXPR_CONDITIONAL_OPERATOR);
974 RECORD(EXPR_IMPLICIT_CAST);
975 RECORD(EXPR_CSTYLE_CAST);
976 RECORD(EXPR_COMPOUND_LITERAL);
977 RECORD(EXPR_EXT_VECTOR_ELEMENT);
978 RECORD(EXPR_INIT_LIST);
979 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000980 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000981 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000982 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000983 RECORD(EXPR_VA_ARG);
984 RECORD(EXPR_ADDR_LABEL);
985 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000986 RECORD(EXPR_CHOOSE);
987 RECORD(EXPR_GNU_NULL);
988 RECORD(EXPR_SHUFFLE_VECTOR);
989 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000990 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000991 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000992 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000993 RECORD(EXPR_OBJC_ARRAY_LITERAL);
994 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000995 RECORD(EXPR_OBJC_ENCODE);
996 RECORD(EXPR_OBJC_SELECTOR_EXPR);
997 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
998 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
999 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
1000 RECORD(EXPR_OBJC_KVC_REF_EXPR);
1001 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001002 RECORD(STMT_OBJC_FOR_COLLECTION);
1003 RECORD(STMT_OBJC_CATCH);
1004 RECORD(STMT_OBJC_FINALLY);
1005 RECORD(STMT_OBJC_AT_TRY);
1006 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
1007 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001008 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001009 RECORD(STMT_CXX_CATCH);
1010 RECORD(STMT_CXX_TRY);
1011 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001012 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001013 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001014 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001015 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001016 RECORD(EXPR_CXX_STATIC_CAST);
1017 RECORD(EXPR_CXX_DYNAMIC_CAST);
1018 RECORD(EXPR_CXX_REINTERPRET_CAST);
1019 RECORD(EXPR_CXX_CONST_CAST);
1020 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +00001021 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +00001022 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +00001023 RECORD(EXPR_CXX_BOOL_LITERAL);
1024 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001025 RECORD(EXPR_CXX_TYPEID_EXPR);
1026 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001027 RECORD(EXPR_CXX_THIS);
1028 RECORD(EXPR_CXX_THROW);
1029 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +00001030 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001031 RECORD(EXPR_CXX_BIND_TEMPORARY);
1032 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
1033 RECORD(EXPR_CXX_NEW);
1034 RECORD(EXPR_CXX_DELETE);
1035 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
1036 RECORD(EXPR_EXPR_WITH_CLEANUPS);
1037 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
1038 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
1039 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
1040 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
1041 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +00001042 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001043 RECORD(EXPR_CXX_NOEXCEPT);
1044 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +00001045 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
1046 RECORD(EXPR_TYPE_TRAIT);
1047 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001048 RECORD(EXPR_PACK_EXPANSION);
1049 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001050 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001051 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001052 RECORD(EXPR_FUNCTION_PARM_PACK);
1053 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001054 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001055 RECORD(EXPR_CXX_UUIDOF_EXPR);
1056 RECORD(EXPR_CXX_UUIDOF_TYPE);
1057 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001058#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001059}
Mike Stump11289f42009-09-09 15:08:12 +00001060
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001061void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001062 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001063 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001064
Sebastian Redl539c5062010-08-18 23:57:32 +00001065#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1066#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001067
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001068 // Control Block.
1069 BLOCK(CONTROL_BLOCK);
1070 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001071 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001072 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001073 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001074 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001075 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001076 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001077 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001078 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001079
1080 BLOCK(OPTIONS_BLOCK);
1081 RECORD(LANGUAGE_OPTIONS);
1082 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001083 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001084 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001085 RECORD(PREPROCESSOR_OPTIONS);
1086
Douglas Gregor108cb222012-10-19 00:45:00 +00001087 BLOCK(INPUT_FILES_BLOCK);
1088 RECORD(INPUT_FILE);
1089
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001090 // AST Top-Level Block.
1091 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001092 RECORD(TYPE_OFFSET);
1093 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001094 RECORD(IDENTIFIER_OFFSET);
1095 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001096 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001097 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001098 RECORD(SPECIAL_TYPES);
1099 RECORD(STATISTICS);
1100 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001101 RECORD(SELECTOR_OFFSETS);
1102 RECORD(METHOD_POOL);
1103 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001104 RECORD(SOURCE_LOCATION_OFFSETS);
1105 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001106 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001107 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001108 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001109 RECORD(VTABLE_USES);
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00001110 RECORD(PPD_SKIPPED_RANGES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001111 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001112 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001113 RECORD(SEMA_DECL_REFS);
1114 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1115 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001116 RECORD(UPDATE_VISIBLE);
1117 RECORD(DECL_UPDATE_OFFSETS);
1118 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001119 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001120 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001121 RECORD(FP_PRAGMA_OPTIONS);
1122 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001123 RECORD(OPENCL_EXTENSION_TYPES);
1124 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001125 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001126 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001127 RECORD(MODULE_OFFSET_MAP);
1128 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001129 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001130 RECORD(FILE_SORTED_DECLS);
1131 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001132 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001133 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001134 RECORD(INTERESTING_IDENTIFIERS);
1135 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001136 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001137 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001138 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001139 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001140 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001141 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001142 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00001143 RECORD(PP_CONDITIONAL_STACK);
Douglas Gregor358cd442012-01-15 16:58:34 +00001144
Chris Lattner28fa4e62009-04-26 22:26:21 +00001145 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001146 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001147 RECORD(SM_SLOC_FILE_ENTRY);
1148 RECORD(SM_SLOC_BUFFER_ENTRY);
1149 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001150 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001151 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001152
Chris Lattner28fa4e62009-04-26 22:26:21 +00001153 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001154 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001155 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001156 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001157 RECORD(PP_MACRO_OBJECT_LIKE);
1158 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001159 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001160
Richard Smithfa715652015-08-31 22:43:10 +00001161 // Submodule Block.
1162 BLOCK(SUBMODULE_BLOCK);
1163 RECORD(SUBMODULE_METADATA);
1164 RECORD(SUBMODULE_DEFINITION);
1165 RECORD(SUBMODULE_UMBRELLA_HEADER);
1166 RECORD(SUBMODULE_HEADER);
1167 RECORD(SUBMODULE_TOPHEADER);
1168 RECORD(SUBMODULE_UMBRELLA_DIR);
1169 RECORD(SUBMODULE_IMPORTS);
1170 RECORD(SUBMODULE_EXPORTS);
1171 RECORD(SUBMODULE_REQUIRES);
1172 RECORD(SUBMODULE_EXCLUDED_HEADER);
1173 RECORD(SUBMODULE_LINK_LIBRARY);
1174 RECORD(SUBMODULE_CONFIG_MACRO);
1175 RECORD(SUBMODULE_CONFLICT);
1176 RECORD(SUBMODULE_PRIVATE_HEADER);
1177 RECORD(SUBMODULE_TEXTUAL_HEADER);
1178 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001179 RECORD(SUBMODULE_INITIALIZERS);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00001180 RECORD(SUBMODULE_EXPORT_AS);
Richard Smithfa715652015-08-31 22:43:10 +00001181
1182 // Comments Block.
1183 BLOCK(COMMENTS_BLOCK);
1184 RECORD(COMMENTS_RAW_COMMENT);
1185
Douglas Gregor12bfa382009-10-17 00:13:19 +00001186 // Decls and Types block.
1187 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001188 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001189 RECORD(TYPE_COMPLEX);
1190 RECORD(TYPE_POINTER);
1191 RECORD(TYPE_BLOCK_POINTER);
1192 RECORD(TYPE_LVALUE_REFERENCE);
1193 RECORD(TYPE_RVALUE_REFERENCE);
1194 RECORD(TYPE_MEMBER_POINTER);
1195 RECORD(TYPE_CONSTANT_ARRAY);
1196 RECORD(TYPE_INCOMPLETE_ARRAY);
1197 RECORD(TYPE_VARIABLE_ARRAY);
1198 RECORD(TYPE_VECTOR);
1199 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001200 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001201 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001202 RECORD(TYPE_TYPEDEF);
1203 RECORD(TYPE_TYPEOF_EXPR);
1204 RECORD(TYPE_TYPEOF);
1205 RECORD(TYPE_RECORD);
1206 RECORD(TYPE_ENUM);
1207 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001208 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001209 RECORD(TYPE_DECLTYPE);
1210 RECORD(TYPE_ELABORATED);
1211 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1212 RECORD(TYPE_UNRESOLVED_USING);
1213 RECORD(TYPE_INJECTED_CLASS_NAME);
1214 RECORD(TYPE_OBJC_OBJECT);
1215 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1216 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1217 RECORD(TYPE_DEPENDENT_NAME);
1218 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1219 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1220 RECORD(TYPE_PAREN);
1221 RECORD(TYPE_PACK_EXPANSION);
1222 RECORD(TYPE_ATTRIBUTED);
1223 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001224 RECORD(TYPE_AUTO);
1225 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001226 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001227 RECORD(TYPE_DECAYED);
1228 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001229 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001230 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001231 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001232 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001233 RECORD(DECL_ENUM);
1234 RECORD(DECL_RECORD);
1235 RECORD(DECL_ENUM_CONSTANT);
1236 RECORD(DECL_FUNCTION);
1237 RECORD(DECL_OBJC_METHOD);
1238 RECORD(DECL_OBJC_INTERFACE);
1239 RECORD(DECL_OBJC_PROTOCOL);
1240 RECORD(DECL_OBJC_IVAR);
1241 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001242 RECORD(DECL_OBJC_CATEGORY);
1243 RECORD(DECL_OBJC_CATEGORY_IMPL);
1244 RECORD(DECL_OBJC_IMPLEMENTATION);
1245 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1246 RECORD(DECL_OBJC_PROPERTY);
1247 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001248 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001249 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001250 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001251 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001252 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001253 RECORD(DECL_FILE_SCOPE_ASM);
1254 RECORD(DECL_BLOCK);
1255 RECORD(DECL_CONTEXT_LEXICAL);
1256 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001257 RECORD(DECL_NAMESPACE);
1258 RECORD(DECL_NAMESPACE_ALIAS);
1259 RECORD(DECL_USING);
1260 RECORD(DECL_USING_SHADOW);
1261 RECORD(DECL_USING_DIRECTIVE);
1262 RECORD(DECL_UNRESOLVED_USING_VALUE);
1263 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1264 RECORD(DECL_LINKAGE_SPEC);
1265 RECORD(DECL_CXX_RECORD);
1266 RECORD(DECL_CXX_METHOD);
1267 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001268 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001269 RECORD(DECL_CXX_DESTRUCTOR);
1270 RECORD(DECL_CXX_CONVERSION);
1271 RECORD(DECL_ACCESS_SPEC);
1272 RECORD(DECL_FRIEND);
1273 RECORD(DECL_FRIEND_TEMPLATE);
1274 RECORD(DECL_CLASS_TEMPLATE);
1275 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1276 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001277 RECORD(DECL_VAR_TEMPLATE);
1278 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1279 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001280 RECORD(DECL_FUNCTION_TEMPLATE);
1281 RECORD(DECL_TEMPLATE_TYPE_PARM);
1282 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1283 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001284 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001285 RECORD(DECL_STATIC_ASSERT);
1286 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001287 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001288 RECORD(DECL_INDIRECTFIELD);
1289 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001290 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1291 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1292 RECORD(DECL_IMPORT);
1293 RECORD(DECL_OMP_THREADPRIVATE);
1294 RECORD(DECL_EMPTY);
1295 RECORD(DECL_OBJC_TYPE_PARAM);
1296 RECORD(DECL_OMP_CAPTUREDEXPR);
1297 RECORD(DECL_PRAGMA_COMMENT);
1298 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1299 RECORD(DECL_OMP_DECLARE_REDUCTION);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001300
Douglas Gregor03412ba2011-06-03 02:27:19 +00001301 // Statements and Exprs can occur in the Decls and Types block.
1302 AddStmtsExprs(Stream, Record);
1303
Douglas Gregor92a96f52011-02-08 21:58:10 +00001304 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001305 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001306 RECORD(PPD_MACRO_DEFINITION);
1307 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001308
1309 // Decls and Types block.
1310 BLOCK(EXTENSION_BLOCK);
1311 RECORD(EXTENSION_METADATA);
1312
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001313 BLOCK(UNHASHED_CONTROL_BLOCK);
1314 RECORD(SIGNATURE);
1315 RECORD(DIAGNOSTIC_OPTIONS);
1316 RECORD(DIAG_PRAGMA_MAPPINGS);
1317
Chris Lattner28fa4e62009-04-26 22:26:21 +00001318#undef RECORD
1319#undef BLOCK
1320 Stream.ExitBlock();
1321}
1322
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001323/// Prepares a path for being written to an AST file by converting it
Richard Smith54cc3c22014-12-11 20:50:24 +00001324/// to an absolute path and removing nested './'s.
1325///
1326/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001327static bool cleanPathForOutput(FileManager &FileMgr,
1328 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001329 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001330 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001331}
1332
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001333/// Adjusts the given filename to only write out the portion of the
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001334/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001335///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001336/// \param Filename the file name to adjust.
1337///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001338/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1339/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001340///
1341/// \returns either the original filename (if it needs no adjustment) or the
1342/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001343static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001344adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001345 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001346
Richard Smith7ed1bc92014-12-05 22:42:13 +00001347 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001348 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001349
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001350 // Verify that the filename and the system root have the same prefix.
1351 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001352 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1353 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001354 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001355
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001356 // We hit the end of the filename before we hit the end of the system root.
1357 if (!Filename[Pos])
1358 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001359
Richard Smith7ed1bc92014-12-05 22:42:13 +00001360 // If there's not a path separator at the end of the base directory nor
1361 // immediately after it, then this isn't within the base directory.
1362 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1363 if (!llvm::sys::path::is_separator(BaseDir.back()))
1364 return Filename;
1365 } else {
1366 // If the file name has a '/' at the current position, skip over the '/'.
1367 // We distinguish relative paths from absolute paths by the
1368 // absence of '/' at the beginning of relative paths.
1369 //
1370 // FIXME: This is wrong. We distinguish them by asking if the path is
1371 // absolute, which isn't the same thing. And there might be multiple '/'s
1372 // in a row. Use a better mechanism to indicate whether we have emitted an
1373 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001374 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001375 }
Mike Stump11289f42009-09-09 15:08:12 +00001376
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001377 return Filename + Pos;
1378}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001379
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001380ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1381 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1382 llvm::SHA1 Hasher;
1383 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1384 auto Hash = Hasher.result();
1385
1386 // Convert to an array [5*i32].
1387 ASTFileSignature Signature;
1388 auto LShift = [&](unsigned char Val, unsigned Shift) {
1389 return (uint32_t)Val << Shift;
1390 };
1391 for (int I = 0; I != 5; ++I)
1392 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1393 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1394
1395 return Signature;
1396}
1397
1398ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1399 ASTContext &Context) {
1400 // Flush first to prepare the PCM hash (signature).
1401 Stream.FlushToWord();
1402 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1403
1404 // Enter the block and prepare to write records.
1405 RecordData Record;
1406 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1407
1408 // For implicit modules, write the hash of the PCM as its signature.
1409 ASTFileSignature Signature;
1410 if (WritingModule &&
1411 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1412 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1413 Record.append(Signature.begin(), Signature.end());
1414 Stream.EmitRecord(SIGNATURE, Record);
1415 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001416 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001417
1418 // Diagnostic options.
1419 const auto &Diags = Context.getDiagnostics();
1420 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1421#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1422#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1423 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1424#include "clang/Basic/DiagnosticOptions.def"
1425 Record.push_back(DiagOpts.Warnings.size());
1426 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1427 AddString(DiagOpts.Warnings[I], Record);
1428 Record.push_back(DiagOpts.Remarks.size());
1429 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1430 AddString(DiagOpts.Remarks[I], Record);
1431 // Note: we don't serialize the log or serialization file names, because they
1432 // are generally transient files and will almost always be overridden.
1433 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1434
1435 // Write out the diagnostic/pragma mappings.
1436 WritePragmaDiagnosticMappings(Diags, /* IsModule = */ WritingModule);
1437
1438 // Leave the options block.
1439 Stream.ExitBlock();
1440 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001441}
1442
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001443/// Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001444void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1445 StringRef isysroot,
1446 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001447 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001448
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001449 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001450 RecordData Record;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001451
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001452 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001453 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001454 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1455 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1456 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1457 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1458 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1459 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001460 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001461 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PCHHasObjectFile
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001462 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1463 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001464 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001465 assert((!WritingModule || isysroot.empty()) &&
1466 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001467 {
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00001468 RecordData::value_type Record[] = {
1469 METADATA,
1470 VERSION_MAJOR,
1471 VERSION_MINOR,
1472 CLANG_VERSION_MAJOR,
1473 CLANG_VERSION_MINOR,
1474 !isysroot.empty(),
1475 IncludeTimestamps,
1476 Context.getLangOpts().BuildingPCHWithObjectFile,
1477 ASTHasCompilerErrors};
Mehdi Amini57a41912015-09-10 01:46:39 +00001478 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1479 getClangFullRepositoryVersion());
1480 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001481
Richard Smith8b706102017-05-31 20:56:55 +00001482 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001483 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001484 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001485 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1486 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001487 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001488 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001489 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1490 }
1491
Richard Smith7ed1bc92014-12-05 22:42:13 +00001492 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001493 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001494 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001495
1496 // If the home of the module is the current working directory, then we
1497 // want to pick up the cwd of the build process loading the module, not
1498 // our cwd, when we load this module.
1499 if (!PP.getHeaderSearchInfo()
1500 .getHeaderSearchOpts()
1501 .ModuleMapFileHomeIsCwd ||
1502 WritingModule->Directory->getName() != StringRef(".")) {
1503 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001504 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001505 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1506 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001507 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001508
Mehdi Amini57a41912015-09-10 01:46:39 +00001509 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001510 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1511 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001512
1513 // Write out all other paths relative to the base directory if possible.
1514 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1515 } else if (!isysroot.empty()) {
1516 // Write out paths relative to the sysroot if possible.
1517 BaseDirectory = isysroot;
1518 }
1519
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001520 // Module map file
Richard Smithd19389a2017-07-05 07:47:11 +00001521 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001522 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001523
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001524 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001525 AddPath(WritingModule->PresumedModuleMapFile.empty()
1526 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1527 : StringRef(WritingModule->PresumedModuleMapFile),
1528 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001529
1530 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001531 if (auto *AdditionalModMaps =
1532 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001533 Record.push_back(AdditionalModMaps->size());
1534 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001535 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001536 } else {
1537 Record.push_back(0);
1538 }
1539
1540 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001541 }
1542
Douglas Gregor112b9072012-10-18 05:31:06 +00001543 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001544 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001545 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001546 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001547
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001548 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001549 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001550 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001551 continue;
1552
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001553 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1554 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001555
1556 // If we have calculated signature, there is no need to store
1557 // the size or timestamp.
1558 Record.push_back(M.Signature ? 0 : M.File->getSize());
1559 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1560
1561 for (auto I : M.Signature)
1562 Record.push_back(I);
1563
Boris Kolpackovd30446f2017-08-31 06:26:43 +00001564 AddString(M.ModuleName, Record);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001565 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001566 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001567 Stream.EmitRecord(IMPORTS, Record);
1568 }
Mike Stump11289f42009-09-09 15:08:12 +00001569
Richard Smith0516b182015-09-08 19:40:14 +00001570 // Write the options block.
1571 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1572
Douglas Gregor112b9072012-10-18 05:31:06 +00001573 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001574 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001575 const LangOptions &LangOpts = Context.getLangOpts();
1576#define LANGOPT(Name, Bits, Default, Description) \
1577 Record.push_back(LangOpts.Name);
1578#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1579 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001580#include "clang/Basic/LangOptions.def"
1581#define SANITIZER(NAME, ID) \
1582 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001583#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001584
Ben Langmuircd98cb72015-06-23 18:20:18 +00001585 Record.push_back(LangOpts.ModuleFeatures.size());
1586 for (StringRef Feature : LangOpts.ModuleFeatures)
1587 AddString(Feature, Record);
1588
Douglas Gregor112b9072012-10-18 05:31:06 +00001589 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1590 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001591
1592 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001593
1594 // Comment options.
1595 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001596 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1597 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001598 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001599 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001600
Samuel Antaoee8fb302016-01-06 13:42:12 +00001601 // OpenMP offloading options.
1602 Record.push_back(LangOpts.OMPTargetTriples.size());
1603 for (auto &T : LangOpts.OMPTargetTriples)
1604 AddString(T.getTriple(), Record);
1605
1606 AddString(LangOpts.OMPHostIRFile, Record);
1607
Douglas Gregor112b9072012-10-18 05:31:06 +00001608 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1609
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001610 // Target options.
1611 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001612 const TargetInfo &Target = Context.getTargetInfo();
1613 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001614 AddString(TargetOpts.Triple, Record);
1615 AddString(TargetOpts.CPU, Record);
1616 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001617 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1618 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1619 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1620 }
1621 Record.push_back(TargetOpts.Features.size());
1622 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1623 AddString(TargetOpts.Features[I], Record);
1624 }
1625 Stream.EmitRecord(TARGET_OPTIONS, Record);
1626
Douglas Gregorc6317db2012-10-24 15:49:58 +00001627 // File system options.
1628 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001629 const FileSystemOptions &FSOpts =
1630 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001631 AddString(FSOpts.WorkingDir, Record);
1632 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1633
Douglas Gregor2d302362012-10-24 16:50:34 +00001634 // Header search options.
1635 Record.clear();
1636 const HeaderSearchOptions &HSOpts
1637 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1638 AddString(HSOpts.Sysroot, Record);
1639
1640 // Include entries.
1641 Record.push_back(HSOpts.UserEntries.size());
1642 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1643 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1644 AddString(Entry.Path, Record);
1645 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001646 Record.push_back(Entry.IsFramework);
1647 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001648 }
1649
1650 // System header prefixes.
1651 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1652 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1653 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1654 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1655 }
1656
1657 AddString(HSOpts.ResourceDir, Record);
1658 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001659 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001660 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001661 Record.push_back(HSOpts.ImplicitModuleMaps);
1662 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001663 Record.push_back(HSOpts.UseBuiltinIncludes);
1664 Record.push_back(HSOpts.UseStandardSystemIncludes);
1665 Record.push_back(HSOpts.UseStandardCXXIncludes);
1666 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001667 // Write out the specific module cache path that contains the module files.
1668 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001669 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1670
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001671 // Preprocessor options.
1672 Record.clear();
1673 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1674
1675 // Macro definitions.
1676 Record.push_back(PPOpts.Macros.size());
1677 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1678 AddString(PPOpts.Macros[I].first, Record);
1679 Record.push_back(PPOpts.Macros[I].second);
1680 }
1681
1682 // Includes
1683 Record.push_back(PPOpts.Includes.size());
1684 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1685 AddString(PPOpts.Includes[I], Record);
1686
1687 // Macro includes
1688 Record.push_back(PPOpts.MacroIncludes.size());
1689 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1690 AddString(PPOpts.MacroIncludes[I], Record);
1691
Douglas Gregorb6368752012-10-24 23:41:50 +00001692 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001693 // Detailed record is important since it is used for the module cache hash.
1694 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001695 AddString(PPOpts.ImplicitPCHInclude, Record);
1696 AddString(PPOpts.ImplicitPTHInclude, Record);
1697 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1698 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1699
Richard Smith0516b182015-09-08 19:40:14 +00001700 // Leave the options block.
1701 Stream.ExitBlock();
1702
Douglas Gregora3b20262011-05-06 21:43:30 +00001703 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001704 SourceManager &SM = Context.getSourceManager();
1705 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001706 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001707 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1708 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001709 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001710 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001711
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001712 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001713 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001714 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001715 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001716 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001717
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001718 Record.clear();
1719 Record.push_back(SM.getMainFileID().getOpaqueValue());
1720 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1721
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001722 // Original PCH directory
1723 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001724 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001725 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001727 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001728
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001729 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001730
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001731 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001732 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1733
Mehdi Amini57a41912015-09-10 01:46:39 +00001734 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001735 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1736 }
1737
Douglas Gregor49491f72013-03-15 22:15:07 +00001738 WriteInputFiles(Context.SourceMgr,
1739 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001740 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001741 Stream.ExitBlock();
1742}
1743
Douglas Gregor49491f72013-03-15 22:15:07 +00001744namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001745
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001746/// An input file.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001747struct InputFileEntry {
1748 const FileEntry *File;
1749 bool IsSystemFile;
1750 bool IsTransient;
1751 bool BufferOverridden;
1752 bool IsTopLevelModuleMap;
1753};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001754
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001755} // namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001756
1757void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1758 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001759 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001760 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001761
Douglas Gregor72be3902012-10-19 00:38:02 +00001762 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001763
Douglas Gregor72be3902012-10-19 00:38:02 +00001764 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001765 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001766 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001767 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001768 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1769 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001770 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001771 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Richard Smithf3f84612017-06-29 02:19:42 +00001772 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
Douglas Gregor72be3902012-10-19 00:38:02 +00001773 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001774 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001775
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001776 // Get all ContentCache objects for files, sorted by whether the file is a
1777 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001778 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001779 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1780 // Get this source location entry.
1781 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001782 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001783
1784 // We only care about file entries that were not overridden.
1785 if (!SLoc->isFile())
1786 continue;
Richard Smithf3f84612017-06-29 02:19:42 +00001787 const SrcMgr::FileInfo &File = SLoc->getFile();
1788 const SrcMgr::ContentCache *Cache = File.getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001789 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001790 continue;
1791
Douglas Gregor49491f72013-03-15 22:15:07 +00001792 InputFileEntry Entry;
1793 Entry.File = Cache->OrigEntry;
1794 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001795 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001796 Entry.BufferOverridden = Cache->BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001797 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1798 File.getIncludeLoc().isInvalid();
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001799 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001800 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001801 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001802 SortedFiles.push_front(Entry);
1803 }
1804
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001805 unsigned UserFilesNum = 0;
1806 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001807 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001808 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001809 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001810 if (InputFileID != 0)
1811 continue; // already recorded this file.
1812
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001813 // Record this entry's offset.
1814 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001815
1816 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001817
Douglas Gregor49491f72013-03-15 22:15:07 +00001818 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001819 ++UserFilesNum;
1820
Douglas Gregor72be3902012-10-19 00:38:02 +00001821 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001822 // And whether this file was overridden.
1823 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001824 INPUT_FILE,
1825 InputFileOffsets.size(),
1826 (uint64_t)Entry.File->getSize(),
1827 (uint64_t)getTimestampForOutput(Entry.File),
1828 Entry.BufferOverridden,
Richard Smithf3f84612017-06-29 02:19:42 +00001829 Entry.IsTransient,
1830 Entry.IsTopLevelModuleMap};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001831
Richard Smith7ed1bc92014-12-05 22:42:13 +00001832 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1833 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001834
Douglas Gregor112b9072012-10-18 05:31:06 +00001835 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001836
1837 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001838 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001839 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1840 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001841 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1842 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001843 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001844 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001845
1846 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001847 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1848 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001849 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001850}
1851
Douglas Gregora7f71a92009-04-10 03:52:48 +00001852//===----------------------------------------------------------------------===//
1853// Source Manager Serialization
1854//===----------------------------------------------------------------------===//
1855
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001856/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001857/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001858static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001859 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001860
David Blaikieb44f0bf2017-01-04 22:36:43 +00001861 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001862 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001867 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001872 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001873}
1874
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001875/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001876/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001877static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001878 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001879
David Blaikieb44f0bf2017-01-04 22:36:43 +00001880 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001881 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001887 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001888}
1889
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001890/// Create an abbreviation for the SLocEntry that refers to a
Douglas Gregora7f71a92009-04-10 03:52:48 +00001891/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001892static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1893 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001894 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001895
David Blaikieb44f0bf2017-01-04 22:36:43 +00001896 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001897 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1898 : SM_SLOC_BUFFER_BLOB));
1899 if (Compressed)
1900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001902 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001903}
1904
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001905/// Create an abbreviation for the SLocEntry that refers to a macro
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001906/// expansion.
1907static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001908 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001909
David Blaikieb44f0bf2017-01-04 22:36:43 +00001910 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001911 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Richard Smithb5f81712018-04-30 05:25:48 +00001916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is token range
Douglas Gregor83243272009-04-15 18:05:10 +00001917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001918 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001919}
1920
Douglas Gregor09b69892011-02-10 17:09:37 +00001921namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001922
Douglas Gregor09b69892011-02-10 17:09:37 +00001923 // Trait used for the on-disk hash table of header search information.
1924 class HeaderFileInfoTrait {
1925 ASTWriter &Writer;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001926
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001927 // Keep track of the framework names we've used during serialization.
1928 SmallVector<char, 128> FrameworkStringData;
1929 llvm::StringMap<unsigned> FrameworkNameOffset;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001930
Douglas Gregor09b69892011-02-10 17:09:37 +00001931 public:
Richard Smith040e1262017-06-02 01:55:39 +00001932 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1933
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001934 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001935 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001936 off_t Size;
1937 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001938 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001939 using key_type_ref = const key_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001940
1941 using UnresolvedModule =
1942 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001943
Richard Smith040e1262017-06-02 01:55:39 +00001944 struct data_type {
1945 const HeaderFileInfo &HFI;
1946 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1947 UnresolvedModule Unresolved;
1948 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001949 using data_type_ref = const data_type &;
Richard Smith040e1262017-06-02 01:55:39 +00001950
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001951 using hash_value_type = unsigned;
1952 using offset_type = unsigned;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001953
Richard Smithe75ee0f2015-08-17 07:13:32 +00001954 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001955 // The hash is based only on size/time of the file, so that the reader can
1956 // match even when symlinking or excess path elements ("foo/../", "../")
1957 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00001958 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00001959 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001960
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001961 std::pair<unsigned, unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001962 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001963 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001964
Peter Collingbournee3f65292018-05-18 19:46:24 +00001965 endian::Writer LE(Out, little);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001966 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001967 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001968 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00001969 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00001970 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1971 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00001972 if (Data.Unresolved.getPointer())
1973 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00001974 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001975 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001976 }
Richard Smith040e1262017-06-02 01:55:39 +00001977
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001978 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001979 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001980
Peter Collingbournee3f65292018-05-18 19:46:24 +00001981 endian::Writer LE(Out, little);
Richard Smith040e1262017-06-02 01:55:39 +00001982 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001983 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00001984 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001985 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001986 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001987 }
Richard Smith040e1262017-06-02 01:55:39 +00001988
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001989 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001990 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001991 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001992
Peter Collingbournee3f65292018-05-18 19:46:24 +00001993 endian::Writer LE(Out, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00001994 uint64_t Start = Out.tell(); (void)Start;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00001995
Richard Smithf3f84612017-06-29 02:19:42 +00001996 unsigned char Flags = (Data.HFI.isImport << 5)
1997 | (Data.HFI.isPragmaOnce << 4)
Richard Smith040e1262017-06-02 01:55:39 +00001998 | (Data.HFI.DirInfo << 1)
1999 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00002000 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00002001 LE.write<uint16_t>(Data.HFI.NumIncludes);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002002
Richard Smith040e1262017-06-02 01:55:39 +00002003 if (!Data.HFI.ControllingMacro)
2004 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00002005 else
Richard Smith040e1262017-06-02 01:55:39 +00002006 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
2007
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002008 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00002009 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002010 // If this header refers into a framework, save the framework name.
2011 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00002012 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002013 if (Pos == FrameworkNameOffset.end()) {
2014 Offset = FrameworkStringData.size() + 1;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002015 FrameworkStringData.append(Data.HFI.Framework.begin(),
Richard Smith040e1262017-06-02 01:55:39 +00002016 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002017 FrameworkStringData.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002018
Richard Smith040e1262017-06-02 01:55:39 +00002019 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002020 } else
2021 Offset = Pos->second;
2022 }
Justin Bognere1c147c2014-03-28 22:03:19 +00002023 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002024
Richard Smith040e1262017-06-02 01:55:39 +00002025 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
2026 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
2027 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00002028 assert((Value >> 2) == ModID && "overflow in header module info");
2029 LE.write<uint32_t>(Value);
2030 }
Richard Smith040e1262017-06-02 01:55:39 +00002031 };
2032
2033 // FIXME: If the header is excluded, we should write out some
2034 // record of that fact.
2035 for (auto ModInfo : Data.KnownHeaders)
2036 EmitModule(ModInfo.getModule(), ModInfo.getRole());
2037 if (Data.Unresolved.getPointer())
2038 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002039
Douglas Gregor09b69892011-02-10 17:09:37 +00002040 assert(Out.tell() - Start == DataLen && "Wrong data length");
2041 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002042
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002043 const char *strings_begin() const { return FrameworkStringData.begin(); }
2044 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00002045 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002046
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002047} // namespace
Douglas Gregor09b69892011-02-10 17:09:37 +00002048
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002049/// Write the header search block for the list of files that
Douglas Gregor09b69892011-02-10 17:09:37 +00002050///
2051/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00002052void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00002053 HeaderFileInfoTrait GeneratorTrait(*this);
2054 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
2055 SmallVector<const char *, 4> SavedStrings;
2056 unsigned NumHeaderSearchEntries = 0;
2057
2058 // Find all unresolved headers for the current module. We generally will
2059 // have resolved them before we get here, but not necessarily: we might be
2060 // compiling a preprocessed module, where there is no requirement for the
2061 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00002062 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00002063 if (WritingModule) {
2064 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
2065 while (!Worklist.empty()) {
2066 Module *M = Worklist.pop_back_val();
2067 if (!M->isAvailable())
2068 continue;
2069
2070 // Map to disk files where possible, to pick up any missing stat
2071 // information. This also means we don't need to check the unresolved
2072 // headers list when emitting resolved headers in the first loop below.
2073 // FIXME: It'd be preferable to avoid doing this if we were given
2074 // sufficient stat information in the module map.
2075 HS.getModuleMap().resolveHeaderDirectives(M);
2076
2077 // If the file didn't exist, we can still create a module if we were given
2078 // enough information in the module map.
2079 for (auto U : M->MissingHeaders) {
2080 // Check that we were given enough information to build a module
2081 // without this file existing on disk.
2082 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2083 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
2084 << WritingModule->getFullModuleName() << U.Size.hasValue()
2085 << U.FileName;
2086 continue;
2087 }
2088
2089 // Form the effective relative pathname for the file.
2090 SmallString<128> Filename(M->Directory->getName());
2091 llvm::sys::path::append(Filename, U.FileName);
2092 PreparePathForOutput(Filename);
2093
2094 StringRef FilenameDup = strdup(Filename.c_str());
2095 SavedStrings.push_back(FilenameDup.data());
2096
2097 HeaderFileInfoTrait::key_type Key = {
2098 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
2099 };
2100 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00002101 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00002102 };
2103 // FIXME: Deal with cases where there are multiple unresolved header
2104 // directives in different submodules for the same header.
2105 Generator.insert(Key, Data, GeneratorTrait);
2106 ++NumHeaderSearchEntries;
2107 }
2108
2109 Worklist.append(M->submodule_begin(), M->submodule_end());
2110 }
2111 }
2112
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002113 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00002114 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002115
Douglas Gregor09b69892011-02-10 17:09:37 +00002116 if (FilesByUID.size() > HS.header_file_size())
2117 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00002118
Douglas Gregor09b69892011-02-10 17:09:37 +00002119 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2120 const FileEntry *File = FilesByUID[UID];
2121 if (!File)
2122 continue;
2123
Richard Smith386bb072015-08-18 23:42:23 +00002124 // Get the file info. This will load info from the external source if
2125 // necessary. Skip emitting this file if we have no information on it
2126 // as a header file (in which case HFI will be null) or if it hasn't
2127 // changed since it was loaded. Also skip it if it's for a modular header
2128 // from a different module; in that case, we rely on the module(s)
2129 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00002130 const HeaderFileInfo *HFI =
2131 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
2132 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00002133 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00002134
Richard Smith7ed1bc92014-12-05 22:42:13 +00002135 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002136 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00002137 SmallString<128> FilenameTmp(Filename);
2138 if (PreparePathForOutput(FilenameTmp)) {
2139 // If we performed any translation on the file name at all, we need to
2140 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002141 Filename = StringRef(strdup(FilenameTmp.c_str()));
2142 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00002143 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002144
Richard Smith040e1262017-06-02 01:55:39 +00002145 HeaderFileInfoTrait::key_type Key = {
2146 Filename, File->getSize(), getTimestampForOutput(File)
2147 };
2148 HeaderFileInfoTrait::data_type Data = {
2149 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
2150 };
2151 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002152 ++NumHeaderSearchEntries;
2153 }
Richard Smith040e1262017-06-02 01:55:39 +00002154
Douglas Gregor09b69892011-02-10 17:09:37 +00002155 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002156 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002157 uint32_t BucketOffset;
2158 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002159 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002160
Douglas Gregor09b69892011-02-10 17:09:37 +00002161 llvm::raw_svector_ostream Out(TableData);
2162 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00002163 endian::write<uint32_t>(Out, 0, little);
Douglas Gregor09b69892011-02-10 17:09:37 +00002164 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2165 }
2166
2167 // Create a blob abbreviation
2168 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002169
David Blaikieb44f0bf2017-01-04 22:36:43 +00002170 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002171 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002176 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002177
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002178 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002179 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2180 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002181 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002182 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002183
Douglas Gregor09b69892011-02-10 17:09:37 +00002184 // Free all of the strings we had to duplicate.
2185 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002186 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002187}
2188
George Rimarc39f5492017-01-17 15:45:31 +00002189static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2190 unsigned SLocBufferBlobCompressedAbbrv,
2191 unsigned SLocBufferBlobAbbrv) {
Eugene Zelenkob7d89102017-11-11 00:08:50 +00002192 using RecordDataType = ASTWriter::RecordData::value_type;
George Rimarc39f5492017-01-17 15:45:31 +00002193
2194 // Compress the buffer if possible. We expect that almost all PCM
2195 // consumers will not want its contents.
2196 SmallString<0> CompressedBuffer;
2197 if (llvm::zlib::isAvailable()) {
2198 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2199 if (!E) {
2200 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2201 Blob.size() - 1};
2202 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2203 CompressedBuffer);
2204 return;
2205 }
2206 llvm::consumeError(std::move(E));
2207 }
2208
2209 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2210 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2211}
2212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002213/// Writes the block containing the serialized form of the
Douglas Gregora7f71a92009-04-10 03:52:48 +00002214/// source manager.
2215///
2216/// TODO: We should probably use an on-disk hash table (stored in a
2217/// blob), indexed based on the file name, so that we only create
2218/// entries for files that we actually need. In the common case (no
2219/// errors), we probably won't have to create file entries for any of
2220/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002221void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002222 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002223 RecordData Record;
2224
Chris Lattner0910e3b2009-04-10 17:16:57 +00002225 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002226 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002227
2228 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002229 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2230 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002231 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2232 unsigned SLocBufferBlobCompressedAbbrv =
2233 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002234 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002235
Douglas Gregor258ae542009-04-27 06:38:32 +00002236 // Write out the source location entry table. We skip the first
2237 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002238 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002239 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002240 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2241 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002242 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002243 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002244 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002245 FileID FID = FileID::get(I);
2246 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002247
Douglas Gregor258ae542009-04-27 06:38:32 +00002248 // Record the offset of this source-location entry.
2249 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2250
2251 // Figure out which record code to use.
2252 unsigned Code;
2253 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002254 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2255 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002256 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002257 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002258 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002259 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002260 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002261 Record.clear();
2262 Record.push_back(Code);
2263
Douglas Gregor925296b2011-07-19 16:10:42 +00002264 // Starting offset of this entry within this module, so skip the dummy.
2265 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002266 if (SLoc->isFile()) {
2267 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002268 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002269 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2270 Record.push_back(File.hasLineDirectives());
2271
2272 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002273 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002274 if (Content->OrigEntry) {
2275 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002276 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002277
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002278 // The source location entry is a file. Emit input file ID.
2279 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2280 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002281
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002282 Record.push_back(File.NumCreatedFIDs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002283
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002284 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002285 if (FDI != FileDeclIDs.end()) {
2286 Record.push_back(FDI->second->FirstDeclIndex);
2287 Record.push_back(FDI->second->DeclIDs.size());
2288 } else {
2289 Record.push_back(0);
2290 Record.push_back(0);
2291 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002292
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002293 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002294
Richard Smithaada85c2016-02-06 02:06:43 +00002295 if (Content->BufferOverridden || Content->IsTransient)
2296 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002297 } else {
2298 // The source location entry is a buffer. The blob associated
2299 // with this entry contains the contents of the buffer.
2300
2301 // We add one to the size so that we capture the trailing NULL
2302 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2303 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002304 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002305 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002306 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002307 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002308 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002309 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002310
Mehdi Amini99d1b292016-10-01 16:38:28 +00002311 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002312 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002313 }
Richard Smithaada85c2016-02-06 02:06:43 +00002314
2315 if (EmitBlob) {
2316 // Include the implicit terminating null character in the on-disk buffer
2317 // if we're writing it uncompressed.
2318 const llvm::MemoryBuffer *Buffer =
2319 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2320 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002321 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2322 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002323 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002324 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002325 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002326 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002327 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2328 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2329 AddSourceLocation(Expansion.isMacroArgExpansion()
2330 ? SourceLocation()
2331 : Expansion.getExpansionLocEnd(),
2332 Record);
Richard Smithb5f81712018-04-30 05:25:48 +00002333 Record.push_back(Expansion.isExpansionTokenRange());
Douglas Gregor258ae542009-04-27 06:38:32 +00002334
2335 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002336 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002337 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002338 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002339 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002340 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002341 }
2342 }
2343
Douglas Gregor8f45df52009-04-16 22:23:12 +00002344 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002345
2346 if (SLocEntryOffsets.empty())
2347 return;
2348
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002349 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002350 // table is used for lazily loading source-location information.
2351 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002352
David Blaikieb44f0bf2017-01-04 22:36:43 +00002353 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002354 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002358 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002359 {
2360 RecordData::value_type Record[] = {
2361 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2362 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2363 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2364 bytes(SLocEntryOffsets));
2365 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002366 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002367 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002368 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002369
2370 // Write the line table. It depends on remapping working, so it must come
2371 // after the source location offsets.
2372 if (SourceMgr.hasLineTable()) {
2373 LineTableInfo &LineTable = SourceMgr.getLineTable();
2374
2375 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002376
2377 // Emit the needed file names.
2378 llvm::DenseMap<int, int> FilenameMap;
Hans Wennborg14487362017-12-04 22:28:45 +00002379 FilenameMap[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00002380 for (const auto &L : LineTable) {
2381 if (L.first.ID < 0)
2382 continue;
2383 for (auto &LE : L.second) {
2384 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
Hans Wennborg14487362017-12-04 22:28:45 +00002385 FilenameMap.size() - 1)).second)
Richard Smith63078492015-09-01 07:41:55 +00002386 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2387 }
2388 }
2389 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002390
2391 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002392 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002393 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002394 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002395 continue;
2396
2397 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002398 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002399
2400 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002401 Record.push_back(L.second.size());
2402 for (const auto &LE : L.second) {
2403 Record.push_back(LE.FileOffset);
2404 Record.push_back(LE.LineNo);
2405 Record.push_back(FilenameMap[LE.FilenameID]);
2406 Record.push_back((unsigned)LE.FileKind);
2407 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002408 }
2409 }
Richard Smith63078492015-09-01 07:41:55 +00002410
Douglas Gregor925296b2011-07-19 16:10:42 +00002411 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2412 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002413}
2414
Douglas Gregorc5046832009-04-27 18:38:38 +00002415//===----------------------------------------------------------------------===//
2416// Preprocessor Serialization
2417//===----------------------------------------------------------------------===//
2418
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002419static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2420 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002421 if (MacroInfo *MI = MD->getMacroInfo())
2422 if (MI->isBuiltinMacro())
2423 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002424
2425 if (IsModule) {
2426 SourceLocation Loc = MD->getLocation();
2427 if (Loc.isInvalid())
2428 return true;
2429 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2430 return true;
2431 }
2432
2433 return false;
2434}
2435
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002436/// Writes the block containing the serialized form of the
Chris Lattnereeffaef2009-04-10 17:15:23 +00002437/// preprocessor.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002438void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002439 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2440 if (PPRec)
2441 WritePreprocessorDetail(*PPRec);
2442
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002443 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002444 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002445
Chris Lattner0af3ba12009-04-13 01:29:17 +00002446 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2447 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002448 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002449 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002450 }
2451
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002452 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2453 assert(!IsModule);
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00002454 auto SkipInfo = PP.getPreambleSkipInfo();
2455 if (SkipInfo.hasValue()) {
2456 Record.push_back(true);
2457 AddSourceLocation(SkipInfo->HashTokenLoc, Record);
2458 AddSourceLocation(SkipInfo->IfTokenLoc, Record);
2459 Record.push_back(SkipInfo->FoundNonSkipPortion);
2460 Record.push_back(SkipInfo->FoundElse);
2461 AddSourceLocation(SkipInfo->ElseLoc, Record);
2462 } else {
2463 Record.push_back(false);
2464 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002465 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2466 AddSourceLocation(Cond.IfLoc, Record);
2467 Record.push_back(Cond.WasSkipping);
2468 Record.push_back(Cond.FoundNonSkip);
2469 Record.push_back(Cond.FoundElse);
2470 }
2471 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2472 Record.clear();
2473 }
2474
Douglas Gregoreda6a892009-04-26 00:07:37 +00002475 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002476 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002477
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002478 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002479 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002480 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002481 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002482
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002483 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002484 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002485
Richard Smithee977932015-05-01 21:22:17 +00002486 // Construct the list of identifiers with macro directives that need to be
2487 // serialized.
2488 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2489 for (auto &Id : PP.getIdentifierTable())
2490 if (Id.second->hadMacroDefinition() &&
2491 (!Id.second->isFromAST() ||
2492 Id.second->hasChangedSinceDeserialization()))
2493 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002494 // Sort the set of macro definitions that need to be serialized by the
2495 // name of the macro, to provide a stable ordering.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00002496 llvm::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2497 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002498
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002499 // Emit the macro directives as a list and associate the offset with the
2500 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002501 for (const IdentifierInfo *Name : MacroIdentifiers) {
2502 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002503 auto StartOffset = Stream.GetCurrentBitNo();
2504
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002505 // Emit the macro directives in reverse source order.
2506 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002507 // Once we hit an ignored macro, we're done: the rest of the chain
2508 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002509 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002510 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002511
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002512 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002513 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002514 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002515 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002516 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002517 Record.push_back(VisMD->isPublic());
2518 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002519 }
Richard Smithd7329392015-04-21 21:46:32 +00002520
Richard Smithb8b2ed62015-04-23 18:18:26 +00002521 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002522 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002523 // We write out exported module macros for PCH as well.
2524 auto Leafs = PP.getLeafModuleMacros(Name);
2525 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2526 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2527 while (!Worklist.empty()) {
2528 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002529
Manman Ren33d80292016-05-31 18:19:32 +00002530 // Emit a record indicating this submodule exports this macro.
2531 ModuleMacroRecord.push_back(
2532 getSubmoduleID(Macro->getOwningModule()));
2533 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2534 for (auto *M : Macro->overrides())
2535 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002536
Manman Ren33d80292016-05-31 18:19:32 +00002537 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2538 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002539
Manman Ren33d80292016-05-31 18:19:32 +00002540 // Enqueue overridden macros once we've visited all their ancestors.
2541 for (auto *M : Macro->overrides())
2542 if (++Visits[M] == M->getNumOverridingMacros())
2543 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002544
Manman Ren33d80292016-05-31 18:19:32 +00002545 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002546 }
Richard Smithd7329392015-04-21 21:46:32 +00002547
Richard Smithee977932015-05-01 21:22:17 +00002548 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002549 continue;
2550
Richard Smithd7329392015-04-21 21:46:32 +00002551 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002552 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2553 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002554 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002555
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002556 /// Offsets of each of the macros into the bitstream, indexed by
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002557 /// the local macro ID
2558 ///
2559 /// For each identifier that is associated with a macro, this map
2560 /// provides the offset into the bitstream where that macro is
2561 /// defined.
2562 std::vector<uint32_t> MacroOffsets;
2563
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002564 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2565 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2566 MacroInfo *MI = MacroInfosToEmit[I].MI;
2567 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002568
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002569 if (ID < FirstMacroID) {
2570 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2571 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002572 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002573
2574 // Record the local offset of this macro.
2575 unsigned Index = ID - FirstMacroID;
2576 if (Index == MacroOffsets.size())
2577 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2578 else {
2579 if (Index > MacroOffsets.size())
2580 MacroOffsets.resize(Index + 1);
2581
2582 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2583 }
2584
2585 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002586 AddSourceLocation(MI->getDefinitionLoc(), Record);
2587 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2588 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002589 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002590 unsigned Code;
2591 if (MI->isObjectLike()) {
2592 Code = PP_MACRO_OBJECT_LIKE;
2593 } else {
2594 Code = PP_MACRO_FUNCTION_LIKE;
2595
2596 Record.push_back(MI->isC99Varargs());
2597 Record.push_back(MI->isGNUVarargs());
2598 Record.push_back(MI->hasCommaPasting());
Faisal Valiac506d72017-07-17 17:18:43 +00002599 Record.push_back(MI->getNumParams());
2600 for (const IdentifierInfo *Param : MI->params())
2601 AddIdentifierRef(Param, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002602 }
2603
2604 // If we have a detailed preprocessing record, record the macro definition
2605 // ID that corresponds to this macro.
2606 if (PPRec)
2607 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2608
2609 Stream.EmitRecord(Code, Record);
2610 Record.clear();
2611
2612 // Emit the tokens array.
2613 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2614 // Note that we know that the preprocessor does not have any annotation
2615 // tokens in it because they are created by the parser, and thus can't
2616 // be in a macro definition.
2617 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002618 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002619 Stream.EmitRecord(PP_TOKEN, Record);
2620 Record.clear();
2621 }
2622 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002623 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002624
Douglas Gregor92a96f52011-02-08 21:58:10 +00002625 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002626
2627 // Write the offsets table for macro IDs.
2628 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002629
David Blaikieb44f0bf2017-01-04 22:36:43 +00002630 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002631 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2635
David Blaikieb44f0bf2017-01-04 22:36:43 +00002636 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002637 {
2638 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2639 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2640 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2641 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002642}
2643
2644void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002645 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002646 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002647
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002648 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002649
Douglas Gregor92a96f52011-02-08 21:58:10 +00002650 // Enter the preprocessor block.
2651 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002652
Douglas Gregoraae92242010-03-19 21:51:54 +00002653 // If the preprocessor has a preprocessing record, emit it.
2654 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002655 using namespace llvm;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002656
2657 // Set up the abbreviation for
Douglas Gregor92a96f52011-02-08 21:58:10 +00002658 unsigned InclusionAbbrev = 0;
2659 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002660 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002661 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002662 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2663 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002667 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002668 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002669
2670 unsigned FirstPreprocessorEntityID
2671 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002672 + NUM_PREDEF_PP_ENTITY_IDS;
2673 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002674 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002675 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2676 EEnd = PPRec.local_end();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002677 E != EEnd;
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002678 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002679 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002680
Richard Smith66a81862015-05-04 02:25:31 +00002681 PreprocessedEntityOffsets.push_back(
2682 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002683
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002684 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002685 // Record this macro definition's ID.
2686 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002687
Douglas Gregor92a96f52011-02-08 21:58:10 +00002688 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002689 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2690 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002691 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002692
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002693 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002694 Record.push_back(ME->isBuiltinMacro());
2695 if (ME->isBuiltinMacro())
2696 AddIdentifierRef(ME->getName(), Record);
2697 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002698 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002699 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002700 continue;
2701 }
2702
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002703 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002704 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002705 Record.push_back(ID->getFileName().size());
2706 Record.push_back(ID->wasInQuotes());
2707 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002708 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002709 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002710 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002711 // Check that the FileEntry is not null because it was not resolved and
2712 // we create a PCH even with compiler errors.
2713 if (ID->getFile())
2714 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002715 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002716 continue;
2717 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002718
Douglas Gregor92a96f52011-02-08 21:58:10 +00002719 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2720 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002721 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002722
Douglas Gregoraae92242010-03-19 21:51:54 +00002723 // Write the offsets table for the preprocessing record.
2724 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002725 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2726
Douglas Gregoraae92242010-03-19 21:51:54 +00002727 // Write the offsets table for identifier IDs.
2728 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002729
David Blaikieb44f0bf2017-01-04 22:36:43 +00002730 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002731 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002734 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002735
Mehdi Amini57a41912015-09-10 01:46:39 +00002736 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2737 FirstPreprocessorEntityID -
2738 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002739 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002740 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002741 }
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00002742
2743 // Write the skipped region table for the preprocessing record.
2744 ArrayRef<SourceRange> SkippedRanges = PPRec.getSkippedRanges();
2745 if (SkippedRanges.size() > 0) {
2746 std::vector<PPSkippedRange> SerializedSkippedRanges;
2747 SerializedSkippedRanges.reserve(SkippedRanges.size());
2748 for (auto const& Range : SkippedRanges)
2749 SerializedSkippedRanges.emplace_back(Range);
2750
2751 using namespace llvm;
2752 auto Abbrev = std::make_shared<BitCodeAbbrev>();
2753 Abbrev->Add(BitCodeAbbrevOp(PPD_SKIPPED_RANGES));
2754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2755 unsigned PPESkippedRangeAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2756
2757 Record.clear();
2758 Record.push_back(PPD_SKIPPED_RANGES);
2759 Stream.EmitRecordWithBlob(PPESkippedRangeAbbrev, Record,
2760 bytes(SerializedSkippedRanges));
2761 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002762}
2763
Richard Smith386bb072015-08-18 23:42:23 +00002764unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002765 if (!Mod)
2766 return 0;
2767
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002768 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2769 if (Known != SubmoduleIDs.end())
2770 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002771
Richard Smithdc1f0422016-07-20 19:10:16 +00002772 auto *Top = Mod->getTopLevelModule();
2773 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002774 (getLangOpts().CompilingPCH ||
2775 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002776 return 0;
2777
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002778 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2779}
2780
Richard Smith386bb072015-08-18 23:42:23 +00002781unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2782 // FIXME: This can easily happen, if we have a reference to a submodule that
2783 // did not result in us loading a module file for that submodule. For
2784 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2785 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2786 assert((ID || !Mod) &&
2787 "asked for module ID for non-local, non-imported module");
2788 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002789}
2790
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002791/// Compute the number of modules within the given tree (including the
Douglas Gregor253eefe2011-12-01 00:59:36 +00002792/// given module).
2793static unsigned getNumberOfModules(Module *Mod) {
2794 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002795 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002796 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002797 ChildModules += getNumberOfModules(*Sub);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002798
Douglas Gregor253eefe2011-12-01 00:59:36 +00002799 return ChildModules + 1;
2800}
2801
Douglas Gregorde3ef502011-11-30 23:21:26 +00002802void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002803 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002804 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002805
Douglas Gregor69021972011-11-30 17:33:56 +00002806 // Write the abbreviations needed for the submodules block.
2807 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002808
David Blaikieb44f0bf2017-01-04 22:36:43 +00002809 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002810 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
Richard Smithdd8b5332017-09-04 05:37:53 +00002813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
Douglas Gregor69021972011-11-30 17:33:56 +00002814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002818 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002819 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002820 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002821 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002822 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ModuleMapIsPriv...
Douglas Gregor69021972011-11-30 17:33:56 +00002823 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002824 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002825
David Blaikieb44f0bf2017-01-04 22:36:43 +00002826 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002827 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002829 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002830
David Blaikieb44f0bf2017-01-04 22:36:43 +00002831 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002832 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002834 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002835
David Blaikieb44f0bf2017-01-04 22:36:43 +00002836 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002837 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002839 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002840
David Blaikieb44f0bf2017-01-04 22:36:43 +00002841 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002842 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002844 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002845
David Blaikieb44f0bf2017-01-04 22:36:43 +00002846 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002847 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002850 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002851
David Blaikieb44f0bf2017-01-04 22:36:43 +00002852 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002853 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2854 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002855 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002856
David Blaikieb44f0bf2017-01-04 22:36:43 +00002857 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002858 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2859 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002860 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002861
David Blaikieb44f0bf2017-01-04 22:36:43 +00002862 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002863 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002865 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002866
David Blaikieb44f0bf2017-01-04 22:36:43 +00002867 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002868 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002870 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002871
David Blaikieb44f0bf2017-01-04 22:36:43 +00002872 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002873 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002876 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002877
David Blaikieb44f0bf2017-01-04 22:36:43 +00002878 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002879 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002881 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002882
David Blaikieb44f0bf2017-01-04 22:36:43 +00002883 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002884 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002887 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002888
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002889 Abbrev = std::make_shared<BitCodeAbbrev>();
2890 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2892 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2893
Douglas Gregor253eefe2011-12-01 00:59:36 +00002894 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002895 RecordData::value_type Record[] = {
2896 getNumberOfModules(WritingModule),
Richard Smithdd8b5332017-09-04 05:37:53 +00002897 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002898 Stream.EmitRecord(SUBMODULE_METADATA, Record);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002899
Douglas Gregor69021972011-11-30 17:33:56 +00002900 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002901 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002902 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002903 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002904 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002905 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002906 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002907
2908 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002909 if (Mod->Parent) {
2910 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002911 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002912 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002913
2914 // Emit the definition of the block.
2915 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002916 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2917 ID,
2918 ParentID,
Simon Pilgrimf331a662017-09-04 10:54:39 +00002919 (RecordData::value_type)Mod->Kind,
David Blaikie9ffe5a32017-01-30 05:00:26 +00002920 Mod->IsFramework,
2921 Mod->IsExplicit,
2922 Mod->IsSystem,
2923 Mod->IsExternC,
2924 Mod->InferSubmodules,
2925 Mod->InferExplicitSubmodules,
2926 Mod->InferExportWildcard,
Jordan Rose90b0a1f2018-04-20 17:16:04 +00002927 Mod->ConfigMacrosExhaustive,
2928 Mod->ModuleMapIsPrivate};
Mehdi Amini57a41912015-09-10 01:46:39 +00002929 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2930 }
2931
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002932 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002933 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002934 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002935 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002936 }
2937
Douglas Gregor69021972011-11-30 17:33:56 +00002938 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002939 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002940 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002941 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2942 UmbrellaHeader.NameAsWritten);
2943 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002944 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2945 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002946 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002947 }
Richard Smith306d8922014-10-22 23:50:56 +00002948
Douglas Gregor69021972011-11-30 17:33:56 +00002949 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002950 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002951 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002952 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002953 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002954 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002955 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2956 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2957 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002958 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002959 Module::HK_PrivateTextual},
2960 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002961 };
2962 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002963 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002964 for (auto &H : Mod->Headers[HL.HeaderKind])
2965 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2966 }
2967
2968 // Emit the top headers.
2969 {
2970 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002971 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002972 for (auto *H : TopHeaders)
2973 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002974 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002975
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002976 // Emit the imports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002977 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002978 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002979 for (auto *I : Mod->Imports)
2980 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002981 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2982 }
2983
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00002984 // Emit the exports.
Douglas Gregor24bb9232011-12-02 18:58:38 +00002985 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002986 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002987 for (const auto &E : Mod->Exports) {
2988 // FIXME: This may fail; we don't require that all exported modules
2989 // are local or imported.
2990 Record.push_back(getSubmoduleID(E.getPointer()));
2991 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002992 }
2993 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2994 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002995
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002996 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2997 // Might be unnecessary as use declarations are only used to build the
2998 // module itself.
2999
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003000 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00003001 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003002 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
3003 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00003004 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003005 }
3006
Douglas Gregorfb912652013-03-20 21:10:35 +00003007 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00003008 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00003009 // FIXME: This may fail; we don't require that all conflicting modules
3010 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00003011 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
3012 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00003013 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00003014 }
3015
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003016 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00003017 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003018 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00003019 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003020 }
3021
Richard Smithdc1f0422016-07-20 19:10:16 +00003022 // Emit the initializers, if any.
3023 RecordData Inits;
3024 for (Decl *D : Context->getModuleInitializers(Mod))
3025 Inits.push_back(GetDeclRef(D));
3026 if (!Inits.empty())
3027 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
3028
Douglas Gregorf0b11de2017-09-14 23:38:44 +00003029 // Emit the name of the re-exported module, if any.
3030 if (!Mod->ExportAsModule.empty()) {
3031 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
3032 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
3033 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003034
Douglas Gregor69021972011-11-30 17:33:56 +00003035 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00003036 for (auto *M : Mod->submodules())
3037 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00003038 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003039
Douglas Gregor69021972011-11-30 17:33:56 +00003040 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00003041
Richard Smith23d8d032015-05-14 00:45:20 +00003042 assert((NextSubmoduleID - FirstSubmoduleID ==
3043 getNumberOfModules(WritingModule)) &&
3044 "Wrong # of submodules; found a reference to a non-local, "
3045 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00003046}
3047
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00003048void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
3049 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003050 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
3051 DiagStateIDMap;
3052 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003053 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003054
Richard Smithe37391c2017-05-03 00:28:49 +00003055 auto EncodeDiagStateFlags =
3056 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
3057 unsigned Result = (unsigned)DS->ExtBehavior;
3058 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00003059 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
3060 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
3061 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00003062 Result = (Result << 1) | Val;
3063 return Result;
3064 };
3065
3066 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
3067 Record.push_back(Flags);
3068
Richard Smithd230de22017-01-26 01:01:01 +00003069 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
3070 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00003071 // Ensure that the diagnostic state wasn't modified since it was created.
3072 // We will not correctly round-trip this information otherwise.
3073 assert(Flags == EncodeDiagStateFlags(State) &&
3074 "diag state flags vary in single AST file");
3075
Richard Smithd230de22017-01-26 01:01:01 +00003076 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003077 Record.push_back(DiagStateID);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003078
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003079 if (DiagStateID == 0) {
3080 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003081
3082 // Add a placeholder for the number of mappings.
3083 auto SizeIdx = Record.size();
3084 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00003085 for (const auto &I : *State) {
3086 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003087 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00003088 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003089 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003090 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003091 // Update the placeholder.
3092 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00003093 }
3094 };
3095
3096 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00003097
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003098 // Reserve a spot for the number of locations with state transitions.
3099 auto NumLocationsIdx = Record.size();
3100 Record.emplace_back();
3101
3102 // Emit the state transitions.
3103 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00003104 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3105 if (!FileIDAndFile.first.isValid() ||
3106 !FileIDAndFile.second.HasLocalTransitions)
3107 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003108 ++NumLocations;
Richard Smith6c2b5a82018-02-09 01:15:13 +00003109
3110 SourceLocation Loc = Diag.SourceMgr->getComposedLoc(FileIDAndFile.first, 0);
3111 assert(!Loc.isInvalid() && "start loc for valid FileID is invalid");
3112 AddSourceLocation(Loc, Record);
3113
Richard Smithd230de22017-01-26 01:01:01 +00003114 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3115 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3116 Record.push_back(StatePoint.Offset);
3117 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003118 }
3119 }
3120
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003121 // Backpatch the number of locations.
3122 Record[NumLocationsIdx] = NumLocations;
3123
3124 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00003125 //
3126 // This also protects against a hypothetical corner case with simulating
3127 // -Werror settings for implicit modules in the ASTReader, where reading
3128 // CurDiagState out of context could change whether warning pragmas are
3129 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003130 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3131 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3132
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00003133 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003134}
3135
Douglas Gregorc5046832009-04-27 18:38:38 +00003136//===----------------------------------------------------------------------===//
3137// Type Serialization
3138//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00003139
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003140/// Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003141void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00003142 TypeIdx &IdxRef = TypeIdxs[T];
3143 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3144 IdxRef = TypeIdx(NextTypeID++);
3145 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00003146
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003147 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00003148
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003149 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00003150
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003151 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003152 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00003153 W.Visit(T);
3154 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00003155
Richard Smith290d8012016-04-06 17:06:00 +00003156 // Record the offset for this type.
3157 unsigned Index = Idx.getIndex() - FirstTypeID;
3158 if (TypeOffsets.size() == Index)
3159 TypeOffsets.push_back(Offset);
3160 else if (TypeOffsets.size() < Index) {
3161 TypeOffsets.resize(Index + 1);
3162 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00003163 } else {
Richard Smith290d8012016-04-06 17:06:00 +00003164 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003165 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003166}
3167
Douglas Gregorc5046832009-04-27 18:38:38 +00003168//===----------------------------------------------------------------------===//
3169// Declaration Serialization
3170//===----------------------------------------------------------------------===//
3171
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003172/// Write the block containing all of the declaration IDs
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003173/// lexically declared within the given DeclContext.
3174///
3175/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003176/// bitstream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003177uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003178 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003179 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003180 return 0;
3181
Douglas Gregor8f45df52009-04-16 22:23:12 +00003182 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003183 SmallVector<uint32_t, 128> KindDeclPairs;
3184 for (const auto *D : DC->decls()) {
3185 KindDeclPairs.push_back(D->getKind());
3186 KindDeclPairs.push_back(GetDeclRef(D));
3187 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003188
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003189 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00003190 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00003191 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3192 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003193 return Offset;
3194}
3195
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003196void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003197 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003198
3199 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003200 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003201 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00003203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003205 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003206 {
3207 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3208 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3209 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3210 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003211
3212 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003213 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003214 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00003216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003218 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003219 {
3220 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3221 FirstDeclID - NUM_PREDEF_DECL_IDS};
3222 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3223 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003224}
3225
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003226void ASTWriter::WriteFileDeclIDsMap() {
3227 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003228
Chandler Carruthb306d732015-03-27 00:31:20 +00003229 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3230 FileDeclIDs.begin(), FileDeclIDs.end());
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00003231 llvm::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3232 llvm::less_first());
Chandler Carruthb306d732015-03-27 00:31:20 +00003233
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003234 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003235 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3236 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3237 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3238 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3239 for (auto &LocDeclEntry : Info.DeclIDs)
3240 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003241 }
3242
David Blaikieb44f0bf2017-01-04 22:36:43 +00003243 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003244 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003247 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003248 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3249 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003250 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003251}
3252
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003253void ASTWriter::WriteComments() {
3254 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003255 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003256 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003257 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003258 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003259 AddSourceRange(I->getSourceRange(), Record);
3260 Record.push_back(I->getKind());
3261 Record.push_back(I->isTrailingComment());
3262 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003263 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3264 }
3265 Stream.ExitBlock();
3266}
3267
Douglas Gregorc5046832009-04-27 18:38:38 +00003268//===----------------------------------------------------------------------===//
3269// Global Method Pool and Selector Serialization
3270//===----------------------------------------------------------------------===//
3271
Douglas Gregore84a9da2009-04-20 20:36:09 +00003272namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003273
Douglas Gregorc78d3462009-04-24 21:10:55 +00003274// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003275class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003276 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003277
3278public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003279 using key_type = Selector;
3280 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003281
Sebastian Redl834bb972010-08-04 17:20:04 +00003282 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003283 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003284 ObjCMethodList Instance, Factory;
3285 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003286 using data_type_ref = const data_type &;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003287
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003288 using hash_value_type = unsigned;
3289 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003290
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003291 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) {}
Mike Stump11289f42009-09-09 15:08:12 +00003292
Justin Bogner25463f12014-04-18 20:27:24 +00003293 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003294 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003295 }
Mike Stump11289f42009-09-09 15:08:12 +00003296
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003297 std::pair<unsigned, unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003298 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003299 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003300 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003301
Peter Collingbournee3f65292018-05-18 19:46:24 +00003302 endian::Writer LE(Out, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003303 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003304 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003305 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3306 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003307 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003308 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003309 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003310 for (const ObjCMethodList *Method = &Methods.Factory; 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;
Justin Bognere1c147c2014-03-28 22:03:19 +00003314 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003315 return std::make_pair(KeyLen, DataLen);
3316 }
Mike Stump11289f42009-09-09 15:08:12 +00003317
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003318 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003319 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003320
Peter Collingbournee3f65292018-05-18 19:46:24 +00003321 endian::Writer LE(Out, little);
Mike Stump11289f42009-09-09 15:08:12 +00003322 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003323 assert((Start >> 32) == 0 && "Selector key offset too large");
3324 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003325 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003326 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003327 if (N == 0)
3328 N = 1;
3329 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003330 LE.write<uint32_t>(
3331 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003332 }
Mike Stump11289f42009-09-09 15:08:12 +00003333
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003334 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003335 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003336 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003337
Peter Collingbournee3f65292018-05-18 19:46:24 +00003338 endian::Writer LE(Out, little);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003339 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003340 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003341 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003342 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003343 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003344 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003345 ++NumInstanceMethods;
3346
3347 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003348 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003349 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003350 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003351 ++NumFactoryMethods;
3352
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003353 unsigned InstanceBits = Methods.Instance.getBits();
3354 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003355 unsigned InstanceHasMoreThanOneDeclBit =
3356 Methods.Instance.hasMoreThanOneDecl();
3357 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3358 (InstanceHasMoreThanOneDeclBit << 2) |
3359 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003360 unsigned FactoryBits = Methods.Factory.getBits();
3361 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003362 unsigned FactoryHasMoreThanOneDeclBit =
3363 Methods.Factory.hasMoreThanOneDecl();
3364 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3365 (FactoryHasMoreThanOneDeclBit << 2) |
3366 FactoryBits;
3367 LE.write<uint16_t>(FullInstanceBits);
3368 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003369 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003370 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003371 if (Method->getMethod())
3372 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003373 for (const ObjCMethodList *Method = &Methods.Factory; 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()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003377
3378 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003379 }
3380};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003381
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003382} // namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +00003383
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003384/// Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003385///
3386/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003387/// in an on-disk hash table indexed by the selector. The hash table also
3388/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003389void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003390 using namespace llvm;
3391
Sebastian Redla19a67f2010-08-03 21:58:15 +00003392 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003393 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003394 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003395 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003396 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003397 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003398 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003399 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003400
Sebastian Redla19a67f2010-08-03 21:58:15 +00003401 // Create the on-disk hash table representation. We walk through every
3402 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003403 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003404 for (auto &SelectorAndID : SelectorIDs) {
3405 Selector S = SelectorAndID.first;
3406 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003407 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003408 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003409 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003410 ObjCMethodList(),
3411 ObjCMethodList()
3412 };
3413 if (F != SemaRef.MethodPool.end()) {
3414 Data.Instance = F->second.first;
3415 Data.Factory = F->second.second;
3416 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003417 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003418 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003419 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003420 // Selector already exists. Did it change?
3421 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003422 for (ObjCMethodList *M = &Data.Instance;
3423 !changed && M && M->getMethod(); M = M->getNext()) {
3424 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003425 changed = true;
3426 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003427 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003428 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003429 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003430 changed = true;
3431 }
3432 if (!changed)
3433 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003434 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003435 // A new method pool entry.
3436 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003437 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003438 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003439 }
3440
Douglas Gregorc78d3462009-04-24 21:10:55 +00003441 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003442 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003443 uint32_t BucketOffset;
3444 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003445 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003446
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003447 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003448 llvm::raw_svector_ostream Out(MethodPool);
3449 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003450 endian::write<uint32_t>(Out, 0, little);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003451 BucketOffset = Generator.Emit(Out, Trait);
3452 }
3453
3454 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003455 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003456 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003460 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003461
Douglas Gregor95c13f52009-04-25 17:48:32 +00003462 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003463 {
3464 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3465 NumTableEntries};
3466 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3467 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003468
3469 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003470 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003471 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003473 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003475 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003476
3477 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003478 {
3479 RecordData::value_type Record[] = {
3480 SELECTOR_OFFSETS, SelectorOffsets.size(),
3481 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3482 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3483 bytes(SelectorOffsets));
3484 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003485 }
3486}
3487
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003488/// Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003489void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003490 using namespace llvm;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003491
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003492 if (SemaRef.ReferencedSelectors.empty())
3493 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003494
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003495 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003496 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003497
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003498 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003499 // very tricky to fix, and given that @selector shouldn't really appear in
3500 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003501 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3502 Selector Sel = SelectorAndLocation.first;
3503 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003504 Writer.AddSelectorRef(Sel);
3505 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003506 }
Richard Smithe64e7452016-04-18 21:54:58 +00003507 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003508}
3509
Douglas Gregorc5046832009-04-27 18:38:38 +00003510//===----------------------------------------------------------------------===//
3511// Identifier Table Serialization
3512//===----------------------------------------------------------------------===//
3513
Richard Smithb3761912015-02-05 23:08:52 +00003514/// Determine the declaration that should be put into the name lookup table to
3515/// represent the given declaration in this module. This is usually D itself,
3516/// but if D was imported and merged into a local declaration, we want the most
3517/// recent local declaration instead. The chosen declaration will be the most
3518/// recent declaration in any module that imports this one.
3519static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3520 NamedDecl *D) {
3521 if (!LangOpts.Modules || !D->isFromASTFile())
3522 return D;
3523
3524 if (Decl *Redecl = D->getPreviousDecl()) {
3525 // For Redeclarable decls, a prior declaration might be local.
3526 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003527 // If we find a local decl, we're done.
3528 if (!Redecl->isFromASTFile()) {
3529 // Exception: in very rare cases (for injected-class-names), not all
3530 // redeclarations are in the same semantic context. Skip ones in a
3531 // different context. They don't go in this lookup table at all.
3532 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3533 D->getDeclContext()->getRedeclContext()))
3534 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003535 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003536 }
3537
Richard Smithfe620d22015-03-05 23:24:12 +00003538 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003539 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003540 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003541 break;
3542 }
3543 } else if (Decl *First = D->getCanonicalDecl()) {
3544 // For Mergeable decls, the first decl might be local.
3545 if (!First->isFromASTFile())
3546 return cast<NamedDecl>(First);
3547 }
3548
3549 // All declarations are imported. Our most recent declaration will also be
3550 // the most recent one in anyone who imports us.
3551 return D;
3552}
3553
Douglas Gregorc78d3462009-04-24 21:10:55 +00003554namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003555
Richard Smithcf97cf62015-04-19 01:34:23 +00003556class ASTIdentifierTableTrait {
3557 ASTWriter &Writer;
3558 Preprocessor &PP;
3559 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003560 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003561 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003562 ASTWriter::RecordData *InterestingIdentifierOffsets;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003563
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003564 /// Determines whether this is an "interesting" identifier that needs a
Richard Smithcf97cf62015-04-19 01:34:23 +00003565 /// full IdentifierInfo structure written into the hash table. Notably, this
3566 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3567 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003568 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003569 if (MacroOffset ||
3570 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003571 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003572 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003573 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003574 return true;
3575
3576 return false;
3577 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003578
Douglas Gregore84a9da2009-04-20 20:36:09 +00003579public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003580 using key_type = IdentifierInfo *;
3581 using key_type_ref = key_type;
Mike Stump11289f42009-09-09 15:08:12 +00003582
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003583 using data_type = IdentID;
3584 using data_type_ref = data_type;
Mike Stump11289f42009-09-09 15:08:12 +00003585
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003586 using hash_value_type = unsigned;
3587 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003588
Richard Smithd7329392015-04-21 21:46:32 +00003589 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003590 IdentifierResolver &IdResolver, bool IsModule,
3591 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003592 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003593 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3594 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003595
Richard Smithd79514e2016-02-05 19:03:40 +00003596 bool needDecls() const { return NeedDecls; }
3597
Justin Bogner25463f12014-04-18 20:27:24 +00003598 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003599 return llvm::djbHash(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003600 }
Mike Stump11289f42009-09-09 15:08:12 +00003601
Richard Smith33e0f7e2015-07-22 02:08:40 +00003602 bool isInterestingIdentifier(const IdentifierInfo *II) {
3603 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3604 return isInterestingIdentifier(II, MacroOffset);
3605 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003606
Richard Smith9c254182015-07-19 21:41:12 +00003607 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3608 return isInterestingIdentifier(II, 0);
3609 }
3610
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003611 std::pair<unsigned, unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003612 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003613 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003614 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003615 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3616 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003617 DataLen += 2; // 2 bytes for builtin ID
3618 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003619 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003620 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003621
Richard Smitha534a312015-07-21 23:54:07 +00003622 if (NeedDecls) {
3623 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3624 DEnd = IdResolver.end();
3625 D != DEnd; ++D)
3626 DataLen += 4;
3627 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003628 }
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003629
Justin Bognere1c147c2014-03-28 22:03:19 +00003630 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003631
Peter Collingbournee3f65292018-05-18 19:46:24 +00003632 endian::Writer LE(Out, little);
Justin Bognere1c147c2014-03-28 22:03:19 +00003633
Richard Smithdf8a8312015-03-13 04:05:01 +00003634 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003635 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003636 // We emit the key length after the data length so that every
3637 // string is preceded by a 16-bit length. This matches the PTH
3638 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003639 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003640 return std::make_pair(KeyLen, DataLen);
3641 }
Mike Stump11289f42009-09-09 15:08:12 +00003642
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003643 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003644 unsigned KeyLen) {
3645 // Record the location of the key data. This is used when generating
3646 // the mapping from persistent IDs to strings.
3647 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003648
3649 // Emit the offset of the key/data length information to the interesting
3650 // identifiers table if necessary.
3651 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3652 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3653
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003654 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003655 }
Mike Stump11289f42009-09-09 15:08:12 +00003656
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003657 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003658 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003659 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003660
Peter Collingbournee3f65292018-05-18 19:46:24 +00003661 endian::Writer LE(Out, little);
Richard Smithcf97cf62015-04-19 01:34:23 +00003662
Richard Smithd7329392015-04-21 21:46:32 +00003663 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3664 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003665 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003666 return;
3667 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003668
Justin Bognere1c147c2014-03-28 22:03:19 +00003669 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003670 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3671 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003672 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003673 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003674 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003675 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003676 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3677 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003678 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003679 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003680 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003681 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003682
Richard Smithd7329392015-04-21 21:46:32 +00003683 if (HadMacroDefinition)
3684 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003685
Richard Smitha534a312015-07-21 23:54:07 +00003686 if (NeedDecls) {
3687 // Emit the declaration IDs in reverse order, because the
3688 // IdentifierResolver provides the declarations as they would be
3689 // visible (e.g., the function "stat" would come before the struct
3690 // "stat"), but the ASTReader adds declarations to the end of the list
3691 // (so we need to see the struct "stat" before the function "stat").
3692 // Only emit declarations that aren't from a chained PCH, though.
3693 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3694 IdResolver.end());
3695 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3696 DEnd = Decls.rend();
3697 D != DEnd; ++D)
3698 LE.write<uint32_t>(
3699 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3700 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003701 }
3702};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003703
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003704} // namespace
Douglas Gregore84a9da2009-04-20 20:36:09 +00003705
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003706/// Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003707///
3708/// The identifier table consists of a blob containing string data
3709/// (the actual identifiers themselves) and a separate "offsets" index
3710/// that maps identifier IDs to locations within the blob.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003711void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003712 IdentifierResolver &IdResolver,
3713 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003714 using namespace llvm;
3715
Richard Smith33e0f7e2015-07-22 02:08:40 +00003716 RecordData InterestingIdents;
3717
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003718 // Create and write out the blob that contains the identifier
3719 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003720 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003721 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003722 ASTIdentifierTableTrait Trait(
3723 *this, PP, IdResolver, IsModule,
3724 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003725
Douglas Gregore6648fb2009-04-28 20:33:11 +00003726 // Look for any identifiers that were named while processing the
3727 // headers, but are otherwise not needed. We add these to the hash
3728 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003729 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003730 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003731 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003732 for (const auto &ID : PP.getIdentifierTable())
3733 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003734 // Sort the identifiers lexicographically before getting them references so
3735 // that their order is stable.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00003736 llvm::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
Chandler Carruth8440c982015-03-26 23:54:15 +00003737 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003738 if (Trait.isInterestingNonMacroIdentifier(II))
3739 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003740
Sebastian Redlff4a2952010-07-23 23:49:55 +00003741 // Create the on-disk hash table representation. We only store offsets
3742 // for identifiers that appear here for the first time.
3743 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003744 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003745 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003746 IdentID ID = IdentIDPair.second;
3747 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003748 // Write out identifiers if either the ID is local or the identifier has
3749 // changed since it was loaded.
3750 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3751 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003752 (Trait.needDecls() &&
3753 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003754 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003755 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003756
Douglas Gregore84a9da2009-04-20 20:36:09 +00003757 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003758 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003759 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003760 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003761 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003762
Douglas Gregore84a9da2009-04-20 20:36:09 +00003763 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003764 // Make sure that no bucket is at offset 0
Peter Collingbournee3f65292018-05-18 19:46:24 +00003765 endian::write<uint32_t>(Out, 0, little);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003766 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003767 }
3768
3769 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003770 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003771 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003774 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003775
3776 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003777 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003778 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003779 }
3780
3781 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003782 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003783 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003784 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003785 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003786 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003787 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003788
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003789#ifndef NDEBUG
3790 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3791 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3792#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003793
3794 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3795 IdentifierOffsets.size(),
3796 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003797 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003798 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003799
3800 // In C++, write the list of interesting identifiers (those that are
3801 // defined as macros, poisoned, or similar unusual things).
3802 if (!InterestingIdents.empty())
3803 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003804}
3805
Douglas Gregorc5046832009-04-27 18:38:38 +00003806//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003807// DeclContext's Name Lookup Table Serialization
3808//===----------------------------------------------------------------------===//
3809
3810namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003811
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003812// Trait used for the on-disk hash table used in the method pool.
3813class ASTDeclContextNameLookupTrait {
3814 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003815 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003816
3817public:
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003818 using key_type = DeclarationNameKey;
3819 using key_type_ref = key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003820
Richard Smithd88a7f12015-09-01 20:35:42 +00003821 /// A start and end index into DeclIDs, representing a sequence of decls.
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003822 using data_type = std::pair<unsigned, unsigned>;
3823 using data_type_ref = const data_type &;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003824
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003825 using hash_value_type = unsigned;
3826 using offset_type = unsigned;
Justin Bogner25463f12014-04-18 20:27:24 +00003827
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003828 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) {}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003829
Richard Smithd88a7f12015-09-01 20:35:42 +00003830 template<typename Coll>
3831 data_type getData(const Coll &Decls) {
3832 unsigned Start = DeclIDs.size();
3833 for (NamedDecl *D : Decls) {
3834 DeclIDs.push_back(
3835 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3836 }
3837 return std::make_pair(Start, DeclIDs.size());
3838 }
3839
3840 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3841 unsigned Start = DeclIDs.size();
3842 for (auto ID : FromReader)
3843 DeclIDs.push_back(ID);
3844 return std::make_pair(Start, DeclIDs.size());
3845 }
3846
3847 static bool EqualKey(key_type_ref a, key_type_ref b) {
3848 return a == b;
3849 }
3850
Richard Smitha06c7e62015-08-26 23:55:49 +00003851 hash_value_type ComputeHash(DeclarationNameKey Name) {
3852 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003853 }
3854
Richard Smithd88a7f12015-09-01 20:35:42 +00003855 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3856 assert(Writer.hasChain() &&
3857 "have reference to loaded module file but no chain?");
3858
3859 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003860
Peter Collingbournee3f65292018-05-18 19:46:24 +00003861 endian::write<uint32_t>(Out, Writer.getChain()->getModuleFileID(F), little);
Richard Smithd88a7f12015-09-01 20:35:42 +00003862 }
3863
Richard Smitha06c7e62015-08-26 23:55:49 +00003864 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3865 DeclarationNameKey Name,
3866 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003867 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003868
Peter Collingbournee3f65292018-05-18 19:46:24 +00003869 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003870 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003871 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003872 case DeclarationName::Identifier:
3873 case DeclarationName::ObjCZeroArgSelector:
3874 case DeclarationName::ObjCOneArgSelector:
3875 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003876 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003877 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003878 KeyLen += 4;
3879 break;
3880 case DeclarationName::CXXOperatorName:
3881 KeyLen += 1;
3882 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003883 case DeclarationName::CXXConstructorName:
3884 case DeclarationName::CXXDestructorName:
3885 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003886 case DeclarationName::CXXUsingDirective:
3887 break;
3888 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003889 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003890
Richard Smithf02662d2015-07-30 03:17:16 +00003891 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003892 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3893 assert(uint16_t(DataLen) == DataLen &&
3894 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003895 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003896
3897 return std::make_pair(KeyLen, DataLen);
3898 }
3899
Richard Smitha06c7e62015-08-26 23:55:49 +00003900 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003901 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003902
Peter Collingbournee3f65292018-05-18 19:46:24 +00003903 endian::Writer LE(Out, little);
Richard Smitha06c7e62015-08-26 23:55:49 +00003904 LE.write<uint8_t>(Name.getKind());
3905 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003906 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003907 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003908 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003909 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003910 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003911 case DeclarationName::ObjCZeroArgSelector:
3912 case DeclarationName::ObjCOneArgSelector:
3913 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003914 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003915 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003916 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003917 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003918 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003919 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003920 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003921 case DeclarationName::CXXConstructorName:
3922 case DeclarationName::CXXDestructorName:
3923 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003924 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003925 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003926 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003927
3928 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003929 }
3930
Richard Smitha06c7e62015-08-26 23:55:49 +00003931 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3932 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003933 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003934
Peter Collingbournee3f65292018-05-18 19:46:24 +00003935 endian::Writer LE(Out, little);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003936 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003937 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3938 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003939 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3940 }
3941};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003942
Eugene Zelenkob7d89102017-11-11 00:08:50 +00003943} // namespace
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003944
Chandler Carruthe972c362015-03-26 03:11:40 +00003945bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3946 DeclContext *DC) {
3947 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3948}
3949
3950bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3951 DeclContext *DC) {
3952 for (auto *D : Result.getLookupResult())
3953 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3954 return false;
3955
3956 return true;
3957}
3958
Richard Smithd88a7f12015-09-01 20:35:42 +00003959void
Chandler Carruthe972c362015-03-26 03:11:40 +00003960ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003961 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003962 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3963 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003964 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003965
Chandler Carruthe972c362015-03-26 03:11:40 +00003966 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003967 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003968 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3969
3970 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003971 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3972 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003973 ASTDeclContextNameLookupTrait Trait(*this);
3974
Chandler Carruthe972c362015-03-26 03:11:40 +00003975 // The first step is to collect the declaration names which we need to
3976 // serialize into the name lookup table, and to collect them in a stable
3977 // order.
3978 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003979
Chandler Carruthe972c362015-03-26 03:11:40 +00003980 // We also build up small sets of the constructor and conversion function
3981 // names which are visible.
3982 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003983
Chandler Carruthe972c362015-03-26 03:11:40 +00003984 for (auto &Lookup : *DC->buildLookup()) {
3985 auto &Name = Lookup.first;
3986 auto &Result = Lookup.second;
3987
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003988 // If there are no local declarations in our lookup result, we
3989 // don't need to write an entry for the name at all. If we can't
3990 // write out a lookup set without performing more deserialization,
3991 // just skip this entry.
3992 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003993 isLookupResultEntirelyExternal(Result, DC))
3994 continue;
3995
3996 // We also skip empty results. If any of the results could be external and
3997 // the currently available results are empty, then all of the results are
3998 // external and we skip it above. So the only way we get here with an empty
3999 // results is when no results could have been external *and* we have
4000 // external results.
4001 //
4002 // FIXME: While we might want to start emitting on-disk entries for negative
4003 // lookups into a decl context as an optimization, today we *have* to skip
4004 // them because there are names with empty lookup results in decl contexts
4005 // which we can't emit in any stable ordering: we lookup constructors and
4006 // conversion functions in the enclosing namespace scope creating empty
4007 // results for them. This in almost certainly a bug in Clang's name lookup,
4008 // but that is likely to be hard or impossible to fix and so we tolerate it
4009 // here by omitting lookups with empty results.
4010 if (Lookup.second.getLookupResult().empty())
4011 continue;
4012
4013 switch (Lookup.first.getNameKind()) {
4014 default:
4015 Names.push_back(Lookup.first);
4016 break;
4017
Richard Smithcd45dbc2014-04-19 03:48:30 +00004018 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004019 assert(isa<CXXRecordDecl>(DC) &&
4020 "Cannot have a constructor name outside of a class!");
4021 ConstructorNameSet.insert(Name);
4022 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004023
4024 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00004025 assert(isa<CXXRecordDecl>(DC) &&
4026 "Cannot have a conversion function name outside of a class!");
4027 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00004028 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004029 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004030 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00004031
Chandler Carruthe972c362015-03-26 03:11:40 +00004032 // Sort the names into a stable order.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004033 llvm::sort(Names.begin(), Names.end());
Chandler Carruthe972c362015-03-26 03:11:40 +00004034
Chandler Carruthffbf7052015-03-26 22:27:09 +00004035 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00004036 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00004037 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00004038
Chandler Carruthffbf7052015-03-26 22:27:09 +00004039 // First we try the easy case by forming the current context's constructor
4040 // name and adding that name first. This is a very useful optimization to
4041 // avoid walking the lexical declarations in many cases, and it also
4042 // handles the only case where a constructor name can come from some other
4043 // lexical context -- when that name is an implicit constructor merged from
4044 // another declaration in the redecl chain. Any non-implicit constructor or
4045 // conversion function which doesn't occur in all the lexical contexts
4046 // would be an ODR violation.
4047 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
4048 Context->getCanonicalType(Context->getRecordType(D)));
4049 if (ConstructorNameSet.erase(ImplicitCtorName))
4050 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00004051
Chandler Carruthffbf7052015-03-26 22:27:09 +00004052 // If we still have constructors or conversion functions, we walk all the
4053 // names in the decl and add the constructors and conversion functions
4054 // which are visible in the order they lexically occur within the context.
4055 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
4056 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
4057 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
4058 auto Name = ChildND->getDeclName();
4059 switch (Name.getNameKind()) {
4060 default:
4061 continue;
4062
4063 case DeclarationName::CXXConstructorName:
4064 if (ConstructorNameSet.erase(Name))
4065 Names.push_back(Name);
4066 break;
4067
4068 case DeclarationName::CXXConversionFunctionName:
4069 if (ConversionNameSet.erase(Name))
4070 Names.push_back(Name);
4071 break;
4072 }
4073
4074 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
4075 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00004076 }
4077
Chandler Carruthe972c362015-03-26 03:11:40 +00004078 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
4079 "constructors by walking all the "
4080 "lexical members of the context.");
4081 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
4082 "conversion functions by walking all "
4083 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00004084 }
4085
Chandler Carruthe972c362015-03-26 03:11:40 +00004086 // Next we need to do a lookup with each name into this decl context to fully
4087 // populate any results from external sources. We don't actually use the
4088 // results of these lookups because we only want to use the results after all
4089 // results have been loaded and the pointers into them will be stable.
4090 for (auto &Name : Names)
4091 DC->lookup(Name);
4092
4093 // Now we need to insert the results for each name into the hash table. For
4094 // constructor names and conversion function names, we actually need to merge
4095 // all of the results for them into one list of results each and insert
4096 // those.
4097 SmallVector<NamedDecl *, 8> ConstructorDecls;
4098 SmallVector<NamedDecl *, 8> ConversionDecls;
4099
4100 // Now loop over the names, either inserting them or appending for the two
4101 // special cases.
4102 for (auto &Name : Names) {
4103 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4104
4105 switch (Name.getNameKind()) {
4106 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00004107 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004108 break;
4109
4110 case DeclarationName::CXXConstructorName:
4111 ConstructorDecls.append(Result.begin(), Result.end());
4112 break;
4113
4114 case DeclarationName::CXXConversionFunctionName:
4115 ConversionDecls.append(Result.begin(), Result.end());
4116 break;
4117 }
4118 }
4119
4120 // Handle our two special cases if we ended up having any. We arbitrarily use
4121 // the first declaration's name here because the name itself isn't part of
4122 // the key, only the kind of name is used.
4123 if (!ConstructorDecls.empty())
4124 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004125 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004126 if (!ConversionDecls.empty())
4127 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004128 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004129
Richard Smithd88a7f12015-09-01 20:35:42 +00004130 // Create the on-disk hash table. Also emit the existing imported and
4131 // merged table if there is one.
4132 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4133 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00004134}
4135
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004136/// Write the block containing all of the declaration IDs
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004137/// visible from the given DeclContext.
4138///
4139/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00004140/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004141uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4142 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00004143 // If we imported a key declaration of this namespace, write the visible
4144 // lookup results as an update record for it rather than including them
4145 // on this declaration. We will only look at key declarations on reload.
4146 if (isa<NamespaceDecl>(DC) && Chain &&
4147 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4148 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004149 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00004150 Prev = Prev->getPreviousDecl())
4151 if (!Prev->isFromASTFile())
4152 return 0;
4153
4154 // Note that we need to emit an update record for the primary context.
4155 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4156
4157 // Make sure all visible decls are written. They will be recorded later. We
4158 // do this using a side data structure so we can sort the names into
4159 // a deterministic order.
4160 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4161 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4162 LookupResults;
4163 if (Map) {
4164 LookupResults.reserve(Map->size());
4165 for (auto &Entry : *Map)
4166 LookupResults.push_back(
4167 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4168 }
4169
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004170 llvm::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
Richard Smith5fc18a92015-07-12 23:43:21 +00004171 for (auto &NameAndResult : LookupResults) {
4172 DeclarationName Name = NameAndResult.first;
4173 DeclContext::lookup_result Result = NameAndResult.second;
4174 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4175 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4176 // We have to work around a name lookup bug here where negative lookup
4177 // results for these names get cached in namespace lookup tables (these
4178 // names should never be looked up in a namespace).
4179 assert(Result.empty() && "Cannot have a constructor or conversion "
4180 "function name in a namespace!");
4181 continue;
4182 }
4183
4184 for (NamedDecl *ND : Result)
4185 if (!ND->isFromASTFile())
4186 GetDeclRef(ND);
4187 }
4188
4189 return 0;
4190 }
4191
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004192 if (DC->getPrimaryContext() != DC)
4193 return 0;
4194
Chandler Carruthe972c362015-03-26 03:11:40 +00004195 // Skip contexts which don't support name lookup.
4196 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004197 return 0;
4198
4199 // If not in C++, we perform name lookup for the translation unit via the
4200 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004201 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004202 return 0;
4203
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004204 // Serialize the contents of the mapping used for lookup. Note that,
4205 // although we have two very different code paths, the serialized
4206 // representation is the same for both cases: a declaration name,
4207 // followed by a size, followed by references to the visible
4208 // declarations that have that name.
4209 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00004210 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004211 if (!Map || Map->empty())
4212 return 0;
4213
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004214 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004215 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004216 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004217
4218 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004219 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004220 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00004221 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004222 ++NumVisibleDeclContexts;
4223 return Offset;
4224}
4225
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004226/// Write an UPDATE_VISIBLE block for the given context.
Sebastian Redla4071b42010-08-24 00:50:09 +00004227///
4228/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4229/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00004230/// (in C++), for namespaces, and for classes with forward-declared unscoped
4231/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00004232void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00004233 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00004234 if (!Map || Map->empty())
4235 return;
4236
Sebastian Redla4071b42010-08-24 00:50:09 +00004237 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004238 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004239 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004240
Richard Smith5fc18a92015-07-12 23:43:21 +00004241 // If we're updating a namespace, select a key declaration as the key for the
4242 // update record; those are the only ones that will be checked on reload.
4243 if (isa<NamespaceDecl>(DC))
4244 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4245
Sebastian Redla4071b42010-08-24 00:50:09 +00004246 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004247 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004248 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004249}
4250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004251/// Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004252void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004253 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004254 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4255}
4256
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004257/// Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004258void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004259 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004260 return;
4261
4262 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4263 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004264 for (const auto &I:Opts.OptMap) {
4265 AddString(I.getKey(), Record);
4266 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004267 Record.push_back(V.Supported ? 1 : 0);
4268 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004269 Record.push_back(V.Avail);
4270 Record.push_back(V.Core);
4271 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004272 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4273}
4274
Yaxun Liu5b746652016-12-18 05:18:55 +00004275void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4276 if (!SemaRef.Context.getLangOpts().OpenCL)
4277 return;
4278
4279 RecordData Record;
4280 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4281 Record.push_back(
4282 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4283 Record.push_back(I.second.size());
4284 for (auto Ext : I.second)
4285 AddString(Ext, Record);
4286 }
4287 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4288}
4289
4290void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4291 if (!SemaRef.Context.getLangOpts().OpenCL)
4292 return;
4293
4294 RecordData Record;
4295 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4296 Record.push_back(getDeclID(I.first));
4297 Record.push_back(static_cast<unsigned>(I.second.size()));
4298 for (auto Ext : I.second)
4299 AddString(Ext, Record);
4300 }
4301 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4302}
4303
Justin Lebar67a78a62016-10-08 22:15:58 +00004304void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4305 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4306 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4307 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4308 }
4309}
4310
Douglas Gregor404cdde2012-01-27 01:47:08 +00004311void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004312 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004313 RecordData Categories;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004314
Douglas Gregor404cdde2012-01-27 01:47:08 +00004315 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4316 unsigned Size = 0;
4317 unsigned StartIndex = Categories.size();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004318
Douglas Gregor404cdde2012-01-27 01:47:08 +00004319 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004320
Douglas Gregor404cdde2012-01-27 01:47:08 +00004321 // Allocate space for the size.
4322 Categories.push_back(0);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004323
Douglas Gregor404cdde2012-01-27 01:47:08 +00004324 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004325 for (ObjCInterfaceDecl::known_categories_iterator
4326 Cat = Class->known_categories_begin(),
4327 CatEnd = Class->known_categories_end();
4328 Cat != CatEnd; ++Cat, ++Size) {
4329 assert(getDeclID(*Cat) != 0 && "Bogus category");
4330 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004331 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004332
Douglas Gregor404cdde2012-01-27 01:47:08 +00004333 // Update the size.
4334 Categories[StartIndex] = Size;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004335
Douglas Gregor404cdde2012-01-27 01:47:08 +00004336 // Record this interface -> category map.
4337 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4338 CategoriesMap.push_back(CatInfo);
4339 }
4340
4341 // Sort the categories map by the definition ID, since the reader will be
4342 // performing binary searches on this information.
4343 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4344
4345 // Emit the categories map.
4346 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004347
David Blaikieb44f0bf2017-01-04 22:36:43 +00004348 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004349 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004352 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004353
4354 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4355 Stream.EmitRecordWithBlob(AbbrevID, Record,
4356 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004357 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004358
Douglas Gregor404cdde2012-01-27 01:47:08 +00004359 // Emit the category lists.
4360 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4361}
4362
Richard Smithe40f2ba2013-08-07 21:41:30 +00004363void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4364 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4365
4366 if (LPTMap.empty())
4367 return;
4368
4369 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004370 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004371 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004372 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004373 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004374 AddDeclRef(LPT.D, Record);
4375 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004376
Justin Lebar28f09c52016-10-10 16:26:08 +00004377 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004378 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004379 }
4380 }
4381 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4382}
4383
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004384/// Write the state of 'pragma clang optimize' at the end of the module.
Dario Domizioli13a0a382014-05-23 12:13:25 +00004385void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4386 RecordData Record;
4387 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4388 AddSourceLocation(PragmaLoc, Record);
4389 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4390}
4391
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004392/// Write the state of 'pragma ms_struct' at the end of the module.
Nico Weber779355f2016-03-02 23:22:00 +00004393void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4394 RecordData Record;
4395 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4396 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4397}
4398
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004399/// Write the state of 'pragma pointers_to_members' at the end of the
Nico Weber42932312016-03-03 00:17:35 +00004400//module.
4401void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4402 RecordData Record;
4403 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4404 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4405 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4406}
4407
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004408/// Write the state of 'pragma pack' at the end of the module.
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004409void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004410 // Don't serialize pragma pack state for modules, since it should only take
4411 // effect on a per-submodule basis.
4412 if (WritingModule)
4413 return;
4414
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004415 RecordData Record;
4416 Record.push_back(SemaRef.PackStack.CurrentValue);
4417 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4418 Record.push_back(SemaRef.PackStack.Stack.size());
4419 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4420 Record.push_back(StackEntry.Value);
4421 AddSourceLocation(StackEntry.PragmaLocation, Record);
Alex Lorenz45b40142017-07-28 14:41:21 +00004422 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004423 AddString(StackEntry.StackSlotLabel, Record);
4424 }
4425 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4426}
4427
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004428void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4429 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004430 // Enter the extension block.
4431 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4432
4433 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004434 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004435 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4436 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4437 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4438 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4439 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4440 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004441 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004442
4443 // Emit the metadata record.
4444 RecordData Record;
4445 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4446 Record.push_back(EXTENSION_METADATA);
4447 Record.push_back(Metadata.MajorVersion);
4448 Record.push_back(Metadata.MinorVersion);
4449 Record.push_back(Metadata.BlockName.size());
4450 Record.push_back(Metadata.UserInfo.size());
4451 SmallString<64> Buffer;
4452 Buffer += Metadata.BlockName;
4453 Buffer += Metadata.UserInfo;
4454 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4455
4456 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004457 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004458
4459 // Exit the extension block.
4460 Stream.ExitBlock();
4461}
4462
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004463//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004464// General Serialization Routines
4465//===----------------------------------------------------------------------===//
4466
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004467/// Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004468void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4469 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004470 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004471 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004472 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004473 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004474
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004475#include "clang/Serialization/AttrPCHWrite.inc"
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004476 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004477}
4478
John McCallf413f5e2013-05-03 00:10:13 +00004479void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4480 AddSourceLocation(Tok.getLocation(), Record);
4481 Record.push_back(Tok.getLength());
4482
4483 // FIXME: When reading literal tokens, reconstruct the literal pointer
4484 // if it is needed.
4485 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4486 // FIXME: Should translate token kind to a stable encoding.
4487 Record.push_back(Tok.getKind());
4488 // FIXME: Should translate token flags to a stable encoding.
4489 Record.push_back(Tok.getFlags());
4490}
4491
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004492void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004493 Record.push_back(Str.size());
4494 Record.insert(Record.end(), Str.begin(), Str.end());
4495}
4496
Richard Smith7ed1bc92014-12-05 22:42:13 +00004497bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004498 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004499
Richard Smith54cc3c22014-12-11 20:50:24 +00004500 bool Changed =
4501 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004502
4503 // Remove a prefix to make the path relative, if relevant.
4504 const char *PathBegin = Path.data();
4505 const char *PathPtr =
4506 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4507 if (PathPtr != PathBegin) {
4508 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4509 Changed = true;
4510 }
4511
4512 return Changed;
4513}
4514
4515void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4516 SmallString<128> FilePath(Path);
4517 PreparePathForOutput(FilePath);
4518 AddString(FilePath, Record);
4519}
4520
Mehdi Amini57a41912015-09-10 01:46:39 +00004521void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004522 StringRef Path) {
4523 SmallString<128> FilePath(Path);
4524 PreparePathForOutput(FilePath);
4525 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4526}
4527
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004528void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4529 RecordDataImpl &Record) {
4530 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004531 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004532 Record.push_back(*Minor + 1);
4533 else
4534 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004535 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004536 Record.push_back(*Subminor + 1);
4537 else
4538 Record.push_back(0);
4539}
4540
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004541/// Note that the identifier II occurs at the given offset
Douglas Gregore84a9da2009-04-20 20:36:09 +00004542/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004543void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004544 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004545 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004546 // up earlier in the chain and thus don't need an offset.
4547 if (ID >= FirstIdentID)
4548 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004549}
4550
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004551/// Note that the selector Sel occurs at the given offset
Douglas Gregor95c13f52009-04-25 17:48:32 +00004552/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004553void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004554 unsigned ID = SelectorIDs[Sel];
4555 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004556 // Don't record offsets for selectors that are also available in a different
4557 // file.
4558 if (ID < FirstSelectorID)
4559 return;
4560 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004561}
4562
David Blaikie61137e12017-01-05 18:23:18 +00004563ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004564 SmallVectorImpl<char> &Buffer, MemoryBufferCache &PCMCache,
David Blaikie61137e12017-01-05 18:23:18 +00004565 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4566 bool IncludeTimestamps)
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004567 : Stream(Stream), Buffer(Buffer), PCMCache(PCMCache),
4568 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004569 for (const auto &Ext : Extensions) {
4570 if (auto Writer = Ext->createExtensionWriter(*this))
4571 ModuleFileExtensionWriters.push_back(std::move(Writer));
4572 }
4573}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004574
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004575ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004576 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004577}
4578
Richard Smithb3761912015-02-05 23:08:52 +00004579const LangOptions &ASTWriter::getLangOpts() const {
4580 assert(WritingAST && "can't determine lang opts when not writing AST");
4581 return Context->getLangOpts();
4582}
4583
Richard Smithe75ee0f2015-08-17 07:13:32 +00004584time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4585 return IncludeTimestamps ? E->getModificationTime() : 0;
4586}
4587
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004588ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4589 const std::string &OutputFile,
4590 Module *WritingModule, StringRef isysroot,
4591 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004592 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004593
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004594 ASTHasCompilerErrors = hasErrors;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004595
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004596 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004597 Stream.Emit((unsigned)'C', 8);
4598 Stream.Emit((unsigned)'P', 8);
4599 Stream.Emit((unsigned)'C', 8);
4600 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004601
Chris Lattner28fa4e62009-04-26 22:26:21 +00004602 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004603
Douglas Gregoreda8e122011-08-09 15:13:55 +00004604 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004605 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004606 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004607 ASTFileSignature Signature =
4608 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004609 Context = nullptr;
4610 PP = nullptr;
4611 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004612 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004613
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004614 WritingAST = false;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004615 if (SemaRef.Context.getLangOpts().ImplicitModules && WritingModule) {
4616 // Construct MemoryBuffer and update buffer manager.
4617 PCMCache.addBuffer(OutputFile,
4618 llvm::MemoryBuffer::getMemBufferCopy(
4619 StringRef(Buffer.begin(), Buffer.size())));
4620 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004621 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004622}
4623
Douglas Gregora94a1542011-07-27 21:45:57 +00004624template<typename Vector>
4625static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4626 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004627 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4628 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004629 Writer.AddDeclRef(*I, Record);
4630 }
4631}
4632
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004633ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4634 const std::string &OutputFile,
4635 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004636 using namespace llvm;
4637
Craig Toppera13603a2014-05-22 05:54:18 +00004638 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004639
Douglas Gregorcf68c582011-12-01 22:20:10 +00004640 // Make sure that the AST reader knows to finalize itself.
4641 if (Chain)
4642 Chain->finalizeForWriting();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004643
Sebastian Redl143413f2010-07-12 22:02:52 +00004644 ASTContext &Context = SemaRef.Context;
4645 Preprocessor &PP = SemaRef.PP;
4646
Douglas Gregordab42432011-08-12 00:15:20 +00004647 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004648 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4649 if (D) {
4650 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4651 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004652 }
4653 };
4654 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4655 PREDEF_DECL_TRANSLATION_UNIT_ID);
4656 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4657 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4658 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4659 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4660 PREDEF_DECL_OBJC_PROTOCOL_ID);
4661 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4662 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4663 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4664 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4665 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004666 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004667 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4668 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004669 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004670 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4671 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004672 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4673 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004674 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4675 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004676 RegisterPredefDecl(Context.TypePackElementDecl,
4677 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004678
Chris Lattner0c797362009-09-08 18:19:27 +00004679 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004680 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004681 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004682 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004683 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004684
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004685 // Build a record containing all of the file scoped decls in this file.
4686 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004687 if (!isModule)
4688 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4689 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004690
Douglas Gregor851443c2011-08-12 01:39:19 +00004691 // Build a record containing all of the delegating constructors we still need
4692 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004693 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004694 if (!isModule)
4695 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004696
Douglas Gregor851443c2011-08-12 01:39:19 +00004697 // Write the set of weak, undeclared identifiers. We always write the
4698 // entire table, since later PCH files in a PCH chain are only interested in
4699 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004700 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004701 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4702 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4703 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4704 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4705 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4706 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4707 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004708 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004709
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004710 // Build a record containing all of the ext_vector declarations.
4711 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004712 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004713
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004714 // Build a record containing all of the VTable uses information.
4715 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004716 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004717 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4718 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4719 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4720 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4721 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004722 }
4723
Nico Weber72889432014-09-06 01:25:55 +00004724 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4725 RecordData UnusedLocalTypedefNameCandidates;
4726 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4727 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4728
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004729 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004730 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004731 for (const auto &I : SemaRef.PendingInstantiations) {
4732 AddDeclRef(I.first, PendingInstantiations);
4733 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004734 }
4735 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4736 "There are local ones at end of translation unit!");
4737
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004738 // Build a record containing some declaration references.
4739 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004740 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004741 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4742 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004743 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004744 }
4745
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004746 RecordData CUDASpecialDeclRefs;
4747 if (Context.getcudaConfigureCallDecl()) {
4748 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4749 }
4750
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004751 // Build a record containing all of the known namespaces.
4752 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004753 for (const auto &I : SemaRef.KnownNamespaces) {
4754 if (!I.second)
4755 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004756 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004757
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004758 // Build a record of all used, undefined objects that require definitions.
4759 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004760
4761 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004762 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004763 for (const auto &I : Undefined) {
4764 AddDeclRef(I.first, UndefinedButUsed);
4765 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004766 }
4767
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004768 // Build a record containing all delete-expressions that we would like to
4769 // analyze later in AST.
4770 RecordData DeleteExprsToAnalyze;
4771
Richard Smithf5262c62018-06-28 01:57:04 +00004772 if (!isModule) {
4773 for (const auto &DeleteExprsInfo :
4774 SemaRef.getMismatchingDeleteExpressions()) {
4775 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4776 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4777 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4778 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4779 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4780 }
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004781 }
4782 }
4783
Douglas Gregor112b9072012-10-18 05:31:06 +00004784 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004785 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004786
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004787 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004788 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004789
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004790 // This is so that older clang versions, before the introduction
4791 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004792 {
4793 RecordData Record = {VERSION_MAJOR};
4794 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4795 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004796
Douglas Gregor851443c2011-08-12 01:39:19 +00004797 // Create a lexical update block containing all of the declarations in the
4798 // translation unit that do not come from other AST files.
4799 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004800 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4801 for (const auto *D : TU->noload_decls()) {
4802 if (!D->isFromASTFile()) {
4803 NewGlobalKindDeclPairs.push_back(D->getKind());
4804 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4805 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004806 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004807
David Blaikieb44f0bf2017-01-04 22:36:43 +00004808 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004809 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4810 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004811 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004812 {
4813 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4814 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4815 bytes(NewGlobalKindDeclPairs));
4816 }
4817
Douglas Gregor851443c2011-08-12 01:39:19 +00004818 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004819 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004820 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4821 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004822 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004823 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004824 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004825
4826 // If we have any extern "C" names, write out a visible update for them.
4827 if (Context.ExternCContext)
4828 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004829
Douglas Gregor851443c2011-08-12 01:39:19 +00004830 // If the translation unit has an anonymous namespace, and we don't already
4831 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004832 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004833 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4834 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004835 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004836 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004837 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004838
Richard Smith5652c0f2014-03-21 01:48:23 +00004839 // Add update records for all mangling numbers and static local numbers.
4840 // These aren't really update records, but this is a convenient way of
4841 // tagging this rare extra data onto the declarations.
4842 for (const auto &Number : Context.MangleNumbers)
4843 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004844 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4845 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004846 for (const auto &Number : Context.StaticLocalNumbers)
4847 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004848 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4849 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004850
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004851 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004852 // an AST file, are registered for serialization. Likewise for template
4853 // specializations added to imported templates.
4854 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004855 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004856 }
4857
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004858 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004859 // serialization, if we're storing decls with identifiers.
4860 if (!WritingModule || !getLangOpts().CPlusPlus) {
4861 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004862 for (const auto &ID : PP.getIdentifierTable()) {
4863 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004864 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4865 IIs.push_back(II);
4866 }
4867 // Sort the identifiers to visit based on their name.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004868 llvm::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
Richard Smith79bf9202015-08-24 03:33:22 +00004869 for (const IdentifierInfo *II : IIs) {
4870 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4871 DEnd = SemaRef.IdResolver.end();
4872 D != DEnd; ++D) {
4873 GetDeclRef(*D);
4874 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004875 }
4876 }
4877
Manman Rena0f31a02016-04-29 19:04:05 +00004878 // For method pool in the module, if it contains an entry for a selector,
4879 // the entry should be complete, containing everything introduced by that
4880 // module and all modules it imports. It's possible that the entry is out of
4881 // date, so we need to pull in the new content here.
4882
4883 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4884 // safe, we copy all selectors out.
4885 llvm::SmallVector<Selector, 256> AllSelectors;
4886 for (auto &SelectorAndID : SelectorIDs)
4887 AllSelectors.push_back(SelectorAndID.first);
4888 for (auto &Selector : AllSelectors)
4889 SemaRef.updateOutOfDateSelector(Selector);
4890
Douglas Gregor5204bde2011-08-02 16:26:37 +00004891 // Form the record of special types.
4892 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004893 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004894 AddTypeRef(Context.getFILEType(), SpecialTypes);
4895 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4896 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4897 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4898 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004899 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004900 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004901
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004902 if (Chain) {
4903 // Write the mapping information describing our module dependencies and how
4904 // each of those modules were mapped into our own offset/ID space, so that
4905 // the reader can build the appropriate mapping to its own offset/ID space.
4906 // The map consists solely of a blob with the following format:
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004907 // *(module-kind:i8
4908 // module-name-len:i16 module-name:len*i8
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004909 // source-location-offset:i32
4910 // identifier-id:i32
4911 // preprocessed-entity-id:i32
4912 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004913 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004914 // selector-id:i32
4915 // declaration-id:i32
4916 // c++-base-specifiers-id:i32
4917 // type-id:i32)
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00004918 //
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004919 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule or
4920 // MK_ExplicitModule, then the module-name is the module name. Otherwise,
4921 // it is the module file name.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004922 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004923 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004925 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004926 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004927 {
4928 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004929 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004930 using namespace llvm::support;
Eugene Zelenkob7d89102017-11-11 00:08:50 +00004931
Peter Collingbournee3f65292018-05-18 19:46:24 +00004932 endian::Writer LE(Out, little);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004933 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
4934 StringRef Name =
4935 M.Kind == MK_PrebuiltModule || M.Kind == MK_ExplicitModule
4936 ? M.ModuleName
4937 : M.FileName;
4938 LE.write<uint16_t>(Name.size());
4939 Out.write(Name.data(), Name.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004940
Ben Langmuir785180e2014-10-20 16:27:30 +00004941 // Note: if a base ID was uint max, it would not be possible to load
4942 // another module after it or have more than one entity inside it.
4943 uint32_t None = std::numeric_limits<uint32_t>::max();
4944
4945 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4946 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4947 if (ShouldWrite)
4948 LE.write<uint32_t>(BaseID);
4949 else
4950 LE.write<uint32_t>(None);
4951 };
4952
Ben Langmuirfe971d92014-08-16 04:54:18 +00004953 // These values should be unique within a chain, since they will be read
4954 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004955 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4956 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4957 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4958 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4959 M.NumPreprocessedEntities);
4960 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4961 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4962 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4963 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004964 }
4965 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004966 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004967 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4968 Buffer.data(), Buffer.size());
4969 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004970
Richard Smithb9eab6d2014-03-20 19:44:17 +00004971 RecordData DeclUpdatesOffsetsRecord;
4972
Richard Smith59442b42014-03-20 20:07:19 +00004973 // Keep writing types, declarations, and declaration update records
4974 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004975 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4976 WriteTypeAbbrevs();
4977 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004978 do {
4979 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4980 while (!DeclTypesToEmit.empty()) {
4981 DeclOrType DOT = DeclTypesToEmit.front();
4982 DeclTypesToEmit.pop();
4983 if (DOT.isType())
4984 WriteType(DOT.getType());
4985 else
4986 WriteDecl(Context, DOT.getDecl());
4987 }
4988 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004989 Stream.ExitBlock();
4990
Richard Smithb9eab6d2014-03-20 19:44:17 +00004991 DoneWritingDeclsAndTypes = true;
4992
4993 // These things can only be done once we've written out decls and types.
4994 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004995 if (!DeclUpdatesOffsetsRecord.empty())
4996 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004997 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004998 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004999 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005000 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005001 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00005002 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00005003 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00005004 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00005005 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00005006 WriteFPPragmaOptions(SemaRef.getFPOptions());
5007 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00005008 WriteOpenCLExtensionTypes(SemaRef);
5009 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00005010 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00005011
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005012 // If we're emitting a module, write out the submodule information.
Douglas Gregora89c5ac2011-12-06 01:10:29 +00005013 if (WritingModule)
5014 WriteSubmodules(WritingModule);
5015
Douglas Gregor5204bde2011-08-02 16:26:37 +00005016 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
5017
Douglas Gregord4df8652009-04-22 22:02:47 +00005018 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00005019 if (!EagerlyDeserializedDecls.empty())
5020 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00005021
David Blaikief63556d2017-04-12 20:58:33 +00005022 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00005023 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
5024
Douglas Gregord4df8652009-04-22 22:02:47 +00005025 // Write the record containing tentative definitions.
5026 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005027 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00005028
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00005029 // Write the record containing unused file scoped decls.
5030 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005031 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005032
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005033 // Write the record containing weak undeclared identifiers.
5034 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005035 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00005036 WeakUndeclaredIdentifiers);
5037
Douglas Gregor61cac2b2009-04-27 20:06:05 +00005038 // Write the record containing ext_vector type names.
5039 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005040 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00005041
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005042 // Write the record containing VTable uses information.
5043 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005044 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00005045
Nico Weber72889432014-09-06 01:25:55 +00005046 // Write the record containing potentially unused local typedefs.
5047 if (!UnusedLocalTypedefNameCandidates.empty())
5048 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
5049 UnusedLocalTypedefNameCandidates);
5050
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005051 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00005052 if (!PendingInstantiations.empty())
5053 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00005054
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005055 // Write the record containing declaration references of Sema.
5056 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00005057 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005058
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005059 // Write the record containing CUDA-specific declaration references.
5060 if (!CUDASpecialDeclRefs.empty())
5061 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005062
Alexis Hunt27a761d2011-05-04 23:29:54 +00005063 // Write the delegating constructors.
5064 if (!DelegatingCtorDecls.empty())
5065 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00005066
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005067 // Write the known namespaces.
5068 if (!KnownNamespaces.empty())
5069 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00005070
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00005071 // Write the undefined internal functions and variables, and inline functions.
5072 if (!UndefinedButUsed.empty())
5073 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00005074
5075 if (!DeleteExprsToAnalyze.empty())
5076 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
5077
Douglas Gregor851443c2011-08-12 01:39:19 +00005078 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00005079 for (auto *DC : UpdatedDeclContexts)
5080 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00005081
Douglas Gregor959bb062011-12-03 01:15:29 +00005082 if (!WritingModule) {
5083 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00005084 struct ModuleInfo {
5085 uint64_t ID;
5086 Module *M;
5087 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
5088 };
Richard Smith56be7542014-03-21 00:33:59 +00005089 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00005090 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00005091 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005092 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5093 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00005094 }
Richard Smith56be7542014-03-21 00:33:59 +00005095
5096 if (!Imports.empty()) {
5097 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5098 return A.ID < B.ID;
5099 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005100 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5101 return A.ID == B.ID;
5102 };
Richard Smith56be7542014-03-21 00:33:59 +00005103
5104 // Sort and deduplicate module IDs.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00005105 llvm::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005106 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00005107 Imports.end());
5108
5109 RecordData ImportedModules;
5110 for (const auto &Import : Imports) {
5111 ImportedModules.push_back(Import.ID);
5112 // FIXME: If the module has macros imported then later has declarations
5113 // imported, this location won't be the right one as a location for the
5114 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00005115 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00005116 }
5117
Douglas Gregor959bb062011-12-03 01:15:29 +00005118 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5119 }
Douglas Gregor0a839132011-12-03 00:59:55 +00005120 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005121
Douglas Gregor404cdde2012-01-27 01:47:08 +00005122 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00005123 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00005124 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005125 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00005126 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005127 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00005128 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00005129
Douglas Gregor08f01292009-04-17 22:13:46 +00005130 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00005131 RecordData::value_type Record[] = {
5132 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00005133 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00005134 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00005135
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005136 // Write the module file extension blocks.
5137 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00005138 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005139
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005140 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005141}
5142
Richard Smithb9eab6d2014-03-20 19:44:17 +00005143void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005144 if (DeclUpdates.empty())
5145 return;
5146
Richard Smith59442b42014-03-20 20:07:19 +00005147 DeclUpdateMap LocalUpdates;
5148 LocalUpdates.swap(DeclUpdates);
5149
Richard Smith6ef42932014-03-20 21:02:00 +00005150 for (auto &DeclUpdate : LocalUpdates) {
5151 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00005152
Richard Smithd28ac5b2014-03-22 23:33:22 +00005153 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00005154 RecordData RecordData;
5155 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00005156 for (auto &Update : DeclUpdate.second) {
5157 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5158
Richard Smith69c82bf2016-04-01 22:52:03 +00005159 // An updated body is emitted last, so that the reader doesn't need
5160 // to skip over the lazy body to reach statements for other records.
5161 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5162 HasUpdatedBody = true;
5163 else
5164 Record.push_back(Kind);
5165
Richard Smith6ef42932014-03-20 21:02:00 +00005166 switch (Kind) {
5167 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5168 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5169 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00005170 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00005171 Record.push_back(GetDeclRef(Update.getDecl()));
5172 break;
5173
Richard Smith4d235792014-08-07 18:53:08 +00005174 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00005175 break;
5176
Richard Smith891fc7f2017-12-05 01:31:47 +00005177 case UPD_CXX_POINT_OF_INSTANTIATION:
5178 // FIXME: Do we need to also save the template specialization kind here?
Richard Smith69c82bf2016-04-01 22:52:03 +00005179 Record.AddSourceLocation(Update.getLoc());
Richard Smith891fc7f2017-12-05 01:31:47 +00005180 break;
5181
5182 case UPD_CXX_ADDED_VAR_DEFINITION: {
5183 const VarDecl *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00005184 Record.push_back(VD->isInline());
5185 Record.push_back(VD->isInlineSpecified());
Richard Smith05a21352017-06-22 22:18:46 +00005186 if (VD->getInit()) {
5187 Record.push_back(!VD->isInitKnownICE() ? 1
5188 : (VD->isInitICE() ? 3 : 2));
5189 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
5190 } else {
5191 Record.push_back(0);
5192 }
Richard Smith4d235792014-08-07 18:53:08 +00005193 break;
Richard Smith05a21352017-06-22 22:18:46 +00005194 }
Richard Smith4d235792014-08-07 18:53:08 +00005195
John McCall32791cc2016-01-06 22:34:54 +00005196 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00005197 Record.AddStmt(const_cast<Expr *>(
5198 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00005199 break;
5200
Richard Smith4b054b22016-08-24 21:25:37 +00005201 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5202 Record.AddStmt(
5203 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5204 break;
5205
Richard Smithcd45dbc2014-04-19 03:48:30 +00005206 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5207 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005208 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00005209 Record.push_back(RD->isParamDestroyedInCallee());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00005210 Record.push_back(RD->getArgPassingRestrictions());
Richard Smith69c82bf2016-04-01 22:52:03 +00005211 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00005212 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00005213 *Context, const_cast<CXXRecordDecl *>(RD)));
5214
5215 // This state is sometimes updated by template instantiation, when we
5216 // switch from the specialization referring to the template declaration
5217 // to it referring to the template definition.
5218 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5219 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005220 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005221 } else {
5222 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5223 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005224 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00005225
5226 // The instantiation might have been resolved to a partial
5227 // specialization. If so, record which one.
5228 auto From = Spec->getInstantiatedFrom();
5229 if (auto PartialSpec =
5230 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5231 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00005232 Record.AddDeclRef(PartialSpec);
5233 Record.AddTemplateArgumentList(
5234 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00005235 } else {
5236 Record.push_back(false);
5237 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00005238 }
5239 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005240 Record.AddSourceLocation(RD->getLocation());
5241 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00005242 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005243
5244 // Instantiation may change attributes; write them all out afresh.
5245 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00005246 if (D->hasAttrs())
5247 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005248
5249 // FIXME: Ensure we don't get here for explicit instantiations.
5250 break;
5251 }
5252
Richard Smithf8134002015-03-10 01:41:22 +00005253 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005254 Record.AddDeclRef(Update.getDecl());
Richard Smith5b349582017-10-13 01:55:36 +00005255 Record.AddStmt(cast<CXXDestructorDecl>(D)->getOperatorDeleteThisArg());
Richard Smithf8134002015-03-10 01:41:22 +00005256 break;
5257
Richard Smith564417a2014-03-20 21:47:22 +00005258 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
5259 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00005260 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
5261 Record);
5262 break;
5263
Richard Smith6ef42932014-03-20 21:02:00 +00005264 case UPD_CXX_DEDUCED_RETURN_TYPE:
5265 Record.push_back(GetOrCreateTypeID(Update.getType()));
5266 break;
5267
5268 case UPD_DECL_MARKED_USED:
5269 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00005270
5271 case UPD_MANGLING_NUMBER:
5272 case UPD_STATIC_LOCAL_NUMBER:
5273 Record.push_back(Update.getNumber());
5274 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005275
Alexey Bataev97720002014-11-11 04:05:39 +00005276 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005277 Record.AddSourceRange(
5278 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005279 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005280
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005281 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5282 Record.AddSourceRange(
5283 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5284 break;
5285
Richard Smith65ebb4a2015-03-26 04:09:53 +00005286 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005287 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005288 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005289
5290 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005291 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005292 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005293 }
5294 }
5295
Richard Smithd28ac5b2014-03-22 23:33:22 +00005296 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005297 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005298 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005299 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005300 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005301 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005302 }
5303
Richard Smithcd45dbc2014-04-19 03:48:30 +00005304 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005305 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005306 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005307}
5308
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005309void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005310 uint32_t Raw = Loc.getRawEncoding();
5311 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005312}
5313
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005314void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005315 AddSourceLocation(Range.getBegin(), Record);
5316 AddSourceLocation(Range.getEnd(), Record);
5317}
5318
Richard Smithf144b542016-04-08 21:54:32 +00005319void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5320 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005321 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005322 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005323}
5324
Richard Smithf144b542016-04-08 21:54:32 +00005325void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5326 Record->push_back(Value.isUnsigned());
5327 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005328}
5329
Richard Smithf144b542016-04-08 21:54:32 +00005330void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5331 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005332}
5333
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005334void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005335 Record.push_back(getIdentifierRef(II));
5336}
5337
Sebastian Redl539c5062010-08-18 23:57:32 +00005338IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005339 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005340 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005341
Sebastian Redl539c5062010-08-18 23:57:32 +00005342 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005343 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005344 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005345 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005346}
5347
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005348MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005349 // Don't emit builtin macros like __LINE__ to the AST file unless they
5350 // have been redefined by the header (in which case they are not
5351 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005352 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005353 return 0;
5354
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005355 MacroID &ID = MacroIDs[MI];
5356 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005357 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005358 MacroInfoToEmitData Info = { Name, MI, ID };
5359 MacroInfosToEmit.push_back(Info);
5360 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005361 return ID;
5362}
5363
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005364MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005365 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005366 return 0;
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005367
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005368 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5369 return MacroIDs[MI];
5370}
5371
5372uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005373 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005374}
5375
Richard Smithe64e7452016-04-18 21:54:58 +00005376void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5377 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005378}
5379
Sebastian Redl539c5062010-08-18 23:57:32 +00005380SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005381 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005382 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005383 }
5384
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005385 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005386 if (SID == 0 && Chain) {
5387 // This might trigger a ReadSelector callback, which will set the ID for
5388 // this selector.
5389 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005390 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005391 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005392 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005393 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005394 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005395 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005396 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005397}
5398
Richard Smithe64e7452016-04-18 21:54:58 +00005399void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5400 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005401}
5402
Richard Smith290d8012016-04-06 17:06:00 +00005403void ASTRecordWriter::AddTemplateArgumentLocInfo(
5404 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005405 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005406 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005407 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005408 break;
5409 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005410 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005411 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005412 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005413 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5414 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005415 break;
5416 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005417 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5418 AddSourceLocation(Arg.getTemplateNameLoc());
5419 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005420 break;
John McCall0ad16662009-10-29 08:12:44 +00005421 case TemplateArgument::Null:
5422 case TemplateArgument::Integral:
5423 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005424 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005425 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005426 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005427 break;
5428 }
5429}
5430
Richard Smith290d8012016-04-06 17:06:00 +00005431void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5432 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005433
5434 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5435 bool InfoHasSameExpr
5436 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005437 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005438 if (InfoHasSameExpr)
5439 return; // Avoid storing the same expr twice.
5440 }
Richard Smith290d8012016-04-06 17:06:00 +00005441 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005442}
5443
Richard Smith290d8012016-04-06 17:06:00 +00005444void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005445 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005446 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005447 return;
5448 }
5449
Richard Smith290d8012016-04-06 17:06:00 +00005450 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005451}
5452
Richard Smith290d8012016-04-06 17:06:00 +00005453void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5454 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005455
Richard Smith290d8012016-04-06 17:06:00 +00005456 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005457 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005458 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005459}
5460
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005461void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005462 Record.push_back(GetOrCreateTypeID(T));
5463}
5464
Richard Smith2095ffe2015-03-16 20:11:03 +00005465TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005466 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005467 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5468 if (T.isNull())
5469 return TypeIdx();
5470 assert(!T.getLocalFastQualifiers());
5471
5472 TypeIdx &Idx = TypeIdxs[T];
5473 if (Idx.getIndex() == 0) {
5474 if (DoneWritingDeclsAndTypes) {
5475 assert(0 && "New type seen after serializing all the types to emit!");
5476 return TypeIdx();
5477 }
5478
5479 // We haven't seen this type before. Assign it a new ID and put it
5480 // into the queue of types to emit.
5481 Idx = TypeIdx(NextTypeID++);
5482 DeclTypesToEmit.push(T);
5483 }
5484 return Idx;
5485 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005486}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005487
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005488TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005489 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005490 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5491 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005492 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005493 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005494
Richard Smith2095ffe2015-03-16 20:11:03 +00005495 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5496 assert(I != TypeIdxs.end() && "Type not emitted!");
5497 return I->second;
5498 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005499}
5500
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005501void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005502 Record.push_back(GetDeclRef(D));
5503}
5504
Sebastian Redl539c5062010-08-18 23:57:32 +00005505DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005506 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005507
5508 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005509 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005510 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005511
Douglas Gregorb3163e52012-01-05 22:33:30 +00005512 // If D comes from an AST file, its declaration ID is already known and
5513 // fixed.
5514 if (D->isFromASTFile())
5515 return D->getGlobalID();
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005516
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005517 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005518 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005519 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005520 if (DoneWritingDeclsAndTypes) {
5521 assert(0 && "New decl seen after serializing all the decls to emit!");
5522 return 0;
5523 }
5524
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005525 // We haven't seen this declaration before. Give it a new ID and
5526 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005527 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005528 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005529 }
5530
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005531 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005532}
5533
Sebastian Redl539c5062010-08-18 23:57:32 +00005534DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005535 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005536 return 0;
5537
Douglas Gregorb3163e52012-01-05 22:33:30 +00005538 // If D comes from an AST file, its declaration ID is already known and
5539 // fixed.
5540 if (D->isFromASTFile())
5541 return D->getGlobalID();
5542
Douglas Gregore84a9da2009-04-20 20:36:09 +00005543 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5544 return DeclIDs[D];
5545}
5546
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005547void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005548 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005549 assert(D);
5550
5551 SourceLocation Loc = D->getLocation();
5552 if (Loc.isInvalid())
5553 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005554
5555 // We only keep track of the file-level declarations of each file.
5556 if (!D->getLexicalDeclContext()->isFileContext())
5557 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005558 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5559 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis71b74eb2018-01-26 19:26:12 +00005560 // TemplateTemplateParmDecls that are part of an alias template, should not
5561 // have TU as lexical context.
5562 if (isa<ParmVarDecl>(D) || isa<TemplateTemplateParmDecl>(D))
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005563 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005564
5565 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005566 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005567 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005568 FileID FID;
5569 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005570 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005571 if (FID.isInvalid())
5572 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005573 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005574
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005575 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005576 if (!Info)
5577 Info = new DeclIDInFileInfo();
5578
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005579 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005580 LocDeclIDsTy &Decls = Info->DeclIDs;
5581
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005582 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005583 Decls.push_back(LocDecl);
5584 return;
5585 }
5586
Benjamin Kramer45025c02013-08-24 13:22:59 +00005587 LocDeclIDsTy::iterator I =
5588 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005589
5590 Decls.insert(I, LocDecl);
5591}
5592
Richard Smithe64e7452016-04-18 21:54:58 +00005593void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005594 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005595 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005596 switch (Name.getNameKind()) {
5597 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005598 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005599 break;
5600
5601 case DeclarationName::ObjCZeroArgSelector:
5602 case DeclarationName::ObjCOneArgSelector:
5603 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005604 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005605 break;
5606
5607 case DeclarationName::CXXConstructorName:
5608 case DeclarationName::CXXDestructorName:
5609 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005610 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005611 break;
5612
Richard Smith35845152017-02-07 01:37:30 +00005613 case DeclarationName::CXXDeductionGuideName:
5614 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5615 break;
5616
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005617 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005618 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005619 break;
5620
Alexis Hunt3d221f22009-11-29 07:34:05 +00005621 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005622 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005623 break;
5624
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005625 case DeclarationName::CXXUsingDirective:
5626 // No extra data to emit
5627 break;
5628 }
5629}
Chris Lattnerca025db2010-05-07 21:43:38 +00005630
Richard Smithd08aeb62014-08-28 01:33:39 +00005631unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5632 assert(needsAnonymousDeclarationNumber(D) &&
5633 "expected an anonymous declaration");
5634
5635 // Number the anonymous declarations within this context, if we've not
5636 // already done so.
5637 auto It = AnonymousDeclarationNumbers.find(D);
5638 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005639 auto *DC = D->getLexicalDeclContext();
5640 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5641 AnonymousDeclarationNumbers[ND] = Number;
5642 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005643
5644 It = AnonymousDeclarationNumbers.find(D);
5645 assert(It != AnonymousDeclarationNumbers.end() &&
5646 "declaration not found within its lexical context");
5647 }
5648
5649 return It->second;
5650}
5651
Richard Smith290d8012016-04-06 17:06:00 +00005652void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5653 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005654 switch (Name.getNameKind()) {
5655 case DeclarationName::CXXConstructorName:
5656 case DeclarationName::CXXDestructorName:
5657 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005658 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005659 break;
5660
5661 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005662 AddSourceLocation(SourceLocation::getFromRawEncoding(
5663 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005664 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005665 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005666 break;
5667
5668 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005669 AddSourceLocation(SourceLocation::getFromRawEncoding(
5670 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005671 break;
5672
5673 case DeclarationName::Identifier:
5674 case DeclarationName::ObjCZeroArgSelector:
5675 case DeclarationName::ObjCOneArgSelector:
5676 case DeclarationName::ObjCMultiArgSelector:
5677 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005678 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005679 break;
5680 }
5681}
5682
Richard Smith290d8012016-04-06 17:06:00 +00005683void ASTRecordWriter::AddDeclarationNameInfo(
5684 const DeclarationNameInfo &NameInfo) {
5685 AddDeclarationName(NameInfo.getName());
5686 AddSourceLocation(NameInfo.getLoc());
5687 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005688}
5689
Richard Smith290d8012016-04-06 17:06:00 +00005690void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5691 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5692 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005693 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005694 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005695}
5696
Richard Smithe64e7452016-04-18 21:54:58 +00005697void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005698 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005699 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005700 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005701
5702 // Push each of the NNS's onto a stack for serialization in reverse order.
5703 while (NNS) {
5704 NestedNames.push_back(NNS);
5705 NNS = NNS->getPrefix();
5706 }
5707
Richard Smithe64e7452016-04-18 21:54:58 +00005708 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005709 while(!NestedNames.empty()) {
5710 NNS = NestedNames.pop_back_val();
5711 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005712 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005713 switch (Kind) {
5714 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005715 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005716 break;
5717
5718 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005719 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005720 break;
5721
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005722 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005723 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005724 break;
5725
Chris Lattnerca025db2010-05-07 21:43:38 +00005726 case NestedNameSpecifier::TypeSpec:
5727 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005728 AddTypeRef(QualType(NNS->getAsType(), 0));
5729 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005730 break;
5731
5732 case NestedNameSpecifier::Global:
5733 // Don't need to write an associated value.
5734 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005735
5736 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005737 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005738 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005739 }
5740 }
5741}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005742
Richard Smith290d8012016-04-06 17:06:00 +00005743void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005744 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005745 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005746 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005747
5748 // Push each of the nested-name-specifiers's onto a stack for
5749 // serialization in reverse order.
5750 while (NNS) {
5751 NestedNames.push_back(NNS);
5752 NNS = NNS.getPrefix();
5753 }
5754
Richard Smith290d8012016-04-06 17:06:00 +00005755 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005756 while(!NestedNames.empty()) {
5757 NNS = NestedNames.pop_back_val();
5758 NestedNameSpecifier::SpecifierKind Kind
5759 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005760 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005761 switch (Kind) {
5762 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005763 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5764 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005765 break;
5766
5767 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005768 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5769 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005770 break;
5771
5772 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005773 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5774 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005775 break;
5776
5777 case NestedNameSpecifier::TypeSpec:
5778 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005779 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5780 AddTypeLoc(NNS.getTypeLoc());
5781 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005782 break;
5783
5784 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005785 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005786 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005787
5788 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005789 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5790 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005791 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005792 }
5793 }
5794}
5795
Richard Smith290d8012016-04-06 17:06:00 +00005796void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005797 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005798 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005799 switch (Kind) {
5800 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005801 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005802 break;
5803
5804 case TemplateName::OverloadedTemplate: {
5805 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005806 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005807 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005808 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005809 break;
5810 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005811
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005812 case TemplateName::QualifiedTemplate: {
5813 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005814 AddNestedNameSpecifier(QualT->getQualifier());
5815 Record->push_back(QualT->hasTemplateKeyword());
5816 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005817 break;
5818 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005819
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005820 case TemplateName::DependentTemplate: {
5821 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005822 AddNestedNameSpecifier(DepT->getQualifier());
5823 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005824 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005825 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005826 else
Richard Smith290d8012016-04-06 17:06:00 +00005827 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005828 break;
5829 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005830
5831 case TemplateName::SubstTemplateTemplateParm: {
5832 SubstTemplateTemplateParmStorage *subst
5833 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005834 AddDeclRef(subst->getParameter());
5835 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005836 break;
5837 }
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005838
Douglas Gregor5590be02011-01-15 06:45:20 +00005839 case TemplateName::SubstTemplateTemplateParmPack: {
5840 SubstTemplateTemplateParmPackStorage *SubstPack
5841 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005842 AddDeclRef(SubstPack->getParameterPack());
5843 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005844 break;
5845 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005846 }
5847}
5848
Richard Smith290d8012016-04-06 17:06:00 +00005849void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5850 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005851 switch (Arg.getKind()) {
5852 case TemplateArgument::Null:
5853 break;
5854 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005855 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005856 break;
5857 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005858 AddDeclRef(Arg.getAsDecl());
5859 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005860 break;
5861 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005862 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005863 break;
5864 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005865 AddAPSInt(Arg.getAsIntegral());
5866 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005867 break;
5868 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005869 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005870 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005871 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005872 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005873 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005874 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005875 else
Richard Smith290d8012016-04-06 17:06:00 +00005876 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005877 break;
5878 case TemplateArgument::Expression:
5879 AddStmt(Arg.getAsExpr());
5880 break;
5881 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005882 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005883 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005884 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005885 break;
5886 }
5887}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005888
Richard Smithe64e7452016-04-18 21:54:58 +00005889void ASTRecordWriter::AddTemplateParameterList(
5890 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005891 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005892 AddSourceLocation(TemplateParams->getTemplateLoc());
5893 AddSourceLocation(TemplateParams->getLAngleLoc());
5894 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005895 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005896 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005897 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005898 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005899}
5900
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005901/// Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005902void ASTRecordWriter::AddTemplateArgumentList(
5903 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005904 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005905 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005906 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005907 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005908}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005909
Richard Smith290d8012016-04-06 17:06:00 +00005910void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5911 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005912 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005913 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5914 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5915 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005916 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005917 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005918 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005919}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005920
Richard Smithe64e7452016-04-18 21:54:58 +00005921void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5922 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005923 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005924 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005925 AddDeclRef(I.getDecl());
5926 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005927 }
5928}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005929
Richard Smith290d8012016-04-06 17:06:00 +00005930// FIXME: Move this out of the main ASTRecordWriter interface.
5931void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5932 Record->push_back(Base.isVirtual());
5933 Record->push_back(Base.isBaseOfClass());
5934 Record->push_back(Base.getAccessSpecifierAsWritten());
5935 Record->push_back(Base.getInheritConstructors());
5936 AddTypeSourceInfo(Base.getTypeSourceInfo());
5937 AddSourceRange(Base.getSourceRange());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00005938 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005939 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005940}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005941
Richard Smith645d2cf2016-04-14 00:29:55 +00005942static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5943 ArrayRef<CXXBaseSpecifier> Bases) {
5944 ASTWriter::RecordData Record;
5945 ASTRecordWriter Writer(W, Record);
5946 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005947
Richard Smith645d2cf2016-04-14 00:29:55 +00005948 for (auto &Base : Bases)
5949 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005950
Richard Smith645d2cf2016-04-14 00:29:55 +00005951 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005952}
5953
Richard Smith290d8012016-04-06 17:06:00 +00005954// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005955void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5956 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5957}
5958
Richard Smithaa165cf2016-04-13 21:57:08 +00005959static uint64_t
5960EmitCXXCtorInitializers(ASTWriter &W,
5961 ArrayRef<CXXCtorInitializer *> CtorInits) {
5962 ASTWriter::RecordData Record;
5963 ASTRecordWriter Writer(W, Record);
5964 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005965
Richard Smithaa165cf2016-04-13 21:57:08 +00005966 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005967 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005968 Writer.push_back(CTOR_INITIALIZER_BASE);
5969 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5970 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005971 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005972 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5973 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005974 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005975 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5976 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005977 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005978 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5979 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005980 }
Francois Pichetd583da02010-12-04 09:14:42 +00005981
Richard Smithaa165cf2016-04-13 21:57:08 +00005982 Writer.AddSourceLocation(Init->getMemberLocation());
5983 Writer.AddStmt(Init->getInit());
5984 Writer.AddSourceLocation(Init->getLParenLoc());
5985 Writer.AddSourceLocation(Init->getRParenLoc());
5986 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005987 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005988 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005989 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005990
5991 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005992}
5993
Richard Smithaa165cf2016-04-13 21:57:08 +00005994// FIXME: Move this out of the main ASTRecordWriter interface.
5995void ASTRecordWriter::AddCXXCtorInitializers(
5996 ArrayRef<CXXCtorInitializer *> CtorInits) {
5997 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005998}
5999
Richard Smith290d8012016-04-06 17:06:00 +00006000void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00006001 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00006002 Record->push_back(Data.IsLambda);
6003 Record->push_back(Data.UserDeclaredConstructor);
6004 Record->push_back(Data.UserDeclaredSpecialMembers);
6005 Record->push_back(Data.Aggregate);
6006 Record->push_back(Data.PlainOldData);
6007 Record->push_back(Data.Empty);
6008 Record->push_back(Data.Polymorphic);
6009 Record->push_back(Data.Abstract);
6010 Record->push_back(Data.IsStandardLayout);
Richard Smithb6070db2018-04-05 18:55:37 +00006011 Record->push_back(Data.IsCXX11StandardLayout);
6012 Record->push_back(Data.HasBasesWithFields);
6013 Record->push_back(Data.HasBasesWithNonStaticDataMembers);
Richard Smith290d8012016-04-06 17:06:00 +00006014 Record->push_back(Data.HasPrivateFields);
6015 Record->push_back(Data.HasProtectedFields);
6016 Record->push_back(Data.HasPublicFields);
6017 Record->push_back(Data.HasMutableFields);
6018 Record->push_back(Data.HasVariantMembers);
6019 Record->push_back(Data.HasOnlyCMembers);
6020 Record->push_back(Data.HasInClassInitializer);
6021 Record->push_back(Data.HasUninitializedReferenceMember);
6022 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00006023 Record->push_back(Data.HasInheritedConstructor);
6024 Record->push_back(Data.HasInheritedAssignment);
Richard Smith96cd6712017-08-16 01:49:53 +00006025 Record->push_back(Data.NeedOverloadResolutionForCopyConstructor);
Richard Smith290d8012016-04-06 17:06:00 +00006026 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
6027 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
6028 Record->push_back(Data.NeedOverloadResolutionForDestructor);
Richard Smith96cd6712017-08-16 01:49:53 +00006029 Record->push_back(Data.DefaultedCopyConstructorIsDeleted);
Richard Smith290d8012016-04-06 17:06:00 +00006030 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
6031 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
6032 Record->push_back(Data.DefaultedDestructorIsDeleted);
6033 Record->push_back(Data.HasTrivialSpecialMembers);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006034 Record->push_back(Data.HasTrivialSpecialMembersForCall);
Richard Smith290d8012016-04-06 17:06:00 +00006035 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006036 Record->push_back(Data.DeclaredNonTrivialSpecialMembersForCall);
Richard Smith290d8012016-04-06 17:06:00 +00006037 Record->push_back(Data.HasIrrelevantDestructor);
6038 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
6039 Record->push_back(Data.HasDefaultedDefaultConstructor);
6040 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
6041 Record->push_back(Data.HasConstexprDefaultConstructor);
6042 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
6043 Record->push_back(Data.ComputedVisibleConversions);
6044 Record->push_back(Data.UserProvidedDefaultConstructor);
6045 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00006046 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
6047 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00006048 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
6049 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
6050 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieufd1acbb2017-04-11 21:31:00 +00006051
6052 // getODRHash will compute the ODRHash if it has not been previously computed.
6053 Record->push_back(D->getODRHash());
David Blaikief63556d2017-04-12 20:58:33 +00006054 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
6055 Writer->WritingModule && !D->isDependentType();
6056 Record->push_back(ModulesDebugInfo);
6057 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00006058 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
6059
Richard Smith561fb152012-02-25 07:33:38 +00006060 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006061
Richard Smith290d8012016-04-06 17:06:00 +00006062 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006063 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006064 AddCXXBaseSpecifiers(Data.bases());
6065
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006066 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00006067 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00006068 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00006069 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006070
Richard Smith290d8012016-04-06 17:06:00 +00006071 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
6072 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006073 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00006074 AddDeclRef(D->getFirstFriend());
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006075
Douglas Gregor99ae8062012-02-14 17:54:36 +00006076 // Add lambda-specific data.
6077 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00006078 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00006079 Record->push_back(Lambda.Dependent);
6080 Record->push_back(Lambda.IsGenericLambda);
6081 Record->push_back(Lambda.CaptureDefault);
6082 Record->push_back(Lambda.NumCaptures);
6083 Record->push_back(Lambda.NumExplicitCaptures);
6084 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00006085 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00006086 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00006087 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00006088 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00006089 AddSourceLocation(Capture.getLocation());
6090 Record->push_back(Capture.isImplicit());
6091 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00006092 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00006093 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00006094 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00006095 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00006096 break;
6097 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00006098 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00006099 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00006100 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00006101 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00006102 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00006103 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00006104 break;
6105 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00006106 }
6107 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006108}
6109
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006110void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00006111 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00006112 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00006113 assert(FirstDeclID == NextDeclID &&
6114 FirstTypeID == NextTypeID &&
6115 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006116 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00006117 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00006118 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00006119 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00006120
Sebastian Redl07a89a82010-07-30 00:29:29 +00006121 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006122
Richard Smith7f330cd2015-03-18 01:42:29 +00006123 // Note, this will get called multiple times, once one the reader starts up
6124 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00006125 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6126 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6127 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006128 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00006129 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00006130 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006131 NextDeclID = FirstDeclID;
6132 NextTypeID = FirstTypeID;
6133 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006134 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006135 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00006136 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00006137}
6138
Sebastian Redl539c5062010-08-18 23:57:32 +00006139void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006140 // Always keep the highest ID. See \p TypeRead() for more information.
6141 IdentID &StoredID = IdentifierIDs[II];
6142 if (ID > StoredID)
6143 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00006144}
6145
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006146void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006147 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006148 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006149 if (ID > StoredID)
6150 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006151}
6152
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00006153void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006154 // Always take the highest-numbered type index. This copes with an interesting
6155 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006156 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006157 // keep the higher-numbered entry so that we can properly write it out to
6158 // the AST file.
6159 TypeIdx &StoredIdx = TypeIdxs[T];
6160 if (Idx.getIndex() >= StoredIdx.getIndex())
6161 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006162}
6163
Sebastian Redl539c5062010-08-18 23:57:32 +00006164void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006165 // Always keep the highest ID. See \p TypeRead() for more information.
6166 SelectorID &StoredID = SelectorIDs[S];
6167 if (ID > StoredID)
6168 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00006169}
Douglas Gregor91096292010-10-02 19:29:26 +00006170
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006171void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00006172 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006173 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00006174 MacroDefinitions[MD] = ID;
6175}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006176
Douglas Gregore37a85a2011-12-02 17:30:13 +00006177void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6178 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
6179 SubmoduleIDs[Mod] = ID;
6180}
6181
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006182void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006183 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00006184 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006185 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006186 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006187 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00006188 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006189 // A forward reference was mutated into a definition. Rewrite it.
6190 // FIXME: This happens during template instantiation, should we
6191 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00006192 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6193 "completed a tag from another module but not by instantiation?");
6194 DeclUpdates[RD].push_back(
6195 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006196 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006197 }
6198}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006199
Richard Smithf983f7f2015-10-13 00:23:25 +00006200static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6201 if (D->isFromASTFile())
6202 return true;
6203
Richard Smithf983f7f2015-10-13 00:23:25 +00006204 // The predefined __va_list_tag struct is imported if we imported any decls.
6205 // FIXME: This is a gross hack.
6206 return D == D->getASTContext().getVaListTagDecl();
6207}
6208
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006209void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006210 if (Chain && Chain->isProcessingUpdateRecords()) return;
6211 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00006212 "Should not add lookup results to non-lookup contexts!");
6213
Vassil Vassilev632eac32016-03-16 11:17:04 +00006214 // TU is handled elsewhere.
6215 if (isa<TranslationUnitDecl>(DC))
6216 return;
6217
6218 // Namespaces are handled elsewhere, except for template instantiations of
6219 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6220 // local instantiations are added to an imported context. Only happens when
6221 // adding ADL lookup candidates, for example templated friends.
6222 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6223 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006224 return;
6225
Richard Smithf983f7f2015-10-13 00:23:25 +00006226 // We're only interested in cases where a local declaration is added to an
6227 // imported context.
6228 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6229 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006230
Richard Smith0f4e2c42015-08-06 04:23:48 +00006231 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00006232 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00006233 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00006234 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6235 // We're adding a visible declaration to a predefined decl context. Ensure
6236 // that we write out all of its lookup results so we don't get a nasty
6237 // surprise when we try to emit its lookup table.
6238 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00006239 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00006240 }
Richard Smithc26d9742016-10-06 20:30:51 +00006241 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006242}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006243
6244void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006245 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006246 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00006247
6248 // We're only interested in cases where a local declaration is added to an
6249 // imported context.
6250 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6251 return;
6252
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006253 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00006254 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006255
6256 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00006257 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00006258 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00006259 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006260}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00006261
Richard Smith564417a2014-03-20 21:47:22 +00006262void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006263 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00006264 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6265 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006266 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006267 // If we don't already know the exception specification for this redecl
6268 // chain, add an update record for it.
6269 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6270 ->getType()
6271 ->castAs<FunctionProtoType>()
6272 ->getExceptionSpecType()))
6273 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6274 });
Richard Smith564417a2014-03-20 21:47:22 +00006275}
6276
Richard Smith1fa5d642013-05-11 05:45:24 +00006277void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006278 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00006279 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00006280 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006281 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006282 DeclUpdates[D].push_back(
6283 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6284 });
Richard Smith1fa5d642013-05-11 05:45:24 +00006285}
6286
Richard Smithf8134002015-03-10 01:41:22 +00006287void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
Richard Smith5b349582017-10-13 01:55:36 +00006288 const FunctionDecl *Delete,
6289 Expr *ThisArg) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006290 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006291 assert(!WritingAST && "Already writing the AST!");
6292 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006293 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006294 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006295 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6296 });
Richard Smithf8134002015-03-10 01:41:22 +00006297}
6298
Sebastian Redlab238a72011-04-24 16:28:06 +00006299void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006300 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006301 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006302 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006303 return; // Declaration not imported from PCH.
6304
Richard Smith4d235792014-08-07 18:53:08 +00006305 // Implicit function decl from a PCH was defined.
6306 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006307}
6308
Richard Smith891fc7f2017-12-05 01:31:47 +00006309void ASTWriter::VariableDefinitionInstantiated(const VarDecl *D) {
6310 if (Chain && Chain->isProcessingUpdateRecords()) return;
6311 assert(!WritingAST && "Already writing the AST!");
6312 if (!D->isFromASTFile())
6313 return;
6314
6315 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_VAR_DEFINITION));
6316}
6317
Richard Smithd28ac5b2014-03-22 23:33:22 +00006318void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006319 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006320 assert(!WritingAST && "Already writing the AST!");
6321 if (!D->isFromASTFile())
6322 return;
6323
Richard Smith9e2341d2015-03-23 03:25:59 +00006324 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006325}
6326
Richard Smith891fc7f2017-12-05 01:31:47 +00006327void ASTWriter::InstantiationRequested(const ValueDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006328 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006329 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006330 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006331 return;
6332
6333 // Since the actual instantiation is delayed, this really means that we need
6334 // to update the instantiation location.
Richard Smith891fc7f2017-12-05 01:31:47 +00006335 SourceLocation POI;
6336 if (auto *VD = dyn_cast<VarDecl>(D))
6337 POI = VD->getPointOfInstantiation();
6338 else
6339 POI = cast<FunctionDecl>(D)->getPointOfInstantiation();
6340 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_POINT_OF_INSTANTIATION, POI));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006341}
6342
John McCall32791cc2016-01-06 22:34:54 +00006343void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006344 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006345 assert(!WritingAST && "Already writing the AST!");
6346 if (!D->isFromASTFile())
6347 return;
6348
6349 DeclUpdates[D].push_back(
6350 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6351}
6352
Richard Smith4b054b22016-08-24 21:25:37 +00006353void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6354 assert(!WritingAST && "Already writing the AST!");
6355 if (!D->isFromASTFile())
6356 return;
6357
6358 DeclUpdates[D].push_back(
6359 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6360}
6361
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006362void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6363 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006364 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006365 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006366 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006367 return; // Declaration not imported from PCH.
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00006368
Douglas Gregor404cdde2012-01-27 01:47:08 +00006369 assert(IFD->getDefinition() && "Category on a class without a definition?");
6370 ObjCClassesWithCategories.insert(
6371 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006372}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006373
Eli Friedman276dd182013-09-05 00:02:25 +00006374void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006375 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006376 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006377
6378 // If there is *any* declaration of the entity that's not from an AST file,
6379 // we can skip writing the update record. We make sure that isUsed() triggers
6380 // completion of the redeclaration chain of the entity.
6381 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6382 if (IsLocalDecl(Prev))
6383 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006384
Aaron Ballman4f45b712014-03-21 15:22:56 +00006385 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006386}
Alexey Bataev97720002014-11-11 04:05:39 +00006387
6388void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006389 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006390 assert(!WritingAST && "Already writing the AST!");
6391 if (!D->isFromASTFile())
6392 return;
6393
6394 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6395}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006396
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006397void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6398 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006399 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006400 assert(!WritingAST && "Already writing the AST!");
6401 if (!D->isFromASTFile())
6402 return;
6403
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006404 DeclUpdates[D].push_back(
6405 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006406}
6407
Richard Smith4caa4492015-05-15 02:34:32 +00006408void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006409 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006410 assert(!WritingAST && "Already writing the AST!");
6411 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006412 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006413}
Alex Denisovfde64952015-06-26 05:28:36 +00006414
6415void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6416 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006417 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006418 assert(!WritingAST && "Already writing the AST!");
6419 if (!Record->isFromASTFile())
6420 return;
6421 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6422}
Richard Smithc26d9742016-10-06 20:30:51 +00006423
6424void ASTWriter::AddedCXXTemplateSpecialization(
6425 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6426 assert(!WritingAST && "Already writing the AST!");
6427
6428 if (!TD->getFirstDecl()->isFromASTFile())
6429 return;
6430 if (Chain && Chain->isProcessingUpdateRecords())
6431 return;
6432
6433 DeclsToEmitEvenIfUnreferenced.push_back(D);
6434}
6435
6436void ASTWriter::AddedCXXTemplateSpecialization(
6437 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6438 assert(!WritingAST && "Already writing the AST!");
6439
6440 if (!TD->getFirstDecl()->isFromASTFile())
6441 return;
6442 if (Chain && Chain->isProcessingUpdateRecords())
6443 return;
6444
6445 DeclsToEmitEvenIfUnreferenced.push_back(D);
6446}
6447
6448void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6449 const FunctionDecl *D) {
6450 assert(!WritingAST && "Already writing the AST!");
6451
6452 if (!TD->getFirstDecl()->isFromASTFile())
6453 return;
6454 if (Chain && Chain->isProcessingUpdateRecords())
6455 return;
6456
6457 DeclsToEmitEvenIfUnreferenced.push_back(D);
6458}