blob: 886523ea943198756248cbccca6bb43350e36d0f [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());
Joey Goulye3c85de2016-12-01 11:30:49 +0000519 Record.push_back(T->isReadOnly());
520 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000521}
522
John McCall8f115c62009-10-16 21:56:05 +0000523namespace {
524
525class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000526 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000527
528public:
Richard Smith290d8012016-04-06 17:06:00 +0000529 TypeLocWriter(ASTRecordWriter &Record)
530 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000531
John McCall17001972009-10-18 01:05:36 +0000532#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000533#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000534 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000535#include "clang/AST/TypeLocNodes.def"
536
John McCall17001972009-10-18 01:05:36 +0000537 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
538 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000539};
540
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000541} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000542
John McCall17001972009-10-18 01:05:36 +0000543void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
544 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000545}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000546
John McCall17001972009-10-18 01:05:36 +0000547void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000548 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000549 if (TL.needsExtraLocalData()) {
550 Record.push_back(TL.getWrittenTypeSpec());
551 Record.push_back(TL.getWrittenSignSpec());
552 Record.push_back(TL.getWrittenWidthSpec());
553 Record.push_back(TL.hasModeAttr());
554 }
John McCall8f115c62009-10-16 21:56:05 +0000555}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000556
John McCall17001972009-10-18 01:05:36 +0000557void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000558 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000559}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000560
John McCall17001972009-10-18 01:05:36 +0000561void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000562 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000563}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000564
Reid Kleckner8a365022013-06-24 17:51:48 +0000565void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
566 // nothing to do
567}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000568
Reid Kleckner0503a872013-12-05 01:23:43 +0000569void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
570 // nothing to do
571}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000572
John McCall17001972009-10-18 01:05:36 +0000573void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000574 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000575}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000576
John McCall17001972009-10-18 01:05:36 +0000577void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000578 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000579}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000580
John McCall17001972009-10-18 01:05:36 +0000581void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000582 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000583}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000584
John McCall17001972009-10-18 01:05:36 +0000585void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000586 Record.AddSourceLocation(TL.getStarLoc());
587 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000588}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000589
John McCall17001972009-10-18 01:05:36 +0000590void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000591 Record.AddSourceLocation(TL.getLBracketLoc());
592 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000593 Record.push_back(TL.getSizeExpr() ? 1 : 0);
594 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000595 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000596}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000597
John McCall17001972009-10-18 01:05:36 +0000598void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
599 VisitArrayTypeLoc(TL);
600}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000601
John McCall17001972009-10-18 01:05:36 +0000602void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
603 VisitArrayTypeLoc(TL);
604}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000605
John McCall17001972009-10-18 01:05:36 +0000606void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
607 VisitArrayTypeLoc(TL);
608}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000609
John McCall17001972009-10-18 01:05:36 +0000610void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
611 DependentSizedArrayTypeLoc TL) {
612 VisitArrayTypeLoc(TL);
613}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000614
John McCall17001972009-10-18 01:05:36 +0000615void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
616 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000617 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000618}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000619
John McCall17001972009-10-18 01:05:36 +0000620void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000621 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000622}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000623
John McCall17001972009-10-18 01:05:36 +0000624void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000625 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000626}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000627
John McCall17001972009-10-18 01:05:36 +0000628void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000629 Record.AddSourceLocation(TL.getLocalRangeBegin());
630 Record.AddSourceLocation(TL.getLParenLoc());
631 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000632 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000633 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000634 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000635 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000636}
637void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
638 VisitFunctionTypeLoc(TL);
639}
640void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
641 VisitFunctionTypeLoc(TL);
642}
John McCallb96ec562009-12-04 22:46:56 +0000643void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000644 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000645}
John McCall17001972009-10-18 01:05:36 +0000646void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000647 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000648}
Manman Rene6be26c2016-09-13 17:25:08 +0000649void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
650 if (TL.getNumProtocols()) {
651 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
652 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
653 }
654 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
655 Record.AddSourceLocation(TL.getProtocolLoc(i));
656}
John McCall17001972009-10-18 01:05:36 +0000657void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000658 Record.AddSourceLocation(TL.getTypeofLoc());
659 Record.AddSourceLocation(TL.getLParenLoc());
660 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000661}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000662
John McCall17001972009-10-18 01:05:36 +0000663void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000664 Record.AddSourceLocation(TL.getTypeofLoc());
665 Record.AddSourceLocation(TL.getLParenLoc());
666 Record.AddSourceLocation(TL.getRParenLoc());
667 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000668}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000669
John McCall17001972009-10-18 01:05:36 +0000670void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000671 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000672}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000673
Alexis Hunte852b102011-05-24 22:41:36 +0000674void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000675 Record.AddSourceLocation(TL.getKWLoc());
676 Record.AddSourceLocation(TL.getLParenLoc());
677 Record.AddSourceLocation(TL.getRParenLoc());
678 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000679}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000680
Richard Smith30482bc2011-02-20 03:19:35 +0000681void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000682 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000683}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000684
John McCall17001972009-10-18 01:05:36 +0000685void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000686 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000687}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000688
John McCall17001972009-10-18 01:05:36 +0000689void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000690 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000691}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000692
John McCall81904512011-01-06 01:58:22 +0000693void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000694 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000695 if (TL.hasAttrOperand()) {
696 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000697 Record.AddSourceLocation(range.getBegin());
698 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000699 }
700 if (TL.hasAttrExprOperand()) {
701 Expr *operand = TL.getAttrExprOperand();
702 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000703 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000704 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000705 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000706 }
707}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000708
John McCall17001972009-10-18 01:05:36 +0000709void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000710 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000711}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000712
John McCallcebee162009-10-18 09:09:24 +0000713void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
714 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000715 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000716}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000717
Douglas Gregorada4b792011-01-14 02:55:32 +0000718void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
719 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000720 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000721}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000722
John McCall17001972009-10-18 01:05:36 +0000723void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
724 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000725 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
726 Record.AddSourceLocation(TL.getTemplateNameLoc());
727 Record.AddSourceLocation(TL.getLAngleLoc());
728 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000729 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000730 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
731 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000732}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000733
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000734void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000735 Record.AddSourceLocation(TL.getLParenLoc());
736 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000737}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000738
Abramo Bagnara6150c882010-05-11 21:36:43 +0000739void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000740 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
741 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000742}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000743
John McCalle78aac42010-03-10 03:28:59 +0000744void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000745 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000746}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000747
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000748void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000749 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
750 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
751 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000752}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000753
John McCallc392f372010-06-11 00:33:02 +0000754void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
755 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000756 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
757 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
758 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
759 Record.AddSourceLocation(TL.getTemplateNameLoc());
760 Record.AddSourceLocation(TL.getLAngleLoc());
761 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000762 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000763 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
764 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000765}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000766
Douglas Gregord2fa7662010-12-20 02:24:11 +0000767void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000768 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000769}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000770
John McCall17001972009-10-18 01:05:36 +0000771void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000772 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000773}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000774
John McCall8b07ec22010-05-15 11:32:37 +0000775void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
776 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000777 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
778 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000779 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000780 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
781 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
782 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000783 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000784 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000785}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000786
John McCallfc93cf92009-10-22 22:37:11 +0000787void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000788 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000789}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000790
Eli Friedman0dfb8892011-10-06 23:00:33 +0000791void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000792 Record.AddSourceLocation(TL.getKWLoc());
793 Record.AddSourceLocation(TL.getLParenLoc());
794 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000795}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000796
Xiuli Pan9c14e282016-01-09 12:53:17 +0000797void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000798 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000799}
John McCall8f115c62009-10-16 21:56:05 +0000800
Richard Smith01b2cb42014-07-26 06:37:51 +0000801void ASTWriter::WriteTypeAbbrevs() {
802 using namespace llvm;
803
David Blaikieb44f0bf2017-01-04 22:36:43 +0000804 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000805
806 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000807 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000808 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
809 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
810 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000811 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000812
813 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000814 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000815 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
816 // FunctionType
817 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
818 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
819 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
820 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
821 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
822 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
823 // FunctionProtoType
824 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
825 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
826 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
827 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
828 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
829 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
830 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
831 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000832 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000833}
834
Chris Lattner19cea4e2009-04-22 05:57:30 +0000835//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000836// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000837//===----------------------------------------------------------------------===//
838
Chris Lattner28fa4e62009-04-26 22:26:21 +0000839static void EmitBlockID(unsigned ID, const char *Name,
840 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000841 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000842 Record.clear();
843 Record.push_back(ID);
844 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
845
846 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000847 if (!Name || Name[0] == 0)
848 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000849 Record.clear();
850 while (*Name)
851 Record.push_back(*Name++);
852 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
853}
854
855static void EmitRecordID(unsigned ID, const char *Name,
856 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000857 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000858 Record.clear();
859 Record.push_back(ID);
860 while (*Name)
861 Record.push_back(*Name++);
862 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000863}
864
865static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000866 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000867#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000868 RECORD(STMT_STOP);
869 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000870 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000871 RECORD(STMT_NULL);
872 RECORD(STMT_COMPOUND);
873 RECORD(STMT_CASE);
874 RECORD(STMT_DEFAULT);
875 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000876 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000877 RECORD(STMT_IF);
878 RECORD(STMT_SWITCH);
879 RECORD(STMT_WHILE);
880 RECORD(STMT_DO);
881 RECORD(STMT_FOR);
882 RECORD(STMT_GOTO);
883 RECORD(STMT_INDIRECT_GOTO);
884 RECORD(STMT_CONTINUE);
885 RECORD(STMT_BREAK);
886 RECORD(STMT_RETURN);
887 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000888 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000889 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000890 RECORD(EXPR_PREDEFINED);
891 RECORD(EXPR_DECL_REF);
892 RECORD(EXPR_INTEGER_LITERAL);
893 RECORD(EXPR_FLOATING_LITERAL);
894 RECORD(EXPR_IMAGINARY_LITERAL);
895 RECORD(EXPR_STRING_LITERAL);
896 RECORD(EXPR_CHARACTER_LITERAL);
897 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000898 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000899 RECORD(EXPR_UNARY_OPERATOR);
900 RECORD(EXPR_SIZEOF_ALIGN_OF);
901 RECORD(EXPR_ARRAY_SUBSCRIPT);
902 RECORD(EXPR_CALL);
903 RECORD(EXPR_MEMBER);
904 RECORD(EXPR_BINARY_OPERATOR);
905 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
906 RECORD(EXPR_CONDITIONAL_OPERATOR);
907 RECORD(EXPR_IMPLICIT_CAST);
908 RECORD(EXPR_CSTYLE_CAST);
909 RECORD(EXPR_COMPOUND_LITERAL);
910 RECORD(EXPR_EXT_VECTOR_ELEMENT);
911 RECORD(EXPR_INIT_LIST);
912 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000913 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000914 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000915 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000916 RECORD(EXPR_VA_ARG);
917 RECORD(EXPR_ADDR_LABEL);
918 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000919 RECORD(EXPR_CHOOSE);
920 RECORD(EXPR_GNU_NULL);
921 RECORD(EXPR_SHUFFLE_VECTOR);
922 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000923 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000924 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000925 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000926 RECORD(EXPR_OBJC_ARRAY_LITERAL);
927 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000928 RECORD(EXPR_OBJC_ENCODE);
929 RECORD(EXPR_OBJC_SELECTOR_EXPR);
930 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
931 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
932 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
933 RECORD(EXPR_OBJC_KVC_REF_EXPR);
934 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000935 RECORD(STMT_OBJC_FOR_COLLECTION);
936 RECORD(STMT_OBJC_CATCH);
937 RECORD(STMT_OBJC_FINALLY);
938 RECORD(STMT_OBJC_AT_TRY);
939 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
940 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000941 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000942 RECORD(STMT_CXX_CATCH);
943 RECORD(STMT_CXX_TRY);
944 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000945 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000946 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000947 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000948 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000949 RECORD(EXPR_CXX_STATIC_CAST);
950 RECORD(EXPR_CXX_DYNAMIC_CAST);
951 RECORD(EXPR_CXX_REINTERPRET_CAST);
952 RECORD(EXPR_CXX_CONST_CAST);
953 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000954 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000955 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000956 RECORD(EXPR_CXX_BOOL_LITERAL);
957 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000958 RECORD(EXPR_CXX_TYPEID_EXPR);
959 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000960 RECORD(EXPR_CXX_THIS);
961 RECORD(EXPR_CXX_THROW);
962 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000963 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000964 RECORD(EXPR_CXX_BIND_TEMPORARY);
965 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
966 RECORD(EXPR_CXX_NEW);
967 RECORD(EXPR_CXX_DELETE);
968 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
969 RECORD(EXPR_EXPR_WITH_CLEANUPS);
970 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
971 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
972 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
973 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
974 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000975 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000976 RECORD(EXPR_CXX_NOEXCEPT);
977 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000978 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
979 RECORD(EXPR_TYPE_TRAIT);
980 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000981 RECORD(EXPR_PACK_EXPANSION);
982 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000983 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000984 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000985 RECORD(EXPR_FUNCTION_PARM_PACK);
986 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000987 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000988 RECORD(EXPR_CXX_UUIDOF_EXPR);
989 RECORD(EXPR_CXX_UUIDOF_TYPE);
990 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000991#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000992}
Mike Stump11289f42009-09-09 15:08:12 +0000993
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000994void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000995 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000996 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +0000997
Sebastian Redl539c5062010-08-18 23:57:32 +0000998#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
999#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001000
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001001 // Control Block.
1002 BLOCK(CONTROL_BLOCK);
1003 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +00001004 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001005 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001006 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001007 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001008 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001009 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001010 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001011 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001012 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001013
1014 BLOCK(OPTIONS_BLOCK);
1015 RECORD(LANGUAGE_OPTIONS);
1016 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001017 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001018 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001019 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001020 RECORD(PREPROCESSOR_OPTIONS);
1021
Douglas Gregor108cb222012-10-19 00:45:00 +00001022 BLOCK(INPUT_FILES_BLOCK);
1023 RECORD(INPUT_FILE);
1024
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001025 // AST Top-Level Block.
1026 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001027 RECORD(TYPE_OFFSET);
1028 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001029 RECORD(IDENTIFIER_OFFSET);
1030 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001031 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001032 RECORD(SPECIAL_TYPES);
1033 RECORD(STATISTICS);
1034 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001035 RECORD(SELECTOR_OFFSETS);
1036 RECORD(METHOD_POOL);
1037 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001038 RECORD(SOURCE_LOCATION_OFFSETS);
1039 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001040 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001041 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001042 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001043 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001044 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001045 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001046 RECORD(SEMA_DECL_REFS);
1047 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1048 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001049 RECORD(UPDATE_VISIBLE);
1050 RECORD(DECL_UPDATE_OFFSETS);
1051 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001052 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001053 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001054 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001055 RECORD(FP_PRAGMA_OPTIONS);
1056 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001057 RECORD(OPENCL_EXTENSION_TYPES);
1058 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001059 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001060 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001061 RECORD(MODULE_OFFSET_MAP);
1062 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001063 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001064 RECORD(FILE_SORTED_DECLS);
1065 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001066 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001067 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001068 RECORD(INTERESTING_IDENTIFIERS);
1069 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001070 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001071 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001072 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001073 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001074 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001075 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001076 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001077
Chris Lattner28fa4e62009-04-26 22:26:21 +00001078 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001079 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001080 RECORD(SM_SLOC_FILE_ENTRY);
1081 RECORD(SM_SLOC_BUFFER_ENTRY);
1082 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001083 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001084 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001085
Chris Lattner28fa4e62009-04-26 22:26:21 +00001086 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001087 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001088 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001089 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001090 RECORD(PP_MACRO_OBJECT_LIKE);
1091 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001092 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001093
Richard Smithfa715652015-08-31 22:43:10 +00001094 // Submodule Block.
1095 BLOCK(SUBMODULE_BLOCK);
1096 RECORD(SUBMODULE_METADATA);
1097 RECORD(SUBMODULE_DEFINITION);
1098 RECORD(SUBMODULE_UMBRELLA_HEADER);
1099 RECORD(SUBMODULE_HEADER);
1100 RECORD(SUBMODULE_TOPHEADER);
1101 RECORD(SUBMODULE_UMBRELLA_DIR);
1102 RECORD(SUBMODULE_IMPORTS);
1103 RECORD(SUBMODULE_EXPORTS);
1104 RECORD(SUBMODULE_REQUIRES);
1105 RECORD(SUBMODULE_EXCLUDED_HEADER);
1106 RECORD(SUBMODULE_LINK_LIBRARY);
1107 RECORD(SUBMODULE_CONFIG_MACRO);
1108 RECORD(SUBMODULE_CONFLICT);
1109 RECORD(SUBMODULE_PRIVATE_HEADER);
1110 RECORD(SUBMODULE_TEXTUAL_HEADER);
1111 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001112 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001113
1114 // Comments Block.
1115 BLOCK(COMMENTS_BLOCK);
1116 RECORD(COMMENTS_RAW_COMMENT);
1117
Douglas Gregor12bfa382009-10-17 00:13:19 +00001118 // Decls and Types block.
1119 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001120 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001121 RECORD(TYPE_COMPLEX);
1122 RECORD(TYPE_POINTER);
1123 RECORD(TYPE_BLOCK_POINTER);
1124 RECORD(TYPE_LVALUE_REFERENCE);
1125 RECORD(TYPE_RVALUE_REFERENCE);
1126 RECORD(TYPE_MEMBER_POINTER);
1127 RECORD(TYPE_CONSTANT_ARRAY);
1128 RECORD(TYPE_INCOMPLETE_ARRAY);
1129 RECORD(TYPE_VARIABLE_ARRAY);
1130 RECORD(TYPE_VECTOR);
1131 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001132 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001133 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001134 RECORD(TYPE_TYPEDEF);
1135 RECORD(TYPE_TYPEOF_EXPR);
1136 RECORD(TYPE_TYPEOF);
1137 RECORD(TYPE_RECORD);
1138 RECORD(TYPE_ENUM);
1139 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001140 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001141 RECORD(TYPE_DECLTYPE);
1142 RECORD(TYPE_ELABORATED);
1143 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1144 RECORD(TYPE_UNRESOLVED_USING);
1145 RECORD(TYPE_INJECTED_CLASS_NAME);
1146 RECORD(TYPE_OBJC_OBJECT);
1147 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1148 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1149 RECORD(TYPE_DEPENDENT_NAME);
1150 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1151 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1152 RECORD(TYPE_PAREN);
1153 RECORD(TYPE_PACK_EXPANSION);
1154 RECORD(TYPE_ATTRIBUTED);
1155 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001156 RECORD(TYPE_AUTO);
1157 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001158 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001159 RECORD(TYPE_DECAYED);
1160 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001161 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001162 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001163 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001164 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001165 RECORD(DECL_ENUM);
1166 RECORD(DECL_RECORD);
1167 RECORD(DECL_ENUM_CONSTANT);
1168 RECORD(DECL_FUNCTION);
1169 RECORD(DECL_OBJC_METHOD);
1170 RECORD(DECL_OBJC_INTERFACE);
1171 RECORD(DECL_OBJC_PROTOCOL);
1172 RECORD(DECL_OBJC_IVAR);
1173 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001174 RECORD(DECL_OBJC_CATEGORY);
1175 RECORD(DECL_OBJC_CATEGORY_IMPL);
1176 RECORD(DECL_OBJC_IMPLEMENTATION);
1177 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1178 RECORD(DECL_OBJC_PROPERTY);
1179 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001180 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001181 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001182 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001183 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001184 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001185 RECORD(DECL_FILE_SCOPE_ASM);
1186 RECORD(DECL_BLOCK);
1187 RECORD(DECL_CONTEXT_LEXICAL);
1188 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001189 RECORD(DECL_NAMESPACE);
1190 RECORD(DECL_NAMESPACE_ALIAS);
1191 RECORD(DECL_USING);
1192 RECORD(DECL_USING_SHADOW);
1193 RECORD(DECL_USING_DIRECTIVE);
1194 RECORD(DECL_UNRESOLVED_USING_VALUE);
1195 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1196 RECORD(DECL_LINKAGE_SPEC);
1197 RECORD(DECL_CXX_RECORD);
1198 RECORD(DECL_CXX_METHOD);
1199 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001200 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001201 RECORD(DECL_CXX_DESTRUCTOR);
1202 RECORD(DECL_CXX_CONVERSION);
1203 RECORD(DECL_ACCESS_SPEC);
1204 RECORD(DECL_FRIEND);
1205 RECORD(DECL_FRIEND_TEMPLATE);
1206 RECORD(DECL_CLASS_TEMPLATE);
1207 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1208 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001209 RECORD(DECL_VAR_TEMPLATE);
1210 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1211 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001212 RECORD(DECL_FUNCTION_TEMPLATE);
1213 RECORD(DECL_TEMPLATE_TYPE_PARM);
1214 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1215 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001216 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001217 RECORD(DECL_STATIC_ASSERT);
1218 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001219 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001220 RECORD(DECL_INDIRECTFIELD);
1221 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001222 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1223 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1224 RECORD(DECL_IMPORT);
1225 RECORD(DECL_OMP_THREADPRIVATE);
1226 RECORD(DECL_EMPTY);
1227 RECORD(DECL_OBJC_TYPE_PARAM);
1228 RECORD(DECL_OMP_CAPTUREDEXPR);
1229 RECORD(DECL_PRAGMA_COMMENT);
1230 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1231 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001232
Douglas Gregor03412ba2011-06-03 02:27:19 +00001233 // Statements and Exprs can occur in the Decls and Types block.
1234 AddStmtsExprs(Stream, Record);
1235
Douglas Gregor92a96f52011-02-08 21:58:10 +00001236 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001237 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001238 RECORD(PPD_MACRO_DEFINITION);
1239 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001240
1241 // Decls and Types block.
1242 BLOCK(EXTENSION_BLOCK);
1243 RECORD(EXTENSION_METADATA);
1244
Chris Lattner28fa4e62009-04-26 22:26:21 +00001245#undef RECORD
1246#undef BLOCK
1247 Stream.ExitBlock();
1248}
1249
Richard Smith54cc3c22014-12-11 20:50:24 +00001250/// \brief Prepares a path for being written to an AST file by converting it
1251/// to an absolute path and removing nested './'s.
1252///
1253/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001254static bool cleanPathForOutput(FileManager &FileMgr,
1255 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001256 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001257 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001258}
1259
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001260/// \brief Adjusts the given filename to only write out the portion of the
1261/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001262///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001263/// \param Filename the file name to adjust.
1264///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001265/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1266/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001267///
1268/// \returns either the original filename (if it needs no adjustment) or the
1269/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001270static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001271adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001272 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001273
Richard Smith7ed1bc92014-12-05 22:42:13 +00001274 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001275 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001276
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001277 // Verify that the filename and the system root have the same prefix.
1278 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001279 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1280 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001281 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001282
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001283 // We hit the end of the filename before we hit the end of the system root.
1284 if (!Filename[Pos])
1285 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001286
Richard Smith7ed1bc92014-12-05 22:42:13 +00001287 // If there's not a path separator at the end of the base directory nor
1288 // immediately after it, then this isn't within the base directory.
1289 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1290 if (!llvm::sys::path::is_separator(BaseDir.back()))
1291 return Filename;
1292 } else {
1293 // If the file name has a '/' at the current position, skip over the '/'.
1294 // We distinguish relative paths from absolute paths by the
1295 // absence of '/' at the beginning of relative paths.
1296 //
1297 // FIXME: This is wrong. We distinguish them by asking if the path is
1298 // absolute, which isn't the same thing. And there might be multiple '/'s
1299 // in a row. Use a better mechanism to indicate whether we have emitted an
1300 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001301 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001302 }
Mike Stump11289f42009-09-09 15:08:12 +00001303
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001304 return Filename + Pos;
1305}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001306
Ben Langmuir487ea142014-10-23 18:05:36 +00001307static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001308 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001309 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1310 return S;
1311 // Rely on GetRandomNumber to eventually return non-zero...
1312 }
1313}
1314
Douglas Gregor112b9072012-10-18 05:31:06 +00001315/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001316uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1317 ASTContext &Context,
1318 StringRef isysroot,
1319 const std::string &OutputFile) {
1320 ASTFileSignature Signature = 0;
1321
Douglas Gregorbfbde532009-04-10 21:16:55 +00001322 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001323 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001324 RecordData Record;
1325
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001326 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001327 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001328 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1329 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1330 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1331 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1332 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1333 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001334 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001335 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1336 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001337 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001338 assert((!WritingModule || isysroot.empty()) &&
1339 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001340 {
1341 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1342 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1343 !isysroot.empty(), IncludeTimestamps,
1344 ASTHasCompilerErrors};
1345 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1346 getClangFullRepositoryVersion());
1347 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001348 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001349 // For implicit modules we output a signature that we can use to ensure
1350 // duplicate module builds don't collide in the cache as their output order
1351 // is non-deterministic.
1352 // FIXME: Remove this when output is deterministic.
1353 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001354 Signature = getSignature();
1355 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001356 Stream.EmitRecord(SIGNATURE, Record);
1357 }
1358
Richard Smith7ed1bc92014-12-05 22:42:13 +00001359 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001360 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001361 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001363 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001364 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001365 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1366 }
1367
Richard Smith7ed1bc92014-12-05 22:42:13 +00001368 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001369 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001370 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001371
1372 // If the home of the module is the current working directory, then we
1373 // want to pick up the cwd of the build process loading the module, not
1374 // our cwd, when we load this module.
1375 if (!PP.getHeaderSearchInfo()
1376 .getHeaderSearchOpts()
1377 .ModuleMapFileHomeIsCwd ||
1378 WritingModule->Directory->getName() != StringRef(".")) {
1379 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001380 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001381 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001383 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001384
Mehdi Amini57a41912015-09-10 01:46:39 +00001385 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001386 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1387 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001388
1389 // Write out all other paths relative to the base directory if possible.
1390 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1391 } else if (!isysroot.empty()) {
1392 // Write out paths relative to the sysroot if possible.
1393 BaseDirectory = isysroot;
1394 }
1395
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001396 // Module map file
1397 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001398 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001399
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001400 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1401
1402 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001403 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001404
1405 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001406 if (auto *AdditionalModMaps =
1407 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001408 Record.push_back(AdditionalModMaps->size());
1409 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001410 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001411 } else {
1412 Record.push_back(0);
1413 }
1414
1415 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001416 }
1417
Douglas Gregor112b9072012-10-18 05:31:06 +00001418 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001419 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001420 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001421 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001422
Richard Smith7f330cd2015-03-18 01:42:29 +00001423 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001424 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001425 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001426 continue;
1427
Richard Smith7f330cd2015-03-18 01:42:29 +00001428 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1429 AddSourceLocation(M->ImportLoc, Record);
1430 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001431 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001432 Record.push_back(M->Signature);
1433 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001434 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001435 Stream.EmitRecord(IMPORTS, Record);
1436 }
Mike Stump11289f42009-09-09 15:08:12 +00001437
Richard Smith0516b182015-09-08 19:40:14 +00001438 // Write the options block.
1439 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1440
Douglas Gregor112b9072012-10-18 05:31:06 +00001441 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001442 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001443 const LangOptions &LangOpts = Context.getLangOpts();
1444#define LANGOPT(Name, Bits, Default, Description) \
1445 Record.push_back(LangOpts.Name);
1446#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1447 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001448#include "clang/Basic/LangOptions.def"
1449#define SANITIZER(NAME, ID) \
1450 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001451#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001452
Ben Langmuircd98cb72015-06-23 18:20:18 +00001453 Record.push_back(LangOpts.ModuleFeatures.size());
1454 for (StringRef Feature : LangOpts.ModuleFeatures)
1455 AddString(Feature, Record);
1456
Douglas Gregor112b9072012-10-18 05:31:06 +00001457 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1458 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001459
1460 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001461
1462 // Comment options.
1463 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001464 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1465 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001466 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001467 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001468
Samuel Antaoee8fb302016-01-06 13:42:12 +00001469 // OpenMP offloading options.
1470 Record.push_back(LangOpts.OMPTargetTriples.size());
1471 for (auto &T : LangOpts.OMPTargetTriples)
1472 AddString(T.getTriple(), Record);
1473
1474 AddString(LangOpts.OMPHostIRFile, Record);
1475
Douglas Gregor112b9072012-10-18 05:31:06 +00001476 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1477
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001478 // Target options.
1479 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001480 const TargetInfo &Target = Context.getTargetInfo();
1481 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001482 AddString(TargetOpts.Triple, Record);
1483 AddString(TargetOpts.CPU, Record);
1484 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001485 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1486 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1487 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1488 }
1489 Record.push_back(TargetOpts.Features.size());
1490 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1491 AddString(TargetOpts.Features[I], Record);
1492 }
1493 Stream.EmitRecord(TARGET_OPTIONS, Record);
1494
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001495 // Diagnostic options.
1496 Record.clear();
1497 const DiagnosticOptions &DiagOpts
1498 = Context.getDiagnostics().getDiagnosticOptions();
1499#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1500#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1501 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1502#include "clang/Basic/DiagnosticOptions.def"
1503 Record.push_back(DiagOpts.Warnings.size());
1504 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1505 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001506 Record.push_back(DiagOpts.Remarks.size());
1507 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1508 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001509 // Note: we don't serialize the log or serialization file names, because they
1510 // are generally transient files and will almost always be overridden.
1511 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1512
Douglas Gregorc6317db2012-10-24 15:49:58 +00001513 // File system options.
1514 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001515 const FileSystemOptions &FSOpts =
1516 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001517 AddString(FSOpts.WorkingDir, Record);
1518 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1519
Douglas Gregor2d302362012-10-24 16:50:34 +00001520 // Header search options.
1521 Record.clear();
1522 const HeaderSearchOptions &HSOpts
1523 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1524 AddString(HSOpts.Sysroot, Record);
1525
1526 // Include entries.
1527 Record.push_back(HSOpts.UserEntries.size());
1528 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1529 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1530 AddString(Entry.Path, Record);
1531 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001532 Record.push_back(Entry.IsFramework);
1533 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001534 }
1535
1536 // System header prefixes.
1537 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1538 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1539 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1540 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1541 }
1542
1543 AddString(HSOpts.ResourceDir, Record);
1544 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001545 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001546 Record.push_back(HSOpts.DisableModuleHash);
1547 Record.push_back(HSOpts.UseBuiltinIncludes);
1548 Record.push_back(HSOpts.UseStandardSystemIncludes);
1549 Record.push_back(HSOpts.UseStandardCXXIncludes);
1550 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001551 // Write out the specific module cache path that contains the module files.
1552 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001553 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1554
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001555 // Preprocessor options.
1556 Record.clear();
1557 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1558
1559 // Macro definitions.
1560 Record.push_back(PPOpts.Macros.size());
1561 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1562 AddString(PPOpts.Macros[I].first, Record);
1563 Record.push_back(PPOpts.Macros[I].second);
1564 }
1565
1566 // Includes
1567 Record.push_back(PPOpts.Includes.size());
1568 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1569 AddString(PPOpts.Includes[I], Record);
1570
1571 // Macro includes
1572 Record.push_back(PPOpts.MacroIncludes.size());
1573 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1574 AddString(PPOpts.MacroIncludes[I], Record);
1575
Douglas Gregorb6368752012-10-24 23:41:50 +00001576 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001577 // Detailed record is important since it is used for the module cache hash.
1578 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001579 AddString(PPOpts.ImplicitPCHInclude, Record);
1580 AddString(PPOpts.ImplicitPTHInclude, Record);
1581 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1582 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1583
Richard Smith0516b182015-09-08 19:40:14 +00001584 // Leave the options block.
1585 Stream.ExitBlock();
1586
Douglas Gregora3b20262011-05-06 21:43:30 +00001587 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001588 SourceManager &SM = Context.getSourceManager();
1589 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001590 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001591 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1592 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001593 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001594 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001595
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001596 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001597 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001598 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001599 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001600 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001601
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001602 Record.clear();
1603 Record.push_back(SM.getMainFileID().getOpaqueValue());
1604 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1605
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001606 // Original PCH directory
1607 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001608 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001609 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001611 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001612
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001613 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001614
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001615 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001616 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1617
Mehdi Amini57a41912015-09-10 01:46:39 +00001618 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001619 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1620 }
1621
Douglas Gregor49491f72013-03-15 22:15:07 +00001622 WriteInputFiles(Context.SourceMgr,
1623 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001624 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001625 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001626 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001627}
1628
Douglas Gregor49491f72013-03-15 22:15:07 +00001629namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001630
Douglas Gregor49491f72013-03-15 22:15:07 +00001631 /// \brief An input file.
1632 struct InputFileEntry {
1633 const FileEntry *File;
1634 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001635 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001636 bool BufferOverridden;
1637 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001638
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001639} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001640
1641void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1642 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001643 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001644 using namespace llvm;
1645 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001646
Douglas Gregor72be3902012-10-19 00:38:02 +00001647 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001648 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001649 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001650 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001651 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1652 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001653 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001654 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001655 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001656 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001657
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001658 // Get all ContentCache objects for files, sorted by whether the file is a
1659 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001660 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001661 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1662 // Get this source location entry.
1663 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001664 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001665
1666 // We only care about file entries that were not overridden.
1667 if (!SLoc->isFile())
1668 continue;
1669 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001670 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001671 continue;
1672
Douglas Gregor49491f72013-03-15 22:15:07 +00001673 InputFileEntry Entry;
1674 Entry.File = Cache->OrigEntry;
1675 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001676 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001677 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001678 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001679 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001680 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001681 SortedFiles.push_front(Entry);
1682 }
1683
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001684 unsigned UserFilesNum = 0;
1685 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001686 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001687 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001688 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001689 if (InputFileID != 0)
1690 continue; // already recorded this file.
1691
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001692 // Record this entry's offset.
1693 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001694
1695 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001696
Douglas Gregor49491f72013-03-15 22:15:07 +00001697 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001698 ++UserFilesNum;
1699
Douglas Gregor72be3902012-10-19 00:38:02 +00001700 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001701 // And whether this file was overridden.
1702 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001703 INPUT_FILE,
1704 InputFileOffsets.size(),
1705 (uint64_t)Entry.File->getSize(),
1706 (uint64_t)getTimestampForOutput(Entry.File),
1707 Entry.BufferOverridden,
1708 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001709
Richard Smith7ed1bc92014-12-05 22:42:13 +00001710 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1711 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001712
Douglas Gregor112b9072012-10-18 05:31:06 +00001713 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001714
1715 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001716 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001717 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1718 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001719 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1720 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001721 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001722 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001723
1724 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001725 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1726 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001727 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001728}
1729
Douglas Gregora7f71a92009-04-10 03:52:48 +00001730//===----------------------------------------------------------------------===//
1731// Source Manager Serialization
1732//===----------------------------------------------------------------------===//
1733
1734/// \brief Create an abbreviation for the SLocEntry that refers to a
1735/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001736static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001737 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001738
David Blaikieb44f0bf2017-01-04 22:36:43 +00001739 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001740 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001745 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001750 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001751}
1752
1753/// \brief Create an abbreviation for the SLocEntry that refers to a
1754/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001755static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001756 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001757
David Blaikieb44f0bf2017-01-04 22:36:43 +00001758 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001759 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001765 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001766}
1767
1768/// \brief Create an abbreviation for the SLocEntry that refers to a
1769/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001770static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1771 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001772 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001773
David Blaikieb44f0bf2017-01-04 22:36:43 +00001774 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001775 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1776 : SM_SLOC_BUFFER_BLOB));
1777 if (Compressed)
1778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001780 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001781}
1782
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001783/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1784/// expansion.
1785static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001786 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001787
David Blaikieb44f0bf2017-01-04 22:36:43 +00001788 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001789 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001795 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001796}
1797
Douglas Gregor09b69892011-02-10 17:09:37 +00001798namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001799
Douglas Gregor09b69892011-02-10 17:09:37 +00001800 // Trait used for the on-disk hash table of header search information.
1801 class HeaderFileInfoTrait {
1802 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001803 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001804
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001805 // Keep track of the framework names we've used during serialization.
1806 SmallVector<char, 128> FrameworkStringData;
1807 llvm::StringMap<unsigned> FrameworkNameOffset;
1808
Douglas Gregor09b69892011-02-10 17:09:37 +00001809 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001810 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1811 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001812
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001813 struct key_type {
1814 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001815 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001816 };
1817 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001818
1819 typedef HeaderFileInfo data_type;
1820 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001821 typedef unsigned hash_value_type;
1822 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001823
Richard Smithe75ee0f2015-08-17 07:13:32 +00001824 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001825 // The hash is based only on size/time of the file, so that the reader can
1826 // match even when symlinking or excess path elements ("foo/../", "../")
1827 // change the form of the name. However, complete path is still the key.
1828 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001829 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001830 }
1831
1832 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001833 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001834 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001835 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001836 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001837 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001838 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001839 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1840 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1841 DataLen += 4;
1842 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001843 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001844 }
1845
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001846 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001847 using namespace llvm::support;
1848 endian::Writer<little> LE(Out);
1849 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001850 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001851 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001852 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001853 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001854 }
1855
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001856 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001857 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001858 using namespace llvm::support;
1859 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001860 uint64_t Start = Out.tell(); (void)Start;
1861
Richard Smith386bb072015-08-18 23:42:23 +00001862 unsigned char Flags = (Data.isImport << 4)
1863 | (Data.isPragmaOnce << 3)
1864 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001865 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001866 LE.write<uint8_t>(Flags);
1867 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001868
1869 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001870 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001871 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001872 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001873
1874 unsigned Offset = 0;
1875 if (!Data.Framework.empty()) {
1876 // If this header refers into a framework, save the framework name.
1877 llvm::StringMap<unsigned>::iterator Pos
1878 = FrameworkNameOffset.find(Data.Framework);
1879 if (Pos == FrameworkNameOffset.end()) {
1880 Offset = FrameworkStringData.size() + 1;
1881 FrameworkStringData.append(Data.Framework.begin(),
1882 Data.Framework.end());
1883 FrameworkStringData.push_back(0);
1884
1885 FrameworkNameOffset[Data.Framework] = Offset;
1886 } else
1887 Offset = Pos->second;
1888 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001889 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001890
Richard Smith386bb072015-08-18 23:42:23 +00001891 // FIXME: If the header is excluded, we should write out some
1892 // record of that fact.
1893 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1894 if (uint32_t ModID =
1895 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1896 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1897 assert((Value >> 2) == ModID && "overflow in header module info");
1898 LE.write<uint32_t>(Value);
1899 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001900 }
1901
Douglas Gregor09b69892011-02-10 17:09:37 +00001902 assert(Out.tell() - Start == DataLen && "Wrong data length");
1903 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001904
1905 const char *strings_begin() const { return FrameworkStringData.begin(); }
1906 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001907 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001908
Douglas Gregor09b69892011-02-10 17:09:37 +00001909} // end anonymous namespace
1910
1911/// \brief Write the header search block for the list of files that
1912///
1913/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001914void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001915 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001916 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1917
1918 if (FilesByUID.size() > HS.header_file_size())
1919 FilesByUID.resize(HS.header_file_size());
1920
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001921 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001922 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001923 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001924 unsigned NumHeaderSearchEntries = 0;
1925 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1926 const FileEntry *File = FilesByUID[UID];
1927 if (!File)
1928 continue;
1929
Richard Smith386bb072015-08-18 23:42:23 +00001930 // Get the file info. This will load info from the external source if
1931 // necessary. Skip emitting this file if we have no information on it
1932 // as a header file (in which case HFI will be null) or if it hasn't
1933 // changed since it was loaded. Also skip it if it's for a modular header
1934 // from a different module; in that case, we rely on the module(s)
1935 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001936 const HeaderFileInfo *HFI =
1937 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1938 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001939 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001940
Richard Smith7ed1bc92014-12-05 22:42:13 +00001941 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001942 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001943 SmallString<128> FilenameTmp(Filename);
1944 if (PreparePathForOutput(FilenameTmp)) {
1945 // If we performed any translation on the file name at all, we need to
1946 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001947 Filename = StringRef(strdup(FilenameTmp.c_str()));
1948 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001949 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001950
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001951 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001952 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001953 ++NumHeaderSearchEntries;
1954 }
1955
1956 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001957 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001958 uint32_t BucketOffset;
1959 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001960 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001961 llvm::raw_svector_ostream Out(TableData);
1962 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001963 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001964 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1965 }
1966
1967 // Create a blob abbreviation
1968 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001969
David Blaikieb44f0bf2017-01-04 22:36:43 +00001970 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00001971 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00001976 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00001977
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001978 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001979 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1980 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001981 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001982 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001983
1984 // Free all of the strings we had to duplicate.
1985 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001986 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001987}
1988
Douglas Gregora7f71a92009-04-10 03:52:48 +00001989/// \brief Writes the block containing the serialized form of the
1990/// source manager.
1991///
1992/// TODO: We should probably use an on-disk hash table (stored in a
1993/// blob), indexed based on the file name, so that we only create
1994/// entries for files that we actually need. In the common case (no
1995/// errors), we probably won't have to create file entries for any of
1996/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001997void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001998 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001999 RecordData Record;
2000
Chris Lattner0910e3b2009-04-10 17:16:57 +00002001 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002002 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002003
2004 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002005 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2006 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002007 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2008 unsigned SLocBufferBlobCompressedAbbrv =
2009 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002010 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002011
Douglas Gregor258ae542009-04-27 06:38:32 +00002012 // Write out the source location entry table. We skip the first
2013 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002014 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002015 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002016 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2017 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002018 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002019 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002020 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002021 FileID FID = FileID::get(I);
2022 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002023
Douglas Gregor258ae542009-04-27 06:38:32 +00002024 // Record the offset of this source-location entry.
2025 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2026
2027 // Figure out which record code to use.
2028 unsigned Code;
2029 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002030 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2031 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002032 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002033 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002034 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002035 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002036 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002037 Record.clear();
2038 Record.push_back(Code);
2039
Douglas Gregor925296b2011-07-19 16:10:42 +00002040 // Starting offset of this entry within this module, so skip the dummy.
2041 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002042 if (SLoc->isFile()) {
2043 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002044 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002045 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2046 Record.push_back(File.hasLineDirectives());
2047
2048 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002049 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002050 if (Content->OrigEntry) {
2051 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002052 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002053
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002054 // The source location entry is a file. Emit input file ID.
2055 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2056 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002057
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002058 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002059
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002060 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002061 if (FDI != FileDeclIDs.end()) {
2062 Record.push_back(FDI->second->FirstDeclIndex);
2063 Record.push_back(FDI->second->DeclIDs.size());
2064 } else {
2065 Record.push_back(0);
2066 Record.push_back(0);
2067 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002068
2069 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2070
Richard Smithaada85c2016-02-06 02:06:43 +00002071 if (Content->BufferOverridden || Content->IsTransient)
2072 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002073 } else {
2074 // The source location entry is a buffer. The blob associated
2075 // with this entry contains the contents of the buffer.
2076
2077 // We add one to the size so that we capture the trailing NULL
2078 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2079 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002080 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002081 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002082 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002083 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002084 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002085 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002086
Mehdi Amini99d1b292016-10-01 16:38:28 +00002087 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002088 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002089 }
Richard Smithaada85c2016-02-06 02:06:43 +00002090
2091 if (EmitBlob) {
2092 // Include the implicit terminating null character in the on-disk buffer
2093 // if we're writing it uncompressed.
2094 const llvm::MemoryBuffer *Buffer =
2095 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2096 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2097
2098 // Compress the buffer if possible. We expect that almost all PCM
2099 // consumers will not want its contents.
2100 SmallString<0> CompressedBuffer;
2101 if (llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer) ==
2102 llvm::zlib::StatusOK) {
2103 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2104 Blob.size() - 1};
2105 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2106 CompressedBuffer);
2107 } else {
2108 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
2109 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2110 }
2111 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002112 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002113 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002114 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002115 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2116 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2117 AddSourceLocation(Expansion.isMacroArgExpansion()
2118 ? SourceLocation()
2119 : Expansion.getExpansionLocEnd(),
2120 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002121
2122 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002123 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002124 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002125 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002126 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002127 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002128 }
2129 }
2130
Douglas Gregor8f45df52009-04-16 22:23:12 +00002131 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002132
2133 if (SLocEntryOffsets.empty())
2134 return;
2135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002136 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002137 // table is used for lazily loading source-location information.
2138 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002139
David Blaikieb44f0bf2017-01-04 22:36:43 +00002140 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002141 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002145 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002146 {
2147 RecordData::value_type Record[] = {
2148 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2149 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2150 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2151 bytes(SLocEntryOffsets));
2152 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002153 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002154 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002155 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002156
2157 // Write the line table. It depends on remapping working, so it must come
2158 // after the source location offsets.
2159 if (SourceMgr.hasLineTable()) {
2160 LineTableInfo &LineTable = SourceMgr.getLineTable();
2161
2162 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002163
2164 // Emit the needed file names.
2165 llvm::DenseMap<int, int> FilenameMap;
2166 for (const auto &L : LineTable) {
2167 if (L.first.ID < 0)
2168 continue;
2169 for (auto &LE : L.second) {
2170 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2171 FilenameMap.size())).second)
2172 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2173 }
2174 }
2175 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002176
2177 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002178 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002179 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002180 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002181 continue;
2182
2183 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002184 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002185
2186 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002187 Record.push_back(L.second.size());
2188 for (const auto &LE : L.second) {
2189 Record.push_back(LE.FileOffset);
2190 Record.push_back(LE.LineNo);
2191 Record.push_back(FilenameMap[LE.FilenameID]);
2192 Record.push_back((unsigned)LE.FileKind);
2193 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002194 }
2195 }
Richard Smith63078492015-09-01 07:41:55 +00002196
Douglas Gregor925296b2011-07-19 16:10:42 +00002197 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2198 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002199}
2200
Douglas Gregorc5046832009-04-27 18:38:38 +00002201//===----------------------------------------------------------------------===//
2202// Preprocessor Serialization
2203//===----------------------------------------------------------------------===//
2204
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002205static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2206 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002207 if (MacroInfo *MI = MD->getMacroInfo())
2208 if (MI->isBuiltinMacro())
2209 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002210
2211 if (IsModule) {
2212 SourceLocation Loc = MD->getLocation();
2213 if (Loc.isInvalid())
2214 return true;
2215 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2216 return true;
2217 }
2218
2219 return false;
2220}
2221
Chris Lattnereeffaef2009-04-10 17:15:23 +00002222/// \brief Writes the block containing the serialized form of the
2223/// preprocessor.
2224///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002225void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002226 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2227 if (PPRec)
2228 WritePreprocessorDetail(*PPRec);
2229
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002230 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002231 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002232
Chris Lattner0af3ba12009-04-13 01:29:17 +00002233 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2234 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002235 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002236 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002237 }
2238
2239 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002240 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002241
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002242 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002243 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002244 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002245 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002246
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002247 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002248 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002249
Richard Smithee977932015-05-01 21:22:17 +00002250 // Construct the list of identifiers with macro directives that need to be
2251 // serialized.
2252 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2253 for (auto &Id : PP.getIdentifierTable())
2254 if (Id.second->hadMacroDefinition() &&
2255 (!Id.second->isFromAST() ||
2256 Id.second->hasChangedSinceDeserialization()))
2257 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002258 // Sort the set of macro definitions that need to be serialized by the
2259 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002260 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2261 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002262
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002263 // Emit the macro directives as a list and associate the offset with the
2264 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002265 for (const IdentifierInfo *Name : MacroIdentifiers) {
2266 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002267 auto StartOffset = Stream.GetCurrentBitNo();
2268
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002269 // Emit the macro directives in reverse source order.
2270 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002271 // Once we hit an ignored macro, we're done: the rest of the chain
2272 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002273 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002274 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002275
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002276 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002277 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002278 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002279 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002280 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002281 Record.push_back(VisMD->isPublic());
2282 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002283 }
Richard Smithd7329392015-04-21 21:46:32 +00002284
Richard Smithb8b2ed62015-04-23 18:18:26 +00002285 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002286 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002287 // We write out exported module macros for PCH as well.
2288 auto Leafs = PP.getLeafModuleMacros(Name);
2289 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2290 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2291 while (!Worklist.empty()) {
2292 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002293
Manman Ren33d80292016-05-31 18:19:32 +00002294 // Emit a record indicating this submodule exports this macro.
2295 ModuleMacroRecord.push_back(
2296 getSubmoduleID(Macro->getOwningModule()));
2297 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2298 for (auto *M : Macro->overrides())
2299 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002300
Manman Ren33d80292016-05-31 18:19:32 +00002301 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2302 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002303
Manman Ren33d80292016-05-31 18:19:32 +00002304 // Enqueue overridden macros once we've visited all their ancestors.
2305 for (auto *M : Macro->overrides())
2306 if (++Visits[M] == M->getNumOverridingMacros())
2307 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002308
Manman Ren33d80292016-05-31 18:19:32 +00002309 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002310 }
Richard Smithd7329392015-04-21 21:46:32 +00002311
Richard Smithee977932015-05-01 21:22:17 +00002312 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002313 continue;
2314
Richard Smithd7329392015-04-21 21:46:32 +00002315 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002316 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2317 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002318 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002319
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002320 /// \brief Offsets of each of the macros into the bitstream, indexed by
2321 /// the local macro ID
2322 ///
2323 /// For each identifier that is associated with a macro, this map
2324 /// provides the offset into the bitstream where that macro is
2325 /// defined.
2326 std::vector<uint32_t> MacroOffsets;
2327
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002328 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2329 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2330 MacroInfo *MI = MacroInfosToEmit[I].MI;
2331 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002332
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002333 if (ID < FirstMacroID) {
2334 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2335 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002336 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002337
2338 // Record the local offset of this macro.
2339 unsigned Index = ID - FirstMacroID;
2340 if (Index == MacroOffsets.size())
2341 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2342 else {
2343 if (Index > MacroOffsets.size())
2344 MacroOffsets.resize(Index + 1);
2345
2346 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2347 }
2348
2349 AddIdentifierRef(Name, Record);
2350 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2351 AddSourceLocation(MI->getDefinitionLoc(), Record);
2352 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2353 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002354 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002355 unsigned Code;
2356 if (MI->isObjectLike()) {
2357 Code = PP_MACRO_OBJECT_LIKE;
2358 } else {
2359 Code = PP_MACRO_FUNCTION_LIKE;
2360
2361 Record.push_back(MI->isC99Varargs());
2362 Record.push_back(MI->isGNUVarargs());
2363 Record.push_back(MI->hasCommaPasting());
2364 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002365 for (const IdentifierInfo *Arg : MI->args())
2366 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002367 }
2368
2369 // If we have a detailed preprocessing record, record the macro definition
2370 // ID that corresponds to this macro.
2371 if (PPRec)
2372 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2373
2374 Stream.EmitRecord(Code, Record);
2375 Record.clear();
2376
2377 // Emit the tokens array.
2378 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2379 // Note that we know that the preprocessor does not have any annotation
2380 // tokens in it because they are created by the parser, and thus can't
2381 // be in a macro definition.
2382 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002383 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002384 Stream.EmitRecord(PP_TOKEN, Record);
2385 Record.clear();
2386 }
2387 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002388 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002389
Douglas Gregor92a96f52011-02-08 21:58:10 +00002390 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002391
2392 // Write the offsets table for macro IDs.
2393 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002394
David Blaikieb44f0bf2017-01-04 22:36:43 +00002395 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002396 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2400
David Blaikieb44f0bf2017-01-04 22:36:43 +00002401 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002402 {
2403 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2404 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2405 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2406 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002407}
2408
2409void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002410 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002411 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002412
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002413 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002414
Douglas Gregor92a96f52011-02-08 21:58:10 +00002415 // Enter the preprocessor block.
2416 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002417
Douglas Gregoraae92242010-03-19 21:51:54 +00002418 // If the preprocessor has a preprocessing record, emit it.
2419 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002420 using namespace llvm;
2421
2422 // Set up the abbreviation for
2423 unsigned InclusionAbbrev = 0;
2424 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002425 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002426 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002432 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002433 }
2434
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002435 unsigned FirstPreprocessorEntityID
2436 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2437 + NUM_PREDEF_PP_ENTITY_IDS;
2438 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002439 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002440 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2441 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002442 E != EEnd;
2443 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002444 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002445
Richard Smith66a81862015-05-04 02:25:31 +00002446 PreprocessedEntityOffsets.push_back(
2447 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002448
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002449 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002450 // Record this macro definition's ID.
2451 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002452
Douglas Gregor92a96f52011-02-08 21:58:10 +00002453 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002454 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2455 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002456 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002457
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002458 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002459 Record.push_back(ME->isBuiltinMacro());
2460 if (ME->isBuiltinMacro())
2461 AddIdentifierRef(ME->getName(), Record);
2462 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002463 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002464 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002465 continue;
2466 }
2467
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002468 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002469 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002470 Record.push_back(ID->getFileName().size());
2471 Record.push_back(ID->wasInQuotes());
2472 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002473 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002474 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002475 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002476 // Check that the FileEntry is not null because it was not resolved and
2477 // we create a PCH even with compiler errors.
2478 if (ID->getFile())
2479 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002480 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002481 continue;
2482 }
2483
2484 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2485 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002486 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002487
Douglas Gregoraae92242010-03-19 21:51:54 +00002488 // Write the offsets table for the preprocessing record.
2489 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002490 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2491
Douglas Gregoraae92242010-03-19 21:51:54 +00002492 // Write the offsets table for identifier IDs.
2493 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002494
David Blaikieb44f0bf2017-01-04 22:36:43 +00002495 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002496 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002499 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002500
Mehdi Amini57a41912015-09-10 01:46:39 +00002501 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2502 FirstPreprocessorEntityID -
2503 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002504 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002505 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002506 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002507}
2508
Richard Smith386bb072015-08-18 23:42:23 +00002509unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002510 if (!Mod)
2511 return 0;
2512
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002513 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2514 if (Known != SubmoduleIDs.end())
2515 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002516
Richard Smithdc1f0422016-07-20 19:10:16 +00002517 auto *Top = Mod->getTopLevelModule();
2518 if (Top != WritingModule &&
2519 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule)))
Richard Smith386bb072015-08-18 23:42:23 +00002520 return 0;
2521
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002522 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2523}
2524
Richard Smith386bb072015-08-18 23:42:23 +00002525unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2526 // FIXME: This can easily happen, if we have a reference to a submodule that
2527 // did not result in us loading a module file for that submodule. For
2528 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2529 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2530 assert((ID || !Mod) &&
2531 "asked for module ID for non-local, non-imported module");
2532 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002533}
2534
Douglas Gregor253eefe2011-12-01 00:59:36 +00002535/// \brief Compute the number of modules within the given tree (including the
2536/// given module).
2537static unsigned getNumberOfModules(Module *Mod) {
2538 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002539 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002540 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002541 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002542
2543 return ChildModules + 1;
2544}
2545
Douglas Gregorde3ef502011-11-30 23:21:26 +00002546void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002547 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002548 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002549
2550 // Write the abbreviations needed for the submodules block.
2551 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002552
David Blaikieb44f0bf2017-01-04 22:36:43 +00002553 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002554 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002566 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002567
David Blaikieb44f0bf2017-01-04 22:36:43 +00002568 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002569 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002571 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002572
David Blaikieb44f0bf2017-01-04 22:36:43 +00002573 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002574 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002576 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002577
David Blaikieb44f0bf2017-01-04 22:36:43 +00002578 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002579 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002581 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002582
David Blaikieb44f0bf2017-01-04 22:36:43 +00002583 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002584 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002586 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002587
David Blaikieb44f0bf2017-01-04 22:36:43 +00002588 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002589 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002592 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002593
David Blaikieb44f0bf2017-01-04 22:36:43 +00002594 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002595 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2596 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002597 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002598
David Blaikieb44f0bf2017-01-04 22:36:43 +00002599 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002600 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002602 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002603
David Blaikieb44f0bf2017-01-04 22:36:43 +00002604 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002605 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002607 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002608
David Blaikieb44f0bf2017-01-04 22:36:43 +00002609 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002610 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002612 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002613
David Blaikieb44f0bf2017-01-04 22:36:43 +00002614 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002615 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002618 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002619
David Blaikieb44f0bf2017-01-04 22:36:43 +00002620 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002621 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002623 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002624
David Blaikieb44f0bf2017-01-04 22:36:43 +00002625 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002626 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002629 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002630
Douglas Gregor253eefe2011-12-01 00:59:36 +00002631 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002632 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2633 FirstSubmoduleID -
2634 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002635 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2636
Douglas Gregor69021972011-11-30 17:33:56 +00002637 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002638 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002639 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002640 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002641 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002642 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002643 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002644
2645 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002646 if (Mod->Parent) {
2647 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002648 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002649 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002650
2651 // Emit the definition of the block.
2652 {
2653 RecordData::value_type Record[] = {
2654 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2655 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2656 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2657 Mod->ConfigMacrosExhaustive};
2658 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2659 }
2660
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002661 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002662 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002663 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002664 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002665 }
2666
Douglas Gregor69021972011-11-30 17:33:56 +00002667 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002668 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002669 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002670 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2671 UmbrellaHeader.NameAsWritten);
2672 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002673 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2674 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002675 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002676 }
Richard Smith306d8922014-10-22 23:50:56 +00002677
Douglas Gregor69021972011-11-30 17:33:56 +00002678 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002679 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002680 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002681 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002682 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002683 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002684 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2685 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2686 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002687 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002688 Module::HK_PrivateTextual},
2689 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002690 };
2691 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002692 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002693 for (auto &H : Mod->Headers[HL.HeaderKind])
2694 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2695 }
2696
2697 // Emit the top headers.
2698 {
2699 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002700 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002701 for (auto *H : TopHeaders)
2702 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002703 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002704
2705 // Emit the imports.
2706 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002707 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002708 for (auto *I : Mod->Imports)
2709 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002710 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2711 }
2712
Douglas Gregor24bb9232011-12-02 18:58:38 +00002713 // Emit the exports.
2714 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002715 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002716 for (const auto &E : Mod->Exports) {
2717 // FIXME: This may fail; we don't require that all exported modules
2718 // are local or imported.
2719 Record.push_back(getSubmoduleID(E.getPointer()));
2720 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002721 }
2722 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2723 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002724
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002725 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2726 // Might be unnecessary as use declarations are only used to build the
2727 // module itself.
2728
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002729 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002730 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002731 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2732 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002733 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002734 }
2735
Douglas Gregorfb912652013-03-20 21:10:35 +00002736 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002737 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002738 // FIXME: This may fail; we don't require that all conflicting modules
2739 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002740 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2741 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002742 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002743 }
2744
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002745 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002746 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002747 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002748 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002749 }
2750
Richard Smithdc1f0422016-07-20 19:10:16 +00002751 // Emit the initializers, if any.
2752 RecordData Inits;
2753 for (Decl *D : Context->getModuleInitializers(Mod))
2754 Inits.push_back(GetDeclRef(D));
2755 if (!Inits.empty())
2756 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2757
Douglas Gregor69021972011-11-30 17:33:56 +00002758 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002759 for (auto *M : Mod->submodules())
2760 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002761 }
2762
2763 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002764
Richard Smith23d8d032015-05-14 00:45:20 +00002765 assert((NextSubmoduleID - FirstSubmoduleID ==
2766 getNumberOfModules(WritingModule)) &&
2767 "Wrong # of submodules; found a reference to a non-local, "
2768 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002769}
2770
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002771serialization::SubmoduleID
2772ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002773 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002774 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002775
2776 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002777 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002778 Module *OwningMod
2779 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002780 if (!OwningMod)
2781 return 0;
2782
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002783 // Check whether this submodule is part of our own module.
2784 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002785 return 0;
2786
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002787 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002788}
2789
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002790void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2791 bool isModule) {
2792 // Make sure set diagnostic pragmas don't affect the translation unit that
2793 // imports the module.
2794 // FIXME: Make diagnostic pragma sections work properly with modules.
2795 if (isModule)
2796 return;
2797
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002798 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2799 DiagStateIDMap;
2800 unsigned CurrID = 0;
2801 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002802 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002803 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002804 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2805 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002806 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002807 if (point.Loc.isInvalid())
2808 continue;
2809
Richard Smithb22a1d12016-03-27 20:13:24 +00002810 AddSourceLocation(point.Loc, Record);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002811 unsigned &DiagStateID = DiagStateIDMap[point.State];
2812 Record.push_back(DiagStateID);
2813
2814 if (DiagStateID == 0) {
2815 DiagStateID = ++CurrID;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002816 for (const auto &I : *(point.State)) {
2817 if (I.second.isPragma()) {
2818 Record.push_back(I.first);
2819 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002820 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002821 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002822 Record.push_back(-1); // mark the end of the diag/map pairs for this
2823 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002824 }
2825 }
2826
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002827 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002828 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002829}
2830
Douglas Gregorc5046832009-04-27 18:38:38 +00002831//===----------------------------------------------------------------------===//
2832// Type Serialization
2833//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002834
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002835/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002836void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002837 TypeIdx &IdxRef = TypeIdxs[T];
2838 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2839 IdxRef = TypeIdx(NextTypeID++);
2840 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002841
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002842 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002843
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002844 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002845
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002846 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002847 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002848 W.Visit(T);
2849 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002850
Richard Smith290d8012016-04-06 17:06:00 +00002851 // Record the offset for this type.
2852 unsigned Index = Idx.getIndex() - FirstTypeID;
2853 if (TypeOffsets.size() == Index)
2854 TypeOffsets.push_back(Offset);
2855 else if (TypeOffsets.size() < Index) {
2856 TypeOffsets.resize(Index + 1);
2857 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002858 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002859 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002860 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002861}
2862
Douglas Gregorc5046832009-04-27 18:38:38 +00002863//===----------------------------------------------------------------------===//
2864// Declaration Serialization
2865//===----------------------------------------------------------------------===//
2866
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002867/// \brief Write the block containing all of the declaration IDs
2868/// lexically declared within the given DeclContext.
2869///
2870/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2871/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002872uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002873 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002874 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002875 return 0;
2876
Douglas Gregor8f45df52009-04-16 22:23:12 +00002877 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002878 SmallVector<uint32_t, 128> KindDeclPairs;
2879 for (const auto *D : DC->decls()) {
2880 KindDeclPairs.push_back(D->getKind());
2881 KindDeclPairs.push_back(GetDeclRef(D));
2882 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002883
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002884 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002885 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002886 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2887 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002888 return Offset;
2889}
2890
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002891void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002892 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002893
2894 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002895 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002896 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002900 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002901 {
2902 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2903 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2904 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2905 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002906
2907 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002908 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002909 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002913 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002914 {
2915 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2916 FirstDeclID - NUM_PREDEF_DECL_IDS};
2917 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2918 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002919}
2920
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002921void ASTWriter::WriteFileDeclIDsMap() {
2922 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002923
Chandler Carruthb306d732015-03-27 00:31:20 +00002924 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2925 FileDeclIDs.begin(), FileDeclIDs.end());
2926 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2927 llvm::less_first());
2928
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002929 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002930 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2931 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2932 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2933 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2934 for (auto &LocDeclEntry : Info.DeclIDs)
2935 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002936 }
2937
David Blaikieb44f0bf2017-01-04 22:36:43 +00002938 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002939 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002942 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002943 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2944 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002945 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002946}
2947
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002948void ASTWriter::WriteComments() {
2949 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002950 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002951 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002952 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002953 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002954 AddSourceRange(I->getSourceRange(), Record);
2955 Record.push_back(I->getKind());
2956 Record.push_back(I->isTrailingComment());
2957 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002958 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2959 }
2960 Stream.ExitBlock();
2961}
2962
Douglas Gregorc5046832009-04-27 18:38:38 +00002963//===----------------------------------------------------------------------===//
2964// Global Method Pool and Selector Serialization
2965//===----------------------------------------------------------------------===//
2966
Douglas Gregore84a9da2009-04-20 20:36:09 +00002967namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002968
Douglas Gregorc78d3462009-04-24 21:10:55 +00002969// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002970class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002971 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002972
2973public:
2974 typedef Selector key_type;
2975 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002976
Sebastian Redl834bb972010-08-04 17:20:04 +00002977 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002978 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002979 ObjCMethodList Instance, Factory;
2980 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002981 typedef const data_type& data_type_ref;
2982
Justin Bogner25463f12014-04-18 20:27:24 +00002983 typedef unsigned hash_value_type;
2984 typedef unsigned offset_type;
2985
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002986 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002987
Justin Bogner25463f12014-04-18 20:27:24 +00002988 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002989 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002990 }
Mike Stump11289f42009-09-09 15:08:12 +00002991
2992 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002993 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002994 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002995 using namespace llvm::support;
2996 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002997 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002998 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002999 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3000 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003001 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003002 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003003 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003004 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003005 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003006 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003007 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003008 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003009 return std::make_pair(KeyLen, DataLen);
3010 }
Mike Stump11289f42009-09-09 15:08:12 +00003011
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003012 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003013 using namespace llvm::support;
3014 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003015 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003016 assert((Start >> 32) == 0 && "Selector key offset too large");
3017 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003018 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003019 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003020 if (N == 0)
3021 N = 1;
3022 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003023 LE.write<uint32_t>(
3024 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003025 }
Mike Stump11289f42009-09-09 15:08:12 +00003026
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003027 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003028 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003029 using namespace llvm::support;
3030 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003031 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003032 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003033 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003034 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003035 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003036 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003037 ++NumInstanceMethods;
3038
3039 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003040 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003041 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003042 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003043 ++NumFactoryMethods;
3044
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003045 unsigned InstanceBits = Methods.Instance.getBits();
3046 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003047 unsigned InstanceHasMoreThanOneDeclBit =
3048 Methods.Instance.hasMoreThanOneDecl();
3049 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3050 (InstanceHasMoreThanOneDeclBit << 2) |
3051 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003052 unsigned FactoryBits = Methods.Factory.getBits();
3053 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003054 unsigned FactoryHasMoreThanOneDeclBit =
3055 Methods.Factory.hasMoreThanOneDecl();
3056 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3057 (FactoryHasMoreThanOneDeclBit << 2) |
3058 FactoryBits;
3059 LE.write<uint16_t>(FullInstanceBits);
3060 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003061 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003062 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003063 if (Method->getMethod())
3064 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003065 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003066 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003067 if (Method->getMethod())
3068 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003069
3070 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003071 }
3072};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003073
Douglas Gregorc78d3462009-04-24 21:10:55 +00003074} // end anonymous namespace
3075
Sebastian Redla19a67f2010-08-03 21:58:15 +00003076/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003077///
3078/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003079/// in an on-disk hash table indexed by the selector. The hash table also
3080/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003081void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003082 using namespace llvm;
3083
Sebastian Redla19a67f2010-08-03 21:58:15 +00003084 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003085 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003086 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003087 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003088 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003089 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003090 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003091 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003092
Sebastian Redla19a67f2010-08-03 21:58:15 +00003093 // Create the on-disk hash table representation. We walk through every
3094 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003095 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003096 for (auto &SelectorAndID : SelectorIDs) {
3097 Selector S = SelectorAndID.first;
3098 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003099 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003100 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003101 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003102 ObjCMethodList(),
3103 ObjCMethodList()
3104 };
3105 if (F != SemaRef.MethodPool.end()) {
3106 Data.Instance = F->second.first;
3107 Data.Factory = F->second.second;
3108 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003109 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003110 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003111 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003112 // Selector already exists. Did it change?
3113 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003114 for (ObjCMethodList *M = &Data.Instance;
3115 !changed && M && M->getMethod(); M = M->getNext()) {
3116 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003117 changed = true;
3118 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003119 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003120 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003121 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003122 changed = true;
3123 }
3124 if (!changed)
3125 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003126 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003127 // A new method pool entry.
3128 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003129 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003130 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003131 }
3132
Douglas Gregorc78d3462009-04-24 21:10:55 +00003133 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003134 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003135 uint32_t BucketOffset;
3136 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003137 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003138 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003139 llvm::raw_svector_ostream Out(MethodPool);
3140 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003141 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003142 BucketOffset = Generator.Emit(Out, Trait);
3143 }
3144
3145 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003146 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003147 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003151 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003152
Douglas Gregor95c13f52009-04-25 17:48:32 +00003153 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003154 {
3155 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3156 NumTableEntries};
3157 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3158 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003159
3160 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003161 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003162 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003166 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003167
3168 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003169 {
3170 RecordData::value_type Record[] = {
3171 SELECTOR_OFFSETS, SelectorOffsets.size(),
3172 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3173 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3174 bytes(SelectorOffsets));
3175 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003176 }
3177}
3178
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003179/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003180void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003181 using namespace llvm;
3182 if (SemaRef.ReferencedSelectors.empty())
3183 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003184
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003185 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003186 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003188 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003189 // very tricky to fix, and given that @selector shouldn't really appear in
3190 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003191 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3192 Selector Sel = SelectorAndLocation.first;
3193 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003194 Writer.AddSelectorRef(Sel);
3195 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003196 }
Richard Smithe64e7452016-04-18 21:54:58 +00003197 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003198}
3199
Douglas Gregorc5046832009-04-27 18:38:38 +00003200//===----------------------------------------------------------------------===//
3201// Identifier Table Serialization
3202//===----------------------------------------------------------------------===//
3203
Richard Smithb3761912015-02-05 23:08:52 +00003204/// Determine the declaration that should be put into the name lookup table to
3205/// represent the given declaration in this module. This is usually D itself,
3206/// but if D was imported and merged into a local declaration, we want the most
3207/// recent local declaration instead. The chosen declaration will be the most
3208/// recent declaration in any module that imports this one.
3209static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3210 NamedDecl *D) {
3211 if (!LangOpts.Modules || !D->isFromASTFile())
3212 return D;
3213
3214 if (Decl *Redecl = D->getPreviousDecl()) {
3215 // For Redeclarable decls, a prior declaration might be local.
3216 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003217 // If we find a local decl, we're done.
3218 if (!Redecl->isFromASTFile()) {
3219 // Exception: in very rare cases (for injected-class-names), not all
3220 // redeclarations are in the same semantic context. Skip ones in a
3221 // different context. They don't go in this lookup table at all.
3222 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3223 D->getDeclContext()->getRedeclContext()))
3224 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003225 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003226 }
3227
Richard Smithfe620d22015-03-05 23:24:12 +00003228 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003229 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003230 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003231 break;
3232 }
3233 } else if (Decl *First = D->getCanonicalDecl()) {
3234 // For Mergeable decls, the first decl might be local.
3235 if (!First->isFromASTFile())
3236 return cast<NamedDecl>(First);
3237 }
3238
3239 // All declarations are imported. Our most recent declaration will also be
3240 // the most recent one in anyone who imports us.
3241 return D;
3242}
3243
Douglas Gregorc78d3462009-04-24 21:10:55 +00003244namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003245
Richard Smithcf97cf62015-04-19 01:34:23 +00003246class ASTIdentifierTableTrait {
3247 ASTWriter &Writer;
3248 Preprocessor &PP;
3249 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003250 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003251 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003252 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003253
3254 /// \brief Determines whether this is an "interesting" identifier that needs a
3255 /// full IdentifierInfo structure written into the hash table. Notably, this
3256 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3257 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003258 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003259 if (MacroOffset ||
3260 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003261 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003262 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003263 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003264 return true;
3265
3266 return false;
3267 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003268
Douglas Gregore84a9da2009-04-20 20:36:09 +00003269public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003270 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003271 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003272
Sebastian Redl539c5062010-08-18 23:57:32 +00003273 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003274 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003275
Justin Bogner25463f12014-04-18 20:27:24 +00003276 typedef unsigned hash_value_type;
3277 typedef unsigned offset_type;
3278
Richard Smithd7329392015-04-21 21:46:32 +00003279 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003280 IdentifierResolver &IdResolver, bool IsModule,
3281 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003282 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003283 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3284 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003285
Richard Smithd79514e2016-02-05 19:03:40 +00003286 bool needDecls() const { return NeedDecls; }
3287
Justin Bogner25463f12014-04-18 20:27:24 +00003288 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003289 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003290 }
Mike Stump11289f42009-09-09 15:08:12 +00003291
Richard Smith33e0f7e2015-07-22 02:08:40 +00003292 bool isInterestingIdentifier(const IdentifierInfo *II) {
3293 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3294 return isInterestingIdentifier(II, MacroOffset);
3295 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003296
Richard Smith9c254182015-07-19 21:41:12 +00003297 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3298 return isInterestingIdentifier(II, 0);
3299 }
3300
Mike Stump11289f42009-09-09 15:08:12 +00003301 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003302 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003303 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003304 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003305 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3306 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003307 DataLen += 2; // 2 bytes for builtin ID
3308 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003309 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003310 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003311
Richard Smitha534a312015-07-21 23:54:07 +00003312 if (NeedDecls) {
3313 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3314 DEnd = IdResolver.end();
3315 D != DEnd; ++D)
3316 DataLen += 4;
3317 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003318 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003319 using namespace llvm::support;
3320 endian::Writer<little> LE(Out);
3321
Richard Smithdf8a8312015-03-13 04:05:01 +00003322 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003323 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003324 // We emit the key length after the data length so that every
3325 // string is preceded by a 16-bit length. This matches the PTH
3326 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003327 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003328 return std::make_pair(KeyLen, DataLen);
3329 }
Mike Stump11289f42009-09-09 15:08:12 +00003330
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003331 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003332 unsigned KeyLen) {
3333 // Record the location of the key data. This is used when generating
3334 // the mapping from persistent IDs to strings.
3335 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003336
3337 // Emit the offset of the key/data length information to the interesting
3338 // identifiers table if necessary.
3339 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3340 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3341
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003342 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003343 }
Mike Stump11289f42009-09-09 15:08:12 +00003344
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003345 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003346 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003347 using namespace llvm::support;
3348 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003349
Richard Smithd7329392015-04-21 21:46:32 +00003350 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3351 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003352 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003353 return;
3354 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003355
Justin Bognere1c147c2014-03-28 22:03:19 +00003356 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003357 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3358 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003359 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003360 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003361 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003362 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003363 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3364 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003365 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003366 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003367 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003368 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003369
Richard Smithd7329392015-04-21 21:46:32 +00003370 if (HadMacroDefinition)
3371 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003372
Richard Smitha534a312015-07-21 23:54:07 +00003373 if (NeedDecls) {
3374 // Emit the declaration IDs in reverse order, because the
3375 // IdentifierResolver provides the declarations as they would be
3376 // visible (e.g., the function "stat" would come before the struct
3377 // "stat"), but the ASTReader adds declarations to the end of the list
3378 // (so we need to see the struct "stat" before the function "stat").
3379 // Only emit declarations that aren't from a chained PCH, though.
3380 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3381 IdResolver.end());
3382 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3383 DEnd = Decls.rend();
3384 D != DEnd; ++D)
3385 LE.write<uint32_t>(
3386 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3387 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003388 }
3389};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003390
Douglas Gregore84a9da2009-04-20 20:36:09 +00003391} // end anonymous namespace
3392
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003393/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003394///
3395/// The identifier table consists of a blob containing string data
3396/// (the actual identifiers themselves) and a separate "offsets" index
3397/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003398void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3399 IdentifierResolver &IdResolver,
3400 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003401 using namespace llvm;
3402
Richard Smith33e0f7e2015-07-22 02:08:40 +00003403 RecordData InterestingIdents;
3404
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003405 // Create and write out the blob that contains the identifier
3406 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003407 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003408 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003409 ASTIdentifierTableTrait Trait(
3410 *this, PP, IdResolver, IsModule,
3411 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003412
Douglas Gregore6648fb2009-04-28 20:33:11 +00003413 // Look for any identifiers that were named while processing the
3414 // headers, but are otherwise not needed. We add these to the hash
3415 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003416 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003417 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003418 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003419 for (const auto &ID : PP.getIdentifierTable())
3420 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003421 // Sort the identifiers lexicographically before getting them references so
3422 // that their order is stable.
3423 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3424 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003425 if (Trait.isInterestingNonMacroIdentifier(II))
3426 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003427
Sebastian Redlff4a2952010-07-23 23:49:55 +00003428 // Create the on-disk hash table representation. We only store offsets
3429 // for identifiers that appear here for the first time.
3430 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003431 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003432 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003433 IdentID ID = IdentIDPair.second;
3434 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003435 // Write out identifiers if either the ID is local or the identifier has
3436 // changed since it was loaded.
3437 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3438 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003439 (Trait.needDecls() &&
3440 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003441 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003442 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003443
Douglas Gregore84a9da2009-04-20 20:36:09 +00003444 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003445 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003446 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003447 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003448 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003449 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003450 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003451 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003452 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003453 }
3454
3455 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003456 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003457 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003460 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003461
3462 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003463 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003464 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003465 }
3466
3467 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003468 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003469 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003470 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003473 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003474
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003475#ifndef NDEBUG
3476 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3477 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3478#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003479
3480 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3481 IdentifierOffsets.size(),
3482 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003483 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003484 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003485
3486 // In C++, write the list of interesting identifiers (those that are
3487 // defined as macros, poisoned, or similar unusual things).
3488 if (!InterestingIdents.empty())
3489 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003490}
3491
Douglas Gregorc5046832009-04-27 18:38:38 +00003492//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003493// DeclContext's Name Lookup Table Serialization
3494//===----------------------------------------------------------------------===//
3495
3496namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003497
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003498// Trait used for the on-disk hash table used in the method pool.
3499class ASTDeclContextNameLookupTrait {
3500 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003501 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003502
3503public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003504 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003505 typedef key_type key_type_ref;
3506
Richard Smithd88a7f12015-09-01 20:35:42 +00003507 /// A start and end index into DeclIDs, representing a sequence of decls.
3508 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003509 typedef const data_type& data_type_ref;
3510
Justin Bogner25463f12014-04-18 20:27:24 +00003511 typedef unsigned hash_value_type;
3512 typedef unsigned offset_type;
3513
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003514 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3515
Richard Smithd88a7f12015-09-01 20:35:42 +00003516 template<typename Coll>
3517 data_type getData(const Coll &Decls) {
3518 unsigned Start = DeclIDs.size();
3519 for (NamedDecl *D : Decls) {
3520 DeclIDs.push_back(
3521 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3522 }
3523 return std::make_pair(Start, DeclIDs.size());
3524 }
3525
3526 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3527 unsigned Start = DeclIDs.size();
3528 for (auto ID : FromReader)
3529 DeclIDs.push_back(ID);
3530 return std::make_pair(Start, DeclIDs.size());
3531 }
3532
3533 static bool EqualKey(key_type_ref a, key_type_ref b) {
3534 return a == b;
3535 }
3536
Richard Smitha06c7e62015-08-26 23:55:49 +00003537 hash_value_type ComputeHash(DeclarationNameKey Name) {
3538 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003539 }
3540
Richard Smithd88a7f12015-09-01 20:35:42 +00003541 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3542 assert(Writer.hasChain() &&
3543 "have reference to loaded module file but no chain?");
3544
3545 using namespace llvm::support;
3546 endian::Writer<little>(Out)
3547 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3548 }
3549
Richard Smitha06c7e62015-08-26 23:55:49 +00003550 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3551 DeclarationNameKey Name,
3552 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003553 using namespace llvm::support;
3554 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003555 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003556 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003557 case DeclarationName::Identifier:
3558 case DeclarationName::ObjCZeroArgSelector:
3559 case DeclarationName::ObjCOneArgSelector:
3560 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003561 case DeclarationName::CXXLiteralOperatorName:
3562 KeyLen += 4;
3563 break;
3564 case DeclarationName::CXXOperatorName:
3565 KeyLen += 1;
3566 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003567 case DeclarationName::CXXConstructorName:
3568 case DeclarationName::CXXDestructorName:
3569 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003570 case DeclarationName::CXXUsingDirective:
3571 break;
3572 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003573 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003574
Richard Smithf02662d2015-07-30 03:17:16 +00003575 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003576 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3577 assert(uint16_t(DataLen) == DataLen &&
3578 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003579 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003580
3581 return std::make_pair(KeyLen, DataLen);
3582 }
3583
Richard Smitha06c7e62015-08-26 23:55:49 +00003584 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003585 using namespace llvm::support;
3586 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003587 LE.write<uint8_t>(Name.getKind());
3588 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003589 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003590 case DeclarationName::CXXLiteralOperatorName:
3591 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003592 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593 case DeclarationName::ObjCZeroArgSelector:
3594 case DeclarationName::ObjCOneArgSelector:
3595 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003596 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003597 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003598 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003599 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003600 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003601 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003602 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003603 case DeclarationName::CXXConstructorName:
3604 case DeclarationName::CXXDestructorName:
3605 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003606 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003607 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003608 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003609
3610 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003611 }
3612
Richard Smitha06c7e62015-08-26 23:55:49 +00003613 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3614 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003615 using namespace llvm::support;
3616 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003617 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003618 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3619 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003620 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3621 }
3622};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003623
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003624} // end anonymous namespace
3625
Chandler Carruthe972c362015-03-26 03:11:40 +00003626bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3627 DeclContext *DC) {
3628 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3629}
3630
3631bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3632 DeclContext *DC) {
3633 for (auto *D : Result.getLookupResult())
3634 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3635 return false;
3636
3637 return true;
3638}
3639
Richard Smithd88a7f12015-09-01 20:35:42 +00003640void
Chandler Carruthe972c362015-03-26 03:11:40 +00003641ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003642 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003643 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3644 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003645 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003646
Chandler Carruthe972c362015-03-26 03:11:40 +00003647 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003648 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003649 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3650
3651 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003652 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3653 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003654 ASTDeclContextNameLookupTrait Trait(*this);
3655
Chandler Carruthe972c362015-03-26 03:11:40 +00003656 // The first step is to collect the declaration names which we need to
3657 // serialize into the name lookup table, and to collect them in a stable
3658 // order.
3659 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003660
Chandler Carruthe972c362015-03-26 03:11:40 +00003661 // We also build up small sets of the constructor and conversion function
3662 // names which are visible.
3663 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003664
Chandler Carruthe972c362015-03-26 03:11:40 +00003665 for (auto &Lookup : *DC->buildLookup()) {
3666 auto &Name = Lookup.first;
3667 auto &Result = Lookup.second;
3668
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003669 // If there are no local declarations in our lookup result, we
3670 // don't need to write an entry for the name at all. If we can't
3671 // write out a lookup set without performing more deserialization,
3672 // just skip this entry.
3673 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003674 isLookupResultEntirelyExternal(Result, DC))
3675 continue;
3676
3677 // We also skip empty results. If any of the results could be external and
3678 // the currently available results are empty, then all of the results are
3679 // external and we skip it above. So the only way we get here with an empty
3680 // results is when no results could have been external *and* we have
3681 // external results.
3682 //
3683 // FIXME: While we might want to start emitting on-disk entries for negative
3684 // lookups into a decl context as an optimization, today we *have* to skip
3685 // them because there are names with empty lookup results in decl contexts
3686 // which we can't emit in any stable ordering: we lookup constructors and
3687 // conversion functions in the enclosing namespace scope creating empty
3688 // results for them. This in almost certainly a bug in Clang's name lookup,
3689 // but that is likely to be hard or impossible to fix and so we tolerate it
3690 // here by omitting lookups with empty results.
3691 if (Lookup.second.getLookupResult().empty())
3692 continue;
3693
3694 switch (Lookup.first.getNameKind()) {
3695 default:
3696 Names.push_back(Lookup.first);
3697 break;
3698
Richard Smithcd45dbc2014-04-19 03:48:30 +00003699 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003700 assert(isa<CXXRecordDecl>(DC) &&
3701 "Cannot have a constructor name outside of a class!");
3702 ConstructorNameSet.insert(Name);
3703 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003704
3705 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003706 assert(isa<CXXRecordDecl>(DC) &&
3707 "Cannot have a conversion function name outside of a class!");
3708 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003709 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003710 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003711 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003712
Chandler Carruthe972c362015-03-26 03:11:40 +00003713 // Sort the names into a stable order.
3714 std::sort(Names.begin(), Names.end());
3715
Chandler Carruthffbf7052015-03-26 22:27:09 +00003716 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003717 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003718 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003719
Chandler Carruthffbf7052015-03-26 22:27:09 +00003720 // First we try the easy case by forming the current context's constructor
3721 // name and adding that name first. This is a very useful optimization to
3722 // avoid walking the lexical declarations in many cases, and it also
3723 // handles the only case where a constructor name can come from some other
3724 // lexical context -- when that name is an implicit constructor merged from
3725 // another declaration in the redecl chain. Any non-implicit constructor or
3726 // conversion function which doesn't occur in all the lexical contexts
3727 // would be an ODR violation.
3728 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3729 Context->getCanonicalType(Context->getRecordType(D)));
3730 if (ConstructorNameSet.erase(ImplicitCtorName))
3731 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003732
Chandler Carruthffbf7052015-03-26 22:27:09 +00003733 // If we still have constructors or conversion functions, we walk all the
3734 // names in the decl and add the constructors and conversion functions
3735 // which are visible in the order they lexically occur within the context.
3736 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3737 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3738 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3739 auto Name = ChildND->getDeclName();
3740 switch (Name.getNameKind()) {
3741 default:
3742 continue;
3743
3744 case DeclarationName::CXXConstructorName:
3745 if (ConstructorNameSet.erase(Name))
3746 Names.push_back(Name);
3747 break;
3748
3749 case DeclarationName::CXXConversionFunctionName:
3750 if (ConversionNameSet.erase(Name))
3751 Names.push_back(Name);
3752 break;
3753 }
3754
3755 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3756 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003757 }
3758
Chandler Carruthe972c362015-03-26 03:11:40 +00003759 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3760 "constructors by walking all the "
3761 "lexical members of the context.");
3762 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3763 "conversion functions by walking all "
3764 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003765 }
3766
Chandler Carruthe972c362015-03-26 03:11:40 +00003767 // Next we need to do a lookup with each name into this decl context to fully
3768 // populate any results from external sources. We don't actually use the
3769 // results of these lookups because we only want to use the results after all
3770 // results have been loaded and the pointers into them will be stable.
3771 for (auto &Name : Names)
3772 DC->lookup(Name);
3773
3774 // Now we need to insert the results for each name into the hash table. For
3775 // constructor names and conversion function names, we actually need to merge
3776 // all of the results for them into one list of results each and insert
3777 // those.
3778 SmallVector<NamedDecl *, 8> ConstructorDecls;
3779 SmallVector<NamedDecl *, 8> ConversionDecls;
3780
3781 // Now loop over the names, either inserting them or appending for the two
3782 // special cases.
3783 for (auto &Name : Names) {
3784 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3785
3786 switch (Name.getNameKind()) {
3787 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003788 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003789 break;
3790
3791 case DeclarationName::CXXConstructorName:
3792 ConstructorDecls.append(Result.begin(), Result.end());
3793 break;
3794
3795 case DeclarationName::CXXConversionFunctionName:
3796 ConversionDecls.append(Result.begin(), Result.end());
3797 break;
3798 }
3799 }
3800
3801 // Handle our two special cases if we ended up having any. We arbitrarily use
3802 // the first declaration's name here because the name itself isn't part of
3803 // the key, only the kind of name is used.
3804 if (!ConstructorDecls.empty())
3805 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003806 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003807 if (!ConversionDecls.empty())
3808 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003809 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003810
Richard Smithd88a7f12015-09-01 20:35:42 +00003811 // Create the on-disk hash table. Also emit the existing imported and
3812 // merged table if there is one.
3813 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3814 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003815}
3816
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003817/// \brief Write the block containing all of the declaration IDs
3818/// visible from the given DeclContext.
3819///
3820/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003821/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003822uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3823 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003824 // If we imported a key declaration of this namespace, write the visible
3825 // lookup results as an update record for it rather than including them
3826 // on this declaration. We will only look at key declarations on reload.
3827 if (isa<NamespaceDecl>(DC) && Chain &&
3828 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3829 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003830 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003831 Prev = Prev->getPreviousDecl())
3832 if (!Prev->isFromASTFile())
3833 return 0;
3834
3835 // Note that we need to emit an update record for the primary context.
3836 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3837
3838 // Make sure all visible decls are written. They will be recorded later. We
3839 // do this using a side data structure so we can sort the names into
3840 // a deterministic order.
3841 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3842 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3843 LookupResults;
3844 if (Map) {
3845 LookupResults.reserve(Map->size());
3846 for (auto &Entry : *Map)
3847 LookupResults.push_back(
3848 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3849 }
3850
3851 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3852 for (auto &NameAndResult : LookupResults) {
3853 DeclarationName Name = NameAndResult.first;
3854 DeclContext::lookup_result Result = NameAndResult.second;
3855 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3856 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3857 // We have to work around a name lookup bug here where negative lookup
3858 // results for these names get cached in namespace lookup tables (these
3859 // names should never be looked up in a namespace).
3860 assert(Result.empty() && "Cannot have a constructor or conversion "
3861 "function name in a namespace!");
3862 continue;
3863 }
3864
3865 for (NamedDecl *ND : Result)
3866 if (!ND->isFromASTFile())
3867 GetDeclRef(ND);
3868 }
3869
3870 return 0;
3871 }
3872
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003873 if (DC->getPrimaryContext() != DC)
3874 return 0;
3875
Chandler Carruthe972c362015-03-26 03:11:40 +00003876 // Skip contexts which don't support name lookup.
3877 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003878 return 0;
3879
3880 // If not in C++, we perform name lookup for the translation unit via the
3881 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003882 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003883 return 0;
3884
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003885 // Serialize the contents of the mapping used for lookup. Note that,
3886 // although we have two very different code paths, the serialized
3887 // representation is the same for both cases: a declaration name,
3888 // followed by a size, followed by references to the visible
3889 // declarations that have that name.
3890 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003891 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003892 if (!Map || Map->empty())
3893 return 0;
3894
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003895 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003896 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003897 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003898
3899 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003900 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003901 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003902 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003903 ++NumVisibleDeclContexts;
3904 return Offset;
3905}
3906
Sebastian Redla4071b42010-08-24 00:50:09 +00003907/// \brief Write an UPDATE_VISIBLE block for the given context.
3908///
3909/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3910/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003911/// (in C++), for namespaces, and for classes with forward-declared unscoped
3912/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003913void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003914 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003915 if (!Map || Map->empty())
3916 return;
3917
Sebastian Redla4071b42010-08-24 00:50:09 +00003918 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003919 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003920 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003921
Richard Smith5fc18a92015-07-12 23:43:21 +00003922 // If we're updating a namespace, select a key declaration as the key for the
3923 // update record; those are the only ones that will be checked on reload.
3924 if (isa<NamespaceDecl>(DC))
3925 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3926
Sebastian Redla4071b42010-08-24 00:50:09 +00003927 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003928 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003929 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003930}
3931
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003932/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3933void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003934 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003935 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3936}
3937
3938/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3939void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003940 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003941 return;
3942
3943 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3944 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00003945 for (const auto &I:Opts.OptMap) {
3946 AddString(I.getKey(), Record);
3947 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00003948 Record.push_back(V.Supported ? 1 : 0);
3949 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00003950 Record.push_back(V.Avail);
3951 Record.push_back(V.Core);
3952 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003953 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3954}
3955
Yaxun Liu5b746652016-12-18 05:18:55 +00003956void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
3957 if (!SemaRef.Context.getLangOpts().OpenCL)
3958 return;
3959
3960 RecordData Record;
3961 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
3962 Record.push_back(
3963 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
3964 Record.push_back(I.second.size());
3965 for (auto Ext : I.second)
3966 AddString(Ext, Record);
3967 }
3968 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
3969}
3970
3971void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
3972 if (!SemaRef.Context.getLangOpts().OpenCL)
3973 return;
3974
3975 RecordData Record;
3976 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
3977 Record.push_back(getDeclID(I.first));
3978 Record.push_back(static_cast<unsigned>(I.second.size()));
3979 for (auto Ext : I.second)
3980 AddString(Ext, Record);
3981 }
3982 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
3983}
3984
Justin Lebar67a78a62016-10-08 22:15:58 +00003985void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
3986 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
3987 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
3988 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
3989 }
3990}
3991
Douglas Gregor404cdde2012-01-27 01:47:08 +00003992void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003993 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003994 RecordData Categories;
3995
3996 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3997 unsigned Size = 0;
3998 unsigned StartIndex = Categories.size();
3999
4000 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4001
4002 // Allocate space for the size.
4003 Categories.push_back(0);
4004
4005 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004006 for (ObjCInterfaceDecl::known_categories_iterator
4007 Cat = Class->known_categories_begin(),
4008 CatEnd = Class->known_categories_end();
4009 Cat != CatEnd; ++Cat, ++Size) {
4010 assert(getDeclID(*Cat) != 0 && "Bogus category");
4011 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004012 }
4013
4014 // Update the size.
4015 Categories[StartIndex] = Size;
4016
4017 // Record this interface -> category map.
4018 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4019 CategoriesMap.push_back(CatInfo);
4020 }
4021
4022 // Sort the categories map by the definition ID, since the reader will be
4023 // performing binary searches on this information.
4024 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4025
4026 // Emit the categories map.
4027 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004028
David Blaikieb44f0bf2017-01-04 22:36:43 +00004029 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004030 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004033 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004034
4035 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4036 Stream.EmitRecordWithBlob(AbbrevID, Record,
4037 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004038 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004039
Douglas Gregor404cdde2012-01-27 01:47:08 +00004040 // Emit the category lists.
4041 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4042}
4043
Richard Smithe40f2ba2013-08-07 21:41:30 +00004044void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4045 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4046
4047 if (LPTMap.empty())
4048 return;
4049
4050 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004051 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004052 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004053 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004054 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004055 AddDeclRef(LPT.D, Record);
4056 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004057
Justin Lebar28f09c52016-10-10 16:26:08 +00004058 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004059 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004060 }
4061 }
4062 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4063}
4064
Dario Domizioli13a0a382014-05-23 12:13:25 +00004065/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4066void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4067 RecordData Record;
4068 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4069 AddSourceLocation(PragmaLoc, Record);
4070 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4071}
4072
Nico Weber779355f2016-03-02 23:22:00 +00004073/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4074void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4075 RecordData Record;
4076 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4077 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4078}
4079
Nico Weber42932312016-03-03 00:17:35 +00004080/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4081//module.
4082void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4083 RecordData Record;
4084 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4085 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4086 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4087}
4088
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004089void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4090 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004091 // Enter the extension block.
4092 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4093
4094 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004095 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004096 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4097 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4098 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4099 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4100 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4101 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004102 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004103
4104 // Emit the metadata record.
4105 RecordData Record;
4106 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4107 Record.push_back(EXTENSION_METADATA);
4108 Record.push_back(Metadata.MajorVersion);
4109 Record.push_back(Metadata.MinorVersion);
4110 Record.push_back(Metadata.BlockName.size());
4111 Record.push_back(Metadata.UserInfo.size());
4112 SmallString<64> Buffer;
4113 Buffer += Metadata.BlockName;
4114 Buffer += Metadata.UserInfo;
4115 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4116
4117 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004118 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004119
4120 // Exit the extension block.
4121 Stream.ExitBlock();
4122}
4123
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004124//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004125// General Serialization Routines
4126//===----------------------------------------------------------------------===//
4127
Richard Smith69c82bf2016-04-01 22:52:03 +00004128/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004129void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4130 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004131 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004132 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004133 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004134 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004135
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004136#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004137
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004138 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004139}
4140
John McCallf413f5e2013-05-03 00:10:13 +00004141void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4142 AddSourceLocation(Tok.getLocation(), Record);
4143 Record.push_back(Tok.getLength());
4144
4145 // FIXME: When reading literal tokens, reconstruct the literal pointer
4146 // if it is needed.
4147 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4148 // FIXME: Should translate token kind to a stable encoding.
4149 Record.push_back(Tok.getKind());
4150 // FIXME: Should translate token flags to a stable encoding.
4151 Record.push_back(Tok.getFlags());
4152}
4153
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004154void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004155 Record.push_back(Str.size());
4156 Record.insert(Record.end(), Str.begin(), Str.end());
4157}
4158
Richard Smith7ed1bc92014-12-05 22:42:13 +00004159bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004160 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004161
Richard Smith54cc3c22014-12-11 20:50:24 +00004162 bool Changed =
4163 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004164
4165 // Remove a prefix to make the path relative, if relevant.
4166 const char *PathBegin = Path.data();
4167 const char *PathPtr =
4168 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4169 if (PathPtr != PathBegin) {
4170 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4171 Changed = true;
4172 }
4173
4174 return Changed;
4175}
4176
4177void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4178 SmallString<128> FilePath(Path);
4179 PreparePathForOutput(FilePath);
4180 AddString(FilePath, Record);
4181}
4182
Mehdi Amini57a41912015-09-10 01:46:39 +00004183void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004184 StringRef Path) {
4185 SmallString<128> FilePath(Path);
4186 PreparePathForOutput(FilePath);
4187 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4188}
4189
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004190void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4191 RecordDataImpl &Record) {
4192 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004193 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004194 Record.push_back(*Minor + 1);
4195 else
4196 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004197 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004198 Record.push_back(*Subminor + 1);
4199 else
4200 Record.push_back(0);
4201}
4202
Douglas Gregore84a9da2009-04-20 20:36:09 +00004203/// \brief Note that the identifier II occurs at the given offset
4204/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004205void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004206 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004207 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004208 // up earlier in the chain and thus don't need an offset.
4209 if (ID >= FirstIdentID)
4210 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004211}
4212
Douglas Gregor95c13f52009-04-25 17:48:32 +00004213/// \brief Note that the selector Sel occurs at the given offset
4214/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004215void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004216 unsigned ID = SelectorIDs[Sel];
4217 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004218 // Don't record offsets for selectors that are also available in a different
4219 // file.
4220 if (ID < FirstSelectorID)
4221 return;
4222 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004223}
4224
David Blaikie61137e12017-01-05 18:23:18 +00004225ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4226 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4227 bool IncludeTimestamps)
David Blaikie95dd3622017-01-05 18:45:45 +00004228 : Stream(Stream), IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004229 for (const auto &Ext : Extensions) {
4230 if (auto Writer = Ext->createExtensionWriter(*this))
4231 ModuleFileExtensionWriters.push_back(std::move(Writer));
4232 }
4233}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004234
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004235ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004236 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004237}
4238
Richard Smithb3761912015-02-05 23:08:52 +00004239const LangOptions &ASTWriter::getLangOpts() const {
4240 assert(WritingAST && "can't determine lang opts when not writing AST");
4241 return Context->getLangOpts();
4242}
4243
Richard Smithe75ee0f2015-08-17 07:13:32 +00004244time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4245 return IncludeTimestamps ? E->getModificationTime() : 0;
4246}
4247
Adrian Prantladbd2b12015-09-22 23:26:31 +00004248uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4249 Module *WritingModule, StringRef isysroot,
4250 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004251 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004252
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004253 ASTHasCompilerErrors = hasErrors;
4254
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004255 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004256 Stream.Emit((unsigned)'C', 8);
4257 Stream.Emit((unsigned)'P', 8);
4258 Stream.Emit((unsigned)'C', 8);
4259 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004260
Chris Lattner28fa4e62009-04-26 22:26:21 +00004261 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004262
Douglas Gregoreda8e122011-08-09 15:13:55 +00004263 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004264 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004265 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004266 ASTFileSignature Signature =
4267 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004268 Context = nullptr;
4269 PP = nullptr;
4270 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004271 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004272
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004273 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004274 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004275}
4276
Douglas Gregora94a1542011-07-27 21:45:57 +00004277template<typename Vector>
4278static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4279 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004280 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4281 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004282 Writer.AddDeclRef(*I, Record);
4283 }
4284}
4285
Adrian Prantladbd2b12015-09-22 23:26:31 +00004286uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4287 const std::string &OutputFile,
4288 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004289 using namespace llvm;
4290
Craig Toppera13603a2014-05-22 05:54:18 +00004291 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004292
Douglas Gregorcf68c582011-12-01 22:20:10 +00004293 // Make sure that the AST reader knows to finalize itself.
4294 if (Chain)
4295 Chain->finalizeForWriting();
4296
Sebastian Redl143413f2010-07-12 22:02:52 +00004297 ASTContext &Context = SemaRef.Context;
4298 Preprocessor &PP = SemaRef.PP;
4299
Douglas Gregordab42432011-08-12 00:15:20 +00004300 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004301 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4302 if (D) {
4303 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4304 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004305 }
4306 };
4307 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4308 PREDEF_DECL_TRANSLATION_UNIT_ID);
4309 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4310 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4311 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4312 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4313 PREDEF_DECL_OBJC_PROTOCOL_ID);
4314 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4315 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4316 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4317 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4318 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004319 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004320 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4321 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004322 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004323 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4324 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004325 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4326 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004327 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4328 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004329 RegisterPredefDecl(Context.TypePackElementDecl,
4330 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004331
Chris Lattner0c797362009-09-08 18:19:27 +00004332 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004333 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004334 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004335 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004336 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004337
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004338 // Build a record containing all of the file scoped decls in this file.
4339 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004340 if (!isModule)
4341 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4342 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004343
Douglas Gregor851443c2011-08-12 01:39:19 +00004344 // Build a record containing all of the delegating constructors we still need
4345 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004346 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004347 if (!isModule)
4348 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004349
Douglas Gregor851443c2011-08-12 01:39:19 +00004350 // Write the set of weak, undeclared identifiers. We always write the
4351 // entire table, since later PCH files in a PCH chain are only interested in
4352 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004353 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004354 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4355 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4356 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4357 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4358 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4359 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4360 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004361 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004362
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004363 // Build a record containing all of the ext_vector declarations.
4364 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004365 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004366
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004367 // Build a record containing all of the VTable uses information.
4368 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004369 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004370 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4371 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4372 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4373 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4374 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004375 }
4376
Nico Weber72889432014-09-06 01:25:55 +00004377 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4378 RecordData UnusedLocalTypedefNameCandidates;
4379 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4380 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4381
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004382 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004383 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004384 for (const auto &I : SemaRef.PendingInstantiations) {
4385 AddDeclRef(I.first, PendingInstantiations);
4386 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004387 }
4388 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4389 "There are local ones at end of translation unit!");
4390
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004391 // Build a record containing some declaration references.
4392 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004393 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004394 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4395 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004396 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004397 }
4398
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004399 RecordData CUDASpecialDeclRefs;
4400 if (Context.getcudaConfigureCallDecl()) {
4401 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4402 }
4403
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004404 // Build a record containing all of the known namespaces.
4405 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004406 for (const auto &I : SemaRef.KnownNamespaces) {
4407 if (!I.second)
4408 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004409 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004410
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004411 // Build a record of all used, undefined objects that require definitions.
4412 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004413
4414 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004415 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004416 for (const auto &I : Undefined) {
4417 AddDeclRef(I.first, UndefinedButUsed);
4418 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004419 }
4420
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004421 // Build a record containing all delete-expressions that we would like to
4422 // analyze later in AST.
4423 RecordData DeleteExprsToAnalyze;
4424
4425 for (const auto &DeleteExprsInfo :
4426 SemaRef.getMismatchingDeleteExpressions()) {
4427 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4428 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4429 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4430 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4431 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4432 }
4433 }
4434
Douglas Gregor112b9072012-10-18 05:31:06 +00004435 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004436 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004437
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004438 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004439 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004440
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004441 // This is so that older clang versions, before the introduction
4442 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004443 {
4444 RecordData Record = {VERSION_MAJOR};
4445 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4446 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004447
Douglas Gregor851443c2011-08-12 01:39:19 +00004448 // Create a lexical update block containing all of the declarations in the
4449 // translation unit that do not come from other AST files.
4450 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004451 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4452 for (const auto *D : TU->noload_decls()) {
4453 if (!D->isFromASTFile()) {
4454 NewGlobalKindDeclPairs.push_back(D->getKind());
4455 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4456 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004457 }
4458
David Blaikieb44f0bf2017-01-04 22:36:43 +00004459 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004460 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4461 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004462 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004463 {
4464 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4465 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4466 bytes(NewGlobalKindDeclPairs));
4467 }
4468
Douglas Gregor851443c2011-08-12 01:39:19 +00004469 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004470 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004471 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4472 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004473 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004474 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004475 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004476
4477 // If we have any extern "C" names, write out a visible update for them.
4478 if (Context.ExternCContext)
4479 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004480
4481 // If the translation unit has an anonymous namespace, and we don't already
4482 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004483 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004484 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4485 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004486 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004487 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004488 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004489
Richard Smith5652c0f2014-03-21 01:48:23 +00004490 // Add update records for all mangling numbers and static local numbers.
4491 // These aren't really update records, but this is a convenient way of
4492 // tagging this rare extra data onto the declarations.
4493 for (const auto &Number : Context.MangleNumbers)
4494 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004495 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4496 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004497 for (const auto &Number : Context.StaticLocalNumbers)
4498 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004499 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4500 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004501
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004502 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004503 // an AST file, are registered for serialization. Likewise for template
4504 // specializations added to imported templates.
4505 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004506 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004507 }
4508
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004509 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004510 // serialization, if we're storing decls with identifiers.
4511 if (!WritingModule || !getLangOpts().CPlusPlus) {
4512 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004513 for (const auto &ID : PP.getIdentifierTable()) {
4514 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004515 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4516 IIs.push_back(II);
4517 }
4518 // Sort the identifiers to visit based on their name.
4519 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4520 for (const IdentifierInfo *II : IIs) {
4521 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4522 DEnd = SemaRef.IdResolver.end();
4523 D != DEnd; ++D) {
4524 GetDeclRef(*D);
4525 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004526 }
4527 }
4528
Manman Rena0f31a02016-04-29 19:04:05 +00004529 // For method pool in the module, if it contains an entry for a selector,
4530 // the entry should be complete, containing everything introduced by that
4531 // module and all modules it imports. It's possible that the entry is out of
4532 // date, so we need to pull in the new content here.
4533
4534 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4535 // safe, we copy all selectors out.
4536 llvm::SmallVector<Selector, 256> AllSelectors;
4537 for (auto &SelectorAndID : SelectorIDs)
4538 AllSelectors.push_back(SelectorAndID.first);
4539 for (auto &Selector : AllSelectors)
4540 SemaRef.updateOutOfDateSelector(Selector);
4541
Douglas Gregor5204bde2011-08-02 16:26:37 +00004542 // Form the record of special types.
4543 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004544 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004545 AddTypeRef(Context.getFILEType(), SpecialTypes);
4546 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4547 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4548 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4549 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004550 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004551 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004552
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004553 if (Chain) {
4554 // Write the mapping information describing our module dependencies and how
4555 // each of those modules were mapped into our own offset/ID space, so that
4556 // the reader can build the appropriate mapping to its own offset/ID space.
4557 // The map consists solely of a blob with the following format:
4558 // *(module-name-len:i16 module-name:len*i8
4559 // source-location-offset:i32
4560 // identifier-id:i32
4561 // preprocessed-entity-id:i32
4562 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004563 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004564 // selector-id:i32
4565 // declaration-id:i32
4566 // c++-base-specifiers-id:i32
4567 // type-id:i32)
4568 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004569 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004570 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004572 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004573 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004574 {
4575 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004576 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004577 using namespace llvm::support;
4578 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004579 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004580 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004581 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004582
Ben Langmuir785180e2014-10-20 16:27:30 +00004583 // Note: if a base ID was uint max, it would not be possible to load
4584 // another module after it or have more than one entity inside it.
4585 uint32_t None = std::numeric_limits<uint32_t>::max();
4586
4587 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4588 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4589 if (ShouldWrite)
4590 LE.write<uint32_t>(BaseID);
4591 else
4592 LE.write<uint32_t>(None);
4593 };
4594
Ben Langmuirfe971d92014-08-16 04:54:18 +00004595 // These values should be unique within a chain, since they will be read
4596 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004597 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4598 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4599 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4600 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4601 M->NumPreprocessedEntities);
4602 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4603 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4604 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4605 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004606 }
4607 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004608 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004609 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4610 Buffer.data(), Buffer.size());
4611 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004612
Richard Smithb9eab6d2014-03-20 19:44:17 +00004613 RecordData DeclUpdatesOffsetsRecord;
4614
Richard Smith59442b42014-03-20 20:07:19 +00004615 // Keep writing types, declarations, and declaration update records
4616 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004617 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4618 WriteTypeAbbrevs();
4619 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004620 do {
4621 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4622 while (!DeclTypesToEmit.empty()) {
4623 DeclOrType DOT = DeclTypesToEmit.front();
4624 DeclTypesToEmit.pop();
4625 if (DOT.isType())
4626 WriteType(DOT.getType());
4627 else
4628 WriteDecl(Context, DOT.getDecl());
4629 }
4630 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004631 Stream.ExitBlock();
4632
Richard Smithb9eab6d2014-03-20 19:44:17 +00004633 DoneWritingDeclsAndTypes = true;
4634
4635 // These things can only be done once we've written out decls and types.
4636 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004637 if (!DeclUpdatesOffsetsRecord.empty())
4638 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004639 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004640 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004641 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004642 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004643 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004644 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004645 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004646 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004647 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004648 WriteFPPragmaOptions(SemaRef.getFPOptions());
4649 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004650 WriteOpenCLExtensionTypes(SemaRef);
4651 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004652 WriteCUDAPragmas(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004653 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004654
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004655 // If we're emitting a module, write out the submodule information.
4656 if (WritingModule)
4657 WriteSubmodules(WritingModule);
4658
Douglas Gregor5204bde2011-08-02 16:26:37 +00004659 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4660
Douglas Gregord4df8652009-04-22 22:02:47 +00004661 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004662 if (!EagerlyDeserializedDecls.empty())
4663 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004664
4665 // Write the record containing tentative definitions.
4666 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004667 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004668
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004669 // Write the record containing unused file scoped decls.
4670 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004671 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004672
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004673 // Write the record containing weak undeclared identifiers.
4674 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004675 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004676 WeakUndeclaredIdentifiers);
4677
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004678 // Write the record containing ext_vector type names.
4679 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004680 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004681
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004682 // Write the record containing VTable uses information.
4683 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004684 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004685
Nico Weber72889432014-09-06 01:25:55 +00004686 // Write the record containing potentially unused local typedefs.
4687 if (!UnusedLocalTypedefNameCandidates.empty())
4688 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4689 UnusedLocalTypedefNameCandidates);
4690
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004691 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004692 if (!PendingInstantiations.empty())
4693 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004694
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004695 // Write the record containing declaration references of Sema.
4696 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004697 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004698
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004699 // Write the record containing CUDA-specific declaration references.
4700 if (!CUDASpecialDeclRefs.empty())
4701 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004702
4703 // Write the delegating constructors.
4704 if (!DelegatingCtorDecls.empty())
4705 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004706
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004707 // Write the known namespaces.
4708 if (!KnownNamespaces.empty())
4709 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004710
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004711 // Write the undefined internal functions and variables, and inline functions.
4712 if (!UndefinedButUsed.empty())
4713 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004714
4715 if (!DeleteExprsToAnalyze.empty())
4716 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4717
Douglas Gregor851443c2011-08-12 01:39:19 +00004718 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004719 for (auto *DC : UpdatedDeclContexts)
4720 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004721
Douglas Gregor959bb062011-12-03 01:15:29 +00004722 if (!WritingModule) {
4723 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004724 struct ModuleInfo {
4725 uint64_t ID;
4726 Module *M;
4727 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4728 };
Richard Smith56be7542014-03-21 00:33:59 +00004729 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004730 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004731 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004732 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4733 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004734 }
Richard Smith56be7542014-03-21 00:33:59 +00004735
4736 if (!Imports.empty()) {
4737 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4738 return A.ID < B.ID;
4739 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004740 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4741 return A.ID == B.ID;
4742 };
Richard Smith56be7542014-03-21 00:33:59 +00004743
4744 // Sort and deduplicate module IDs.
4745 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004746 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004747 Imports.end());
4748
4749 RecordData ImportedModules;
4750 for (const auto &Import : Imports) {
4751 ImportedModules.push_back(Import.ID);
4752 // FIXME: If the module has macros imported then later has declarations
4753 // imported, this location won't be the right one as a location for the
4754 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004755 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004756 }
4757
Douglas Gregor959bb062011-12-03 01:15:29 +00004758 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4759 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004760 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004761
Douglas Gregor404cdde2012-01-27 01:47:08 +00004762 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004763 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004764 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004765 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004766 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004767 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004768
Douglas Gregor08f01292009-04-17 22:13:46 +00004769 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004770 RecordData::value_type Record[] = {
4771 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004772 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004773 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004774
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004775 // Write the module file extension blocks.
4776 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004777 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004778
Adrian Prantladbd2b12015-09-22 23:26:31 +00004779 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004780}
4781
Richard Smithb9eab6d2014-03-20 19:44:17 +00004782void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004783 if (DeclUpdates.empty())
4784 return;
4785
Richard Smith59442b42014-03-20 20:07:19 +00004786 DeclUpdateMap LocalUpdates;
4787 LocalUpdates.swap(DeclUpdates);
4788
Richard Smith6ef42932014-03-20 21:02:00 +00004789 for (auto &DeclUpdate : LocalUpdates) {
4790 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004791
Richard Smithd28ac5b2014-03-22 23:33:22 +00004792 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004793 RecordData RecordData;
4794 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004795 for (auto &Update : DeclUpdate.second) {
4796 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4797
Richard Smith69c82bf2016-04-01 22:52:03 +00004798 // An updated body is emitted last, so that the reader doesn't need
4799 // to skip over the lazy body to reach statements for other records.
4800 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4801 HasUpdatedBody = true;
4802 else
4803 Record.push_back(Kind);
4804
Richard Smith6ef42932014-03-20 21:02:00 +00004805 switch (Kind) {
4806 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4807 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4808 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004809 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004810 Record.push_back(GetDeclRef(Update.getDecl()));
4811 break;
4812
Richard Smith4d235792014-08-07 18:53:08 +00004813 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004814 break;
4815
Richard Smith4d235792014-08-07 18:53:08 +00004816 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004817 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004818 break;
4819
John McCall32791cc2016-01-06 22:34:54 +00004820 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004821 Record.AddStmt(const_cast<Expr *>(
4822 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004823 break;
4824
Richard Smith4b054b22016-08-24 21:25:37 +00004825 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4826 Record.AddStmt(
4827 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4828 break;
4829
Richard Smithcd45dbc2014-04-19 03:48:30 +00004830 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4831 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004832 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004833 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004834 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004835 *Context, const_cast<CXXRecordDecl *>(RD)));
4836
4837 // This state is sometimes updated by template instantiation, when we
4838 // switch from the specialization referring to the template declaration
4839 // to it referring to the template definition.
4840 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4841 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004842 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004843 } else {
4844 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4845 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004846 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004847
4848 // The instantiation might have been resolved to a partial
4849 // specialization. If so, record which one.
4850 auto From = Spec->getInstantiatedFrom();
4851 if (auto PartialSpec =
4852 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4853 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004854 Record.AddDeclRef(PartialSpec);
4855 Record.AddTemplateArgumentList(
4856 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004857 } else {
4858 Record.push_back(false);
4859 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004860 }
4861 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004862 Record.AddSourceLocation(RD->getLocation());
4863 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004864 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004865
4866 // Instantiation may change attributes; write them all out afresh.
4867 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004868 if (D->hasAttrs())
4869 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004870
4871 // FIXME: Ensure we don't get here for explicit instantiations.
4872 break;
4873 }
4874
Richard Smithf8134002015-03-10 01:41:22 +00004875 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004876 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004877 break;
4878
Richard Smith564417a2014-03-20 21:47:22 +00004879 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4880 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004881 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4882 Record);
4883 break;
4884
Richard Smith6ef42932014-03-20 21:02:00 +00004885 case UPD_CXX_DEDUCED_RETURN_TYPE:
4886 Record.push_back(GetOrCreateTypeID(Update.getType()));
4887 break;
4888
4889 case UPD_DECL_MARKED_USED:
4890 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004891
4892 case UPD_MANGLING_NUMBER:
4893 case UPD_STATIC_LOCAL_NUMBER:
4894 Record.push_back(Update.getNumber());
4895 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004896
Alexey Bataev97720002014-11-11 04:05:39 +00004897 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004898 Record.AddSourceRange(
4899 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004900 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004901
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004902 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4903 Record.AddSourceRange(
4904 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4905 break;
4906
Richard Smith65ebb4a2015-03-26 04:09:53 +00004907 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004908 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004909 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004910
4911 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004912 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004913 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004914 }
4915 }
4916
Richard Smithd28ac5b2014-03-22 23:33:22 +00004917 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004918 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004919 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004920 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004921 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004922 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004923 }
4924
Richard Smithcd45dbc2014-04-19 03:48:30 +00004925 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004926 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004927 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004928}
4929
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004930void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004931 uint32_t Raw = Loc.getRawEncoding();
4932 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004933}
4934
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004935void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004936 AddSourceLocation(Range.getBegin(), Record);
4937 AddSourceLocation(Range.getEnd(), Record);
4938}
4939
Richard Smithf144b542016-04-08 21:54:32 +00004940void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4941 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004942 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004943 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004944}
4945
Richard Smithf144b542016-04-08 21:54:32 +00004946void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4947 Record->push_back(Value.isUnsigned());
4948 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004949}
4950
Richard Smithf144b542016-04-08 21:54:32 +00004951void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4952 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00004953}
4954
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004955void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004956 Record.push_back(getIdentifierRef(II));
4957}
4958
Sebastian Redl539c5062010-08-18 23:57:32 +00004959IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004960 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004961 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004962
Sebastian Redl539c5062010-08-18 23:57:32 +00004963 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004964 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004965 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004966 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004967}
4968
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004969MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004970 // Don't emit builtin macros like __LINE__ to the AST file unless they
4971 // have been redefined by the header (in which case they are not
4972 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004973 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004974 return 0;
4975
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004976 MacroID &ID = MacroIDs[MI];
4977 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004978 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004979 MacroInfoToEmitData Info = { Name, MI, ID };
4980 MacroInfosToEmit.push_back(Info);
4981 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004982 return ID;
4983}
4984
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004985MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004986 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004987 return 0;
4988
4989 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4990 return MacroIDs[MI];
4991}
4992
4993uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004994 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004995}
4996
Richard Smithe64e7452016-04-18 21:54:58 +00004997void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
4998 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00004999}
5000
Sebastian Redl539c5062010-08-18 23:57:32 +00005001SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005002 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005003 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005004 }
5005
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005006 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005007 if (SID == 0 && Chain) {
5008 // This might trigger a ReadSelector callback, which will set the ID for
5009 // this selector.
5010 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005011 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005012 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005013 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005014 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005015 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005016 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005017 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005018}
5019
Richard Smithe64e7452016-04-18 21:54:58 +00005020void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5021 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005022}
5023
Richard Smith290d8012016-04-06 17:06:00 +00005024void ASTRecordWriter::AddTemplateArgumentLocInfo(
5025 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005026 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005027 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005028 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005029 break;
5030 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005031 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005032 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005033 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005034 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5035 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005036 break;
5037 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005038 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5039 AddSourceLocation(Arg.getTemplateNameLoc());
5040 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005041 break;
John McCall0ad16662009-10-29 08:12:44 +00005042 case TemplateArgument::Null:
5043 case TemplateArgument::Integral:
5044 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005045 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005046 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005047 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005048 break;
5049 }
5050}
5051
Richard Smith290d8012016-04-06 17:06:00 +00005052void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5053 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005054
5055 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5056 bool InfoHasSameExpr
5057 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005058 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005059 if (InfoHasSameExpr)
5060 return; // Avoid storing the same expr twice.
5061 }
Richard Smith290d8012016-04-06 17:06:00 +00005062 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005063}
5064
Richard Smith290d8012016-04-06 17:06:00 +00005065void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005066 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005067 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005068 return;
5069 }
5070
Richard Smith290d8012016-04-06 17:06:00 +00005071 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005072}
5073
Richard Smith290d8012016-04-06 17:06:00 +00005074void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5075 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005076
Richard Smith290d8012016-04-06 17:06:00 +00005077 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005078 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005079 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005080}
5081
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005082void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005083 Record.push_back(GetOrCreateTypeID(T));
5084}
5085
Richard Smith2095ffe2015-03-16 20:11:03 +00005086TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005087 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005088 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5089 if (T.isNull())
5090 return TypeIdx();
5091 assert(!T.getLocalFastQualifiers());
5092
5093 TypeIdx &Idx = TypeIdxs[T];
5094 if (Idx.getIndex() == 0) {
5095 if (DoneWritingDeclsAndTypes) {
5096 assert(0 && "New type seen after serializing all the types to emit!");
5097 return TypeIdx();
5098 }
5099
5100 // We haven't seen this type before. Assign it a new ID and put it
5101 // into the queue of types to emit.
5102 Idx = TypeIdx(NextTypeID++);
5103 DeclTypesToEmit.push(T);
5104 }
5105 return Idx;
5106 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005107}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005108
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005109TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005110 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005111 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5112 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005113 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005114 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005115
Richard Smith2095ffe2015-03-16 20:11:03 +00005116 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5117 assert(I != TypeIdxs.end() && "Type not emitted!");
5118 return I->second;
5119 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005120}
5121
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005122void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005123 Record.push_back(GetDeclRef(D));
5124}
5125
Sebastian Redl539c5062010-08-18 23:57:32 +00005126DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005127 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005128
5129 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005130 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005131 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005132
5133 // If D comes from an AST file, its declaration ID is already known and
5134 // fixed.
5135 if (D->isFromASTFile())
5136 return D->getGlobalID();
5137
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005138 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005139 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005140 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005141 if (DoneWritingDeclsAndTypes) {
5142 assert(0 && "New decl seen after serializing all the decls to emit!");
5143 return 0;
5144 }
5145
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005146 // We haven't seen this declaration before. Give it a new ID and
5147 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005148 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005149 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005150 }
5151
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005152 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005153}
5154
Sebastian Redl539c5062010-08-18 23:57:32 +00005155DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005156 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005157 return 0;
5158
Douglas Gregorb3163e52012-01-05 22:33:30 +00005159 // If D comes from an AST file, its declaration ID is already known and
5160 // fixed.
5161 if (D->isFromASTFile())
5162 return D->getGlobalID();
5163
Douglas Gregore84a9da2009-04-20 20:36:09 +00005164 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5165 return DeclIDs[D];
5166}
5167
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005168void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005169 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005170 assert(D);
5171
5172 SourceLocation Loc = D->getLocation();
5173 if (Loc.isInvalid())
5174 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005175
5176 // We only keep track of the file-level declarations of each file.
5177 if (!D->getLexicalDeclContext()->isFileContext())
5178 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005179 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5180 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005181 if (isa<ParmVarDecl>(D))
5182 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005183
5184 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005185 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005186 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005187 FileID FID;
5188 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005189 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005190 if (FID.isInvalid())
5191 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005192 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005193
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005194 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005195 if (!Info)
5196 Info = new DeclIDInFileInfo();
5197
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005198 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005199 LocDeclIDsTy &Decls = Info->DeclIDs;
5200
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005201 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005202 Decls.push_back(LocDecl);
5203 return;
5204 }
5205
Benjamin Kramer45025c02013-08-24 13:22:59 +00005206 LocDeclIDsTy::iterator I =
5207 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005208
5209 Decls.insert(I, LocDecl);
5210}
5211
Richard Smithe64e7452016-04-18 21:54:58 +00005212void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005213 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005214 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005215 switch (Name.getNameKind()) {
5216 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005217 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005218 break;
5219
5220 case DeclarationName::ObjCZeroArgSelector:
5221 case DeclarationName::ObjCOneArgSelector:
5222 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005223 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005224 break;
5225
5226 case DeclarationName::CXXConstructorName:
5227 case DeclarationName::CXXDestructorName:
5228 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005229 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005230 break;
5231
5232 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005233 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005234 break;
5235
Alexis Hunt3d221f22009-11-29 07:34:05 +00005236 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005237 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005238 break;
5239
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005240 case DeclarationName::CXXUsingDirective:
5241 // No extra data to emit
5242 break;
5243 }
5244}
Chris Lattnerca025db2010-05-07 21:43:38 +00005245
Richard Smithd08aeb62014-08-28 01:33:39 +00005246unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5247 assert(needsAnonymousDeclarationNumber(D) &&
5248 "expected an anonymous declaration");
5249
5250 // Number the anonymous declarations within this context, if we've not
5251 // already done so.
5252 auto It = AnonymousDeclarationNumbers.find(D);
5253 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005254 auto *DC = D->getLexicalDeclContext();
5255 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5256 AnonymousDeclarationNumbers[ND] = Number;
5257 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005258
5259 It = AnonymousDeclarationNumbers.find(D);
5260 assert(It != AnonymousDeclarationNumbers.end() &&
5261 "declaration not found within its lexical context");
5262 }
5263
5264 return It->second;
5265}
5266
Richard Smith290d8012016-04-06 17:06:00 +00005267void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5268 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005269 switch (Name.getNameKind()) {
5270 case DeclarationName::CXXConstructorName:
5271 case DeclarationName::CXXDestructorName:
5272 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005273 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005274 break;
5275
5276 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005277 AddSourceLocation(SourceLocation::getFromRawEncoding(
5278 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005279 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005280 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005281 break;
5282
5283 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005284 AddSourceLocation(SourceLocation::getFromRawEncoding(
5285 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005286 break;
5287
5288 case DeclarationName::Identifier:
5289 case DeclarationName::ObjCZeroArgSelector:
5290 case DeclarationName::ObjCOneArgSelector:
5291 case DeclarationName::ObjCMultiArgSelector:
5292 case DeclarationName::CXXUsingDirective:
5293 break;
5294 }
5295}
5296
Richard Smith290d8012016-04-06 17:06:00 +00005297void ASTRecordWriter::AddDeclarationNameInfo(
5298 const DeclarationNameInfo &NameInfo) {
5299 AddDeclarationName(NameInfo.getName());
5300 AddSourceLocation(NameInfo.getLoc());
5301 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005302}
5303
Richard Smith290d8012016-04-06 17:06:00 +00005304void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5305 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5306 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005307 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005308 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005309}
5310
Richard Smithe64e7452016-04-18 21:54:58 +00005311void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005312 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005313 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005314 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005315
5316 // Push each of the NNS's onto a stack for serialization in reverse order.
5317 while (NNS) {
5318 NestedNames.push_back(NNS);
5319 NNS = NNS->getPrefix();
5320 }
5321
Richard Smithe64e7452016-04-18 21:54:58 +00005322 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005323 while(!NestedNames.empty()) {
5324 NNS = NestedNames.pop_back_val();
5325 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005326 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005327 switch (Kind) {
5328 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005329 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005330 break;
5331
5332 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005333 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005334 break;
5335
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005336 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005337 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005338 break;
5339
Chris Lattnerca025db2010-05-07 21:43:38 +00005340 case NestedNameSpecifier::TypeSpec:
5341 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005342 AddTypeRef(QualType(NNS->getAsType(), 0));
5343 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005344 break;
5345
5346 case NestedNameSpecifier::Global:
5347 // Don't need to write an associated value.
5348 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005349
5350 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005351 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005352 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005353 }
5354 }
5355}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005356
Richard Smith290d8012016-04-06 17:06:00 +00005357void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005358 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005359 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005360 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005361
5362 // Push each of the nested-name-specifiers's onto a stack for
5363 // serialization in reverse order.
5364 while (NNS) {
5365 NestedNames.push_back(NNS);
5366 NNS = NNS.getPrefix();
5367 }
5368
Richard Smith290d8012016-04-06 17:06:00 +00005369 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005370 while(!NestedNames.empty()) {
5371 NNS = NestedNames.pop_back_val();
5372 NestedNameSpecifier::SpecifierKind Kind
5373 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005374 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005375 switch (Kind) {
5376 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005377 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5378 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005379 break;
5380
5381 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005382 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5383 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005384 break;
5385
5386 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005387 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5388 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005389 break;
5390
5391 case NestedNameSpecifier::TypeSpec:
5392 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005393 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5394 AddTypeLoc(NNS.getTypeLoc());
5395 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005396 break;
5397
5398 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005399 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005400 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005401
5402 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005403 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5404 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005405 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005406 }
5407 }
5408}
5409
Richard Smith290d8012016-04-06 17:06:00 +00005410void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005411 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005412 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005413 switch (Kind) {
5414 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005415 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005416 break;
5417
5418 case TemplateName::OverloadedTemplate: {
5419 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005420 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005421 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005422 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005423 break;
5424 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005425
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005426 case TemplateName::QualifiedTemplate: {
5427 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005428 AddNestedNameSpecifier(QualT->getQualifier());
5429 Record->push_back(QualT->hasTemplateKeyword());
5430 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005431 break;
5432 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005433
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005434 case TemplateName::DependentTemplate: {
5435 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005436 AddNestedNameSpecifier(DepT->getQualifier());
5437 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005438 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005439 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005440 else
Richard Smith290d8012016-04-06 17:06:00 +00005441 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005442 break;
5443 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005444
5445 case TemplateName::SubstTemplateTemplateParm: {
5446 SubstTemplateTemplateParmStorage *subst
5447 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005448 AddDeclRef(subst->getParameter());
5449 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005450 break;
5451 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005452
5453 case TemplateName::SubstTemplateTemplateParmPack: {
5454 SubstTemplateTemplateParmPackStorage *SubstPack
5455 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005456 AddDeclRef(SubstPack->getParameterPack());
5457 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005458 break;
5459 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005460 }
5461}
5462
Richard Smith290d8012016-04-06 17:06:00 +00005463void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5464 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005465 switch (Arg.getKind()) {
5466 case TemplateArgument::Null:
5467 break;
5468 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005469 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005470 break;
5471 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005472 AddDeclRef(Arg.getAsDecl());
5473 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005474 break;
5475 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005476 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005477 break;
5478 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005479 AddAPSInt(Arg.getAsIntegral());
5480 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005481 break;
5482 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005483 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005484 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005485 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005486 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005487 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005488 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005489 else
Richard Smith290d8012016-04-06 17:06:00 +00005490 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005491 break;
5492 case TemplateArgument::Expression:
5493 AddStmt(Arg.getAsExpr());
5494 break;
5495 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005496 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005497 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005498 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005499 break;
5500 }
5501}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005502
Richard Smithe64e7452016-04-18 21:54:58 +00005503void ASTRecordWriter::AddTemplateParameterList(
5504 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005505 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005506 AddSourceLocation(TemplateParams->getTemplateLoc());
5507 AddSourceLocation(TemplateParams->getLAngleLoc());
5508 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005509 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005510 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005511 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005512 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005513}
5514
5515/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005516void ASTRecordWriter::AddTemplateArgumentList(
5517 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005518 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005519 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005520 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005521 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005522}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005523
Richard Smith290d8012016-04-06 17:06:00 +00005524void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5525 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005526 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005527 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5528 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5529 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005530 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005531 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005532 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005533}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005534
Richard Smithe64e7452016-04-18 21:54:58 +00005535void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5536 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005537 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005538 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005539 AddDeclRef(I.getDecl());
5540 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005541 }
5542}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005543
Richard Smith290d8012016-04-06 17:06:00 +00005544// FIXME: Move this out of the main ASTRecordWriter interface.
5545void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5546 Record->push_back(Base.isVirtual());
5547 Record->push_back(Base.isBaseOfClass());
5548 Record->push_back(Base.getAccessSpecifierAsWritten());
5549 Record->push_back(Base.getInheritConstructors());
5550 AddTypeSourceInfo(Base.getTypeSourceInfo());
5551 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005552 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005553 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005554}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005555
Richard Smith645d2cf2016-04-14 00:29:55 +00005556static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5557 ArrayRef<CXXBaseSpecifier> Bases) {
5558 ASTWriter::RecordData Record;
5559 ASTRecordWriter Writer(W, Record);
5560 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005561
Richard Smith645d2cf2016-04-14 00:29:55 +00005562 for (auto &Base : Bases)
5563 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005564
Richard Smith645d2cf2016-04-14 00:29:55 +00005565 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005566}
5567
Richard Smith290d8012016-04-06 17:06:00 +00005568// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005569void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5570 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5571}
5572
Richard Smithaa165cf2016-04-13 21:57:08 +00005573static uint64_t
5574EmitCXXCtorInitializers(ASTWriter &W,
5575 ArrayRef<CXXCtorInitializer *> CtorInits) {
5576 ASTWriter::RecordData Record;
5577 ASTRecordWriter Writer(W, Record);
5578 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005579
Richard Smithaa165cf2016-04-13 21:57:08 +00005580 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005581 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005582 Writer.push_back(CTOR_INITIALIZER_BASE);
5583 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5584 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005585 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005586 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5587 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005588 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005589 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5590 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005591 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005592 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5593 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005594 }
Francois Pichetd583da02010-12-04 09:14:42 +00005595
Richard Smithaa165cf2016-04-13 21:57:08 +00005596 Writer.AddSourceLocation(Init->getMemberLocation());
5597 Writer.AddStmt(Init->getInit());
5598 Writer.AddSourceLocation(Init->getLParenLoc());
5599 Writer.AddSourceLocation(Init->getRParenLoc());
5600 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005601 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005602 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005603 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005604
5605 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005606}
5607
Richard Smithaa165cf2016-04-13 21:57:08 +00005608// FIXME: Move this out of the main ASTRecordWriter interface.
5609void ASTRecordWriter::AddCXXCtorInitializers(
5610 ArrayRef<CXXCtorInitializer *> CtorInits) {
5611 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005612}
5613
Richard Smith290d8012016-04-06 17:06:00 +00005614void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005615 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005616 Record->push_back(Data.IsLambda);
5617 Record->push_back(Data.UserDeclaredConstructor);
5618 Record->push_back(Data.UserDeclaredSpecialMembers);
5619 Record->push_back(Data.Aggregate);
5620 Record->push_back(Data.PlainOldData);
5621 Record->push_back(Data.Empty);
5622 Record->push_back(Data.Polymorphic);
5623 Record->push_back(Data.Abstract);
5624 Record->push_back(Data.IsStandardLayout);
5625 Record->push_back(Data.HasNoNonEmptyBases);
5626 Record->push_back(Data.HasPrivateFields);
5627 Record->push_back(Data.HasProtectedFields);
5628 Record->push_back(Data.HasPublicFields);
5629 Record->push_back(Data.HasMutableFields);
5630 Record->push_back(Data.HasVariantMembers);
5631 Record->push_back(Data.HasOnlyCMembers);
5632 Record->push_back(Data.HasInClassInitializer);
5633 Record->push_back(Data.HasUninitializedReferenceMember);
5634 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005635 Record->push_back(Data.HasInheritedConstructor);
5636 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005637 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5638 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5639 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5640 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5641 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5642 Record->push_back(Data.DefaultedDestructorIsDeleted);
5643 Record->push_back(Data.HasTrivialSpecialMembers);
5644 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5645 Record->push_back(Data.HasIrrelevantDestructor);
5646 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5647 Record->push_back(Data.HasDefaultedDefaultConstructor);
5648 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5649 Record->push_back(Data.HasConstexprDefaultConstructor);
5650 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5651 Record->push_back(Data.ComputedVisibleConversions);
5652 Record->push_back(Data.UserProvidedDefaultConstructor);
5653 Record->push_back(Data.DeclaredSpecialMembers);
5654 Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
5655 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5656 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5657 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005658 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005659
Richard Smith290d8012016-04-06 17:06:00 +00005660 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005661 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005662 AddCXXBaseSpecifiers(Data.bases());
5663
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005664 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005665 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005666 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005667 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005668
Richard Smith290d8012016-04-06 17:06:00 +00005669 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5670 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005671 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005672 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005673
5674 // Add lambda-specific data.
5675 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005676 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005677 Record->push_back(Lambda.Dependent);
5678 Record->push_back(Lambda.IsGenericLambda);
5679 Record->push_back(Lambda.CaptureDefault);
5680 Record->push_back(Lambda.NumCaptures);
5681 Record->push_back(Lambda.NumExplicitCaptures);
5682 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005683 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005684 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005685 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005686 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005687 AddSourceLocation(Capture.getLocation());
5688 Record->push_back(Capture.isImplicit());
5689 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005690 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005691 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005692 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005693 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005694 break;
5695 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005696 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005697 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005698 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005699 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005700 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005701 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005702 break;
5703 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005704 }
5705 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005706}
5707
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005708void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005709 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005710 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005711 assert(FirstDeclID == NextDeclID &&
5712 FirstTypeID == NextTypeID &&
5713 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005714 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005715 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005716 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005717 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005718
Sebastian Redl07a89a82010-07-30 00:29:29 +00005719 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005720
Richard Smith7f330cd2015-03-18 01:42:29 +00005721 // Note, this will get called multiple times, once one the reader starts up
5722 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005723 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5724 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5725 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005726 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005727 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005728 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005729 NextDeclID = FirstDeclID;
5730 NextTypeID = FirstTypeID;
5731 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005732 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005733 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005734 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005735}
5736
Sebastian Redl539c5062010-08-18 23:57:32 +00005737void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005738 // Always keep the highest ID. See \p TypeRead() for more information.
5739 IdentID &StoredID = IdentifierIDs[II];
5740 if (ID > StoredID)
5741 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005742}
5743
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005744void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005745 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005746 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005747 if (ID > StoredID)
5748 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005749}
5750
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005751void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005752 // Always take the highest-numbered type index. This copes with an interesting
5753 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005754 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005755 // keep the higher-numbered entry so that we can properly write it out to
5756 // the AST file.
5757 TypeIdx &StoredIdx = TypeIdxs[T];
5758 if (Idx.getIndex() >= StoredIdx.getIndex())
5759 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005760}
5761
Sebastian Redl539c5062010-08-18 23:57:32 +00005762void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005763 // Always keep the highest ID. See \p TypeRead() for more information.
5764 SelectorID &StoredID = SelectorIDs[S];
5765 if (ID > StoredID)
5766 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005767}
Douglas Gregor91096292010-10-02 19:29:26 +00005768
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005769void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005770 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005771 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005772 MacroDefinitions[MD] = ID;
5773}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005774
Douglas Gregore37a85a2011-12-02 17:30:13 +00005775void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5776 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5777 SubmoduleIDs[Mod] = ID;
5778}
5779
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005780void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005781 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005782 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005783 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005784 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005785 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005786 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005787 // A forward reference was mutated into a definition. Rewrite it.
5788 // FIXME: This happens during template instantiation, should we
5789 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005790 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5791 "completed a tag from another module but not by instantiation?");
5792 DeclUpdates[RD].push_back(
5793 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005794 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005795 }
5796}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005797
Richard Smithf983f7f2015-10-13 00:23:25 +00005798static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5799 if (D->isFromASTFile())
5800 return true;
5801
Richard Smithf983f7f2015-10-13 00:23:25 +00005802 // The predefined __va_list_tag struct is imported if we imported any decls.
5803 // FIXME: This is a gross hack.
5804 return D == D->getASTContext().getVaListTagDecl();
5805}
5806
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005807void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005808 if (Chain && Chain->isProcessingUpdateRecords()) return;
5809 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005810 "Should not add lookup results to non-lookup contexts!");
5811
Vassil Vassilev632eac32016-03-16 11:17:04 +00005812 // TU is handled elsewhere.
5813 if (isa<TranslationUnitDecl>(DC))
5814 return;
5815
5816 // Namespaces are handled elsewhere, except for template instantiations of
5817 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5818 // local instantiations are added to an imported context. Only happens when
5819 // adding ADL lookup candidates, for example templated friends.
5820 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5821 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005822 return;
5823
Richard Smithf983f7f2015-10-13 00:23:25 +00005824 // We're only interested in cases where a local declaration is added to an
5825 // imported context.
5826 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5827 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005828
Richard Smith0f4e2c42015-08-06 04:23:48 +00005829 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005830 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005831 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005832 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5833 // We're adding a visible declaration to a predefined decl context. Ensure
5834 // that we write out all of its lookup results so we don't get a nasty
5835 // surprise when we try to emit its lookup table.
5836 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005837 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005838 }
Richard Smithc26d9742016-10-06 20:30:51 +00005839 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005840}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005841
5842void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005843 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005844 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005845
5846 // We're only interested in cases where a local declaration is added to an
5847 // imported context.
5848 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5849 return;
5850
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005851 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005852 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005853
5854 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005855 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005856 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005857 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005858}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005859
Richard Smith564417a2014-03-20 21:47:22 +00005860void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005861 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005862 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5863 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005864 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005865 // If we don't already know the exception specification for this redecl
5866 // chain, add an update record for it.
5867 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5868 ->getType()
5869 ->castAs<FunctionProtoType>()
5870 ->getExceptionSpecType()))
5871 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5872 });
Richard Smith564417a2014-03-20 21:47:22 +00005873}
5874
Richard Smith1fa5d642013-05-11 05:45:24 +00005875void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005876 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005877 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005878 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005879 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005880 DeclUpdates[D].push_back(
5881 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5882 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005883}
5884
Richard Smithf8134002015-03-10 01:41:22 +00005885void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5886 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005887 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005888 assert(!WritingAST && "Already writing the AST!");
5889 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005890 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005891 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005892 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5893 });
Richard Smithf8134002015-03-10 01:41:22 +00005894}
5895
Sebastian Redlab238a72011-04-24 16:28:06 +00005896void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005897 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005898 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005899 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005900 return; // Declaration not imported from PCH.
5901
Richard Smith4d235792014-08-07 18:53:08 +00005902 // Implicit function decl from a PCH was defined.
5903 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005904}
5905
Richard Smithd28ac5b2014-03-22 23:33:22 +00005906void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005907 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005908 assert(!WritingAST && "Already writing the AST!");
5909 if (!D->isFromASTFile())
5910 return;
5911
Richard Smith9e2341d2015-03-23 03:25:59 +00005912 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005913}
5914
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005915void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005916 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005917 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005918 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005919 return;
5920
5921 // Since the actual instantiation is delayed, this really means that we need
5922 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005923 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005924 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5925 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005926}
5927
John McCall32791cc2016-01-06 22:34:54 +00005928void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005929 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005930 assert(!WritingAST && "Already writing the AST!");
5931 if (!D->isFromASTFile())
5932 return;
5933
5934 DeclUpdates[D].push_back(
5935 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5936}
5937
Richard Smith4b054b22016-08-24 21:25:37 +00005938void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5939 assert(!WritingAST && "Already writing the AST!");
5940 if (!D->isFromASTFile())
5941 return;
5942
5943 DeclUpdates[D].push_back(
5944 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5945}
5946
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005947void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5948 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005949 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005950 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005951 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005952 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005953
5954 assert(IFD->getDefinition() && "Category on a class without a definition?");
5955 ObjCClassesWithCategories.insert(
5956 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005957}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005958
Eli Friedman276dd182013-09-05 00:02:25 +00005959void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005960 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005961 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005962
5963 // If there is *any* declaration of the entity that's not from an AST file,
5964 // we can skip writing the update record. We make sure that isUsed() triggers
5965 // completion of the redeclaration chain of the entity.
5966 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5967 if (IsLocalDecl(Prev))
5968 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005969
Aaron Ballman4f45b712014-03-21 15:22:56 +00005970 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005971}
Alexey Bataev97720002014-11-11 04:05:39 +00005972
5973void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005974 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005975 assert(!WritingAST && "Already writing the AST!");
5976 if (!D->isFromASTFile())
5977 return;
5978
5979 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5980}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005981
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005982void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5983 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005984 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005985 assert(!WritingAST && "Already writing the AST!");
5986 if (!D->isFromASTFile())
5987 return;
5988
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005989 DeclUpdates[D].push_back(
5990 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005991}
5992
Richard Smith4caa4492015-05-15 02:34:32 +00005993void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005994 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005995 assert(!WritingAST && "Already writing the AST!");
5996 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005997 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005998}
Alex Denisovfde64952015-06-26 05:28:36 +00005999
6000void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6001 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006002 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006003 assert(!WritingAST && "Already writing the AST!");
6004 if (!Record->isFromASTFile())
6005 return;
6006 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6007}
Richard Smithc26d9742016-10-06 20:30:51 +00006008
6009void ASTWriter::AddedCXXTemplateSpecialization(
6010 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6011 assert(!WritingAST && "Already writing the AST!");
6012
6013 if (!TD->getFirstDecl()->isFromASTFile())
6014 return;
6015 if (Chain && Chain->isProcessingUpdateRecords())
6016 return;
6017
6018 DeclsToEmitEvenIfUnreferenced.push_back(D);
6019}
6020
6021void ASTWriter::AddedCXXTemplateSpecialization(
6022 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6023 assert(!WritingAST && "Already writing the AST!");
6024
6025 if (!TD->getFirstDecl()->isFromASTFile())
6026 return;
6027 if (Chain && Chain->isProcessingUpdateRecords())
6028 return;
6029
6030 DeclsToEmitEvenIfUnreferenced.push_back(D);
6031}
6032
6033void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6034 const FunctionDecl *D) {
6035 assert(!WritingAST && "Already writing the AST!");
6036
6037 if (!TD->getFirstDecl()->isFromASTFile())
6038 return;
6039 if (Chain && Chain->isProcessingUpdateRecords())
6040 return;
6041
6042 DeclsToEmitEvenIfUnreferenced.push_back(D);
6043}