blob: cdc1cf5bcb0e42d010706a3d70d0dfa4f347bc06 [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
Richard Smith600b5262017-01-26 20:40:47 +0000352void ASTTypeWriter::VisitDeducedTemplateSpecializationType(
353 const DeducedTemplateSpecializationType *T) {
354 Record.AddTemplateName(T->getTemplateName());
355 Record.AddTypeRef(T->getDeducedType());
356 if (T->getDeducedType().isNull())
357 Record.push_back(T->isDependentType());
358 Code = TYPE_DEDUCED_TEMPLATE_SPECIALIZATION;
359}
360
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000361void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000362 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000363 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000364 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000365 "Cannot serialize in the middle of a type definition");
366}
367
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000368void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000369 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000370 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000371}
372
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000373void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000374 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000375 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000376}
377
John McCall81904512011-01-06 01:58:22 +0000378void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000379 Record.AddTypeRef(T->getModifiedType());
380 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000381 Record.push_back(T->getAttrKind());
382 Code = TYPE_ATTRIBUTED;
383}
384
Mike Stump11289f42009-09-09 15:08:12 +0000385void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000386ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000387 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000388 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
389 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000390 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000391}
392
393void
Douglas Gregorada4b792011-01-14 02:55:32 +0000394ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
395 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000396 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
397 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000398 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
399}
400
401void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000402ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000403 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000404 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000405 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000406 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000407 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000408 Record.AddTemplateArgument(ArgI);
409 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
410 : T->isCanonicalUnqualified()
411 ? QualType()
412 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000413 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000414}
415
416void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000417ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000418 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000419 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000420 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000421 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000422}
423
424void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000425ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000426 const DependentSizedExtVectorType *T) {
427 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000428 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000429}
430
431void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000432ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000433 Record.push_back(T->getDepth());
434 Record.push_back(T->getIndex());
435 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000436 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000437 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000438}
439
440void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000441ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000442 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000443 Record.AddNestedNameSpecifier(T->getQualifier());
444 Record.AddIdentifierRef(T->getIdentifier());
445 Record.AddTypeRef(
446 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000447 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000448}
449
450void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000451ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000452 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000453 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000454 Record.AddNestedNameSpecifier(T->getQualifier());
455 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000456 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000457 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000458 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000459 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000460}
461
Douglas Gregord2fa7662010-12-20 02:24:11 +0000462void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000463 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000464 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000465 Record.push_back(*NumExpansions + 1);
466 else
467 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000468 Code = TYPE_PACK_EXPANSION;
469}
470
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000471void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000472 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000473 Code = TYPE_PAREN;
474}
475
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000476void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000477 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000478 Record.AddNestedNameSpecifier(T->getQualifier());
479 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000480 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000481}
482
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000483void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000484 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
485 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000486 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000487}
488
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000489void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000490 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000491 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000492}
493
Manman Rene6be26c2016-09-13 17:25:08 +0000494void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
495 Record.AddDeclRef(T->getDecl());
496 Record.push_back(T->getNumProtocols());
497 for (const auto *I : T->quals())
498 Record.AddDeclRef(I);
499 Code = TYPE_OBJC_TYPE_PARAM;
500}
501
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000502void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000503 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000504 Record.push_back(T->getTypeArgsAsWritten().size());
505 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000506 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000507 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000508 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000509 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000510 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000511 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000512}
513
Steve Narofffb4330f2009-06-17 22:40:22 +0000514void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000515ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000516 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000517 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000518}
519
Eli Friedman0dfb8892011-10-06 23:00:33 +0000520void
521ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000522 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000523 Code = TYPE_ATOMIC;
524}
525
Xiuli Pan9c14e282016-01-09 12:53:17 +0000526void
527ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000528 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000529 Record.push_back(T->isReadOnly());
530 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000531}
532
John McCall8f115c62009-10-16 21:56:05 +0000533namespace {
534
535class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000536 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000537
538public:
Richard Smith290d8012016-04-06 17:06:00 +0000539 TypeLocWriter(ASTRecordWriter &Record)
540 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000541
John McCall17001972009-10-18 01:05:36 +0000542#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000543#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000544 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000545#include "clang/AST/TypeLocNodes.def"
546
John McCall17001972009-10-18 01:05:36 +0000547 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
548 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000549};
550
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000551} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000552
John McCall17001972009-10-18 01:05:36 +0000553void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
554 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000555}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000556
John McCall17001972009-10-18 01:05:36 +0000557void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000558 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000559 if (TL.needsExtraLocalData()) {
560 Record.push_back(TL.getWrittenTypeSpec());
561 Record.push_back(TL.getWrittenSignSpec());
562 Record.push_back(TL.getWrittenWidthSpec());
563 Record.push_back(TL.hasModeAttr());
564 }
John McCall8f115c62009-10-16 21:56:05 +0000565}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000566
John McCall17001972009-10-18 01:05:36 +0000567void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000568 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000569}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000570
John McCall17001972009-10-18 01:05:36 +0000571void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000572 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000573}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000574
Reid Kleckner8a365022013-06-24 17:51:48 +0000575void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
576 // nothing to do
577}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000578
Reid Kleckner0503a872013-12-05 01:23:43 +0000579void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
580 // nothing to do
581}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000582
John McCall17001972009-10-18 01:05:36 +0000583void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000584 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000585}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000586
John McCall17001972009-10-18 01:05:36 +0000587void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000588 Record.AddSourceLocation(TL.getAmpLoc());
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::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000592 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000593}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000594
John McCall17001972009-10-18 01:05:36 +0000595void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000596 Record.AddSourceLocation(TL.getStarLoc());
597 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000598}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000599
John McCall17001972009-10-18 01:05:36 +0000600void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000601 Record.AddSourceLocation(TL.getLBracketLoc());
602 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000603 Record.push_back(TL.getSizeExpr() ? 1 : 0);
604 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000605 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000606}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000607
John McCall17001972009-10-18 01:05:36 +0000608void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
609 VisitArrayTypeLoc(TL);
610}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000611
John McCall17001972009-10-18 01:05:36 +0000612void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
613 VisitArrayTypeLoc(TL);
614}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000615
John McCall17001972009-10-18 01:05:36 +0000616void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
617 VisitArrayTypeLoc(TL);
618}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000619
John McCall17001972009-10-18 01:05:36 +0000620void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
621 DependentSizedArrayTypeLoc TL) {
622 VisitArrayTypeLoc(TL);
623}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000624
John McCall17001972009-10-18 01:05:36 +0000625void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
626 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000627 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000628}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000629
John McCall17001972009-10-18 01:05:36 +0000630void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000631 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000632}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000633
John McCall17001972009-10-18 01:05:36 +0000634void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000635 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000636}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000637
John McCall17001972009-10-18 01:05:36 +0000638void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000639 Record.AddSourceLocation(TL.getLocalRangeBegin());
640 Record.AddSourceLocation(TL.getLParenLoc());
641 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000642 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000643 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000644 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000645 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000646}
647void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
648 VisitFunctionTypeLoc(TL);
649}
650void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
651 VisitFunctionTypeLoc(TL);
652}
John McCallb96ec562009-12-04 22:46:56 +0000653void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000654 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000655}
John McCall17001972009-10-18 01:05:36 +0000656void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000657 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000658}
Manman Rene6be26c2016-09-13 17:25:08 +0000659void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
660 if (TL.getNumProtocols()) {
661 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
662 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
663 }
664 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
665 Record.AddSourceLocation(TL.getProtocolLoc(i));
666}
John McCall17001972009-10-18 01:05:36 +0000667void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000668 Record.AddSourceLocation(TL.getTypeofLoc());
669 Record.AddSourceLocation(TL.getLParenLoc());
670 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000671}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000672
John McCall17001972009-10-18 01:05:36 +0000673void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000674 Record.AddSourceLocation(TL.getTypeofLoc());
675 Record.AddSourceLocation(TL.getLParenLoc());
676 Record.AddSourceLocation(TL.getRParenLoc());
677 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000678}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000679
John McCall17001972009-10-18 01:05:36 +0000680void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000681 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000682}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000683
Alexis Hunte852b102011-05-24 22:41:36 +0000684void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000685 Record.AddSourceLocation(TL.getKWLoc());
686 Record.AddSourceLocation(TL.getLParenLoc());
687 Record.AddSourceLocation(TL.getRParenLoc());
688 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000689}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000690
Richard Smith30482bc2011-02-20 03:19:35 +0000691void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000692 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000693}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000694
Richard Smith600b5262017-01-26 20:40:47 +0000695void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
696 DeducedTemplateSpecializationTypeLoc TL) {
697 Record.AddSourceLocation(TL.getTemplateNameLoc());
698}
699
John McCall17001972009-10-18 01:05:36 +0000700void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000701 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000702}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000703
John McCall17001972009-10-18 01:05:36 +0000704void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000705 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000706}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000707
John McCall81904512011-01-06 01:58:22 +0000708void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000709 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000710 if (TL.hasAttrOperand()) {
711 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000712 Record.AddSourceLocation(range.getBegin());
713 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000714 }
715 if (TL.hasAttrExprOperand()) {
716 Expr *operand = TL.getAttrExprOperand();
717 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000718 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000719 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000720 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000721 }
722}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000723
John McCall17001972009-10-18 01:05:36 +0000724void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000725 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000726}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000727
John McCallcebee162009-10-18 09:09:24 +0000728void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
729 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000730 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000731}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000732
Douglas Gregorada4b792011-01-14 02:55:32 +0000733void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
734 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000735 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000736}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000737
John McCall17001972009-10-18 01:05:36 +0000738void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
739 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000740 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
741 Record.AddSourceLocation(TL.getTemplateNameLoc());
742 Record.AddSourceLocation(TL.getLAngleLoc());
743 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000744 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000745 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
746 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000747}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000748
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000749void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000750 Record.AddSourceLocation(TL.getLParenLoc());
751 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000752}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000753
Abramo Bagnara6150c882010-05-11 21:36:43 +0000754void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000755 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
756 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000757}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000758
John McCalle78aac42010-03-10 03:28:59 +0000759void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000760 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000761}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000762
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000763void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000764 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
765 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
766 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000767}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000768
John McCallc392f372010-06-11 00:33:02 +0000769void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
770 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000771 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
772 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
773 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
774 Record.AddSourceLocation(TL.getTemplateNameLoc());
775 Record.AddSourceLocation(TL.getLAngleLoc());
776 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000777 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000778 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
779 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000780}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000781
Douglas Gregord2fa7662010-12-20 02:24:11 +0000782void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000783 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000784}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000785
John McCall17001972009-10-18 01:05:36 +0000786void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000787 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000788}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000789
John McCall8b07ec22010-05-15 11:32:37 +0000790void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
791 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000792 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
793 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000794 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000795 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
796 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
797 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000798 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000799 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000800}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000801
John McCallfc93cf92009-10-22 22:37:11 +0000802void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000803 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000804}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000805
Eli Friedman0dfb8892011-10-06 23:00:33 +0000806void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000807 Record.AddSourceLocation(TL.getKWLoc());
808 Record.AddSourceLocation(TL.getLParenLoc());
809 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000810}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000811
Xiuli Pan9c14e282016-01-09 12:53:17 +0000812void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000813 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000814}
John McCall8f115c62009-10-16 21:56:05 +0000815
Richard Smith01b2cb42014-07-26 06:37:51 +0000816void ASTWriter::WriteTypeAbbrevs() {
817 using namespace llvm;
818
David Blaikieb44f0bf2017-01-04 22:36:43 +0000819 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000820
821 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000822 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000823 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
824 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
825 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000826 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000827
828 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000829 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000830 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
831 // FunctionType
832 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
833 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
834 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
835 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
836 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
837 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
838 // FunctionProtoType
839 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
840 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
841 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
842 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
843 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
844 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
845 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
846 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000847 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000848}
849
Chris Lattner19cea4e2009-04-22 05:57:30 +0000850//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000851// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000852//===----------------------------------------------------------------------===//
853
Chris Lattner28fa4e62009-04-26 22:26:21 +0000854static void EmitBlockID(unsigned ID, const char *Name,
855 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000856 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000857 Record.clear();
858 Record.push_back(ID);
859 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
860
861 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000862 if (!Name || Name[0] == 0)
863 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000864 Record.clear();
865 while (*Name)
866 Record.push_back(*Name++);
867 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
868}
869
870static void EmitRecordID(unsigned ID, const char *Name,
871 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000872 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000873 Record.clear();
874 Record.push_back(ID);
875 while (*Name)
876 Record.push_back(*Name++);
877 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000878}
879
880static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000881 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000882#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000883 RECORD(STMT_STOP);
884 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000885 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000886 RECORD(STMT_NULL);
887 RECORD(STMT_COMPOUND);
888 RECORD(STMT_CASE);
889 RECORD(STMT_DEFAULT);
890 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000891 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000892 RECORD(STMT_IF);
893 RECORD(STMT_SWITCH);
894 RECORD(STMT_WHILE);
895 RECORD(STMT_DO);
896 RECORD(STMT_FOR);
897 RECORD(STMT_GOTO);
898 RECORD(STMT_INDIRECT_GOTO);
899 RECORD(STMT_CONTINUE);
900 RECORD(STMT_BREAK);
901 RECORD(STMT_RETURN);
902 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000903 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000904 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000905 RECORD(EXPR_PREDEFINED);
906 RECORD(EXPR_DECL_REF);
907 RECORD(EXPR_INTEGER_LITERAL);
908 RECORD(EXPR_FLOATING_LITERAL);
909 RECORD(EXPR_IMAGINARY_LITERAL);
910 RECORD(EXPR_STRING_LITERAL);
911 RECORD(EXPR_CHARACTER_LITERAL);
912 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000913 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000914 RECORD(EXPR_UNARY_OPERATOR);
915 RECORD(EXPR_SIZEOF_ALIGN_OF);
916 RECORD(EXPR_ARRAY_SUBSCRIPT);
917 RECORD(EXPR_CALL);
918 RECORD(EXPR_MEMBER);
919 RECORD(EXPR_BINARY_OPERATOR);
920 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
921 RECORD(EXPR_CONDITIONAL_OPERATOR);
922 RECORD(EXPR_IMPLICIT_CAST);
923 RECORD(EXPR_CSTYLE_CAST);
924 RECORD(EXPR_COMPOUND_LITERAL);
925 RECORD(EXPR_EXT_VECTOR_ELEMENT);
926 RECORD(EXPR_INIT_LIST);
927 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000928 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000929 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000930 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000931 RECORD(EXPR_VA_ARG);
932 RECORD(EXPR_ADDR_LABEL);
933 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000934 RECORD(EXPR_CHOOSE);
935 RECORD(EXPR_GNU_NULL);
936 RECORD(EXPR_SHUFFLE_VECTOR);
937 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000938 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000939 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000940 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000941 RECORD(EXPR_OBJC_ARRAY_LITERAL);
942 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000943 RECORD(EXPR_OBJC_ENCODE);
944 RECORD(EXPR_OBJC_SELECTOR_EXPR);
945 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
946 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
947 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
948 RECORD(EXPR_OBJC_KVC_REF_EXPR);
949 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000950 RECORD(STMT_OBJC_FOR_COLLECTION);
951 RECORD(STMT_OBJC_CATCH);
952 RECORD(STMT_OBJC_FINALLY);
953 RECORD(STMT_OBJC_AT_TRY);
954 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
955 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000956 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000957 RECORD(STMT_CXX_CATCH);
958 RECORD(STMT_CXX_TRY);
959 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000960 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000961 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000962 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000963 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000964 RECORD(EXPR_CXX_STATIC_CAST);
965 RECORD(EXPR_CXX_DYNAMIC_CAST);
966 RECORD(EXPR_CXX_REINTERPRET_CAST);
967 RECORD(EXPR_CXX_CONST_CAST);
968 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000969 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000970 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000971 RECORD(EXPR_CXX_BOOL_LITERAL);
972 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000973 RECORD(EXPR_CXX_TYPEID_EXPR);
974 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000975 RECORD(EXPR_CXX_THIS);
976 RECORD(EXPR_CXX_THROW);
977 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000978 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000979 RECORD(EXPR_CXX_BIND_TEMPORARY);
980 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
981 RECORD(EXPR_CXX_NEW);
982 RECORD(EXPR_CXX_DELETE);
983 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
984 RECORD(EXPR_EXPR_WITH_CLEANUPS);
985 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
986 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
987 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
988 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
989 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000990 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000991 RECORD(EXPR_CXX_NOEXCEPT);
992 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000993 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
994 RECORD(EXPR_TYPE_TRAIT);
995 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000996 RECORD(EXPR_PACK_EXPANSION);
997 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000998 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000999 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001000 RECORD(EXPR_FUNCTION_PARM_PACK);
1001 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001002 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001003 RECORD(EXPR_CXX_UUIDOF_EXPR);
1004 RECORD(EXPR_CXX_UUIDOF_TYPE);
1005 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001006#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001007}
Mike Stump11289f42009-09-09 15:08:12 +00001008
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001009void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001010 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001011 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001012
Sebastian Redl539c5062010-08-18 23:57:32 +00001013#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1014#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001015
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001016 // Control Block.
1017 BLOCK(CONTROL_BLOCK);
1018 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +00001019 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001020 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001021 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001022 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001023 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001024 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001025 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001026 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001027 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001028
1029 BLOCK(OPTIONS_BLOCK);
1030 RECORD(LANGUAGE_OPTIONS);
1031 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001032 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001033 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001034 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001035 RECORD(PREPROCESSOR_OPTIONS);
1036
Douglas Gregor108cb222012-10-19 00:45:00 +00001037 BLOCK(INPUT_FILES_BLOCK);
1038 RECORD(INPUT_FILE);
1039
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001040 // AST Top-Level Block.
1041 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001042 RECORD(TYPE_OFFSET);
1043 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001044 RECORD(IDENTIFIER_OFFSET);
1045 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001046 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001047 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001048 RECORD(SPECIAL_TYPES);
1049 RECORD(STATISTICS);
1050 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001051 RECORD(SELECTOR_OFFSETS);
1052 RECORD(METHOD_POOL);
1053 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001054 RECORD(SOURCE_LOCATION_OFFSETS);
1055 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001056 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001057 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001058 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001059 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001060 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001061 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001062 RECORD(SEMA_DECL_REFS);
1063 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1064 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001065 RECORD(UPDATE_VISIBLE);
1066 RECORD(DECL_UPDATE_OFFSETS);
1067 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001068 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001069 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001070 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001071 RECORD(FP_PRAGMA_OPTIONS);
1072 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001073 RECORD(OPENCL_EXTENSION_TYPES);
1074 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001075 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001076 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001077 RECORD(MODULE_OFFSET_MAP);
1078 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001079 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001080 RECORD(FILE_SORTED_DECLS);
1081 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001082 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001083 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001084 RECORD(INTERESTING_IDENTIFIERS);
1085 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001086 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001087 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001088 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001089 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001090 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001091 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001092 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001093
Chris Lattner28fa4e62009-04-26 22:26:21 +00001094 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001095 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001096 RECORD(SM_SLOC_FILE_ENTRY);
1097 RECORD(SM_SLOC_BUFFER_ENTRY);
1098 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001099 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001100 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001101
Chris Lattner28fa4e62009-04-26 22:26:21 +00001102 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001103 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001104 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001105 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001106 RECORD(PP_MACRO_OBJECT_LIKE);
1107 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001108 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001109
Richard Smithfa715652015-08-31 22:43:10 +00001110 // Submodule Block.
1111 BLOCK(SUBMODULE_BLOCK);
1112 RECORD(SUBMODULE_METADATA);
1113 RECORD(SUBMODULE_DEFINITION);
1114 RECORD(SUBMODULE_UMBRELLA_HEADER);
1115 RECORD(SUBMODULE_HEADER);
1116 RECORD(SUBMODULE_TOPHEADER);
1117 RECORD(SUBMODULE_UMBRELLA_DIR);
1118 RECORD(SUBMODULE_IMPORTS);
1119 RECORD(SUBMODULE_EXPORTS);
1120 RECORD(SUBMODULE_REQUIRES);
1121 RECORD(SUBMODULE_EXCLUDED_HEADER);
1122 RECORD(SUBMODULE_LINK_LIBRARY);
1123 RECORD(SUBMODULE_CONFIG_MACRO);
1124 RECORD(SUBMODULE_CONFLICT);
1125 RECORD(SUBMODULE_PRIVATE_HEADER);
1126 RECORD(SUBMODULE_TEXTUAL_HEADER);
1127 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001128 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001129
1130 // Comments Block.
1131 BLOCK(COMMENTS_BLOCK);
1132 RECORD(COMMENTS_RAW_COMMENT);
1133
Douglas Gregor12bfa382009-10-17 00:13:19 +00001134 // Decls and Types block.
1135 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001136 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001137 RECORD(TYPE_COMPLEX);
1138 RECORD(TYPE_POINTER);
1139 RECORD(TYPE_BLOCK_POINTER);
1140 RECORD(TYPE_LVALUE_REFERENCE);
1141 RECORD(TYPE_RVALUE_REFERENCE);
1142 RECORD(TYPE_MEMBER_POINTER);
1143 RECORD(TYPE_CONSTANT_ARRAY);
1144 RECORD(TYPE_INCOMPLETE_ARRAY);
1145 RECORD(TYPE_VARIABLE_ARRAY);
1146 RECORD(TYPE_VECTOR);
1147 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001148 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001149 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001150 RECORD(TYPE_TYPEDEF);
1151 RECORD(TYPE_TYPEOF_EXPR);
1152 RECORD(TYPE_TYPEOF);
1153 RECORD(TYPE_RECORD);
1154 RECORD(TYPE_ENUM);
1155 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001156 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001157 RECORD(TYPE_DECLTYPE);
1158 RECORD(TYPE_ELABORATED);
1159 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1160 RECORD(TYPE_UNRESOLVED_USING);
1161 RECORD(TYPE_INJECTED_CLASS_NAME);
1162 RECORD(TYPE_OBJC_OBJECT);
1163 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1164 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1165 RECORD(TYPE_DEPENDENT_NAME);
1166 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1167 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1168 RECORD(TYPE_PAREN);
1169 RECORD(TYPE_PACK_EXPANSION);
1170 RECORD(TYPE_ATTRIBUTED);
1171 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001172 RECORD(TYPE_AUTO);
1173 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001174 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001175 RECORD(TYPE_DECAYED);
1176 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001177 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001178 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001179 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001180 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001181 RECORD(DECL_ENUM);
1182 RECORD(DECL_RECORD);
1183 RECORD(DECL_ENUM_CONSTANT);
1184 RECORD(DECL_FUNCTION);
1185 RECORD(DECL_OBJC_METHOD);
1186 RECORD(DECL_OBJC_INTERFACE);
1187 RECORD(DECL_OBJC_PROTOCOL);
1188 RECORD(DECL_OBJC_IVAR);
1189 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001190 RECORD(DECL_OBJC_CATEGORY);
1191 RECORD(DECL_OBJC_CATEGORY_IMPL);
1192 RECORD(DECL_OBJC_IMPLEMENTATION);
1193 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1194 RECORD(DECL_OBJC_PROPERTY);
1195 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001196 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001197 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001198 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001199 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001200 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001201 RECORD(DECL_FILE_SCOPE_ASM);
1202 RECORD(DECL_BLOCK);
1203 RECORD(DECL_CONTEXT_LEXICAL);
1204 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001205 RECORD(DECL_NAMESPACE);
1206 RECORD(DECL_NAMESPACE_ALIAS);
1207 RECORD(DECL_USING);
1208 RECORD(DECL_USING_SHADOW);
1209 RECORD(DECL_USING_DIRECTIVE);
1210 RECORD(DECL_UNRESOLVED_USING_VALUE);
1211 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1212 RECORD(DECL_LINKAGE_SPEC);
1213 RECORD(DECL_CXX_RECORD);
1214 RECORD(DECL_CXX_METHOD);
1215 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001216 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001217 RECORD(DECL_CXX_DESTRUCTOR);
1218 RECORD(DECL_CXX_CONVERSION);
1219 RECORD(DECL_ACCESS_SPEC);
1220 RECORD(DECL_FRIEND);
1221 RECORD(DECL_FRIEND_TEMPLATE);
1222 RECORD(DECL_CLASS_TEMPLATE);
1223 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1224 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001225 RECORD(DECL_VAR_TEMPLATE);
1226 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1227 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001228 RECORD(DECL_FUNCTION_TEMPLATE);
1229 RECORD(DECL_TEMPLATE_TYPE_PARM);
1230 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1231 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001232 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001233 RECORD(DECL_STATIC_ASSERT);
1234 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001235 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001236 RECORD(DECL_INDIRECTFIELD);
1237 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001238 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1239 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1240 RECORD(DECL_IMPORT);
1241 RECORD(DECL_OMP_THREADPRIVATE);
1242 RECORD(DECL_EMPTY);
1243 RECORD(DECL_OBJC_TYPE_PARAM);
1244 RECORD(DECL_OMP_CAPTUREDEXPR);
1245 RECORD(DECL_PRAGMA_COMMENT);
1246 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1247 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001248
Douglas Gregor03412ba2011-06-03 02:27:19 +00001249 // Statements and Exprs can occur in the Decls and Types block.
1250 AddStmtsExprs(Stream, Record);
1251
Douglas Gregor92a96f52011-02-08 21:58:10 +00001252 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001253 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001254 RECORD(PPD_MACRO_DEFINITION);
1255 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001256
1257 // Decls and Types block.
1258 BLOCK(EXTENSION_BLOCK);
1259 RECORD(EXTENSION_METADATA);
1260
Chris Lattner28fa4e62009-04-26 22:26:21 +00001261#undef RECORD
1262#undef BLOCK
1263 Stream.ExitBlock();
1264}
1265
Richard Smith54cc3c22014-12-11 20:50:24 +00001266/// \brief Prepares a path for being written to an AST file by converting it
1267/// to an absolute path and removing nested './'s.
1268///
1269/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001270static bool cleanPathForOutput(FileManager &FileMgr,
1271 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001272 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001273 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001274}
1275
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001276/// \brief Adjusts the given filename to only write out the portion of the
1277/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001278///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001279/// \param Filename the file name to adjust.
1280///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001281/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1282/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001283///
1284/// \returns either the original filename (if it needs no adjustment) or the
1285/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001286static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001287adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001288 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001289
Richard Smith7ed1bc92014-12-05 22:42:13 +00001290 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001291 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001292
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001293 // Verify that the filename and the system root have the same prefix.
1294 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001295 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1296 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001297 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001299 // We hit the end of the filename before we hit the end of the system root.
1300 if (!Filename[Pos])
1301 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001302
Richard Smith7ed1bc92014-12-05 22:42:13 +00001303 // If there's not a path separator at the end of the base directory nor
1304 // immediately after it, then this isn't within the base directory.
1305 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1306 if (!llvm::sys::path::is_separator(BaseDir.back()))
1307 return Filename;
1308 } else {
1309 // If the file name has a '/' at the current position, skip over the '/'.
1310 // We distinguish relative paths from absolute paths by the
1311 // absence of '/' at the beginning of relative paths.
1312 //
1313 // FIXME: This is wrong. We distinguish them by asking if the path is
1314 // absolute, which isn't the same thing. And there might be multiple '/'s
1315 // in a row. Use a better mechanism to indicate whether we have emitted an
1316 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001317 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001318 }
Mike Stump11289f42009-09-09 15:08:12 +00001319
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001320 return Filename + Pos;
1321}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001322
Ben Langmuir487ea142014-10-23 18:05:36 +00001323static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001324 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001325 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1326 return S;
1327 // Rely on GetRandomNumber to eventually return non-zero...
1328 }
1329}
1330
Douglas Gregor112b9072012-10-18 05:31:06 +00001331/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001332uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1333 ASTContext &Context,
1334 StringRef isysroot,
1335 const std::string &OutputFile) {
1336 ASTFileSignature Signature = 0;
1337
Douglas Gregorbfbde532009-04-10 21:16:55 +00001338 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001339 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001340 RecordData Record;
1341
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001342 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001343 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001344 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1345 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1346 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1347 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1348 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1349 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001350 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001351 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1352 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001353 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001354 assert((!WritingModule || isysroot.empty()) &&
1355 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001356 {
1357 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1358 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1359 !isysroot.empty(), IncludeTimestamps,
1360 ASTHasCompilerErrors};
1361 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1362 getClangFullRepositoryVersion());
1363 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001364 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001365 // For implicit modules we output a signature that we can use to ensure
1366 // duplicate module builds don't collide in the cache as their output order
1367 // is non-deterministic.
1368 // FIXME: Remove this when output is deterministic.
1369 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001370 Signature = getSignature();
1371 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001372 Stream.EmitRecord(SIGNATURE, Record);
1373 }
1374
Richard Smith7ed1bc92014-12-05 22:42:13 +00001375 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001376 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001377 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001379 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001380 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001381 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1382 }
1383
Richard Smith7ed1bc92014-12-05 22:42:13 +00001384 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001385 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001386 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001387
1388 // If the home of the module is the current working directory, then we
1389 // want to pick up the cwd of the build process loading the module, not
1390 // our cwd, when we load this module.
1391 if (!PP.getHeaderSearchInfo()
1392 .getHeaderSearchOpts()
1393 .ModuleMapFileHomeIsCwd ||
1394 WritingModule->Directory->getName() != StringRef(".")) {
1395 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001396 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001397 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001399 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001400
Mehdi Amini57a41912015-09-10 01:46:39 +00001401 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001402 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1403 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001404
1405 // Write out all other paths relative to the base directory if possible.
1406 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1407 } else if (!isysroot.empty()) {
1408 // Write out paths relative to the sysroot if possible.
1409 BaseDirectory = isysroot;
1410 }
1411
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001412 // Module map file
1413 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001414 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001415
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001416 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1417
1418 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001419 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001420
1421 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001422 if (auto *AdditionalModMaps =
1423 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001424 Record.push_back(AdditionalModMaps->size());
1425 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001426 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001427 } else {
1428 Record.push_back(0);
1429 }
1430
1431 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001432 }
1433
Douglas Gregor112b9072012-10-18 05:31:06 +00001434 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001435 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001436 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001437 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001438
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001439 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001440 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001441 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001442 continue;
1443
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001444 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1445 AddSourceLocation(M.ImportLoc, Record);
1446 Record.push_back(M.File->getSize());
1447 Record.push_back(getTimestampForOutput(M.File));
1448 Record.push_back(M.Signature);
1449 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001450 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001451 Stream.EmitRecord(IMPORTS, Record);
1452 }
Mike Stump11289f42009-09-09 15:08:12 +00001453
Richard Smith0516b182015-09-08 19:40:14 +00001454 // Write the options block.
1455 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1456
Douglas Gregor112b9072012-10-18 05:31:06 +00001457 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001458 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001459 const LangOptions &LangOpts = Context.getLangOpts();
1460#define LANGOPT(Name, Bits, Default, Description) \
1461 Record.push_back(LangOpts.Name);
1462#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1463 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001464#include "clang/Basic/LangOptions.def"
1465#define SANITIZER(NAME, ID) \
1466 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001467#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001468
Ben Langmuircd98cb72015-06-23 18:20:18 +00001469 Record.push_back(LangOpts.ModuleFeatures.size());
1470 for (StringRef Feature : LangOpts.ModuleFeatures)
1471 AddString(Feature, Record);
1472
Douglas Gregor112b9072012-10-18 05:31:06 +00001473 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1474 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001475
1476 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001477
1478 // Comment options.
1479 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001480 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1481 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001482 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001483 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001484
Samuel Antaoee8fb302016-01-06 13:42:12 +00001485 // OpenMP offloading options.
1486 Record.push_back(LangOpts.OMPTargetTriples.size());
1487 for (auto &T : LangOpts.OMPTargetTriples)
1488 AddString(T.getTriple(), Record);
1489
1490 AddString(LangOpts.OMPHostIRFile, Record);
1491
Douglas Gregor112b9072012-10-18 05:31:06 +00001492 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1493
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001494 // Target options.
1495 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001496 const TargetInfo &Target = Context.getTargetInfo();
1497 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001498 AddString(TargetOpts.Triple, Record);
1499 AddString(TargetOpts.CPU, Record);
1500 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001501 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1502 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1503 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1504 }
1505 Record.push_back(TargetOpts.Features.size());
1506 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1507 AddString(TargetOpts.Features[I], Record);
1508 }
1509 Stream.EmitRecord(TARGET_OPTIONS, Record);
1510
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001511 // Diagnostic options.
1512 Record.clear();
1513 const DiagnosticOptions &DiagOpts
1514 = Context.getDiagnostics().getDiagnosticOptions();
1515#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1516#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1517 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1518#include "clang/Basic/DiagnosticOptions.def"
1519 Record.push_back(DiagOpts.Warnings.size());
1520 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1521 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001522 Record.push_back(DiagOpts.Remarks.size());
1523 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1524 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001525 // Note: we don't serialize the log or serialization file names, because they
1526 // are generally transient files and will almost always be overridden.
1527 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1528
Douglas Gregorc6317db2012-10-24 15:49:58 +00001529 // File system options.
1530 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001531 const FileSystemOptions &FSOpts =
1532 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001533 AddString(FSOpts.WorkingDir, Record);
1534 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1535
Douglas Gregor2d302362012-10-24 16:50:34 +00001536 // Header search options.
1537 Record.clear();
1538 const HeaderSearchOptions &HSOpts
1539 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1540 AddString(HSOpts.Sysroot, Record);
1541
1542 // Include entries.
1543 Record.push_back(HSOpts.UserEntries.size());
1544 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1545 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1546 AddString(Entry.Path, Record);
1547 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001548 Record.push_back(Entry.IsFramework);
1549 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001550 }
1551
1552 // System header prefixes.
1553 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1554 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1555 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1556 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1557 }
1558
1559 AddString(HSOpts.ResourceDir, Record);
1560 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001561 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001562 Record.push_back(HSOpts.DisableModuleHash);
1563 Record.push_back(HSOpts.UseBuiltinIncludes);
1564 Record.push_back(HSOpts.UseStandardSystemIncludes);
1565 Record.push_back(HSOpts.UseStandardCXXIncludes);
1566 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001567 // Write out the specific module cache path that contains the module files.
1568 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001569 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1570
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001571 // Preprocessor options.
1572 Record.clear();
1573 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1574
1575 // Macro definitions.
1576 Record.push_back(PPOpts.Macros.size());
1577 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1578 AddString(PPOpts.Macros[I].first, Record);
1579 Record.push_back(PPOpts.Macros[I].second);
1580 }
1581
1582 // Includes
1583 Record.push_back(PPOpts.Includes.size());
1584 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1585 AddString(PPOpts.Includes[I], Record);
1586
1587 // Macro includes
1588 Record.push_back(PPOpts.MacroIncludes.size());
1589 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1590 AddString(PPOpts.MacroIncludes[I], Record);
1591
Douglas Gregorb6368752012-10-24 23:41:50 +00001592 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001593 // Detailed record is important since it is used for the module cache hash.
1594 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001595 AddString(PPOpts.ImplicitPCHInclude, Record);
1596 AddString(PPOpts.ImplicitPTHInclude, Record);
1597 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1598 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1599
Richard Smith0516b182015-09-08 19:40:14 +00001600 // Leave the options block.
1601 Stream.ExitBlock();
1602
Douglas Gregora3b20262011-05-06 21:43:30 +00001603 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001604 SourceManager &SM = Context.getSourceManager();
1605 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001606 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001607 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1608 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001609 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001610 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001611
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001612 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001613 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001614 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001615 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001616 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001617
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001618 Record.clear();
1619 Record.push_back(SM.getMainFileID().getOpaqueValue());
1620 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1621
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001622 // Original PCH directory
1623 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001624 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001625 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001627 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001628
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001629 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001630
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001631 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001632 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1633
Mehdi Amini57a41912015-09-10 01:46:39 +00001634 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001635 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1636 }
1637
Douglas Gregor49491f72013-03-15 22:15:07 +00001638 WriteInputFiles(Context.SourceMgr,
1639 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001640 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001641 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001642 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001643}
1644
Douglas Gregor49491f72013-03-15 22:15:07 +00001645namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001646
Douglas Gregor49491f72013-03-15 22:15:07 +00001647 /// \brief An input file.
1648 struct InputFileEntry {
1649 const FileEntry *File;
1650 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001651 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001652 bool BufferOverridden;
1653 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001654
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001655} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001656
1657void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1658 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001659 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001660 using namespace llvm;
1661 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001662
Douglas Gregor72be3902012-10-19 00:38:02 +00001663 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001664 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001665 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001666 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001667 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1668 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001669 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001670 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001671 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001672 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001673
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001674 // Get all ContentCache objects for files, sorted by whether the file is a
1675 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001676 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001677 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1678 // Get this source location entry.
1679 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001680 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001681
1682 // We only care about file entries that were not overridden.
1683 if (!SLoc->isFile())
1684 continue;
1685 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001686 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001687 continue;
1688
Douglas Gregor49491f72013-03-15 22:15:07 +00001689 InputFileEntry Entry;
1690 Entry.File = Cache->OrigEntry;
1691 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001692 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001693 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001694 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001695 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001696 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001697 SortedFiles.push_front(Entry);
1698 }
1699
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001700 unsigned UserFilesNum = 0;
1701 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001702 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001703 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001704 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001705 if (InputFileID != 0)
1706 continue; // already recorded this file.
1707
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001708 // Record this entry's offset.
1709 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001710
1711 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001712
Douglas Gregor49491f72013-03-15 22:15:07 +00001713 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001714 ++UserFilesNum;
1715
Douglas Gregor72be3902012-10-19 00:38:02 +00001716 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001717 // And whether this file was overridden.
1718 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001719 INPUT_FILE,
1720 InputFileOffsets.size(),
1721 (uint64_t)Entry.File->getSize(),
1722 (uint64_t)getTimestampForOutput(Entry.File),
1723 Entry.BufferOverridden,
1724 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001725
Richard Smith7ed1bc92014-12-05 22:42:13 +00001726 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1727 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001728
Douglas Gregor112b9072012-10-18 05:31:06 +00001729 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001730
1731 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001732 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001733 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1734 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001735 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1736 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001737 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001738 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001739
1740 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001741 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1742 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001743 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001744}
1745
Douglas Gregora7f71a92009-04-10 03:52:48 +00001746//===----------------------------------------------------------------------===//
1747// Source Manager Serialization
1748//===----------------------------------------------------------------------===//
1749
1750/// \brief Create an abbreviation for the SLocEntry that refers to a
1751/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001752static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001753 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001754
David Blaikieb44f0bf2017-01-04 22:36:43 +00001755 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001756 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001761 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
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.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001771static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001772 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001773
David Blaikieb44f0bf2017-01-04 22:36:43 +00001774 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001775 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001781 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001782}
1783
1784/// \brief Create an abbreviation for the SLocEntry that refers to a
1785/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001786static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1787 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001788 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001789
David Blaikieb44f0bf2017-01-04 22:36:43 +00001790 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001791 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1792 : SM_SLOC_BUFFER_BLOB));
1793 if (Compressed)
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001796 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001797}
1798
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001799/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1800/// expansion.
1801static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001802 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001803
David Blaikieb44f0bf2017-01-04 22:36:43 +00001804 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001805 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001811 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001812}
1813
Douglas Gregor09b69892011-02-10 17:09:37 +00001814namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001815
Douglas Gregor09b69892011-02-10 17:09:37 +00001816 // Trait used for the on-disk hash table of header search information.
1817 class HeaderFileInfoTrait {
1818 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001819 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001820
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001821 // Keep track of the framework names we've used during serialization.
1822 SmallVector<char, 128> FrameworkStringData;
1823 llvm::StringMap<unsigned> FrameworkNameOffset;
1824
Douglas Gregor09b69892011-02-10 17:09:37 +00001825 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001826 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1827 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001828
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001829 struct key_type {
1830 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001831 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001832 };
1833 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001834
1835 typedef HeaderFileInfo data_type;
1836 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001837 typedef unsigned hash_value_type;
1838 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001839
Richard Smithe75ee0f2015-08-17 07:13:32 +00001840 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001841 // The hash is based only on size/time of the file, so that the reader can
1842 // match even when symlinking or excess path elements ("foo/../", "../")
1843 // change the form of the name. However, complete path is still the key.
1844 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001845 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001846 }
1847
1848 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001849 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001850 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001851 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001852 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001853 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001854 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001855 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1856 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1857 DataLen += 4;
1858 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001859 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001860 }
1861
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001862 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001863 using namespace llvm::support;
1864 endian::Writer<little> LE(Out);
1865 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001866 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001867 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001868 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001869 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001870 }
1871
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001872 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001873 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001874 using namespace llvm::support;
1875 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001876 uint64_t Start = Out.tell(); (void)Start;
1877
Richard Smith386bb072015-08-18 23:42:23 +00001878 unsigned char Flags = (Data.isImport << 4)
1879 | (Data.isPragmaOnce << 3)
1880 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001881 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001882 LE.write<uint8_t>(Flags);
1883 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001884
1885 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001886 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001887 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001888 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001889
1890 unsigned Offset = 0;
1891 if (!Data.Framework.empty()) {
1892 // If this header refers into a framework, save the framework name.
1893 llvm::StringMap<unsigned>::iterator Pos
1894 = FrameworkNameOffset.find(Data.Framework);
1895 if (Pos == FrameworkNameOffset.end()) {
1896 Offset = FrameworkStringData.size() + 1;
1897 FrameworkStringData.append(Data.Framework.begin(),
1898 Data.Framework.end());
1899 FrameworkStringData.push_back(0);
1900
1901 FrameworkNameOffset[Data.Framework] = Offset;
1902 } else
1903 Offset = Pos->second;
1904 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001905 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001906
Richard Smith386bb072015-08-18 23:42:23 +00001907 // FIXME: If the header is excluded, we should write out some
1908 // record of that fact.
1909 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1910 if (uint32_t ModID =
1911 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1912 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1913 assert((Value >> 2) == ModID && "overflow in header module info");
1914 LE.write<uint32_t>(Value);
1915 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001916 }
1917
Douglas Gregor09b69892011-02-10 17:09:37 +00001918 assert(Out.tell() - Start == DataLen && "Wrong data length");
1919 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001920
1921 const char *strings_begin() const { return FrameworkStringData.begin(); }
1922 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001923 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001924
Douglas Gregor09b69892011-02-10 17:09:37 +00001925} // end anonymous namespace
1926
1927/// \brief Write the header search block for the list of files that
1928///
1929/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001930void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001931 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001932 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1933
1934 if (FilesByUID.size() > HS.header_file_size())
1935 FilesByUID.resize(HS.header_file_size());
1936
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001937 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001938 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001939 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001940 unsigned NumHeaderSearchEntries = 0;
1941 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1942 const FileEntry *File = FilesByUID[UID];
1943 if (!File)
1944 continue;
1945
Richard Smith386bb072015-08-18 23:42:23 +00001946 // Get the file info. This will load info from the external source if
1947 // necessary. Skip emitting this file if we have no information on it
1948 // as a header file (in which case HFI will be null) or if it hasn't
1949 // changed since it was loaded. Also skip it if it's for a modular header
1950 // from a different module; in that case, we rely on the module(s)
1951 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001952 const HeaderFileInfo *HFI =
1953 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1954 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001955 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001956
Richard Smith7ed1bc92014-12-05 22:42:13 +00001957 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001958 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001959 SmallString<128> FilenameTmp(Filename);
1960 if (PreparePathForOutput(FilenameTmp)) {
1961 // If we performed any translation on the file name at all, we need to
1962 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001963 Filename = StringRef(strdup(FilenameTmp.c_str()));
1964 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001965 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001966
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001967 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001968 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001969 ++NumHeaderSearchEntries;
1970 }
1971
1972 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001973 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001974 uint32_t BucketOffset;
1975 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001976 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001977 llvm::raw_svector_ostream Out(TableData);
1978 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001979 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001980 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1981 }
1982
1983 // Create a blob abbreviation
1984 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001985
David Blaikieb44f0bf2017-01-04 22:36:43 +00001986 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00001987 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00001992 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00001993
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001994 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001995 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1996 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001997 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001998 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001999
2000 // Free all of the strings we had to duplicate.
2001 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002002 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002003}
2004
George Rimarc39f5492017-01-17 15:45:31 +00002005static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2006 unsigned SLocBufferBlobCompressedAbbrv,
2007 unsigned SLocBufferBlobAbbrv) {
2008 typedef ASTWriter::RecordData::value_type RecordDataType;
2009
2010 // Compress the buffer if possible. We expect that almost all PCM
2011 // consumers will not want its contents.
2012 SmallString<0> CompressedBuffer;
2013 if (llvm::zlib::isAvailable()) {
2014 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2015 if (!E) {
2016 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2017 Blob.size() - 1};
2018 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2019 CompressedBuffer);
2020 return;
2021 }
2022 llvm::consumeError(std::move(E));
2023 }
2024
2025 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2026 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2027}
2028
Douglas Gregora7f71a92009-04-10 03:52:48 +00002029/// \brief Writes the block containing the serialized form of the
2030/// source manager.
2031///
2032/// TODO: We should probably use an on-disk hash table (stored in a
2033/// blob), indexed based on the file name, so that we only create
2034/// entries for files that we actually need. In the common case (no
2035/// errors), we probably won't have to create file entries for any of
2036/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002037void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002038 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002039 RecordData Record;
2040
Chris Lattner0910e3b2009-04-10 17:16:57 +00002041 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002042 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002043
2044 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002045 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2046 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002047 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2048 unsigned SLocBufferBlobCompressedAbbrv =
2049 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002050 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002051
Douglas Gregor258ae542009-04-27 06:38:32 +00002052 // Write out the source location entry table. We skip the first
2053 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002054 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002055 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002056 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2057 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002058 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002059 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002060 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002061 FileID FID = FileID::get(I);
2062 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002063
Douglas Gregor258ae542009-04-27 06:38:32 +00002064 // Record the offset of this source-location entry.
2065 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2066
2067 // Figure out which record code to use.
2068 unsigned Code;
2069 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002070 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2071 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002072 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002073 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002074 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002075 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002076 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002077 Record.clear();
2078 Record.push_back(Code);
2079
Douglas Gregor925296b2011-07-19 16:10:42 +00002080 // Starting offset of this entry within this module, so skip the dummy.
2081 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002082 if (SLoc->isFile()) {
2083 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002084 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002085 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2086 Record.push_back(File.hasLineDirectives());
2087
2088 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002089 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002090 if (Content->OrigEntry) {
2091 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002092 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002093
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002094 // The source location entry is a file. Emit input file ID.
2095 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2096 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002097
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002098 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002099
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002100 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002101 if (FDI != FileDeclIDs.end()) {
2102 Record.push_back(FDI->second->FirstDeclIndex);
2103 Record.push_back(FDI->second->DeclIDs.size());
2104 } else {
2105 Record.push_back(0);
2106 Record.push_back(0);
2107 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002108
2109 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2110
Richard Smithaada85c2016-02-06 02:06:43 +00002111 if (Content->BufferOverridden || Content->IsTransient)
2112 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002113 } else {
2114 // The source location entry is a buffer. The blob associated
2115 // with this entry contains the contents of the buffer.
2116
2117 // We add one to the size so that we capture the trailing NULL
2118 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2119 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002120 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002121 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002122 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002123 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002124 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002125 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002126
Mehdi Amini99d1b292016-10-01 16:38:28 +00002127 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002128 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002129 }
Richard Smithaada85c2016-02-06 02:06:43 +00002130
2131 if (EmitBlob) {
2132 // Include the implicit terminating null character in the on-disk buffer
2133 // if we're writing it uncompressed.
2134 const llvm::MemoryBuffer *Buffer =
2135 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2136 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002137 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2138 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002139 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002140 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002141 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002142 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002143 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2144 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2145 AddSourceLocation(Expansion.isMacroArgExpansion()
2146 ? SourceLocation()
2147 : Expansion.getExpansionLocEnd(),
2148 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002149
2150 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002151 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002152 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002153 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002154 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002155 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002156 }
2157 }
2158
Douglas Gregor8f45df52009-04-16 22:23:12 +00002159 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002160
2161 if (SLocEntryOffsets.empty())
2162 return;
2163
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002164 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002165 // table is used for lazily loading source-location information.
2166 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002167
David Blaikieb44f0bf2017-01-04 22:36:43 +00002168 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002169 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002173 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002174 {
2175 RecordData::value_type Record[] = {
2176 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2177 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2178 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2179 bytes(SLocEntryOffsets));
2180 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002181 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002182 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002183 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002184
2185 // Write the line table. It depends on remapping working, so it must come
2186 // after the source location offsets.
2187 if (SourceMgr.hasLineTable()) {
2188 LineTableInfo &LineTable = SourceMgr.getLineTable();
2189
2190 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002191
2192 // Emit the needed file names.
2193 llvm::DenseMap<int, int> FilenameMap;
2194 for (const auto &L : LineTable) {
2195 if (L.first.ID < 0)
2196 continue;
2197 for (auto &LE : L.second) {
2198 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2199 FilenameMap.size())).second)
2200 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2201 }
2202 }
2203 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002204
2205 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002206 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002207 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002208 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002209 continue;
2210
2211 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002212 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002213
2214 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002215 Record.push_back(L.second.size());
2216 for (const auto &LE : L.second) {
2217 Record.push_back(LE.FileOffset);
2218 Record.push_back(LE.LineNo);
2219 Record.push_back(FilenameMap[LE.FilenameID]);
2220 Record.push_back((unsigned)LE.FileKind);
2221 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002222 }
2223 }
Richard Smith63078492015-09-01 07:41:55 +00002224
Douglas Gregor925296b2011-07-19 16:10:42 +00002225 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2226 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002227}
2228
Douglas Gregorc5046832009-04-27 18:38:38 +00002229//===----------------------------------------------------------------------===//
2230// Preprocessor Serialization
2231//===----------------------------------------------------------------------===//
2232
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002233static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2234 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002235 if (MacroInfo *MI = MD->getMacroInfo())
2236 if (MI->isBuiltinMacro())
2237 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002238
2239 if (IsModule) {
2240 SourceLocation Loc = MD->getLocation();
2241 if (Loc.isInvalid())
2242 return true;
2243 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2244 return true;
2245 }
2246
2247 return false;
2248}
2249
Chris Lattnereeffaef2009-04-10 17:15:23 +00002250/// \brief Writes the block containing the serialized form of the
2251/// preprocessor.
2252///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002253void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002254 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2255 if (PPRec)
2256 WritePreprocessorDetail(*PPRec);
2257
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002258 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002259 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002260
Chris Lattner0af3ba12009-04-13 01:29:17 +00002261 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2262 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002263 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002264 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002265 }
2266
2267 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002268 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002269
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002270 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002271 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002272 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002273 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002274
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002275 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002276 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002277
Richard Smithee977932015-05-01 21:22:17 +00002278 // Construct the list of identifiers with macro directives that need to be
2279 // serialized.
2280 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2281 for (auto &Id : PP.getIdentifierTable())
2282 if (Id.second->hadMacroDefinition() &&
2283 (!Id.second->isFromAST() ||
2284 Id.second->hasChangedSinceDeserialization()))
2285 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002286 // Sort the set of macro definitions that need to be serialized by the
2287 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002288 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2289 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002290
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002291 // Emit the macro directives as a list and associate the offset with the
2292 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002293 for (const IdentifierInfo *Name : MacroIdentifiers) {
2294 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002295 auto StartOffset = Stream.GetCurrentBitNo();
2296
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002297 // Emit the macro directives in reverse source order.
2298 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002299 // Once we hit an ignored macro, we're done: the rest of the chain
2300 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002301 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002302 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002303
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002304 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002305 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002306 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002307 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002308 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002309 Record.push_back(VisMD->isPublic());
2310 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002311 }
Richard Smithd7329392015-04-21 21:46:32 +00002312
Richard Smithb8b2ed62015-04-23 18:18:26 +00002313 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002314 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002315 // We write out exported module macros for PCH as well.
2316 auto Leafs = PP.getLeafModuleMacros(Name);
2317 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2318 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2319 while (!Worklist.empty()) {
2320 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002321
Manman Ren33d80292016-05-31 18:19:32 +00002322 // Emit a record indicating this submodule exports this macro.
2323 ModuleMacroRecord.push_back(
2324 getSubmoduleID(Macro->getOwningModule()));
2325 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2326 for (auto *M : Macro->overrides())
2327 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002328
Manman Ren33d80292016-05-31 18:19:32 +00002329 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2330 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002331
Manman Ren33d80292016-05-31 18:19:32 +00002332 // Enqueue overridden macros once we've visited all their ancestors.
2333 for (auto *M : Macro->overrides())
2334 if (++Visits[M] == M->getNumOverridingMacros())
2335 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002336
Manman Ren33d80292016-05-31 18:19:32 +00002337 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002338 }
Richard Smithd7329392015-04-21 21:46:32 +00002339
Richard Smithee977932015-05-01 21:22:17 +00002340 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002341 continue;
2342
Richard Smithd7329392015-04-21 21:46:32 +00002343 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002344 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2345 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002346 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002347
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002348 /// \brief Offsets of each of the macros into the bitstream, indexed by
2349 /// the local macro ID
2350 ///
2351 /// For each identifier that is associated with a macro, this map
2352 /// provides the offset into the bitstream where that macro is
2353 /// defined.
2354 std::vector<uint32_t> MacroOffsets;
2355
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002356 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2357 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2358 MacroInfo *MI = MacroInfosToEmit[I].MI;
2359 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002360
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002361 if (ID < FirstMacroID) {
2362 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2363 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002364 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002365
2366 // Record the local offset of this macro.
2367 unsigned Index = ID - FirstMacroID;
2368 if (Index == MacroOffsets.size())
2369 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2370 else {
2371 if (Index > MacroOffsets.size())
2372 MacroOffsets.resize(Index + 1);
2373
2374 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2375 }
2376
2377 AddIdentifierRef(Name, Record);
2378 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2379 AddSourceLocation(MI->getDefinitionLoc(), Record);
2380 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2381 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002382 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002383 unsigned Code;
2384 if (MI->isObjectLike()) {
2385 Code = PP_MACRO_OBJECT_LIKE;
2386 } else {
2387 Code = PP_MACRO_FUNCTION_LIKE;
2388
2389 Record.push_back(MI->isC99Varargs());
2390 Record.push_back(MI->isGNUVarargs());
2391 Record.push_back(MI->hasCommaPasting());
2392 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002393 for (const IdentifierInfo *Arg : MI->args())
2394 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002395 }
2396
2397 // If we have a detailed preprocessing record, record the macro definition
2398 // ID that corresponds to this macro.
2399 if (PPRec)
2400 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2401
2402 Stream.EmitRecord(Code, Record);
2403 Record.clear();
2404
2405 // Emit the tokens array.
2406 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2407 // Note that we know that the preprocessor does not have any annotation
2408 // tokens in it because they are created by the parser, and thus can't
2409 // be in a macro definition.
2410 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002411 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002412 Stream.EmitRecord(PP_TOKEN, Record);
2413 Record.clear();
2414 }
2415 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002416 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002417
Douglas Gregor92a96f52011-02-08 21:58:10 +00002418 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002419
2420 // Write the offsets table for macro IDs.
2421 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002422
David Blaikieb44f0bf2017-01-04 22:36:43 +00002423 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002424 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2428
David Blaikieb44f0bf2017-01-04 22:36:43 +00002429 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002430 {
2431 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2432 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2433 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2434 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002435}
2436
2437void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002438 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002439 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002440
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002441 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002442
Douglas Gregor92a96f52011-02-08 21:58:10 +00002443 // Enter the preprocessor block.
2444 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002445
Douglas Gregoraae92242010-03-19 21:51:54 +00002446 // If the preprocessor has a preprocessing record, emit it.
2447 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002448 using namespace llvm;
2449
2450 // Set up the abbreviation for
2451 unsigned InclusionAbbrev = 0;
2452 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002453 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002454 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002460 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002461 }
2462
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002463 unsigned FirstPreprocessorEntityID
2464 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2465 + NUM_PREDEF_PP_ENTITY_IDS;
2466 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002467 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002468 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2469 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002470 E != EEnd;
2471 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002472 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002473
Richard Smith66a81862015-05-04 02:25:31 +00002474 PreprocessedEntityOffsets.push_back(
2475 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002476
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002477 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002478 // Record this macro definition's ID.
2479 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002480
Douglas Gregor92a96f52011-02-08 21:58:10 +00002481 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002482 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2483 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002484 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002485
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002486 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002487 Record.push_back(ME->isBuiltinMacro());
2488 if (ME->isBuiltinMacro())
2489 AddIdentifierRef(ME->getName(), Record);
2490 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002491 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002492 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002493 continue;
2494 }
2495
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002496 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002497 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002498 Record.push_back(ID->getFileName().size());
2499 Record.push_back(ID->wasInQuotes());
2500 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002501 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002502 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002503 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002504 // Check that the FileEntry is not null because it was not resolved and
2505 // we create a PCH even with compiler errors.
2506 if (ID->getFile())
2507 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002508 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002509 continue;
2510 }
2511
2512 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2513 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002514 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002515
Douglas Gregoraae92242010-03-19 21:51:54 +00002516 // Write the offsets table for the preprocessing record.
2517 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002518 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2519
Douglas Gregoraae92242010-03-19 21:51:54 +00002520 // Write the offsets table for identifier IDs.
2521 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002522
David Blaikieb44f0bf2017-01-04 22:36:43 +00002523 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002524 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002527 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002528
Mehdi Amini57a41912015-09-10 01:46:39 +00002529 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2530 FirstPreprocessorEntityID -
2531 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002532 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002533 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002534 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002535}
2536
Richard Smith386bb072015-08-18 23:42:23 +00002537unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002538 if (!Mod)
2539 return 0;
2540
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002541 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2542 if (Known != SubmoduleIDs.end())
2543 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002544
Richard Smithdc1f0422016-07-20 19:10:16 +00002545 auto *Top = Mod->getTopLevelModule();
2546 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002547 (getLangOpts().CompilingPCH ||
2548 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002549 return 0;
2550
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002551 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2552}
2553
Richard Smith386bb072015-08-18 23:42:23 +00002554unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2555 // FIXME: This can easily happen, if we have a reference to a submodule that
2556 // did not result in us loading a module file for that submodule. For
2557 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2558 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2559 assert((ID || !Mod) &&
2560 "asked for module ID for non-local, non-imported module");
2561 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002562}
2563
Douglas Gregor253eefe2011-12-01 00:59:36 +00002564/// \brief Compute the number of modules within the given tree (including the
2565/// given module).
2566static unsigned getNumberOfModules(Module *Mod) {
2567 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002568 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002569 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002570 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002571
2572 return ChildModules + 1;
2573}
2574
Douglas Gregorde3ef502011-11-30 23:21:26 +00002575void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002576 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002577 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002578
2579 // Write the abbreviations needed for the submodules block.
2580 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002581
David Blaikieb44f0bf2017-01-04 22:36:43 +00002582 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002583 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
David Blaikie4d923012017-01-31 21:28:19 +00002594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // WithCodegen
Douglas Gregor69021972011-11-30 17:33:56 +00002595 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002596 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002597
David Blaikieb44f0bf2017-01-04 22:36:43 +00002598 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002599 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002600 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002601 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002602
David Blaikieb44f0bf2017-01-04 22:36:43 +00002603 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002604 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002606 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002607
David Blaikieb44f0bf2017-01-04 22:36:43 +00002608 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002609 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002611 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002612
David Blaikieb44f0bf2017-01-04 22:36:43 +00002613 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002614 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002616 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002617
David Blaikieb44f0bf2017-01-04 22:36:43 +00002618 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002619 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002622 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002623
David Blaikieb44f0bf2017-01-04 22:36:43 +00002624 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002625 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002627 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002628
David Blaikieb44f0bf2017-01-04 22:36:43 +00002629 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002630 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002632 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002633
David Blaikieb44f0bf2017-01-04 22:36:43 +00002634 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002635 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002637 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002638
David Blaikieb44f0bf2017-01-04 22:36:43 +00002639 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002640 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002642 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002643
David Blaikieb44f0bf2017-01-04 22:36:43 +00002644 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002645 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2647 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002648 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002649
David Blaikieb44f0bf2017-01-04 22:36:43 +00002650 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002651 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002653 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002654
David Blaikieb44f0bf2017-01-04 22:36:43 +00002655 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002656 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2657 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2658 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002659 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002660
Douglas Gregor253eefe2011-12-01 00:59:36 +00002661 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002662 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2663 FirstSubmoduleID -
2664 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002665 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2666
Douglas Gregor69021972011-11-30 17:33:56 +00002667 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002668 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002669 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002670 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002671 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002672 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002673 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002674
2675 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002676 if (Mod->Parent) {
2677 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002678 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002679 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002680
2681 // Emit the definition of the block.
2682 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002683 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2684 ID,
2685 ParentID,
2686 Mod->IsFramework,
2687 Mod->IsExplicit,
2688 Mod->IsSystem,
2689 Mod->IsExternC,
2690 Mod->InferSubmodules,
2691 Mod->InferExplicitSubmodules,
2692 Mod->InferExportWildcard,
2693 Mod->ConfigMacrosExhaustive,
David Blaikie4d923012017-01-31 21:28:19 +00002694 Context->getLangOpts().ModularCodegen && WritingModule};
Mehdi Amini57a41912015-09-10 01:46:39 +00002695 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2696 }
2697
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002698 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002699 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002700 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002701 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002702 }
2703
Douglas Gregor69021972011-11-30 17:33:56 +00002704 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002705 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002706 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002707 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2708 UmbrellaHeader.NameAsWritten);
2709 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002710 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2711 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002712 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002713 }
Richard Smith306d8922014-10-22 23:50:56 +00002714
Douglas Gregor69021972011-11-30 17:33:56 +00002715 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002716 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002717 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002718 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002719 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002720 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002721 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2722 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2723 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002724 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002725 Module::HK_PrivateTextual},
2726 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002727 };
2728 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002729 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002730 for (auto &H : Mod->Headers[HL.HeaderKind])
2731 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2732 }
2733
2734 // Emit the top headers.
2735 {
2736 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002737 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002738 for (auto *H : TopHeaders)
2739 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002740 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002741
2742 // Emit the imports.
2743 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002744 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002745 for (auto *I : Mod->Imports)
2746 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002747 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2748 }
2749
Douglas Gregor24bb9232011-12-02 18:58:38 +00002750 // Emit the exports.
2751 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002752 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002753 for (const auto &E : Mod->Exports) {
2754 // FIXME: This may fail; we don't require that all exported modules
2755 // are local or imported.
2756 Record.push_back(getSubmoduleID(E.getPointer()));
2757 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002758 }
2759 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2760 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002761
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002762 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2763 // Might be unnecessary as use declarations are only used to build the
2764 // module itself.
2765
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002766 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002767 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002768 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2769 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002770 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002771 }
2772
Douglas Gregorfb912652013-03-20 21:10:35 +00002773 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002774 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002775 // FIXME: This may fail; we don't require that all conflicting modules
2776 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002777 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2778 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002779 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002780 }
2781
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002782 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002783 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002784 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002785 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002786 }
2787
Richard Smithdc1f0422016-07-20 19:10:16 +00002788 // Emit the initializers, if any.
2789 RecordData Inits;
2790 for (Decl *D : Context->getModuleInitializers(Mod))
2791 Inits.push_back(GetDeclRef(D));
2792 if (!Inits.empty())
2793 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2794
Douglas Gregor69021972011-11-30 17:33:56 +00002795 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002796 for (auto *M : Mod->submodules())
2797 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002798 }
2799
2800 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002801
Richard Smith23d8d032015-05-14 00:45:20 +00002802 assert((NextSubmoduleID - FirstSubmoduleID ==
2803 getNumberOfModules(WritingModule)) &&
2804 "Wrong # of submodules; found a reference to a non-local, "
2805 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002806}
2807
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002808serialization::SubmoduleID
2809ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002810 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002811 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002812
2813 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002814 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002815 Module *OwningMod
2816 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002817 if (!OwningMod)
2818 return 0;
2819
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002820 // Check whether this submodule is part of our own module.
2821 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002822 return 0;
2823
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002824 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002825}
2826
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002827void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2828 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002829 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2830 DiagStateIDMap;
2831 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002832 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002833
Richard Smithd230de22017-01-26 01:01:01 +00002834 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2835 bool IncludeNonPragmaStates) {
2836 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002837 Record.push_back(DiagStateID);
Richard Smithd230de22017-01-26 01:01:01 +00002838
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002839 if (DiagStateID == 0) {
2840 DiagStateID = ++CurrID;
Richard Smithd230de22017-01-26 01:01:01 +00002841 for (const auto &I : *State) {
2842 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002843 Record.push_back(I.first);
2844 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002845 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002846 }
Richard Smithd230de22017-01-26 01:01:01 +00002847 // Add a sentinel to mark the end of the diag IDs.
2848 Record.push_back(unsigned(-1));
2849 }
2850 };
2851
2852 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2853 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2854 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2855
2856 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2857 if (!FileIDAndFile.first.isValid() ||
2858 !FileIDAndFile.second.HasLocalTransitions)
2859 continue;
2860 AddSourceLocation(Diag.SourceMgr->getLocForStartOfFile(FileIDAndFile.first),
2861 Record);
2862 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2863 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2864 Record.push_back(StatePoint.Offset);
2865 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002866 }
2867 }
2868
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002869 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002870 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002871}
2872
Douglas Gregorc5046832009-04-27 18:38:38 +00002873//===----------------------------------------------------------------------===//
2874// Type Serialization
2875//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002876
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002877/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002878void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002879 TypeIdx &IdxRef = TypeIdxs[T];
2880 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2881 IdxRef = TypeIdx(NextTypeID++);
2882 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002883
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002884 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002885
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002886 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002887
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002888 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002889 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002890 W.Visit(T);
2891 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002892
Richard Smith290d8012016-04-06 17:06:00 +00002893 // Record the offset for this type.
2894 unsigned Index = Idx.getIndex() - FirstTypeID;
2895 if (TypeOffsets.size() == Index)
2896 TypeOffsets.push_back(Offset);
2897 else if (TypeOffsets.size() < Index) {
2898 TypeOffsets.resize(Index + 1);
2899 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002900 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002901 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002902 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002903}
2904
Douglas Gregorc5046832009-04-27 18:38:38 +00002905//===----------------------------------------------------------------------===//
2906// Declaration Serialization
2907//===----------------------------------------------------------------------===//
2908
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002909/// \brief Write the block containing all of the declaration IDs
2910/// lexically declared within the given DeclContext.
2911///
2912/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2913/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002914uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002915 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002916 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002917 return 0;
2918
Douglas Gregor8f45df52009-04-16 22:23:12 +00002919 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002920 SmallVector<uint32_t, 128> KindDeclPairs;
2921 for (const auto *D : DC->decls()) {
2922 KindDeclPairs.push_back(D->getKind());
2923 KindDeclPairs.push_back(GetDeclRef(D));
2924 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002925
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002926 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002927 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002928 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2929 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002930 return Offset;
2931}
2932
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002933void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002934 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002935
2936 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002937 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002938 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002942 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002943 {
2944 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2945 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2946 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2947 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002948
2949 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002950 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002951 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002955 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002956 {
2957 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2958 FirstDeclID - NUM_PREDEF_DECL_IDS};
2959 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2960 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002961}
2962
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002963void ASTWriter::WriteFileDeclIDsMap() {
2964 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002965
Chandler Carruthb306d732015-03-27 00:31:20 +00002966 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2967 FileDeclIDs.begin(), FileDeclIDs.end());
2968 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2969 llvm::less_first());
2970
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002971 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002972 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2973 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2974 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2975 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2976 for (auto &LocDeclEntry : Info.DeclIDs)
2977 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002978 }
2979
David Blaikieb44f0bf2017-01-04 22:36:43 +00002980 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002981 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002984 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002985 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2986 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002987 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002988}
2989
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002990void ASTWriter::WriteComments() {
2991 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002992 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002993 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002994 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002995 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002996 AddSourceRange(I->getSourceRange(), Record);
2997 Record.push_back(I->getKind());
2998 Record.push_back(I->isTrailingComment());
2999 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003000 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3001 }
3002 Stream.ExitBlock();
3003}
3004
Douglas Gregorc5046832009-04-27 18:38:38 +00003005//===----------------------------------------------------------------------===//
3006// Global Method Pool and Selector Serialization
3007//===----------------------------------------------------------------------===//
3008
Douglas Gregore84a9da2009-04-20 20:36:09 +00003009namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003010
Douglas Gregorc78d3462009-04-24 21:10:55 +00003011// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003012class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003013 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003014
3015public:
3016 typedef Selector key_type;
3017 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003018
Sebastian Redl834bb972010-08-04 17:20:04 +00003019 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003020 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003021 ObjCMethodList Instance, Factory;
3022 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003023 typedef const data_type& data_type_ref;
3024
Justin Bogner25463f12014-04-18 20:27:24 +00003025 typedef unsigned hash_value_type;
3026 typedef unsigned offset_type;
3027
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003028 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003029
Justin Bogner25463f12014-04-18 20:27:24 +00003030 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003031 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003032 }
Mike Stump11289f42009-09-09 15:08:12 +00003033
3034 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003035 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003036 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003037 using namespace llvm::support;
3038 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003039 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003040 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003041 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3042 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003043 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003044 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003045 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003046 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003047 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003048 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003049 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003050 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003051 return std::make_pair(KeyLen, DataLen);
3052 }
Mike Stump11289f42009-09-09 15:08:12 +00003053
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003054 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003055 using namespace llvm::support;
3056 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003057 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003058 assert((Start >> 32) == 0 && "Selector key offset too large");
3059 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003060 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003061 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003062 if (N == 0)
3063 N = 1;
3064 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003065 LE.write<uint32_t>(
3066 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003067 }
Mike Stump11289f42009-09-09 15:08:12 +00003068
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003069 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003070 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003071 using namespace llvm::support;
3072 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003073 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003074 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003075 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003076 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003077 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003078 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003079 ++NumInstanceMethods;
3080
3081 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003082 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003083 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003084 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003085 ++NumFactoryMethods;
3086
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003087 unsigned InstanceBits = Methods.Instance.getBits();
3088 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003089 unsigned InstanceHasMoreThanOneDeclBit =
3090 Methods.Instance.hasMoreThanOneDecl();
3091 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3092 (InstanceHasMoreThanOneDeclBit << 2) |
3093 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003094 unsigned FactoryBits = Methods.Factory.getBits();
3095 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003096 unsigned FactoryHasMoreThanOneDeclBit =
3097 Methods.Factory.hasMoreThanOneDecl();
3098 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3099 (FactoryHasMoreThanOneDeclBit << 2) |
3100 FactoryBits;
3101 LE.write<uint16_t>(FullInstanceBits);
3102 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003103 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003104 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003105 if (Method->getMethod())
3106 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003107 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003108 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003109 if (Method->getMethod())
3110 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003111
3112 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003113 }
3114};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003115
Douglas Gregorc78d3462009-04-24 21:10:55 +00003116} // end anonymous namespace
3117
Sebastian Redla19a67f2010-08-03 21:58:15 +00003118/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003119///
3120/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003121/// in an on-disk hash table indexed by the selector. The hash table also
3122/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003123void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003124 using namespace llvm;
3125
Sebastian Redla19a67f2010-08-03 21:58:15 +00003126 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003127 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003128 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003129 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003130 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003131 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003132 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003133 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003134
Sebastian Redla19a67f2010-08-03 21:58:15 +00003135 // Create the on-disk hash table representation. We walk through every
3136 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003137 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003138 for (auto &SelectorAndID : SelectorIDs) {
3139 Selector S = SelectorAndID.first;
3140 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003141 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003142 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003143 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003144 ObjCMethodList(),
3145 ObjCMethodList()
3146 };
3147 if (F != SemaRef.MethodPool.end()) {
3148 Data.Instance = F->second.first;
3149 Data.Factory = F->second.second;
3150 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003151 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003152 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003153 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003154 // Selector already exists. Did it change?
3155 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003156 for (ObjCMethodList *M = &Data.Instance;
3157 !changed && M && M->getMethod(); M = M->getNext()) {
3158 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003159 changed = true;
3160 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003161 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003162 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003163 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003164 changed = true;
3165 }
3166 if (!changed)
3167 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003168 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003169 // A new method pool entry.
3170 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003171 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003172 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003173 }
3174
Douglas Gregorc78d3462009-04-24 21:10:55 +00003175 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003176 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003177 uint32_t BucketOffset;
3178 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003179 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003180 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003181 llvm::raw_svector_ostream Out(MethodPool);
3182 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003183 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003184 BucketOffset = Generator.Emit(Out, Trait);
3185 }
3186
3187 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003188 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003189 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003193 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003194
Douglas Gregor95c13f52009-04-25 17:48:32 +00003195 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003196 {
3197 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3198 NumTableEntries};
3199 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3200 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003201
3202 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003203 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003204 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003208 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003209
3210 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003211 {
3212 RecordData::value_type Record[] = {
3213 SELECTOR_OFFSETS, SelectorOffsets.size(),
3214 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3215 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3216 bytes(SelectorOffsets));
3217 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003218 }
3219}
3220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003221/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003222void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003223 using namespace llvm;
3224 if (SemaRef.ReferencedSelectors.empty())
3225 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003226
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003227 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003228 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003229
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003230 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003231 // very tricky to fix, and given that @selector shouldn't really appear in
3232 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003233 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3234 Selector Sel = SelectorAndLocation.first;
3235 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003236 Writer.AddSelectorRef(Sel);
3237 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003238 }
Richard Smithe64e7452016-04-18 21:54:58 +00003239 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003240}
3241
Douglas Gregorc5046832009-04-27 18:38:38 +00003242//===----------------------------------------------------------------------===//
3243// Identifier Table Serialization
3244//===----------------------------------------------------------------------===//
3245
Richard Smithb3761912015-02-05 23:08:52 +00003246/// Determine the declaration that should be put into the name lookup table to
3247/// represent the given declaration in this module. This is usually D itself,
3248/// but if D was imported and merged into a local declaration, we want the most
3249/// recent local declaration instead. The chosen declaration will be the most
3250/// recent declaration in any module that imports this one.
3251static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3252 NamedDecl *D) {
3253 if (!LangOpts.Modules || !D->isFromASTFile())
3254 return D;
3255
3256 if (Decl *Redecl = D->getPreviousDecl()) {
3257 // For Redeclarable decls, a prior declaration might be local.
3258 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003259 // If we find a local decl, we're done.
3260 if (!Redecl->isFromASTFile()) {
3261 // Exception: in very rare cases (for injected-class-names), not all
3262 // redeclarations are in the same semantic context. Skip ones in a
3263 // different context. They don't go in this lookup table at all.
3264 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3265 D->getDeclContext()->getRedeclContext()))
3266 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003267 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003268 }
3269
Richard Smithfe620d22015-03-05 23:24:12 +00003270 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003271 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003272 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003273 break;
3274 }
3275 } else if (Decl *First = D->getCanonicalDecl()) {
3276 // For Mergeable decls, the first decl might be local.
3277 if (!First->isFromASTFile())
3278 return cast<NamedDecl>(First);
3279 }
3280
3281 // All declarations are imported. Our most recent declaration will also be
3282 // the most recent one in anyone who imports us.
3283 return D;
3284}
3285
Douglas Gregorc78d3462009-04-24 21:10:55 +00003286namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003287
Richard Smithcf97cf62015-04-19 01:34:23 +00003288class ASTIdentifierTableTrait {
3289 ASTWriter &Writer;
3290 Preprocessor &PP;
3291 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003292 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003293 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003294 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003295
3296 /// \brief Determines whether this is an "interesting" identifier that needs a
3297 /// full IdentifierInfo structure written into the hash table. Notably, this
3298 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3299 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003300 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003301 if (MacroOffset ||
3302 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003303 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003304 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003305 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003306 return true;
3307
3308 return false;
3309 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003310
Douglas Gregore84a9da2009-04-20 20:36:09 +00003311public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003312 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003313 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003314
Sebastian Redl539c5062010-08-18 23:57:32 +00003315 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003316 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003317
Justin Bogner25463f12014-04-18 20:27:24 +00003318 typedef unsigned hash_value_type;
3319 typedef unsigned offset_type;
3320
Richard Smithd7329392015-04-21 21:46:32 +00003321 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003322 IdentifierResolver &IdResolver, bool IsModule,
3323 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003324 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003325 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3326 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003327
Richard Smithd79514e2016-02-05 19:03:40 +00003328 bool needDecls() const { return NeedDecls; }
3329
Justin Bogner25463f12014-04-18 20:27:24 +00003330 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003331 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003332 }
Mike Stump11289f42009-09-09 15:08:12 +00003333
Richard Smith33e0f7e2015-07-22 02:08:40 +00003334 bool isInterestingIdentifier(const IdentifierInfo *II) {
3335 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3336 return isInterestingIdentifier(II, MacroOffset);
3337 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003338
Richard Smith9c254182015-07-19 21:41:12 +00003339 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3340 return isInterestingIdentifier(II, 0);
3341 }
3342
Mike Stump11289f42009-09-09 15:08:12 +00003343 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003344 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003345 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003346 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003347 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3348 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003349 DataLen += 2; // 2 bytes for builtin ID
3350 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003351 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003352 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003353
Richard Smitha534a312015-07-21 23:54:07 +00003354 if (NeedDecls) {
3355 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3356 DEnd = IdResolver.end();
3357 D != DEnd; ++D)
3358 DataLen += 4;
3359 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003360 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003361 using namespace llvm::support;
3362 endian::Writer<little> LE(Out);
3363
Richard Smithdf8a8312015-03-13 04:05:01 +00003364 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003365 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003366 // We emit the key length after the data length so that every
3367 // string is preceded by a 16-bit length. This matches the PTH
3368 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003369 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003370 return std::make_pair(KeyLen, DataLen);
3371 }
Mike Stump11289f42009-09-09 15:08:12 +00003372
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003373 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003374 unsigned KeyLen) {
3375 // Record the location of the key data. This is used when generating
3376 // the mapping from persistent IDs to strings.
3377 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003378
3379 // Emit the offset of the key/data length information to the interesting
3380 // identifiers table if necessary.
3381 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3382 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3383
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003384 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003385 }
Mike Stump11289f42009-09-09 15:08:12 +00003386
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003387 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003388 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003389 using namespace llvm::support;
3390 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003391
Richard Smithd7329392015-04-21 21:46:32 +00003392 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3393 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003394 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003395 return;
3396 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003397
Justin Bognere1c147c2014-03-28 22:03:19 +00003398 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003399 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3400 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003401 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003402 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003403 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003404 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003405 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3406 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003407 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003408 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003409 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003410 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003411
Richard Smithd7329392015-04-21 21:46:32 +00003412 if (HadMacroDefinition)
3413 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003414
Richard Smitha534a312015-07-21 23:54:07 +00003415 if (NeedDecls) {
3416 // Emit the declaration IDs in reverse order, because the
3417 // IdentifierResolver provides the declarations as they would be
3418 // visible (e.g., the function "stat" would come before the struct
3419 // "stat"), but the ASTReader adds declarations to the end of the list
3420 // (so we need to see the struct "stat" before the function "stat").
3421 // Only emit declarations that aren't from a chained PCH, though.
3422 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3423 IdResolver.end());
3424 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3425 DEnd = Decls.rend();
3426 D != DEnd; ++D)
3427 LE.write<uint32_t>(
3428 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3429 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003430 }
3431};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003432
Douglas Gregore84a9da2009-04-20 20:36:09 +00003433} // end anonymous namespace
3434
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003435/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003436///
3437/// The identifier table consists of a blob containing string data
3438/// (the actual identifiers themselves) and a separate "offsets" index
3439/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003440void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3441 IdentifierResolver &IdResolver,
3442 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003443 using namespace llvm;
3444
Richard Smith33e0f7e2015-07-22 02:08:40 +00003445 RecordData InterestingIdents;
3446
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003447 // Create and write out the blob that contains the identifier
3448 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003449 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003450 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003451 ASTIdentifierTableTrait Trait(
3452 *this, PP, IdResolver, IsModule,
3453 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003454
Douglas Gregore6648fb2009-04-28 20:33:11 +00003455 // Look for any identifiers that were named while processing the
3456 // headers, but are otherwise not needed. We add these to the hash
3457 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003458 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003459 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003460 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003461 for (const auto &ID : PP.getIdentifierTable())
3462 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003463 // Sort the identifiers lexicographically before getting them references so
3464 // that their order is stable.
3465 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3466 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003467 if (Trait.isInterestingNonMacroIdentifier(II))
3468 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003469
Sebastian Redlff4a2952010-07-23 23:49:55 +00003470 // Create the on-disk hash table representation. We only store offsets
3471 // for identifiers that appear here for the first time.
3472 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003473 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003474 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003475 IdentID ID = IdentIDPair.second;
3476 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003477 // Write out identifiers if either the ID is local or the identifier has
3478 // changed since it was loaded.
3479 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3480 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003481 (Trait.needDecls() &&
3482 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003483 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003484 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003485
Douglas Gregore84a9da2009-04-20 20:36:09 +00003486 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003487 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003488 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003489 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003490 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003491 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003492 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003493 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003494 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003495 }
3496
3497 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003498 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003499 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003502 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003503
3504 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003505 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003506 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003507 }
3508
3509 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003510 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003511 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003515 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003516
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003517#ifndef NDEBUG
3518 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3519 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3520#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003521
3522 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3523 IdentifierOffsets.size(),
3524 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003525 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003526 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003527
3528 // In C++, write the list of interesting identifiers (those that are
3529 // defined as macros, poisoned, or similar unusual things).
3530 if (!InterestingIdents.empty())
3531 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003532}
3533
Douglas Gregorc5046832009-04-27 18:38:38 +00003534//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003535// DeclContext's Name Lookup Table Serialization
3536//===----------------------------------------------------------------------===//
3537
3538namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003539
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003540// Trait used for the on-disk hash table used in the method pool.
3541class ASTDeclContextNameLookupTrait {
3542 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003543 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003544
3545public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003546 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003547 typedef key_type key_type_ref;
3548
Richard Smithd88a7f12015-09-01 20:35:42 +00003549 /// A start and end index into DeclIDs, representing a sequence of decls.
3550 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003551 typedef const data_type& data_type_ref;
3552
Justin Bogner25463f12014-04-18 20:27:24 +00003553 typedef unsigned hash_value_type;
3554 typedef unsigned offset_type;
3555
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003556 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3557
Richard Smithd88a7f12015-09-01 20:35:42 +00003558 template<typename Coll>
3559 data_type getData(const Coll &Decls) {
3560 unsigned Start = DeclIDs.size();
3561 for (NamedDecl *D : Decls) {
3562 DeclIDs.push_back(
3563 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3564 }
3565 return std::make_pair(Start, DeclIDs.size());
3566 }
3567
3568 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3569 unsigned Start = DeclIDs.size();
3570 for (auto ID : FromReader)
3571 DeclIDs.push_back(ID);
3572 return std::make_pair(Start, DeclIDs.size());
3573 }
3574
3575 static bool EqualKey(key_type_ref a, key_type_ref b) {
3576 return a == b;
3577 }
3578
Richard Smitha06c7e62015-08-26 23:55:49 +00003579 hash_value_type ComputeHash(DeclarationNameKey Name) {
3580 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003581 }
3582
Richard Smithd88a7f12015-09-01 20:35:42 +00003583 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3584 assert(Writer.hasChain() &&
3585 "have reference to loaded module file but no chain?");
3586
3587 using namespace llvm::support;
3588 endian::Writer<little>(Out)
3589 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3590 }
3591
Richard Smitha06c7e62015-08-26 23:55:49 +00003592 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3593 DeclarationNameKey Name,
3594 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003595 using namespace llvm::support;
3596 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003597 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003598 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003599 case DeclarationName::Identifier:
3600 case DeclarationName::ObjCZeroArgSelector:
3601 case DeclarationName::ObjCOneArgSelector:
3602 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003603 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003604 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003605 KeyLen += 4;
3606 break;
3607 case DeclarationName::CXXOperatorName:
3608 KeyLen += 1;
3609 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003610 case DeclarationName::CXXConstructorName:
3611 case DeclarationName::CXXDestructorName:
3612 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003613 case DeclarationName::CXXUsingDirective:
3614 break;
3615 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003616 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003617
Richard Smithf02662d2015-07-30 03:17:16 +00003618 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003619 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3620 assert(uint16_t(DataLen) == DataLen &&
3621 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003622 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003623
3624 return std::make_pair(KeyLen, DataLen);
3625 }
3626
Richard Smitha06c7e62015-08-26 23:55:49 +00003627 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003628 using namespace llvm::support;
3629 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003630 LE.write<uint8_t>(Name.getKind());
3631 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003632 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003633 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003634 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003635 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003636 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003637 case DeclarationName::ObjCZeroArgSelector:
3638 case DeclarationName::ObjCOneArgSelector:
3639 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003640 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003641 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003642 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003643 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003644 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003645 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003646 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003647 case DeclarationName::CXXConstructorName:
3648 case DeclarationName::CXXDestructorName:
3649 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003650 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003651 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003652 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003653
3654 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003655 }
3656
Richard Smitha06c7e62015-08-26 23:55:49 +00003657 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3658 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003659 using namespace llvm::support;
3660 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003661 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003662 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3663 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003664 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3665 }
3666};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003667
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003668} // end anonymous namespace
3669
Chandler Carruthe972c362015-03-26 03:11:40 +00003670bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3671 DeclContext *DC) {
3672 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3673}
3674
3675bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3676 DeclContext *DC) {
3677 for (auto *D : Result.getLookupResult())
3678 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3679 return false;
3680
3681 return true;
3682}
3683
Richard Smithd88a7f12015-09-01 20:35:42 +00003684void
Chandler Carruthe972c362015-03-26 03:11:40 +00003685ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003686 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003687 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3688 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003689 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003690
Chandler Carruthe972c362015-03-26 03:11:40 +00003691 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003692 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003693 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3694
3695 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003696 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3697 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003698 ASTDeclContextNameLookupTrait Trait(*this);
3699
Chandler Carruthe972c362015-03-26 03:11:40 +00003700 // The first step is to collect the declaration names which we need to
3701 // serialize into the name lookup table, and to collect them in a stable
3702 // order.
3703 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003704
Chandler Carruthe972c362015-03-26 03:11:40 +00003705 // We also build up small sets of the constructor and conversion function
3706 // names which are visible.
3707 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003708
Chandler Carruthe972c362015-03-26 03:11:40 +00003709 for (auto &Lookup : *DC->buildLookup()) {
3710 auto &Name = Lookup.first;
3711 auto &Result = Lookup.second;
3712
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003713 // If there are no local declarations in our lookup result, we
3714 // don't need to write an entry for the name at all. If we can't
3715 // write out a lookup set without performing more deserialization,
3716 // just skip this entry.
3717 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003718 isLookupResultEntirelyExternal(Result, DC))
3719 continue;
3720
3721 // We also skip empty results. If any of the results could be external and
3722 // the currently available results are empty, then all of the results are
3723 // external and we skip it above. So the only way we get here with an empty
3724 // results is when no results could have been external *and* we have
3725 // external results.
3726 //
3727 // FIXME: While we might want to start emitting on-disk entries for negative
3728 // lookups into a decl context as an optimization, today we *have* to skip
3729 // them because there are names with empty lookup results in decl contexts
3730 // which we can't emit in any stable ordering: we lookup constructors and
3731 // conversion functions in the enclosing namespace scope creating empty
3732 // results for them. This in almost certainly a bug in Clang's name lookup,
3733 // but that is likely to be hard or impossible to fix and so we tolerate it
3734 // here by omitting lookups with empty results.
3735 if (Lookup.second.getLookupResult().empty())
3736 continue;
3737
3738 switch (Lookup.first.getNameKind()) {
3739 default:
3740 Names.push_back(Lookup.first);
3741 break;
3742
Richard Smithcd45dbc2014-04-19 03:48:30 +00003743 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003744 assert(isa<CXXRecordDecl>(DC) &&
3745 "Cannot have a constructor name outside of a class!");
3746 ConstructorNameSet.insert(Name);
3747 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003748
3749 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003750 assert(isa<CXXRecordDecl>(DC) &&
3751 "Cannot have a conversion function name outside of a class!");
3752 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003753 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003754 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003755 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003756
Chandler Carruthe972c362015-03-26 03:11:40 +00003757 // Sort the names into a stable order.
3758 std::sort(Names.begin(), Names.end());
3759
Chandler Carruthffbf7052015-03-26 22:27:09 +00003760 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003761 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003762 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003763
Chandler Carruthffbf7052015-03-26 22:27:09 +00003764 // First we try the easy case by forming the current context's constructor
3765 // name and adding that name first. This is a very useful optimization to
3766 // avoid walking the lexical declarations in many cases, and it also
3767 // handles the only case where a constructor name can come from some other
3768 // lexical context -- when that name is an implicit constructor merged from
3769 // another declaration in the redecl chain. Any non-implicit constructor or
3770 // conversion function which doesn't occur in all the lexical contexts
3771 // would be an ODR violation.
3772 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3773 Context->getCanonicalType(Context->getRecordType(D)));
3774 if (ConstructorNameSet.erase(ImplicitCtorName))
3775 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003776
Chandler Carruthffbf7052015-03-26 22:27:09 +00003777 // If we still have constructors or conversion functions, we walk all the
3778 // names in the decl and add the constructors and conversion functions
3779 // which are visible in the order they lexically occur within the context.
3780 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3781 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3782 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3783 auto Name = ChildND->getDeclName();
3784 switch (Name.getNameKind()) {
3785 default:
3786 continue;
3787
3788 case DeclarationName::CXXConstructorName:
3789 if (ConstructorNameSet.erase(Name))
3790 Names.push_back(Name);
3791 break;
3792
3793 case DeclarationName::CXXConversionFunctionName:
3794 if (ConversionNameSet.erase(Name))
3795 Names.push_back(Name);
3796 break;
3797 }
3798
3799 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3800 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003801 }
3802
Chandler Carruthe972c362015-03-26 03:11:40 +00003803 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3804 "constructors by walking all the "
3805 "lexical members of the context.");
3806 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3807 "conversion functions by walking all "
3808 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003809 }
3810
Chandler Carruthe972c362015-03-26 03:11:40 +00003811 // Next we need to do a lookup with each name into this decl context to fully
3812 // populate any results from external sources. We don't actually use the
3813 // results of these lookups because we only want to use the results after all
3814 // results have been loaded and the pointers into them will be stable.
3815 for (auto &Name : Names)
3816 DC->lookup(Name);
3817
3818 // Now we need to insert the results for each name into the hash table. For
3819 // constructor names and conversion function names, we actually need to merge
3820 // all of the results for them into one list of results each and insert
3821 // those.
3822 SmallVector<NamedDecl *, 8> ConstructorDecls;
3823 SmallVector<NamedDecl *, 8> ConversionDecls;
3824
3825 // Now loop over the names, either inserting them or appending for the two
3826 // special cases.
3827 for (auto &Name : Names) {
3828 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3829
3830 switch (Name.getNameKind()) {
3831 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003832 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003833 break;
3834
3835 case DeclarationName::CXXConstructorName:
3836 ConstructorDecls.append(Result.begin(), Result.end());
3837 break;
3838
3839 case DeclarationName::CXXConversionFunctionName:
3840 ConversionDecls.append(Result.begin(), Result.end());
3841 break;
3842 }
3843 }
3844
3845 // Handle our two special cases if we ended up having any. We arbitrarily use
3846 // the first declaration's name here because the name itself isn't part of
3847 // the key, only the kind of name is used.
3848 if (!ConstructorDecls.empty())
3849 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003850 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003851 if (!ConversionDecls.empty())
3852 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003853 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003854
Richard Smithd88a7f12015-09-01 20:35:42 +00003855 // Create the on-disk hash table. Also emit the existing imported and
3856 // merged table if there is one.
3857 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3858 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003859}
3860
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003861/// \brief Write the block containing all of the declaration IDs
3862/// visible from the given DeclContext.
3863///
3864/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003865/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003866uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3867 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003868 // If we imported a key declaration of this namespace, write the visible
3869 // lookup results as an update record for it rather than including them
3870 // on this declaration. We will only look at key declarations on reload.
3871 if (isa<NamespaceDecl>(DC) && Chain &&
3872 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3873 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003874 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003875 Prev = Prev->getPreviousDecl())
3876 if (!Prev->isFromASTFile())
3877 return 0;
3878
3879 // Note that we need to emit an update record for the primary context.
3880 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3881
3882 // Make sure all visible decls are written. They will be recorded later. We
3883 // do this using a side data structure so we can sort the names into
3884 // a deterministic order.
3885 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3886 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3887 LookupResults;
3888 if (Map) {
3889 LookupResults.reserve(Map->size());
3890 for (auto &Entry : *Map)
3891 LookupResults.push_back(
3892 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3893 }
3894
3895 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3896 for (auto &NameAndResult : LookupResults) {
3897 DeclarationName Name = NameAndResult.first;
3898 DeclContext::lookup_result Result = NameAndResult.second;
3899 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3900 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3901 // We have to work around a name lookup bug here where negative lookup
3902 // results for these names get cached in namespace lookup tables (these
3903 // names should never be looked up in a namespace).
3904 assert(Result.empty() && "Cannot have a constructor or conversion "
3905 "function name in a namespace!");
3906 continue;
3907 }
3908
3909 for (NamedDecl *ND : Result)
3910 if (!ND->isFromASTFile())
3911 GetDeclRef(ND);
3912 }
3913
3914 return 0;
3915 }
3916
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003917 if (DC->getPrimaryContext() != DC)
3918 return 0;
3919
Chandler Carruthe972c362015-03-26 03:11:40 +00003920 // Skip contexts which don't support name lookup.
3921 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003922 return 0;
3923
3924 // If not in C++, we perform name lookup for the translation unit via the
3925 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003926 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003927 return 0;
3928
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003929 // Serialize the contents of the mapping used for lookup. Note that,
3930 // although we have two very different code paths, the serialized
3931 // representation is the same for both cases: a declaration name,
3932 // followed by a size, followed by references to the visible
3933 // declarations that have that name.
3934 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003935 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003936 if (!Map || Map->empty())
3937 return 0;
3938
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003939 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003940 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003941 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003942
3943 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003944 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003945 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003946 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003947 ++NumVisibleDeclContexts;
3948 return Offset;
3949}
3950
Sebastian Redla4071b42010-08-24 00:50:09 +00003951/// \brief Write an UPDATE_VISIBLE block for the given context.
3952///
3953/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3954/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003955/// (in C++), for namespaces, and for classes with forward-declared unscoped
3956/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003957void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003958 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003959 if (!Map || Map->empty())
3960 return;
3961
Sebastian Redla4071b42010-08-24 00:50:09 +00003962 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003963 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003964 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003965
Richard Smith5fc18a92015-07-12 23:43:21 +00003966 // If we're updating a namespace, select a key declaration as the key for the
3967 // update record; those are the only ones that will be checked on reload.
3968 if (isa<NamespaceDecl>(DC))
3969 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3970
Sebastian Redla4071b42010-08-24 00:50:09 +00003971 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003972 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003973 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003974}
3975
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003976/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3977void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003978 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003979 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3980}
3981
3982/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3983void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003984 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003985 return;
3986
3987 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3988 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00003989 for (const auto &I:Opts.OptMap) {
3990 AddString(I.getKey(), Record);
3991 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00003992 Record.push_back(V.Supported ? 1 : 0);
3993 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00003994 Record.push_back(V.Avail);
3995 Record.push_back(V.Core);
3996 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003997 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3998}
3999
Yaxun Liu5b746652016-12-18 05:18:55 +00004000void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4001 if (!SemaRef.Context.getLangOpts().OpenCL)
4002 return;
4003
4004 RecordData Record;
4005 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4006 Record.push_back(
4007 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4008 Record.push_back(I.second.size());
4009 for (auto Ext : I.second)
4010 AddString(Ext, Record);
4011 }
4012 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4013}
4014
4015void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4016 if (!SemaRef.Context.getLangOpts().OpenCL)
4017 return;
4018
4019 RecordData Record;
4020 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4021 Record.push_back(getDeclID(I.first));
4022 Record.push_back(static_cast<unsigned>(I.second.size()));
4023 for (auto Ext : I.second)
4024 AddString(Ext, Record);
4025 }
4026 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4027}
4028
Justin Lebar67a78a62016-10-08 22:15:58 +00004029void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4030 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4031 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4032 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4033 }
4034}
4035
Douglas Gregor404cdde2012-01-27 01:47:08 +00004036void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004037 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004038 RecordData Categories;
4039
4040 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4041 unsigned Size = 0;
4042 unsigned StartIndex = Categories.size();
4043
4044 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4045
4046 // Allocate space for the size.
4047 Categories.push_back(0);
4048
4049 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004050 for (ObjCInterfaceDecl::known_categories_iterator
4051 Cat = Class->known_categories_begin(),
4052 CatEnd = Class->known_categories_end();
4053 Cat != CatEnd; ++Cat, ++Size) {
4054 assert(getDeclID(*Cat) != 0 && "Bogus category");
4055 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004056 }
4057
4058 // Update the size.
4059 Categories[StartIndex] = Size;
4060
4061 // Record this interface -> category map.
4062 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4063 CategoriesMap.push_back(CatInfo);
4064 }
4065
4066 // Sort the categories map by the definition ID, since the reader will be
4067 // performing binary searches on this information.
4068 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4069
4070 // Emit the categories map.
4071 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004072
David Blaikieb44f0bf2017-01-04 22:36:43 +00004073 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004074 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004077 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004078
4079 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4080 Stream.EmitRecordWithBlob(AbbrevID, Record,
4081 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004082 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004083
Douglas Gregor404cdde2012-01-27 01:47:08 +00004084 // Emit the category lists.
4085 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4086}
4087
Richard Smithe40f2ba2013-08-07 21:41:30 +00004088void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4089 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4090
4091 if (LPTMap.empty())
4092 return;
4093
4094 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004095 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004096 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004097 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004098 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004099 AddDeclRef(LPT.D, Record);
4100 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004101
Justin Lebar28f09c52016-10-10 16:26:08 +00004102 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004103 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004104 }
4105 }
4106 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4107}
4108
Dario Domizioli13a0a382014-05-23 12:13:25 +00004109/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4110void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4111 RecordData Record;
4112 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4113 AddSourceLocation(PragmaLoc, Record);
4114 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4115}
4116
Nico Weber779355f2016-03-02 23:22:00 +00004117/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4118void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4119 RecordData Record;
4120 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4121 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4122}
4123
Nico Weber42932312016-03-03 00:17:35 +00004124/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4125//module.
4126void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4127 RecordData Record;
4128 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4129 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4130 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4131}
4132
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004133void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4134 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004135 // Enter the extension block.
4136 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4137
4138 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004139 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004140 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4141 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4142 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4143 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4144 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4145 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004146 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004147
4148 // Emit the metadata record.
4149 RecordData Record;
4150 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4151 Record.push_back(EXTENSION_METADATA);
4152 Record.push_back(Metadata.MajorVersion);
4153 Record.push_back(Metadata.MinorVersion);
4154 Record.push_back(Metadata.BlockName.size());
4155 Record.push_back(Metadata.UserInfo.size());
4156 SmallString<64> Buffer;
4157 Buffer += Metadata.BlockName;
4158 Buffer += Metadata.UserInfo;
4159 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4160
4161 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004162 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004163
4164 // Exit the extension block.
4165 Stream.ExitBlock();
4166}
4167
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004168//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004169// General Serialization Routines
4170//===----------------------------------------------------------------------===//
4171
Richard Smith69c82bf2016-04-01 22:52:03 +00004172/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004173void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4174 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004175 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004176 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004177 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004178 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004179
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004180#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004181
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004182 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004183}
4184
John McCallf413f5e2013-05-03 00:10:13 +00004185void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4186 AddSourceLocation(Tok.getLocation(), Record);
4187 Record.push_back(Tok.getLength());
4188
4189 // FIXME: When reading literal tokens, reconstruct the literal pointer
4190 // if it is needed.
4191 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4192 // FIXME: Should translate token kind to a stable encoding.
4193 Record.push_back(Tok.getKind());
4194 // FIXME: Should translate token flags to a stable encoding.
4195 Record.push_back(Tok.getFlags());
4196}
4197
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004198void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004199 Record.push_back(Str.size());
4200 Record.insert(Record.end(), Str.begin(), Str.end());
4201}
4202
Richard Smith7ed1bc92014-12-05 22:42:13 +00004203bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004204 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004205
Richard Smith54cc3c22014-12-11 20:50:24 +00004206 bool Changed =
4207 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004208
4209 // Remove a prefix to make the path relative, if relevant.
4210 const char *PathBegin = Path.data();
4211 const char *PathPtr =
4212 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4213 if (PathPtr != PathBegin) {
4214 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4215 Changed = true;
4216 }
4217
4218 return Changed;
4219}
4220
4221void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4222 SmallString<128> FilePath(Path);
4223 PreparePathForOutput(FilePath);
4224 AddString(FilePath, Record);
4225}
4226
Mehdi Amini57a41912015-09-10 01:46:39 +00004227void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004228 StringRef Path) {
4229 SmallString<128> FilePath(Path);
4230 PreparePathForOutput(FilePath);
4231 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4232}
4233
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004234void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4235 RecordDataImpl &Record) {
4236 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004237 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004238 Record.push_back(*Minor + 1);
4239 else
4240 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004241 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004242 Record.push_back(*Subminor + 1);
4243 else
4244 Record.push_back(0);
4245}
4246
Douglas Gregore84a9da2009-04-20 20:36:09 +00004247/// \brief Note that the identifier II occurs at the given offset
4248/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004249void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004250 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004251 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004252 // up earlier in the chain and thus don't need an offset.
4253 if (ID >= FirstIdentID)
4254 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004255}
4256
Douglas Gregor95c13f52009-04-25 17:48:32 +00004257/// \brief Note that the selector Sel occurs at the given offset
4258/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004259void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004260 unsigned ID = SelectorIDs[Sel];
4261 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004262 // Don't record offsets for selectors that are also available in a different
4263 // file.
4264 if (ID < FirstSelectorID)
4265 return;
4266 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004267}
4268
David Blaikie61137e12017-01-05 18:23:18 +00004269ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4270 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4271 bool IncludeTimestamps)
David Blaikie95dd3622017-01-05 18:45:45 +00004272 : Stream(Stream), IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004273 for (const auto &Ext : Extensions) {
4274 if (auto Writer = Ext->createExtensionWriter(*this))
4275 ModuleFileExtensionWriters.push_back(std::move(Writer));
4276 }
4277}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004278
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004279ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004280 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004281}
4282
Richard Smithb3761912015-02-05 23:08:52 +00004283const LangOptions &ASTWriter::getLangOpts() const {
4284 assert(WritingAST && "can't determine lang opts when not writing AST");
4285 return Context->getLangOpts();
4286}
4287
Richard Smithe75ee0f2015-08-17 07:13:32 +00004288time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4289 return IncludeTimestamps ? E->getModificationTime() : 0;
4290}
4291
Adrian Prantladbd2b12015-09-22 23:26:31 +00004292uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4293 Module *WritingModule, StringRef isysroot,
4294 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004295 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004296
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004297 ASTHasCompilerErrors = hasErrors;
4298
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004299 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004300 Stream.Emit((unsigned)'C', 8);
4301 Stream.Emit((unsigned)'P', 8);
4302 Stream.Emit((unsigned)'C', 8);
4303 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004304
Chris Lattner28fa4e62009-04-26 22:26:21 +00004305 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004306
Douglas Gregoreda8e122011-08-09 15:13:55 +00004307 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004308 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004309 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004310 ASTFileSignature Signature =
4311 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004312 Context = nullptr;
4313 PP = nullptr;
4314 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004315 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004316
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004317 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004318 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004319}
4320
Douglas Gregora94a1542011-07-27 21:45:57 +00004321template<typename Vector>
4322static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4323 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004324 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4325 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004326 Writer.AddDeclRef(*I, Record);
4327 }
4328}
4329
Adrian Prantladbd2b12015-09-22 23:26:31 +00004330uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4331 const std::string &OutputFile,
4332 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004333 using namespace llvm;
4334
Craig Toppera13603a2014-05-22 05:54:18 +00004335 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004336
Douglas Gregorcf68c582011-12-01 22:20:10 +00004337 // Make sure that the AST reader knows to finalize itself.
4338 if (Chain)
4339 Chain->finalizeForWriting();
4340
Sebastian Redl143413f2010-07-12 22:02:52 +00004341 ASTContext &Context = SemaRef.Context;
4342 Preprocessor &PP = SemaRef.PP;
4343
Douglas Gregordab42432011-08-12 00:15:20 +00004344 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004345 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4346 if (D) {
4347 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4348 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004349 }
4350 };
4351 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4352 PREDEF_DECL_TRANSLATION_UNIT_ID);
4353 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4354 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4355 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4356 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4357 PREDEF_DECL_OBJC_PROTOCOL_ID);
4358 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4359 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4360 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4361 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4362 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004363 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004364 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4365 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004366 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004367 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4368 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004369 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4370 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004371 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4372 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004373 RegisterPredefDecl(Context.TypePackElementDecl,
4374 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004375
Chris Lattner0c797362009-09-08 18:19:27 +00004376 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004377 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004378 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004379 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004380 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004381
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004382 // Build a record containing all of the file scoped decls in this file.
4383 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004384 if (!isModule)
4385 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4386 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004387
Douglas Gregor851443c2011-08-12 01:39:19 +00004388 // Build a record containing all of the delegating constructors we still need
4389 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004390 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004391 if (!isModule)
4392 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004393
Douglas Gregor851443c2011-08-12 01:39:19 +00004394 // Write the set of weak, undeclared identifiers. We always write the
4395 // entire table, since later PCH files in a PCH chain are only interested in
4396 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004397 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004398 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4399 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4400 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4401 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4402 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4403 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4404 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004405 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004406
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004407 // Build a record containing all of the ext_vector declarations.
4408 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004409 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004410
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004411 // Build a record containing all of the VTable uses information.
4412 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004413 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004414 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4415 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4416 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4417 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4418 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004419 }
4420
Nico Weber72889432014-09-06 01:25:55 +00004421 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4422 RecordData UnusedLocalTypedefNameCandidates;
4423 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4424 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4425
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004426 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004427 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004428 for (const auto &I : SemaRef.PendingInstantiations) {
4429 AddDeclRef(I.first, PendingInstantiations);
4430 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004431 }
4432 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4433 "There are local ones at end of translation unit!");
4434
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004435 // Build a record containing some declaration references.
4436 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004437 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004438 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4439 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004440 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004441 }
4442
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004443 RecordData CUDASpecialDeclRefs;
4444 if (Context.getcudaConfigureCallDecl()) {
4445 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4446 }
4447
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004448 // Build a record containing all of the known namespaces.
4449 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004450 for (const auto &I : SemaRef.KnownNamespaces) {
4451 if (!I.second)
4452 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004453 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004454
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004455 // Build a record of all used, undefined objects that require definitions.
4456 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004457
4458 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004459 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004460 for (const auto &I : Undefined) {
4461 AddDeclRef(I.first, UndefinedButUsed);
4462 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004463 }
4464
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004465 // Build a record containing all delete-expressions that we would like to
4466 // analyze later in AST.
4467 RecordData DeleteExprsToAnalyze;
4468
4469 for (const auto &DeleteExprsInfo :
4470 SemaRef.getMismatchingDeleteExpressions()) {
4471 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4472 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4473 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4474 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4475 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4476 }
4477 }
4478
Douglas Gregor112b9072012-10-18 05:31:06 +00004479 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004480 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004481
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004482 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004483 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004484
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004485 // This is so that older clang versions, before the introduction
4486 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004487 {
4488 RecordData Record = {VERSION_MAJOR};
4489 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4490 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004491
Douglas Gregor851443c2011-08-12 01:39:19 +00004492 // Create a lexical update block containing all of the declarations in the
4493 // translation unit that do not come from other AST files.
4494 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004495 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4496 for (const auto *D : TU->noload_decls()) {
4497 if (!D->isFromASTFile()) {
4498 NewGlobalKindDeclPairs.push_back(D->getKind());
4499 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4500 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004501 }
4502
David Blaikieb44f0bf2017-01-04 22:36:43 +00004503 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004504 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4505 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004506 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004507 {
4508 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4509 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4510 bytes(NewGlobalKindDeclPairs));
4511 }
4512
Douglas Gregor851443c2011-08-12 01:39:19 +00004513 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004514 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004515 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4516 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004517 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004518 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004519 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004520
4521 // If we have any extern "C" names, write out a visible update for them.
4522 if (Context.ExternCContext)
4523 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004524
4525 // If the translation unit has an anonymous namespace, and we don't already
4526 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004527 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004528 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4529 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004530 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004531 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004532 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004533
Richard Smith5652c0f2014-03-21 01:48:23 +00004534 // Add update records for all mangling numbers and static local numbers.
4535 // These aren't really update records, but this is a convenient way of
4536 // tagging this rare extra data onto the declarations.
4537 for (const auto &Number : Context.MangleNumbers)
4538 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004539 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4540 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004541 for (const auto &Number : Context.StaticLocalNumbers)
4542 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004543 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4544 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004545
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004546 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004547 // an AST file, are registered for serialization. Likewise for template
4548 // specializations added to imported templates.
4549 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004550 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004551 }
4552
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004553 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004554 // serialization, if we're storing decls with identifiers.
4555 if (!WritingModule || !getLangOpts().CPlusPlus) {
4556 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004557 for (const auto &ID : PP.getIdentifierTable()) {
4558 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004559 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4560 IIs.push_back(II);
4561 }
4562 // Sort the identifiers to visit based on their name.
4563 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4564 for (const IdentifierInfo *II : IIs) {
4565 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4566 DEnd = SemaRef.IdResolver.end();
4567 D != DEnd; ++D) {
4568 GetDeclRef(*D);
4569 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004570 }
4571 }
4572
Manman Rena0f31a02016-04-29 19:04:05 +00004573 // For method pool in the module, if it contains an entry for a selector,
4574 // the entry should be complete, containing everything introduced by that
4575 // module and all modules it imports. It's possible that the entry is out of
4576 // date, so we need to pull in the new content here.
4577
4578 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4579 // safe, we copy all selectors out.
4580 llvm::SmallVector<Selector, 256> AllSelectors;
4581 for (auto &SelectorAndID : SelectorIDs)
4582 AllSelectors.push_back(SelectorAndID.first);
4583 for (auto &Selector : AllSelectors)
4584 SemaRef.updateOutOfDateSelector(Selector);
4585
Douglas Gregor5204bde2011-08-02 16:26:37 +00004586 // Form the record of special types.
4587 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004588 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004589 AddTypeRef(Context.getFILEType(), SpecialTypes);
4590 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4591 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4592 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4593 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004594 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004595 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004596
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004597 if (Chain) {
4598 // Write the mapping information describing our module dependencies and how
4599 // each of those modules were mapped into our own offset/ID space, so that
4600 // the reader can build the appropriate mapping to its own offset/ID space.
4601 // The map consists solely of a blob with the following format:
4602 // *(module-name-len:i16 module-name:len*i8
4603 // source-location-offset:i32
4604 // identifier-id:i32
4605 // preprocessed-entity-id:i32
4606 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004607 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004608 // selector-id:i32
4609 // declaration-id:i32
4610 // c++-base-specifiers-id:i32
4611 // type-id:i32)
4612 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004613 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004614 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004616 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004617 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004618 {
4619 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004620 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004621 using namespace llvm::support;
4622 endian::Writer<little> LE(Out);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004623 StringRef FileName = M.FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004624 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004625 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004626
Ben Langmuir785180e2014-10-20 16:27:30 +00004627 // Note: if a base ID was uint max, it would not be possible to load
4628 // another module after it or have more than one entity inside it.
4629 uint32_t None = std::numeric_limits<uint32_t>::max();
4630
4631 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4632 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4633 if (ShouldWrite)
4634 LE.write<uint32_t>(BaseID);
4635 else
4636 LE.write<uint32_t>(None);
4637 };
4638
Ben Langmuirfe971d92014-08-16 04:54:18 +00004639 // These values should be unique within a chain, since they will be read
4640 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004641 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4642 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4643 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4644 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4645 M.NumPreprocessedEntities);
4646 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4647 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4648 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4649 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004650 }
4651 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004652 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004653 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4654 Buffer.data(), Buffer.size());
4655 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004656
Richard Smithb9eab6d2014-03-20 19:44:17 +00004657 RecordData DeclUpdatesOffsetsRecord;
4658
Richard Smith59442b42014-03-20 20:07:19 +00004659 // Keep writing types, declarations, and declaration update records
4660 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004661 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4662 WriteTypeAbbrevs();
4663 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004664 do {
4665 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4666 while (!DeclTypesToEmit.empty()) {
4667 DeclOrType DOT = DeclTypesToEmit.front();
4668 DeclTypesToEmit.pop();
4669 if (DOT.isType())
4670 WriteType(DOT.getType());
4671 else
4672 WriteDecl(Context, DOT.getDecl());
4673 }
4674 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004675 Stream.ExitBlock();
4676
Richard Smithb9eab6d2014-03-20 19:44:17 +00004677 DoneWritingDeclsAndTypes = true;
4678
4679 // These things can only be done once we've written out decls and types.
4680 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004681 if (!DeclUpdatesOffsetsRecord.empty())
4682 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004683 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004684 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004685 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004686 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004687 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004688 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004689 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004690 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004691 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004692 WriteFPPragmaOptions(SemaRef.getFPOptions());
4693 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004694 WriteOpenCLExtensionTypes(SemaRef);
4695 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004696 WriteCUDAPragmas(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004697 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004698
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004699 // If we're emitting a module, write out the submodule information.
4700 if (WritingModule)
4701 WriteSubmodules(WritingModule);
4702
Douglas Gregor5204bde2011-08-02 16:26:37 +00004703 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4704
Douglas Gregord4df8652009-04-22 22:02:47 +00004705 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004706 if (!EagerlyDeserializedDecls.empty())
4707 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004708
David Blaikie9ffe5a32017-01-30 05:00:26 +00004709 if (Context.getLangOpts().ModularCodegen)
4710 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4711
Douglas Gregord4df8652009-04-22 22:02:47 +00004712 // Write the record containing tentative definitions.
4713 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004714 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004715
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004716 // Write the record containing unused file scoped decls.
4717 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004718 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004719
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004720 // Write the record containing weak undeclared identifiers.
4721 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004722 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004723 WeakUndeclaredIdentifiers);
4724
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004725 // Write the record containing ext_vector type names.
4726 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004727 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004728
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004729 // Write the record containing VTable uses information.
4730 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004731 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004732
Nico Weber72889432014-09-06 01:25:55 +00004733 // Write the record containing potentially unused local typedefs.
4734 if (!UnusedLocalTypedefNameCandidates.empty())
4735 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4736 UnusedLocalTypedefNameCandidates);
4737
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004738 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004739 if (!PendingInstantiations.empty())
4740 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004741
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004742 // Write the record containing declaration references of Sema.
4743 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004744 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004745
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004746 // Write the record containing CUDA-specific declaration references.
4747 if (!CUDASpecialDeclRefs.empty())
4748 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004749
4750 // Write the delegating constructors.
4751 if (!DelegatingCtorDecls.empty())
4752 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004753
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004754 // Write the known namespaces.
4755 if (!KnownNamespaces.empty())
4756 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004757
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004758 // Write the undefined internal functions and variables, and inline functions.
4759 if (!UndefinedButUsed.empty())
4760 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004761
4762 if (!DeleteExprsToAnalyze.empty())
4763 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4764
Douglas Gregor851443c2011-08-12 01:39:19 +00004765 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004766 for (auto *DC : UpdatedDeclContexts)
4767 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004768
Douglas Gregor959bb062011-12-03 01:15:29 +00004769 if (!WritingModule) {
4770 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004771 struct ModuleInfo {
4772 uint64_t ID;
4773 Module *M;
4774 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4775 };
Richard Smith56be7542014-03-21 00:33:59 +00004776 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004777 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004778 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004779 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4780 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004781 }
Richard Smith56be7542014-03-21 00:33:59 +00004782
4783 if (!Imports.empty()) {
4784 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4785 return A.ID < B.ID;
4786 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004787 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4788 return A.ID == B.ID;
4789 };
Richard Smith56be7542014-03-21 00:33:59 +00004790
4791 // Sort and deduplicate module IDs.
4792 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004793 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004794 Imports.end());
4795
4796 RecordData ImportedModules;
4797 for (const auto &Import : Imports) {
4798 ImportedModules.push_back(Import.ID);
4799 // FIXME: If the module has macros imported then later has declarations
4800 // imported, this location won't be the right one as a location for the
4801 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004802 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004803 }
4804
Douglas Gregor959bb062011-12-03 01:15:29 +00004805 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4806 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004807 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004808
Douglas Gregor404cdde2012-01-27 01:47:08 +00004809 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004810 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004811 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004812 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004813 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004814 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004815
Douglas Gregor08f01292009-04-17 22:13:46 +00004816 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004817 RecordData::value_type Record[] = {
4818 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004819 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004820 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004821
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004822 // Write the module file extension blocks.
4823 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004824 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004825
Adrian Prantladbd2b12015-09-22 23:26:31 +00004826 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004827}
4828
Richard Smithb9eab6d2014-03-20 19:44:17 +00004829void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004830 if (DeclUpdates.empty())
4831 return;
4832
Richard Smith59442b42014-03-20 20:07:19 +00004833 DeclUpdateMap LocalUpdates;
4834 LocalUpdates.swap(DeclUpdates);
4835
Richard Smith6ef42932014-03-20 21:02:00 +00004836 for (auto &DeclUpdate : LocalUpdates) {
4837 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004838
Richard Smithd28ac5b2014-03-22 23:33:22 +00004839 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004840 RecordData RecordData;
4841 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004842 for (auto &Update : DeclUpdate.second) {
4843 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4844
Richard Smith69c82bf2016-04-01 22:52:03 +00004845 // An updated body is emitted last, so that the reader doesn't need
4846 // to skip over the lazy body to reach statements for other records.
4847 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4848 HasUpdatedBody = true;
4849 else
4850 Record.push_back(Kind);
4851
Richard Smith6ef42932014-03-20 21:02:00 +00004852 switch (Kind) {
4853 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4854 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4855 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004856 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004857 Record.push_back(GetDeclRef(Update.getDecl()));
4858 break;
4859
Richard Smith4d235792014-08-07 18:53:08 +00004860 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004861 break;
4862
Richard Smith4d235792014-08-07 18:53:08 +00004863 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004864 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004865 break;
4866
John McCall32791cc2016-01-06 22:34:54 +00004867 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004868 Record.AddStmt(const_cast<Expr *>(
4869 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004870 break;
4871
Richard Smith4b054b22016-08-24 21:25:37 +00004872 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4873 Record.AddStmt(
4874 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4875 break;
4876
Richard Smithcd45dbc2014-04-19 03:48:30 +00004877 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4878 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004879 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004880 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004881 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004882 *Context, const_cast<CXXRecordDecl *>(RD)));
4883
4884 // This state is sometimes updated by template instantiation, when we
4885 // switch from the specialization referring to the template declaration
4886 // to it referring to the template definition.
4887 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4888 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004889 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004890 } else {
4891 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4892 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004893 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004894
4895 // The instantiation might have been resolved to a partial
4896 // specialization. If so, record which one.
4897 auto From = Spec->getInstantiatedFrom();
4898 if (auto PartialSpec =
4899 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4900 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004901 Record.AddDeclRef(PartialSpec);
4902 Record.AddTemplateArgumentList(
4903 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004904 } else {
4905 Record.push_back(false);
4906 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004907 }
4908 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004909 Record.AddSourceLocation(RD->getLocation());
4910 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004911 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004912
4913 // Instantiation may change attributes; write them all out afresh.
4914 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004915 if (D->hasAttrs())
4916 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004917
4918 // FIXME: Ensure we don't get here for explicit instantiations.
4919 break;
4920 }
4921
Richard Smithf8134002015-03-10 01:41:22 +00004922 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004923 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004924 break;
4925
Richard Smith564417a2014-03-20 21:47:22 +00004926 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4927 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004928 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4929 Record);
4930 break;
4931
Richard Smith6ef42932014-03-20 21:02:00 +00004932 case UPD_CXX_DEDUCED_RETURN_TYPE:
4933 Record.push_back(GetOrCreateTypeID(Update.getType()));
4934 break;
4935
4936 case UPD_DECL_MARKED_USED:
4937 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004938
4939 case UPD_MANGLING_NUMBER:
4940 case UPD_STATIC_LOCAL_NUMBER:
4941 Record.push_back(Update.getNumber());
4942 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004943
Alexey Bataev97720002014-11-11 04:05:39 +00004944 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004945 Record.AddSourceRange(
4946 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004947 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004948
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004949 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4950 Record.AddSourceRange(
4951 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4952 break;
4953
Richard Smith65ebb4a2015-03-26 04:09:53 +00004954 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004955 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004956 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004957
4958 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004959 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004960 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004961 }
4962 }
4963
Richard Smithd28ac5b2014-03-22 23:33:22 +00004964 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004965 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004966 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004967 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004968 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004969 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004970 }
4971
Richard Smithcd45dbc2014-04-19 03:48:30 +00004972 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004973 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004974 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004975}
4976
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004977void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004978 uint32_t Raw = Loc.getRawEncoding();
4979 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004980}
4981
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004982void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004983 AddSourceLocation(Range.getBegin(), Record);
4984 AddSourceLocation(Range.getEnd(), Record);
4985}
4986
Richard Smithf144b542016-04-08 21:54:32 +00004987void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4988 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004989 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004990 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004991}
4992
Richard Smithf144b542016-04-08 21:54:32 +00004993void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4994 Record->push_back(Value.isUnsigned());
4995 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004996}
4997
Richard Smithf144b542016-04-08 21:54:32 +00004998void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4999 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005000}
5001
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005002void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005003 Record.push_back(getIdentifierRef(II));
5004}
5005
Sebastian Redl539c5062010-08-18 23:57:32 +00005006IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005007 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005008 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005009
Sebastian Redl539c5062010-08-18 23:57:32 +00005010 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005011 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005012 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005013 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005014}
5015
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005016MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005017 // Don't emit builtin macros like __LINE__ to the AST file unless they
5018 // have been redefined by the header (in which case they are not
5019 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005020 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005021 return 0;
5022
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005023 MacroID &ID = MacroIDs[MI];
5024 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005025 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005026 MacroInfoToEmitData Info = { Name, MI, ID };
5027 MacroInfosToEmit.push_back(Info);
5028 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005029 return ID;
5030}
5031
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005032MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005033 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005034 return 0;
5035
5036 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5037 return MacroIDs[MI];
5038}
5039
5040uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005041 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005042}
5043
Richard Smithe64e7452016-04-18 21:54:58 +00005044void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5045 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005046}
5047
Sebastian Redl539c5062010-08-18 23:57:32 +00005048SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005049 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005050 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005051 }
5052
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005053 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005054 if (SID == 0 && Chain) {
5055 // This might trigger a ReadSelector callback, which will set the ID for
5056 // this selector.
5057 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005058 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005059 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005060 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005061 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005062 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005063 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005064 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005065}
5066
Richard Smithe64e7452016-04-18 21:54:58 +00005067void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5068 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005069}
5070
Richard Smith290d8012016-04-06 17:06:00 +00005071void ASTRecordWriter::AddTemplateArgumentLocInfo(
5072 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005073 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005074 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005075 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005076 break;
5077 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005078 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005079 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005080 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005081 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5082 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005083 break;
5084 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005085 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5086 AddSourceLocation(Arg.getTemplateNameLoc());
5087 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005088 break;
John McCall0ad16662009-10-29 08:12:44 +00005089 case TemplateArgument::Null:
5090 case TemplateArgument::Integral:
5091 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005092 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005093 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005094 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005095 break;
5096 }
5097}
5098
Richard Smith290d8012016-04-06 17:06:00 +00005099void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5100 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005101
5102 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5103 bool InfoHasSameExpr
5104 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005105 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005106 if (InfoHasSameExpr)
5107 return; // Avoid storing the same expr twice.
5108 }
Richard Smith290d8012016-04-06 17:06:00 +00005109 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005110}
5111
Richard Smith290d8012016-04-06 17:06:00 +00005112void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005113 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005114 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005115 return;
5116 }
5117
Richard Smith290d8012016-04-06 17:06:00 +00005118 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005119}
5120
Richard Smith290d8012016-04-06 17:06:00 +00005121void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5122 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005123
Richard Smith290d8012016-04-06 17:06:00 +00005124 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005125 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005126 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005127}
5128
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005129void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005130 Record.push_back(GetOrCreateTypeID(T));
5131}
5132
Richard Smith2095ffe2015-03-16 20:11:03 +00005133TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005134 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005135 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5136 if (T.isNull())
5137 return TypeIdx();
5138 assert(!T.getLocalFastQualifiers());
5139
5140 TypeIdx &Idx = TypeIdxs[T];
5141 if (Idx.getIndex() == 0) {
5142 if (DoneWritingDeclsAndTypes) {
5143 assert(0 && "New type seen after serializing all the types to emit!");
5144 return TypeIdx();
5145 }
5146
5147 // We haven't seen this type before. Assign it a new ID and put it
5148 // into the queue of types to emit.
5149 Idx = TypeIdx(NextTypeID++);
5150 DeclTypesToEmit.push(T);
5151 }
5152 return Idx;
5153 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005154}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005155
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005156TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005157 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005158 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5159 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005160 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005161 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005162
Richard Smith2095ffe2015-03-16 20:11:03 +00005163 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5164 assert(I != TypeIdxs.end() && "Type not emitted!");
5165 return I->second;
5166 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005167}
5168
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005169void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005170 Record.push_back(GetDeclRef(D));
5171}
5172
Sebastian Redl539c5062010-08-18 23:57:32 +00005173DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005174 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005175
5176 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005177 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005178 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005179
5180 // If D comes from an AST file, its declaration ID is already known and
5181 // fixed.
5182 if (D->isFromASTFile())
5183 return D->getGlobalID();
5184
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005185 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005186 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005187 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005188 if (DoneWritingDeclsAndTypes) {
5189 assert(0 && "New decl seen after serializing all the decls to emit!");
5190 return 0;
5191 }
5192
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005193 // We haven't seen this declaration before. Give it a new ID and
5194 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005195 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005196 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005197 }
5198
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005199 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005200}
5201
Sebastian Redl539c5062010-08-18 23:57:32 +00005202DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005203 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005204 return 0;
5205
Douglas Gregorb3163e52012-01-05 22:33:30 +00005206 // If D comes from an AST file, its declaration ID is already known and
5207 // fixed.
5208 if (D->isFromASTFile())
5209 return D->getGlobalID();
5210
Douglas Gregore84a9da2009-04-20 20:36:09 +00005211 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5212 return DeclIDs[D];
5213}
5214
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005215void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005216 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005217 assert(D);
5218
5219 SourceLocation Loc = D->getLocation();
5220 if (Loc.isInvalid())
5221 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005222
5223 // We only keep track of the file-level declarations of each file.
5224 if (!D->getLexicalDeclContext()->isFileContext())
5225 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005226 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5227 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005228 if (isa<ParmVarDecl>(D))
5229 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005230
5231 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005232 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005233 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005234 FileID FID;
5235 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005236 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005237 if (FID.isInvalid())
5238 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005239 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005240
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005241 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005242 if (!Info)
5243 Info = new DeclIDInFileInfo();
5244
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005245 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005246 LocDeclIDsTy &Decls = Info->DeclIDs;
5247
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005248 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005249 Decls.push_back(LocDecl);
5250 return;
5251 }
5252
Benjamin Kramer45025c02013-08-24 13:22:59 +00005253 LocDeclIDsTy::iterator I =
5254 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005255
5256 Decls.insert(I, LocDecl);
5257}
5258
Richard Smithe64e7452016-04-18 21:54:58 +00005259void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005260 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005261 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005262 switch (Name.getNameKind()) {
5263 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005264 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005265 break;
5266
5267 case DeclarationName::ObjCZeroArgSelector:
5268 case DeclarationName::ObjCOneArgSelector:
5269 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005270 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005271 break;
5272
5273 case DeclarationName::CXXConstructorName:
5274 case DeclarationName::CXXDestructorName:
5275 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005276 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005277 break;
5278
Richard Smith35845152017-02-07 01:37:30 +00005279 case DeclarationName::CXXDeductionGuideName:
5280 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5281 break;
5282
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005283 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005284 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005285 break;
5286
Alexis Hunt3d221f22009-11-29 07:34:05 +00005287 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005288 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005289 break;
5290
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005291 case DeclarationName::CXXUsingDirective:
5292 // No extra data to emit
5293 break;
5294 }
5295}
Chris Lattnerca025db2010-05-07 21:43:38 +00005296
Richard Smithd08aeb62014-08-28 01:33:39 +00005297unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5298 assert(needsAnonymousDeclarationNumber(D) &&
5299 "expected an anonymous declaration");
5300
5301 // Number the anonymous declarations within this context, if we've not
5302 // already done so.
5303 auto It = AnonymousDeclarationNumbers.find(D);
5304 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005305 auto *DC = D->getLexicalDeclContext();
5306 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5307 AnonymousDeclarationNumbers[ND] = Number;
5308 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005309
5310 It = AnonymousDeclarationNumbers.find(D);
5311 assert(It != AnonymousDeclarationNumbers.end() &&
5312 "declaration not found within its lexical context");
5313 }
5314
5315 return It->second;
5316}
5317
Richard Smith290d8012016-04-06 17:06:00 +00005318void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5319 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005320 switch (Name.getNameKind()) {
5321 case DeclarationName::CXXConstructorName:
5322 case DeclarationName::CXXDestructorName:
5323 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005324 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005325 break;
5326
5327 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005328 AddSourceLocation(SourceLocation::getFromRawEncoding(
5329 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005330 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005331 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005332 break;
5333
5334 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005335 AddSourceLocation(SourceLocation::getFromRawEncoding(
5336 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005337 break;
5338
5339 case DeclarationName::Identifier:
5340 case DeclarationName::ObjCZeroArgSelector:
5341 case DeclarationName::ObjCOneArgSelector:
5342 case DeclarationName::ObjCMultiArgSelector:
5343 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005344 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005345 break;
5346 }
5347}
5348
Richard Smith290d8012016-04-06 17:06:00 +00005349void ASTRecordWriter::AddDeclarationNameInfo(
5350 const DeclarationNameInfo &NameInfo) {
5351 AddDeclarationName(NameInfo.getName());
5352 AddSourceLocation(NameInfo.getLoc());
5353 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005354}
5355
Richard Smith290d8012016-04-06 17:06:00 +00005356void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5357 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5358 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005359 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005360 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005361}
5362
Richard Smithe64e7452016-04-18 21:54:58 +00005363void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005364 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005365 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005366 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005367
5368 // Push each of the NNS's onto a stack for serialization in reverse order.
5369 while (NNS) {
5370 NestedNames.push_back(NNS);
5371 NNS = NNS->getPrefix();
5372 }
5373
Richard Smithe64e7452016-04-18 21:54:58 +00005374 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005375 while(!NestedNames.empty()) {
5376 NNS = NestedNames.pop_back_val();
5377 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005378 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005379 switch (Kind) {
5380 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005381 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005382 break;
5383
5384 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005385 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005386 break;
5387
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005388 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005389 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005390 break;
5391
Chris Lattnerca025db2010-05-07 21:43:38 +00005392 case NestedNameSpecifier::TypeSpec:
5393 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005394 AddTypeRef(QualType(NNS->getAsType(), 0));
5395 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005396 break;
5397
5398 case NestedNameSpecifier::Global:
5399 // Don't need to write an associated value.
5400 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005401
5402 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005403 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005404 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005405 }
5406 }
5407}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005408
Richard Smith290d8012016-04-06 17:06:00 +00005409void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005410 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005411 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005412 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005413
5414 // Push each of the nested-name-specifiers's onto a stack for
5415 // serialization in reverse order.
5416 while (NNS) {
5417 NestedNames.push_back(NNS);
5418 NNS = NNS.getPrefix();
5419 }
5420
Richard Smith290d8012016-04-06 17:06:00 +00005421 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005422 while(!NestedNames.empty()) {
5423 NNS = NestedNames.pop_back_val();
5424 NestedNameSpecifier::SpecifierKind Kind
5425 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005426 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005427 switch (Kind) {
5428 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005429 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5430 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005431 break;
5432
5433 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005434 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5435 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005436 break;
5437
5438 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005439 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5440 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005441 break;
5442
5443 case NestedNameSpecifier::TypeSpec:
5444 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005445 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5446 AddTypeLoc(NNS.getTypeLoc());
5447 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005448 break;
5449
5450 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005451 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005452 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005453
5454 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005455 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5456 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005457 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005458 }
5459 }
5460}
5461
Richard Smith290d8012016-04-06 17:06:00 +00005462void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005463 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005464 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005465 switch (Kind) {
5466 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005467 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005468 break;
5469
5470 case TemplateName::OverloadedTemplate: {
5471 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005472 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005473 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005474 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005475 break;
5476 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005477
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005478 case TemplateName::QualifiedTemplate: {
5479 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005480 AddNestedNameSpecifier(QualT->getQualifier());
5481 Record->push_back(QualT->hasTemplateKeyword());
5482 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005483 break;
5484 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005485
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005486 case TemplateName::DependentTemplate: {
5487 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005488 AddNestedNameSpecifier(DepT->getQualifier());
5489 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005490 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005491 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005492 else
Richard Smith290d8012016-04-06 17:06:00 +00005493 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005494 break;
5495 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005496
5497 case TemplateName::SubstTemplateTemplateParm: {
5498 SubstTemplateTemplateParmStorage *subst
5499 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005500 AddDeclRef(subst->getParameter());
5501 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005502 break;
5503 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005504
5505 case TemplateName::SubstTemplateTemplateParmPack: {
5506 SubstTemplateTemplateParmPackStorage *SubstPack
5507 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005508 AddDeclRef(SubstPack->getParameterPack());
5509 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005510 break;
5511 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005512 }
5513}
5514
Richard Smith290d8012016-04-06 17:06:00 +00005515void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5516 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005517 switch (Arg.getKind()) {
5518 case TemplateArgument::Null:
5519 break;
5520 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005521 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005522 break;
5523 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005524 AddDeclRef(Arg.getAsDecl());
5525 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005526 break;
5527 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005528 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005529 break;
5530 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005531 AddAPSInt(Arg.getAsIntegral());
5532 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005533 break;
5534 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005535 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005536 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005537 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005538 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005539 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005540 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005541 else
Richard Smith290d8012016-04-06 17:06:00 +00005542 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005543 break;
5544 case TemplateArgument::Expression:
5545 AddStmt(Arg.getAsExpr());
5546 break;
5547 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005548 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005549 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005550 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005551 break;
5552 }
5553}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005554
Richard Smithe64e7452016-04-18 21:54:58 +00005555void ASTRecordWriter::AddTemplateParameterList(
5556 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005557 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005558 AddSourceLocation(TemplateParams->getTemplateLoc());
5559 AddSourceLocation(TemplateParams->getLAngleLoc());
5560 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005561 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005562 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005563 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005564 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005565}
5566
5567/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005568void ASTRecordWriter::AddTemplateArgumentList(
5569 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005570 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005571 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005572 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005573 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005574}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005575
Richard Smith290d8012016-04-06 17:06:00 +00005576void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5577 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005578 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005579 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5580 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5581 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005582 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005583 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005584 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005585}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005586
Richard Smithe64e7452016-04-18 21:54:58 +00005587void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5588 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005589 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005590 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005591 AddDeclRef(I.getDecl());
5592 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005593 }
5594}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005595
Richard Smith290d8012016-04-06 17:06:00 +00005596// FIXME: Move this out of the main ASTRecordWriter interface.
5597void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5598 Record->push_back(Base.isVirtual());
5599 Record->push_back(Base.isBaseOfClass());
5600 Record->push_back(Base.getAccessSpecifierAsWritten());
5601 Record->push_back(Base.getInheritConstructors());
5602 AddTypeSourceInfo(Base.getTypeSourceInfo());
5603 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005604 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005605 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005606}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005607
Richard Smith645d2cf2016-04-14 00:29:55 +00005608static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5609 ArrayRef<CXXBaseSpecifier> Bases) {
5610 ASTWriter::RecordData Record;
5611 ASTRecordWriter Writer(W, Record);
5612 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005613
Richard Smith645d2cf2016-04-14 00:29:55 +00005614 for (auto &Base : Bases)
5615 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005616
Richard Smith645d2cf2016-04-14 00:29:55 +00005617 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005618}
5619
Richard Smith290d8012016-04-06 17:06:00 +00005620// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005621void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5622 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5623}
5624
Richard Smithaa165cf2016-04-13 21:57:08 +00005625static uint64_t
5626EmitCXXCtorInitializers(ASTWriter &W,
5627 ArrayRef<CXXCtorInitializer *> CtorInits) {
5628 ASTWriter::RecordData Record;
5629 ASTRecordWriter Writer(W, Record);
5630 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005631
Richard Smithaa165cf2016-04-13 21:57:08 +00005632 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005633 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005634 Writer.push_back(CTOR_INITIALIZER_BASE);
5635 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5636 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005637 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005638 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5639 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005640 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005641 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5642 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005643 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005644 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5645 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005646 }
Francois Pichetd583da02010-12-04 09:14:42 +00005647
Richard Smithaa165cf2016-04-13 21:57:08 +00005648 Writer.AddSourceLocation(Init->getMemberLocation());
5649 Writer.AddStmt(Init->getInit());
5650 Writer.AddSourceLocation(Init->getLParenLoc());
5651 Writer.AddSourceLocation(Init->getRParenLoc());
5652 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005653 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005654 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005655 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005656
5657 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005658}
5659
Richard Smithaa165cf2016-04-13 21:57:08 +00005660// FIXME: Move this out of the main ASTRecordWriter interface.
5661void ASTRecordWriter::AddCXXCtorInitializers(
5662 ArrayRef<CXXCtorInitializer *> CtorInits) {
5663 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005664}
5665
Richard Smith290d8012016-04-06 17:06:00 +00005666void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005667 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005668 Record->push_back(Data.IsLambda);
5669 Record->push_back(Data.UserDeclaredConstructor);
5670 Record->push_back(Data.UserDeclaredSpecialMembers);
5671 Record->push_back(Data.Aggregate);
5672 Record->push_back(Data.PlainOldData);
5673 Record->push_back(Data.Empty);
5674 Record->push_back(Data.Polymorphic);
5675 Record->push_back(Data.Abstract);
5676 Record->push_back(Data.IsStandardLayout);
5677 Record->push_back(Data.HasNoNonEmptyBases);
5678 Record->push_back(Data.HasPrivateFields);
5679 Record->push_back(Data.HasProtectedFields);
5680 Record->push_back(Data.HasPublicFields);
5681 Record->push_back(Data.HasMutableFields);
5682 Record->push_back(Data.HasVariantMembers);
5683 Record->push_back(Data.HasOnlyCMembers);
5684 Record->push_back(Data.HasInClassInitializer);
5685 Record->push_back(Data.HasUninitializedReferenceMember);
5686 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005687 Record->push_back(Data.HasInheritedConstructor);
5688 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005689 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5690 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5691 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5692 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5693 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5694 Record->push_back(Data.DefaultedDestructorIsDeleted);
5695 Record->push_back(Data.HasTrivialSpecialMembers);
5696 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5697 Record->push_back(Data.HasIrrelevantDestructor);
5698 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5699 Record->push_back(Data.HasDefaultedDefaultConstructor);
5700 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5701 Record->push_back(Data.HasConstexprDefaultConstructor);
5702 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5703 Record->push_back(Data.ComputedVisibleConversions);
5704 Record->push_back(Data.UserProvidedDefaultConstructor);
5705 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005706 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5707 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005708 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5709 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5710 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieub6adf542017-02-18 02:09:28 +00005711 Record->push_back(Data.ODRHash);
Richard Smith561fb152012-02-25 07:33:38 +00005712 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005713
Richard Smith290d8012016-04-06 17:06:00 +00005714 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005715 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005716 AddCXXBaseSpecifiers(Data.bases());
5717
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005718 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005719 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005720 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005721 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005722
Richard Smith290d8012016-04-06 17:06:00 +00005723 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5724 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005725 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005726 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005727
5728 // Add lambda-specific data.
5729 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005730 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005731 Record->push_back(Lambda.Dependent);
5732 Record->push_back(Lambda.IsGenericLambda);
5733 Record->push_back(Lambda.CaptureDefault);
5734 Record->push_back(Lambda.NumCaptures);
5735 Record->push_back(Lambda.NumExplicitCaptures);
5736 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005737 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005738 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005739 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005740 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005741 AddSourceLocation(Capture.getLocation());
5742 Record->push_back(Capture.isImplicit());
5743 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005744 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005745 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005746 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005747 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005748 break;
5749 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005750 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005751 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005752 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005753 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005754 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005755 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005756 break;
5757 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005758 }
5759 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005760}
5761
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005762void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005763 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005764 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005765 assert(FirstDeclID == NextDeclID &&
5766 FirstTypeID == NextTypeID &&
5767 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005768 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005769 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005770 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005771 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005772
Sebastian Redl07a89a82010-07-30 00:29:29 +00005773 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005774
Richard Smith7f330cd2015-03-18 01:42:29 +00005775 // Note, this will get called multiple times, once one the reader starts up
5776 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005777 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5778 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5779 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005780 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005781 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005782 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005783 NextDeclID = FirstDeclID;
5784 NextTypeID = FirstTypeID;
5785 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005786 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005787 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005788 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005789}
5790
Sebastian Redl539c5062010-08-18 23:57:32 +00005791void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005792 // Always keep the highest ID. See \p TypeRead() for more information.
5793 IdentID &StoredID = IdentifierIDs[II];
5794 if (ID > StoredID)
5795 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005796}
5797
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005798void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005799 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005800 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005801 if (ID > StoredID)
5802 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005803}
5804
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005805void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005806 // Always take the highest-numbered type index. This copes with an interesting
5807 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005808 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005809 // keep the higher-numbered entry so that we can properly write it out to
5810 // the AST file.
5811 TypeIdx &StoredIdx = TypeIdxs[T];
5812 if (Idx.getIndex() >= StoredIdx.getIndex())
5813 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005814}
5815
Sebastian Redl539c5062010-08-18 23:57:32 +00005816void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005817 // Always keep the highest ID. See \p TypeRead() for more information.
5818 SelectorID &StoredID = SelectorIDs[S];
5819 if (ID > StoredID)
5820 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005821}
Douglas Gregor91096292010-10-02 19:29:26 +00005822
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005823void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005824 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005825 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005826 MacroDefinitions[MD] = ID;
5827}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005828
Douglas Gregore37a85a2011-12-02 17:30:13 +00005829void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5830 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5831 SubmoduleIDs[Mod] = ID;
5832}
5833
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005834void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005835 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005836 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005837 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005838 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005839 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005840 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005841 // A forward reference was mutated into a definition. Rewrite it.
5842 // FIXME: This happens during template instantiation, should we
5843 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005844 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5845 "completed a tag from another module but not by instantiation?");
5846 DeclUpdates[RD].push_back(
5847 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005848 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005849 }
5850}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005851
Richard Smithf983f7f2015-10-13 00:23:25 +00005852static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5853 if (D->isFromASTFile())
5854 return true;
5855
Richard Smithf983f7f2015-10-13 00:23:25 +00005856 // The predefined __va_list_tag struct is imported if we imported any decls.
5857 // FIXME: This is a gross hack.
5858 return D == D->getASTContext().getVaListTagDecl();
5859}
5860
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005861void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005862 if (Chain && Chain->isProcessingUpdateRecords()) return;
5863 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005864 "Should not add lookup results to non-lookup contexts!");
5865
Vassil Vassilev632eac32016-03-16 11:17:04 +00005866 // TU is handled elsewhere.
5867 if (isa<TranslationUnitDecl>(DC))
5868 return;
5869
5870 // Namespaces are handled elsewhere, except for template instantiations of
5871 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5872 // local instantiations are added to an imported context. Only happens when
5873 // adding ADL lookup candidates, for example templated friends.
5874 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5875 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005876 return;
5877
Richard Smithf983f7f2015-10-13 00:23:25 +00005878 // We're only interested in cases where a local declaration is added to an
5879 // imported context.
5880 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5881 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005882
Richard Smith0f4e2c42015-08-06 04:23:48 +00005883 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005884 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005885 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005886 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5887 // We're adding a visible declaration to a predefined decl context. Ensure
5888 // that we write out all of its lookup results so we don't get a nasty
5889 // surprise when we try to emit its lookup table.
5890 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005891 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005892 }
Richard Smithc26d9742016-10-06 20:30:51 +00005893 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005894}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005895
5896void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005897 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005898 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005899
5900 // We're only interested in cases where a local declaration is added to an
5901 // imported context.
5902 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5903 return;
5904
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005905 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005906 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005907
5908 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005909 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005910 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005911 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005912}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005913
Richard Smith564417a2014-03-20 21:47:22 +00005914void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005915 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005916 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5917 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005918 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005919 // If we don't already know the exception specification for this redecl
5920 // chain, add an update record for it.
5921 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5922 ->getType()
5923 ->castAs<FunctionProtoType>()
5924 ->getExceptionSpecType()))
5925 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5926 });
Richard Smith564417a2014-03-20 21:47:22 +00005927}
5928
Richard Smith1fa5d642013-05-11 05:45:24 +00005929void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005930 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005931 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005932 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005933 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005934 DeclUpdates[D].push_back(
5935 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5936 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005937}
5938
Richard Smithf8134002015-03-10 01:41:22 +00005939void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5940 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005941 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005942 assert(!WritingAST && "Already writing the AST!");
5943 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005944 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005945 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005946 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5947 });
Richard Smithf8134002015-03-10 01:41:22 +00005948}
5949
Sebastian Redlab238a72011-04-24 16:28:06 +00005950void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005951 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005952 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005953 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005954 return; // Declaration not imported from PCH.
5955
Richard Smith4d235792014-08-07 18:53:08 +00005956 // Implicit function decl from a PCH was defined.
5957 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005958}
5959
Richard Smithd28ac5b2014-03-22 23:33:22 +00005960void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005961 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005962 assert(!WritingAST && "Already writing the AST!");
5963 if (!D->isFromASTFile())
5964 return;
5965
Richard Smith9e2341d2015-03-23 03:25:59 +00005966 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005967}
5968
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005969void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005970 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005971 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005972 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005973 return;
5974
5975 // Since the actual instantiation is delayed, this really means that we need
5976 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005977 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005978 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5979 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005980}
5981
John McCall32791cc2016-01-06 22:34:54 +00005982void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005983 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005984 assert(!WritingAST && "Already writing the AST!");
5985 if (!D->isFromASTFile())
5986 return;
5987
5988 DeclUpdates[D].push_back(
5989 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5990}
5991
Richard Smith4b054b22016-08-24 21:25:37 +00005992void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5993 assert(!WritingAST && "Already writing the AST!");
5994 if (!D->isFromASTFile())
5995 return;
5996
5997 DeclUpdates[D].push_back(
5998 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5999}
6000
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006001void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6002 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006003 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006004 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006005 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006006 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006007
6008 assert(IFD->getDefinition() && "Category on a class without a definition?");
6009 ObjCClassesWithCategories.insert(
6010 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006011}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006012
Eli Friedman276dd182013-09-05 00:02:25 +00006013void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006014 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006015 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006016
6017 // If there is *any* declaration of the entity that's not from an AST file,
6018 // we can skip writing the update record. We make sure that isUsed() triggers
6019 // completion of the redeclaration chain of the entity.
6020 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6021 if (IsLocalDecl(Prev))
6022 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006023
Aaron Ballman4f45b712014-03-21 15:22:56 +00006024 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006025}
Alexey Bataev97720002014-11-11 04:05:39 +00006026
6027void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006028 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006029 assert(!WritingAST && "Already writing the AST!");
6030 if (!D->isFromASTFile())
6031 return;
6032
6033 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6034}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006035
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006036void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6037 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006038 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006039 assert(!WritingAST && "Already writing the AST!");
6040 if (!D->isFromASTFile())
6041 return;
6042
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006043 DeclUpdates[D].push_back(
6044 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006045}
6046
Richard Smith4caa4492015-05-15 02:34:32 +00006047void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006048 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006049 assert(!WritingAST && "Already writing the AST!");
6050 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006051 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006052}
Alex Denisovfde64952015-06-26 05:28:36 +00006053
6054void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6055 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006056 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006057 assert(!WritingAST && "Already writing the AST!");
6058 if (!Record->isFromASTFile())
6059 return;
6060 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6061}
Richard Smithc26d9742016-10-06 20:30:51 +00006062
6063void ASTWriter::AddedCXXTemplateSpecialization(
6064 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6065 assert(!WritingAST && "Already writing the AST!");
6066
6067 if (!TD->getFirstDecl()->isFromASTFile())
6068 return;
6069 if (Chain && Chain->isProcessingUpdateRecords())
6070 return;
6071
6072 DeclsToEmitEvenIfUnreferenced.push_back(D);
6073}
6074
6075void ASTWriter::AddedCXXTemplateSpecialization(
6076 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6077 assert(!WritingAST && "Already writing the AST!");
6078
6079 if (!TD->getFirstDecl()->isFromASTFile())
6080 return;
6081 if (Chain && Chain->isProcessingUpdateRecords())
6082 return;
6083
6084 DeclsToEmitEvenIfUnreferenced.push_back(D);
6085}
6086
6087void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6088 const FunctionDecl *D) {
6089 assert(!WritingAST && "Already writing the AST!");
6090
6091 if (!TD->getFirstDecl()->isFromASTFile())
6092 return;
6093 if (Chain && Chain->isProcessingUpdateRecords())
6094 return;
6095
6096 DeclsToEmitEvenIfUnreferenced.push_back(D);
6097}