blob: fff824e4373a3fbdc69e61384ac4f57a87251060 [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"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000076#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000077#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000078#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000079#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000080#include "llvm/Support/Process.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000081#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000082#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000083#include <cassert>
84#include <cstdint>
85#include <cstdlib>
86#include <cstring>
87#include <deque>
88#include <limits>
89#include <new>
90#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +000091#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000092
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000094using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095
Sebastian Redl3df5a082010-07-30 17:03:48 +000096template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000097static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000098 if (v.empty()) return StringRef();
99 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000100 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000101}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000102
103template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000104static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000105 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000106 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000107}
108
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109//===----------------------------------------------------------------------===//
110// Type serialization
111//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000112
Richard Smith290d8012016-04-06 17:06:00 +0000113namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000116 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000117 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000118
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +0000120 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +0000121 /// \brief Abbreviation to use for the record, if any.
122 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000123
Richard Smith290d8012016-04-06 17:06:00 +0000124 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000125 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Richard Smith290d8012016-04-06 17:06:00 +0000126 : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
127
128 uint64_t Emit() {
129 return Record.Emit(Code, AbbrevToUse);
130 }
131
132 void Visit(QualType T) {
133 if (T.hasLocalNonFastQualifiers()) {
134 Qualifiers Qs = T.getLocalQualifiers();
135 Record.AddTypeRef(T.getLocalUnqualifiedType());
136 Record.push_back(Qs.getAsOpaqueValue());
137 Code = TYPE_EXT_QUAL;
138 AbbrevToUse = Writer.TypeExtQualAbbrev;
139 } else {
140 switch (T->getTypeClass()) {
141 // For all of the concrete, non-dependent types, call the
142 // appropriate visitor function.
143#define TYPE(Class, Base) \
144 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
145#define ABSTRACT_TYPE(Class, Base)
146#include "clang/AST/TypeNodes.def"
147 }
148 }
149 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150
151 void VisitArrayType(const ArrayType *T);
152 void VisitFunctionType(const FunctionType *T);
153 void VisitTagType(const TagType *T);
154
155#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
156#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157#include "clang/AST/TypeNodes.def"
158 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000159
Richard Smith290d8012016-04-06 17:06:00 +0000160} // end namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000163 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000167 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000168 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169}
170
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000172 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000173 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174}
175
Reid Kleckner8a365022013-06-24 17:51:48 +0000176void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000177 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000178 Code = TYPE_DECAYED;
179}
180
Reid Kleckner0503a872013-12-05 01:23:43 +0000181void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000182 Record.AddTypeRef(T->getOriginalType());
183 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000184 Code = TYPE_ADJUSTED;
185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000188 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000189 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190}
191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000193 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000194 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000195 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196}
197
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000198void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000199 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000200 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000201}
202
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000203void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000204 Record.AddTypeRef(T->getPointeeType());
205 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000206 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207}
208
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000210 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000211 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000212 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213}
214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000217 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000218 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000228 Record.AddSourceLocation(T->getLBracketLoc());
229 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000230 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000231 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000232}
233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000235 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000237 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000238 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239}
240
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000243 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244}
245
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000246void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000247 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000248 FunctionType::ExtInfo C = T->getExtInfo();
249 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000250 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000251 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000252 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000253 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000254 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000255
256 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
257 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Richard Smith290d8012016-04-06 17:06:00 +0000265static void addExceptionSpec(const FunctionProtoType *T,
266 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000267 Record.push_back(T->getExceptionSpecType());
268 if (T->getExceptionSpecType() == EST_Dynamic) {
269 Record.push_back(T->getNumExceptions());
270 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000271 Record.AddTypeRef(T->getExceptionType(I));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000272 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
Richard Smith290d8012016-04-06 17:06:00 +0000273 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000274 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000275 Record.AddDeclRef(T->getExceptionSpecDecl());
276 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000277 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000278 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000279 }
Richard Smith564417a2014-03-20 21:47:22 +0000280}
281
282void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
283 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000284
Richard Smith564417a2014-03-20 21:47:22 +0000285 Record.push_back(T->isVariadic());
286 Record.push_back(T->hasTrailingReturn());
287 Record.push_back(T->getTypeQuals());
288 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000289 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000290
291 Record.push_back(T->getNumParams());
292 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000293 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000294
John McCall18afab72016-03-01 00:49:02 +0000295 if (T->hasExtParameterInfos()) {
296 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
297 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
298 }
299
Richard Smith01b2cb42014-07-26 06:37:51 +0000300 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000301 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
302 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000303 AbbrevToUse = 0;
304
Sebastian Redl539c5062010-08-18 23:57:32 +0000305 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000306}
307
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000308void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000309 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000310 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000311}
John McCallb96ec562009-12-04 22:46:56 +0000312
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000313void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000314 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000315 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000316 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000317 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000318}
319
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000320void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000321 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000322 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000323}
324
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000325void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000326 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000327 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000328}
329
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000331 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000332 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000333 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000334}
335
Alexis Hunte852b102011-05-24 22:41:36 +0000336void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000337 Record.AddTypeRef(T->getBaseType());
338 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000339 Record.push_back(T->getUTTKind());
340 Code = TYPE_UNARY_TRANSFORM;
341}
342
Richard Smith30482bc2011-02-20 03:19:35 +0000343void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000344 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000345 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000346 if (T->getDeducedType().isNull())
347 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000348 Code = TYPE_AUTO;
349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000352 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000353 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000354 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000355 "Cannot serialize in the middle of a type definition");
356}
357
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000358void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000359 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000360 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000361}
362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000363void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000364 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000365 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000366}
367
John McCall81904512011-01-06 01:58:22 +0000368void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000369 Record.AddTypeRef(T->getModifiedType());
370 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000371 Record.push_back(T->getAttrKind());
372 Code = TYPE_ATTRIBUTED;
373}
374
Mike Stump11289f42009-09-09 15:08:12 +0000375void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000376ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000377 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000378 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
379 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000380 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000381}
382
383void
Douglas Gregorada4b792011-01-14 02:55:32 +0000384ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
385 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000386 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
387 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000388 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
389}
390
391void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000392ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000393 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000394 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000395 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000396 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000397 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000398 Record.AddTemplateArgument(ArgI);
399 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
400 : T->isCanonicalUnqualified()
401 ? QualType()
402 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000403 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000404}
405
406void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000407ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000408 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000409 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000410 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000411 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000412}
413
414void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000415ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000416 const DependentSizedExtVectorType *T) {
417 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000418 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000419}
420
421void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000422ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000423 Record.push_back(T->getDepth());
424 Record.push_back(T->getIndex());
425 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000426 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000427 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000428}
429
430void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000431ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000432 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000433 Record.AddNestedNameSpecifier(T->getQualifier());
434 Record.AddIdentifierRef(T->getIdentifier());
435 Record.AddTypeRef(
436 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000437 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000438}
439
440void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000441ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000442 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000443 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000444 Record.AddNestedNameSpecifier(T->getQualifier());
445 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000446 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000447 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000448 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000449 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000450}
451
Douglas Gregord2fa7662010-12-20 02:24:11 +0000452void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000453 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000454 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000455 Record.push_back(*NumExpansions + 1);
456 else
457 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000458 Code = TYPE_PACK_EXPANSION;
459}
460
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000461void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000462 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000463 Code = TYPE_PAREN;
464}
465
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000466void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000467 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000468 Record.AddNestedNameSpecifier(T->getQualifier());
469 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000470 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000471}
472
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000473void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000474 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
475 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000476 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000477}
478
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000479void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000480 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000481 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000482}
483
Manman Rene6be26c2016-09-13 17:25:08 +0000484void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
485 Record.AddDeclRef(T->getDecl());
486 Record.push_back(T->getNumProtocols());
487 for (const auto *I : T->quals())
488 Record.AddDeclRef(I);
489 Code = TYPE_OBJC_TYPE_PARAM;
490}
491
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000492void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000493 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000494 Record.push_back(T->getTypeArgsAsWritten().size());
495 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000496 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000497 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000498 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000499 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000500 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000501 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000502}
503
Steve Narofffb4330f2009-06-17 22:40:22 +0000504void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000505ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000506 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000507 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000508}
509
Eli Friedman0dfb8892011-10-06 23:00:33 +0000510void
511ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000512 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000513 Code = TYPE_ATOMIC;
514}
515
Xiuli Pan9c14e282016-01-09 12:53:17 +0000516void
517ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000518 Record.AddTypeRef(T->getElementType());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000519 Code = TYPE_PIPE;
520}
521
John McCall8f115c62009-10-16 21:56:05 +0000522namespace {
523
524class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000525 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000526
527public:
Richard Smith290d8012016-04-06 17:06:00 +0000528 TypeLocWriter(ASTRecordWriter &Record)
529 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000530
John McCall17001972009-10-18 01:05:36 +0000531#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000532#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000533 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000534#include "clang/AST/TypeLocNodes.def"
535
John McCall17001972009-10-18 01:05:36 +0000536 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
537 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000538};
539
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000540} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000541
John McCall17001972009-10-18 01:05:36 +0000542void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
543 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000544}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000545
John McCall17001972009-10-18 01:05:36 +0000546void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000547 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000548 if (TL.needsExtraLocalData()) {
549 Record.push_back(TL.getWrittenTypeSpec());
550 Record.push_back(TL.getWrittenSignSpec());
551 Record.push_back(TL.getWrittenWidthSpec());
552 Record.push_back(TL.hasModeAttr());
553 }
John McCall8f115c62009-10-16 21:56:05 +0000554}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000555
John McCall17001972009-10-18 01:05:36 +0000556void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000557 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000558}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000559
John McCall17001972009-10-18 01:05:36 +0000560void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000561 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000562}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000563
Reid Kleckner8a365022013-06-24 17:51:48 +0000564void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
565 // nothing to do
566}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000567
Reid Kleckner0503a872013-12-05 01:23:43 +0000568void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
569 // nothing to do
570}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000571
John McCall17001972009-10-18 01:05:36 +0000572void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000573 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000574}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000575
John McCall17001972009-10-18 01:05:36 +0000576void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000577 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000578}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000579
John McCall17001972009-10-18 01:05:36 +0000580void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000581 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000582}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000583
John McCall17001972009-10-18 01:05:36 +0000584void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000585 Record.AddSourceLocation(TL.getStarLoc());
586 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000587}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000588
John McCall17001972009-10-18 01:05:36 +0000589void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000590 Record.AddSourceLocation(TL.getLBracketLoc());
591 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000592 Record.push_back(TL.getSizeExpr() ? 1 : 0);
593 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000594 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000595}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000596
John McCall17001972009-10-18 01:05:36 +0000597void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
598 VisitArrayTypeLoc(TL);
599}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000600
John McCall17001972009-10-18 01:05:36 +0000601void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
602 VisitArrayTypeLoc(TL);
603}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000604
John McCall17001972009-10-18 01:05:36 +0000605void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
606 VisitArrayTypeLoc(TL);
607}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000608
John McCall17001972009-10-18 01:05:36 +0000609void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
610 DependentSizedArrayTypeLoc TL) {
611 VisitArrayTypeLoc(TL);
612}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000613
John McCall17001972009-10-18 01:05:36 +0000614void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
615 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000616 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000617}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000618
John McCall17001972009-10-18 01:05:36 +0000619void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000620 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000621}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000622
John McCall17001972009-10-18 01:05:36 +0000623void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000624 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000625}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000626
John McCall17001972009-10-18 01:05:36 +0000627void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000628 Record.AddSourceLocation(TL.getLocalRangeBegin());
629 Record.AddSourceLocation(TL.getLParenLoc());
630 Record.AddSourceLocation(TL.getRParenLoc());
631 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000632 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000633 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000634}
635void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
636 VisitFunctionTypeLoc(TL);
637}
638void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
639 VisitFunctionTypeLoc(TL);
640}
John McCallb96ec562009-12-04 22:46:56 +0000641void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000642 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000643}
John McCall17001972009-10-18 01:05:36 +0000644void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000645 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000646}
Manman Rene6be26c2016-09-13 17:25:08 +0000647void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
648 if (TL.getNumProtocols()) {
649 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
650 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
651 }
652 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
653 Record.AddSourceLocation(TL.getProtocolLoc(i));
654}
John McCall17001972009-10-18 01:05:36 +0000655void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000656 Record.AddSourceLocation(TL.getTypeofLoc());
657 Record.AddSourceLocation(TL.getLParenLoc());
658 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000659}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000660
John McCall17001972009-10-18 01:05:36 +0000661void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000662 Record.AddSourceLocation(TL.getTypeofLoc());
663 Record.AddSourceLocation(TL.getLParenLoc());
664 Record.AddSourceLocation(TL.getRParenLoc());
665 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000666}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000667
John McCall17001972009-10-18 01:05:36 +0000668void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000669 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000670}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000671
Alexis Hunte852b102011-05-24 22:41:36 +0000672void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000673 Record.AddSourceLocation(TL.getKWLoc());
674 Record.AddSourceLocation(TL.getLParenLoc());
675 Record.AddSourceLocation(TL.getRParenLoc());
676 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000677}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000678
Richard Smith30482bc2011-02-20 03:19:35 +0000679void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000680 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000681}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000682
John McCall17001972009-10-18 01:05:36 +0000683void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000684 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000685}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000686
John McCall17001972009-10-18 01:05:36 +0000687void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000688 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000689}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000690
John McCall81904512011-01-06 01:58:22 +0000691void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000692 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000693 if (TL.hasAttrOperand()) {
694 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000695 Record.AddSourceLocation(range.getBegin());
696 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000697 }
698 if (TL.hasAttrExprOperand()) {
699 Expr *operand = TL.getAttrExprOperand();
700 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000701 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000702 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000703 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000704 }
705}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000706
John McCall17001972009-10-18 01:05:36 +0000707void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000708 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000709}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000710
John McCallcebee162009-10-18 09:09:24 +0000711void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
712 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000713 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000714}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000715
Douglas Gregorada4b792011-01-14 02:55:32 +0000716void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
717 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000718 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000719}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000720
John McCall17001972009-10-18 01:05:36 +0000721void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
722 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000723 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
724 Record.AddSourceLocation(TL.getTemplateNameLoc());
725 Record.AddSourceLocation(TL.getLAngleLoc());
726 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000727 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000728 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
729 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000730}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000731
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000732void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000733 Record.AddSourceLocation(TL.getLParenLoc());
734 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000735}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000736
Abramo Bagnara6150c882010-05-11 21:36:43 +0000737void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000738 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
739 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000740}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000741
John McCalle78aac42010-03-10 03:28:59 +0000742void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000743 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000744}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000745
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000746void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000747 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
748 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
749 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000750}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000751
John McCallc392f372010-06-11 00:33:02 +0000752void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
753 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000754 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
755 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
756 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
757 Record.AddSourceLocation(TL.getTemplateNameLoc());
758 Record.AddSourceLocation(TL.getLAngleLoc());
759 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000760 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000761 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
762 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000763}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000764
Douglas Gregord2fa7662010-12-20 02:24:11 +0000765void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000766 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000767}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000768
John McCall17001972009-10-18 01:05:36 +0000769void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000770 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000771}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000772
John McCall8b07ec22010-05-15 11:32:37 +0000773void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
774 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000775 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
776 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000777 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000778 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
779 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
780 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000781 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000782 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000783}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000784
John McCallfc93cf92009-10-22 22:37:11 +0000785void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000786 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000787}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000788
Eli Friedman0dfb8892011-10-06 23:00:33 +0000789void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000790 Record.AddSourceLocation(TL.getKWLoc());
791 Record.AddSourceLocation(TL.getLParenLoc());
792 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000793}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000794
Xiuli Pan9c14e282016-01-09 12:53:17 +0000795void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000796 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000797}
John McCall8f115c62009-10-16 21:56:05 +0000798
Richard Smith01b2cb42014-07-26 06:37:51 +0000799void ASTWriter::WriteTypeAbbrevs() {
800 using namespace llvm;
801
802 BitCodeAbbrev *Abv;
803
804 // Abbreviation for TYPE_EXT_QUAL
805 Abv = new BitCodeAbbrev();
806 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
807 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
808 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
809 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
810
811 // Abbreviation for TYPE_FUNCTION_PROTO
812 Abv = new BitCodeAbbrev();
813 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
814 // FunctionType
815 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
816 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
817 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
818 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
819 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
820 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
821 // FunctionProtoType
822 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
823 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
824 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
825 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
826 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
827 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
828 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
829 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
830 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
831}
832
Chris Lattner19cea4e2009-04-22 05:57:30 +0000833//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000834// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000835//===----------------------------------------------------------------------===//
836
Chris Lattner28fa4e62009-04-26 22:26:21 +0000837static void EmitBlockID(unsigned ID, const char *Name,
838 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000839 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000840 Record.clear();
841 Record.push_back(ID);
842 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
843
844 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000845 if (!Name || Name[0] == 0)
846 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000847 Record.clear();
848 while (*Name)
849 Record.push_back(*Name++);
850 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
851}
852
853static void EmitRecordID(unsigned ID, const char *Name,
854 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000855 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000856 Record.clear();
857 Record.push_back(ID);
858 while (*Name)
859 Record.push_back(*Name++);
860 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000861}
862
863static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000864 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000865#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000866 RECORD(STMT_STOP);
867 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000868 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000869 RECORD(STMT_NULL);
870 RECORD(STMT_COMPOUND);
871 RECORD(STMT_CASE);
872 RECORD(STMT_DEFAULT);
873 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000874 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000875 RECORD(STMT_IF);
876 RECORD(STMT_SWITCH);
877 RECORD(STMT_WHILE);
878 RECORD(STMT_DO);
879 RECORD(STMT_FOR);
880 RECORD(STMT_GOTO);
881 RECORD(STMT_INDIRECT_GOTO);
882 RECORD(STMT_CONTINUE);
883 RECORD(STMT_BREAK);
884 RECORD(STMT_RETURN);
885 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000886 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000887 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000888 RECORD(EXPR_PREDEFINED);
889 RECORD(EXPR_DECL_REF);
890 RECORD(EXPR_INTEGER_LITERAL);
891 RECORD(EXPR_FLOATING_LITERAL);
892 RECORD(EXPR_IMAGINARY_LITERAL);
893 RECORD(EXPR_STRING_LITERAL);
894 RECORD(EXPR_CHARACTER_LITERAL);
895 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000896 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000897 RECORD(EXPR_UNARY_OPERATOR);
898 RECORD(EXPR_SIZEOF_ALIGN_OF);
899 RECORD(EXPR_ARRAY_SUBSCRIPT);
900 RECORD(EXPR_CALL);
901 RECORD(EXPR_MEMBER);
902 RECORD(EXPR_BINARY_OPERATOR);
903 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
904 RECORD(EXPR_CONDITIONAL_OPERATOR);
905 RECORD(EXPR_IMPLICIT_CAST);
906 RECORD(EXPR_CSTYLE_CAST);
907 RECORD(EXPR_COMPOUND_LITERAL);
908 RECORD(EXPR_EXT_VECTOR_ELEMENT);
909 RECORD(EXPR_INIT_LIST);
910 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000911 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000912 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000913 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000914 RECORD(EXPR_VA_ARG);
915 RECORD(EXPR_ADDR_LABEL);
916 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000917 RECORD(EXPR_CHOOSE);
918 RECORD(EXPR_GNU_NULL);
919 RECORD(EXPR_SHUFFLE_VECTOR);
920 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000921 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000922 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000923 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000924 RECORD(EXPR_OBJC_ARRAY_LITERAL);
925 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000926 RECORD(EXPR_OBJC_ENCODE);
927 RECORD(EXPR_OBJC_SELECTOR_EXPR);
928 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
929 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
930 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
931 RECORD(EXPR_OBJC_KVC_REF_EXPR);
932 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000933 RECORD(STMT_OBJC_FOR_COLLECTION);
934 RECORD(STMT_OBJC_CATCH);
935 RECORD(STMT_OBJC_FINALLY);
936 RECORD(STMT_OBJC_AT_TRY);
937 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
938 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000939 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000940 RECORD(STMT_CXX_CATCH);
941 RECORD(STMT_CXX_TRY);
942 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000943 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000944 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000945 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000946 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000947 RECORD(EXPR_CXX_STATIC_CAST);
948 RECORD(EXPR_CXX_DYNAMIC_CAST);
949 RECORD(EXPR_CXX_REINTERPRET_CAST);
950 RECORD(EXPR_CXX_CONST_CAST);
951 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000952 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000953 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000954 RECORD(EXPR_CXX_BOOL_LITERAL);
955 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000956 RECORD(EXPR_CXX_TYPEID_EXPR);
957 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000958 RECORD(EXPR_CXX_THIS);
959 RECORD(EXPR_CXX_THROW);
960 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000961 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000962 RECORD(EXPR_CXX_BIND_TEMPORARY);
963 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
964 RECORD(EXPR_CXX_NEW);
965 RECORD(EXPR_CXX_DELETE);
966 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
967 RECORD(EXPR_EXPR_WITH_CLEANUPS);
968 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
969 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
970 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
971 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
972 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000973 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000974 RECORD(EXPR_CXX_NOEXCEPT);
975 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000976 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
977 RECORD(EXPR_TYPE_TRAIT);
978 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000979 RECORD(EXPR_PACK_EXPANSION);
980 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000981 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000982 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000983 RECORD(EXPR_FUNCTION_PARM_PACK);
984 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000985 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000986 RECORD(EXPR_CXX_UUIDOF_EXPR);
987 RECORD(EXPR_CXX_UUIDOF_TYPE);
988 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000989#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000990}
Mike Stump11289f42009-09-09 15:08:12 +0000991
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000992void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000993 RecordData Record;
994 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000995
Sebastian Redl539c5062010-08-18 23:57:32 +0000996#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
997#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000998
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000999 // Control Block.
1000 BLOCK(CONTROL_BLOCK);
1001 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +00001002 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001003 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001004 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001005 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001006 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001007 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001008 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001009 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001010 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001011
1012 BLOCK(OPTIONS_BLOCK);
1013 RECORD(LANGUAGE_OPTIONS);
1014 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001015 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001016 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001017 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001018 RECORD(PREPROCESSOR_OPTIONS);
1019
Douglas Gregor108cb222012-10-19 00:45:00 +00001020 BLOCK(INPUT_FILES_BLOCK);
1021 RECORD(INPUT_FILE);
1022
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001023 // AST Top-Level Block.
1024 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001025 RECORD(TYPE_OFFSET);
1026 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001027 RECORD(IDENTIFIER_OFFSET);
1028 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001029 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001030 RECORD(SPECIAL_TYPES);
1031 RECORD(STATISTICS);
1032 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001033 RECORD(SELECTOR_OFFSETS);
1034 RECORD(METHOD_POOL);
1035 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001036 RECORD(SOURCE_LOCATION_OFFSETS);
1037 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001038 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001039 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001040 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001041 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001042 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001043 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001044 RECORD(SEMA_DECL_REFS);
1045 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1046 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001047 RECORD(UPDATE_VISIBLE);
1048 RECORD(DECL_UPDATE_OFFSETS);
1049 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001050 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001051 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001052 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001053 RECORD(FP_PRAGMA_OPTIONS);
1054 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001055 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001056 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001057 RECORD(MODULE_OFFSET_MAP);
1058 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001059 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001060 RECORD(FILE_SORTED_DECLS);
1061 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001062 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001063 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001064 RECORD(INTERESTING_IDENTIFIERS);
1065 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001066 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001067 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001068 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001069 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001070 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001071 RECORD(DELETE_EXPRS_TO_ANALYZE);
Douglas Gregor358cd442012-01-15 16:58:34 +00001072
Chris Lattner28fa4e62009-04-26 22:26:21 +00001073 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001074 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001075 RECORD(SM_SLOC_FILE_ENTRY);
1076 RECORD(SM_SLOC_BUFFER_ENTRY);
1077 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001078 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001079 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001080
Chris Lattner28fa4e62009-04-26 22:26:21 +00001081 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001082 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001083 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001084 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001085 RECORD(PP_MACRO_OBJECT_LIKE);
1086 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001087 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001088
Richard Smithfa715652015-08-31 22:43:10 +00001089 // Submodule Block.
1090 BLOCK(SUBMODULE_BLOCK);
1091 RECORD(SUBMODULE_METADATA);
1092 RECORD(SUBMODULE_DEFINITION);
1093 RECORD(SUBMODULE_UMBRELLA_HEADER);
1094 RECORD(SUBMODULE_HEADER);
1095 RECORD(SUBMODULE_TOPHEADER);
1096 RECORD(SUBMODULE_UMBRELLA_DIR);
1097 RECORD(SUBMODULE_IMPORTS);
1098 RECORD(SUBMODULE_EXPORTS);
1099 RECORD(SUBMODULE_REQUIRES);
1100 RECORD(SUBMODULE_EXCLUDED_HEADER);
1101 RECORD(SUBMODULE_LINK_LIBRARY);
1102 RECORD(SUBMODULE_CONFIG_MACRO);
1103 RECORD(SUBMODULE_CONFLICT);
1104 RECORD(SUBMODULE_PRIVATE_HEADER);
1105 RECORD(SUBMODULE_TEXTUAL_HEADER);
1106 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001107 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001108
1109 // Comments Block.
1110 BLOCK(COMMENTS_BLOCK);
1111 RECORD(COMMENTS_RAW_COMMENT);
1112
Douglas Gregor12bfa382009-10-17 00:13:19 +00001113 // Decls and Types block.
1114 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001115 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001116 RECORD(TYPE_COMPLEX);
1117 RECORD(TYPE_POINTER);
1118 RECORD(TYPE_BLOCK_POINTER);
1119 RECORD(TYPE_LVALUE_REFERENCE);
1120 RECORD(TYPE_RVALUE_REFERENCE);
1121 RECORD(TYPE_MEMBER_POINTER);
1122 RECORD(TYPE_CONSTANT_ARRAY);
1123 RECORD(TYPE_INCOMPLETE_ARRAY);
1124 RECORD(TYPE_VARIABLE_ARRAY);
1125 RECORD(TYPE_VECTOR);
1126 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001127 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001128 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001129 RECORD(TYPE_TYPEDEF);
1130 RECORD(TYPE_TYPEOF_EXPR);
1131 RECORD(TYPE_TYPEOF);
1132 RECORD(TYPE_RECORD);
1133 RECORD(TYPE_ENUM);
1134 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001135 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001136 RECORD(TYPE_DECLTYPE);
1137 RECORD(TYPE_ELABORATED);
1138 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1139 RECORD(TYPE_UNRESOLVED_USING);
1140 RECORD(TYPE_INJECTED_CLASS_NAME);
1141 RECORD(TYPE_OBJC_OBJECT);
1142 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1143 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1144 RECORD(TYPE_DEPENDENT_NAME);
1145 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1146 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1147 RECORD(TYPE_PAREN);
1148 RECORD(TYPE_PACK_EXPANSION);
1149 RECORD(TYPE_ATTRIBUTED);
1150 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001151 RECORD(TYPE_AUTO);
1152 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001153 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001154 RECORD(TYPE_DECAYED);
1155 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001156 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001157 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001158 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001159 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001160 RECORD(DECL_ENUM);
1161 RECORD(DECL_RECORD);
1162 RECORD(DECL_ENUM_CONSTANT);
1163 RECORD(DECL_FUNCTION);
1164 RECORD(DECL_OBJC_METHOD);
1165 RECORD(DECL_OBJC_INTERFACE);
1166 RECORD(DECL_OBJC_PROTOCOL);
1167 RECORD(DECL_OBJC_IVAR);
1168 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001169 RECORD(DECL_OBJC_CATEGORY);
1170 RECORD(DECL_OBJC_CATEGORY_IMPL);
1171 RECORD(DECL_OBJC_IMPLEMENTATION);
1172 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1173 RECORD(DECL_OBJC_PROPERTY);
1174 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001175 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001176 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001177 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001178 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001179 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001180 RECORD(DECL_FILE_SCOPE_ASM);
1181 RECORD(DECL_BLOCK);
1182 RECORD(DECL_CONTEXT_LEXICAL);
1183 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001184 RECORD(DECL_NAMESPACE);
1185 RECORD(DECL_NAMESPACE_ALIAS);
1186 RECORD(DECL_USING);
1187 RECORD(DECL_USING_SHADOW);
1188 RECORD(DECL_USING_DIRECTIVE);
1189 RECORD(DECL_UNRESOLVED_USING_VALUE);
1190 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1191 RECORD(DECL_LINKAGE_SPEC);
1192 RECORD(DECL_CXX_RECORD);
1193 RECORD(DECL_CXX_METHOD);
1194 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001195 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001196 RECORD(DECL_CXX_DESTRUCTOR);
1197 RECORD(DECL_CXX_CONVERSION);
1198 RECORD(DECL_ACCESS_SPEC);
1199 RECORD(DECL_FRIEND);
1200 RECORD(DECL_FRIEND_TEMPLATE);
1201 RECORD(DECL_CLASS_TEMPLATE);
1202 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1203 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001204 RECORD(DECL_VAR_TEMPLATE);
1205 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1206 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001207 RECORD(DECL_FUNCTION_TEMPLATE);
1208 RECORD(DECL_TEMPLATE_TYPE_PARM);
1209 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1210 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001211 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001212 RECORD(DECL_STATIC_ASSERT);
1213 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001214 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001215 RECORD(DECL_INDIRECTFIELD);
1216 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001217 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1218 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1219 RECORD(DECL_IMPORT);
1220 RECORD(DECL_OMP_THREADPRIVATE);
1221 RECORD(DECL_EMPTY);
1222 RECORD(DECL_OBJC_TYPE_PARAM);
1223 RECORD(DECL_OMP_CAPTUREDEXPR);
1224 RECORD(DECL_PRAGMA_COMMENT);
1225 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1226 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001227
Douglas Gregor03412ba2011-06-03 02:27:19 +00001228 // Statements and Exprs can occur in the Decls and Types block.
1229 AddStmtsExprs(Stream, Record);
1230
Douglas Gregor92a96f52011-02-08 21:58:10 +00001231 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001232 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001233 RECORD(PPD_MACRO_DEFINITION);
1234 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001235
1236 // Decls and Types block.
1237 BLOCK(EXTENSION_BLOCK);
1238 RECORD(EXTENSION_METADATA);
1239
Chris Lattner28fa4e62009-04-26 22:26:21 +00001240#undef RECORD
1241#undef BLOCK
1242 Stream.ExitBlock();
1243}
1244
Richard Smith54cc3c22014-12-11 20:50:24 +00001245/// \brief Prepares a path for being written to an AST file by converting it
1246/// to an absolute path and removing nested './'s.
1247///
1248/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001249static bool cleanPathForOutput(FileManager &FileMgr,
1250 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001251 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001252 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001253}
1254
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001255/// \brief Adjusts the given filename to only write out the portion of the
1256/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001257///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001258/// \param Filename the file name to adjust.
1259///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001260/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1261/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001262///
1263/// \returns either the original filename (if it needs no adjustment) or the
1264/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001265static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001266adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001267 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001268
Richard Smith7ed1bc92014-12-05 22:42:13 +00001269 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001270 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001271
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001272 // Verify that the filename and the system root have the same prefix.
1273 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001274 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1275 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001276 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001277
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001278 // We hit the end of the filename before we hit the end of the system root.
1279 if (!Filename[Pos])
1280 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001281
Richard Smith7ed1bc92014-12-05 22:42:13 +00001282 // If there's not a path separator at the end of the base directory nor
1283 // immediately after it, then this isn't within the base directory.
1284 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1285 if (!llvm::sys::path::is_separator(BaseDir.back()))
1286 return Filename;
1287 } else {
1288 // If the file name has a '/' at the current position, skip over the '/'.
1289 // We distinguish relative paths from absolute paths by the
1290 // absence of '/' at the beginning of relative paths.
1291 //
1292 // FIXME: This is wrong. We distinguish them by asking if the path is
1293 // absolute, which isn't the same thing. And there might be multiple '/'s
1294 // in a row. Use a better mechanism to indicate whether we have emitted an
1295 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001296 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001299 return Filename + Pos;
1300}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001301
Ben Langmuir487ea142014-10-23 18:05:36 +00001302static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001303 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001304 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1305 return S;
1306 // Rely on GetRandomNumber to eventually return non-zero...
1307 }
1308}
1309
Douglas Gregor112b9072012-10-18 05:31:06 +00001310/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001311uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1312 ASTContext &Context,
1313 StringRef isysroot,
1314 const std::string &OutputFile) {
1315 ASTFileSignature Signature = 0;
1316
Douglas Gregorbfbde532009-04-10 21:16:55 +00001317 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001318 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001319 RecordData Record;
1320
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001321 // Metadata
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001322 auto *MetadataAbbrev = new BitCodeAbbrev();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001323 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1324 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1325 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1326 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1327 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1328 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001329 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001330 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1331 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1332 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001333 assert((!WritingModule || isysroot.empty()) &&
1334 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001335 {
1336 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1337 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1338 !isysroot.empty(), IncludeTimestamps,
1339 ASTHasCompilerErrors};
1340 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1341 getClangFullRepositoryVersion());
1342 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001343 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001344 // For implicit modules we output a signature that we can use to ensure
1345 // duplicate module builds don't collide in the cache as their output order
1346 // is non-deterministic.
1347 // FIXME: Remove this when output is deterministic.
1348 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001349 Signature = getSignature();
1350 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001351 Stream.EmitRecord(SIGNATURE, Record);
1352 }
1353
Richard Smith7ed1bc92014-12-05 22:42:13 +00001354 // Module name
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001355 auto *Abbrev = new BitCodeAbbrev();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001356 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1358 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00001359 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001360 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1361 }
1362
Richard Smith7ed1bc92014-12-05 22:42:13 +00001363 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001364 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001365 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001366
1367 // If the home of the module is the current working directory, then we
1368 // want to pick up the cwd of the build process loading the module, not
1369 // our cwd, when we load this module.
1370 if (!PP.getHeaderSearchInfo()
1371 .getHeaderSearchOpts()
1372 .ModuleMapFileHomeIsCwd ||
1373 WritingModule->Directory->getName() != StringRef(".")) {
1374 // Module directory.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001375 auto *Abbrev = new BitCodeAbbrev();
Richard Smithf7b41372015-08-13 23:47:44 +00001376 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1378 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1379
Mehdi Amini57a41912015-09-10 01:46:39 +00001380 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001381 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1382 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001383
1384 // Write out all other paths relative to the base directory if possible.
1385 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1386 } else if (!isysroot.empty()) {
1387 // Write out paths relative to the sysroot if possible.
1388 BaseDirectory = isysroot;
1389 }
1390
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001391 // Module map file
1392 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001393 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001394
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001395 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1396
1397 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001398 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001399
1400 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001401 if (auto *AdditionalModMaps =
1402 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001403 Record.push_back(AdditionalModMaps->size());
1404 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001405 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001406 } else {
1407 Record.push_back(0);
1408 }
1409
1410 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001411 }
1412
Douglas Gregor112b9072012-10-18 05:31:06 +00001413 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001414 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001415 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001416 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001417
Richard Smith7f330cd2015-03-18 01:42:29 +00001418 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001419 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001420 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001421 continue;
1422
Richard Smith7f330cd2015-03-18 01:42:29 +00001423 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1424 AddSourceLocation(M->ImportLoc, Record);
1425 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001426 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001427 Record.push_back(M->Signature);
1428 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001429 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001430 Stream.EmitRecord(IMPORTS, Record);
1431 }
Mike Stump11289f42009-09-09 15:08:12 +00001432
Richard Smith0516b182015-09-08 19:40:14 +00001433 // Write the options block.
1434 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1435
Douglas Gregor112b9072012-10-18 05:31:06 +00001436 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001437 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001438 const LangOptions &LangOpts = Context.getLangOpts();
1439#define LANGOPT(Name, Bits, Default, Description) \
1440 Record.push_back(LangOpts.Name);
1441#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1442 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001443#include "clang/Basic/LangOptions.def"
1444#define SANITIZER(NAME, ID) \
1445 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001446#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001447
Ben Langmuircd98cb72015-06-23 18:20:18 +00001448 Record.push_back(LangOpts.ModuleFeatures.size());
1449 for (StringRef Feature : LangOpts.ModuleFeatures)
1450 AddString(Feature, Record);
1451
Douglas Gregor112b9072012-10-18 05:31:06 +00001452 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1453 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001454
1455 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001456
1457 // Comment options.
1458 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001459 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1460 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001461 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001462 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001463
Samuel Antaoee8fb302016-01-06 13:42:12 +00001464 // OpenMP offloading options.
1465 Record.push_back(LangOpts.OMPTargetTriples.size());
1466 for (auto &T : LangOpts.OMPTargetTriples)
1467 AddString(T.getTriple(), Record);
1468
1469 AddString(LangOpts.OMPHostIRFile, Record);
1470
Douglas Gregor112b9072012-10-18 05:31:06 +00001471 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1472
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001473 // Target options.
1474 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001475 const TargetInfo &Target = Context.getTargetInfo();
1476 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001477 AddString(TargetOpts.Triple, Record);
1478 AddString(TargetOpts.CPU, Record);
1479 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001480 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1481 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1482 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1483 }
1484 Record.push_back(TargetOpts.Features.size());
1485 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1486 AddString(TargetOpts.Features[I], Record);
1487 }
1488 Stream.EmitRecord(TARGET_OPTIONS, Record);
1489
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001490 // Diagnostic options.
1491 Record.clear();
1492 const DiagnosticOptions &DiagOpts
1493 = Context.getDiagnostics().getDiagnosticOptions();
1494#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1495#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1496 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1497#include "clang/Basic/DiagnosticOptions.def"
1498 Record.push_back(DiagOpts.Warnings.size());
1499 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1500 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001501 Record.push_back(DiagOpts.Remarks.size());
1502 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1503 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001504 // Note: we don't serialize the log or serialization file names, because they
1505 // are generally transient files and will almost always be overridden.
1506 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1507
Douglas Gregorc6317db2012-10-24 15:49:58 +00001508 // File system options.
1509 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001510 const FileSystemOptions &FSOpts =
1511 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001512 AddString(FSOpts.WorkingDir, Record);
1513 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1514
Douglas Gregor2d302362012-10-24 16:50:34 +00001515 // Header search options.
1516 Record.clear();
1517 const HeaderSearchOptions &HSOpts
1518 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1519 AddString(HSOpts.Sysroot, Record);
1520
1521 // Include entries.
1522 Record.push_back(HSOpts.UserEntries.size());
1523 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1524 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1525 AddString(Entry.Path, Record);
1526 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001527 Record.push_back(Entry.IsFramework);
1528 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001529 }
1530
1531 // System header prefixes.
1532 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1533 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1534 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1535 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1536 }
1537
1538 AddString(HSOpts.ResourceDir, Record);
1539 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001540 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001541 Record.push_back(HSOpts.DisableModuleHash);
1542 Record.push_back(HSOpts.UseBuiltinIncludes);
1543 Record.push_back(HSOpts.UseStandardSystemIncludes);
1544 Record.push_back(HSOpts.UseStandardCXXIncludes);
1545 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001546 // Write out the specific module cache path that contains the module files.
1547 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001548 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1549
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001550 // Preprocessor options.
1551 Record.clear();
1552 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1553
1554 // Macro definitions.
1555 Record.push_back(PPOpts.Macros.size());
1556 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1557 AddString(PPOpts.Macros[I].first, Record);
1558 Record.push_back(PPOpts.Macros[I].second);
1559 }
1560
1561 // Includes
1562 Record.push_back(PPOpts.Includes.size());
1563 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1564 AddString(PPOpts.Includes[I], Record);
1565
1566 // Macro includes
1567 Record.push_back(PPOpts.MacroIncludes.size());
1568 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1569 AddString(PPOpts.MacroIncludes[I], Record);
1570
Douglas Gregorb6368752012-10-24 23:41:50 +00001571 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001572 // Detailed record is important since it is used for the module cache hash.
1573 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001574 AddString(PPOpts.ImplicitPCHInclude, Record);
1575 AddString(PPOpts.ImplicitPTHInclude, Record);
1576 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1577 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1578
Richard Smith0516b182015-09-08 19:40:14 +00001579 // Leave the options block.
1580 Stream.ExitBlock();
1581
Douglas Gregora3b20262011-05-06 21:43:30 +00001582 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001583 SourceManager &SM = Context.getSourceManager();
1584 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001585 auto *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001586 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1587 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001588 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1589 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1590
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001591 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001592 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001593 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001594 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001595 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001596
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001597 Record.clear();
1598 Record.push_back(SM.getMainFileID().getOpaqueValue());
1599 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1600
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001601 // Original PCH directory
1602 if (!OutputFile.empty() && OutputFile != "-") {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001603 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001604 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1606 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1607
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001608 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001609
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001610 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001611 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1612
Mehdi Amini57a41912015-09-10 01:46:39 +00001613 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001614 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1615 }
1616
Douglas Gregor49491f72013-03-15 22:15:07 +00001617 WriteInputFiles(Context.SourceMgr,
1618 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001619 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001620 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001621 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001622}
1623
Douglas Gregor49491f72013-03-15 22:15:07 +00001624namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001625
Douglas Gregor49491f72013-03-15 22:15:07 +00001626 /// \brief An input file.
1627 struct InputFileEntry {
1628 const FileEntry *File;
1629 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001630 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001631 bool BufferOverridden;
1632 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001633
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001634} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001635
1636void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1637 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001638 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001639 using namespace llvm;
1640 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001641
Douglas Gregor72be3902012-10-19 00:38:02 +00001642 // Create input-file abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001643 auto *IFAbbrev = new BitCodeAbbrev();
Douglas Gregor72be3902012-10-19 00:38:02 +00001644 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001645 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001646 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1647 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001648 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001649 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001650 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1651 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1652
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001653 // Get all ContentCache objects for files, sorted by whether the file is a
1654 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001655 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001656 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1657 // Get this source location entry.
1658 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001659 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001660
1661 // We only care about file entries that were not overridden.
1662 if (!SLoc->isFile())
1663 continue;
1664 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001665 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001666 continue;
1667
Douglas Gregor49491f72013-03-15 22:15:07 +00001668 InputFileEntry Entry;
1669 Entry.File = Cache->OrigEntry;
1670 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001671 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001672 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001673 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001674 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001675 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001676 SortedFiles.push_front(Entry);
1677 }
1678
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001679 unsigned UserFilesNum = 0;
1680 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001681 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001682 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001683 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001684 if (InputFileID != 0)
1685 continue; // already recorded this file.
1686
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001687 // Record this entry's offset.
1688 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001689
1690 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001691
Douglas Gregor49491f72013-03-15 22:15:07 +00001692 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001693 ++UserFilesNum;
1694
Douglas Gregor72be3902012-10-19 00:38:02 +00001695 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001696 // And whether this file was overridden.
1697 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001698 INPUT_FILE,
1699 InputFileOffsets.size(),
1700 (uint64_t)Entry.File->getSize(),
1701 (uint64_t)getTimestampForOutput(Entry.File),
1702 Entry.BufferOverridden,
1703 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001704
Richard Smith7ed1bc92014-12-05 22:42:13 +00001705 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1706 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001707
Douglas Gregor112b9072012-10-18 05:31:06 +00001708 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001709
1710 // Create input file offsets abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001711 auto *OffsetsAbbrev = new BitCodeAbbrev();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001712 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1713 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001714 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1715 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001716 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1717 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1718
1719 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001720 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1721 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001722 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001723}
1724
Douglas Gregora7f71a92009-04-10 03:52:48 +00001725//===----------------------------------------------------------------------===//
1726// Source Manager Serialization
1727//===----------------------------------------------------------------------===//
1728
1729/// \brief Create an abbreviation for the SLocEntry that refers to a
1730/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001731static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001732 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001733
1734 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001735 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001740 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001745 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001746}
1747
1748/// \brief Create an abbreviation for the SLocEntry that refers to a
1749/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001750static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001751 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001752
1753 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001754 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001760 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001761}
1762
1763/// \brief Create an abbreviation for the SLocEntry that refers to a
1764/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001765static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1766 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001767 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001768
1769 auto *Abbrev = new BitCodeAbbrev();
Richard Smithaada85c2016-02-06 02:06:43 +00001770 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1771 : SM_SLOC_BUFFER_BLOB));
1772 if (Compressed)
1773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001775 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001776}
1777
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001778/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1779/// expansion.
1780static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001781 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001782
1783 auto *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001784 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001785 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1786 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001790 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001791}
1792
Douglas Gregor09b69892011-02-10 17:09:37 +00001793namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001794
Douglas Gregor09b69892011-02-10 17:09:37 +00001795 // Trait used for the on-disk hash table of header search information.
1796 class HeaderFileInfoTrait {
1797 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001798 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001799
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001800 // Keep track of the framework names we've used during serialization.
1801 SmallVector<char, 128> FrameworkStringData;
1802 llvm::StringMap<unsigned> FrameworkNameOffset;
1803
Douglas Gregor09b69892011-02-10 17:09:37 +00001804 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001805 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1806 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001807
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001808 struct key_type {
1809 const FileEntry *FE;
1810 const char *Filename;
1811 };
1812 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001813
1814 typedef HeaderFileInfo data_type;
1815 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001816 typedef unsigned hash_value_type;
1817 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001818
Richard Smithe75ee0f2015-08-17 07:13:32 +00001819 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001820 // The hash is based only on size/time of the file, so that the reader can
1821 // match even when symlinking or excess path elements ("foo/../", "../")
1822 // change the form of the name. However, complete path is still the key.
1823 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001824 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001825 }
1826
1827 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001828 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001829 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001830 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001831 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001832 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001833 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001834 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1835 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1836 DataLen += 4;
1837 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001838 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001839 }
1840
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001841 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001842 using namespace llvm::support;
1843 endian::Writer<little> LE(Out);
1844 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001845 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001846 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001847 KeyLen -= 8;
1848 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001849 }
1850
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001851 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001852 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001853 using namespace llvm::support;
1854 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001855 uint64_t Start = Out.tell(); (void)Start;
1856
Richard Smith386bb072015-08-18 23:42:23 +00001857 unsigned char Flags = (Data.isImport << 4)
1858 | (Data.isPragmaOnce << 3)
1859 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001860 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001861 LE.write<uint8_t>(Flags);
1862 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001863
1864 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001865 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001866 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001867 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001868
1869 unsigned Offset = 0;
1870 if (!Data.Framework.empty()) {
1871 // If this header refers into a framework, save the framework name.
1872 llvm::StringMap<unsigned>::iterator Pos
1873 = FrameworkNameOffset.find(Data.Framework);
1874 if (Pos == FrameworkNameOffset.end()) {
1875 Offset = FrameworkStringData.size() + 1;
1876 FrameworkStringData.append(Data.Framework.begin(),
1877 Data.Framework.end());
1878 FrameworkStringData.push_back(0);
1879
1880 FrameworkNameOffset[Data.Framework] = Offset;
1881 } else
1882 Offset = Pos->second;
1883 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001884 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001885
Richard Smith386bb072015-08-18 23:42:23 +00001886 // FIXME: If the header is excluded, we should write out some
1887 // record of that fact.
1888 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1889 if (uint32_t ModID =
1890 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1891 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1892 assert((Value >> 2) == ModID && "overflow in header module info");
1893 LE.write<uint32_t>(Value);
1894 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001895 }
1896
Douglas Gregor09b69892011-02-10 17:09:37 +00001897 assert(Out.tell() - Start == DataLen && "Wrong data length");
1898 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001899
1900 const char *strings_begin() const { return FrameworkStringData.begin(); }
1901 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001902 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001903
Douglas Gregor09b69892011-02-10 17:09:37 +00001904} // end anonymous namespace
1905
1906/// \brief Write the header search block for the list of files that
1907///
1908/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001909void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001910 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001911 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1912
1913 if (FilesByUID.size() > HS.header_file_size())
1914 FilesByUID.resize(HS.header_file_size());
1915
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001916 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001917 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001918 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001919 unsigned NumHeaderSearchEntries = 0;
1920 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1921 const FileEntry *File = FilesByUID[UID];
1922 if (!File)
1923 continue;
1924
Richard Smith386bb072015-08-18 23:42:23 +00001925 // Get the file info. This will load info from the external source if
1926 // necessary. Skip emitting this file if we have no information on it
1927 // as a header file (in which case HFI will be null) or if it hasn't
1928 // changed since it was loaded. Also skip it if it's for a modular header
1929 // from a different module; in that case, we rely on the module(s)
1930 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001931 const HeaderFileInfo *HFI =
1932 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1933 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001934 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001935
Richard Smith7ed1bc92014-12-05 22:42:13 +00001936 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001937 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001938 SmallString<128> FilenameTmp(Filename);
1939 if (PreparePathForOutput(FilenameTmp)) {
1940 // If we performed any translation on the file name at all, we need to
1941 // save this string, since the generator will refer to it later.
1942 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001943 SavedStrings.push_back(Filename);
1944 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001945
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001946 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001947 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001948 ++NumHeaderSearchEntries;
1949 }
1950
1951 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001952 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001953 uint32_t BucketOffset;
1954 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001955 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001956 llvm::raw_svector_ostream Out(TableData);
1957 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001958 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001959 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1960 }
1961
1962 // Create a blob abbreviation
1963 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001964
1965 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor09b69892011-02-10 17:09:37 +00001966 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1971 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1972
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001973 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001974 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1975 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001976 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001977 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001978
1979 // Free all of the strings we had to duplicate.
1980 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001981 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001982}
1983
Douglas Gregora7f71a92009-04-10 03:52:48 +00001984/// \brief Writes the block containing the serialized form of the
1985/// source manager.
1986///
1987/// TODO: We should probably use an on-disk hash table (stored in a
1988/// blob), indexed based on the file name, so that we only create
1989/// entries for files that we actually need. In the common case (no
1990/// errors), we probably won't have to create file entries for any of
1991/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001992void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001993 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001994 RecordData Record;
1995
Chris Lattner0910e3b2009-04-10 17:16:57 +00001996 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00001997 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001998
1999 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002000 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2001 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002002 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2003 unsigned SLocBufferBlobCompressedAbbrv =
2004 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002005 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002006
Douglas Gregor258ae542009-04-27 06:38:32 +00002007 // Write out the source location entry table. We skip the first
2008 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002009 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002010 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002011 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2012 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002013 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002014 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002015 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002016 FileID FID = FileID::get(I);
2017 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002018
Douglas Gregor258ae542009-04-27 06:38:32 +00002019 // Record the offset of this source-location entry.
2020 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2021
2022 // Figure out which record code to use.
2023 unsigned Code;
2024 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002025 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2026 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002027 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002028 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002029 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002030 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002031 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002032 Record.clear();
2033 Record.push_back(Code);
2034
Douglas Gregor925296b2011-07-19 16:10:42 +00002035 // Starting offset of this entry within this module, so skip the dummy.
2036 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002037 if (SLoc->isFile()) {
2038 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002039 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002040 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2041 Record.push_back(File.hasLineDirectives());
2042
2043 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002044 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002045 if (Content->OrigEntry) {
2046 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002047 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002048
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002049 // The source location entry is a file. Emit input file ID.
2050 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2051 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002052
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002053 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002054
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002055 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002056 if (FDI != FileDeclIDs.end()) {
2057 Record.push_back(FDI->second->FirstDeclIndex);
2058 Record.push_back(FDI->second->DeclIDs.size());
2059 } else {
2060 Record.push_back(0);
2061 Record.push_back(0);
2062 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002063
2064 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2065
Richard Smithaada85c2016-02-06 02:06:43 +00002066 if (Content->BufferOverridden || Content->IsTransient)
2067 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002068 } else {
2069 // The source location entry is a buffer. The blob associated
2070 // with this entry contains the contents of the buffer.
2071
2072 // We add one to the size so that we capture the trailing NULL
2073 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2074 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002075 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002076 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002077 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002078 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002079 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002080 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002081
Mehdi Amini99d1b292016-10-01 16:38:28 +00002082 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002083 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002084 }
Richard Smithaada85c2016-02-06 02:06:43 +00002085
2086 if (EmitBlob) {
2087 // Include the implicit terminating null character in the on-disk buffer
2088 // if we're writing it uncompressed.
2089 const llvm::MemoryBuffer *Buffer =
2090 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2091 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2092
2093 // Compress the buffer if possible. We expect that almost all PCM
2094 // consumers will not want its contents.
2095 SmallString<0> CompressedBuffer;
2096 if (llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer) ==
2097 llvm::zlib::StatusOK) {
2098 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2099 Blob.size() - 1};
2100 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2101 CompressedBuffer);
2102 } else {
2103 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
2104 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2105 }
2106 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002107 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002108 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002109 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002110 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2111 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2112 AddSourceLocation(Expansion.isMacroArgExpansion()
2113 ? SourceLocation()
2114 : Expansion.getExpansionLocEnd(),
2115 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002116
2117 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002118 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002119 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002120 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002121 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002122 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002123 }
2124 }
2125
Douglas Gregor8f45df52009-04-16 22:23:12 +00002126 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002127
2128 if (SLocEntryOffsets.empty())
2129 return;
2130
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002131 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002132 // table is used for lazily loading source-location information.
2133 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002134
2135 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002136 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2140 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002141 {
2142 RecordData::value_type Record[] = {
2143 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2144 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2145 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2146 bytes(SLocEntryOffsets));
2147 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002148 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002149 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002150 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002151
2152 // Write the line table. It depends on remapping working, so it must come
2153 // after the source location offsets.
2154 if (SourceMgr.hasLineTable()) {
2155 LineTableInfo &LineTable = SourceMgr.getLineTable();
2156
2157 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002158
2159 // Emit the needed file names.
2160 llvm::DenseMap<int, int> FilenameMap;
2161 for (const auto &L : LineTable) {
2162 if (L.first.ID < 0)
2163 continue;
2164 for (auto &LE : L.second) {
2165 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2166 FilenameMap.size())).second)
2167 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2168 }
2169 }
2170 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002171
2172 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002173 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002174 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002175 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002176 continue;
2177
2178 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002179 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002180
2181 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002182 Record.push_back(L.second.size());
2183 for (const auto &LE : L.second) {
2184 Record.push_back(LE.FileOffset);
2185 Record.push_back(LE.LineNo);
2186 Record.push_back(FilenameMap[LE.FilenameID]);
2187 Record.push_back((unsigned)LE.FileKind);
2188 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002189 }
2190 }
Richard Smith63078492015-09-01 07:41:55 +00002191
Douglas Gregor925296b2011-07-19 16:10:42 +00002192 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2193 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002194}
2195
Douglas Gregorc5046832009-04-27 18:38:38 +00002196//===----------------------------------------------------------------------===//
2197// Preprocessor Serialization
2198//===----------------------------------------------------------------------===//
2199
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002200static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2201 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002202 if (MacroInfo *MI = MD->getMacroInfo())
2203 if (MI->isBuiltinMacro())
2204 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002205
2206 if (IsModule) {
2207 SourceLocation Loc = MD->getLocation();
2208 if (Loc.isInvalid())
2209 return true;
2210 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2211 return true;
2212 }
2213
2214 return false;
2215}
2216
Chris Lattnereeffaef2009-04-10 17:15:23 +00002217/// \brief Writes the block containing the serialized form of the
2218/// preprocessor.
2219///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002220void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002221 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2222 if (PPRec)
2223 WritePreprocessorDetail(*PPRec);
2224
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002225 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002226 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002227
Chris Lattner0af3ba12009-04-13 01:29:17 +00002228 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2229 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002230 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002231 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002232 }
2233
2234 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002235 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002236
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002237 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002238 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002239 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002240 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002241
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002242 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002243 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002244
Richard Smithee977932015-05-01 21:22:17 +00002245 // Construct the list of identifiers with macro directives that need to be
2246 // serialized.
2247 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2248 for (auto &Id : PP.getIdentifierTable())
2249 if (Id.second->hadMacroDefinition() &&
2250 (!Id.second->isFromAST() ||
2251 Id.second->hasChangedSinceDeserialization()))
2252 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002253 // Sort the set of macro definitions that need to be serialized by the
2254 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002255 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2256 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002257
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002258 // Emit the macro directives as a list and associate the offset with the
2259 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002260 for (const IdentifierInfo *Name : MacroIdentifiers) {
2261 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002262 auto StartOffset = Stream.GetCurrentBitNo();
2263
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002264 // Emit the macro directives in reverse source order.
2265 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002266 // Once we hit an ignored macro, we're done: the rest of the chain
2267 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002268 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002269 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002270
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002271 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002272 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002273 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002274 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002275 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002276 Record.push_back(VisMD->isPublic());
2277 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002278 }
Richard Smithd7329392015-04-21 21:46:32 +00002279
Richard Smithb8b2ed62015-04-23 18:18:26 +00002280 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002281 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002282 // We write out exported module macros for PCH as well.
2283 auto Leafs = PP.getLeafModuleMacros(Name);
2284 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2285 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2286 while (!Worklist.empty()) {
2287 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002288
Manman Ren33d80292016-05-31 18:19:32 +00002289 // Emit a record indicating this submodule exports this macro.
2290 ModuleMacroRecord.push_back(
2291 getSubmoduleID(Macro->getOwningModule()));
2292 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2293 for (auto *M : Macro->overrides())
2294 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002295
Manman Ren33d80292016-05-31 18:19:32 +00002296 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2297 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002298
Manman Ren33d80292016-05-31 18:19:32 +00002299 // Enqueue overridden macros once we've visited all their ancestors.
2300 for (auto *M : Macro->overrides())
2301 if (++Visits[M] == M->getNumOverridingMacros())
2302 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002303
Manman Ren33d80292016-05-31 18:19:32 +00002304 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002305 }
Richard Smithd7329392015-04-21 21:46:32 +00002306
Richard Smithee977932015-05-01 21:22:17 +00002307 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002308 continue;
2309
Richard Smithd7329392015-04-21 21:46:32 +00002310 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002311 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2312 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002313 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002314
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002315 /// \brief Offsets of each of the macros into the bitstream, indexed by
2316 /// the local macro ID
2317 ///
2318 /// For each identifier that is associated with a macro, this map
2319 /// provides the offset into the bitstream where that macro is
2320 /// defined.
2321 std::vector<uint32_t> MacroOffsets;
2322
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002323 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2324 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2325 MacroInfo *MI = MacroInfosToEmit[I].MI;
2326 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002327
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002328 if (ID < FirstMacroID) {
2329 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2330 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002331 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002332
2333 // Record the local offset of this macro.
2334 unsigned Index = ID - FirstMacroID;
2335 if (Index == MacroOffsets.size())
2336 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2337 else {
2338 if (Index > MacroOffsets.size())
2339 MacroOffsets.resize(Index + 1);
2340
2341 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2342 }
2343
2344 AddIdentifierRef(Name, Record);
2345 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2346 AddSourceLocation(MI->getDefinitionLoc(), Record);
2347 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2348 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002349 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002350 unsigned Code;
2351 if (MI->isObjectLike()) {
2352 Code = PP_MACRO_OBJECT_LIKE;
2353 } else {
2354 Code = PP_MACRO_FUNCTION_LIKE;
2355
2356 Record.push_back(MI->isC99Varargs());
2357 Record.push_back(MI->isGNUVarargs());
2358 Record.push_back(MI->hasCommaPasting());
2359 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002360 for (const IdentifierInfo *Arg : MI->args())
2361 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002362 }
2363
2364 // If we have a detailed preprocessing record, record the macro definition
2365 // ID that corresponds to this macro.
2366 if (PPRec)
2367 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2368
2369 Stream.EmitRecord(Code, Record);
2370 Record.clear();
2371
2372 // Emit the tokens array.
2373 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2374 // Note that we know that the preprocessor does not have any annotation
2375 // tokens in it because they are created by the parser, and thus can't
2376 // be in a macro definition.
2377 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002378 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002379 Stream.EmitRecord(PP_TOKEN, Record);
2380 Record.clear();
2381 }
2382 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002383 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002384
Douglas Gregor92a96f52011-02-08 21:58:10 +00002385 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002386
2387 // Write the offsets table for macro IDs.
2388 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002389
Richard Smith13090a22015-04-10 02:02:24 +00002390 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002391 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2395
2396 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002397 {
2398 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2399 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2400 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2401 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002402}
2403
2404void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002405 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002406 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002407
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002408 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002409
Douglas Gregor92a96f52011-02-08 21:58:10 +00002410 // Enter the preprocessor block.
2411 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002412
Douglas Gregoraae92242010-03-19 21:51:54 +00002413 // If the preprocessor has a preprocessing record, emit it.
2414 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002415 using namespace llvm;
2416
2417 // Set up the abbreviation for
2418 unsigned InclusionAbbrev = 0;
2419 {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002420 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002421 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2427 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2428 }
2429
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002430 unsigned FirstPreprocessorEntityID
2431 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2432 + NUM_PREDEF_PP_ENTITY_IDS;
2433 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002434 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002435 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2436 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002437 E != EEnd;
2438 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002439 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002440
Richard Smith66a81862015-05-04 02:25:31 +00002441 PreprocessedEntityOffsets.push_back(
2442 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002443
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002444 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002445 // Record this macro definition's ID.
2446 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002447
Douglas Gregor92a96f52011-02-08 21:58:10 +00002448 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002449 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2450 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002451 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002452
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002453 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002454 Record.push_back(ME->isBuiltinMacro());
2455 if (ME->isBuiltinMacro())
2456 AddIdentifierRef(ME->getName(), Record);
2457 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002458 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002459 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002460 continue;
2461 }
2462
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002463 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002464 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002465 Record.push_back(ID->getFileName().size());
2466 Record.push_back(ID->wasInQuotes());
2467 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002468 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002469 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002470 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002471 // Check that the FileEntry is not null because it was not resolved and
2472 // we create a PCH even with compiler errors.
2473 if (ID->getFile())
2474 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002475 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002476 continue;
2477 }
2478
2479 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2480 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002481 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002482
Douglas Gregoraae92242010-03-19 21:51:54 +00002483 // Write the offsets table for the preprocessing record.
2484 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002485 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2486
Douglas Gregoraae92242010-03-19 21:51:54 +00002487 // Write the offsets table for identifier IDs.
2488 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002489
2490 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002491 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002493 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002494 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002495
Mehdi Amini57a41912015-09-10 01:46:39 +00002496 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2497 FirstPreprocessorEntityID -
2498 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002499 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002500 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002501 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002502}
2503
Richard Smith386bb072015-08-18 23:42:23 +00002504unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002505 if (!Mod)
2506 return 0;
2507
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002508 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2509 if (Known != SubmoduleIDs.end())
2510 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002511
Richard Smithdc1f0422016-07-20 19:10:16 +00002512 auto *Top = Mod->getTopLevelModule();
2513 if (Top != WritingModule &&
2514 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule)))
Richard Smith386bb072015-08-18 23:42:23 +00002515 return 0;
2516
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002517 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2518}
2519
Richard Smith386bb072015-08-18 23:42:23 +00002520unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2521 // FIXME: This can easily happen, if we have a reference to a submodule that
2522 // did not result in us loading a module file for that submodule. For
2523 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2524 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2525 assert((ID || !Mod) &&
2526 "asked for module ID for non-local, non-imported module");
2527 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002528}
2529
Douglas Gregor253eefe2011-12-01 00:59:36 +00002530/// \brief Compute the number of modules within the given tree (including the
2531/// given module).
2532static unsigned getNumberOfModules(Module *Mod) {
2533 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002534 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002535 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002536 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002537
2538 return ChildModules + 1;
2539}
2540
Douglas Gregorde3ef502011-11-30 23:21:26 +00002541void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002542 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002543 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002544
2545 // Write the abbreviations needed for the submodules block.
2546 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002547
2548 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor69021972011-11-30 17:33:56 +00002549 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2561 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2562
2563 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002564 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2566 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2567
2568 Abbrev = new BitCodeAbbrev();
2569 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2571 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002572
2573 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002574 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2576 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2577
2578 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002579 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2581 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2582
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002583 Abbrev = new BitCodeAbbrev();
2584 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002587 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2588
Douglas Gregor59527662012-10-15 06:28:11 +00002589 Abbrev = new BitCodeAbbrev();
2590 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2592 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2593
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002594 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002595 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2596 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2597 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2598
2599 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002600 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2602 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2603
2604 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002605 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2607 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2608
2609 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002610 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2613 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2614
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002615 Abbrev = new BitCodeAbbrev();
2616 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2618 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2619
Douglas Gregorfb912652013-03-20 21:10:35 +00002620 Abbrev = new BitCodeAbbrev();
2621 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2624 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2625
Douglas Gregor253eefe2011-12-01 00:59:36 +00002626 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002627 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2628 FirstSubmoduleID -
2629 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002630 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2631
Douglas Gregor69021972011-11-30 17:33:56 +00002632 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002633 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002634 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002635 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002636 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002637 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002638 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002639
2640 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002641 if (Mod->Parent) {
2642 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002643 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002644 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002645
2646 // Emit the definition of the block.
2647 {
2648 RecordData::value_type Record[] = {
2649 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2650 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2651 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2652 Mod->ConfigMacrosExhaustive};
2653 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2654 }
2655
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002656 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002657 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002658 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002659 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002660 }
2661
Douglas Gregor69021972011-11-30 17:33:56 +00002662 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002663 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002664 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002665 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2666 UmbrellaHeader.NameAsWritten);
2667 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002668 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2669 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002670 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002671 }
Richard Smith306d8922014-10-22 23:50:56 +00002672
Douglas Gregor69021972011-11-30 17:33:56 +00002673 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002674 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002675 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002676 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002677 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002678 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002679 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2680 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2681 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002682 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002683 Module::HK_PrivateTextual},
2684 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002685 };
2686 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002687 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002688 for (auto &H : Mod->Headers[HL.HeaderKind])
2689 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2690 }
2691
2692 // Emit the top headers.
2693 {
2694 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002695 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002696 for (auto *H : TopHeaders)
2697 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002698 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002699
2700 // Emit the imports.
2701 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002702 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002703 for (auto *I : Mod->Imports)
2704 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002705 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2706 }
2707
Douglas Gregor24bb9232011-12-02 18:58:38 +00002708 // Emit the exports.
2709 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002710 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002711 for (const auto &E : Mod->Exports) {
2712 // FIXME: This may fail; we don't require that all exported modules
2713 // are local or imported.
2714 Record.push_back(getSubmoduleID(E.getPointer()));
2715 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002716 }
2717 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2718 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002719
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002720 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2721 // Might be unnecessary as use declarations are only used to build the
2722 // module itself.
2723
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002724 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002725 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002726 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2727 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002728 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002729 }
2730
Douglas Gregorfb912652013-03-20 21:10:35 +00002731 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002732 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002733 // FIXME: This may fail; we don't require that all conflicting modules
2734 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002735 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2736 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002737 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002738 }
2739
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002740 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002741 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002742 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002743 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002744 }
2745
Richard Smithdc1f0422016-07-20 19:10:16 +00002746 // Emit the initializers, if any.
2747 RecordData Inits;
2748 for (Decl *D : Context->getModuleInitializers(Mod))
2749 Inits.push_back(GetDeclRef(D));
2750 if (!Inits.empty())
2751 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2752
Douglas Gregor69021972011-11-30 17:33:56 +00002753 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002754 for (auto *M : Mod->submodules())
2755 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002756 }
2757
2758 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002759
Richard Smith23d8d032015-05-14 00:45:20 +00002760 assert((NextSubmoduleID - FirstSubmoduleID ==
2761 getNumberOfModules(WritingModule)) &&
2762 "Wrong # of submodules; found a reference to a non-local, "
2763 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002764}
2765
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002766serialization::SubmoduleID
2767ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002768 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002769 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002770
2771 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002772 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002773 Module *OwningMod
2774 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002775 if (!OwningMod)
2776 return 0;
2777
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002778 // Check whether this submodule is part of our own module.
2779 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002780 return 0;
2781
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002782 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002783}
2784
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002785void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2786 bool isModule) {
2787 // Make sure set diagnostic pragmas don't affect the translation unit that
2788 // imports the module.
2789 // FIXME: Make diagnostic pragma sections work properly with modules.
2790 if (isModule)
2791 return;
2792
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002793 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2794 DiagStateIDMap;
2795 unsigned CurrID = 0;
2796 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002797 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002798 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002799 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2800 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002801 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002802 if (point.Loc.isInvalid())
2803 continue;
2804
Richard Smithb22a1d12016-03-27 20:13:24 +00002805 AddSourceLocation(point.Loc, Record);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002806 unsigned &DiagStateID = DiagStateIDMap[point.State];
2807 Record.push_back(DiagStateID);
2808
2809 if (DiagStateID == 0) {
2810 DiagStateID = ++CurrID;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002811 for (const auto &I : *(point.State)) {
2812 if (I.second.isPragma()) {
2813 Record.push_back(I.first);
2814 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002815 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002816 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002817 Record.push_back(-1); // mark the end of the diag/map pairs for this
2818 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002819 }
2820 }
2821
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002822 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002823 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002824}
2825
Douglas Gregorc5046832009-04-27 18:38:38 +00002826//===----------------------------------------------------------------------===//
2827// Type Serialization
2828//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002829
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002830/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002831void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002832 TypeIdx &IdxRef = TypeIdxs[T];
2833 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2834 IdxRef = TypeIdx(NextTypeID++);
2835 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002836
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002837 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002838
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002839 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002840
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002841 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002842 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002843 W.Visit(T);
2844 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002845
Richard Smith290d8012016-04-06 17:06:00 +00002846 // Record the offset for this type.
2847 unsigned Index = Idx.getIndex() - FirstTypeID;
2848 if (TypeOffsets.size() == Index)
2849 TypeOffsets.push_back(Offset);
2850 else if (TypeOffsets.size() < Index) {
2851 TypeOffsets.resize(Index + 1);
2852 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002853 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002854 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002855 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002856}
2857
Douglas Gregorc5046832009-04-27 18:38:38 +00002858//===----------------------------------------------------------------------===//
2859// Declaration Serialization
2860//===----------------------------------------------------------------------===//
2861
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002862/// \brief Write the block containing all of the declaration IDs
2863/// lexically declared within the given DeclContext.
2864///
2865/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2866/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002867uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002868 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002869 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002870 return 0;
2871
Douglas Gregor8f45df52009-04-16 22:23:12 +00002872 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002873 SmallVector<uint32_t, 128> KindDeclPairs;
2874 for (const auto *D : DC->decls()) {
2875 KindDeclPairs.push_back(D->getKind());
2876 KindDeclPairs.push_back(GetDeclRef(D));
2877 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002878
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002879 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002880 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002881 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2882 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002883 return Offset;
2884}
2885
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002886void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002887 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002888
2889 // Write the type offsets array
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002890 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002891 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002892 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2895 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002896 {
2897 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2898 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2899 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2900 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002901
2902 // Write the declaration offsets array
2903 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002904 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2908 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002909 {
2910 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2911 FirstDeclID - NUM_PREDEF_DECL_IDS};
2912 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2913 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002914}
2915
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002916void ASTWriter::WriteFileDeclIDsMap() {
2917 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002918
Chandler Carruthb306d732015-03-27 00:31:20 +00002919 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2920 FileDeclIDs.begin(), FileDeclIDs.end());
2921 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2922 llvm::less_first());
2923
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002924 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002925 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2926 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2927 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2928 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2929 for (auto &LocDeclEntry : Info.DeclIDs)
2930 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002931 }
2932
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002933 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002934 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2937 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002938 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2939 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002940 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002941}
2942
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002943void ASTWriter::WriteComments() {
2944 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002945 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002946 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002947 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002948 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002949 AddSourceRange(I->getSourceRange(), Record);
2950 Record.push_back(I->getKind());
2951 Record.push_back(I->isTrailingComment());
2952 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002953 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2954 }
2955 Stream.ExitBlock();
2956}
2957
Douglas Gregorc5046832009-04-27 18:38:38 +00002958//===----------------------------------------------------------------------===//
2959// Global Method Pool and Selector Serialization
2960//===----------------------------------------------------------------------===//
2961
Douglas Gregore84a9da2009-04-20 20:36:09 +00002962namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002963
Douglas Gregorc78d3462009-04-24 21:10:55 +00002964// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002965class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002966 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002967
2968public:
2969 typedef Selector key_type;
2970 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002971
Sebastian Redl834bb972010-08-04 17:20:04 +00002972 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002973 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002974 ObjCMethodList Instance, Factory;
2975 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002976 typedef const data_type& data_type_ref;
2977
Justin Bogner25463f12014-04-18 20:27:24 +00002978 typedef unsigned hash_value_type;
2979 typedef unsigned offset_type;
2980
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002981 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002982
Justin Bogner25463f12014-04-18 20:27:24 +00002983 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002984 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002985 }
Mike Stump11289f42009-09-09 15:08:12 +00002986
2987 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002988 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002989 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002990 using namespace llvm::support;
2991 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002992 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002993 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002994 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2995 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002996 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002997 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002998 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002999 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003000 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003001 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003002 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003003 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003004 return std::make_pair(KeyLen, DataLen);
3005 }
Mike Stump11289f42009-09-09 15:08:12 +00003006
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003007 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003008 using namespace llvm::support;
3009 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003010 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003011 assert((Start >> 32) == 0 && "Selector key offset too large");
3012 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003013 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003014 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003015 if (N == 0)
3016 N = 1;
3017 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003018 LE.write<uint32_t>(
3019 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003020 }
Mike Stump11289f42009-09-09 15:08:12 +00003021
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003022 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003023 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003024 using namespace llvm::support;
3025 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003026 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003027 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003028 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003029 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003030 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003031 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003032 ++NumInstanceMethods;
3033
3034 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003035 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003036 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003037 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003038 ++NumFactoryMethods;
3039
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003040 unsigned InstanceBits = Methods.Instance.getBits();
3041 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003042 unsigned InstanceHasMoreThanOneDeclBit =
3043 Methods.Instance.hasMoreThanOneDecl();
3044 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3045 (InstanceHasMoreThanOneDeclBit << 2) |
3046 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003047 unsigned FactoryBits = Methods.Factory.getBits();
3048 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003049 unsigned FactoryHasMoreThanOneDeclBit =
3050 Methods.Factory.hasMoreThanOneDecl();
3051 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3052 (FactoryHasMoreThanOneDeclBit << 2) |
3053 FactoryBits;
3054 LE.write<uint16_t>(FullInstanceBits);
3055 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003056 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003057 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003058 if (Method->getMethod())
3059 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003060 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003061 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003062 if (Method->getMethod())
3063 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003064
3065 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003066 }
3067};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003068
Douglas Gregorc78d3462009-04-24 21:10:55 +00003069} // end anonymous namespace
3070
Sebastian Redla19a67f2010-08-03 21:58:15 +00003071/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003072///
3073/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003074/// in an on-disk hash table indexed by the selector. The hash table also
3075/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003076void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003077 using namespace llvm;
3078
Sebastian Redla19a67f2010-08-03 21:58:15 +00003079 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003080 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003081 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003082 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003083 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003084 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003085 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003086 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003087
Sebastian Redla19a67f2010-08-03 21:58:15 +00003088 // Create the on-disk hash table representation. We walk through every
3089 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003090 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003091 for (auto &SelectorAndID : SelectorIDs) {
3092 Selector S = SelectorAndID.first;
3093 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003094 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003095 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003096 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003097 ObjCMethodList(),
3098 ObjCMethodList()
3099 };
3100 if (F != SemaRef.MethodPool.end()) {
3101 Data.Instance = F->second.first;
3102 Data.Factory = F->second.second;
3103 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003104 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003105 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003106 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003107 // Selector already exists. Did it change?
3108 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003109 for (ObjCMethodList *M = &Data.Instance;
3110 !changed && M && M->getMethod(); M = M->getNext()) {
3111 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003112 changed = true;
3113 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003114 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003115 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003116 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003117 changed = true;
3118 }
3119 if (!changed)
3120 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003121 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003122 // A new method pool entry.
3123 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003124 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003125 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003126 }
3127
Douglas Gregorc78d3462009-04-24 21:10:55 +00003128 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003129 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003130 uint32_t BucketOffset;
3131 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003132 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003133 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003134 llvm::raw_svector_ostream Out(MethodPool);
3135 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003136 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003137 BucketOffset = Generator.Emit(Out, Trait);
3138 }
3139
3140 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003141 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003142 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3146 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3147
Douglas Gregor95c13f52009-04-25 17:48:32 +00003148 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003149 {
3150 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3151 NumTableEntries};
3152 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3153 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003154
3155 // Create a blob abbreviation for the selector table offsets.
3156 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003157 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3161 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3162
3163 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003164 {
3165 RecordData::value_type Record[] = {
3166 SELECTOR_OFFSETS, SelectorOffsets.size(),
3167 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3168 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3169 bytes(SelectorOffsets));
3170 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003171 }
3172}
3173
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003174/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003175void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003176 using namespace llvm;
3177 if (SemaRef.ReferencedSelectors.empty())
3178 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003179
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003180 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003181 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003182
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003183 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003184 // very tricky to fix, and given that @selector shouldn't really appear in
3185 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003186 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3187 Selector Sel = SelectorAndLocation.first;
3188 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003189 Writer.AddSelectorRef(Sel);
3190 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003191 }
Richard Smithe64e7452016-04-18 21:54:58 +00003192 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003193}
3194
Douglas Gregorc5046832009-04-27 18:38:38 +00003195//===----------------------------------------------------------------------===//
3196// Identifier Table Serialization
3197//===----------------------------------------------------------------------===//
3198
Richard Smithb3761912015-02-05 23:08:52 +00003199/// Determine the declaration that should be put into the name lookup table to
3200/// represent the given declaration in this module. This is usually D itself,
3201/// but if D was imported and merged into a local declaration, we want the most
3202/// recent local declaration instead. The chosen declaration will be the most
3203/// recent declaration in any module that imports this one.
3204static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3205 NamedDecl *D) {
3206 if (!LangOpts.Modules || !D->isFromASTFile())
3207 return D;
3208
3209 if (Decl *Redecl = D->getPreviousDecl()) {
3210 // For Redeclarable decls, a prior declaration might be local.
3211 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003212 // If we find a local decl, we're done.
3213 if (!Redecl->isFromASTFile()) {
3214 // Exception: in very rare cases (for injected-class-names), not all
3215 // redeclarations are in the same semantic context. Skip ones in a
3216 // different context. They don't go in this lookup table at all.
3217 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3218 D->getDeclContext()->getRedeclContext()))
3219 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003220 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003221 }
3222
Richard Smithfe620d22015-03-05 23:24:12 +00003223 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003224 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003225 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003226 break;
3227 }
3228 } else if (Decl *First = D->getCanonicalDecl()) {
3229 // For Mergeable decls, the first decl might be local.
3230 if (!First->isFromASTFile())
3231 return cast<NamedDecl>(First);
3232 }
3233
3234 // All declarations are imported. Our most recent declaration will also be
3235 // the most recent one in anyone who imports us.
3236 return D;
3237}
3238
Douglas Gregorc78d3462009-04-24 21:10:55 +00003239namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003240
Richard Smithcf97cf62015-04-19 01:34:23 +00003241class ASTIdentifierTableTrait {
3242 ASTWriter &Writer;
3243 Preprocessor &PP;
3244 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003245 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003246 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003247 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003248
3249 /// \brief Determines whether this is an "interesting" identifier that needs a
3250 /// full IdentifierInfo structure written into the hash table. Notably, this
3251 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3252 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003253 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003254 if (MacroOffset ||
3255 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003256 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003257 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003258 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003259 return true;
3260
3261 return false;
3262 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003263
Douglas Gregore84a9da2009-04-20 20:36:09 +00003264public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003265 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003266 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003267
Sebastian Redl539c5062010-08-18 23:57:32 +00003268 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003269 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003270
Justin Bogner25463f12014-04-18 20:27:24 +00003271 typedef unsigned hash_value_type;
3272 typedef unsigned offset_type;
3273
Richard Smithd7329392015-04-21 21:46:32 +00003274 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003275 IdentifierResolver &IdResolver, bool IsModule,
3276 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003277 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003278 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3279 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003280
Richard Smithd79514e2016-02-05 19:03:40 +00003281 bool needDecls() const { return NeedDecls; }
3282
Justin Bogner25463f12014-04-18 20:27:24 +00003283 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003284 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003285 }
Mike Stump11289f42009-09-09 15:08:12 +00003286
Richard Smith33e0f7e2015-07-22 02:08:40 +00003287 bool isInterestingIdentifier(const IdentifierInfo *II) {
3288 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3289 return isInterestingIdentifier(II, MacroOffset);
3290 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003291
Richard Smith9c254182015-07-19 21:41:12 +00003292 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3293 return isInterestingIdentifier(II, 0);
3294 }
3295
Mike Stump11289f42009-09-09 15:08:12 +00003296 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003297 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003298 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003299 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003300 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3301 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003302 DataLen += 2; // 2 bytes for builtin ID
3303 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003304 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003305 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003306
Richard Smitha534a312015-07-21 23:54:07 +00003307 if (NeedDecls) {
3308 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3309 DEnd = IdResolver.end();
3310 D != DEnd; ++D)
3311 DataLen += 4;
3312 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003313 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003314 using namespace llvm::support;
3315 endian::Writer<little> LE(Out);
3316
Richard Smithdf8a8312015-03-13 04:05:01 +00003317 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003318 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003319 // We emit the key length after the data length so that every
3320 // string is preceded by a 16-bit length. This matches the PTH
3321 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003322 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003323 return std::make_pair(KeyLen, DataLen);
3324 }
Mike Stump11289f42009-09-09 15:08:12 +00003325
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003326 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003327 unsigned KeyLen) {
3328 // Record the location of the key data. This is used when generating
3329 // the mapping from persistent IDs to strings.
3330 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003331
3332 // Emit the offset of the key/data length information to the interesting
3333 // identifiers table if necessary.
3334 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3335 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3336
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003337 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003338 }
Mike Stump11289f42009-09-09 15:08:12 +00003339
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003340 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003341 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003342 using namespace llvm::support;
3343 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003344
Richard Smithd7329392015-04-21 21:46:32 +00003345 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3346 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003347 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003348 return;
3349 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003350
Justin Bognere1c147c2014-03-28 22:03:19 +00003351 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003352 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3353 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003354 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003355 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003356 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003357 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003358 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3359 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003360 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003361 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003362 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003363 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003364
Richard Smithd7329392015-04-21 21:46:32 +00003365 if (HadMacroDefinition)
3366 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003367
Richard Smitha534a312015-07-21 23:54:07 +00003368 if (NeedDecls) {
3369 // Emit the declaration IDs in reverse order, because the
3370 // IdentifierResolver provides the declarations as they would be
3371 // visible (e.g., the function "stat" would come before the struct
3372 // "stat"), but the ASTReader adds declarations to the end of the list
3373 // (so we need to see the struct "stat" before the function "stat").
3374 // Only emit declarations that aren't from a chained PCH, though.
3375 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3376 IdResolver.end());
3377 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3378 DEnd = Decls.rend();
3379 D != DEnd; ++D)
3380 LE.write<uint32_t>(
3381 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3382 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003383 }
3384};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003385
Douglas Gregore84a9da2009-04-20 20:36:09 +00003386} // end anonymous namespace
3387
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003388/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003389///
3390/// The identifier table consists of a blob containing string data
3391/// (the actual identifiers themselves) and a separate "offsets" index
3392/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003393void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3394 IdentifierResolver &IdResolver,
3395 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003396 using namespace llvm;
3397
Richard Smith33e0f7e2015-07-22 02:08:40 +00003398 RecordData InterestingIdents;
3399
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003400 // Create and write out the blob that contains the identifier
3401 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003402 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003403 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003404 ASTIdentifierTableTrait Trait(
3405 *this, PP, IdResolver, IsModule,
3406 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003407
Douglas Gregore6648fb2009-04-28 20:33:11 +00003408 // Look for any identifiers that were named while processing the
3409 // headers, but are otherwise not needed. We add these to the hash
3410 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003411 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003412 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003413 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003414 for (const auto &ID : PP.getIdentifierTable())
3415 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003416 // Sort the identifiers lexicographically before getting them references so
3417 // that their order is stable.
3418 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3419 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003420 if (Trait.isInterestingNonMacroIdentifier(II))
3421 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003422
Sebastian Redlff4a2952010-07-23 23:49:55 +00003423 // Create the on-disk hash table representation. We only store offsets
3424 // for identifiers that appear here for the first time.
3425 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003426 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003427 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003428 IdentID ID = IdentIDPair.second;
3429 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003430 // Write out identifiers if either the ID is local or the identifier has
3431 // changed since it was loaded.
3432 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3433 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003434 (Trait.needDecls() &&
3435 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003436 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003437 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003438
Douglas Gregore84a9da2009-04-20 20:36:09 +00003439 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003440 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003441 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003442 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003443 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003444 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003445 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003446 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003447 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003448 }
3449
3450 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003451 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003452 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003455 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003456
3457 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003458 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003459 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003460 }
3461
3462 // Write the offsets table for identifier IDs.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003463 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003464 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003466 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003467 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3468 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3469
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003470#ifndef NDEBUG
3471 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3472 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3473#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003474
3475 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3476 IdentifierOffsets.size(),
3477 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003478 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003479 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003480
3481 // In C++, write the list of interesting identifiers (those that are
3482 // defined as macros, poisoned, or similar unusual things).
3483 if (!InterestingIdents.empty())
3484 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003485}
3486
Douglas Gregorc5046832009-04-27 18:38:38 +00003487//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003488// DeclContext's Name Lookup Table Serialization
3489//===----------------------------------------------------------------------===//
3490
3491namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003492
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003493// Trait used for the on-disk hash table used in the method pool.
3494class ASTDeclContextNameLookupTrait {
3495 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003496 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003497
3498public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003499 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003500 typedef key_type key_type_ref;
3501
Richard Smithd88a7f12015-09-01 20:35:42 +00003502 /// A start and end index into DeclIDs, representing a sequence of decls.
3503 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003504 typedef const data_type& data_type_ref;
3505
Justin Bogner25463f12014-04-18 20:27:24 +00003506 typedef unsigned hash_value_type;
3507 typedef unsigned offset_type;
3508
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003509 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3510
Richard Smithd88a7f12015-09-01 20:35:42 +00003511 template<typename Coll>
3512 data_type getData(const Coll &Decls) {
3513 unsigned Start = DeclIDs.size();
3514 for (NamedDecl *D : Decls) {
3515 DeclIDs.push_back(
3516 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3517 }
3518 return std::make_pair(Start, DeclIDs.size());
3519 }
3520
3521 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3522 unsigned Start = DeclIDs.size();
3523 for (auto ID : FromReader)
3524 DeclIDs.push_back(ID);
3525 return std::make_pair(Start, DeclIDs.size());
3526 }
3527
3528 static bool EqualKey(key_type_ref a, key_type_ref b) {
3529 return a == b;
3530 }
3531
Richard Smitha06c7e62015-08-26 23:55:49 +00003532 hash_value_type ComputeHash(DeclarationNameKey Name) {
3533 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003534 }
3535
Richard Smithd88a7f12015-09-01 20:35:42 +00003536 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3537 assert(Writer.hasChain() &&
3538 "have reference to loaded module file but no chain?");
3539
3540 using namespace llvm::support;
3541 endian::Writer<little>(Out)
3542 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3543 }
3544
Richard Smitha06c7e62015-08-26 23:55:49 +00003545 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3546 DeclarationNameKey Name,
3547 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003548 using namespace llvm::support;
3549 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003550 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003551 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003552 case DeclarationName::Identifier:
3553 case DeclarationName::ObjCZeroArgSelector:
3554 case DeclarationName::ObjCOneArgSelector:
3555 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003556 case DeclarationName::CXXLiteralOperatorName:
3557 KeyLen += 4;
3558 break;
3559 case DeclarationName::CXXOperatorName:
3560 KeyLen += 1;
3561 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003562 case DeclarationName::CXXConstructorName:
3563 case DeclarationName::CXXDestructorName:
3564 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003565 case DeclarationName::CXXUsingDirective:
3566 break;
3567 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003568 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003569
Richard Smithf02662d2015-07-30 03:17:16 +00003570 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003571 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3572 assert(uint16_t(DataLen) == DataLen &&
3573 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003574 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003575
3576 return std::make_pair(KeyLen, DataLen);
3577 }
3578
Richard Smitha06c7e62015-08-26 23:55:49 +00003579 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003580 using namespace llvm::support;
3581 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003582 LE.write<uint8_t>(Name.getKind());
3583 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003584 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003585 case DeclarationName::CXXLiteralOperatorName:
3586 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003587 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003588 case DeclarationName::ObjCZeroArgSelector:
3589 case DeclarationName::ObjCOneArgSelector:
3590 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003591 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003592 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003594 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003595 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003596 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003597 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003598 case DeclarationName::CXXConstructorName:
3599 case DeclarationName::CXXDestructorName:
3600 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003601 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003602 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003603 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003604
3605 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003606 }
3607
Richard Smitha06c7e62015-08-26 23:55:49 +00003608 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3609 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003610 using namespace llvm::support;
3611 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003612 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003613 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3614 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003615 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3616 }
3617};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003618
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003619} // end anonymous namespace
3620
Chandler Carruthe972c362015-03-26 03:11:40 +00003621bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3622 DeclContext *DC) {
3623 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3624}
3625
3626bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3627 DeclContext *DC) {
3628 for (auto *D : Result.getLookupResult())
3629 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3630 return false;
3631
3632 return true;
3633}
3634
Richard Smithd88a7f12015-09-01 20:35:42 +00003635void
Chandler Carruthe972c362015-03-26 03:11:40 +00003636ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003637 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003638 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3639 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003640 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003641
Chandler Carruthe972c362015-03-26 03:11:40 +00003642 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003643 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003644 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3645
3646 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003647 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3648 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003649 ASTDeclContextNameLookupTrait Trait(*this);
3650
Chandler Carruthe972c362015-03-26 03:11:40 +00003651 // The first step is to collect the declaration names which we need to
3652 // serialize into the name lookup table, and to collect them in a stable
3653 // order.
3654 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003655
Chandler Carruthe972c362015-03-26 03:11:40 +00003656 // We also build up small sets of the constructor and conversion function
3657 // names which are visible.
3658 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003659
Chandler Carruthe972c362015-03-26 03:11:40 +00003660 for (auto &Lookup : *DC->buildLookup()) {
3661 auto &Name = Lookup.first;
3662 auto &Result = Lookup.second;
3663
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003664 // If there are no local declarations in our lookup result, we
3665 // don't need to write an entry for the name at all. If we can't
3666 // write out a lookup set without performing more deserialization,
3667 // just skip this entry.
3668 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003669 isLookupResultEntirelyExternal(Result, DC))
3670 continue;
3671
3672 // We also skip empty results. If any of the results could be external and
3673 // the currently available results are empty, then all of the results are
3674 // external and we skip it above. So the only way we get here with an empty
3675 // results is when no results could have been external *and* we have
3676 // external results.
3677 //
3678 // FIXME: While we might want to start emitting on-disk entries for negative
3679 // lookups into a decl context as an optimization, today we *have* to skip
3680 // them because there are names with empty lookup results in decl contexts
3681 // which we can't emit in any stable ordering: we lookup constructors and
3682 // conversion functions in the enclosing namespace scope creating empty
3683 // results for them. This in almost certainly a bug in Clang's name lookup,
3684 // but that is likely to be hard or impossible to fix and so we tolerate it
3685 // here by omitting lookups with empty results.
3686 if (Lookup.second.getLookupResult().empty())
3687 continue;
3688
3689 switch (Lookup.first.getNameKind()) {
3690 default:
3691 Names.push_back(Lookup.first);
3692 break;
3693
Richard Smithcd45dbc2014-04-19 03:48:30 +00003694 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003695 assert(isa<CXXRecordDecl>(DC) &&
3696 "Cannot have a constructor name outside of a class!");
3697 ConstructorNameSet.insert(Name);
3698 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003699
3700 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003701 assert(isa<CXXRecordDecl>(DC) &&
3702 "Cannot have a conversion function name outside of a class!");
3703 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003704 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003705 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003706 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003707
Chandler Carruthe972c362015-03-26 03:11:40 +00003708 // Sort the names into a stable order.
3709 std::sort(Names.begin(), Names.end());
3710
Chandler Carruthffbf7052015-03-26 22:27:09 +00003711 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003712 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003713 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003714
Chandler Carruthffbf7052015-03-26 22:27:09 +00003715 // First we try the easy case by forming the current context's constructor
3716 // name and adding that name first. This is a very useful optimization to
3717 // avoid walking the lexical declarations in many cases, and it also
3718 // handles the only case where a constructor name can come from some other
3719 // lexical context -- when that name is an implicit constructor merged from
3720 // another declaration in the redecl chain. Any non-implicit constructor or
3721 // conversion function which doesn't occur in all the lexical contexts
3722 // would be an ODR violation.
3723 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3724 Context->getCanonicalType(Context->getRecordType(D)));
3725 if (ConstructorNameSet.erase(ImplicitCtorName))
3726 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003727
Chandler Carruthffbf7052015-03-26 22:27:09 +00003728 // If we still have constructors or conversion functions, we walk all the
3729 // names in the decl and add the constructors and conversion functions
3730 // which are visible in the order they lexically occur within the context.
3731 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3732 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3733 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3734 auto Name = ChildND->getDeclName();
3735 switch (Name.getNameKind()) {
3736 default:
3737 continue;
3738
3739 case DeclarationName::CXXConstructorName:
3740 if (ConstructorNameSet.erase(Name))
3741 Names.push_back(Name);
3742 break;
3743
3744 case DeclarationName::CXXConversionFunctionName:
3745 if (ConversionNameSet.erase(Name))
3746 Names.push_back(Name);
3747 break;
3748 }
3749
3750 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3751 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003752 }
3753
Chandler Carruthe972c362015-03-26 03:11:40 +00003754 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3755 "constructors by walking all the "
3756 "lexical members of the context.");
3757 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3758 "conversion functions by walking all "
3759 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003760 }
3761
Chandler Carruthe972c362015-03-26 03:11:40 +00003762 // Next we need to do a lookup with each name into this decl context to fully
3763 // populate any results from external sources. We don't actually use the
3764 // results of these lookups because we only want to use the results after all
3765 // results have been loaded and the pointers into them will be stable.
3766 for (auto &Name : Names)
3767 DC->lookup(Name);
3768
3769 // Now we need to insert the results for each name into the hash table. For
3770 // constructor names and conversion function names, we actually need to merge
3771 // all of the results for them into one list of results each and insert
3772 // those.
3773 SmallVector<NamedDecl *, 8> ConstructorDecls;
3774 SmallVector<NamedDecl *, 8> ConversionDecls;
3775
3776 // Now loop over the names, either inserting them or appending for the two
3777 // special cases.
3778 for (auto &Name : Names) {
3779 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3780
3781 switch (Name.getNameKind()) {
3782 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003783 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003784 break;
3785
3786 case DeclarationName::CXXConstructorName:
3787 ConstructorDecls.append(Result.begin(), Result.end());
3788 break;
3789
3790 case DeclarationName::CXXConversionFunctionName:
3791 ConversionDecls.append(Result.begin(), Result.end());
3792 break;
3793 }
3794 }
3795
3796 // Handle our two special cases if we ended up having any. We arbitrarily use
3797 // the first declaration's name here because the name itself isn't part of
3798 // the key, only the kind of name is used.
3799 if (!ConstructorDecls.empty())
3800 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003801 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003802 if (!ConversionDecls.empty())
3803 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003804 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003805
Richard Smithd88a7f12015-09-01 20:35:42 +00003806 // Create the on-disk hash table. Also emit the existing imported and
3807 // merged table if there is one.
3808 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3809 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003810}
3811
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003812/// \brief Write the block containing all of the declaration IDs
3813/// visible from the given DeclContext.
3814///
3815/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003816/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003817uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3818 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003819 // If we imported a key declaration of this namespace, write the visible
3820 // lookup results as an update record for it rather than including them
3821 // on this declaration. We will only look at key declarations on reload.
3822 if (isa<NamespaceDecl>(DC) && Chain &&
3823 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3824 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003825 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003826 Prev = Prev->getPreviousDecl())
3827 if (!Prev->isFromASTFile())
3828 return 0;
3829
3830 // Note that we need to emit an update record for the primary context.
3831 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3832
3833 // Make sure all visible decls are written. They will be recorded later. We
3834 // do this using a side data structure so we can sort the names into
3835 // a deterministic order.
3836 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3837 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3838 LookupResults;
3839 if (Map) {
3840 LookupResults.reserve(Map->size());
3841 for (auto &Entry : *Map)
3842 LookupResults.push_back(
3843 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3844 }
3845
3846 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3847 for (auto &NameAndResult : LookupResults) {
3848 DeclarationName Name = NameAndResult.first;
3849 DeclContext::lookup_result Result = NameAndResult.second;
3850 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3851 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3852 // We have to work around a name lookup bug here where negative lookup
3853 // results for these names get cached in namespace lookup tables (these
3854 // names should never be looked up in a namespace).
3855 assert(Result.empty() && "Cannot have a constructor or conversion "
3856 "function name in a namespace!");
3857 continue;
3858 }
3859
3860 for (NamedDecl *ND : Result)
3861 if (!ND->isFromASTFile())
3862 GetDeclRef(ND);
3863 }
3864
3865 return 0;
3866 }
3867
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003868 if (DC->getPrimaryContext() != DC)
3869 return 0;
3870
Chandler Carruthe972c362015-03-26 03:11:40 +00003871 // Skip contexts which don't support name lookup.
3872 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003873 return 0;
3874
3875 // If not in C++, we perform name lookup for the translation unit via the
3876 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003877 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003878 return 0;
3879
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003880 // Serialize the contents of the mapping used for lookup. Note that,
3881 // although we have two very different code paths, the serialized
3882 // representation is the same for both cases: a declaration name,
3883 // followed by a size, followed by references to the visible
3884 // declarations that have that name.
3885 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003886 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003887 if (!Map || Map->empty())
3888 return 0;
3889
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003890 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003891 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003892 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003893
3894 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003895 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003896 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003897 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003898 ++NumVisibleDeclContexts;
3899 return Offset;
3900}
3901
Sebastian Redla4071b42010-08-24 00:50:09 +00003902/// \brief Write an UPDATE_VISIBLE block for the given context.
3903///
3904/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3905/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003906/// (in C++), for namespaces, and for classes with forward-declared unscoped
3907/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003908void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003909 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003910 if (!Map || Map->empty())
3911 return;
3912
Sebastian Redla4071b42010-08-24 00:50:09 +00003913 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003914 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003915 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003916
Richard Smith5fc18a92015-07-12 23:43:21 +00003917 // If we're updating a namespace, select a key declaration as the key for the
3918 // update record; those are the only ones that will be checked on reload.
3919 if (isa<NamespaceDecl>(DC))
3920 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3921
Sebastian Redla4071b42010-08-24 00:50:09 +00003922 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003923 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003924 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003925}
3926
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003927/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3928void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003929 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003930 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3931}
3932
3933/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3934void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003935 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003936 return;
3937
3938 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3939 RecordData Record;
3940#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3941#include "clang/Basic/OpenCLExtensions.def"
3942 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3943}
3944
Douglas Gregor404cdde2012-01-27 01:47:08 +00003945void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003946 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003947 RecordData Categories;
3948
3949 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3950 unsigned Size = 0;
3951 unsigned StartIndex = Categories.size();
3952
3953 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3954
3955 // Allocate space for the size.
3956 Categories.push_back(0);
3957
3958 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003959 for (ObjCInterfaceDecl::known_categories_iterator
3960 Cat = Class->known_categories_begin(),
3961 CatEnd = Class->known_categories_end();
3962 Cat != CatEnd; ++Cat, ++Size) {
3963 assert(getDeclID(*Cat) != 0 && "Bogus category");
3964 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003965 }
3966
3967 // Update the size.
3968 Categories[StartIndex] = Size;
3969
3970 // Record this interface -> category map.
3971 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3972 CategoriesMap.push_back(CatInfo);
3973 }
3974
3975 // Sort the categories map by the definition ID, since the reader will be
3976 // performing binary searches on this information.
3977 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3978
3979 // Emit the categories map.
3980 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003981
3982 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003983 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3986 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00003987
3988 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
3989 Stream.EmitRecordWithBlob(AbbrevID, Record,
3990 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00003991 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00003992
Douglas Gregor404cdde2012-01-27 01:47:08 +00003993 // Emit the category lists.
3994 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3995}
3996
Richard Smithe40f2ba2013-08-07 21:41:30 +00003997void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3998 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3999
4000 if (LPTMap.empty())
4001 return;
4002
4003 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004004 for (auto LPTMapEntry : LPTMap) {
4005 const FunctionDecl *FD = LPTMapEntry.first;
4006 LateParsedTemplate *LPT = LPTMapEntry.second;
4007 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004008 AddDeclRef(LPT->D, Record);
4009 Record.push_back(LPT->Toks.size());
4010
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004011 for (const auto &Tok : LPT->Toks) {
4012 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004013 }
4014 }
4015 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4016}
4017
Dario Domizioli13a0a382014-05-23 12:13:25 +00004018/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4019void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4020 RecordData Record;
4021 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4022 AddSourceLocation(PragmaLoc, Record);
4023 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4024}
4025
Nico Weber779355f2016-03-02 23:22:00 +00004026/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4027void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4028 RecordData Record;
4029 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4030 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4031}
4032
Nico Weber42932312016-03-03 00:17:35 +00004033/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4034//module.
4035void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4036 RecordData Record;
4037 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4038 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4039 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4040}
4041
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004042void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4043 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004044 // Enter the extension block.
4045 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4046
4047 // Emit the metadata record abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004048 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004049 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4050 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4051 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4052 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4053 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4054 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4055 unsigned Abbrev = Stream.EmitAbbrev(Abv);
4056
4057 // Emit the metadata record.
4058 RecordData Record;
4059 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4060 Record.push_back(EXTENSION_METADATA);
4061 Record.push_back(Metadata.MajorVersion);
4062 Record.push_back(Metadata.MinorVersion);
4063 Record.push_back(Metadata.BlockName.size());
4064 Record.push_back(Metadata.UserInfo.size());
4065 SmallString<64> Buffer;
4066 Buffer += Metadata.BlockName;
4067 Buffer += Metadata.UserInfo;
4068 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4069
4070 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004071 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004072
4073 // Exit the extension block.
4074 Stream.ExitBlock();
4075}
4076
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004077//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004078// General Serialization Routines
4079//===----------------------------------------------------------------------===//
4080
Richard Smith69c82bf2016-04-01 22:52:03 +00004081/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004082void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4083 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004084 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004085 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004086 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004087 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004088
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004089#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004090
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004091 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004092}
4093
John McCallf413f5e2013-05-03 00:10:13 +00004094void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4095 AddSourceLocation(Tok.getLocation(), Record);
4096 Record.push_back(Tok.getLength());
4097
4098 // FIXME: When reading literal tokens, reconstruct the literal pointer
4099 // if it is needed.
4100 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4101 // FIXME: Should translate token kind to a stable encoding.
4102 Record.push_back(Tok.getKind());
4103 // FIXME: Should translate token flags to a stable encoding.
4104 Record.push_back(Tok.getFlags());
4105}
4106
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004107void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004108 Record.push_back(Str.size());
4109 Record.insert(Record.end(), Str.begin(), Str.end());
4110}
4111
Richard Smith7ed1bc92014-12-05 22:42:13 +00004112bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004113 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004114
Richard Smith54cc3c22014-12-11 20:50:24 +00004115 bool Changed =
4116 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004117
4118 // Remove a prefix to make the path relative, if relevant.
4119 const char *PathBegin = Path.data();
4120 const char *PathPtr =
4121 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4122 if (PathPtr != PathBegin) {
4123 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4124 Changed = true;
4125 }
4126
4127 return Changed;
4128}
4129
4130void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4131 SmallString<128> FilePath(Path);
4132 PreparePathForOutput(FilePath);
4133 AddString(FilePath, Record);
4134}
4135
Mehdi Amini57a41912015-09-10 01:46:39 +00004136void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004137 StringRef Path) {
4138 SmallString<128> FilePath(Path);
4139 PreparePathForOutput(FilePath);
4140 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4141}
4142
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004143void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4144 RecordDataImpl &Record) {
4145 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004146 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004147 Record.push_back(*Minor + 1);
4148 else
4149 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004150 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004151 Record.push_back(*Subminor + 1);
4152 else
4153 Record.push_back(0);
4154}
4155
Douglas Gregore84a9da2009-04-20 20:36:09 +00004156/// \brief Note that the identifier II occurs at the given offset
4157/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004158void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004159 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004160 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004161 // up earlier in the chain and thus don't need an offset.
4162 if (ID >= FirstIdentID)
4163 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004164}
4165
Douglas Gregor95c13f52009-04-25 17:48:32 +00004166/// \brief Note that the selector Sel occurs at the given offset
4167/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004168void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004169 unsigned ID = SelectorIDs[Sel];
4170 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004171 // Don't record offsets for selectors that are also available in a different
4172 // file.
4173 if (ID < FirstSelectorID)
4174 return;
4175 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004176}
4177
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004178ASTWriter::ASTWriter(
4179 llvm::BitstreamWriter &Stream,
4180 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
4181 bool IncludeTimestamps)
Richard Smith01b2cb42014-07-26 06:37:51 +00004182 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004183 WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
4184 WritingAST(false), DoneWritingDeclsAndTypes(false),
4185 ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
4186 NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
4187 NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
4188 NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
4189 NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
Richard Smith01b2cb42014-07-26 06:37:51 +00004190 NextSubmoduleID(FirstSubmoduleID),
4191 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Richard Smith290d8012016-04-06 17:06:00 +00004192 NumStatements(0), NumMacros(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004193 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004194 TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004195 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004196 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004197 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004198 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4199 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004200 ExprImplicitCastAbbrev(0) {
4201 for (const auto &Ext : Extensions) {
4202 if (auto Writer = Ext->createExtensionWriter(*this))
4203 ModuleFileExtensionWriters.push_back(std::move(Writer));
4204 }
4205}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004206
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004207ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004208 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004209}
4210
Richard Smithb3761912015-02-05 23:08:52 +00004211const LangOptions &ASTWriter::getLangOpts() const {
4212 assert(WritingAST && "can't determine lang opts when not writing AST");
4213 return Context->getLangOpts();
4214}
4215
Richard Smithe75ee0f2015-08-17 07:13:32 +00004216time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4217 return IncludeTimestamps ? E->getModificationTime() : 0;
4218}
4219
Adrian Prantladbd2b12015-09-22 23:26:31 +00004220uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4221 Module *WritingModule, StringRef isysroot,
4222 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004223 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004224
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004225 ASTHasCompilerErrors = hasErrors;
4226
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004227 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004228 Stream.Emit((unsigned)'C', 8);
4229 Stream.Emit((unsigned)'P', 8);
4230 Stream.Emit((unsigned)'C', 8);
4231 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004232
Chris Lattner28fa4e62009-04-26 22:26:21 +00004233 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004234
Douglas Gregoreda8e122011-08-09 15:13:55 +00004235 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004236 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004237 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004238 ASTFileSignature Signature =
4239 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004240 Context = nullptr;
4241 PP = nullptr;
4242 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004243 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004244
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004245 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004246 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004247}
4248
Douglas Gregora94a1542011-07-27 21:45:57 +00004249template<typename Vector>
4250static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4251 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004252 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4253 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004254 Writer.AddDeclRef(*I, Record);
4255 }
4256}
4257
Adrian Prantladbd2b12015-09-22 23:26:31 +00004258uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4259 const std::string &OutputFile,
4260 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004261 using namespace llvm;
4262
Craig Toppera13603a2014-05-22 05:54:18 +00004263 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004264
Douglas Gregorcf68c582011-12-01 22:20:10 +00004265 // Make sure that the AST reader knows to finalize itself.
4266 if (Chain)
4267 Chain->finalizeForWriting();
4268
Sebastian Redl143413f2010-07-12 22:02:52 +00004269 ASTContext &Context = SemaRef.Context;
4270 Preprocessor &PP = SemaRef.PP;
4271
Douglas Gregordab42432011-08-12 00:15:20 +00004272 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004273 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4274 if (D) {
4275 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4276 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004277 }
4278 };
4279 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4280 PREDEF_DECL_TRANSLATION_UNIT_ID);
4281 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4282 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4283 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4284 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4285 PREDEF_DECL_OBJC_PROTOCOL_ID);
4286 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4287 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4288 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4289 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4290 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004291 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004292 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4293 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004294 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004295 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4296 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004297 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4298 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004299 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4300 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004301 RegisterPredefDecl(Context.TypePackElementDecl,
4302 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004303
Chris Lattner0c797362009-09-08 18:19:27 +00004304 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004305 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004306 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004307 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004308 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004309
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004310 // Build a record containing all of the file scoped decls in this file.
4311 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004312 if (!isModule)
4313 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4314 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004315
Douglas Gregor851443c2011-08-12 01:39:19 +00004316 // Build a record containing all of the delegating constructors we still need
4317 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004318 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004319 if (!isModule)
4320 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004321
Douglas Gregor851443c2011-08-12 01:39:19 +00004322 // Write the set of weak, undeclared identifiers. We always write the
4323 // entire table, since later PCH files in a PCH chain are only interested in
4324 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004325 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004326 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4327 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4328 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4329 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4330 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4331 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4332 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004333 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004334
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004335 // Build a record containing all of the ext_vector declarations.
4336 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004337 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004338
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004339 // Build a record containing all of the VTable uses information.
4340 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004341 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004342 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4343 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4344 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4345 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4346 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004347 }
4348
Nico Weber72889432014-09-06 01:25:55 +00004349 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4350 RecordData UnusedLocalTypedefNameCandidates;
4351 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4352 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4353
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004354 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004355 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004356 for (const auto &I : SemaRef.PendingInstantiations) {
4357 AddDeclRef(I.first, PendingInstantiations);
4358 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004359 }
4360 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4361 "There are local ones at end of translation unit!");
4362
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004363 // Build a record containing some declaration references.
4364 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004365 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004366 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4367 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004368 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004369 }
4370
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004371 RecordData CUDASpecialDeclRefs;
4372 if (Context.getcudaConfigureCallDecl()) {
4373 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4374 }
4375
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004376 // Build a record containing all of the known namespaces.
4377 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004378 for (const auto &I : SemaRef.KnownNamespaces) {
4379 if (!I.second)
4380 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004381 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004382
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004383 // Build a record of all used, undefined objects that require definitions.
4384 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004385
4386 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004387 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004388 for (const auto &I : Undefined) {
4389 AddDeclRef(I.first, UndefinedButUsed);
4390 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004391 }
4392
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004393 // Build a record containing all delete-expressions that we would like to
4394 // analyze later in AST.
4395 RecordData DeleteExprsToAnalyze;
4396
4397 for (const auto &DeleteExprsInfo :
4398 SemaRef.getMismatchingDeleteExpressions()) {
4399 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4400 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4401 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4402 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4403 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4404 }
4405 }
4406
Douglas Gregor112b9072012-10-18 05:31:06 +00004407 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004408 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004409
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004410 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004411 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004412
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004413 // This is so that older clang versions, before the introduction
4414 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004415 {
4416 RecordData Record = {VERSION_MAJOR};
4417 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4418 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004419
Douglas Gregor851443c2011-08-12 01:39:19 +00004420 // Create a lexical update block containing all of the declarations in the
4421 // translation unit that do not come from other AST files.
4422 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004423 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4424 for (const auto *D : TU->noload_decls()) {
4425 if (!D->isFromASTFile()) {
4426 NewGlobalKindDeclPairs.push_back(D->getKind());
4427 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4428 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004429 }
4430
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004431 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor851443c2011-08-12 01:39:19 +00004432 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4433 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4434 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
Mehdi Amini57a41912015-09-10 01:46:39 +00004435 {
4436 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4437 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4438 bytes(NewGlobalKindDeclPairs));
4439 }
4440
Douglas Gregor851443c2011-08-12 01:39:19 +00004441 // And a visible updates block for the translation unit.
4442 Abv = new llvm::BitCodeAbbrev();
4443 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4444 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004445 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4446 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4447 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004448
4449 // If we have any extern "C" names, write out a visible update for them.
4450 if (Context.ExternCContext)
4451 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004452
4453 // If the translation unit has an anonymous namespace, and we don't already
4454 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004455 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004456 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4457 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004458 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004459 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004460 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004461
Richard Smith5652c0f2014-03-21 01:48:23 +00004462 // Add update records for all mangling numbers and static local numbers.
4463 // These aren't really update records, but this is a convenient way of
4464 // tagging this rare extra data onto the declarations.
4465 for (const auto &Number : Context.MangleNumbers)
4466 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004467 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4468 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004469 for (const auto &Number : Context.StaticLocalNumbers)
4470 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004471 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4472 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004473
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004474 // Make sure visible decls, added to DeclContexts previously loaded from
4475 // an AST file, are registered for serialization.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004476 for (const auto *I : UpdatingVisibleDecls) {
4477 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004478 }
4479
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004480 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004481 // serialization, if we're storing decls with identifiers.
4482 if (!WritingModule || !getLangOpts().CPlusPlus) {
4483 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004484 for (const auto &ID : PP.getIdentifierTable()) {
4485 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004486 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4487 IIs.push_back(II);
4488 }
4489 // Sort the identifiers to visit based on their name.
4490 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4491 for (const IdentifierInfo *II : IIs) {
4492 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4493 DEnd = SemaRef.IdResolver.end();
4494 D != DEnd; ++D) {
4495 GetDeclRef(*D);
4496 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004497 }
4498 }
4499
Manman Rena0f31a02016-04-29 19:04:05 +00004500 // For method pool in the module, if it contains an entry for a selector,
4501 // the entry should be complete, containing everything introduced by that
4502 // module and all modules it imports. It's possible that the entry is out of
4503 // date, so we need to pull in the new content here.
4504
4505 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4506 // safe, we copy all selectors out.
4507 llvm::SmallVector<Selector, 256> AllSelectors;
4508 for (auto &SelectorAndID : SelectorIDs)
4509 AllSelectors.push_back(SelectorAndID.first);
4510 for (auto &Selector : AllSelectors)
4511 SemaRef.updateOutOfDateSelector(Selector);
4512
Douglas Gregor5204bde2011-08-02 16:26:37 +00004513 // Form the record of special types.
4514 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004515 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004516 AddTypeRef(Context.getFILEType(), SpecialTypes);
4517 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4518 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4519 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4520 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004521 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004522 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004523
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004524 if (Chain) {
4525 // Write the mapping information describing our module dependencies and how
4526 // each of those modules were mapped into our own offset/ID space, so that
4527 // the reader can build the appropriate mapping to its own offset/ID space.
4528 // The map consists solely of a blob with the following format:
4529 // *(module-name-len:i16 module-name:len*i8
4530 // source-location-offset:i32
4531 // identifier-id:i32
4532 // preprocessed-entity-id:i32
4533 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004534 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004535 // selector-id:i32
4536 // declaration-id:i32
4537 // c++-base-specifiers-id:i32
4538 // type-id:i32)
4539 //
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004540 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004541 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4543 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004544 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004545 {
4546 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004547 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004548 using namespace llvm::support;
4549 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004550 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004551 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004552 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004553
Ben Langmuir785180e2014-10-20 16:27:30 +00004554 // Note: if a base ID was uint max, it would not be possible to load
4555 // another module after it or have more than one entity inside it.
4556 uint32_t None = std::numeric_limits<uint32_t>::max();
4557
4558 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4559 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4560 if (ShouldWrite)
4561 LE.write<uint32_t>(BaseID);
4562 else
4563 LE.write<uint32_t>(None);
4564 };
4565
Ben Langmuirfe971d92014-08-16 04:54:18 +00004566 // These values should be unique within a chain, since they will be read
4567 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004568 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4569 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4570 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4571 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4572 M->NumPreprocessedEntities);
4573 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4574 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4575 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4576 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004577 }
4578 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004579 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004580 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4581 Buffer.data(), Buffer.size());
4582 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004583
Richard Smithb9eab6d2014-03-20 19:44:17 +00004584 RecordData DeclUpdatesOffsetsRecord;
4585
Richard Smith59442b42014-03-20 20:07:19 +00004586 // Keep writing types, declarations, and declaration update records
4587 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004588 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4589 WriteTypeAbbrevs();
4590 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004591 do {
4592 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4593 while (!DeclTypesToEmit.empty()) {
4594 DeclOrType DOT = DeclTypesToEmit.front();
4595 DeclTypesToEmit.pop();
4596 if (DOT.isType())
4597 WriteType(DOT.getType());
4598 else
4599 WriteDecl(Context, DOT.getDecl());
4600 }
4601 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004602 Stream.ExitBlock();
4603
Richard Smithb9eab6d2014-03-20 19:44:17 +00004604 DoneWritingDeclsAndTypes = true;
4605
4606 // These things can only be done once we've written out decls and types.
4607 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004608 if (!DeclUpdatesOffsetsRecord.empty())
4609 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004610 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004611 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004612 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004613 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004614 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004615 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004616 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004617 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004618 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004619 WriteFPPragmaOptions(SemaRef.getFPOptions());
4620 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004621 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004622
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004623 // If we're emitting a module, write out the submodule information.
4624 if (WritingModule)
4625 WriteSubmodules(WritingModule);
Richard Smithdc1f0422016-07-20 19:10:16 +00004626 else if (!getLangOpts().CurrentModule.empty()) {
4627 // If we're building a PCH in the implementation of a module, we may need
4628 // the description of the current module.
4629 //
4630 // FIXME: We may need other modules that we did not load from an AST file,
4631 // such as if a module declares a 'conflicts' on a different module.
4632 Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
4633 getLangOpts().CurrentModule);
4634 if (M && !M->IsFromModuleFile)
4635 WriteSubmodules(M);
4636 }
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004637
Douglas Gregor5204bde2011-08-02 16:26:37 +00004638 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4639
Douglas Gregord4df8652009-04-22 22:02:47 +00004640 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004641 if (!EagerlyDeserializedDecls.empty())
4642 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004643
4644 // Write the record containing tentative definitions.
4645 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004646 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004647
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004648 // Write the record containing unused file scoped decls.
4649 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004650 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004651
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004652 // Write the record containing weak undeclared identifiers.
4653 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004654 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004655 WeakUndeclaredIdentifiers);
4656
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004657 // Write the record containing ext_vector type names.
4658 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004659 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004660
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004661 // Write the record containing VTable uses information.
4662 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004663 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004664
Nico Weber72889432014-09-06 01:25:55 +00004665 // Write the record containing potentially unused local typedefs.
4666 if (!UnusedLocalTypedefNameCandidates.empty())
4667 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4668 UnusedLocalTypedefNameCandidates);
4669
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004670 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004671 if (!PendingInstantiations.empty())
4672 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004673
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004674 // Write the record containing declaration references of Sema.
4675 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004676 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004677
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004678 // Write the record containing CUDA-specific declaration references.
4679 if (!CUDASpecialDeclRefs.empty())
4680 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004681
4682 // Write the delegating constructors.
4683 if (!DelegatingCtorDecls.empty())
4684 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004685
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004686 // Write the known namespaces.
4687 if (!KnownNamespaces.empty())
4688 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004689
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004690 // Write the undefined internal functions and variables, and inline functions.
4691 if (!UndefinedButUsed.empty())
4692 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004693
4694 if (!DeleteExprsToAnalyze.empty())
4695 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4696
Douglas Gregor851443c2011-08-12 01:39:19 +00004697 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004698 for (auto *DC : UpdatedDeclContexts)
4699 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004700
Douglas Gregor959bb062011-12-03 01:15:29 +00004701 if (!WritingModule) {
4702 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004703 struct ModuleInfo {
4704 uint64_t ID;
4705 Module *M;
4706 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4707 };
Richard Smith56be7542014-03-21 00:33:59 +00004708 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004709 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004710 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004711 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4712 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004713 }
Richard Smith56be7542014-03-21 00:33:59 +00004714
4715 if (!Imports.empty()) {
4716 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4717 return A.ID < B.ID;
4718 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004719 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4720 return A.ID == B.ID;
4721 };
Richard Smith56be7542014-03-21 00:33:59 +00004722
4723 // Sort and deduplicate module IDs.
4724 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004725 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004726 Imports.end());
4727
4728 RecordData ImportedModules;
4729 for (const auto &Import : Imports) {
4730 ImportedModules.push_back(Import.ID);
4731 // FIXME: If the module has macros imported then later has declarations
4732 // imported, this location won't be the right one as a location for the
4733 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004734 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004735 }
4736
Douglas Gregor959bb062011-12-03 01:15:29 +00004737 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4738 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004739 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004740
Douglas Gregor404cdde2012-01-27 01:47:08 +00004741 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004742 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004743 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004744 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004745 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004746 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004747
Douglas Gregor08f01292009-04-17 22:13:46 +00004748 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004749 RecordData::value_type Record[] = {
4750 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004751 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004752 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004753
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004754 // Write the module file extension blocks.
4755 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004756 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004757
Adrian Prantladbd2b12015-09-22 23:26:31 +00004758 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004759}
4760
Richard Smithb9eab6d2014-03-20 19:44:17 +00004761void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004762 if (DeclUpdates.empty())
4763 return;
4764
Richard Smith59442b42014-03-20 20:07:19 +00004765 DeclUpdateMap LocalUpdates;
4766 LocalUpdates.swap(DeclUpdates);
4767
Richard Smith6ef42932014-03-20 21:02:00 +00004768 for (auto &DeclUpdate : LocalUpdates) {
4769 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004770
Richard Smithd28ac5b2014-03-22 23:33:22 +00004771 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004772 RecordData RecordData;
4773 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004774 for (auto &Update : DeclUpdate.second) {
4775 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4776
Richard Smith69c82bf2016-04-01 22:52:03 +00004777 // An updated body is emitted last, so that the reader doesn't need
4778 // to skip over the lazy body to reach statements for other records.
4779 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4780 HasUpdatedBody = true;
4781 else
4782 Record.push_back(Kind);
4783
Richard Smith6ef42932014-03-20 21:02:00 +00004784 switch (Kind) {
4785 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4786 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4787 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004788 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004789 Record.push_back(GetDeclRef(Update.getDecl()));
4790 break;
4791
Richard Smith4d235792014-08-07 18:53:08 +00004792 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004793 break;
4794
Richard Smith4d235792014-08-07 18:53:08 +00004795 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004796 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004797 break;
4798
John McCall32791cc2016-01-06 22:34:54 +00004799 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004800 Record.AddStmt(const_cast<Expr *>(
4801 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004802 break;
4803
Richard Smith4b054b22016-08-24 21:25:37 +00004804 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4805 Record.AddStmt(
4806 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4807 break;
4808
Richard Smithcd45dbc2014-04-19 03:48:30 +00004809 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4810 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004811 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004812 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004813 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004814 *Context, const_cast<CXXRecordDecl *>(RD)));
4815
4816 // This state is sometimes updated by template instantiation, when we
4817 // switch from the specialization referring to the template declaration
4818 // to it referring to the template definition.
4819 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4820 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004821 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004822 } else {
4823 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4824 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004825 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004826
4827 // The instantiation might have been resolved to a partial
4828 // specialization. If so, record which one.
4829 auto From = Spec->getInstantiatedFrom();
4830 if (auto PartialSpec =
4831 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4832 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004833 Record.AddDeclRef(PartialSpec);
4834 Record.AddTemplateArgumentList(
4835 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004836 } else {
4837 Record.push_back(false);
4838 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004839 }
4840 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004841 Record.AddSourceLocation(RD->getLocation());
4842 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004843 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004844
4845 // Instantiation may change attributes; write them all out afresh.
4846 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004847 if (D->hasAttrs())
4848 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004849
4850 // FIXME: Ensure we don't get here for explicit instantiations.
4851 break;
4852 }
4853
Richard Smithf8134002015-03-10 01:41:22 +00004854 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004855 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004856 break;
4857
Richard Smith564417a2014-03-20 21:47:22 +00004858 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4859 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004860 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4861 Record);
4862 break;
4863
Richard Smith6ef42932014-03-20 21:02:00 +00004864 case UPD_CXX_DEDUCED_RETURN_TYPE:
4865 Record.push_back(GetOrCreateTypeID(Update.getType()));
4866 break;
4867
4868 case UPD_DECL_MARKED_USED:
4869 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004870
4871 case UPD_MANGLING_NUMBER:
4872 case UPD_STATIC_LOCAL_NUMBER:
4873 Record.push_back(Update.getNumber());
4874 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004875
Alexey Bataev97720002014-11-11 04:05:39 +00004876 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004877 Record.AddSourceRange(
4878 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004879 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004880
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004881 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4882 Record.AddSourceRange(
4883 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4884 break;
4885
Richard Smith65ebb4a2015-03-26 04:09:53 +00004886 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004887 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004888 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004889
4890 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004891 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004892 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004893 }
4894 }
4895
Richard Smithd28ac5b2014-03-22 23:33:22 +00004896 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004897 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004898 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004899 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004900 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004901 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004902 }
4903
Richard Smithcd45dbc2014-04-19 03:48:30 +00004904 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004905 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004906 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004907}
4908
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004909void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004910 uint32_t Raw = Loc.getRawEncoding();
4911 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004912}
4913
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004914void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004915 AddSourceLocation(Range.getBegin(), Record);
4916 AddSourceLocation(Range.getEnd(), Record);
4917}
4918
Richard Smithf144b542016-04-08 21:54:32 +00004919void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4920 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004921 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004922 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004923}
4924
Richard Smithf144b542016-04-08 21:54:32 +00004925void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4926 Record->push_back(Value.isUnsigned());
4927 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004928}
4929
Richard Smithf144b542016-04-08 21:54:32 +00004930void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4931 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00004932}
4933
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004934void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004935 Record.push_back(getIdentifierRef(II));
4936}
4937
Sebastian Redl539c5062010-08-18 23:57:32 +00004938IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004939 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004940 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004941
Sebastian Redl539c5062010-08-18 23:57:32 +00004942 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004943 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004944 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004945 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004946}
4947
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004948MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004949 // Don't emit builtin macros like __LINE__ to the AST file unless they
4950 // have been redefined by the header (in which case they are not
4951 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004952 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004953 return 0;
4954
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004955 MacroID &ID = MacroIDs[MI];
4956 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004957 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004958 MacroInfoToEmitData Info = { Name, MI, ID };
4959 MacroInfosToEmit.push_back(Info);
4960 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004961 return ID;
4962}
4963
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004964MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004965 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004966 return 0;
4967
4968 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4969 return MacroIDs[MI];
4970}
4971
4972uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004973 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004974}
4975
Richard Smithe64e7452016-04-18 21:54:58 +00004976void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
4977 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00004978}
4979
Sebastian Redl539c5062010-08-18 23:57:32 +00004980SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004981 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004982 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004983 }
4984
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004985 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004986 if (SID == 0 && Chain) {
4987 // This might trigger a ReadSelector callback, which will set the ID for
4988 // this selector.
4989 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004990 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004991 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004992 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004993 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004994 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004995 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004996 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004997}
4998
Richard Smithe64e7452016-04-18 21:54:58 +00004999void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5000 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005001}
5002
Richard Smith290d8012016-04-06 17:06:00 +00005003void ASTRecordWriter::AddTemplateArgumentLocInfo(
5004 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005005 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005006 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005007 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005008 break;
5009 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005010 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005011 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005012 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005013 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5014 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005015 break;
5016 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005017 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5018 AddSourceLocation(Arg.getTemplateNameLoc());
5019 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005020 break;
John McCall0ad16662009-10-29 08:12:44 +00005021 case TemplateArgument::Null:
5022 case TemplateArgument::Integral:
5023 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005024 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005025 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005026 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005027 break;
5028 }
5029}
5030
Richard Smith290d8012016-04-06 17:06:00 +00005031void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5032 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005033
5034 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5035 bool InfoHasSameExpr
5036 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005037 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005038 if (InfoHasSameExpr)
5039 return; // Avoid storing the same expr twice.
5040 }
Richard Smith290d8012016-04-06 17:06:00 +00005041 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005042}
5043
Richard Smith290d8012016-04-06 17:06:00 +00005044void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005045 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005046 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005047 return;
5048 }
5049
Richard Smith290d8012016-04-06 17:06:00 +00005050 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005051}
5052
Richard Smith290d8012016-04-06 17:06:00 +00005053void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5054 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005055
Richard Smith290d8012016-04-06 17:06:00 +00005056 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005057 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005058 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005059}
5060
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005061void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005062 Record.push_back(GetOrCreateTypeID(T));
5063}
5064
Richard Smith2095ffe2015-03-16 20:11:03 +00005065TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005066 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005067 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5068 if (T.isNull())
5069 return TypeIdx();
5070 assert(!T.getLocalFastQualifiers());
5071
5072 TypeIdx &Idx = TypeIdxs[T];
5073 if (Idx.getIndex() == 0) {
5074 if (DoneWritingDeclsAndTypes) {
5075 assert(0 && "New type seen after serializing all the types to emit!");
5076 return TypeIdx();
5077 }
5078
5079 // We haven't seen this type before. Assign it a new ID and put it
5080 // into the queue of types to emit.
5081 Idx = TypeIdx(NextTypeID++);
5082 DeclTypesToEmit.push(T);
5083 }
5084 return Idx;
5085 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005086}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005087
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005088TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005089 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005090 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5091 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005092 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005093 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005094
Richard Smith2095ffe2015-03-16 20:11:03 +00005095 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5096 assert(I != TypeIdxs.end() && "Type not emitted!");
5097 return I->second;
5098 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005099}
5100
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005101void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005102 Record.push_back(GetDeclRef(D));
5103}
5104
Sebastian Redl539c5062010-08-18 23:57:32 +00005105DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005106 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005107
5108 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005109 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005110 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005111
5112 // If D comes from an AST file, its declaration ID is already known and
5113 // fixed.
5114 if (D->isFromASTFile())
5115 return D->getGlobalID();
5116
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005117 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005118 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005119 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005120 if (DoneWritingDeclsAndTypes) {
5121 assert(0 && "New decl seen after serializing all the decls to emit!");
5122 return 0;
5123 }
5124
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005125 // We haven't seen this declaration before. Give it a new ID and
5126 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005127 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005128 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005129 }
5130
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005131 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005132}
5133
Sebastian Redl539c5062010-08-18 23:57:32 +00005134DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005135 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005136 return 0;
5137
Douglas Gregorb3163e52012-01-05 22:33:30 +00005138 // If D comes from an AST file, its declaration ID is already known and
5139 // fixed.
5140 if (D->isFromASTFile())
5141 return D->getGlobalID();
5142
Douglas Gregore84a9da2009-04-20 20:36:09 +00005143 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5144 return DeclIDs[D];
5145}
5146
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005147void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005148 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005149 assert(D);
5150
5151 SourceLocation Loc = D->getLocation();
5152 if (Loc.isInvalid())
5153 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005154
5155 // We only keep track of the file-level declarations of each file.
5156 if (!D->getLexicalDeclContext()->isFileContext())
5157 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005158 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5159 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005160 if (isa<ParmVarDecl>(D))
5161 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005162
5163 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005164 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005165 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005166 FileID FID;
5167 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005168 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005169 if (FID.isInvalid())
5170 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005171 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005172
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005173 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005174 if (!Info)
5175 Info = new DeclIDInFileInfo();
5176
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005177 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005178 LocDeclIDsTy &Decls = Info->DeclIDs;
5179
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005180 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005181 Decls.push_back(LocDecl);
5182 return;
5183 }
5184
Benjamin Kramer45025c02013-08-24 13:22:59 +00005185 LocDeclIDsTy::iterator I =
5186 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005187
5188 Decls.insert(I, LocDecl);
5189}
5190
Richard Smithe64e7452016-04-18 21:54:58 +00005191void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005192 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005193 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005194 switch (Name.getNameKind()) {
5195 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005196 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005197 break;
5198
5199 case DeclarationName::ObjCZeroArgSelector:
5200 case DeclarationName::ObjCOneArgSelector:
5201 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005202 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005203 break;
5204
5205 case DeclarationName::CXXConstructorName:
5206 case DeclarationName::CXXDestructorName:
5207 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005208 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005209 break;
5210
5211 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005212 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005213 break;
5214
Alexis Hunt3d221f22009-11-29 07:34:05 +00005215 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005216 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005217 break;
5218
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005219 case DeclarationName::CXXUsingDirective:
5220 // No extra data to emit
5221 break;
5222 }
5223}
Chris Lattnerca025db2010-05-07 21:43:38 +00005224
Richard Smithd08aeb62014-08-28 01:33:39 +00005225unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5226 assert(needsAnonymousDeclarationNumber(D) &&
5227 "expected an anonymous declaration");
5228
5229 // Number the anonymous declarations within this context, if we've not
5230 // already done so.
5231 auto It = AnonymousDeclarationNumbers.find(D);
5232 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005233 auto *DC = D->getLexicalDeclContext();
5234 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5235 AnonymousDeclarationNumbers[ND] = Number;
5236 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005237
5238 It = AnonymousDeclarationNumbers.find(D);
5239 assert(It != AnonymousDeclarationNumbers.end() &&
5240 "declaration not found within its lexical context");
5241 }
5242
5243 return It->second;
5244}
5245
Richard Smith290d8012016-04-06 17:06:00 +00005246void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5247 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005248 switch (Name.getNameKind()) {
5249 case DeclarationName::CXXConstructorName:
5250 case DeclarationName::CXXDestructorName:
5251 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005252 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005253 break;
5254
5255 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005256 AddSourceLocation(SourceLocation::getFromRawEncoding(
5257 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005258 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005259 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005260 break;
5261
5262 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005263 AddSourceLocation(SourceLocation::getFromRawEncoding(
5264 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005265 break;
5266
5267 case DeclarationName::Identifier:
5268 case DeclarationName::ObjCZeroArgSelector:
5269 case DeclarationName::ObjCOneArgSelector:
5270 case DeclarationName::ObjCMultiArgSelector:
5271 case DeclarationName::CXXUsingDirective:
5272 break;
5273 }
5274}
5275
Richard Smith290d8012016-04-06 17:06:00 +00005276void ASTRecordWriter::AddDeclarationNameInfo(
5277 const DeclarationNameInfo &NameInfo) {
5278 AddDeclarationName(NameInfo.getName());
5279 AddSourceLocation(NameInfo.getLoc());
5280 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005281}
5282
Richard Smith290d8012016-04-06 17:06:00 +00005283void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5284 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5285 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005286 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005287 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005288}
5289
Richard Smithe64e7452016-04-18 21:54:58 +00005290void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005291 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005292 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005293 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005294
5295 // Push each of the NNS's onto a stack for serialization in reverse order.
5296 while (NNS) {
5297 NestedNames.push_back(NNS);
5298 NNS = NNS->getPrefix();
5299 }
5300
Richard Smithe64e7452016-04-18 21:54:58 +00005301 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005302 while(!NestedNames.empty()) {
5303 NNS = NestedNames.pop_back_val();
5304 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005305 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005306 switch (Kind) {
5307 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005308 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005309 break;
5310
5311 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005312 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005313 break;
5314
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005315 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005316 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005317 break;
5318
Chris Lattnerca025db2010-05-07 21:43:38 +00005319 case NestedNameSpecifier::TypeSpec:
5320 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005321 AddTypeRef(QualType(NNS->getAsType(), 0));
5322 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005323 break;
5324
5325 case NestedNameSpecifier::Global:
5326 // Don't need to write an associated value.
5327 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005328
5329 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005330 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005331 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005332 }
5333 }
5334}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005335
Richard Smith290d8012016-04-06 17:06:00 +00005336void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005337 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005338 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005339 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005340
5341 // Push each of the nested-name-specifiers's onto a stack for
5342 // serialization in reverse order.
5343 while (NNS) {
5344 NestedNames.push_back(NNS);
5345 NNS = NNS.getPrefix();
5346 }
5347
Richard Smith290d8012016-04-06 17:06:00 +00005348 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005349 while(!NestedNames.empty()) {
5350 NNS = NestedNames.pop_back_val();
5351 NestedNameSpecifier::SpecifierKind Kind
5352 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005353 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005354 switch (Kind) {
5355 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005356 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5357 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005358 break;
5359
5360 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005361 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5362 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005363 break;
5364
5365 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005366 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5367 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005368 break;
5369
5370 case NestedNameSpecifier::TypeSpec:
5371 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005372 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5373 AddTypeLoc(NNS.getTypeLoc());
5374 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005375 break;
5376
5377 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005378 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005379 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005380
5381 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005382 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5383 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005384 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005385 }
5386 }
5387}
5388
Richard Smith290d8012016-04-06 17:06:00 +00005389void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005390 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005391 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005392 switch (Kind) {
5393 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005394 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005395 break;
5396
5397 case TemplateName::OverloadedTemplate: {
5398 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005399 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005400 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005401 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005402 break;
5403 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005404
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005405 case TemplateName::QualifiedTemplate: {
5406 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005407 AddNestedNameSpecifier(QualT->getQualifier());
5408 Record->push_back(QualT->hasTemplateKeyword());
5409 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005410 break;
5411 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005412
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005413 case TemplateName::DependentTemplate: {
5414 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005415 AddNestedNameSpecifier(DepT->getQualifier());
5416 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005417 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005418 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005419 else
Richard Smith290d8012016-04-06 17:06:00 +00005420 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005421 break;
5422 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005423
5424 case TemplateName::SubstTemplateTemplateParm: {
5425 SubstTemplateTemplateParmStorage *subst
5426 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005427 AddDeclRef(subst->getParameter());
5428 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005429 break;
5430 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005431
5432 case TemplateName::SubstTemplateTemplateParmPack: {
5433 SubstTemplateTemplateParmPackStorage *SubstPack
5434 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005435 AddDeclRef(SubstPack->getParameterPack());
5436 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005437 break;
5438 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005439 }
5440}
5441
Richard Smith290d8012016-04-06 17:06:00 +00005442void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5443 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005444 switch (Arg.getKind()) {
5445 case TemplateArgument::Null:
5446 break;
5447 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005448 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005449 break;
5450 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005451 AddDeclRef(Arg.getAsDecl());
5452 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005453 break;
5454 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005455 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005456 break;
5457 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005458 AddAPSInt(Arg.getAsIntegral());
5459 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005460 break;
5461 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005462 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005463 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005464 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005465 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005466 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005467 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005468 else
Richard Smith290d8012016-04-06 17:06:00 +00005469 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005470 break;
5471 case TemplateArgument::Expression:
5472 AddStmt(Arg.getAsExpr());
5473 break;
5474 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005475 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005476 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005477 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005478 break;
5479 }
5480}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005481
Richard Smithe64e7452016-04-18 21:54:58 +00005482void ASTRecordWriter::AddTemplateParameterList(
5483 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005484 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005485 AddSourceLocation(TemplateParams->getTemplateLoc());
5486 AddSourceLocation(TemplateParams->getLAngleLoc());
5487 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005488 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005489 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005490 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005491 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005492}
5493
5494/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005495void ASTRecordWriter::AddTemplateArgumentList(
5496 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005497 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005498 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005499 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005500 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005501}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005502
Richard Smith290d8012016-04-06 17:06:00 +00005503void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5504 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005505 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005506 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5507 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5508 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005509 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005510 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005511 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005512}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005513
Richard Smithe64e7452016-04-18 21:54:58 +00005514void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5515 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005516 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005517 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005518 AddDeclRef(I.getDecl());
5519 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005520 }
5521}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005522
Richard Smith290d8012016-04-06 17:06:00 +00005523// FIXME: Move this out of the main ASTRecordWriter interface.
5524void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5525 Record->push_back(Base.isVirtual());
5526 Record->push_back(Base.isBaseOfClass());
5527 Record->push_back(Base.getAccessSpecifierAsWritten());
5528 Record->push_back(Base.getInheritConstructors());
5529 AddTypeSourceInfo(Base.getTypeSourceInfo());
5530 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005531 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005532 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005533}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005534
Richard Smith645d2cf2016-04-14 00:29:55 +00005535static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5536 ArrayRef<CXXBaseSpecifier> Bases) {
5537 ASTWriter::RecordData Record;
5538 ASTRecordWriter Writer(W, Record);
5539 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005540
Richard Smith645d2cf2016-04-14 00:29:55 +00005541 for (auto &Base : Bases)
5542 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005543
Richard Smith645d2cf2016-04-14 00:29:55 +00005544 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005545}
5546
Richard Smith290d8012016-04-06 17:06:00 +00005547// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005548void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5549 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5550}
5551
Richard Smithaa165cf2016-04-13 21:57:08 +00005552static uint64_t
5553EmitCXXCtorInitializers(ASTWriter &W,
5554 ArrayRef<CXXCtorInitializer *> CtorInits) {
5555 ASTWriter::RecordData Record;
5556 ASTRecordWriter Writer(W, Record);
5557 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005558
Richard Smithaa165cf2016-04-13 21:57:08 +00005559 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005560 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005561 Writer.push_back(CTOR_INITIALIZER_BASE);
5562 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5563 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005564 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005565 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5566 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005567 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005568 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5569 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005570 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005571 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5572 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005573 }
Francois Pichetd583da02010-12-04 09:14:42 +00005574
Richard Smithaa165cf2016-04-13 21:57:08 +00005575 Writer.AddSourceLocation(Init->getMemberLocation());
5576 Writer.AddStmt(Init->getInit());
5577 Writer.AddSourceLocation(Init->getLParenLoc());
5578 Writer.AddSourceLocation(Init->getRParenLoc());
5579 Writer.push_back(Init->isWritten());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005580 if (Init->isWritten()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005581 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005582 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005583 Writer.push_back(Init->getNumArrayIndices());
5584 for (auto *VD : Init->getArrayIndices())
5585 Writer.AddDeclRef(VD);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005586 }
5587 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005588
5589 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005590}
5591
Richard Smithaa165cf2016-04-13 21:57:08 +00005592// FIXME: Move this out of the main ASTRecordWriter interface.
5593void ASTRecordWriter::AddCXXCtorInitializers(
5594 ArrayRef<CXXCtorInitializer *> CtorInits) {
5595 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005596}
5597
Richard Smith290d8012016-04-06 17:06:00 +00005598void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005599 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005600 Record->push_back(Data.IsLambda);
5601 Record->push_back(Data.UserDeclaredConstructor);
5602 Record->push_back(Data.UserDeclaredSpecialMembers);
5603 Record->push_back(Data.Aggregate);
5604 Record->push_back(Data.PlainOldData);
5605 Record->push_back(Data.Empty);
5606 Record->push_back(Data.Polymorphic);
5607 Record->push_back(Data.Abstract);
5608 Record->push_back(Data.IsStandardLayout);
5609 Record->push_back(Data.HasNoNonEmptyBases);
5610 Record->push_back(Data.HasPrivateFields);
5611 Record->push_back(Data.HasProtectedFields);
5612 Record->push_back(Data.HasPublicFields);
5613 Record->push_back(Data.HasMutableFields);
5614 Record->push_back(Data.HasVariantMembers);
5615 Record->push_back(Data.HasOnlyCMembers);
5616 Record->push_back(Data.HasInClassInitializer);
5617 Record->push_back(Data.HasUninitializedReferenceMember);
5618 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005619 Record->push_back(Data.HasInheritedConstructor);
5620 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005621 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5622 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5623 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5624 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5625 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5626 Record->push_back(Data.DefaultedDestructorIsDeleted);
5627 Record->push_back(Data.HasTrivialSpecialMembers);
5628 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5629 Record->push_back(Data.HasIrrelevantDestructor);
5630 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5631 Record->push_back(Data.HasDefaultedDefaultConstructor);
5632 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5633 Record->push_back(Data.HasConstexprDefaultConstructor);
5634 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5635 Record->push_back(Data.ComputedVisibleConversions);
5636 Record->push_back(Data.UserProvidedDefaultConstructor);
5637 Record->push_back(Data.DeclaredSpecialMembers);
5638 Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
5639 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5640 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5641 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005642 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005643
Richard Smith290d8012016-04-06 17:06:00 +00005644 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005645 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005646 AddCXXBaseSpecifiers(Data.bases());
5647
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005648 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005649 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005650 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005651 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005652
Richard Smith290d8012016-04-06 17:06:00 +00005653 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5654 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005655 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005656 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005657
5658 // Add lambda-specific data.
5659 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005660 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005661 Record->push_back(Lambda.Dependent);
5662 Record->push_back(Lambda.IsGenericLambda);
5663 Record->push_back(Lambda.CaptureDefault);
5664 Record->push_back(Lambda.NumCaptures);
5665 Record->push_back(Lambda.NumExplicitCaptures);
5666 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005667 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005668 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005669 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005670 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005671 AddSourceLocation(Capture.getLocation());
5672 Record->push_back(Capture.isImplicit());
5673 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005674 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005675 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005676 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005677 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005678 break;
5679 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005680 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005681 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005682 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005683 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005684 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005685 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005686 break;
5687 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005688 }
5689 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005690}
5691
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005692void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005693 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005694 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005695 assert(FirstDeclID == NextDeclID &&
5696 FirstTypeID == NextTypeID &&
5697 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005698 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005699 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005700 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005701 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005702
Sebastian Redl07a89a82010-07-30 00:29:29 +00005703 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005704
Richard Smith7f330cd2015-03-18 01:42:29 +00005705 // Note, this will get called multiple times, once one the reader starts up
5706 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005707 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5708 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5709 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005710 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005711 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005712 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005713 NextDeclID = FirstDeclID;
5714 NextTypeID = FirstTypeID;
5715 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005716 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005717 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005718 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005719}
5720
Sebastian Redl539c5062010-08-18 23:57:32 +00005721void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005722 // Always keep the highest ID. See \p TypeRead() for more information.
5723 IdentID &StoredID = IdentifierIDs[II];
5724 if (ID > StoredID)
5725 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005726}
5727
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005728void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005729 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005730 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005731 if (ID > StoredID)
5732 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005733}
5734
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005735void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005736 // Always take the highest-numbered type index. This copes with an interesting
5737 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005738 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005739 // keep the higher-numbered entry so that we can properly write it out to
5740 // the AST file.
5741 TypeIdx &StoredIdx = TypeIdxs[T];
5742 if (Idx.getIndex() >= StoredIdx.getIndex())
5743 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005744}
5745
Sebastian Redl539c5062010-08-18 23:57:32 +00005746void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005747 // Always keep the highest ID. See \p TypeRead() for more information.
5748 SelectorID &StoredID = SelectorIDs[S];
5749 if (ID > StoredID)
5750 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005751}
Douglas Gregor91096292010-10-02 19:29:26 +00005752
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005753void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005754 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005755 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005756 MacroDefinitions[MD] = ID;
5757}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005758
Douglas Gregore37a85a2011-12-02 17:30:13 +00005759void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5760 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5761 SubmoduleIDs[Mod] = ID;
5762}
5763
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005764void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005765 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005766 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005767 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005768 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005769 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005770 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005771 // A forward reference was mutated into a definition. Rewrite it.
5772 // FIXME: This happens during template instantiation, should we
5773 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005774 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5775 "completed a tag from another module but not by instantiation?");
5776 DeclUpdates[RD].push_back(
5777 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005778 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005779 }
5780}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005781
Richard Smithf983f7f2015-10-13 00:23:25 +00005782static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5783 if (D->isFromASTFile())
5784 return true;
5785
Richard Smithf983f7f2015-10-13 00:23:25 +00005786 // The predefined __va_list_tag struct is imported if we imported any decls.
5787 // FIXME: This is a gross hack.
5788 return D == D->getASTContext().getVaListTagDecl();
5789}
5790
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005791void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005792 if (Chain && Chain->isProcessingUpdateRecords()) return;
5793 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005794 "Should not add lookup results to non-lookup contexts!");
5795
Vassil Vassilev632eac32016-03-16 11:17:04 +00005796 // TU is handled elsewhere.
5797 if (isa<TranslationUnitDecl>(DC))
5798 return;
5799
5800 // Namespaces are handled elsewhere, except for template instantiations of
5801 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5802 // local instantiations are added to an imported context. Only happens when
5803 // adding ADL lookup candidates, for example templated friends.
5804 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5805 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005806 return;
5807
Richard Smithf983f7f2015-10-13 00:23:25 +00005808 // We're only interested in cases where a local declaration is added to an
5809 // imported context.
5810 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5811 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005812
Richard Smith0f4e2c42015-08-06 04:23:48 +00005813 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005814 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005815 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005816 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5817 // We're adding a visible declaration to a predefined decl context. Ensure
5818 // that we write out all of its lookup results so we don't get a nasty
5819 // surprise when we try to emit its lookup table.
5820 for (auto *Child : DC->decls())
5821 UpdatingVisibleDecls.push_back(Child);
5822 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005823 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005824}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005825
5826void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005827 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005828 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005829
5830 // We're only interested in cases where a local declaration is added to an
5831 // imported context.
5832 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5833 return;
5834
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005835 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005836 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005837
5838 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005839 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005840 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005841 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005842}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005843
Richard Smith564417a2014-03-20 21:47:22 +00005844void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005845 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005846 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5847 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005848 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005849 // If we don't already know the exception specification for this redecl
5850 // chain, add an update record for it.
5851 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5852 ->getType()
5853 ->castAs<FunctionProtoType>()
5854 ->getExceptionSpecType()))
5855 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5856 });
Richard Smith564417a2014-03-20 21:47:22 +00005857}
5858
Richard Smith1fa5d642013-05-11 05:45:24 +00005859void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005860 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005861 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005862 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005863 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005864 DeclUpdates[D].push_back(
5865 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5866 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005867}
5868
Richard Smithf8134002015-03-10 01:41:22 +00005869void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5870 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005871 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005872 assert(!WritingAST && "Already writing the AST!");
5873 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005874 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005875 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005876 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5877 });
Richard Smithf8134002015-03-10 01:41:22 +00005878}
5879
Sebastian Redlab238a72011-04-24 16:28:06 +00005880void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005881 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005882 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005883 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005884 return; // Declaration not imported from PCH.
5885
Richard Smith4d235792014-08-07 18:53:08 +00005886 // Implicit function decl from a PCH was defined.
5887 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005888}
5889
Richard Smithd28ac5b2014-03-22 23:33:22 +00005890void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005891 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005892 assert(!WritingAST && "Already writing the AST!");
5893 if (!D->isFromASTFile())
5894 return;
5895
Richard Smith9e2341d2015-03-23 03:25:59 +00005896 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005897}
5898
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005899void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005900 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005901 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005902 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005903 return;
5904
5905 // Since the actual instantiation is delayed, this really means that we need
5906 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005907 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005908 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5909 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005910}
5911
John McCall32791cc2016-01-06 22:34:54 +00005912void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005913 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005914 assert(!WritingAST && "Already writing the AST!");
5915 if (!D->isFromASTFile())
5916 return;
5917
5918 DeclUpdates[D].push_back(
5919 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5920}
5921
Richard Smith4b054b22016-08-24 21:25:37 +00005922void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5923 assert(!WritingAST && "Already writing the AST!");
5924 if (!D->isFromASTFile())
5925 return;
5926
5927 DeclUpdates[D].push_back(
5928 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5929}
5930
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005931void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5932 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005933 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005934 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005935 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005936 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005937
5938 assert(IFD->getDefinition() && "Category on a class without a definition?");
5939 ObjCClassesWithCategories.insert(
5940 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005941}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005942
Eli Friedman276dd182013-09-05 00:02:25 +00005943void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005944 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005945 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005946
5947 // If there is *any* declaration of the entity that's not from an AST file,
5948 // we can skip writing the update record. We make sure that isUsed() triggers
5949 // completion of the redeclaration chain of the entity.
5950 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5951 if (IsLocalDecl(Prev))
5952 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005953
Aaron Ballman4f45b712014-03-21 15:22:56 +00005954 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005955}
Alexey Bataev97720002014-11-11 04:05:39 +00005956
5957void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005958 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005959 assert(!WritingAST && "Already writing the AST!");
5960 if (!D->isFromASTFile())
5961 return;
5962
5963 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5964}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005965
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005966void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5967 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005968 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005969 assert(!WritingAST && "Already writing the AST!");
5970 if (!D->isFromASTFile())
5971 return;
5972
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005973 DeclUpdates[D].push_back(
5974 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005975}
5976
Richard Smith4caa4492015-05-15 02:34:32 +00005977void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005978 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005979 assert(!WritingAST && "Already writing the AST!");
5980 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005981 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005982}
Alex Denisovfde64952015-06-26 05:28:36 +00005983
5984void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5985 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005986 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00005987 assert(!WritingAST && "Already writing the AST!");
5988 if (!Record->isFromASTFile())
5989 return;
5990 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5991}