blob: 4400585fd4e1ef20865c63b8bf0774ca0d9b4018 [file] [log] [blame]
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001//===--- ASTWriter.cpp - AST File Writer ------------------------*- C++ -*-===//
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"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000022#include "clang/AST/DeclCXX.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000027#include "clang/AST/LambdaCapture.h"
28#include "clang/AST/NestedNameSpecifier.h"
29#include "clang/AST/RawCommentList.h"
30#include "clang/AST/TemplateName.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000031#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000032#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000033#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000035#include "clang/Basic/FileSystemOptions.h"
36#include "clang/Basic/LangOptions.h"
37#include "clang/Basic/LLVM.h"
38#include "clang/Basic/Module.h"
39#include "clang/Basic/ObjCRuntime.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000040#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000041#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000042#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000043#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000044#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000045#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000046#include "clang/Lex/HeaderSearch.h"
47#include "clang/Lex/HeaderSearchOptions.h"
48#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000049#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000050#include "clang/Lex/PreprocessingRecord.h"
51#include "clang/Lex/Preprocessor.h"
52#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000053#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000054#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000055#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000056#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000057#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000058#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000059#include "clang/Serialization/Module.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000060#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000061#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000062#include "llvm/ADT/APFloat.h"
63#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000064#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000065#include "llvm/ADT/IntrusiveRefCntPtr.h"
66#include "llvm/ADT/Optional.h"
67#include "llvm/ADT/SmallSet.h"
68#include "llvm/ADT/SmallString.h"
69#include "llvm/ADT/STLExtras.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000070#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000071#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000073#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000074#include "llvm/Support/Compression.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000075#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000076#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000077#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000078#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000079#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000080#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000081#include "llvm/Support/Process.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000082#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000083#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000084#include <cassert>
85#include <cstdint>
86#include <cstdlib>
87#include <cstring>
88#include <deque>
89#include <limits>
90#include <new>
91#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +000092#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000093
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000095using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096
Sebastian Redl3df5a082010-07-30 17:03:48 +000097template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000098static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000099 if (v.empty()) return StringRef();
100 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000101 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000102}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000103
104template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000105static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000106 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000107 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000108}
109
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110//===----------------------------------------------------------------------===//
111// Type serialization
112//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000113
Richard Smith290d8012016-04-06 17:06:00 +0000114namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000115
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000116 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000117 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000118 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +0000121 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +0000122 /// \brief Abbreviation to use for the record, if any.
123 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000124
Richard Smith290d8012016-04-06 17:06:00 +0000125 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000126 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Richard Smith290d8012016-04-06 17:06:00 +0000127 : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
128
129 uint64_t Emit() {
130 return Record.Emit(Code, AbbrevToUse);
131 }
132
133 void Visit(QualType T) {
134 if (T.hasLocalNonFastQualifiers()) {
135 Qualifiers Qs = T.getLocalQualifiers();
136 Record.AddTypeRef(T.getLocalUnqualifiedType());
137 Record.push_back(Qs.getAsOpaqueValue());
138 Code = TYPE_EXT_QUAL;
139 AbbrevToUse = Writer.TypeExtQualAbbrev;
140 } else {
141 switch (T->getTypeClass()) {
142 // For all of the concrete, non-dependent types, call the
143 // appropriate visitor function.
144#define TYPE(Class, Base) \
145 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
146#define ABSTRACT_TYPE(Class, Base)
147#include "clang/AST/TypeNodes.def"
148 }
149 }
150 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151
152 void VisitArrayType(const ArrayType *T);
153 void VisitFunctionType(const FunctionType *T);
154 void VisitTagType(const TagType *T);
155
156#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
157#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158#include "clang/AST/TypeNodes.def"
159 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000160
Richard Smith290d8012016-04-06 17:06:00 +0000161} // end namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000164 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000168 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000173 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000174 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175}
176
Reid Kleckner8a365022013-06-24 17:51:48 +0000177void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000178 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000179 Code = TYPE_DECAYED;
180}
181
Reid Kleckner0503a872013-12-05 01:23:43 +0000182void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000183 Record.AddTypeRef(T->getOriginalType());
184 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000185 Code = TYPE_ADJUSTED;
186}
187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000188void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000189 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000190 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000191}
192
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000193void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000194 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000195 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000200 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202}
203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000205 Record.AddTypeRef(T->getPointeeType());
206 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000207 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000211 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000213 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000218 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000229 Record.AddSourceLocation(T->getLBracketLoc());
230 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000231 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000232 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000233}
234
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000235void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000236 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000238 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000239 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240}
241
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000242void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000244 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245}
246
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000248 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000249 FunctionType::ExtInfo C = T->getExtInfo();
250 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000251 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000252 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000253 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000254 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000255 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000256
257 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
258 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000259}
260
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000261void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000263 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000264}
265
Richard Smith290d8012016-04-06 17:06:00 +0000266static void addExceptionSpec(const FunctionProtoType *T,
267 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000268 Record.push_back(T->getExceptionSpecType());
269 if (T->getExceptionSpecType() == EST_Dynamic) {
270 Record.push_back(T->getNumExceptions());
271 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000272 Record.AddTypeRef(T->getExceptionType(I));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000273 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
Richard Smith290d8012016-04-06 17:06:00 +0000274 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000275 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000276 Record.AddDeclRef(T->getExceptionSpecDecl());
277 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000278 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000279 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000280 }
Richard Smith564417a2014-03-20 21:47:22 +0000281}
282
283void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
284 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000285
Richard Smith564417a2014-03-20 21:47:22 +0000286 Record.push_back(T->isVariadic());
287 Record.push_back(T->hasTrailingReturn());
288 Record.push_back(T->getTypeQuals());
289 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000290 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000291
292 Record.push_back(T->getNumParams());
293 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000294 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000295
John McCall18afab72016-03-01 00:49:02 +0000296 if (T->hasExtParameterInfos()) {
297 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
298 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
299 }
300
Richard Smith01b2cb42014-07-26 06:37:51 +0000301 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000302 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
303 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000304 AbbrevToUse = 0;
305
Sebastian Redl539c5062010-08-18 23:57:32 +0000306 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000307}
308
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000309void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000310 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000311 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000312}
John McCallb96ec562009-12-04 22:46:56 +0000313
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000314void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000315 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000316 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000317 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000319}
320
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000321void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000322 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000323 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000324}
325
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000326void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000327 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000328 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000329}
330
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000331void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000332 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000333 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000334 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000335}
336
Alexis Hunte852b102011-05-24 22:41:36 +0000337void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000338 Record.AddTypeRef(T->getBaseType());
339 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000340 Record.push_back(T->getUTTKind());
341 Code = TYPE_UNARY_TRANSFORM;
342}
343
Richard Smith30482bc2011-02-20 03:19:35 +0000344void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000345 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000346 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000347 if (T->getDeducedType().isNull())
348 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000349 Code = TYPE_AUTO;
350}
351
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000352void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000353 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000354 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000355 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000356 "Cannot serialize in the middle of a type definition");
357}
358
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000359void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000360 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000361 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000362}
363
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000364void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000365 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000367}
368
John McCall81904512011-01-06 01:58:22 +0000369void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000370 Record.AddTypeRef(T->getModifiedType());
371 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000372 Record.push_back(T->getAttrKind());
373 Code = TYPE_ATTRIBUTED;
374}
375
Mike Stump11289f42009-09-09 15:08:12 +0000376void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000377ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000378 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000379 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
380 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000381 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000382}
383
384void
Douglas Gregorada4b792011-01-14 02:55:32 +0000385ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
386 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000387 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
388 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000389 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
390}
391
392void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000393ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000394 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000395 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000396 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000397 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000398 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000399 Record.AddTemplateArgument(ArgI);
400 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
401 : T->isCanonicalUnqualified()
402 ? QualType()
403 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000404 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000405}
406
407void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000408ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000409 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000410 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000411 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000412 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000413}
414
415void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000416ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000417 const DependentSizedExtVectorType *T) {
418 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000419 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000420}
421
422void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000423ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000424 Record.push_back(T->getDepth());
425 Record.push_back(T->getIndex());
426 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000427 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000428 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000429}
430
431void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000432ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000433 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000434 Record.AddNestedNameSpecifier(T->getQualifier());
435 Record.AddIdentifierRef(T->getIdentifier());
436 Record.AddTypeRef(
437 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000438 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000439}
440
441void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000442ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000443 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000444 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000445 Record.AddNestedNameSpecifier(T->getQualifier());
446 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000447 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000448 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000449 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000450 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000451}
452
Douglas Gregord2fa7662010-12-20 02:24:11 +0000453void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000454 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000455 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000456 Record.push_back(*NumExpansions + 1);
457 else
458 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000459 Code = TYPE_PACK_EXPANSION;
460}
461
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000462void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000463 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000464 Code = TYPE_PAREN;
465}
466
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000467void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000468 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000469 Record.AddNestedNameSpecifier(T->getQualifier());
470 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000471 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000472}
473
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000474void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000475 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
476 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000477 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000478}
479
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000480void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000481 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000482 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000483}
484
Manman Rene6be26c2016-09-13 17:25:08 +0000485void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
486 Record.AddDeclRef(T->getDecl());
487 Record.push_back(T->getNumProtocols());
488 for (const auto *I : T->quals())
489 Record.AddDeclRef(I);
490 Code = TYPE_OBJC_TYPE_PARAM;
491}
492
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000493void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000494 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000495 Record.push_back(T->getTypeArgsAsWritten().size());
496 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000497 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000498 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000499 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000500 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000501 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000502 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000503}
504
Steve Narofffb4330f2009-06-17 22:40:22 +0000505void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000506ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000507 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000508 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000509}
510
Eli Friedman0dfb8892011-10-06 23:00:33 +0000511void
512ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000513 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000514 Code = TYPE_ATOMIC;
515}
516
Xiuli Pan9c14e282016-01-09 12:53:17 +0000517void
518ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000519 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000520 Record.push_back(T->isReadOnly());
521 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000522}
523
John McCall8f115c62009-10-16 21:56:05 +0000524namespace {
525
526class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000527 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000528
529public:
Richard Smith290d8012016-04-06 17:06:00 +0000530 TypeLocWriter(ASTRecordWriter &Record)
531 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000532
John McCall17001972009-10-18 01:05:36 +0000533#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000534#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000535 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000536#include "clang/AST/TypeLocNodes.def"
537
John McCall17001972009-10-18 01:05:36 +0000538 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
539 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000540};
541
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000542} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000543
John McCall17001972009-10-18 01:05:36 +0000544void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
545 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000546}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000547
John McCall17001972009-10-18 01:05:36 +0000548void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000549 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000550 if (TL.needsExtraLocalData()) {
551 Record.push_back(TL.getWrittenTypeSpec());
552 Record.push_back(TL.getWrittenSignSpec());
553 Record.push_back(TL.getWrittenWidthSpec());
554 Record.push_back(TL.hasModeAttr());
555 }
John McCall8f115c62009-10-16 21:56:05 +0000556}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000557
John McCall17001972009-10-18 01:05:36 +0000558void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000559 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000560}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000561
John McCall17001972009-10-18 01:05:36 +0000562void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000563 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000564}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000565
Reid Kleckner8a365022013-06-24 17:51:48 +0000566void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
567 // nothing to do
568}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000569
Reid Kleckner0503a872013-12-05 01:23:43 +0000570void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
571 // nothing to do
572}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000573
John McCall17001972009-10-18 01:05:36 +0000574void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000575 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000576}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000577
John McCall17001972009-10-18 01:05:36 +0000578void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000579 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000580}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000581
John McCall17001972009-10-18 01:05:36 +0000582void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000583 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000584}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000585
John McCall17001972009-10-18 01:05:36 +0000586void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000587 Record.AddSourceLocation(TL.getStarLoc());
588 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000589}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000590
John McCall17001972009-10-18 01:05:36 +0000591void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000592 Record.AddSourceLocation(TL.getLBracketLoc());
593 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000594 Record.push_back(TL.getSizeExpr() ? 1 : 0);
595 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000596 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000597}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000598
John McCall17001972009-10-18 01:05:36 +0000599void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
600 VisitArrayTypeLoc(TL);
601}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000602
John McCall17001972009-10-18 01:05:36 +0000603void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
604 VisitArrayTypeLoc(TL);
605}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000606
John McCall17001972009-10-18 01:05:36 +0000607void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
608 VisitArrayTypeLoc(TL);
609}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000610
John McCall17001972009-10-18 01:05:36 +0000611void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
612 DependentSizedArrayTypeLoc TL) {
613 VisitArrayTypeLoc(TL);
614}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000615
John McCall17001972009-10-18 01:05:36 +0000616void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
617 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000618 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000619}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000620
John McCall17001972009-10-18 01:05:36 +0000621void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000622 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000623}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000624
John McCall17001972009-10-18 01:05:36 +0000625void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000626 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000627}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000628
John McCall17001972009-10-18 01:05:36 +0000629void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000630 Record.AddSourceLocation(TL.getLocalRangeBegin());
631 Record.AddSourceLocation(TL.getLParenLoc());
632 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000633 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000634 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000635 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000636 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000637}
638void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
639 VisitFunctionTypeLoc(TL);
640}
641void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
642 VisitFunctionTypeLoc(TL);
643}
John McCallb96ec562009-12-04 22:46:56 +0000644void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000645 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000646}
John McCall17001972009-10-18 01:05:36 +0000647void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000648 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000649}
Manman Rene6be26c2016-09-13 17:25:08 +0000650void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
651 if (TL.getNumProtocols()) {
652 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
653 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
654 }
655 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
656 Record.AddSourceLocation(TL.getProtocolLoc(i));
657}
John McCall17001972009-10-18 01:05:36 +0000658void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000659 Record.AddSourceLocation(TL.getTypeofLoc());
660 Record.AddSourceLocation(TL.getLParenLoc());
661 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000662}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000663
John McCall17001972009-10-18 01:05:36 +0000664void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000665 Record.AddSourceLocation(TL.getTypeofLoc());
666 Record.AddSourceLocation(TL.getLParenLoc());
667 Record.AddSourceLocation(TL.getRParenLoc());
668 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000669}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000670
John McCall17001972009-10-18 01:05:36 +0000671void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000672 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000673}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000674
Alexis Hunte852b102011-05-24 22:41:36 +0000675void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000676 Record.AddSourceLocation(TL.getKWLoc());
677 Record.AddSourceLocation(TL.getLParenLoc());
678 Record.AddSourceLocation(TL.getRParenLoc());
679 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000680}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000681
Richard Smith30482bc2011-02-20 03:19:35 +0000682void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000683 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000684}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000685
John McCall17001972009-10-18 01:05:36 +0000686void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000687 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000688}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000689
John McCall17001972009-10-18 01:05:36 +0000690void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000691 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000692}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000693
John McCall81904512011-01-06 01:58:22 +0000694void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000695 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000696 if (TL.hasAttrOperand()) {
697 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000698 Record.AddSourceLocation(range.getBegin());
699 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000700 }
701 if (TL.hasAttrExprOperand()) {
702 Expr *operand = TL.getAttrExprOperand();
703 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000704 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000705 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000706 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000707 }
708}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000709
John McCall17001972009-10-18 01:05:36 +0000710void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000711 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000712}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000713
John McCallcebee162009-10-18 09:09:24 +0000714void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
715 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000716 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000717}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000718
Douglas Gregorada4b792011-01-14 02:55:32 +0000719void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
720 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000721 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000722}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000723
John McCall17001972009-10-18 01:05:36 +0000724void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
725 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000726 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
727 Record.AddSourceLocation(TL.getTemplateNameLoc());
728 Record.AddSourceLocation(TL.getLAngleLoc());
729 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000730 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000731 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
732 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000733}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000734
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000735void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000736 Record.AddSourceLocation(TL.getLParenLoc());
737 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000738}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000739
Abramo Bagnara6150c882010-05-11 21:36:43 +0000740void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000741 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
742 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000743}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000744
John McCalle78aac42010-03-10 03:28:59 +0000745void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000746 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000747}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000748
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000749void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000750 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
751 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
752 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000753}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000754
John McCallc392f372010-06-11 00:33:02 +0000755void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
756 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000757 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
758 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
759 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
760 Record.AddSourceLocation(TL.getTemplateNameLoc());
761 Record.AddSourceLocation(TL.getLAngleLoc());
762 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000763 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000764 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
765 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000766}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000767
Douglas Gregord2fa7662010-12-20 02:24:11 +0000768void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000769 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000770}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000771
John McCall17001972009-10-18 01:05:36 +0000772void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000773 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000774}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000775
John McCall8b07ec22010-05-15 11:32:37 +0000776void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
777 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000778 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
779 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000780 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000781 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
782 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
783 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000784 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000785 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000786}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000787
John McCallfc93cf92009-10-22 22:37:11 +0000788void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000789 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000790}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000791
Eli Friedman0dfb8892011-10-06 23:00:33 +0000792void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000793 Record.AddSourceLocation(TL.getKWLoc());
794 Record.AddSourceLocation(TL.getLParenLoc());
795 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000796}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000797
Xiuli Pan9c14e282016-01-09 12:53:17 +0000798void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000799 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000800}
John McCall8f115c62009-10-16 21:56:05 +0000801
Richard Smith01b2cb42014-07-26 06:37:51 +0000802void ASTWriter::WriteTypeAbbrevs() {
803 using namespace llvm;
804
David Blaikieb44f0bf2017-01-04 22:36:43 +0000805 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000806
807 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000808 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000809 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
810 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
811 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000812 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000813
814 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000815 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000816 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
817 // FunctionType
818 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
819 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
820 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
821 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
822 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
823 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
824 // FunctionProtoType
825 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
826 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
827 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
828 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
829 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
830 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
831 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
832 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000833 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000834}
835
Chris Lattner19cea4e2009-04-22 05:57:30 +0000836//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000837// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000838//===----------------------------------------------------------------------===//
839
Chris Lattner28fa4e62009-04-26 22:26:21 +0000840static void EmitBlockID(unsigned ID, const char *Name,
841 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000842 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000843 Record.clear();
844 Record.push_back(ID);
845 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
846
847 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000848 if (!Name || Name[0] == 0)
849 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000850 Record.clear();
851 while (*Name)
852 Record.push_back(*Name++);
853 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
854}
855
856static void EmitRecordID(unsigned ID, const char *Name,
857 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000858 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000859 Record.clear();
860 Record.push_back(ID);
861 while (*Name)
862 Record.push_back(*Name++);
863 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000864}
865
866static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000867 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000868#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000869 RECORD(STMT_STOP);
870 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000871 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000872 RECORD(STMT_NULL);
873 RECORD(STMT_COMPOUND);
874 RECORD(STMT_CASE);
875 RECORD(STMT_DEFAULT);
876 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000877 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000878 RECORD(STMT_IF);
879 RECORD(STMT_SWITCH);
880 RECORD(STMT_WHILE);
881 RECORD(STMT_DO);
882 RECORD(STMT_FOR);
883 RECORD(STMT_GOTO);
884 RECORD(STMT_INDIRECT_GOTO);
885 RECORD(STMT_CONTINUE);
886 RECORD(STMT_BREAK);
887 RECORD(STMT_RETURN);
888 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000889 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000890 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000891 RECORD(EXPR_PREDEFINED);
892 RECORD(EXPR_DECL_REF);
893 RECORD(EXPR_INTEGER_LITERAL);
894 RECORD(EXPR_FLOATING_LITERAL);
895 RECORD(EXPR_IMAGINARY_LITERAL);
896 RECORD(EXPR_STRING_LITERAL);
897 RECORD(EXPR_CHARACTER_LITERAL);
898 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000899 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000900 RECORD(EXPR_UNARY_OPERATOR);
901 RECORD(EXPR_SIZEOF_ALIGN_OF);
902 RECORD(EXPR_ARRAY_SUBSCRIPT);
903 RECORD(EXPR_CALL);
904 RECORD(EXPR_MEMBER);
905 RECORD(EXPR_BINARY_OPERATOR);
906 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
907 RECORD(EXPR_CONDITIONAL_OPERATOR);
908 RECORD(EXPR_IMPLICIT_CAST);
909 RECORD(EXPR_CSTYLE_CAST);
910 RECORD(EXPR_COMPOUND_LITERAL);
911 RECORD(EXPR_EXT_VECTOR_ELEMENT);
912 RECORD(EXPR_INIT_LIST);
913 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000914 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000915 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000916 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000917 RECORD(EXPR_VA_ARG);
918 RECORD(EXPR_ADDR_LABEL);
919 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000920 RECORD(EXPR_CHOOSE);
921 RECORD(EXPR_GNU_NULL);
922 RECORD(EXPR_SHUFFLE_VECTOR);
923 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000924 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000925 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000926 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000927 RECORD(EXPR_OBJC_ARRAY_LITERAL);
928 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000929 RECORD(EXPR_OBJC_ENCODE);
930 RECORD(EXPR_OBJC_SELECTOR_EXPR);
931 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
932 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
933 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
934 RECORD(EXPR_OBJC_KVC_REF_EXPR);
935 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000936 RECORD(STMT_OBJC_FOR_COLLECTION);
937 RECORD(STMT_OBJC_CATCH);
938 RECORD(STMT_OBJC_FINALLY);
939 RECORD(STMT_OBJC_AT_TRY);
940 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
941 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000942 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000943 RECORD(STMT_CXX_CATCH);
944 RECORD(STMT_CXX_TRY);
945 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000946 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000947 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000948 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000949 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000950 RECORD(EXPR_CXX_STATIC_CAST);
951 RECORD(EXPR_CXX_DYNAMIC_CAST);
952 RECORD(EXPR_CXX_REINTERPRET_CAST);
953 RECORD(EXPR_CXX_CONST_CAST);
954 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000955 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000956 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000957 RECORD(EXPR_CXX_BOOL_LITERAL);
958 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000959 RECORD(EXPR_CXX_TYPEID_EXPR);
960 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000961 RECORD(EXPR_CXX_THIS);
962 RECORD(EXPR_CXX_THROW);
963 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000964 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000965 RECORD(EXPR_CXX_BIND_TEMPORARY);
966 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
967 RECORD(EXPR_CXX_NEW);
968 RECORD(EXPR_CXX_DELETE);
969 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
970 RECORD(EXPR_EXPR_WITH_CLEANUPS);
971 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
972 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
973 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
974 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
975 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000976 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000977 RECORD(EXPR_CXX_NOEXCEPT);
978 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000979 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
980 RECORD(EXPR_TYPE_TRAIT);
981 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000982 RECORD(EXPR_PACK_EXPANSION);
983 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000984 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000985 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000986 RECORD(EXPR_FUNCTION_PARM_PACK);
987 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000988 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000989 RECORD(EXPR_CXX_UUIDOF_EXPR);
990 RECORD(EXPR_CXX_UUIDOF_TYPE);
991 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000992#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000993}
Mike Stump11289f42009-09-09 15:08:12 +0000994
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000995void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000996 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000997 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +0000998
Sebastian Redl539c5062010-08-18 23:57:32 +0000999#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1000#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001001
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001002 // Control Block.
1003 BLOCK(CONTROL_BLOCK);
1004 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +00001005 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001006 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001007 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001008 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001009 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001010 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001011 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001012 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001013 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001014
1015 BLOCK(OPTIONS_BLOCK);
1016 RECORD(LANGUAGE_OPTIONS);
1017 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001018 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001019 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001020 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001021 RECORD(PREPROCESSOR_OPTIONS);
1022
Douglas Gregor108cb222012-10-19 00:45:00 +00001023 BLOCK(INPUT_FILES_BLOCK);
1024 RECORD(INPUT_FILE);
1025
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001026 // AST Top-Level Block.
1027 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001028 RECORD(TYPE_OFFSET);
1029 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001030 RECORD(IDENTIFIER_OFFSET);
1031 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001032 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001033 RECORD(SPECIAL_TYPES);
1034 RECORD(STATISTICS);
1035 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001036 RECORD(SELECTOR_OFFSETS);
1037 RECORD(METHOD_POOL);
1038 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001039 RECORD(SOURCE_LOCATION_OFFSETS);
1040 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001041 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001042 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001043 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001044 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001045 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001046 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001047 RECORD(SEMA_DECL_REFS);
1048 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1049 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001050 RECORD(UPDATE_VISIBLE);
1051 RECORD(DECL_UPDATE_OFFSETS);
1052 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001053 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001054 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001055 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001056 RECORD(FP_PRAGMA_OPTIONS);
1057 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001058 RECORD(OPENCL_EXTENSION_TYPES);
1059 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001060 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001061 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001062 RECORD(MODULE_OFFSET_MAP);
1063 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001064 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001065 RECORD(FILE_SORTED_DECLS);
1066 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001067 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001068 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001069 RECORD(INTERESTING_IDENTIFIERS);
1070 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001071 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001072 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001073 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001074 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001075 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001076 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001077 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001078
Chris Lattner28fa4e62009-04-26 22:26:21 +00001079 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001080 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001081 RECORD(SM_SLOC_FILE_ENTRY);
1082 RECORD(SM_SLOC_BUFFER_ENTRY);
1083 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001084 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001085 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattner28fa4e62009-04-26 22:26:21 +00001087 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001088 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001089 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001090 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001091 RECORD(PP_MACRO_OBJECT_LIKE);
1092 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001093 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001094
Richard Smithfa715652015-08-31 22:43:10 +00001095 // Submodule Block.
1096 BLOCK(SUBMODULE_BLOCK);
1097 RECORD(SUBMODULE_METADATA);
1098 RECORD(SUBMODULE_DEFINITION);
1099 RECORD(SUBMODULE_UMBRELLA_HEADER);
1100 RECORD(SUBMODULE_HEADER);
1101 RECORD(SUBMODULE_TOPHEADER);
1102 RECORD(SUBMODULE_UMBRELLA_DIR);
1103 RECORD(SUBMODULE_IMPORTS);
1104 RECORD(SUBMODULE_EXPORTS);
1105 RECORD(SUBMODULE_REQUIRES);
1106 RECORD(SUBMODULE_EXCLUDED_HEADER);
1107 RECORD(SUBMODULE_LINK_LIBRARY);
1108 RECORD(SUBMODULE_CONFIG_MACRO);
1109 RECORD(SUBMODULE_CONFLICT);
1110 RECORD(SUBMODULE_PRIVATE_HEADER);
1111 RECORD(SUBMODULE_TEXTUAL_HEADER);
1112 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001113 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001114
1115 // Comments Block.
1116 BLOCK(COMMENTS_BLOCK);
1117 RECORD(COMMENTS_RAW_COMMENT);
1118
Douglas Gregor12bfa382009-10-17 00:13:19 +00001119 // Decls and Types block.
1120 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001121 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001122 RECORD(TYPE_COMPLEX);
1123 RECORD(TYPE_POINTER);
1124 RECORD(TYPE_BLOCK_POINTER);
1125 RECORD(TYPE_LVALUE_REFERENCE);
1126 RECORD(TYPE_RVALUE_REFERENCE);
1127 RECORD(TYPE_MEMBER_POINTER);
1128 RECORD(TYPE_CONSTANT_ARRAY);
1129 RECORD(TYPE_INCOMPLETE_ARRAY);
1130 RECORD(TYPE_VARIABLE_ARRAY);
1131 RECORD(TYPE_VECTOR);
1132 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001133 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001134 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001135 RECORD(TYPE_TYPEDEF);
1136 RECORD(TYPE_TYPEOF_EXPR);
1137 RECORD(TYPE_TYPEOF);
1138 RECORD(TYPE_RECORD);
1139 RECORD(TYPE_ENUM);
1140 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001141 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001142 RECORD(TYPE_DECLTYPE);
1143 RECORD(TYPE_ELABORATED);
1144 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1145 RECORD(TYPE_UNRESOLVED_USING);
1146 RECORD(TYPE_INJECTED_CLASS_NAME);
1147 RECORD(TYPE_OBJC_OBJECT);
1148 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1149 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1150 RECORD(TYPE_DEPENDENT_NAME);
1151 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1152 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1153 RECORD(TYPE_PAREN);
1154 RECORD(TYPE_PACK_EXPANSION);
1155 RECORD(TYPE_ATTRIBUTED);
1156 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001157 RECORD(TYPE_AUTO);
1158 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001159 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001160 RECORD(TYPE_DECAYED);
1161 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001162 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001163 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001164 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001165 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001166 RECORD(DECL_ENUM);
1167 RECORD(DECL_RECORD);
1168 RECORD(DECL_ENUM_CONSTANT);
1169 RECORD(DECL_FUNCTION);
1170 RECORD(DECL_OBJC_METHOD);
1171 RECORD(DECL_OBJC_INTERFACE);
1172 RECORD(DECL_OBJC_PROTOCOL);
1173 RECORD(DECL_OBJC_IVAR);
1174 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001175 RECORD(DECL_OBJC_CATEGORY);
1176 RECORD(DECL_OBJC_CATEGORY_IMPL);
1177 RECORD(DECL_OBJC_IMPLEMENTATION);
1178 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1179 RECORD(DECL_OBJC_PROPERTY);
1180 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001181 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001182 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001183 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001184 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001185 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001186 RECORD(DECL_FILE_SCOPE_ASM);
1187 RECORD(DECL_BLOCK);
1188 RECORD(DECL_CONTEXT_LEXICAL);
1189 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001190 RECORD(DECL_NAMESPACE);
1191 RECORD(DECL_NAMESPACE_ALIAS);
1192 RECORD(DECL_USING);
1193 RECORD(DECL_USING_SHADOW);
1194 RECORD(DECL_USING_DIRECTIVE);
1195 RECORD(DECL_UNRESOLVED_USING_VALUE);
1196 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1197 RECORD(DECL_LINKAGE_SPEC);
1198 RECORD(DECL_CXX_RECORD);
1199 RECORD(DECL_CXX_METHOD);
1200 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001201 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001202 RECORD(DECL_CXX_DESTRUCTOR);
1203 RECORD(DECL_CXX_CONVERSION);
1204 RECORD(DECL_ACCESS_SPEC);
1205 RECORD(DECL_FRIEND);
1206 RECORD(DECL_FRIEND_TEMPLATE);
1207 RECORD(DECL_CLASS_TEMPLATE);
1208 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1209 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001210 RECORD(DECL_VAR_TEMPLATE);
1211 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1212 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001213 RECORD(DECL_FUNCTION_TEMPLATE);
1214 RECORD(DECL_TEMPLATE_TYPE_PARM);
1215 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1216 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001217 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001218 RECORD(DECL_STATIC_ASSERT);
1219 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001220 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001221 RECORD(DECL_INDIRECTFIELD);
1222 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001223 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1224 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1225 RECORD(DECL_IMPORT);
1226 RECORD(DECL_OMP_THREADPRIVATE);
1227 RECORD(DECL_EMPTY);
1228 RECORD(DECL_OBJC_TYPE_PARAM);
1229 RECORD(DECL_OMP_CAPTUREDEXPR);
1230 RECORD(DECL_PRAGMA_COMMENT);
1231 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1232 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001233
Douglas Gregor03412ba2011-06-03 02:27:19 +00001234 // Statements and Exprs can occur in the Decls and Types block.
1235 AddStmtsExprs(Stream, Record);
1236
Douglas Gregor92a96f52011-02-08 21:58:10 +00001237 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001238 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001239 RECORD(PPD_MACRO_DEFINITION);
1240 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001241
1242 // Decls and Types block.
1243 BLOCK(EXTENSION_BLOCK);
1244 RECORD(EXTENSION_METADATA);
1245
Chris Lattner28fa4e62009-04-26 22:26:21 +00001246#undef RECORD
1247#undef BLOCK
1248 Stream.ExitBlock();
1249}
1250
Richard Smith54cc3c22014-12-11 20:50:24 +00001251/// \brief Prepares a path for being written to an AST file by converting it
1252/// to an absolute path and removing nested './'s.
1253///
1254/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001255static bool cleanPathForOutput(FileManager &FileMgr,
1256 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001257 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001258 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001259}
1260
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001261/// \brief Adjusts the given filename to only write out the portion of the
1262/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001263///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001264/// \param Filename the file name to adjust.
1265///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001266/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1267/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001268///
1269/// \returns either the original filename (if it needs no adjustment) or the
1270/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001271static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001272adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001273 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001274
Richard Smith7ed1bc92014-12-05 22:42:13 +00001275 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001276 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001277
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001278 // Verify that the filename and the system root have the same prefix.
1279 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001280 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1281 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001282 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001284 // We hit the end of the filename before we hit the end of the system root.
1285 if (!Filename[Pos])
1286 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001287
Richard Smith7ed1bc92014-12-05 22:42:13 +00001288 // If there's not a path separator at the end of the base directory nor
1289 // immediately after it, then this isn't within the base directory.
1290 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1291 if (!llvm::sys::path::is_separator(BaseDir.back()))
1292 return Filename;
1293 } else {
1294 // If the file name has a '/' at the current position, skip over the '/'.
1295 // We distinguish relative paths from absolute paths by the
1296 // absence of '/' at the beginning of relative paths.
1297 //
1298 // FIXME: This is wrong. We distinguish them by asking if the path is
1299 // absolute, which isn't the same thing. And there might be multiple '/'s
1300 // in a row. Use a better mechanism to indicate whether we have emitted an
1301 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001302 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001303 }
Mike Stump11289f42009-09-09 15:08:12 +00001304
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001305 return Filename + Pos;
1306}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001307
Ben Langmuir487ea142014-10-23 18:05:36 +00001308static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001309 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001310 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1311 return S;
1312 // Rely on GetRandomNumber to eventually return non-zero...
1313 }
1314}
1315
Douglas Gregor112b9072012-10-18 05:31:06 +00001316/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001317uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1318 ASTContext &Context,
1319 StringRef isysroot,
1320 const std::string &OutputFile) {
1321 ASTFileSignature Signature = 0;
1322
Douglas Gregorbfbde532009-04-10 21:16:55 +00001323 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001324 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001325 RecordData Record;
1326
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001327 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001328 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001329 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1330 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1331 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1332 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1333 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1334 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001335 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001336 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1337 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001338 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001339 assert((!WritingModule || isysroot.empty()) &&
1340 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001341 {
1342 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1343 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1344 !isysroot.empty(), IncludeTimestamps,
1345 ASTHasCompilerErrors};
1346 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1347 getClangFullRepositoryVersion());
1348 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001349 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001350 // For implicit modules we output a signature that we can use to ensure
1351 // duplicate module builds don't collide in the cache as their output order
1352 // is non-deterministic.
1353 // FIXME: Remove this when output is deterministic.
1354 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001355 Signature = getSignature();
1356 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001357 Stream.EmitRecord(SIGNATURE, Record);
1358 }
1359
Richard Smith7ed1bc92014-12-05 22:42:13 +00001360 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001361 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001362 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001364 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001365 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001366 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1367 }
1368
Richard Smith7ed1bc92014-12-05 22:42:13 +00001369 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001370 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001371 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001372
1373 // If the home of the module is the current working directory, then we
1374 // want to pick up the cwd of the build process loading the module, not
1375 // our cwd, when we load this module.
1376 if (!PP.getHeaderSearchInfo()
1377 .getHeaderSearchOpts()
1378 .ModuleMapFileHomeIsCwd ||
1379 WritingModule->Directory->getName() != StringRef(".")) {
1380 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001381 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001382 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001384 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001385
Mehdi Amini57a41912015-09-10 01:46:39 +00001386 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001387 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1388 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001389
1390 // Write out all other paths relative to the base directory if possible.
1391 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1392 } else if (!isysroot.empty()) {
1393 // Write out paths relative to the sysroot if possible.
1394 BaseDirectory = isysroot;
1395 }
1396
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001397 // Module map file
1398 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001399 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001400
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001401 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1402
1403 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001404 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001405
1406 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001407 if (auto *AdditionalModMaps =
1408 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001409 Record.push_back(AdditionalModMaps->size());
1410 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001411 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001412 } else {
1413 Record.push_back(0);
1414 }
1415
1416 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001417 }
1418
Douglas Gregor112b9072012-10-18 05:31:06 +00001419 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001420 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001421 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001422 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001423
Richard Smith7f330cd2015-03-18 01:42:29 +00001424 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001425 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001426 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001427 continue;
1428
Richard Smith7f330cd2015-03-18 01:42:29 +00001429 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1430 AddSourceLocation(M->ImportLoc, Record);
1431 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001432 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001433 Record.push_back(M->Signature);
1434 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001435 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001436 Stream.EmitRecord(IMPORTS, Record);
1437 }
Mike Stump11289f42009-09-09 15:08:12 +00001438
Richard Smith0516b182015-09-08 19:40:14 +00001439 // Write the options block.
1440 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1441
Douglas Gregor112b9072012-10-18 05:31:06 +00001442 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001443 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001444 const LangOptions &LangOpts = Context.getLangOpts();
1445#define LANGOPT(Name, Bits, Default, Description) \
1446 Record.push_back(LangOpts.Name);
1447#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1448 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001449#include "clang/Basic/LangOptions.def"
1450#define SANITIZER(NAME, ID) \
1451 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001452#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001453
Ben Langmuircd98cb72015-06-23 18:20:18 +00001454 Record.push_back(LangOpts.ModuleFeatures.size());
1455 for (StringRef Feature : LangOpts.ModuleFeatures)
1456 AddString(Feature, Record);
1457
Douglas Gregor112b9072012-10-18 05:31:06 +00001458 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1459 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001460
1461 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001462
1463 // Comment options.
1464 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001465 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1466 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001467 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001468 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001469
Samuel Antaoee8fb302016-01-06 13:42:12 +00001470 // OpenMP offloading options.
1471 Record.push_back(LangOpts.OMPTargetTriples.size());
1472 for (auto &T : LangOpts.OMPTargetTriples)
1473 AddString(T.getTriple(), Record);
1474
1475 AddString(LangOpts.OMPHostIRFile, Record);
1476
Douglas Gregor112b9072012-10-18 05:31:06 +00001477 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1478
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001479 // Target options.
1480 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001481 const TargetInfo &Target = Context.getTargetInfo();
1482 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001483 AddString(TargetOpts.Triple, Record);
1484 AddString(TargetOpts.CPU, Record);
1485 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001486 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1487 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1488 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1489 }
1490 Record.push_back(TargetOpts.Features.size());
1491 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1492 AddString(TargetOpts.Features[I], Record);
1493 }
1494 Stream.EmitRecord(TARGET_OPTIONS, Record);
1495
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001496 // Diagnostic options.
1497 Record.clear();
1498 const DiagnosticOptions &DiagOpts
1499 = Context.getDiagnostics().getDiagnosticOptions();
1500#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1501#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1502 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1503#include "clang/Basic/DiagnosticOptions.def"
1504 Record.push_back(DiagOpts.Warnings.size());
1505 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1506 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001507 Record.push_back(DiagOpts.Remarks.size());
1508 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1509 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001510 // Note: we don't serialize the log or serialization file names, because they
1511 // are generally transient files and will almost always be overridden.
1512 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1513
Douglas Gregorc6317db2012-10-24 15:49:58 +00001514 // File system options.
1515 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001516 const FileSystemOptions &FSOpts =
1517 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001518 AddString(FSOpts.WorkingDir, Record);
1519 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1520
Douglas Gregor2d302362012-10-24 16:50:34 +00001521 // Header search options.
1522 Record.clear();
1523 const HeaderSearchOptions &HSOpts
1524 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1525 AddString(HSOpts.Sysroot, Record);
1526
1527 // Include entries.
1528 Record.push_back(HSOpts.UserEntries.size());
1529 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1530 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1531 AddString(Entry.Path, Record);
1532 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001533 Record.push_back(Entry.IsFramework);
1534 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001535 }
1536
1537 // System header prefixes.
1538 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1539 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1540 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1541 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1542 }
1543
1544 AddString(HSOpts.ResourceDir, Record);
1545 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001546 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001547 Record.push_back(HSOpts.DisableModuleHash);
1548 Record.push_back(HSOpts.UseBuiltinIncludes);
1549 Record.push_back(HSOpts.UseStandardSystemIncludes);
1550 Record.push_back(HSOpts.UseStandardCXXIncludes);
1551 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001552 // Write out the specific module cache path that contains the module files.
1553 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001554 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1555
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001556 // Preprocessor options.
1557 Record.clear();
1558 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1559
1560 // Macro definitions.
1561 Record.push_back(PPOpts.Macros.size());
1562 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1563 AddString(PPOpts.Macros[I].first, Record);
1564 Record.push_back(PPOpts.Macros[I].second);
1565 }
1566
1567 // Includes
1568 Record.push_back(PPOpts.Includes.size());
1569 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1570 AddString(PPOpts.Includes[I], Record);
1571
1572 // Macro includes
1573 Record.push_back(PPOpts.MacroIncludes.size());
1574 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1575 AddString(PPOpts.MacroIncludes[I], Record);
1576
Douglas Gregorb6368752012-10-24 23:41:50 +00001577 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001578 // Detailed record is important since it is used for the module cache hash.
1579 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001580 AddString(PPOpts.ImplicitPCHInclude, Record);
1581 AddString(PPOpts.ImplicitPTHInclude, Record);
1582 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1583 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1584
Richard Smith0516b182015-09-08 19:40:14 +00001585 // Leave the options block.
1586 Stream.ExitBlock();
1587
Douglas Gregora3b20262011-05-06 21:43:30 +00001588 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001589 SourceManager &SM = Context.getSourceManager();
1590 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001591 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001592 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1593 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001594 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001595 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001596
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001597 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001598 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001599 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001600 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001601 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001602
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001603 Record.clear();
1604 Record.push_back(SM.getMainFileID().getOpaqueValue());
1605 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1606
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001607 // Original PCH directory
1608 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001609 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001610 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001612 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001613
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001614 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001615
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001616 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001617 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1618
Mehdi Amini57a41912015-09-10 01:46:39 +00001619 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001620 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1621 }
1622
Douglas Gregor49491f72013-03-15 22:15:07 +00001623 WriteInputFiles(Context.SourceMgr,
1624 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001625 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001626 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001627 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001628}
1629
Douglas Gregor49491f72013-03-15 22:15:07 +00001630namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001631
Douglas Gregor49491f72013-03-15 22:15:07 +00001632 /// \brief An input file.
1633 struct InputFileEntry {
1634 const FileEntry *File;
1635 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001636 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001637 bool BufferOverridden;
1638 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001639
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001640} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001641
1642void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1643 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001644 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001645 using namespace llvm;
1646 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001647
Douglas Gregor72be3902012-10-19 00:38:02 +00001648 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001649 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001650 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001651 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001652 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1653 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001654 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001655 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001656 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001657 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001658
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001659 // Get all ContentCache objects for files, sorted by whether the file is a
1660 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001661 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001662 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1663 // Get this source location entry.
1664 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001665 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001666
1667 // We only care about file entries that were not overridden.
1668 if (!SLoc->isFile())
1669 continue;
1670 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001671 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001672 continue;
1673
Douglas Gregor49491f72013-03-15 22:15:07 +00001674 InputFileEntry Entry;
1675 Entry.File = Cache->OrigEntry;
1676 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001677 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001678 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001679 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001680 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001681 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001682 SortedFiles.push_front(Entry);
1683 }
1684
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001685 unsigned UserFilesNum = 0;
1686 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001687 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001688 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001689 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001690 if (InputFileID != 0)
1691 continue; // already recorded this file.
1692
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001693 // Record this entry's offset.
1694 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001695
1696 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001697
Douglas Gregor49491f72013-03-15 22:15:07 +00001698 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001699 ++UserFilesNum;
1700
Douglas Gregor72be3902012-10-19 00:38:02 +00001701 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001702 // And whether this file was overridden.
1703 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001704 INPUT_FILE,
1705 InputFileOffsets.size(),
1706 (uint64_t)Entry.File->getSize(),
1707 (uint64_t)getTimestampForOutput(Entry.File),
1708 Entry.BufferOverridden,
1709 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001710
Richard Smith7ed1bc92014-12-05 22:42:13 +00001711 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1712 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001713
Douglas Gregor112b9072012-10-18 05:31:06 +00001714 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001715
1716 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001717 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001718 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1719 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001720 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1721 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001722 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001723 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001724
1725 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001726 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1727 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001728 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001729}
1730
Douglas Gregora7f71a92009-04-10 03:52:48 +00001731//===----------------------------------------------------------------------===//
1732// Source Manager Serialization
1733//===----------------------------------------------------------------------===//
1734
1735/// \brief Create an abbreviation for the SLocEntry that refers to a
1736/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001737static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001738 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001739
David Blaikieb44f0bf2017-01-04 22:36:43 +00001740 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001741 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001746 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001751 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001752}
1753
1754/// \brief Create an abbreviation for the SLocEntry that refers to a
1755/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001756static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001757 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001758
David Blaikieb44f0bf2017-01-04 22:36:43 +00001759 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001760 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001766 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001767}
1768
1769/// \brief Create an abbreviation for the SLocEntry that refers to a
1770/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001771static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1772 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001773 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001774
David Blaikieb44f0bf2017-01-04 22:36:43 +00001775 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001776 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1777 : SM_SLOC_BUFFER_BLOB));
1778 if (Compressed)
1779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001781 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001782}
1783
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001784/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1785/// expansion.
1786static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001787 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001788
David Blaikieb44f0bf2017-01-04 22:36:43 +00001789 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001790 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001796 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001797}
1798
Douglas Gregor09b69892011-02-10 17:09:37 +00001799namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001800
Douglas Gregor09b69892011-02-10 17:09:37 +00001801 // Trait used for the on-disk hash table of header search information.
1802 class HeaderFileInfoTrait {
1803 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001804 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001805
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001806 // Keep track of the framework names we've used during serialization.
1807 SmallVector<char, 128> FrameworkStringData;
1808 llvm::StringMap<unsigned> FrameworkNameOffset;
1809
Douglas Gregor09b69892011-02-10 17:09:37 +00001810 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001811 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1812 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001813
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001814 struct key_type {
1815 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001816 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001817 };
1818 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001819
1820 typedef HeaderFileInfo data_type;
1821 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001822 typedef unsigned hash_value_type;
1823 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001824
Richard Smithe75ee0f2015-08-17 07:13:32 +00001825 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001826 // The hash is based only on size/time of the file, so that the reader can
1827 // match even when symlinking or excess path elements ("foo/../", "../")
1828 // change the form of the name. However, complete path is still the key.
1829 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001830 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001831 }
1832
1833 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001834 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001835 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001836 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001837 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001838 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001839 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001840 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1841 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1842 DataLen += 4;
1843 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001844 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001845 }
1846
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001847 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001848 using namespace llvm::support;
1849 endian::Writer<little> LE(Out);
1850 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001851 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001852 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001853 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001854 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001855 }
1856
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001857 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001858 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001859 using namespace llvm::support;
1860 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001861 uint64_t Start = Out.tell(); (void)Start;
1862
Richard Smith386bb072015-08-18 23:42:23 +00001863 unsigned char Flags = (Data.isImport << 4)
1864 | (Data.isPragmaOnce << 3)
1865 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001866 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001867 LE.write<uint8_t>(Flags);
1868 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001869
1870 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001871 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001872 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001873 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001874
1875 unsigned Offset = 0;
1876 if (!Data.Framework.empty()) {
1877 // If this header refers into a framework, save the framework name.
1878 llvm::StringMap<unsigned>::iterator Pos
1879 = FrameworkNameOffset.find(Data.Framework);
1880 if (Pos == FrameworkNameOffset.end()) {
1881 Offset = FrameworkStringData.size() + 1;
1882 FrameworkStringData.append(Data.Framework.begin(),
1883 Data.Framework.end());
1884 FrameworkStringData.push_back(0);
1885
1886 FrameworkNameOffset[Data.Framework] = Offset;
1887 } else
1888 Offset = Pos->second;
1889 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001890 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001891
Richard Smith386bb072015-08-18 23:42:23 +00001892 // FIXME: If the header is excluded, we should write out some
1893 // record of that fact.
1894 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1895 if (uint32_t ModID =
1896 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1897 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1898 assert((Value >> 2) == ModID && "overflow in header module info");
1899 LE.write<uint32_t>(Value);
1900 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001901 }
1902
Douglas Gregor09b69892011-02-10 17:09:37 +00001903 assert(Out.tell() - Start == DataLen && "Wrong data length");
1904 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001905
1906 const char *strings_begin() const { return FrameworkStringData.begin(); }
1907 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001908 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001909
Douglas Gregor09b69892011-02-10 17:09:37 +00001910} // end anonymous namespace
1911
1912/// \brief Write the header search block for the list of files that
1913///
1914/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001915void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001916 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001917 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1918
1919 if (FilesByUID.size() > HS.header_file_size())
1920 FilesByUID.resize(HS.header_file_size());
1921
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001922 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001923 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001924 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001925 unsigned NumHeaderSearchEntries = 0;
1926 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1927 const FileEntry *File = FilesByUID[UID];
1928 if (!File)
1929 continue;
1930
Richard Smith386bb072015-08-18 23:42:23 +00001931 // Get the file info. This will load info from the external source if
1932 // necessary. Skip emitting this file if we have no information on it
1933 // as a header file (in which case HFI will be null) or if it hasn't
1934 // changed since it was loaded. Also skip it if it's for a modular header
1935 // from a different module; in that case, we rely on the module(s)
1936 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001937 const HeaderFileInfo *HFI =
1938 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1939 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001940 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001941
Richard Smith7ed1bc92014-12-05 22:42:13 +00001942 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001943 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001944 SmallString<128> FilenameTmp(Filename);
1945 if (PreparePathForOutput(FilenameTmp)) {
1946 // If we performed any translation on the file name at all, we need to
1947 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001948 Filename = StringRef(strdup(FilenameTmp.c_str()));
1949 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001950 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001951
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001952 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001953 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001954 ++NumHeaderSearchEntries;
1955 }
1956
1957 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001958 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001959 uint32_t BucketOffset;
1960 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001961 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001962 llvm::raw_svector_ostream Out(TableData);
1963 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001964 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001965 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1966 }
1967
1968 // Create a blob abbreviation
1969 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001970
David Blaikieb44f0bf2017-01-04 22:36:43 +00001971 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00001972 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00001977 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00001978
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001979 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001980 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1981 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001982 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001983 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001984
1985 // Free all of the strings we had to duplicate.
1986 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001987 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001988}
1989
George Rimarc39f5492017-01-17 15:45:31 +00001990static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
1991 unsigned SLocBufferBlobCompressedAbbrv,
1992 unsigned SLocBufferBlobAbbrv) {
1993 typedef ASTWriter::RecordData::value_type RecordDataType;
1994
1995 // Compress the buffer if possible. We expect that almost all PCM
1996 // consumers will not want its contents.
1997 SmallString<0> CompressedBuffer;
1998 if (llvm::zlib::isAvailable()) {
1999 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2000 if (!E) {
2001 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2002 Blob.size() - 1};
2003 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2004 CompressedBuffer);
2005 return;
2006 }
2007 llvm::consumeError(std::move(E));
2008 }
2009
2010 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2011 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2012}
2013
Douglas Gregora7f71a92009-04-10 03:52:48 +00002014/// \brief Writes the block containing the serialized form of the
2015/// source manager.
2016///
2017/// TODO: We should probably use an on-disk hash table (stored in a
2018/// blob), indexed based on the file name, so that we only create
2019/// entries for files that we actually need. In the common case (no
2020/// errors), we probably won't have to create file entries for any of
2021/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002022void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002023 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002024 RecordData Record;
2025
Chris Lattner0910e3b2009-04-10 17:16:57 +00002026 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002027 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002028
2029 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002030 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2031 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002032 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2033 unsigned SLocBufferBlobCompressedAbbrv =
2034 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002035 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002036
Douglas Gregor258ae542009-04-27 06:38:32 +00002037 // Write out the source location entry table. We skip the first
2038 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002039 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002040 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002041 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2042 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002043 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002044 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002045 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002046 FileID FID = FileID::get(I);
2047 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002048
Douglas Gregor258ae542009-04-27 06:38:32 +00002049 // Record the offset of this source-location entry.
2050 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2051
2052 // Figure out which record code to use.
2053 unsigned Code;
2054 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002055 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2056 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002057 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002058 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002059 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002060 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002061 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002062 Record.clear();
2063 Record.push_back(Code);
2064
Douglas Gregor925296b2011-07-19 16:10:42 +00002065 // Starting offset of this entry within this module, so skip the dummy.
2066 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002067 if (SLoc->isFile()) {
2068 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002069 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002070 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2071 Record.push_back(File.hasLineDirectives());
2072
2073 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002074 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002075 if (Content->OrigEntry) {
2076 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002077 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002078
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002079 // The source location entry is a file. Emit input file ID.
2080 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2081 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002082
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002083 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002084
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002085 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002086 if (FDI != FileDeclIDs.end()) {
2087 Record.push_back(FDI->second->FirstDeclIndex);
2088 Record.push_back(FDI->second->DeclIDs.size());
2089 } else {
2090 Record.push_back(0);
2091 Record.push_back(0);
2092 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002093
2094 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2095
Richard Smithaada85c2016-02-06 02:06:43 +00002096 if (Content->BufferOverridden || Content->IsTransient)
2097 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002098 } else {
2099 // The source location entry is a buffer. The blob associated
2100 // with this entry contains the contents of the buffer.
2101
2102 // We add one to the size so that we capture the trailing NULL
2103 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2104 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002105 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002106 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002107 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002108 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002109 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002110 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002111
Mehdi Amini99d1b292016-10-01 16:38:28 +00002112 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002113 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002114 }
Richard Smithaada85c2016-02-06 02:06:43 +00002115
2116 if (EmitBlob) {
2117 // Include the implicit terminating null character in the on-disk buffer
2118 // if we're writing it uncompressed.
2119 const llvm::MemoryBuffer *Buffer =
2120 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2121 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002122 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2123 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002124 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002125 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002126 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002127 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002128 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2129 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2130 AddSourceLocation(Expansion.isMacroArgExpansion()
2131 ? SourceLocation()
2132 : Expansion.getExpansionLocEnd(),
2133 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002134
2135 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002136 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002137 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002138 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002139 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002140 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002141 }
2142 }
2143
Douglas Gregor8f45df52009-04-16 22:23:12 +00002144 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002145
2146 if (SLocEntryOffsets.empty())
2147 return;
2148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002149 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002150 // table is used for lazily loading source-location information.
2151 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002152
David Blaikieb44f0bf2017-01-04 22:36:43 +00002153 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002154 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002158 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002159 {
2160 RecordData::value_type Record[] = {
2161 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2162 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2163 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2164 bytes(SLocEntryOffsets));
2165 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002166 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002167 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002168 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002169
2170 // Write the line table. It depends on remapping working, so it must come
2171 // after the source location offsets.
2172 if (SourceMgr.hasLineTable()) {
2173 LineTableInfo &LineTable = SourceMgr.getLineTable();
2174
2175 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002176
2177 // Emit the needed file names.
2178 llvm::DenseMap<int, int> FilenameMap;
2179 for (const auto &L : LineTable) {
2180 if (L.first.ID < 0)
2181 continue;
2182 for (auto &LE : L.second) {
2183 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2184 FilenameMap.size())).second)
2185 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2186 }
2187 }
2188 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002189
2190 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002191 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002192 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002193 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002194 continue;
2195
2196 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002197 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002198
2199 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002200 Record.push_back(L.second.size());
2201 for (const auto &LE : L.second) {
2202 Record.push_back(LE.FileOffset);
2203 Record.push_back(LE.LineNo);
2204 Record.push_back(FilenameMap[LE.FilenameID]);
2205 Record.push_back((unsigned)LE.FileKind);
2206 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002207 }
2208 }
Richard Smith63078492015-09-01 07:41:55 +00002209
Douglas Gregor925296b2011-07-19 16:10:42 +00002210 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2211 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002212}
2213
Douglas Gregorc5046832009-04-27 18:38:38 +00002214//===----------------------------------------------------------------------===//
2215// Preprocessor Serialization
2216//===----------------------------------------------------------------------===//
2217
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002218static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2219 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002220 if (MacroInfo *MI = MD->getMacroInfo())
2221 if (MI->isBuiltinMacro())
2222 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002223
2224 if (IsModule) {
2225 SourceLocation Loc = MD->getLocation();
2226 if (Loc.isInvalid())
2227 return true;
2228 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2229 return true;
2230 }
2231
2232 return false;
2233}
2234
Chris Lattnereeffaef2009-04-10 17:15:23 +00002235/// \brief Writes the block containing the serialized form of the
2236/// preprocessor.
2237///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002238void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002239 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2240 if (PPRec)
2241 WritePreprocessorDetail(*PPRec);
2242
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002243 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002244 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002245
Chris Lattner0af3ba12009-04-13 01:29:17 +00002246 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2247 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002248 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002249 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002250 }
2251
2252 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002253 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002254
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002255 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002256 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002257 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002258 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002259
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002260 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002261 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002262
Richard Smithee977932015-05-01 21:22:17 +00002263 // Construct the list of identifiers with macro directives that need to be
2264 // serialized.
2265 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2266 for (auto &Id : PP.getIdentifierTable())
2267 if (Id.second->hadMacroDefinition() &&
2268 (!Id.second->isFromAST() ||
2269 Id.second->hasChangedSinceDeserialization()))
2270 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002271 // Sort the set of macro definitions that need to be serialized by the
2272 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002273 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2274 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002275
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002276 // Emit the macro directives as a list and associate the offset with the
2277 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002278 for (const IdentifierInfo *Name : MacroIdentifiers) {
2279 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002280 auto StartOffset = Stream.GetCurrentBitNo();
2281
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002282 // Emit the macro directives in reverse source order.
2283 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002284 // Once we hit an ignored macro, we're done: the rest of the chain
2285 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002286 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002287 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002288
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002289 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002290 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002291 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002292 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002293 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002294 Record.push_back(VisMD->isPublic());
2295 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002296 }
Richard Smithd7329392015-04-21 21:46:32 +00002297
Richard Smithb8b2ed62015-04-23 18:18:26 +00002298 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002299 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002300 // We write out exported module macros for PCH as well.
2301 auto Leafs = PP.getLeafModuleMacros(Name);
2302 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2303 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2304 while (!Worklist.empty()) {
2305 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002306
Manman Ren33d80292016-05-31 18:19:32 +00002307 // Emit a record indicating this submodule exports this macro.
2308 ModuleMacroRecord.push_back(
2309 getSubmoduleID(Macro->getOwningModule()));
2310 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2311 for (auto *M : Macro->overrides())
2312 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002313
Manman Ren33d80292016-05-31 18:19:32 +00002314 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2315 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002316
Manman Ren33d80292016-05-31 18:19:32 +00002317 // Enqueue overridden macros once we've visited all their ancestors.
2318 for (auto *M : Macro->overrides())
2319 if (++Visits[M] == M->getNumOverridingMacros())
2320 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002321
Manman Ren33d80292016-05-31 18:19:32 +00002322 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002323 }
Richard Smithd7329392015-04-21 21:46:32 +00002324
Richard Smithee977932015-05-01 21:22:17 +00002325 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002326 continue;
2327
Richard Smithd7329392015-04-21 21:46:32 +00002328 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002329 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2330 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002331 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002332
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002333 /// \brief Offsets of each of the macros into the bitstream, indexed by
2334 /// the local macro ID
2335 ///
2336 /// For each identifier that is associated with a macro, this map
2337 /// provides the offset into the bitstream where that macro is
2338 /// defined.
2339 std::vector<uint32_t> MacroOffsets;
2340
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002341 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2342 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2343 MacroInfo *MI = MacroInfosToEmit[I].MI;
2344 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002345
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002346 if (ID < FirstMacroID) {
2347 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2348 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002349 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002350
2351 // Record the local offset of this macro.
2352 unsigned Index = ID - FirstMacroID;
2353 if (Index == MacroOffsets.size())
2354 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2355 else {
2356 if (Index > MacroOffsets.size())
2357 MacroOffsets.resize(Index + 1);
2358
2359 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2360 }
2361
2362 AddIdentifierRef(Name, Record);
2363 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2364 AddSourceLocation(MI->getDefinitionLoc(), Record);
2365 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2366 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002367 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002368 unsigned Code;
2369 if (MI->isObjectLike()) {
2370 Code = PP_MACRO_OBJECT_LIKE;
2371 } else {
2372 Code = PP_MACRO_FUNCTION_LIKE;
2373
2374 Record.push_back(MI->isC99Varargs());
2375 Record.push_back(MI->isGNUVarargs());
2376 Record.push_back(MI->hasCommaPasting());
2377 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002378 for (const IdentifierInfo *Arg : MI->args())
2379 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002380 }
2381
2382 // If we have a detailed preprocessing record, record the macro definition
2383 // ID that corresponds to this macro.
2384 if (PPRec)
2385 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2386
2387 Stream.EmitRecord(Code, Record);
2388 Record.clear();
2389
2390 // Emit the tokens array.
2391 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2392 // Note that we know that the preprocessor does not have any annotation
2393 // tokens in it because they are created by the parser, and thus can't
2394 // be in a macro definition.
2395 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002396 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002397 Stream.EmitRecord(PP_TOKEN, Record);
2398 Record.clear();
2399 }
2400 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002401 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002402
Douglas Gregor92a96f52011-02-08 21:58:10 +00002403 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002404
2405 // Write the offsets table for macro IDs.
2406 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002407
David Blaikieb44f0bf2017-01-04 22:36:43 +00002408 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002409 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2413
David Blaikieb44f0bf2017-01-04 22:36:43 +00002414 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002415 {
2416 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2417 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2418 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2419 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002420}
2421
2422void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002423 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002424 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002425
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002426 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002427
Douglas Gregor92a96f52011-02-08 21:58:10 +00002428 // Enter the preprocessor block.
2429 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002430
Douglas Gregoraae92242010-03-19 21:51:54 +00002431 // If the preprocessor has a preprocessing record, emit it.
2432 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002433 using namespace llvm;
2434
2435 // Set up the abbreviation for
2436 unsigned InclusionAbbrev = 0;
2437 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002438 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002439 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002440 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002445 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002446 }
2447
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002448 unsigned FirstPreprocessorEntityID
2449 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2450 + NUM_PREDEF_PP_ENTITY_IDS;
2451 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002452 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002453 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2454 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002455 E != EEnd;
2456 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002457 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002458
Richard Smith66a81862015-05-04 02:25:31 +00002459 PreprocessedEntityOffsets.push_back(
2460 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002461
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002462 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002463 // Record this macro definition's ID.
2464 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002465
Douglas Gregor92a96f52011-02-08 21:58:10 +00002466 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002467 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2468 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002469 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002470
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002471 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002472 Record.push_back(ME->isBuiltinMacro());
2473 if (ME->isBuiltinMacro())
2474 AddIdentifierRef(ME->getName(), Record);
2475 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002476 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002477 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002478 continue;
2479 }
2480
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002481 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002482 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002483 Record.push_back(ID->getFileName().size());
2484 Record.push_back(ID->wasInQuotes());
2485 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002486 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002487 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002488 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002489 // Check that the FileEntry is not null because it was not resolved and
2490 // we create a PCH even with compiler errors.
2491 if (ID->getFile())
2492 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002493 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002494 continue;
2495 }
2496
2497 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2498 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002499 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002500
Douglas Gregoraae92242010-03-19 21:51:54 +00002501 // Write the offsets table for the preprocessing record.
2502 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002503 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2504
Douglas Gregoraae92242010-03-19 21:51:54 +00002505 // Write the offsets table for identifier IDs.
2506 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002507
David Blaikieb44f0bf2017-01-04 22:36:43 +00002508 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002509 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002512 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002513
Mehdi Amini57a41912015-09-10 01:46:39 +00002514 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2515 FirstPreprocessorEntityID -
2516 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002517 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002518 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002519 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002520}
2521
Richard Smith386bb072015-08-18 23:42:23 +00002522unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002523 if (!Mod)
2524 return 0;
2525
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002526 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2527 if (Known != SubmoduleIDs.end())
2528 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002529
Richard Smithdc1f0422016-07-20 19:10:16 +00002530 auto *Top = Mod->getTopLevelModule();
2531 if (Top != WritingModule &&
2532 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule)))
Richard Smith386bb072015-08-18 23:42:23 +00002533 return 0;
2534
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002535 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2536}
2537
Richard Smith386bb072015-08-18 23:42:23 +00002538unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2539 // FIXME: This can easily happen, if we have a reference to a submodule that
2540 // did not result in us loading a module file for that submodule. For
2541 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2542 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2543 assert((ID || !Mod) &&
2544 "asked for module ID for non-local, non-imported module");
2545 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002546}
2547
Douglas Gregor253eefe2011-12-01 00:59:36 +00002548/// \brief Compute the number of modules within the given tree (including the
2549/// given module).
2550static unsigned getNumberOfModules(Module *Mod) {
2551 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002552 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002553 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002554 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002555
2556 return ChildModules + 1;
2557}
2558
Douglas Gregorde3ef502011-11-30 23:21:26 +00002559void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002560 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002561 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002562
2563 // Write the abbreviations needed for the submodules block.
2564 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002565
David Blaikieb44f0bf2017-01-04 22:36:43 +00002566 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002567 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002579 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002580
David Blaikieb44f0bf2017-01-04 22:36:43 +00002581 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002582 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002584 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002585
David Blaikieb44f0bf2017-01-04 22:36:43 +00002586 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002587 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002589 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002590
David Blaikieb44f0bf2017-01-04 22:36:43 +00002591 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002592 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002594 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002595
David Blaikieb44f0bf2017-01-04 22:36:43 +00002596 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002597 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002599 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002600
David Blaikieb44f0bf2017-01-04 22:36:43 +00002601 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002602 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002605 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002606
David Blaikieb44f0bf2017-01-04 22:36:43 +00002607 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002608 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002610 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002611
David Blaikieb44f0bf2017-01-04 22:36:43 +00002612 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002613 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002615 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002616
David Blaikieb44f0bf2017-01-04 22:36:43 +00002617 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002618 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002620 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002621
David Blaikieb44f0bf2017-01-04 22:36:43 +00002622 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002623 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002625 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002626
David Blaikieb44f0bf2017-01-04 22:36:43 +00002627 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002628 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002631 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002632
David Blaikieb44f0bf2017-01-04 22:36:43 +00002633 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002634 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002636 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002637
David Blaikieb44f0bf2017-01-04 22:36:43 +00002638 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002639 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2640 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002642 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002643
Douglas Gregor253eefe2011-12-01 00:59:36 +00002644 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002645 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2646 FirstSubmoduleID -
2647 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002648 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2649
Douglas Gregor69021972011-11-30 17:33:56 +00002650 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002651 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002652 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002653 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002654 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002655 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002656 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002657
2658 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002659 if (Mod->Parent) {
2660 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002661 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002662 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002663
2664 // Emit the definition of the block.
2665 {
2666 RecordData::value_type Record[] = {
2667 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2668 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2669 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2670 Mod->ConfigMacrosExhaustive};
2671 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2672 }
2673
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002674 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002675 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002676 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002677 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002678 }
2679
Douglas Gregor69021972011-11-30 17:33:56 +00002680 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002681 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002682 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002683 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2684 UmbrellaHeader.NameAsWritten);
2685 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002686 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2687 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002688 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002689 }
Richard Smith306d8922014-10-22 23:50:56 +00002690
Douglas Gregor69021972011-11-30 17:33:56 +00002691 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002692 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002693 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002694 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002695 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002696 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002697 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2698 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2699 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002700 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002701 Module::HK_PrivateTextual},
2702 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002703 };
2704 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002705 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002706 for (auto &H : Mod->Headers[HL.HeaderKind])
2707 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2708 }
2709
2710 // Emit the top headers.
2711 {
2712 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002713 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002714 for (auto *H : TopHeaders)
2715 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002716 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002717
2718 // Emit the imports.
2719 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002720 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002721 for (auto *I : Mod->Imports)
2722 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002723 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2724 }
2725
Douglas Gregor24bb9232011-12-02 18:58:38 +00002726 // Emit the exports.
2727 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002728 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002729 for (const auto &E : Mod->Exports) {
2730 // FIXME: This may fail; we don't require that all exported modules
2731 // are local or imported.
2732 Record.push_back(getSubmoduleID(E.getPointer()));
2733 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002734 }
2735 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2736 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002737
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002738 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2739 // Might be unnecessary as use declarations are only used to build the
2740 // module itself.
2741
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002742 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002743 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002744 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2745 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002746 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002747 }
2748
Douglas Gregorfb912652013-03-20 21:10:35 +00002749 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002750 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002751 // FIXME: This may fail; we don't require that all conflicting modules
2752 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002753 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2754 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002755 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002756 }
2757
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002758 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002759 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002760 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002761 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002762 }
2763
Richard Smithdc1f0422016-07-20 19:10:16 +00002764 // Emit the initializers, if any.
2765 RecordData Inits;
2766 for (Decl *D : Context->getModuleInitializers(Mod))
2767 Inits.push_back(GetDeclRef(D));
2768 if (!Inits.empty())
2769 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2770
Douglas Gregor69021972011-11-30 17:33:56 +00002771 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002772 for (auto *M : Mod->submodules())
2773 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002774 }
2775
2776 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002777
Richard Smith23d8d032015-05-14 00:45:20 +00002778 assert((NextSubmoduleID - FirstSubmoduleID ==
2779 getNumberOfModules(WritingModule)) &&
2780 "Wrong # of submodules; found a reference to a non-local, "
2781 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002782}
2783
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002784serialization::SubmoduleID
2785ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002786 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002787 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002788
2789 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002790 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002791 Module *OwningMod
2792 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002793 if (!OwningMod)
2794 return 0;
2795
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002796 // Check whether this submodule is part of our own module.
2797 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002798 return 0;
2799
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002800 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002801}
2802
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002803void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2804 bool isModule) {
2805 // Make sure set diagnostic pragmas don't affect the translation unit that
2806 // imports the module.
2807 // FIXME: Make diagnostic pragma sections work properly with modules.
2808 if (isModule)
2809 return;
2810
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002811 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2812 DiagStateIDMap;
2813 unsigned CurrID = 0;
2814 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002815 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002816 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002817 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2818 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002819 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002820 if (point.Loc.isInvalid())
2821 continue;
2822
Richard Smithb22a1d12016-03-27 20:13:24 +00002823 AddSourceLocation(point.Loc, Record);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002824 unsigned &DiagStateID = DiagStateIDMap[point.State];
2825 Record.push_back(DiagStateID);
2826
2827 if (DiagStateID == 0) {
2828 DiagStateID = ++CurrID;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002829 for (const auto &I : *(point.State)) {
2830 if (I.second.isPragma()) {
2831 Record.push_back(I.first);
2832 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002833 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002834 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002835 Record.push_back(-1); // mark the end of the diag/map pairs for this
2836 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002837 }
2838 }
2839
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002840 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002841 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002842}
2843
Douglas Gregorc5046832009-04-27 18:38:38 +00002844//===----------------------------------------------------------------------===//
2845// Type Serialization
2846//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002847
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002848/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002849void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002850 TypeIdx &IdxRef = TypeIdxs[T];
2851 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2852 IdxRef = TypeIdx(NextTypeID++);
2853 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002854
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002855 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002856
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002857 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002858
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002859 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002860 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002861 W.Visit(T);
2862 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002863
Richard Smith290d8012016-04-06 17:06:00 +00002864 // Record the offset for this type.
2865 unsigned Index = Idx.getIndex() - FirstTypeID;
2866 if (TypeOffsets.size() == Index)
2867 TypeOffsets.push_back(Offset);
2868 else if (TypeOffsets.size() < Index) {
2869 TypeOffsets.resize(Index + 1);
2870 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002871 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002872 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002873 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002874}
2875
Douglas Gregorc5046832009-04-27 18:38:38 +00002876//===----------------------------------------------------------------------===//
2877// Declaration Serialization
2878//===----------------------------------------------------------------------===//
2879
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002880/// \brief Write the block containing all of the declaration IDs
2881/// lexically declared within the given DeclContext.
2882///
2883/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2884/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002885uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002886 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002887 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002888 return 0;
2889
Douglas Gregor8f45df52009-04-16 22:23:12 +00002890 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002891 SmallVector<uint32_t, 128> KindDeclPairs;
2892 for (const auto *D : DC->decls()) {
2893 KindDeclPairs.push_back(D->getKind());
2894 KindDeclPairs.push_back(GetDeclRef(D));
2895 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002896
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002897 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002898 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002899 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2900 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002901 return Offset;
2902}
2903
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002904void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002905 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002906
2907 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002908 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002909 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002913 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002914 {
2915 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2916 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2917 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2918 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002919
2920 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002921 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002922 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002926 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002927 {
2928 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2929 FirstDeclID - NUM_PREDEF_DECL_IDS};
2930 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2931 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002932}
2933
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002934void ASTWriter::WriteFileDeclIDsMap() {
2935 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002936
Chandler Carruthb306d732015-03-27 00:31:20 +00002937 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2938 FileDeclIDs.begin(), FileDeclIDs.end());
2939 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2940 llvm::less_first());
2941
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002942 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002943 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2944 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2945 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2946 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2947 for (auto &LocDeclEntry : Info.DeclIDs)
2948 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002949 }
2950
David Blaikieb44f0bf2017-01-04 22:36:43 +00002951 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002952 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002955 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002956 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2957 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002958 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002959}
2960
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002961void ASTWriter::WriteComments() {
2962 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002963 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002964 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002965 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002966 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002967 AddSourceRange(I->getSourceRange(), Record);
2968 Record.push_back(I->getKind());
2969 Record.push_back(I->isTrailingComment());
2970 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002971 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2972 }
2973 Stream.ExitBlock();
2974}
2975
Douglas Gregorc5046832009-04-27 18:38:38 +00002976//===----------------------------------------------------------------------===//
2977// Global Method Pool and Selector Serialization
2978//===----------------------------------------------------------------------===//
2979
Douglas Gregore84a9da2009-04-20 20:36:09 +00002980namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002981
Douglas Gregorc78d3462009-04-24 21:10:55 +00002982// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002983class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002984 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002985
2986public:
2987 typedef Selector key_type;
2988 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002989
Sebastian Redl834bb972010-08-04 17:20:04 +00002990 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002991 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002992 ObjCMethodList Instance, Factory;
2993 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002994 typedef const data_type& data_type_ref;
2995
Justin Bogner25463f12014-04-18 20:27:24 +00002996 typedef unsigned hash_value_type;
2997 typedef unsigned offset_type;
2998
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002999 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003000
Justin Bogner25463f12014-04-18 20:27:24 +00003001 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003002 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003003 }
Mike Stump11289f42009-09-09 15:08:12 +00003004
3005 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003006 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003007 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003008 using namespace llvm::support;
3009 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003010 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003011 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003012 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3013 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003014 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003015 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003016 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003017 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003018 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003019 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003020 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003021 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003022 return std::make_pair(KeyLen, DataLen);
3023 }
Mike Stump11289f42009-09-09 15:08:12 +00003024
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003025 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003026 using namespace llvm::support;
3027 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003028 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003029 assert((Start >> 32) == 0 && "Selector key offset too large");
3030 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003031 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003032 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003033 if (N == 0)
3034 N = 1;
3035 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003036 LE.write<uint32_t>(
3037 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003038 }
Mike Stump11289f42009-09-09 15:08:12 +00003039
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003040 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003041 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003042 using namespace llvm::support;
3043 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003044 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003045 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003046 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003047 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003048 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003049 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003050 ++NumInstanceMethods;
3051
3052 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003053 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003054 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003055 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003056 ++NumFactoryMethods;
3057
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003058 unsigned InstanceBits = Methods.Instance.getBits();
3059 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003060 unsigned InstanceHasMoreThanOneDeclBit =
3061 Methods.Instance.hasMoreThanOneDecl();
3062 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3063 (InstanceHasMoreThanOneDeclBit << 2) |
3064 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003065 unsigned FactoryBits = Methods.Factory.getBits();
3066 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003067 unsigned FactoryHasMoreThanOneDeclBit =
3068 Methods.Factory.hasMoreThanOneDecl();
3069 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3070 (FactoryHasMoreThanOneDeclBit << 2) |
3071 FactoryBits;
3072 LE.write<uint16_t>(FullInstanceBits);
3073 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003074 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003075 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003076 if (Method->getMethod())
3077 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003078 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003079 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003080 if (Method->getMethod())
3081 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003082
3083 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003084 }
3085};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003086
Douglas Gregorc78d3462009-04-24 21:10:55 +00003087} // end anonymous namespace
3088
Sebastian Redla19a67f2010-08-03 21:58:15 +00003089/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003090///
3091/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003092/// in an on-disk hash table indexed by the selector. The hash table also
3093/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003094void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003095 using namespace llvm;
3096
Sebastian Redla19a67f2010-08-03 21:58:15 +00003097 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003098 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003099 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003100 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003101 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003102 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003103 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003104 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003105
Sebastian Redla19a67f2010-08-03 21:58:15 +00003106 // Create the on-disk hash table representation. We walk through every
3107 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003108 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003109 for (auto &SelectorAndID : SelectorIDs) {
3110 Selector S = SelectorAndID.first;
3111 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003112 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003113 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003114 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003115 ObjCMethodList(),
3116 ObjCMethodList()
3117 };
3118 if (F != SemaRef.MethodPool.end()) {
3119 Data.Instance = F->second.first;
3120 Data.Factory = F->second.second;
3121 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003122 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003123 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003124 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003125 // Selector already exists. Did it change?
3126 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003127 for (ObjCMethodList *M = &Data.Instance;
3128 !changed && M && M->getMethod(); M = M->getNext()) {
3129 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003130 changed = true;
3131 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003132 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003133 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003134 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003135 changed = true;
3136 }
3137 if (!changed)
3138 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003139 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003140 // A new method pool entry.
3141 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003142 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003143 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003144 }
3145
Douglas Gregorc78d3462009-04-24 21:10:55 +00003146 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003147 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003148 uint32_t BucketOffset;
3149 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003150 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003151 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003152 llvm::raw_svector_ostream Out(MethodPool);
3153 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003154 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003155 BucketOffset = Generator.Emit(Out, Trait);
3156 }
3157
3158 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003159 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003160 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003164 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003165
Douglas Gregor95c13f52009-04-25 17:48:32 +00003166 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003167 {
3168 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3169 NumTableEntries};
3170 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3171 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003172
3173 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003174 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003175 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003179 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003180
3181 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003182 {
3183 RecordData::value_type Record[] = {
3184 SELECTOR_OFFSETS, SelectorOffsets.size(),
3185 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3186 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3187 bytes(SelectorOffsets));
3188 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003189 }
3190}
3191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003192/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003193void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003194 using namespace llvm;
3195 if (SemaRef.ReferencedSelectors.empty())
3196 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003197
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003198 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003199 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003201 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003202 // very tricky to fix, and given that @selector shouldn't really appear in
3203 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003204 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3205 Selector Sel = SelectorAndLocation.first;
3206 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003207 Writer.AddSelectorRef(Sel);
3208 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003209 }
Richard Smithe64e7452016-04-18 21:54:58 +00003210 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003211}
3212
Douglas Gregorc5046832009-04-27 18:38:38 +00003213//===----------------------------------------------------------------------===//
3214// Identifier Table Serialization
3215//===----------------------------------------------------------------------===//
3216
Richard Smithb3761912015-02-05 23:08:52 +00003217/// Determine the declaration that should be put into the name lookup table to
3218/// represent the given declaration in this module. This is usually D itself,
3219/// but if D was imported and merged into a local declaration, we want the most
3220/// recent local declaration instead. The chosen declaration will be the most
3221/// recent declaration in any module that imports this one.
3222static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3223 NamedDecl *D) {
3224 if (!LangOpts.Modules || !D->isFromASTFile())
3225 return D;
3226
3227 if (Decl *Redecl = D->getPreviousDecl()) {
3228 // For Redeclarable decls, a prior declaration might be local.
3229 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003230 // If we find a local decl, we're done.
3231 if (!Redecl->isFromASTFile()) {
3232 // Exception: in very rare cases (for injected-class-names), not all
3233 // redeclarations are in the same semantic context. Skip ones in a
3234 // different context. They don't go in this lookup table at all.
3235 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3236 D->getDeclContext()->getRedeclContext()))
3237 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003238 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003239 }
3240
Richard Smithfe620d22015-03-05 23:24:12 +00003241 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003242 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003243 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003244 break;
3245 }
3246 } else if (Decl *First = D->getCanonicalDecl()) {
3247 // For Mergeable decls, the first decl might be local.
3248 if (!First->isFromASTFile())
3249 return cast<NamedDecl>(First);
3250 }
3251
3252 // All declarations are imported. Our most recent declaration will also be
3253 // the most recent one in anyone who imports us.
3254 return D;
3255}
3256
Douglas Gregorc78d3462009-04-24 21:10:55 +00003257namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003258
Richard Smithcf97cf62015-04-19 01:34:23 +00003259class ASTIdentifierTableTrait {
3260 ASTWriter &Writer;
3261 Preprocessor &PP;
3262 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003263 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003264 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003265 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003266
3267 /// \brief Determines whether this is an "interesting" identifier that needs a
3268 /// full IdentifierInfo structure written into the hash table. Notably, this
3269 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3270 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003271 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003272 if (MacroOffset ||
3273 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003274 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003275 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003276 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003277 return true;
3278
3279 return false;
3280 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003281
Douglas Gregore84a9da2009-04-20 20:36:09 +00003282public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003283 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003284 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003285
Sebastian Redl539c5062010-08-18 23:57:32 +00003286 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003287 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003288
Justin Bogner25463f12014-04-18 20:27:24 +00003289 typedef unsigned hash_value_type;
3290 typedef unsigned offset_type;
3291
Richard Smithd7329392015-04-21 21:46:32 +00003292 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003293 IdentifierResolver &IdResolver, bool IsModule,
3294 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003295 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003296 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3297 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003298
Richard Smithd79514e2016-02-05 19:03:40 +00003299 bool needDecls() const { return NeedDecls; }
3300
Justin Bogner25463f12014-04-18 20:27:24 +00003301 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003302 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003303 }
Mike Stump11289f42009-09-09 15:08:12 +00003304
Richard Smith33e0f7e2015-07-22 02:08:40 +00003305 bool isInterestingIdentifier(const IdentifierInfo *II) {
3306 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3307 return isInterestingIdentifier(II, MacroOffset);
3308 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003309
Richard Smith9c254182015-07-19 21:41:12 +00003310 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3311 return isInterestingIdentifier(II, 0);
3312 }
3313
Mike Stump11289f42009-09-09 15:08:12 +00003314 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003315 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003316 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003317 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003318 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3319 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003320 DataLen += 2; // 2 bytes for builtin ID
3321 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003322 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003323 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003324
Richard Smitha534a312015-07-21 23:54:07 +00003325 if (NeedDecls) {
3326 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3327 DEnd = IdResolver.end();
3328 D != DEnd; ++D)
3329 DataLen += 4;
3330 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003331 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003332 using namespace llvm::support;
3333 endian::Writer<little> LE(Out);
3334
Richard Smithdf8a8312015-03-13 04:05:01 +00003335 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003336 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003337 // We emit the key length after the data length so that every
3338 // string is preceded by a 16-bit length. This matches the PTH
3339 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003340 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003341 return std::make_pair(KeyLen, DataLen);
3342 }
Mike Stump11289f42009-09-09 15:08:12 +00003343
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003344 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003345 unsigned KeyLen) {
3346 // Record the location of the key data. This is used when generating
3347 // the mapping from persistent IDs to strings.
3348 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003349
3350 // Emit the offset of the key/data length information to the interesting
3351 // identifiers table if necessary.
3352 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3353 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3354
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003355 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003356 }
Mike Stump11289f42009-09-09 15:08:12 +00003357
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003358 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003359 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003360 using namespace llvm::support;
3361 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003362
Richard Smithd7329392015-04-21 21:46:32 +00003363 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3364 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003365 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003366 return;
3367 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003368
Justin Bognere1c147c2014-03-28 22:03:19 +00003369 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003370 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3371 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003372 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003373 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003374 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003375 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003376 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3377 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003378 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003379 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003380 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003381 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003382
Richard Smithd7329392015-04-21 21:46:32 +00003383 if (HadMacroDefinition)
3384 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003385
Richard Smitha534a312015-07-21 23:54:07 +00003386 if (NeedDecls) {
3387 // Emit the declaration IDs in reverse order, because the
3388 // IdentifierResolver provides the declarations as they would be
3389 // visible (e.g., the function "stat" would come before the struct
3390 // "stat"), but the ASTReader adds declarations to the end of the list
3391 // (so we need to see the struct "stat" before the function "stat").
3392 // Only emit declarations that aren't from a chained PCH, though.
3393 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3394 IdResolver.end());
3395 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3396 DEnd = Decls.rend();
3397 D != DEnd; ++D)
3398 LE.write<uint32_t>(
3399 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3400 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003401 }
3402};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003403
Douglas Gregore84a9da2009-04-20 20:36:09 +00003404} // end anonymous namespace
3405
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003406/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003407///
3408/// The identifier table consists of a blob containing string data
3409/// (the actual identifiers themselves) and a separate "offsets" index
3410/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003411void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3412 IdentifierResolver &IdResolver,
3413 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003414 using namespace llvm;
3415
Richard Smith33e0f7e2015-07-22 02:08:40 +00003416 RecordData InterestingIdents;
3417
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003418 // Create and write out the blob that contains the identifier
3419 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003420 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003421 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003422 ASTIdentifierTableTrait Trait(
3423 *this, PP, IdResolver, IsModule,
3424 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003425
Douglas Gregore6648fb2009-04-28 20:33:11 +00003426 // Look for any identifiers that were named while processing the
3427 // headers, but are otherwise not needed. We add these to the hash
3428 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003429 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003430 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003431 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003432 for (const auto &ID : PP.getIdentifierTable())
3433 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003434 // Sort the identifiers lexicographically before getting them references so
3435 // that their order is stable.
3436 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3437 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003438 if (Trait.isInterestingNonMacroIdentifier(II))
3439 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003440
Sebastian Redlff4a2952010-07-23 23:49:55 +00003441 // Create the on-disk hash table representation. We only store offsets
3442 // for identifiers that appear here for the first time.
3443 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003444 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003445 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003446 IdentID ID = IdentIDPair.second;
3447 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003448 // Write out identifiers if either the ID is local or the identifier has
3449 // changed since it was loaded.
3450 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3451 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003452 (Trait.needDecls() &&
3453 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003454 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003455 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003456
Douglas Gregore84a9da2009-04-20 20:36:09 +00003457 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003458 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003459 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003460 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003461 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003462 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003463 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003464 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003465 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003466 }
3467
3468 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003469 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003470 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003473 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003474
3475 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003476 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003477 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003478 }
3479
3480 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003481 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003482 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003483 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003486 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003487
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003488#ifndef NDEBUG
3489 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3490 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3491#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003492
3493 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3494 IdentifierOffsets.size(),
3495 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003496 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003497 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003498
3499 // In C++, write the list of interesting identifiers (those that are
3500 // defined as macros, poisoned, or similar unusual things).
3501 if (!InterestingIdents.empty())
3502 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003503}
3504
Douglas Gregorc5046832009-04-27 18:38:38 +00003505//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003506// DeclContext's Name Lookup Table Serialization
3507//===----------------------------------------------------------------------===//
3508
3509namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003510
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003511// Trait used for the on-disk hash table used in the method pool.
3512class ASTDeclContextNameLookupTrait {
3513 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003514 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003515
3516public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003517 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003518 typedef key_type key_type_ref;
3519
Richard Smithd88a7f12015-09-01 20:35:42 +00003520 /// A start and end index into DeclIDs, representing a sequence of decls.
3521 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003522 typedef const data_type& data_type_ref;
3523
Justin Bogner25463f12014-04-18 20:27:24 +00003524 typedef unsigned hash_value_type;
3525 typedef unsigned offset_type;
3526
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003527 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3528
Richard Smithd88a7f12015-09-01 20:35:42 +00003529 template<typename Coll>
3530 data_type getData(const Coll &Decls) {
3531 unsigned Start = DeclIDs.size();
3532 for (NamedDecl *D : Decls) {
3533 DeclIDs.push_back(
3534 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3535 }
3536 return std::make_pair(Start, DeclIDs.size());
3537 }
3538
3539 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3540 unsigned Start = DeclIDs.size();
3541 for (auto ID : FromReader)
3542 DeclIDs.push_back(ID);
3543 return std::make_pair(Start, DeclIDs.size());
3544 }
3545
3546 static bool EqualKey(key_type_ref a, key_type_ref b) {
3547 return a == b;
3548 }
3549
Richard Smitha06c7e62015-08-26 23:55:49 +00003550 hash_value_type ComputeHash(DeclarationNameKey Name) {
3551 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003552 }
3553
Richard Smithd88a7f12015-09-01 20:35:42 +00003554 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3555 assert(Writer.hasChain() &&
3556 "have reference to loaded module file but no chain?");
3557
3558 using namespace llvm::support;
3559 endian::Writer<little>(Out)
3560 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3561 }
3562
Richard Smitha06c7e62015-08-26 23:55:49 +00003563 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3564 DeclarationNameKey Name,
3565 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003566 using namespace llvm::support;
3567 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003568 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003569 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003570 case DeclarationName::Identifier:
3571 case DeclarationName::ObjCZeroArgSelector:
3572 case DeclarationName::ObjCOneArgSelector:
3573 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003574 case DeclarationName::CXXLiteralOperatorName:
3575 KeyLen += 4;
3576 break;
3577 case DeclarationName::CXXOperatorName:
3578 KeyLen += 1;
3579 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003580 case DeclarationName::CXXConstructorName:
3581 case DeclarationName::CXXDestructorName:
3582 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003583 case DeclarationName::CXXUsingDirective:
3584 break;
3585 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003586 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003587
Richard Smithf02662d2015-07-30 03:17:16 +00003588 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003589 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3590 assert(uint16_t(DataLen) == DataLen &&
3591 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003592 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593
3594 return std::make_pair(KeyLen, DataLen);
3595 }
3596
Richard Smitha06c7e62015-08-26 23:55:49 +00003597 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003598 using namespace llvm::support;
3599 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003600 LE.write<uint8_t>(Name.getKind());
3601 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003602 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003603 case DeclarationName::CXXLiteralOperatorName:
3604 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003605 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003606 case DeclarationName::ObjCZeroArgSelector:
3607 case DeclarationName::ObjCOneArgSelector:
3608 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003609 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003610 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003611 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003612 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003613 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003614 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003615 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003616 case DeclarationName::CXXConstructorName:
3617 case DeclarationName::CXXDestructorName:
3618 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003619 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003620 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003621 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003622
3623 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003624 }
3625
Richard Smitha06c7e62015-08-26 23:55:49 +00003626 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3627 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003628 using namespace llvm::support;
3629 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003630 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003631 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3632 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003633 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3634 }
3635};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003636
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003637} // end anonymous namespace
3638
Chandler Carruthe972c362015-03-26 03:11:40 +00003639bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3640 DeclContext *DC) {
3641 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3642}
3643
3644bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3645 DeclContext *DC) {
3646 for (auto *D : Result.getLookupResult())
3647 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3648 return false;
3649
3650 return true;
3651}
3652
Richard Smithd88a7f12015-09-01 20:35:42 +00003653void
Chandler Carruthe972c362015-03-26 03:11:40 +00003654ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003655 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003656 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3657 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003658 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003659
Chandler Carruthe972c362015-03-26 03:11:40 +00003660 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003661 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003662 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3663
3664 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003665 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3666 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003667 ASTDeclContextNameLookupTrait Trait(*this);
3668
Chandler Carruthe972c362015-03-26 03:11:40 +00003669 // The first step is to collect the declaration names which we need to
3670 // serialize into the name lookup table, and to collect them in a stable
3671 // order.
3672 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003673
Chandler Carruthe972c362015-03-26 03:11:40 +00003674 // We also build up small sets of the constructor and conversion function
3675 // names which are visible.
3676 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003677
Chandler Carruthe972c362015-03-26 03:11:40 +00003678 for (auto &Lookup : *DC->buildLookup()) {
3679 auto &Name = Lookup.first;
3680 auto &Result = Lookup.second;
3681
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003682 // If there are no local declarations in our lookup result, we
3683 // don't need to write an entry for the name at all. If we can't
3684 // write out a lookup set without performing more deserialization,
3685 // just skip this entry.
3686 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003687 isLookupResultEntirelyExternal(Result, DC))
3688 continue;
3689
3690 // We also skip empty results. If any of the results could be external and
3691 // the currently available results are empty, then all of the results are
3692 // external and we skip it above. So the only way we get here with an empty
3693 // results is when no results could have been external *and* we have
3694 // external results.
3695 //
3696 // FIXME: While we might want to start emitting on-disk entries for negative
3697 // lookups into a decl context as an optimization, today we *have* to skip
3698 // them because there are names with empty lookup results in decl contexts
3699 // which we can't emit in any stable ordering: we lookup constructors and
3700 // conversion functions in the enclosing namespace scope creating empty
3701 // results for them. This in almost certainly a bug in Clang's name lookup,
3702 // but that is likely to be hard or impossible to fix and so we tolerate it
3703 // here by omitting lookups with empty results.
3704 if (Lookup.second.getLookupResult().empty())
3705 continue;
3706
3707 switch (Lookup.first.getNameKind()) {
3708 default:
3709 Names.push_back(Lookup.first);
3710 break;
3711
Richard Smithcd45dbc2014-04-19 03:48:30 +00003712 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003713 assert(isa<CXXRecordDecl>(DC) &&
3714 "Cannot have a constructor name outside of a class!");
3715 ConstructorNameSet.insert(Name);
3716 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003717
3718 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003719 assert(isa<CXXRecordDecl>(DC) &&
3720 "Cannot have a conversion function name outside of a class!");
3721 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003722 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003723 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003724 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003725
Chandler Carruthe972c362015-03-26 03:11:40 +00003726 // Sort the names into a stable order.
3727 std::sort(Names.begin(), Names.end());
3728
Chandler Carruthffbf7052015-03-26 22:27:09 +00003729 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003730 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003731 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003732
Chandler Carruthffbf7052015-03-26 22:27:09 +00003733 // First we try the easy case by forming the current context's constructor
3734 // name and adding that name first. This is a very useful optimization to
3735 // avoid walking the lexical declarations in many cases, and it also
3736 // handles the only case where a constructor name can come from some other
3737 // lexical context -- when that name is an implicit constructor merged from
3738 // another declaration in the redecl chain. Any non-implicit constructor or
3739 // conversion function which doesn't occur in all the lexical contexts
3740 // would be an ODR violation.
3741 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3742 Context->getCanonicalType(Context->getRecordType(D)));
3743 if (ConstructorNameSet.erase(ImplicitCtorName))
3744 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003745
Chandler Carruthffbf7052015-03-26 22:27:09 +00003746 // If we still have constructors or conversion functions, we walk all the
3747 // names in the decl and add the constructors and conversion functions
3748 // which are visible in the order they lexically occur within the context.
3749 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3750 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3751 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3752 auto Name = ChildND->getDeclName();
3753 switch (Name.getNameKind()) {
3754 default:
3755 continue;
3756
3757 case DeclarationName::CXXConstructorName:
3758 if (ConstructorNameSet.erase(Name))
3759 Names.push_back(Name);
3760 break;
3761
3762 case DeclarationName::CXXConversionFunctionName:
3763 if (ConversionNameSet.erase(Name))
3764 Names.push_back(Name);
3765 break;
3766 }
3767
3768 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3769 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003770 }
3771
Chandler Carruthe972c362015-03-26 03:11:40 +00003772 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3773 "constructors by walking all the "
3774 "lexical members of the context.");
3775 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3776 "conversion functions by walking all "
3777 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003778 }
3779
Chandler Carruthe972c362015-03-26 03:11:40 +00003780 // Next we need to do a lookup with each name into this decl context to fully
3781 // populate any results from external sources. We don't actually use the
3782 // results of these lookups because we only want to use the results after all
3783 // results have been loaded and the pointers into them will be stable.
3784 for (auto &Name : Names)
3785 DC->lookup(Name);
3786
3787 // Now we need to insert the results for each name into the hash table. For
3788 // constructor names and conversion function names, we actually need to merge
3789 // all of the results for them into one list of results each and insert
3790 // those.
3791 SmallVector<NamedDecl *, 8> ConstructorDecls;
3792 SmallVector<NamedDecl *, 8> ConversionDecls;
3793
3794 // Now loop over the names, either inserting them or appending for the two
3795 // special cases.
3796 for (auto &Name : Names) {
3797 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3798
3799 switch (Name.getNameKind()) {
3800 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003801 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003802 break;
3803
3804 case DeclarationName::CXXConstructorName:
3805 ConstructorDecls.append(Result.begin(), Result.end());
3806 break;
3807
3808 case DeclarationName::CXXConversionFunctionName:
3809 ConversionDecls.append(Result.begin(), Result.end());
3810 break;
3811 }
3812 }
3813
3814 // Handle our two special cases if we ended up having any. We arbitrarily use
3815 // the first declaration's name here because the name itself isn't part of
3816 // the key, only the kind of name is used.
3817 if (!ConstructorDecls.empty())
3818 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003819 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003820 if (!ConversionDecls.empty())
3821 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003822 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003823
Richard Smithd88a7f12015-09-01 20:35:42 +00003824 // Create the on-disk hash table. Also emit the existing imported and
3825 // merged table if there is one.
3826 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3827 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003828}
3829
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003830/// \brief Write the block containing all of the declaration IDs
3831/// visible from the given DeclContext.
3832///
3833/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003834/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003835uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3836 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003837 // If we imported a key declaration of this namespace, write the visible
3838 // lookup results as an update record for it rather than including them
3839 // on this declaration. We will only look at key declarations on reload.
3840 if (isa<NamespaceDecl>(DC) && Chain &&
3841 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3842 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003843 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003844 Prev = Prev->getPreviousDecl())
3845 if (!Prev->isFromASTFile())
3846 return 0;
3847
3848 // Note that we need to emit an update record for the primary context.
3849 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3850
3851 // Make sure all visible decls are written. They will be recorded later. We
3852 // do this using a side data structure so we can sort the names into
3853 // a deterministic order.
3854 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3855 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3856 LookupResults;
3857 if (Map) {
3858 LookupResults.reserve(Map->size());
3859 for (auto &Entry : *Map)
3860 LookupResults.push_back(
3861 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3862 }
3863
3864 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3865 for (auto &NameAndResult : LookupResults) {
3866 DeclarationName Name = NameAndResult.first;
3867 DeclContext::lookup_result Result = NameAndResult.second;
3868 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3869 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3870 // We have to work around a name lookup bug here where negative lookup
3871 // results for these names get cached in namespace lookup tables (these
3872 // names should never be looked up in a namespace).
3873 assert(Result.empty() && "Cannot have a constructor or conversion "
3874 "function name in a namespace!");
3875 continue;
3876 }
3877
3878 for (NamedDecl *ND : Result)
3879 if (!ND->isFromASTFile())
3880 GetDeclRef(ND);
3881 }
3882
3883 return 0;
3884 }
3885
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003886 if (DC->getPrimaryContext() != DC)
3887 return 0;
3888
Chandler Carruthe972c362015-03-26 03:11:40 +00003889 // Skip contexts which don't support name lookup.
3890 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003891 return 0;
3892
3893 // If not in C++, we perform name lookup for the translation unit via the
3894 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003895 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003896 return 0;
3897
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003898 // Serialize the contents of the mapping used for lookup. Note that,
3899 // although we have two very different code paths, the serialized
3900 // representation is the same for both cases: a declaration name,
3901 // followed by a size, followed by references to the visible
3902 // declarations that have that name.
3903 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003904 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003905 if (!Map || Map->empty())
3906 return 0;
3907
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003908 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003909 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003910 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003911
3912 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003913 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003914 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003915 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003916 ++NumVisibleDeclContexts;
3917 return Offset;
3918}
3919
Sebastian Redla4071b42010-08-24 00:50:09 +00003920/// \brief Write an UPDATE_VISIBLE block for the given context.
3921///
3922/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3923/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003924/// (in C++), for namespaces, and for classes with forward-declared unscoped
3925/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003926void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003927 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003928 if (!Map || Map->empty())
3929 return;
3930
Sebastian Redla4071b42010-08-24 00:50:09 +00003931 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003932 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003933 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003934
Richard Smith5fc18a92015-07-12 23:43:21 +00003935 // If we're updating a namespace, select a key declaration as the key for the
3936 // update record; those are the only ones that will be checked on reload.
3937 if (isa<NamespaceDecl>(DC))
3938 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3939
Sebastian Redla4071b42010-08-24 00:50:09 +00003940 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003941 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003942 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003943}
3944
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003945/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3946void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003947 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003948 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3949}
3950
3951/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3952void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003953 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003954 return;
3955
3956 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3957 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00003958 for (const auto &I:Opts.OptMap) {
3959 AddString(I.getKey(), Record);
3960 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00003961 Record.push_back(V.Supported ? 1 : 0);
3962 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00003963 Record.push_back(V.Avail);
3964 Record.push_back(V.Core);
3965 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003966 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3967}
3968
Yaxun Liu5b746652016-12-18 05:18:55 +00003969void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
3970 if (!SemaRef.Context.getLangOpts().OpenCL)
3971 return;
3972
3973 RecordData Record;
3974 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
3975 Record.push_back(
3976 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
3977 Record.push_back(I.second.size());
3978 for (auto Ext : I.second)
3979 AddString(Ext, Record);
3980 }
3981 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
3982}
3983
3984void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
3985 if (!SemaRef.Context.getLangOpts().OpenCL)
3986 return;
3987
3988 RecordData Record;
3989 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
3990 Record.push_back(getDeclID(I.first));
3991 Record.push_back(static_cast<unsigned>(I.second.size()));
3992 for (auto Ext : I.second)
3993 AddString(Ext, Record);
3994 }
3995 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
3996}
3997
Justin Lebar67a78a62016-10-08 22:15:58 +00003998void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
3999 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4000 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4001 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4002 }
4003}
4004
Douglas Gregor404cdde2012-01-27 01:47:08 +00004005void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004006 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004007 RecordData Categories;
4008
4009 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4010 unsigned Size = 0;
4011 unsigned StartIndex = Categories.size();
4012
4013 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4014
4015 // Allocate space for the size.
4016 Categories.push_back(0);
4017
4018 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004019 for (ObjCInterfaceDecl::known_categories_iterator
4020 Cat = Class->known_categories_begin(),
4021 CatEnd = Class->known_categories_end();
4022 Cat != CatEnd; ++Cat, ++Size) {
4023 assert(getDeclID(*Cat) != 0 && "Bogus category");
4024 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004025 }
4026
4027 // Update the size.
4028 Categories[StartIndex] = Size;
4029
4030 // Record this interface -> category map.
4031 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4032 CategoriesMap.push_back(CatInfo);
4033 }
4034
4035 // Sort the categories map by the definition ID, since the reader will be
4036 // performing binary searches on this information.
4037 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4038
4039 // Emit the categories map.
4040 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004041
David Blaikieb44f0bf2017-01-04 22:36:43 +00004042 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004043 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004046 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004047
4048 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4049 Stream.EmitRecordWithBlob(AbbrevID, Record,
4050 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004051 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004052
Douglas Gregor404cdde2012-01-27 01:47:08 +00004053 // Emit the category lists.
4054 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4055}
4056
Richard Smithe40f2ba2013-08-07 21:41:30 +00004057void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4058 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4059
4060 if (LPTMap.empty())
4061 return;
4062
4063 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004064 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004065 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004066 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004067 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004068 AddDeclRef(LPT.D, Record);
4069 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004070
Justin Lebar28f09c52016-10-10 16:26:08 +00004071 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004072 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004073 }
4074 }
4075 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4076}
4077
Dario Domizioli13a0a382014-05-23 12:13:25 +00004078/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4079void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4080 RecordData Record;
4081 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4082 AddSourceLocation(PragmaLoc, Record);
4083 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4084}
4085
Nico Weber779355f2016-03-02 23:22:00 +00004086/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4087void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4088 RecordData Record;
4089 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4090 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4091}
4092
Nico Weber42932312016-03-03 00:17:35 +00004093/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4094//module.
4095void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4096 RecordData Record;
4097 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4098 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4099 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4100}
4101
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004102void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4103 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004104 // Enter the extension block.
4105 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4106
4107 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004108 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004109 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4110 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4111 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4112 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4113 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4114 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004115 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004116
4117 // Emit the metadata record.
4118 RecordData Record;
4119 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4120 Record.push_back(EXTENSION_METADATA);
4121 Record.push_back(Metadata.MajorVersion);
4122 Record.push_back(Metadata.MinorVersion);
4123 Record.push_back(Metadata.BlockName.size());
4124 Record.push_back(Metadata.UserInfo.size());
4125 SmallString<64> Buffer;
4126 Buffer += Metadata.BlockName;
4127 Buffer += Metadata.UserInfo;
4128 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4129
4130 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004131 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004132
4133 // Exit the extension block.
4134 Stream.ExitBlock();
4135}
4136
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004137//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004138// General Serialization Routines
4139//===----------------------------------------------------------------------===//
4140
Richard Smith69c82bf2016-04-01 22:52:03 +00004141/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004142void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4143 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004144 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004145 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004146 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004147 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004148
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004149#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004150
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004151 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004152}
4153
John McCallf413f5e2013-05-03 00:10:13 +00004154void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4155 AddSourceLocation(Tok.getLocation(), Record);
4156 Record.push_back(Tok.getLength());
4157
4158 // FIXME: When reading literal tokens, reconstruct the literal pointer
4159 // if it is needed.
4160 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4161 // FIXME: Should translate token kind to a stable encoding.
4162 Record.push_back(Tok.getKind());
4163 // FIXME: Should translate token flags to a stable encoding.
4164 Record.push_back(Tok.getFlags());
4165}
4166
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004167void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004168 Record.push_back(Str.size());
4169 Record.insert(Record.end(), Str.begin(), Str.end());
4170}
4171
Richard Smith7ed1bc92014-12-05 22:42:13 +00004172bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004173 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004174
Richard Smith54cc3c22014-12-11 20:50:24 +00004175 bool Changed =
4176 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004177
4178 // Remove a prefix to make the path relative, if relevant.
4179 const char *PathBegin = Path.data();
4180 const char *PathPtr =
4181 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4182 if (PathPtr != PathBegin) {
4183 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4184 Changed = true;
4185 }
4186
4187 return Changed;
4188}
4189
4190void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4191 SmallString<128> FilePath(Path);
4192 PreparePathForOutput(FilePath);
4193 AddString(FilePath, Record);
4194}
4195
Mehdi Amini57a41912015-09-10 01:46:39 +00004196void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004197 StringRef Path) {
4198 SmallString<128> FilePath(Path);
4199 PreparePathForOutput(FilePath);
4200 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4201}
4202
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004203void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4204 RecordDataImpl &Record) {
4205 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004206 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004207 Record.push_back(*Minor + 1);
4208 else
4209 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004210 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004211 Record.push_back(*Subminor + 1);
4212 else
4213 Record.push_back(0);
4214}
4215
Douglas Gregore84a9da2009-04-20 20:36:09 +00004216/// \brief Note that the identifier II occurs at the given offset
4217/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004218void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004219 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004220 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004221 // up earlier in the chain and thus don't need an offset.
4222 if (ID >= FirstIdentID)
4223 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004224}
4225
Douglas Gregor95c13f52009-04-25 17:48:32 +00004226/// \brief Note that the selector Sel occurs at the given offset
4227/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004228void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004229 unsigned ID = SelectorIDs[Sel];
4230 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004231 // Don't record offsets for selectors that are also available in a different
4232 // file.
4233 if (ID < FirstSelectorID)
4234 return;
4235 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004236}
4237
David Blaikie61137e12017-01-05 18:23:18 +00004238ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4239 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4240 bool IncludeTimestamps)
David Blaikie95dd3622017-01-05 18:45:45 +00004241 : Stream(Stream), IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004242 for (const auto &Ext : Extensions) {
4243 if (auto Writer = Ext->createExtensionWriter(*this))
4244 ModuleFileExtensionWriters.push_back(std::move(Writer));
4245 }
4246}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004247
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004248ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004249 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004250}
4251
Richard Smithb3761912015-02-05 23:08:52 +00004252const LangOptions &ASTWriter::getLangOpts() const {
4253 assert(WritingAST && "can't determine lang opts when not writing AST");
4254 return Context->getLangOpts();
4255}
4256
Richard Smithe75ee0f2015-08-17 07:13:32 +00004257time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4258 return IncludeTimestamps ? E->getModificationTime() : 0;
4259}
4260
Adrian Prantladbd2b12015-09-22 23:26:31 +00004261uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4262 Module *WritingModule, StringRef isysroot,
4263 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004264 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004265
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004266 ASTHasCompilerErrors = hasErrors;
4267
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004268 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004269 Stream.Emit((unsigned)'C', 8);
4270 Stream.Emit((unsigned)'P', 8);
4271 Stream.Emit((unsigned)'C', 8);
4272 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004273
Chris Lattner28fa4e62009-04-26 22:26:21 +00004274 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004275
Douglas Gregoreda8e122011-08-09 15:13:55 +00004276 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004277 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004278 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004279 ASTFileSignature Signature =
4280 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004281 Context = nullptr;
4282 PP = nullptr;
4283 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004284 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004285
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004286 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004287 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004288}
4289
Douglas Gregora94a1542011-07-27 21:45:57 +00004290template<typename Vector>
4291static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4292 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004293 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4294 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004295 Writer.AddDeclRef(*I, Record);
4296 }
4297}
4298
Adrian Prantladbd2b12015-09-22 23:26:31 +00004299uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4300 const std::string &OutputFile,
4301 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004302 using namespace llvm;
4303
Craig Toppera13603a2014-05-22 05:54:18 +00004304 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004305
Douglas Gregorcf68c582011-12-01 22:20:10 +00004306 // Make sure that the AST reader knows to finalize itself.
4307 if (Chain)
4308 Chain->finalizeForWriting();
4309
Sebastian Redl143413f2010-07-12 22:02:52 +00004310 ASTContext &Context = SemaRef.Context;
4311 Preprocessor &PP = SemaRef.PP;
4312
Douglas Gregordab42432011-08-12 00:15:20 +00004313 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004314 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4315 if (D) {
4316 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4317 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004318 }
4319 };
4320 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4321 PREDEF_DECL_TRANSLATION_UNIT_ID);
4322 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4323 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4324 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4325 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4326 PREDEF_DECL_OBJC_PROTOCOL_ID);
4327 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4328 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4329 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4330 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4331 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004332 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004333 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4334 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004335 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004336 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4337 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004338 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4339 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004340 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4341 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004342 RegisterPredefDecl(Context.TypePackElementDecl,
4343 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004344
Chris Lattner0c797362009-09-08 18:19:27 +00004345 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004346 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004347 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004348 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004349 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004350
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004351 // Build a record containing all of the file scoped decls in this file.
4352 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004353 if (!isModule)
4354 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4355 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004356
Douglas Gregor851443c2011-08-12 01:39:19 +00004357 // Build a record containing all of the delegating constructors we still need
4358 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004359 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004360 if (!isModule)
4361 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004362
Douglas Gregor851443c2011-08-12 01:39:19 +00004363 // Write the set of weak, undeclared identifiers. We always write the
4364 // entire table, since later PCH files in a PCH chain are only interested in
4365 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004366 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004367 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4368 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4369 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4370 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4371 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4372 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4373 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004374 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004375
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004376 // Build a record containing all of the ext_vector declarations.
4377 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004378 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004379
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004380 // Build a record containing all of the VTable uses information.
4381 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004382 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004383 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4384 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4385 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4386 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4387 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004388 }
4389
Nico Weber72889432014-09-06 01:25:55 +00004390 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4391 RecordData UnusedLocalTypedefNameCandidates;
4392 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4393 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4394
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004395 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004396 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004397 for (const auto &I : SemaRef.PendingInstantiations) {
4398 AddDeclRef(I.first, PendingInstantiations);
4399 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004400 }
4401 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4402 "There are local ones at end of translation unit!");
4403
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004404 // Build a record containing some declaration references.
4405 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004406 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004407 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4408 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004409 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004410 }
4411
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004412 RecordData CUDASpecialDeclRefs;
4413 if (Context.getcudaConfigureCallDecl()) {
4414 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4415 }
4416
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004417 // Build a record containing all of the known namespaces.
4418 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004419 for (const auto &I : SemaRef.KnownNamespaces) {
4420 if (!I.second)
4421 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004422 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004423
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004424 // Build a record of all used, undefined objects that require definitions.
4425 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004426
4427 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004428 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004429 for (const auto &I : Undefined) {
4430 AddDeclRef(I.first, UndefinedButUsed);
4431 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004432 }
4433
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004434 // Build a record containing all delete-expressions that we would like to
4435 // analyze later in AST.
4436 RecordData DeleteExprsToAnalyze;
4437
4438 for (const auto &DeleteExprsInfo :
4439 SemaRef.getMismatchingDeleteExpressions()) {
4440 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4441 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4442 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4443 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4444 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4445 }
4446 }
4447
Douglas Gregor112b9072012-10-18 05:31:06 +00004448 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004449 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004450
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004451 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004452 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004453
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004454 // This is so that older clang versions, before the introduction
4455 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004456 {
4457 RecordData Record = {VERSION_MAJOR};
4458 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4459 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004460
Douglas Gregor851443c2011-08-12 01:39:19 +00004461 // Create a lexical update block containing all of the declarations in the
4462 // translation unit that do not come from other AST files.
4463 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004464 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4465 for (const auto *D : TU->noload_decls()) {
4466 if (!D->isFromASTFile()) {
4467 NewGlobalKindDeclPairs.push_back(D->getKind());
4468 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4469 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004470 }
4471
David Blaikieb44f0bf2017-01-04 22:36:43 +00004472 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004473 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4474 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004475 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004476 {
4477 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4478 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4479 bytes(NewGlobalKindDeclPairs));
4480 }
4481
Douglas Gregor851443c2011-08-12 01:39:19 +00004482 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004483 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004484 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4485 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004486 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004487 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004488 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004489
4490 // If we have any extern "C" names, write out a visible update for them.
4491 if (Context.ExternCContext)
4492 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004493
4494 // If the translation unit has an anonymous namespace, and we don't already
4495 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004496 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004497 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4498 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004499 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004500 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004501 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004502
Richard Smith5652c0f2014-03-21 01:48:23 +00004503 // Add update records for all mangling numbers and static local numbers.
4504 // These aren't really update records, but this is a convenient way of
4505 // tagging this rare extra data onto the declarations.
4506 for (const auto &Number : Context.MangleNumbers)
4507 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004508 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4509 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004510 for (const auto &Number : Context.StaticLocalNumbers)
4511 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004512 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4513 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004514
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004515 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004516 // an AST file, are registered for serialization. Likewise for template
4517 // specializations added to imported templates.
4518 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004519 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004520 }
4521
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004522 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004523 // serialization, if we're storing decls with identifiers.
4524 if (!WritingModule || !getLangOpts().CPlusPlus) {
4525 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004526 for (const auto &ID : PP.getIdentifierTable()) {
4527 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004528 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4529 IIs.push_back(II);
4530 }
4531 // Sort the identifiers to visit based on their name.
4532 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4533 for (const IdentifierInfo *II : IIs) {
4534 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4535 DEnd = SemaRef.IdResolver.end();
4536 D != DEnd; ++D) {
4537 GetDeclRef(*D);
4538 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004539 }
4540 }
4541
Manman Rena0f31a02016-04-29 19:04:05 +00004542 // For method pool in the module, if it contains an entry for a selector,
4543 // the entry should be complete, containing everything introduced by that
4544 // module and all modules it imports. It's possible that the entry is out of
4545 // date, so we need to pull in the new content here.
4546
4547 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4548 // safe, we copy all selectors out.
4549 llvm::SmallVector<Selector, 256> AllSelectors;
4550 for (auto &SelectorAndID : SelectorIDs)
4551 AllSelectors.push_back(SelectorAndID.first);
4552 for (auto &Selector : AllSelectors)
4553 SemaRef.updateOutOfDateSelector(Selector);
4554
Douglas Gregor5204bde2011-08-02 16:26:37 +00004555 // Form the record of special types.
4556 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004557 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004558 AddTypeRef(Context.getFILEType(), SpecialTypes);
4559 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4560 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4561 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4562 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004563 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004564 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004565
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004566 if (Chain) {
4567 // Write the mapping information describing our module dependencies and how
4568 // each of those modules were mapped into our own offset/ID space, so that
4569 // the reader can build the appropriate mapping to its own offset/ID space.
4570 // The map consists solely of a blob with the following format:
4571 // *(module-name-len:i16 module-name:len*i8
4572 // source-location-offset:i32
4573 // identifier-id:i32
4574 // preprocessed-entity-id:i32
4575 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004576 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004577 // selector-id:i32
4578 // declaration-id:i32
4579 // c++-base-specifiers-id:i32
4580 // type-id:i32)
4581 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004582 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004583 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004585 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004586 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004587 {
4588 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004589 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004590 using namespace llvm::support;
4591 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004592 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004593 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004594 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004595
Ben Langmuir785180e2014-10-20 16:27:30 +00004596 // Note: if a base ID was uint max, it would not be possible to load
4597 // another module after it or have more than one entity inside it.
4598 uint32_t None = std::numeric_limits<uint32_t>::max();
4599
4600 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4601 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4602 if (ShouldWrite)
4603 LE.write<uint32_t>(BaseID);
4604 else
4605 LE.write<uint32_t>(None);
4606 };
4607
Ben Langmuirfe971d92014-08-16 04:54:18 +00004608 // These values should be unique within a chain, since they will be read
4609 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004610 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4611 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4612 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4613 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4614 M->NumPreprocessedEntities);
4615 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4616 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4617 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4618 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004619 }
4620 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004621 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004622 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4623 Buffer.data(), Buffer.size());
4624 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004625
Richard Smithb9eab6d2014-03-20 19:44:17 +00004626 RecordData DeclUpdatesOffsetsRecord;
4627
Richard Smith59442b42014-03-20 20:07:19 +00004628 // Keep writing types, declarations, and declaration update records
4629 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004630 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4631 WriteTypeAbbrevs();
4632 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004633 do {
4634 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4635 while (!DeclTypesToEmit.empty()) {
4636 DeclOrType DOT = DeclTypesToEmit.front();
4637 DeclTypesToEmit.pop();
4638 if (DOT.isType())
4639 WriteType(DOT.getType());
4640 else
4641 WriteDecl(Context, DOT.getDecl());
4642 }
4643 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004644 Stream.ExitBlock();
4645
Richard Smithb9eab6d2014-03-20 19:44:17 +00004646 DoneWritingDeclsAndTypes = true;
4647
4648 // These things can only be done once we've written out decls and types.
4649 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004650 if (!DeclUpdatesOffsetsRecord.empty())
4651 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004652 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004653 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004654 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004655 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004656 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004657 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004658 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004659 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004660 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004661 WriteFPPragmaOptions(SemaRef.getFPOptions());
4662 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004663 WriteOpenCLExtensionTypes(SemaRef);
4664 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004665 WriteCUDAPragmas(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004666 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004667
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004668 // If we're emitting a module, write out the submodule information.
4669 if (WritingModule)
4670 WriteSubmodules(WritingModule);
4671
Douglas Gregor5204bde2011-08-02 16:26:37 +00004672 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4673
Douglas Gregord4df8652009-04-22 22:02:47 +00004674 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004675 if (!EagerlyDeserializedDecls.empty())
4676 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004677
4678 // Write the record containing tentative definitions.
4679 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004680 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004681
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004682 // Write the record containing unused file scoped decls.
4683 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004684 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004685
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004686 // Write the record containing weak undeclared identifiers.
4687 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004688 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004689 WeakUndeclaredIdentifiers);
4690
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004691 // Write the record containing ext_vector type names.
4692 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004693 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004694
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004695 // Write the record containing VTable uses information.
4696 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004697 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004698
Nico Weber72889432014-09-06 01:25:55 +00004699 // Write the record containing potentially unused local typedefs.
4700 if (!UnusedLocalTypedefNameCandidates.empty())
4701 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4702 UnusedLocalTypedefNameCandidates);
4703
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004704 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004705 if (!PendingInstantiations.empty())
4706 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004707
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004708 // Write the record containing declaration references of Sema.
4709 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004710 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004711
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004712 // Write the record containing CUDA-specific declaration references.
4713 if (!CUDASpecialDeclRefs.empty())
4714 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004715
4716 // Write the delegating constructors.
4717 if (!DelegatingCtorDecls.empty())
4718 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004719
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004720 // Write the known namespaces.
4721 if (!KnownNamespaces.empty())
4722 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004723
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004724 // Write the undefined internal functions and variables, and inline functions.
4725 if (!UndefinedButUsed.empty())
4726 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004727
4728 if (!DeleteExprsToAnalyze.empty())
4729 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4730
Douglas Gregor851443c2011-08-12 01:39:19 +00004731 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004732 for (auto *DC : UpdatedDeclContexts)
4733 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004734
Douglas Gregor959bb062011-12-03 01:15:29 +00004735 if (!WritingModule) {
4736 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004737 struct ModuleInfo {
4738 uint64_t ID;
4739 Module *M;
4740 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4741 };
Richard Smith56be7542014-03-21 00:33:59 +00004742 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004743 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004744 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004745 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4746 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004747 }
Richard Smith56be7542014-03-21 00:33:59 +00004748
4749 if (!Imports.empty()) {
4750 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4751 return A.ID < B.ID;
4752 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004753 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4754 return A.ID == B.ID;
4755 };
Richard Smith56be7542014-03-21 00:33:59 +00004756
4757 // Sort and deduplicate module IDs.
4758 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004759 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004760 Imports.end());
4761
4762 RecordData ImportedModules;
4763 for (const auto &Import : Imports) {
4764 ImportedModules.push_back(Import.ID);
4765 // FIXME: If the module has macros imported then later has declarations
4766 // imported, this location won't be the right one as a location for the
4767 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004768 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004769 }
4770
Douglas Gregor959bb062011-12-03 01:15:29 +00004771 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4772 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004773 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004774
Douglas Gregor404cdde2012-01-27 01:47:08 +00004775 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004776 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004777 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004778 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004779 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004780 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004781
Douglas Gregor08f01292009-04-17 22:13:46 +00004782 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004783 RecordData::value_type Record[] = {
4784 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004785 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004786 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004787
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004788 // Write the module file extension blocks.
4789 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004790 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004791
Adrian Prantladbd2b12015-09-22 23:26:31 +00004792 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004793}
4794
Richard Smithb9eab6d2014-03-20 19:44:17 +00004795void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004796 if (DeclUpdates.empty())
4797 return;
4798
Richard Smith59442b42014-03-20 20:07:19 +00004799 DeclUpdateMap LocalUpdates;
4800 LocalUpdates.swap(DeclUpdates);
4801
Richard Smith6ef42932014-03-20 21:02:00 +00004802 for (auto &DeclUpdate : LocalUpdates) {
4803 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004804
Richard Smithd28ac5b2014-03-22 23:33:22 +00004805 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004806 RecordData RecordData;
4807 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004808 for (auto &Update : DeclUpdate.second) {
4809 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4810
Richard Smith69c82bf2016-04-01 22:52:03 +00004811 // An updated body is emitted last, so that the reader doesn't need
4812 // to skip over the lazy body to reach statements for other records.
4813 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4814 HasUpdatedBody = true;
4815 else
4816 Record.push_back(Kind);
4817
Richard Smith6ef42932014-03-20 21:02:00 +00004818 switch (Kind) {
4819 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4820 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4821 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004822 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004823 Record.push_back(GetDeclRef(Update.getDecl()));
4824 break;
4825
Richard Smith4d235792014-08-07 18:53:08 +00004826 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004827 break;
4828
Richard Smith4d235792014-08-07 18:53:08 +00004829 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004830 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004831 break;
4832
John McCall32791cc2016-01-06 22:34:54 +00004833 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004834 Record.AddStmt(const_cast<Expr *>(
4835 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004836 break;
4837
Richard Smith4b054b22016-08-24 21:25:37 +00004838 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4839 Record.AddStmt(
4840 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4841 break;
4842
Richard Smithcd45dbc2014-04-19 03:48:30 +00004843 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4844 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004845 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004846 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004847 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004848 *Context, const_cast<CXXRecordDecl *>(RD)));
4849
4850 // This state is sometimes updated by template instantiation, when we
4851 // switch from the specialization referring to the template declaration
4852 // to it referring to the template definition.
4853 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4854 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004855 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004856 } else {
4857 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4858 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004859 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004860
4861 // The instantiation might have been resolved to a partial
4862 // specialization. If so, record which one.
4863 auto From = Spec->getInstantiatedFrom();
4864 if (auto PartialSpec =
4865 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4866 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004867 Record.AddDeclRef(PartialSpec);
4868 Record.AddTemplateArgumentList(
4869 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004870 } else {
4871 Record.push_back(false);
4872 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004873 }
4874 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004875 Record.AddSourceLocation(RD->getLocation());
4876 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004877 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004878
4879 // Instantiation may change attributes; write them all out afresh.
4880 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004881 if (D->hasAttrs())
4882 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004883
4884 // FIXME: Ensure we don't get here for explicit instantiations.
4885 break;
4886 }
4887
Richard Smithf8134002015-03-10 01:41:22 +00004888 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004889 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004890 break;
4891
Richard Smith564417a2014-03-20 21:47:22 +00004892 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4893 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004894 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4895 Record);
4896 break;
4897
Richard Smith6ef42932014-03-20 21:02:00 +00004898 case UPD_CXX_DEDUCED_RETURN_TYPE:
4899 Record.push_back(GetOrCreateTypeID(Update.getType()));
4900 break;
4901
4902 case UPD_DECL_MARKED_USED:
4903 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004904
4905 case UPD_MANGLING_NUMBER:
4906 case UPD_STATIC_LOCAL_NUMBER:
4907 Record.push_back(Update.getNumber());
4908 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004909
Alexey Bataev97720002014-11-11 04:05:39 +00004910 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004911 Record.AddSourceRange(
4912 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004913 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004914
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004915 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4916 Record.AddSourceRange(
4917 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4918 break;
4919
Richard Smith65ebb4a2015-03-26 04:09:53 +00004920 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004921 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004922 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004923
4924 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004925 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004926 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004927 }
4928 }
4929
Richard Smithd28ac5b2014-03-22 23:33:22 +00004930 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004931 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004932 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004933 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004934 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004935 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004936 }
4937
Richard Smithcd45dbc2014-04-19 03:48:30 +00004938 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004939 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004940 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004941}
4942
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004943void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004944 uint32_t Raw = Loc.getRawEncoding();
4945 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004946}
4947
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004948void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004949 AddSourceLocation(Range.getBegin(), Record);
4950 AddSourceLocation(Range.getEnd(), Record);
4951}
4952
Richard Smithf144b542016-04-08 21:54:32 +00004953void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4954 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004955 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004956 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004957}
4958
Richard Smithf144b542016-04-08 21:54:32 +00004959void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4960 Record->push_back(Value.isUnsigned());
4961 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004962}
4963
Richard Smithf144b542016-04-08 21:54:32 +00004964void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4965 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00004966}
4967
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004968void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004969 Record.push_back(getIdentifierRef(II));
4970}
4971
Sebastian Redl539c5062010-08-18 23:57:32 +00004972IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004973 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004974 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004975
Sebastian Redl539c5062010-08-18 23:57:32 +00004976 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004977 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004978 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004979 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004980}
4981
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004982MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004983 // Don't emit builtin macros like __LINE__ to the AST file unless they
4984 // have been redefined by the header (in which case they are not
4985 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004986 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004987 return 0;
4988
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004989 MacroID &ID = MacroIDs[MI];
4990 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004991 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004992 MacroInfoToEmitData Info = { Name, MI, ID };
4993 MacroInfosToEmit.push_back(Info);
4994 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004995 return ID;
4996}
4997
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004998MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004999 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005000 return 0;
5001
5002 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5003 return MacroIDs[MI];
5004}
5005
5006uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005007 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005008}
5009
Richard Smithe64e7452016-04-18 21:54:58 +00005010void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5011 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005012}
5013
Sebastian Redl539c5062010-08-18 23:57:32 +00005014SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005015 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005016 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005017 }
5018
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005019 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005020 if (SID == 0 && Chain) {
5021 // This might trigger a ReadSelector callback, which will set the ID for
5022 // this selector.
5023 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005024 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005025 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005026 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005027 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005028 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005029 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005030 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005031}
5032
Richard Smithe64e7452016-04-18 21:54:58 +00005033void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5034 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005035}
5036
Richard Smith290d8012016-04-06 17:06:00 +00005037void ASTRecordWriter::AddTemplateArgumentLocInfo(
5038 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005039 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005040 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005041 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005042 break;
5043 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005044 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005045 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005046 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005047 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5048 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005049 break;
5050 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005051 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5052 AddSourceLocation(Arg.getTemplateNameLoc());
5053 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005054 break;
John McCall0ad16662009-10-29 08:12:44 +00005055 case TemplateArgument::Null:
5056 case TemplateArgument::Integral:
5057 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005058 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005059 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005060 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005061 break;
5062 }
5063}
5064
Richard Smith290d8012016-04-06 17:06:00 +00005065void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5066 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005067
5068 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5069 bool InfoHasSameExpr
5070 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005071 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005072 if (InfoHasSameExpr)
5073 return; // Avoid storing the same expr twice.
5074 }
Richard Smith290d8012016-04-06 17:06:00 +00005075 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005076}
5077
Richard Smith290d8012016-04-06 17:06:00 +00005078void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005079 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005080 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005081 return;
5082 }
5083
Richard Smith290d8012016-04-06 17:06:00 +00005084 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005085}
5086
Richard Smith290d8012016-04-06 17:06:00 +00005087void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5088 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005089
Richard Smith290d8012016-04-06 17:06:00 +00005090 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005091 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005092 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005093}
5094
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005095void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005096 Record.push_back(GetOrCreateTypeID(T));
5097}
5098
Richard Smith2095ffe2015-03-16 20:11:03 +00005099TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005100 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005101 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5102 if (T.isNull())
5103 return TypeIdx();
5104 assert(!T.getLocalFastQualifiers());
5105
5106 TypeIdx &Idx = TypeIdxs[T];
5107 if (Idx.getIndex() == 0) {
5108 if (DoneWritingDeclsAndTypes) {
5109 assert(0 && "New type seen after serializing all the types to emit!");
5110 return TypeIdx();
5111 }
5112
5113 // We haven't seen this type before. Assign it a new ID and put it
5114 // into the queue of types to emit.
5115 Idx = TypeIdx(NextTypeID++);
5116 DeclTypesToEmit.push(T);
5117 }
5118 return Idx;
5119 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005120}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005121
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005122TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005123 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005124 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5125 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005126 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005127 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005128
Richard Smith2095ffe2015-03-16 20:11:03 +00005129 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5130 assert(I != TypeIdxs.end() && "Type not emitted!");
5131 return I->second;
5132 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005133}
5134
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005135void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005136 Record.push_back(GetDeclRef(D));
5137}
5138
Sebastian Redl539c5062010-08-18 23:57:32 +00005139DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005140 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005141
5142 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005143 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005144 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005145
5146 // If D comes from an AST file, its declaration ID is already known and
5147 // fixed.
5148 if (D->isFromASTFile())
5149 return D->getGlobalID();
5150
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005151 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005152 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005153 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005154 if (DoneWritingDeclsAndTypes) {
5155 assert(0 && "New decl seen after serializing all the decls to emit!");
5156 return 0;
5157 }
5158
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005159 // We haven't seen this declaration before. Give it a new ID and
5160 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005161 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005162 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005163 }
5164
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005165 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005166}
5167
Sebastian Redl539c5062010-08-18 23:57:32 +00005168DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005169 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005170 return 0;
5171
Douglas Gregorb3163e52012-01-05 22:33:30 +00005172 // If D comes from an AST file, its declaration ID is already known and
5173 // fixed.
5174 if (D->isFromASTFile())
5175 return D->getGlobalID();
5176
Douglas Gregore84a9da2009-04-20 20:36:09 +00005177 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5178 return DeclIDs[D];
5179}
5180
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005181void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005182 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005183 assert(D);
5184
5185 SourceLocation Loc = D->getLocation();
5186 if (Loc.isInvalid())
5187 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005188
5189 // We only keep track of the file-level declarations of each file.
5190 if (!D->getLexicalDeclContext()->isFileContext())
5191 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005192 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5193 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005194 if (isa<ParmVarDecl>(D))
5195 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005196
5197 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005198 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005199 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005200 FileID FID;
5201 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005202 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005203 if (FID.isInvalid())
5204 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005205 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005206
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005207 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005208 if (!Info)
5209 Info = new DeclIDInFileInfo();
5210
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005211 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005212 LocDeclIDsTy &Decls = Info->DeclIDs;
5213
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005214 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005215 Decls.push_back(LocDecl);
5216 return;
5217 }
5218
Benjamin Kramer45025c02013-08-24 13:22:59 +00005219 LocDeclIDsTy::iterator I =
5220 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005221
5222 Decls.insert(I, LocDecl);
5223}
5224
Richard Smithe64e7452016-04-18 21:54:58 +00005225void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005226 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005227 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005228 switch (Name.getNameKind()) {
5229 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005230 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005231 break;
5232
5233 case DeclarationName::ObjCZeroArgSelector:
5234 case DeclarationName::ObjCOneArgSelector:
5235 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005236 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005237 break;
5238
5239 case DeclarationName::CXXConstructorName:
5240 case DeclarationName::CXXDestructorName:
5241 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005242 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005243 break;
5244
5245 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005246 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005247 break;
5248
Alexis Hunt3d221f22009-11-29 07:34:05 +00005249 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005250 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005251 break;
5252
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005253 case DeclarationName::CXXUsingDirective:
5254 // No extra data to emit
5255 break;
5256 }
5257}
Chris Lattnerca025db2010-05-07 21:43:38 +00005258
Richard Smithd08aeb62014-08-28 01:33:39 +00005259unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5260 assert(needsAnonymousDeclarationNumber(D) &&
5261 "expected an anonymous declaration");
5262
5263 // Number the anonymous declarations within this context, if we've not
5264 // already done so.
5265 auto It = AnonymousDeclarationNumbers.find(D);
5266 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005267 auto *DC = D->getLexicalDeclContext();
5268 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5269 AnonymousDeclarationNumbers[ND] = Number;
5270 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005271
5272 It = AnonymousDeclarationNumbers.find(D);
5273 assert(It != AnonymousDeclarationNumbers.end() &&
5274 "declaration not found within its lexical context");
5275 }
5276
5277 return It->second;
5278}
5279
Richard Smith290d8012016-04-06 17:06:00 +00005280void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5281 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005282 switch (Name.getNameKind()) {
5283 case DeclarationName::CXXConstructorName:
5284 case DeclarationName::CXXDestructorName:
5285 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005286 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005287 break;
5288
5289 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005290 AddSourceLocation(SourceLocation::getFromRawEncoding(
5291 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005292 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005293 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005294 break;
5295
5296 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005297 AddSourceLocation(SourceLocation::getFromRawEncoding(
5298 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005299 break;
5300
5301 case DeclarationName::Identifier:
5302 case DeclarationName::ObjCZeroArgSelector:
5303 case DeclarationName::ObjCOneArgSelector:
5304 case DeclarationName::ObjCMultiArgSelector:
5305 case DeclarationName::CXXUsingDirective:
5306 break;
5307 }
5308}
5309
Richard Smith290d8012016-04-06 17:06:00 +00005310void ASTRecordWriter::AddDeclarationNameInfo(
5311 const DeclarationNameInfo &NameInfo) {
5312 AddDeclarationName(NameInfo.getName());
5313 AddSourceLocation(NameInfo.getLoc());
5314 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005315}
5316
Richard Smith290d8012016-04-06 17:06:00 +00005317void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5318 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5319 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005320 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005321 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005322}
5323
Richard Smithe64e7452016-04-18 21:54:58 +00005324void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005325 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005326 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005327 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005328
5329 // Push each of the NNS's onto a stack for serialization in reverse order.
5330 while (NNS) {
5331 NestedNames.push_back(NNS);
5332 NNS = NNS->getPrefix();
5333 }
5334
Richard Smithe64e7452016-04-18 21:54:58 +00005335 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005336 while(!NestedNames.empty()) {
5337 NNS = NestedNames.pop_back_val();
5338 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005339 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005340 switch (Kind) {
5341 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005342 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005343 break;
5344
5345 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005346 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005347 break;
5348
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005349 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005350 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005351 break;
5352
Chris Lattnerca025db2010-05-07 21:43:38 +00005353 case NestedNameSpecifier::TypeSpec:
5354 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005355 AddTypeRef(QualType(NNS->getAsType(), 0));
5356 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005357 break;
5358
5359 case NestedNameSpecifier::Global:
5360 // Don't need to write an associated value.
5361 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005362
5363 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005364 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005365 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005366 }
5367 }
5368}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005369
Richard Smith290d8012016-04-06 17:06:00 +00005370void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005371 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005372 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005373 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005374
5375 // Push each of the nested-name-specifiers's onto a stack for
5376 // serialization in reverse order.
5377 while (NNS) {
5378 NestedNames.push_back(NNS);
5379 NNS = NNS.getPrefix();
5380 }
5381
Richard Smith290d8012016-04-06 17:06:00 +00005382 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005383 while(!NestedNames.empty()) {
5384 NNS = NestedNames.pop_back_val();
5385 NestedNameSpecifier::SpecifierKind Kind
5386 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005387 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005388 switch (Kind) {
5389 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005390 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5391 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005392 break;
5393
5394 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005395 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5396 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005397 break;
5398
5399 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005400 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5401 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005402 break;
5403
5404 case NestedNameSpecifier::TypeSpec:
5405 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005406 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5407 AddTypeLoc(NNS.getTypeLoc());
5408 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005409 break;
5410
5411 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005412 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005413 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005414
5415 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005416 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5417 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005418 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005419 }
5420 }
5421}
5422
Richard Smith290d8012016-04-06 17:06:00 +00005423void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005424 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005425 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005426 switch (Kind) {
5427 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005428 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005429 break;
5430
5431 case TemplateName::OverloadedTemplate: {
5432 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005433 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005434 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005435 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005436 break;
5437 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005438
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005439 case TemplateName::QualifiedTemplate: {
5440 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005441 AddNestedNameSpecifier(QualT->getQualifier());
5442 Record->push_back(QualT->hasTemplateKeyword());
5443 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005444 break;
5445 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005446
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005447 case TemplateName::DependentTemplate: {
5448 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005449 AddNestedNameSpecifier(DepT->getQualifier());
5450 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005451 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005452 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005453 else
Richard Smith290d8012016-04-06 17:06:00 +00005454 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005455 break;
5456 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005457
5458 case TemplateName::SubstTemplateTemplateParm: {
5459 SubstTemplateTemplateParmStorage *subst
5460 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005461 AddDeclRef(subst->getParameter());
5462 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005463 break;
5464 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005465
5466 case TemplateName::SubstTemplateTemplateParmPack: {
5467 SubstTemplateTemplateParmPackStorage *SubstPack
5468 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005469 AddDeclRef(SubstPack->getParameterPack());
5470 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005471 break;
5472 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005473 }
5474}
5475
Richard Smith290d8012016-04-06 17:06:00 +00005476void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5477 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005478 switch (Arg.getKind()) {
5479 case TemplateArgument::Null:
5480 break;
5481 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005482 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005483 break;
5484 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005485 AddDeclRef(Arg.getAsDecl());
5486 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005487 break;
5488 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005489 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005490 break;
5491 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005492 AddAPSInt(Arg.getAsIntegral());
5493 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005494 break;
5495 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005496 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005497 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005498 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005499 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005500 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005501 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005502 else
Richard Smith290d8012016-04-06 17:06:00 +00005503 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005504 break;
5505 case TemplateArgument::Expression:
5506 AddStmt(Arg.getAsExpr());
5507 break;
5508 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005509 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005510 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005511 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005512 break;
5513 }
5514}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005515
Richard Smithe64e7452016-04-18 21:54:58 +00005516void ASTRecordWriter::AddTemplateParameterList(
5517 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005518 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005519 AddSourceLocation(TemplateParams->getTemplateLoc());
5520 AddSourceLocation(TemplateParams->getLAngleLoc());
5521 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005522 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005523 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005524 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005525 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005526}
5527
5528/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005529void ASTRecordWriter::AddTemplateArgumentList(
5530 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005531 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005532 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005533 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005534 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005535}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005536
Richard Smith290d8012016-04-06 17:06:00 +00005537void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5538 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005539 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005540 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5541 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5542 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005543 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005544 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005545 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005546}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005547
Richard Smithe64e7452016-04-18 21:54:58 +00005548void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5549 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005550 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005551 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005552 AddDeclRef(I.getDecl());
5553 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005554 }
5555}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005556
Richard Smith290d8012016-04-06 17:06:00 +00005557// FIXME: Move this out of the main ASTRecordWriter interface.
5558void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5559 Record->push_back(Base.isVirtual());
5560 Record->push_back(Base.isBaseOfClass());
5561 Record->push_back(Base.getAccessSpecifierAsWritten());
5562 Record->push_back(Base.getInheritConstructors());
5563 AddTypeSourceInfo(Base.getTypeSourceInfo());
5564 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005565 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005566 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005567}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005568
Richard Smith645d2cf2016-04-14 00:29:55 +00005569static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5570 ArrayRef<CXXBaseSpecifier> Bases) {
5571 ASTWriter::RecordData Record;
5572 ASTRecordWriter Writer(W, Record);
5573 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005574
Richard Smith645d2cf2016-04-14 00:29:55 +00005575 for (auto &Base : Bases)
5576 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005577
Richard Smith645d2cf2016-04-14 00:29:55 +00005578 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005579}
5580
Richard Smith290d8012016-04-06 17:06:00 +00005581// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005582void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5583 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5584}
5585
Richard Smithaa165cf2016-04-13 21:57:08 +00005586static uint64_t
5587EmitCXXCtorInitializers(ASTWriter &W,
5588 ArrayRef<CXXCtorInitializer *> CtorInits) {
5589 ASTWriter::RecordData Record;
5590 ASTRecordWriter Writer(W, Record);
5591 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005592
Richard Smithaa165cf2016-04-13 21:57:08 +00005593 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005594 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005595 Writer.push_back(CTOR_INITIALIZER_BASE);
5596 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5597 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005598 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005599 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5600 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005601 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005602 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5603 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005604 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005605 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5606 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005607 }
Francois Pichetd583da02010-12-04 09:14:42 +00005608
Richard Smithaa165cf2016-04-13 21:57:08 +00005609 Writer.AddSourceLocation(Init->getMemberLocation());
5610 Writer.AddStmt(Init->getInit());
5611 Writer.AddSourceLocation(Init->getLParenLoc());
5612 Writer.AddSourceLocation(Init->getRParenLoc());
5613 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005614 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005615 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005616 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005617
5618 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005619}
5620
Richard Smithaa165cf2016-04-13 21:57:08 +00005621// FIXME: Move this out of the main ASTRecordWriter interface.
5622void ASTRecordWriter::AddCXXCtorInitializers(
5623 ArrayRef<CXXCtorInitializer *> CtorInits) {
5624 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005625}
5626
Richard Smith290d8012016-04-06 17:06:00 +00005627void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005628 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005629 Record->push_back(Data.IsLambda);
5630 Record->push_back(Data.UserDeclaredConstructor);
5631 Record->push_back(Data.UserDeclaredSpecialMembers);
5632 Record->push_back(Data.Aggregate);
5633 Record->push_back(Data.PlainOldData);
5634 Record->push_back(Data.Empty);
5635 Record->push_back(Data.Polymorphic);
5636 Record->push_back(Data.Abstract);
5637 Record->push_back(Data.IsStandardLayout);
5638 Record->push_back(Data.HasNoNonEmptyBases);
5639 Record->push_back(Data.HasPrivateFields);
5640 Record->push_back(Data.HasProtectedFields);
5641 Record->push_back(Data.HasPublicFields);
5642 Record->push_back(Data.HasMutableFields);
5643 Record->push_back(Data.HasVariantMembers);
5644 Record->push_back(Data.HasOnlyCMembers);
5645 Record->push_back(Data.HasInClassInitializer);
5646 Record->push_back(Data.HasUninitializedReferenceMember);
5647 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005648 Record->push_back(Data.HasInheritedConstructor);
5649 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005650 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5651 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5652 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5653 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5654 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5655 Record->push_back(Data.DefaultedDestructorIsDeleted);
5656 Record->push_back(Data.HasTrivialSpecialMembers);
5657 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5658 Record->push_back(Data.HasIrrelevantDestructor);
5659 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5660 Record->push_back(Data.HasDefaultedDefaultConstructor);
5661 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5662 Record->push_back(Data.HasConstexprDefaultConstructor);
5663 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5664 Record->push_back(Data.ComputedVisibleConversions);
5665 Record->push_back(Data.UserProvidedDefaultConstructor);
5666 Record->push_back(Data.DeclaredSpecialMembers);
5667 Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
5668 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5669 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5670 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005671 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005672
Richard Smith290d8012016-04-06 17:06:00 +00005673 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005674 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005675 AddCXXBaseSpecifiers(Data.bases());
5676
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005677 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005678 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005679 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005680 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005681
Richard Smith290d8012016-04-06 17:06:00 +00005682 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5683 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005684 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005685 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005686
5687 // Add lambda-specific data.
5688 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005689 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005690 Record->push_back(Lambda.Dependent);
5691 Record->push_back(Lambda.IsGenericLambda);
5692 Record->push_back(Lambda.CaptureDefault);
5693 Record->push_back(Lambda.NumCaptures);
5694 Record->push_back(Lambda.NumExplicitCaptures);
5695 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005696 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005697 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005698 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005699 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005700 AddSourceLocation(Capture.getLocation());
5701 Record->push_back(Capture.isImplicit());
5702 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005703 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005704 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005705 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005706 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005707 break;
5708 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005709 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005710 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005711 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005712 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005713 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005714 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005715 break;
5716 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005717 }
5718 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005719}
5720
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005721void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005722 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005723 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005724 assert(FirstDeclID == NextDeclID &&
5725 FirstTypeID == NextTypeID &&
5726 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005727 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005728 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005729 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005730 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005731
Sebastian Redl07a89a82010-07-30 00:29:29 +00005732 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005733
Richard Smith7f330cd2015-03-18 01:42:29 +00005734 // Note, this will get called multiple times, once one the reader starts up
5735 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005736 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5737 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5738 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005739 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005740 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005741 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005742 NextDeclID = FirstDeclID;
5743 NextTypeID = FirstTypeID;
5744 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005745 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005746 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005747 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005748}
5749
Sebastian Redl539c5062010-08-18 23:57:32 +00005750void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005751 // Always keep the highest ID. See \p TypeRead() for more information.
5752 IdentID &StoredID = IdentifierIDs[II];
5753 if (ID > StoredID)
5754 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005755}
5756
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005757void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005758 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005759 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005760 if (ID > StoredID)
5761 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005762}
5763
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005764void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005765 // Always take the highest-numbered type index. This copes with an interesting
5766 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005767 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005768 // keep the higher-numbered entry so that we can properly write it out to
5769 // the AST file.
5770 TypeIdx &StoredIdx = TypeIdxs[T];
5771 if (Idx.getIndex() >= StoredIdx.getIndex())
5772 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005773}
5774
Sebastian Redl539c5062010-08-18 23:57:32 +00005775void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005776 // Always keep the highest ID. See \p TypeRead() for more information.
5777 SelectorID &StoredID = SelectorIDs[S];
5778 if (ID > StoredID)
5779 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005780}
Douglas Gregor91096292010-10-02 19:29:26 +00005781
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005782void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005783 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005784 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005785 MacroDefinitions[MD] = ID;
5786}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005787
Douglas Gregore37a85a2011-12-02 17:30:13 +00005788void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5789 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5790 SubmoduleIDs[Mod] = ID;
5791}
5792
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005793void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005794 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005795 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005796 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005797 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005798 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005799 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005800 // A forward reference was mutated into a definition. Rewrite it.
5801 // FIXME: This happens during template instantiation, should we
5802 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005803 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5804 "completed a tag from another module but not by instantiation?");
5805 DeclUpdates[RD].push_back(
5806 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005807 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005808 }
5809}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005810
Richard Smithf983f7f2015-10-13 00:23:25 +00005811static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5812 if (D->isFromASTFile())
5813 return true;
5814
Richard Smithf983f7f2015-10-13 00:23:25 +00005815 // The predefined __va_list_tag struct is imported if we imported any decls.
5816 // FIXME: This is a gross hack.
5817 return D == D->getASTContext().getVaListTagDecl();
5818}
5819
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005820void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005821 if (Chain && Chain->isProcessingUpdateRecords()) return;
5822 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005823 "Should not add lookup results to non-lookup contexts!");
5824
Vassil Vassilev632eac32016-03-16 11:17:04 +00005825 // TU is handled elsewhere.
5826 if (isa<TranslationUnitDecl>(DC))
5827 return;
5828
5829 // Namespaces are handled elsewhere, except for template instantiations of
5830 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5831 // local instantiations are added to an imported context. Only happens when
5832 // adding ADL lookup candidates, for example templated friends.
5833 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5834 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005835 return;
5836
Richard Smithf983f7f2015-10-13 00:23:25 +00005837 // We're only interested in cases where a local declaration is added to an
5838 // imported context.
5839 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5840 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005841
Richard Smith0f4e2c42015-08-06 04:23:48 +00005842 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005843 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005844 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005845 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5846 // We're adding a visible declaration to a predefined decl context. Ensure
5847 // that we write out all of its lookup results so we don't get a nasty
5848 // surprise when we try to emit its lookup table.
5849 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005850 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005851 }
Richard Smithc26d9742016-10-06 20:30:51 +00005852 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005853}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005854
5855void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005856 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005857 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005858
5859 // We're only interested in cases where a local declaration is added to an
5860 // imported context.
5861 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5862 return;
5863
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005864 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005865 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005866
5867 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005868 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005869 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005870 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005871}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005872
Richard Smith564417a2014-03-20 21:47:22 +00005873void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005874 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005875 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5876 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005877 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005878 // If we don't already know the exception specification for this redecl
5879 // chain, add an update record for it.
5880 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5881 ->getType()
5882 ->castAs<FunctionProtoType>()
5883 ->getExceptionSpecType()))
5884 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5885 });
Richard Smith564417a2014-03-20 21:47:22 +00005886}
5887
Richard Smith1fa5d642013-05-11 05:45:24 +00005888void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005889 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005890 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005891 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005892 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005893 DeclUpdates[D].push_back(
5894 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5895 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005896}
5897
Richard Smithf8134002015-03-10 01:41:22 +00005898void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5899 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005900 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005901 assert(!WritingAST && "Already writing the AST!");
5902 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005903 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005904 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005905 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5906 });
Richard Smithf8134002015-03-10 01:41:22 +00005907}
5908
Sebastian Redlab238a72011-04-24 16:28:06 +00005909void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005910 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005911 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005912 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005913 return; // Declaration not imported from PCH.
5914
Richard Smith4d235792014-08-07 18:53:08 +00005915 // Implicit function decl from a PCH was defined.
5916 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005917}
5918
Richard Smithd28ac5b2014-03-22 23:33:22 +00005919void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005920 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005921 assert(!WritingAST && "Already writing the AST!");
5922 if (!D->isFromASTFile())
5923 return;
5924
Richard Smith9e2341d2015-03-23 03:25:59 +00005925 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005926}
5927
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005928void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005929 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005930 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005931 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005932 return;
5933
5934 // Since the actual instantiation is delayed, this really means that we need
5935 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005936 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005937 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5938 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005939}
5940
John McCall32791cc2016-01-06 22:34:54 +00005941void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005942 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005943 assert(!WritingAST && "Already writing the AST!");
5944 if (!D->isFromASTFile())
5945 return;
5946
5947 DeclUpdates[D].push_back(
5948 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5949}
5950
Richard Smith4b054b22016-08-24 21:25:37 +00005951void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5952 assert(!WritingAST && "Already writing the AST!");
5953 if (!D->isFromASTFile())
5954 return;
5955
5956 DeclUpdates[D].push_back(
5957 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5958}
5959
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005960void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5961 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005962 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005963 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005964 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005965 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005966
5967 assert(IFD->getDefinition() && "Category on a class without a definition?");
5968 ObjCClassesWithCategories.insert(
5969 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005970}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005971
Eli Friedman276dd182013-09-05 00:02:25 +00005972void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005973 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005974 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005975
5976 // If there is *any* declaration of the entity that's not from an AST file,
5977 // we can skip writing the update record. We make sure that isUsed() triggers
5978 // completion of the redeclaration chain of the entity.
5979 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5980 if (IsLocalDecl(Prev))
5981 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005982
Aaron Ballman4f45b712014-03-21 15:22:56 +00005983 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005984}
Alexey Bataev97720002014-11-11 04:05:39 +00005985
5986void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005987 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005988 assert(!WritingAST && "Already writing the AST!");
5989 if (!D->isFromASTFile())
5990 return;
5991
5992 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5993}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005994
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005995void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5996 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005997 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005998 assert(!WritingAST && "Already writing the AST!");
5999 if (!D->isFromASTFile())
6000 return;
6001
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006002 DeclUpdates[D].push_back(
6003 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006004}
6005
Richard Smith4caa4492015-05-15 02:34:32 +00006006void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006007 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006008 assert(!WritingAST && "Already writing the AST!");
6009 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006010 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006011}
Alex Denisovfde64952015-06-26 05:28:36 +00006012
6013void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6014 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006015 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006016 assert(!WritingAST && "Already writing the AST!");
6017 if (!Record->isFromASTFile())
6018 return;
6019 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6020}
Richard Smithc26d9742016-10-06 20:30:51 +00006021
6022void ASTWriter::AddedCXXTemplateSpecialization(
6023 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6024 assert(!WritingAST && "Already writing the AST!");
6025
6026 if (!TD->getFirstDecl()->isFromASTFile())
6027 return;
6028 if (Chain && Chain->isProcessingUpdateRecords())
6029 return;
6030
6031 DeclsToEmitEvenIfUnreferenced.push_back(D);
6032}
6033
6034void ASTWriter::AddedCXXTemplateSpecialization(
6035 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6036 assert(!WritingAST && "Already writing the AST!");
6037
6038 if (!TD->getFirstDecl()->isFromASTFile())
6039 return;
6040 if (Chain && Chain->isProcessingUpdateRecords())
6041 return;
6042
6043 DeclsToEmitEvenIfUnreferenced.push_back(D);
6044}
6045
6046void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6047 const FunctionDecl *D) {
6048 assert(!WritingAST && "Already writing the AST!");
6049
6050 if (!TD->getFirstDecl()->isFromASTFile())
6051 return;
6052 if (Chain && Chain->isProcessingUpdateRecords())
6053 return;
6054
6055 DeclsToEmitEvenIfUnreferenced.push_back(D);
6056}