blob: 2a5eda436f096e367270bcdc41c7f150aa4cd8db [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());
632 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000633 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000634 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000635}
636void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
637 VisitFunctionTypeLoc(TL);
638}
639void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
640 VisitFunctionTypeLoc(TL);
641}
John McCallb96ec562009-12-04 22:46:56 +0000642void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000643 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000644}
John McCall17001972009-10-18 01:05:36 +0000645void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000646 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000647}
Manman Rene6be26c2016-09-13 17:25:08 +0000648void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
649 if (TL.getNumProtocols()) {
650 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
651 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
652 }
653 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
654 Record.AddSourceLocation(TL.getProtocolLoc(i));
655}
John McCall17001972009-10-18 01:05:36 +0000656void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000657 Record.AddSourceLocation(TL.getTypeofLoc());
658 Record.AddSourceLocation(TL.getLParenLoc());
659 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000660}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000661
John McCall17001972009-10-18 01:05:36 +0000662void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000663 Record.AddSourceLocation(TL.getTypeofLoc());
664 Record.AddSourceLocation(TL.getLParenLoc());
665 Record.AddSourceLocation(TL.getRParenLoc());
666 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000667}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000668
John McCall17001972009-10-18 01:05:36 +0000669void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000670 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000671}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000672
Alexis Hunte852b102011-05-24 22:41:36 +0000673void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000674 Record.AddSourceLocation(TL.getKWLoc());
675 Record.AddSourceLocation(TL.getLParenLoc());
676 Record.AddSourceLocation(TL.getRParenLoc());
677 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000678}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000679
Richard Smith30482bc2011-02-20 03:19:35 +0000680void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000681 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000682}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000683
John McCall17001972009-10-18 01:05:36 +0000684void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000685 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000686}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000687
John McCall17001972009-10-18 01:05:36 +0000688void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000689 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000690}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000691
John McCall81904512011-01-06 01:58:22 +0000692void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000693 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000694 if (TL.hasAttrOperand()) {
695 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000696 Record.AddSourceLocation(range.getBegin());
697 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000698 }
699 if (TL.hasAttrExprOperand()) {
700 Expr *operand = TL.getAttrExprOperand();
701 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000702 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000703 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000704 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000705 }
706}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000707
John McCall17001972009-10-18 01:05:36 +0000708void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000709 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000710}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000711
John McCallcebee162009-10-18 09:09:24 +0000712void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
713 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000714 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000715}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000716
Douglas Gregorada4b792011-01-14 02:55:32 +0000717void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
718 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000719 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000720}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000721
John McCall17001972009-10-18 01:05:36 +0000722void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
723 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000724 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
725 Record.AddSourceLocation(TL.getTemplateNameLoc());
726 Record.AddSourceLocation(TL.getLAngleLoc());
727 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000728 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000729 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
730 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000731}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000732
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000733void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000734 Record.AddSourceLocation(TL.getLParenLoc());
735 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000736}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000737
Abramo Bagnara6150c882010-05-11 21:36:43 +0000738void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000739 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
740 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000741}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000742
John McCalle78aac42010-03-10 03:28:59 +0000743void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000744 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000745}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000746
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000747void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000748 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
749 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
750 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000751}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000752
John McCallc392f372010-06-11 00:33:02 +0000753void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
754 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000755 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
756 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
757 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
758 Record.AddSourceLocation(TL.getTemplateNameLoc());
759 Record.AddSourceLocation(TL.getLAngleLoc());
760 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000761 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000762 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
763 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000764}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000765
Douglas Gregord2fa7662010-12-20 02:24:11 +0000766void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000767 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000768}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000769
John McCall17001972009-10-18 01:05:36 +0000770void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000771 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000772}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000773
John McCall8b07ec22010-05-15 11:32:37 +0000774void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
775 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000776 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
777 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000778 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000779 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
780 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
781 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000782 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000783 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000784}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000785
John McCallfc93cf92009-10-22 22:37:11 +0000786void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000787 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000788}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000789
Eli Friedman0dfb8892011-10-06 23:00:33 +0000790void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000791 Record.AddSourceLocation(TL.getKWLoc());
792 Record.AddSourceLocation(TL.getLParenLoc());
793 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000794}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000795
Xiuli Pan9c14e282016-01-09 12:53:17 +0000796void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000797 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000798}
John McCall8f115c62009-10-16 21:56:05 +0000799
Richard Smith01b2cb42014-07-26 06:37:51 +0000800void ASTWriter::WriteTypeAbbrevs() {
801 using namespace llvm;
802
David Blaikieb44f0bf2017-01-04 22:36:43 +0000803 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000804
805 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000806 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000807 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
808 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
809 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000810 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000811
812 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000813 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000814 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
815 // FunctionType
816 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
817 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
818 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
819 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
820 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
821 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
822 // FunctionProtoType
823 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
824 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
825 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
826 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
827 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
828 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
829 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
830 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000831 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000832}
833
Chris Lattner19cea4e2009-04-22 05:57:30 +0000834//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000835// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000836//===----------------------------------------------------------------------===//
837
Chris Lattner28fa4e62009-04-26 22:26:21 +0000838static void EmitBlockID(unsigned ID, const char *Name,
839 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000840 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000841 Record.clear();
842 Record.push_back(ID);
843 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
844
845 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000846 if (!Name || Name[0] == 0)
847 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000848 Record.clear();
849 while (*Name)
850 Record.push_back(*Name++);
851 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
852}
853
854static void EmitRecordID(unsigned ID, const char *Name,
855 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000856 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000857 Record.clear();
858 Record.push_back(ID);
859 while (*Name)
860 Record.push_back(*Name++);
861 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000862}
863
864static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000865 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000866#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000867 RECORD(STMT_STOP);
868 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000869 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000870 RECORD(STMT_NULL);
871 RECORD(STMT_COMPOUND);
872 RECORD(STMT_CASE);
873 RECORD(STMT_DEFAULT);
874 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000875 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000876 RECORD(STMT_IF);
877 RECORD(STMT_SWITCH);
878 RECORD(STMT_WHILE);
879 RECORD(STMT_DO);
880 RECORD(STMT_FOR);
881 RECORD(STMT_GOTO);
882 RECORD(STMT_INDIRECT_GOTO);
883 RECORD(STMT_CONTINUE);
884 RECORD(STMT_BREAK);
885 RECORD(STMT_RETURN);
886 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000887 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000888 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000889 RECORD(EXPR_PREDEFINED);
890 RECORD(EXPR_DECL_REF);
891 RECORD(EXPR_INTEGER_LITERAL);
892 RECORD(EXPR_FLOATING_LITERAL);
893 RECORD(EXPR_IMAGINARY_LITERAL);
894 RECORD(EXPR_STRING_LITERAL);
895 RECORD(EXPR_CHARACTER_LITERAL);
896 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000897 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000898 RECORD(EXPR_UNARY_OPERATOR);
899 RECORD(EXPR_SIZEOF_ALIGN_OF);
900 RECORD(EXPR_ARRAY_SUBSCRIPT);
901 RECORD(EXPR_CALL);
902 RECORD(EXPR_MEMBER);
903 RECORD(EXPR_BINARY_OPERATOR);
904 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
905 RECORD(EXPR_CONDITIONAL_OPERATOR);
906 RECORD(EXPR_IMPLICIT_CAST);
907 RECORD(EXPR_CSTYLE_CAST);
908 RECORD(EXPR_COMPOUND_LITERAL);
909 RECORD(EXPR_EXT_VECTOR_ELEMENT);
910 RECORD(EXPR_INIT_LIST);
911 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000912 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000913 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000914 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000915 RECORD(EXPR_VA_ARG);
916 RECORD(EXPR_ADDR_LABEL);
917 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000918 RECORD(EXPR_CHOOSE);
919 RECORD(EXPR_GNU_NULL);
920 RECORD(EXPR_SHUFFLE_VECTOR);
921 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000922 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000923 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000924 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000925 RECORD(EXPR_OBJC_ARRAY_LITERAL);
926 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000927 RECORD(EXPR_OBJC_ENCODE);
928 RECORD(EXPR_OBJC_SELECTOR_EXPR);
929 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
930 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
931 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
932 RECORD(EXPR_OBJC_KVC_REF_EXPR);
933 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000934 RECORD(STMT_OBJC_FOR_COLLECTION);
935 RECORD(STMT_OBJC_CATCH);
936 RECORD(STMT_OBJC_FINALLY);
937 RECORD(STMT_OBJC_AT_TRY);
938 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
939 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000940 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000941 RECORD(STMT_CXX_CATCH);
942 RECORD(STMT_CXX_TRY);
943 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000944 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000945 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000946 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000947 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000948 RECORD(EXPR_CXX_STATIC_CAST);
949 RECORD(EXPR_CXX_DYNAMIC_CAST);
950 RECORD(EXPR_CXX_REINTERPRET_CAST);
951 RECORD(EXPR_CXX_CONST_CAST);
952 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000953 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000954 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000955 RECORD(EXPR_CXX_BOOL_LITERAL);
956 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000957 RECORD(EXPR_CXX_TYPEID_EXPR);
958 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000959 RECORD(EXPR_CXX_THIS);
960 RECORD(EXPR_CXX_THROW);
961 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000962 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000963 RECORD(EXPR_CXX_BIND_TEMPORARY);
964 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
965 RECORD(EXPR_CXX_NEW);
966 RECORD(EXPR_CXX_DELETE);
967 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
968 RECORD(EXPR_EXPR_WITH_CLEANUPS);
969 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
970 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
971 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
972 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
973 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000974 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000975 RECORD(EXPR_CXX_NOEXCEPT);
976 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000977 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
978 RECORD(EXPR_TYPE_TRAIT);
979 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000980 RECORD(EXPR_PACK_EXPANSION);
981 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000982 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000983 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000984 RECORD(EXPR_FUNCTION_PARM_PACK);
985 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000986 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000987 RECORD(EXPR_CXX_UUIDOF_EXPR);
988 RECORD(EXPR_CXX_UUIDOF_TYPE);
989 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000990#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000991}
Mike Stump11289f42009-09-09 15:08:12 +0000992
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000993void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000994 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000995 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +0000996
Sebastian Redl539c5062010-08-18 23:57:32 +0000997#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
998#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000999
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001000 // Control Block.
1001 BLOCK(CONTROL_BLOCK);
1002 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +00001003 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001004 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001005 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001006 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001007 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001008 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001009 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001010 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001011 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001012
1013 BLOCK(OPTIONS_BLOCK);
1014 RECORD(LANGUAGE_OPTIONS);
1015 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001016 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001017 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001018 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001019 RECORD(PREPROCESSOR_OPTIONS);
1020
Douglas Gregor108cb222012-10-19 00:45:00 +00001021 BLOCK(INPUT_FILES_BLOCK);
1022 RECORD(INPUT_FILE);
1023
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001024 // AST Top-Level Block.
1025 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001026 RECORD(TYPE_OFFSET);
1027 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001028 RECORD(IDENTIFIER_OFFSET);
1029 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001030 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001031 RECORD(SPECIAL_TYPES);
1032 RECORD(STATISTICS);
1033 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001034 RECORD(SELECTOR_OFFSETS);
1035 RECORD(METHOD_POOL);
1036 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001037 RECORD(SOURCE_LOCATION_OFFSETS);
1038 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001039 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001040 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001041 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001042 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001043 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001044 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001045 RECORD(SEMA_DECL_REFS);
1046 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1047 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001048 RECORD(UPDATE_VISIBLE);
1049 RECORD(DECL_UPDATE_OFFSETS);
1050 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001051 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001052 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001053 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001054 RECORD(FP_PRAGMA_OPTIONS);
1055 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001056 RECORD(OPENCL_EXTENSION_TYPES);
1057 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001058 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001059 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001060 RECORD(MODULE_OFFSET_MAP);
1061 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001062 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001063 RECORD(FILE_SORTED_DECLS);
1064 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001065 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001066 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001067 RECORD(INTERESTING_IDENTIFIERS);
1068 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001069 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001070 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001071 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001072 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001073 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001074 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001075 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001076
Chris Lattner28fa4e62009-04-26 22:26:21 +00001077 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001078 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001079 RECORD(SM_SLOC_FILE_ENTRY);
1080 RECORD(SM_SLOC_BUFFER_ENTRY);
1081 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001082 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001083 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001084
Chris Lattner28fa4e62009-04-26 22:26:21 +00001085 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001086 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001087 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001088 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001089 RECORD(PP_MACRO_OBJECT_LIKE);
1090 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001091 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001092
Richard Smithfa715652015-08-31 22:43:10 +00001093 // Submodule Block.
1094 BLOCK(SUBMODULE_BLOCK);
1095 RECORD(SUBMODULE_METADATA);
1096 RECORD(SUBMODULE_DEFINITION);
1097 RECORD(SUBMODULE_UMBRELLA_HEADER);
1098 RECORD(SUBMODULE_HEADER);
1099 RECORD(SUBMODULE_TOPHEADER);
1100 RECORD(SUBMODULE_UMBRELLA_DIR);
1101 RECORD(SUBMODULE_IMPORTS);
1102 RECORD(SUBMODULE_EXPORTS);
1103 RECORD(SUBMODULE_REQUIRES);
1104 RECORD(SUBMODULE_EXCLUDED_HEADER);
1105 RECORD(SUBMODULE_LINK_LIBRARY);
1106 RECORD(SUBMODULE_CONFIG_MACRO);
1107 RECORD(SUBMODULE_CONFLICT);
1108 RECORD(SUBMODULE_PRIVATE_HEADER);
1109 RECORD(SUBMODULE_TEXTUAL_HEADER);
1110 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001111 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001112
1113 // Comments Block.
1114 BLOCK(COMMENTS_BLOCK);
1115 RECORD(COMMENTS_RAW_COMMENT);
1116
Douglas Gregor12bfa382009-10-17 00:13:19 +00001117 // Decls and Types block.
1118 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001119 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001120 RECORD(TYPE_COMPLEX);
1121 RECORD(TYPE_POINTER);
1122 RECORD(TYPE_BLOCK_POINTER);
1123 RECORD(TYPE_LVALUE_REFERENCE);
1124 RECORD(TYPE_RVALUE_REFERENCE);
1125 RECORD(TYPE_MEMBER_POINTER);
1126 RECORD(TYPE_CONSTANT_ARRAY);
1127 RECORD(TYPE_INCOMPLETE_ARRAY);
1128 RECORD(TYPE_VARIABLE_ARRAY);
1129 RECORD(TYPE_VECTOR);
1130 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001131 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001132 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001133 RECORD(TYPE_TYPEDEF);
1134 RECORD(TYPE_TYPEOF_EXPR);
1135 RECORD(TYPE_TYPEOF);
1136 RECORD(TYPE_RECORD);
1137 RECORD(TYPE_ENUM);
1138 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001139 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001140 RECORD(TYPE_DECLTYPE);
1141 RECORD(TYPE_ELABORATED);
1142 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1143 RECORD(TYPE_UNRESOLVED_USING);
1144 RECORD(TYPE_INJECTED_CLASS_NAME);
1145 RECORD(TYPE_OBJC_OBJECT);
1146 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1147 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1148 RECORD(TYPE_DEPENDENT_NAME);
1149 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1150 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1151 RECORD(TYPE_PAREN);
1152 RECORD(TYPE_PACK_EXPANSION);
1153 RECORD(TYPE_ATTRIBUTED);
1154 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001155 RECORD(TYPE_AUTO);
1156 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001157 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001158 RECORD(TYPE_DECAYED);
1159 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001160 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001161 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001162 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001163 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001164 RECORD(DECL_ENUM);
1165 RECORD(DECL_RECORD);
1166 RECORD(DECL_ENUM_CONSTANT);
1167 RECORD(DECL_FUNCTION);
1168 RECORD(DECL_OBJC_METHOD);
1169 RECORD(DECL_OBJC_INTERFACE);
1170 RECORD(DECL_OBJC_PROTOCOL);
1171 RECORD(DECL_OBJC_IVAR);
1172 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001173 RECORD(DECL_OBJC_CATEGORY);
1174 RECORD(DECL_OBJC_CATEGORY_IMPL);
1175 RECORD(DECL_OBJC_IMPLEMENTATION);
1176 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1177 RECORD(DECL_OBJC_PROPERTY);
1178 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001179 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001180 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001181 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001182 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001183 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001184 RECORD(DECL_FILE_SCOPE_ASM);
1185 RECORD(DECL_BLOCK);
1186 RECORD(DECL_CONTEXT_LEXICAL);
1187 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001188 RECORD(DECL_NAMESPACE);
1189 RECORD(DECL_NAMESPACE_ALIAS);
1190 RECORD(DECL_USING);
1191 RECORD(DECL_USING_SHADOW);
1192 RECORD(DECL_USING_DIRECTIVE);
1193 RECORD(DECL_UNRESOLVED_USING_VALUE);
1194 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1195 RECORD(DECL_LINKAGE_SPEC);
1196 RECORD(DECL_CXX_RECORD);
1197 RECORD(DECL_CXX_METHOD);
1198 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001199 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001200 RECORD(DECL_CXX_DESTRUCTOR);
1201 RECORD(DECL_CXX_CONVERSION);
1202 RECORD(DECL_ACCESS_SPEC);
1203 RECORD(DECL_FRIEND);
1204 RECORD(DECL_FRIEND_TEMPLATE);
1205 RECORD(DECL_CLASS_TEMPLATE);
1206 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1207 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001208 RECORD(DECL_VAR_TEMPLATE);
1209 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1210 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001211 RECORD(DECL_FUNCTION_TEMPLATE);
1212 RECORD(DECL_TEMPLATE_TYPE_PARM);
1213 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1214 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001215 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001216 RECORD(DECL_STATIC_ASSERT);
1217 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001218 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001219 RECORD(DECL_INDIRECTFIELD);
1220 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001221 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1222 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1223 RECORD(DECL_IMPORT);
1224 RECORD(DECL_OMP_THREADPRIVATE);
1225 RECORD(DECL_EMPTY);
1226 RECORD(DECL_OBJC_TYPE_PARAM);
1227 RECORD(DECL_OMP_CAPTUREDEXPR);
1228 RECORD(DECL_PRAGMA_COMMENT);
1229 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1230 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001231
Douglas Gregor03412ba2011-06-03 02:27:19 +00001232 // Statements and Exprs can occur in the Decls and Types block.
1233 AddStmtsExprs(Stream, Record);
1234
Douglas Gregor92a96f52011-02-08 21:58:10 +00001235 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001236 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001237 RECORD(PPD_MACRO_DEFINITION);
1238 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001239
1240 // Decls and Types block.
1241 BLOCK(EXTENSION_BLOCK);
1242 RECORD(EXTENSION_METADATA);
1243
Chris Lattner28fa4e62009-04-26 22:26:21 +00001244#undef RECORD
1245#undef BLOCK
1246 Stream.ExitBlock();
1247}
1248
Richard Smith54cc3c22014-12-11 20:50:24 +00001249/// \brief Prepares a path for being written to an AST file by converting it
1250/// to an absolute path and removing nested './'s.
1251///
1252/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001253static bool cleanPathForOutput(FileManager &FileMgr,
1254 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001255 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001256 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001257}
1258
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001259/// \brief Adjusts the given filename to only write out the portion of the
1260/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001261///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001262/// \param Filename the file name to adjust.
1263///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001264/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1265/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001266///
1267/// \returns either the original filename (if it needs no adjustment) or the
1268/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001269static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001270adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001271 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001272
Richard Smith7ed1bc92014-12-05 22:42:13 +00001273 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001274 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001275
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001276 // Verify that the filename and the system root have the same prefix.
1277 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001278 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1279 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001280 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001282 // We hit the end of the filename before we hit the end of the system root.
1283 if (!Filename[Pos])
1284 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001285
Richard Smith7ed1bc92014-12-05 22:42:13 +00001286 // If there's not a path separator at the end of the base directory nor
1287 // immediately after it, then this isn't within the base directory.
1288 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1289 if (!llvm::sys::path::is_separator(BaseDir.back()))
1290 return Filename;
1291 } else {
1292 // If the file name has a '/' at the current position, skip over the '/'.
1293 // We distinguish relative paths from absolute paths by the
1294 // absence of '/' at the beginning of relative paths.
1295 //
1296 // FIXME: This is wrong. We distinguish them by asking if the path is
1297 // absolute, which isn't the same thing. And there might be multiple '/'s
1298 // in a row. Use a better mechanism to indicate whether we have emitted an
1299 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001300 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001301 }
Mike Stump11289f42009-09-09 15:08:12 +00001302
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001303 return Filename + Pos;
1304}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001305
Ben Langmuir487ea142014-10-23 18:05:36 +00001306static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001307 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001308 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1309 return S;
1310 // Rely on GetRandomNumber to eventually return non-zero...
1311 }
1312}
1313
Douglas Gregor112b9072012-10-18 05:31:06 +00001314/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001315uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1316 ASTContext &Context,
1317 StringRef isysroot,
1318 const std::string &OutputFile) {
1319 ASTFileSignature Signature = 0;
1320
Douglas Gregorbfbde532009-04-10 21:16:55 +00001321 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001322 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001323 RecordData Record;
1324
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001325 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001326 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001327 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1328 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1329 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1330 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1331 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1332 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001333 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001334 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1335 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001336 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001337 assert((!WritingModule || isysroot.empty()) &&
1338 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001339 {
1340 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1341 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1342 !isysroot.empty(), IncludeTimestamps,
1343 ASTHasCompilerErrors};
1344 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1345 getClangFullRepositoryVersion());
1346 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001347 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001348 // For implicit modules we output a signature that we can use to ensure
1349 // duplicate module builds don't collide in the cache as their output order
1350 // is non-deterministic.
1351 // FIXME: Remove this when output is deterministic.
1352 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001353 Signature = getSignature();
1354 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001355 Stream.EmitRecord(SIGNATURE, Record);
1356 }
1357
Richard Smith7ed1bc92014-12-05 22:42:13 +00001358 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001359 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001360 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001362 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001363 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001364 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1365 }
1366
Richard Smith7ed1bc92014-12-05 22:42:13 +00001367 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001368 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001369 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001370
1371 // If the home of the module is the current working directory, then we
1372 // want to pick up the cwd of the build process loading the module, not
1373 // our cwd, when we load this module.
1374 if (!PP.getHeaderSearchInfo()
1375 .getHeaderSearchOpts()
1376 .ModuleMapFileHomeIsCwd ||
1377 WritingModule->Directory->getName() != StringRef(".")) {
1378 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001379 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001380 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001382 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001383
Mehdi Amini57a41912015-09-10 01:46:39 +00001384 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001385 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1386 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001387
1388 // Write out all other paths relative to the base directory if possible.
1389 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1390 } else if (!isysroot.empty()) {
1391 // Write out paths relative to the sysroot if possible.
1392 BaseDirectory = isysroot;
1393 }
1394
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001395 // Module map file
1396 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001397 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001398
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001399 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1400
1401 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001402 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001403
1404 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001405 if (auto *AdditionalModMaps =
1406 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001407 Record.push_back(AdditionalModMaps->size());
1408 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001409 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001410 } else {
1411 Record.push_back(0);
1412 }
1413
1414 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001415 }
1416
Douglas Gregor112b9072012-10-18 05:31:06 +00001417 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001418 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001419 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001420 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001421
Richard Smith7f330cd2015-03-18 01:42:29 +00001422 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001423 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001424 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001425 continue;
1426
Richard Smith7f330cd2015-03-18 01:42:29 +00001427 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1428 AddSourceLocation(M->ImportLoc, Record);
1429 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001430 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001431 Record.push_back(M->Signature);
1432 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001433 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001434 Stream.EmitRecord(IMPORTS, Record);
1435 }
Mike Stump11289f42009-09-09 15:08:12 +00001436
Richard Smith0516b182015-09-08 19:40:14 +00001437 // Write the options block.
1438 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1439
Douglas Gregor112b9072012-10-18 05:31:06 +00001440 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001441 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001442 const LangOptions &LangOpts = Context.getLangOpts();
1443#define LANGOPT(Name, Bits, Default, Description) \
1444 Record.push_back(LangOpts.Name);
1445#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1446 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001447#include "clang/Basic/LangOptions.def"
1448#define SANITIZER(NAME, ID) \
1449 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001450#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001451
Ben Langmuircd98cb72015-06-23 18:20:18 +00001452 Record.push_back(LangOpts.ModuleFeatures.size());
1453 for (StringRef Feature : LangOpts.ModuleFeatures)
1454 AddString(Feature, Record);
1455
Douglas Gregor112b9072012-10-18 05:31:06 +00001456 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1457 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001458
1459 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001460
1461 // Comment options.
1462 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001463 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1464 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001465 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001466 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001467
Samuel Antaoee8fb302016-01-06 13:42:12 +00001468 // OpenMP offloading options.
1469 Record.push_back(LangOpts.OMPTargetTriples.size());
1470 for (auto &T : LangOpts.OMPTargetTriples)
1471 AddString(T.getTriple(), Record);
1472
1473 AddString(LangOpts.OMPHostIRFile, Record);
1474
Douglas Gregor112b9072012-10-18 05:31:06 +00001475 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1476
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001477 // Target options.
1478 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001479 const TargetInfo &Target = Context.getTargetInfo();
1480 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001481 AddString(TargetOpts.Triple, Record);
1482 AddString(TargetOpts.CPU, Record);
1483 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001484 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1485 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1486 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1487 }
1488 Record.push_back(TargetOpts.Features.size());
1489 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1490 AddString(TargetOpts.Features[I], Record);
1491 }
1492 Stream.EmitRecord(TARGET_OPTIONS, Record);
1493
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001494 // Diagnostic options.
1495 Record.clear();
1496 const DiagnosticOptions &DiagOpts
1497 = Context.getDiagnostics().getDiagnosticOptions();
1498#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1499#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1500 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1501#include "clang/Basic/DiagnosticOptions.def"
1502 Record.push_back(DiagOpts.Warnings.size());
1503 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1504 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001505 Record.push_back(DiagOpts.Remarks.size());
1506 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1507 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001508 // Note: we don't serialize the log or serialization file names, because they
1509 // are generally transient files and will almost always be overridden.
1510 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1511
Douglas Gregorc6317db2012-10-24 15:49:58 +00001512 // File system options.
1513 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001514 const FileSystemOptions &FSOpts =
1515 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001516 AddString(FSOpts.WorkingDir, Record);
1517 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1518
Douglas Gregor2d302362012-10-24 16:50:34 +00001519 // Header search options.
1520 Record.clear();
1521 const HeaderSearchOptions &HSOpts
1522 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1523 AddString(HSOpts.Sysroot, Record);
1524
1525 // Include entries.
1526 Record.push_back(HSOpts.UserEntries.size());
1527 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1528 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1529 AddString(Entry.Path, Record);
1530 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001531 Record.push_back(Entry.IsFramework);
1532 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001533 }
1534
1535 // System header prefixes.
1536 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1537 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1538 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1539 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1540 }
1541
1542 AddString(HSOpts.ResourceDir, Record);
1543 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001544 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001545 Record.push_back(HSOpts.DisableModuleHash);
1546 Record.push_back(HSOpts.UseBuiltinIncludes);
1547 Record.push_back(HSOpts.UseStandardSystemIncludes);
1548 Record.push_back(HSOpts.UseStandardCXXIncludes);
1549 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001550 // Write out the specific module cache path that contains the module files.
1551 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001552 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1553
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001554 // Preprocessor options.
1555 Record.clear();
1556 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1557
1558 // Macro definitions.
1559 Record.push_back(PPOpts.Macros.size());
1560 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1561 AddString(PPOpts.Macros[I].first, Record);
1562 Record.push_back(PPOpts.Macros[I].second);
1563 }
1564
1565 // Includes
1566 Record.push_back(PPOpts.Includes.size());
1567 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1568 AddString(PPOpts.Includes[I], Record);
1569
1570 // Macro includes
1571 Record.push_back(PPOpts.MacroIncludes.size());
1572 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1573 AddString(PPOpts.MacroIncludes[I], Record);
1574
Douglas Gregorb6368752012-10-24 23:41:50 +00001575 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001576 // Detailed record is important since it is used for the module cache hash.
1577 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001578 AddString(PPOpts.ImplicitPCHInclude, Record);
1579 AddString(PPOpts.ImplicitPTHInclude, Record);
1580 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1581 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1582
Richard Smith0516b182015-09-08 19:40:14 +00001583 // Leave the options block.
1584 Stream.ExitBlock();
1585
Douglas Gregora3b20262011-05-06 21:43:30 +00001586 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001587 SourceManager &SM = Context.getSourceManager();
1588 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001589 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001590 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1591 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001592 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001593 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001594
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001595 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001596 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001597 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001598 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001599 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001600
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001601 Record.clear();
1602 Record.push_back(SM.getMainFileID().getOpaqueValue());
1603 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1604
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001605 // Original PCH directory
1606 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001607 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001608 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001610 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001611
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001612 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001613
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001614 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001615 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1616
Mehdi Amini57a41912015-09-10 01:46:39 +00001617 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001618 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1619 }
1620
Douglas Gregor49491f72013-03-15 22:15:07 +00001621 WriteInputFiles(Context.SourceMgr,
1622 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001623 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001624 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001625 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001626}
1627
Douglas Gregor49491f72013-03-15 22:15:07 +00001628namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001629
Douglas Gregor49491f72013-03-15 22:15:07 +00001630 /// \brief An input file.
1631 struct InputFileEntry {
1632 const FileEntry *File;
1633 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001634 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001635 bool BufferOverridden;
1636 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001637
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001638} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001639
1640void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1641 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001642 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001643 using namespace llvm;
1644 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001645
Douglas Gregor72be3902012-10-19 00:38:02 +00001646 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001647 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001648 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001649 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001650 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1651 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001652 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001653 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001654 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001655 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001656
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001657 // Get all ContentCache objects for files, sorted by whether the file is a
1658 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001659 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001660 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1661 // Get this source location entry.
1662 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001663 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001664
1665 // We only care about file entries that were not overridden.
1666 if (!SLoc->isFile())
1667 continue;
1668 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001669 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001670 continue;
1671
Douglas Gregor49491f72013-03-15 22:15:07 +00001672 InputFileEntry Entry;
1673 Entry.File = Cache->OrigEntry;
1674 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001675 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001676 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001677 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001678 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001679 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001680 SortedFiles.push_front(Entry);
1681 }
1682
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001683 unsigned UserFilesNum = 0;
1684 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001685 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001686 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001687 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001688 if (InputFileID != 0)
1689 continue; // already recorded this file.
1690
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001691 // Record this entry's offset.
1692 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001693
1694 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001695
Douglas Gregor49491f72013-03-15 22:15:07 +00001696 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001697 ++UserFilesNum;
1698
Douglas Gregor72be3902012-10-19 00:38:02 +00001699 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001700 // And whether this file was overridden.
1701 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001702 INPUT_FILE,
1703 InputFileOffsets.size(),
1704 (uint64_t)Entry.File->getSize(),
1705 (uint64_t)getTimestampForOutput(Entry.File),
1706 Entry.BufferOverridden,
1707 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001708
Richard Smith7ed1bc92014-12-05 22:42:13 +00001709 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1710 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001711
Douglas Gregor112b9072012-10-18 05:31:06 +00001712 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001713
1714 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001715 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001716 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1717 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001718 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1719 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001720 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001721 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001722
1723 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001724 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1725 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001726 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001727}
1728
Douglas Gregora7f71a92009-04-10 03:52:48 +00001729//===----------------------------------------------------------------------===//
1730// Source Manager Serialization
1731//===----------------------------------------------------------------------===//
1732
1733/// \brief Create an abbreviation for the SLocEntry that refers to a
1734/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001735static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001736 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001737
David Blaikieb44f0bf2017-01-04 22:36:43 +00001738 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001739 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001744 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001749 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001750}
1751
1752/// \brief Create an abbreviation for the SLocEntry that refers to a
1753/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001754static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001755 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001756
David Blaikieb44f0bf2017-01-04 22:36:43 +00001757 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001758 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001764 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001765}
1766
1767/// \brief Create an abbreviation for the SLocEntry that refers to a
1768/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001769static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1770 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001771 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001772
David Blaikieb44f0bf2017-01-04 22:36:43 +00001773 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001774 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1775 : SM_SLOC_BUFFER_BLOB));
1776 if (Compressed)
1777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001779 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001780}
1781
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001782/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1783/// expansion.
1784static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001785 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001786
David Blaikieb44f0bf2017-01-04 22:36:43 +00001787 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001788 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001794 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001795}
1796
Douglas Gregor09b69892011-02-10 17:09:37 +00001797namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001798
Douglas Gregor09b69892011-02-10 17:09:37 +00001799 // Trait used for the on-disk hash table of header search information.
1800 class HeaderFileInfoTrait {
1801 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001802 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001803
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001804 // Keep track of the framework names we've used during serialization.
1805 SmallVector<char, 128> FrameworkStringData;
1806 llvm::StringMap<unsigned> FrameworkNameOffset;
1807
Douglas Gregor09b69892011-02-10 17:09:37 +00001808 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001809 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1810 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001811
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001812 struct key_type {
1813 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001814 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001815 };
1816 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001817
1818 typedef HeaderFileInfo data_type;
1819 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001820 typedef unsigned hash_value_type;
1821 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001822
Richard Smithe75ee0f2015-08-17 07:13:32 +00001823 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001824 // The hash is based only on size/time of the file, so that the reader can
1825 // match even when symlinking or excess path elements ("foo/../", "../")
1826 // change the form of the name. However, complete path is still the key.
1827 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001828 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001829 }
1830
1831 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001832 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001833 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001834 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001835 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001836 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001837 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001838 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1839 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1840 DataLen += 4;
1841 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001842 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001843 }
1844
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001845 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001846 using namespace llvm::support;
1847 endian::Writer<little> LE(Out);
1848 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001849 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001850 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001851 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001852 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001853 }
1854
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001855 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001856 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001857 using namespace llvm::support;
1858 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001859 uint64_t Start = Out.tell(); (void)Start;
1860
Richard Smith386bb072015-08-18 23:42:23 +00001861 unsigned char Flags = (Data.isImport << 4)
1862 | (Data.isPragmaOnce << 3)
1863 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001864 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001865 LE.write<uint8_t>(Flags);
1866 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001867
1868 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001869 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001870 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001871 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001872
1873 unsigned Offset = 0;
1874 if (!Data.Framework.empty()) {
1875 // If this header refers into a framework, save the framework name.
1876 llvm::StringMap<unsigned>::iterator Pos
1877 = FrameworkNameOffset.find(Data.Framework);
1878 if (Pos == FrameworkNameOffset.end()) {
1879 Offset = FrameworkStringData.size() + 1;
1880 FrameworkStringData.append(Data.Framework.begin(),
1881 Data.Framework.end());
1882 FrameworkStringData.push_back(0);
1883
1884 FrameworkNameOffset[Data.Framework] = Offset;
1885 } else
1886 Offset = Pos->second;
1887 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001888 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001889
Richard Smith386bb072015-08-18 23:42:23 +00001890 // FIXME: If the header is excluded, we should write out some
1891 // record of that fact.
1892 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1893 if (uint32_t ModID =
1894 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1895 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1896 assert((Value >> 2) == ModID && "overflow in header module info");
1897 LE.write<uint32_t>(Value);
1898 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001899 }
1900
Douglas Gregor09b69892011-02-10 17:09:37 +00001901 assert(Out.tell() - Start == DataLen && "Wrong data length");
1902 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001903
1904 const char *strings_begin() const { return FrameworkStringData.begin(); }
1905 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001906 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001907
Douglas Gregor09b69892011-02-10 17:09:37 +00001908} // end anonymous namespace
1909
1910/// \brief Write the header search block for the list of files that
1911///
1912/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001913void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001914 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001915 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1916
1917 if (FilesByUID.size() > HS.header_file_size())
1918 FilesByUID.resize(HS.header_file_size());
1919
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001920 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001921 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001922 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001923 unsigned NumHeaderSearchEntries = 0;
1924 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1925 const FileEntry *File = FilesByUID[UID];
1926 if (!File)
1927 continue;
1928
Richard Smith386bb072015-08-18 23:42:23 +00001929 // Get the file info. This will load info from the external source if
1930 // necessary. Skip emitting this file if we have no information on it
1931 // as a header file (in which case HFI will be null) or if it hasn't
1932 // changed since it was loaded. Also skip it if it's for a modular header
1933 // from a different module; in that case, we rely on the module(s)
1934 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001935 const HeaderFileInfo *HFI =
1936 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1937 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001938 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001939
Richard Smith7ed1bc92014-12-05 22:42:13 +00001940 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001941 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001942 SmallString<128> FilenameTmp(Filename);
1943 if (PreparePathForOutput(FilenameTmp)) {
1944 // If we performed any translation on the file name at all, we need to
1945 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001946 Filename = StringRef(strdup(FilenameTmp.c_str()));
1947 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001948 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001949
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001950 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001951 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001952 ++NumHeaderSearchEntries;
1953 }
1954
1955 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001956 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001957 uint32_t BucketOffset;
1958 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001959 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001960 llvm::raw_svector_ostream Out(TableData);
1961 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001962 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001963 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1964 }
1965
1966 // Create a blob abbreviation
1967 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001968
David Blaikieb44f0bf2017-01-04 22:36:43 +00001969 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00001970 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00001975 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00001976
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001977 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001978 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1979 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001980 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001981 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001982
1983 // Free all of the strings we had to duplicate.
1984 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001985 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001986}
1987
Douglas Gregora7f71a92009-04-10 03:52:48 +00001988/// \brief Writes the block containing the serialized form of the
1989/// source manager.
1990///
1991/// TODO: We should probably use an on-disk hash table (stored in a
1992/// blob), indexed based on the file name, so that we only create
1993/// entries for files that we actually need. In the common case (no
1994/// errors), we probably won't have to create file entries for any of
1995/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001996void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001997 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001998 RecordData Record;
1999
Chris Lattner0910e3b2009-04-10 17:16:57 +00002000 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002001 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002002
2003 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002004 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2005 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002006 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2007 unsigned SLocBufferBlobCompressedAbbrv =
2008 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002009 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002010
Douglas Gregor258ae542009-04-27 06:38:32 +00002011 // Write out the source location entry table. We skip the first
2012 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002013 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002014 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002015 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2016 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002017 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002018 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002019 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002020 FileID FID = FileID::get(I);
2021 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002022
Douglas Gregor258ae542009-04-27 06:38:32 +00002023 // Record the offset of this source-location entry.
2024 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2025
2026 // Figure out which record code to use.
2027 unsigned Code;
2028 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002029 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2030 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002031 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002032 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002033 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002034 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002035 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002036 Record.clear();
2037 Record.push_back(Code);
2038
Douglas Gregor925296b2011-07-19 16:10:42 +00002039 // Starting offset of this entry within this module, so skip the dummy.
2040 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002041 if (SLoc->isFile()) {
2042 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002043 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002044 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2045 Record.push_back(File.hasLineDirectives());
2046
2047 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002048 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002049 if (Content->OrigEntry) {
2050 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002051 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002052
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002053 // The source location entry is a file. Emit input file ID.
2054 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2055 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002056
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002057 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002058
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002059 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002060 if (FDI != FileDeclIDs.end()) {
2061 Record.push_back(FDI->second->FirstDeclIndex);
2062 Record.push_back(FDI->second->DeclIDs.size());
2063 } else {
2064 Record.push_back(0);
2065 Record.push_back(0);
2066 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002067
2068 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2069
Richard Smithaada85c2016-02-06 02:06:43 +00002070 if (Content->BufferOverridden || Content->IsTransient)
2071 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002072 } else {
2073 // The source location entry is a buffer. The blob associated
2074 // with this entry contains the contents of the buffer.
2075
2076 // We add one to the size so that we capture the trailing NULL
2077 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2078 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002079 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002080 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002081 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002082 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002083 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002084 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002085
Mehdi Amini99d1b292016-10-01 16:38:28 +00002086 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002087 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002088 }
Richard Smithaada85c2016-02-06 02:06:43 +00002089
2090 if (EmitBlob) {
2091 // Include the implicit terminating null character in the on-disk buffer
2092 // if we're writing it uncompressed.
2093 const llvm::MemoryBuffer *Buffer =
2094 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2095 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2096
2097 // Compress the buffer if possible. We expect that almost all PCM
2098 // consumers will not want its contents.
2099 SmallString<0> CompressedBuffer;
2100 if (llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer) ==
2101 llvm::zlib::StatusOK) {
2102 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2103 Blob.size() - 1};
2104 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2105 CompressedBuffer);
2106 } else {
2107 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
2108 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2109 }
2110 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002111 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002112 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002113 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002114 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2115 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2116 AddSourceLocation(Expansion.isMacroArgExpansion()
2117 ? SourceLocation()
2118 : Expansion.getExpansionLocEnd(),
2119 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002120
2121 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002122 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002123 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002124 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002125 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002126 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002127 }
2128 }
2129
Douglas Gregor8f45df52009-04-16 22:23:12 +00002130 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002131
2132 if (SLocEntryOffsets.empty())
2133 return;
2134
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002135 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002136 // table is used for lazily loading source-location information.
2137 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002138
David Blaikieb44f0bf2017-01-04 22:36:43 +00002139 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002140 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002144 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002145 {
2146 RecordData::value_type Record[] = {
2147 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2148 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2149 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2150 bytes(SLocEntryOffsets));
2151 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002152 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002153 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002154 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002155
2156 // Write the line table. It depends on remapping working, so it must come
2157 // after the source location offsets.
2158 if (SourceMgr.hasLineTable()) {
2159 LineTableInfo &LineTable = SourceMgr.getLineTable();
2160
2161 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002162
2163 // Emit the needed file names.
2164 llvm::DenseMap<int, int> FilenameMap;
2165 for (const auto &L : LineTable) {
2166 if (L.first.ID < 0)
2167 continue;
2168 for (auto &LE : L.second) {
2169 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2170 FilenameMap.size())).second)
2171 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2172 }
2173 }
2174 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002175
2176 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002177 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002178 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002179 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002180 continue;
2181
2182 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002183 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002184
2185 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002186 Record.push_back(L.second.size());
2187 for (const auto &LE : L.second) {
2188 Record.push_back(LE.FileOffset);
2189 Record.push_back(LE.LineNo);
2190 Record.push_back(FilenameMap[LE.FilenameID]);
2191 Record.push_back((unsigned)LE.FileKind);
2192 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002193 }
2194 }
Richard Smith63078492015-09-01 07:41:55 +00002195
Douglas Gregor925296b2011-07-19 16:10:42 +00002196 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2197 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002198}
2199
Douglas Gregorc5046832009-04-27 18:38:38 +00002200//===----------------------------------------------------------------------===//
2201// Preprocessor Serialization
2202//===----------------------------------------------------------------------===//
2203
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002204static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2205 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002206 if (MacroInfo *MI = MD->getMacroInfo())
2207 if (MI->isBuiltinMacro())
2208 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002209
2210 if (IsModule) {
2211 SourceLocation Loc = MD->getLocation();
2212 if (Loc.isInvalid())
2213 return true;
2214 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2215 return true;
2216 }
2217
2218 return false;
2219}
2220
Chris Lattnereeffaef2009-04-10 17:15:23 +00002221/// \brief Writes the block containing the serialized form of the
2222/// preprocessor.
2223///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002224void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002225 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2226 if (PPRec)
2227 WritePreprocessorDetail(*PPRec);
2228
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002229 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002230 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002231
Chris Lattner0af3ba12009-04-13 01:29:17 +00002232 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2233 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002234 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002235 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002236 }
2237
2238 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002239 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002240
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002241 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002242 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002243 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002244 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002245
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002246 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002247 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002248
Richard Smithee977932015-05-01 21:22:17 +00002249 // Construct the list of identifiers with macro directives that need to be
2250 // serialized.
2251 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2252 for (auto &Id : PP.getIdentifierTable())
2253 if (Id.second->hadMacroDefinition() &&
2254 (!Id.second->isFromAST() ||
2255 Id.second->hasChangedSinceDeserialization()))
2256 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002257 // Sort the set of macro definitions that need to be serialized by the
2258 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002259 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2260 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002261
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002262 // Emit the macro directives as a list and associate the offset with the
2263 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002264 for (const IdentifierInfo *Name : MacroIdentifiers) {
2265 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002266 auto StartOffset = Stream.GetCurrentBitNo();
2267
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002268 // Emit the macro directives in reverse source order.
2269 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002270 // Once we hit an ignored macro, we're done: the rest of the chain
2271 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002272 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002273 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002274
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002275 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002276 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002277 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002278 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002279 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002280 Record.push_back(VisMD->isPublic());
2281 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002282 }
Richard Smithd7329392015-04-21 21:46:32 +00002283
Richard Smithb8b2ed62015-04-23 18:18:26 +00002284 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002285 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002286 // We write out exported module macros for PCH as well.
2287 auto Leafs = PP.getLeafModuleMacros(Name);
2288 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2289 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2290 while (!Worklist.empty()) {
2291 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002292
Manman Ren33d80292016-05-31 18:19:32 +00002293 // Emit a record indicating this submodule exports this macro.
2294 ModuleMacroRecord.push_back(
2295 getSubmoduleID(Macro->getOwningModule()));
2296 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2297 for (auto *M : Macro->overrides())
2298 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002299
Manman Ren33d80292016-05-31 18:19:32 +00002300 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2301 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002302
Manman Ren33d80292016-05-31 18:19:32 +00002303 // Enqueue overridden macros once we've visited all their ancestors.
2304 for (auto *M : Macro->overrides())
2305 if (++Visits[M] == M->getNumOverridingMacros())
2306 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002307
Manman Ren33d80292016-05-31 18:19:32 +00002308 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002309 }
Richard Smithd7329392015-04-21 21:46:32 +00002310
Richard Smithee977932015-05-01 21:22:17 +00002311 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002312 continue;
2313
Richard Smithd7329392015-04-21 21:46:32 +00002314 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002315 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2316 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002317 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002318
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002319 /// \brief Offsets of each of the macros into the bitstream, indexed by
2320 /// the local macro ID
2321 ///
2322 /// For each identifier that is associated with a macro, this map
2323 /// provides the offset into the bitstream where that macro is
2324 /// defined.
2325 std::vector<uint32_t> MacroOffsets;
2326
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002327 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2328 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2329 MacroInfo *MI = MacroInfosToEmit[I].MI;
2330 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002331
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002332 if (ID < FirstMacroID) {
2333 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2334 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002335 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002336
2337 // Record the local offset of this macro.
2338 unsigned Index = ID - FirstMacroID;
2339 if (Index == MacroOffsets.size())
2340 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2341 else {
2342 if (Index > MacroOffsets.size())
2343 MacroOffsets.resize(Index + 1);
2344
2345 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2346 }
2347
2348 AddIdentifierRef(Name, Record);
2349 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2350 AddSourceLocation(MI->getDefinitionLoc(), Record);
2351 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2352 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002353 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002354 unsigned Code;
2355 if (MI->isObjectLike()) {
2356 Code = PP_MACRO_OBJECT_LIKE;
2357 } else {
2358 Code = PP_MACRO_FUNCTION_LIKE;
2359
2360 Record.push_back(MI->isC99Varargs());
2361 Record.push_back(MI->isGNUVarargs());
2362 Record.push_back(MI->hasCommaPasting());
2363 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002364 for (const IdentifierInfo *Arg : MI->args())
2365 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002366 }
2367
2368 // If we have a detailed preprocessing record, record the macro definition
2369 // ID that corresponds to this macro.
2370 if (PPRec)
2371 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2372
2373 Stream.EmitRecord(Code, Record);
2374 Record.clear();
2375
2376 // Emit the tokens array.
2377 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2378 // Note that we know that the preprocessor does not have any annotation
2379 // tokens in it because they are created by the parser, and thus can't
2380 // be in a macro definition.
2381 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002382 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002383 Stream.EmitRecord(PP_TOKEN, Record);
2384 Record.clear();
2385 }
2386 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002387 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002388
Douglas Gregor92a96f52011-02-08 21:58:10 +00002389 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002390
2391 // Write the offsets table for macro IDs.
2392 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002393
David Blaikieb44f0bf2017-01-04 22:36:43 +00002394 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002395 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2399
David Blaikieb44f0bf2017-01-04 22:36:43 +00002400 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002401 {
2402 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2403 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2404 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2405 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002406}
2407
2408void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002409 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002410 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002411
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002412 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002413
Douglas Gregor92a96f52011-02-08 21:58:10 +00002414 // Enter the preprocessor block.
2415 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002416
Douglas Gregoraae92242010-03-19 21:51:54 +00002417 // If the preprocessor has a preprocessing record, emit it.
2418 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002419 using namespace llvm;
2420
2421 // Set up the abbreviation for
2422 unsigned InclusionAbbrev = 0;
2423 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002424 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002425 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002431 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002432 }
2433
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002434 unsigned FirstPreprocessorEntityID
2435 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2436 + NUM_PREDEF_PP_ENTITY_IDS;
2437 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002438 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002439 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2440 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002441 E != EEnd;
2442 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002443 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002444
Richard Smith66a81862015-05-04 02:25:31 +00002445 PreprocessedEntityOffsets.push_back(
2446 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002447
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002448 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002449 // Record this macro definition's ID.
2450 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002451
Douglas Gregor92a96f52011-02-08 21:58:10 +00002452 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002453 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2454 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002455 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002456
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002457 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002458 Record.push_back(ME->isBuiltinMacro());
2459 if (ME->isBuiltinMacro())
2460 AddIdentifierRef(ME->getName(), Record);
2461 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002462 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002463 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002464 continue;
2465 }
2466
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002467 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002468 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002469 Record.push_back(ID->getFileName().size());
2470 Record.push_back(ID->wasInQuotes());
2471 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002472 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002473 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002474 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002475 // Check that the FileEntry is not null because it was not resolved and
2476 // we create a PCH even with compiler errors.
2477 if (ID->getFile())
2478 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002479 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002480 continue;
2481 }
2482
2483 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2484 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002485 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002486
Douglas Gregoraae92242010-03-19 21:51:54 +00002487 // Write the offsets table for the preprocessing record.
2488 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002489 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2490
Douglas Gregoraae92242010-03-19 21:51:54 +00002491 // Write the offsets table for identifier IDs.
2492 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002493
David Blaikieb44f0bf2017-01-04 22:36:43 +00002494 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002495 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002498 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002499
Mehdi Amini57a41912015-09-10 01:46:39 +00002500 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2501 FirstPreprocessorEntityID -
2502 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002503 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002504 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002505 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002506}
2507
Richard Smith386bb072015-08-18 23:42:23 +00002508unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002509 if (!Mod)
2510 return 0;
2511
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002512 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2513 if (Known != SubmoduleIDs.end())
2514 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002515
Richard Smithdc1f0422016-07-20 19:10:16 +00002516 auto *Top = Mod->getTopLevelModule();
2517 if (Top != WritingModule &&
2518 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule)))
Richard Smith386bb072015-08-18 23:42:23 +00002519 return 0;
2520
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002521 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2522}
2523
Richard Smith386bb072015-08-18 23:42:23 +00002524unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2525 // FIXME: This can easily happen, if we have a reference to a submodule that
2526 // did not result in us loading a module file for that submodule. For
2527 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2528 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2529 assert((ID || !Mod) &&
2530 "asked for module ID for non-local, non-imported module");
2531 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002532}
2533
Douglas Gregor253eefe2011-12-01 00:59:36 +00002534/// \brief Compute the number of modules within the given tree (including the
2535/// given module).
2536static unsigned getNumberOfModules(Module *Mod) {
2537 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002538 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002539 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002540 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002541
2542 return ChildModules + 1;
2543}
2544
Douglas Gregorde3ef502011-11-30 23:21:26 +00002545void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002546 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002547 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002548
2549 // Write the abbreviations needed for the submodules block.
2550 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002551
David Blaikieb44f0bf2017-01-04 22:36:43 +00002552 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002553 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002565 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002566
David Blaikieb44f0bf2017-01-04 22:36:43 +00002567 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002568 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002570 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002571
David Blaikieb44f0bf2017-01-04 22:36:43 +00002572 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002573 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002575 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002576
David Blaikieb44f0bf2017-01-04 22:36:43 +00002577 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002578 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002580 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002581
David Blaikieb44f0bf2017-01-04 22:36:43 +00002582 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002583 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002585 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002586
David Blaikieb44f0bf2017-01-04 22:36:43 +00002587 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002588 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002591 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002592
David Blaikieb44f0bf2017-01-04 22:36:43 +00002593 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002594 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2595 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002596 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002597
David Blaikieb44f0bf2017-01-04 22:36:43 +00002598 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002599 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2600 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002601 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002602
David Blaikieb44f0bf2017-01-04 22:36:43 +00002603 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002604 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002606 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002607
David Blaikieb44f0bf2017-01-04 22:36:43 +00002608 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002609 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002611 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002612
David Blaikieb44f0bf2017-01-04 22:36:43 +00002613 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002614 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002617 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002618
David Blaikieb44f0bf2017-01-04 22:36:43 +00002619 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002620 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002622 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002623
David Blaikieb44f0bf2017-01-04 22:36:43 +00002624 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002625 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002628 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002629
Douglas Gregor253eefe2011-12-01 00:59:36 +00002630 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002631 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2632 FirstSubmoduleID -
2633 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002634 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2635
Douglas Gregor69021972011-11-30 17:33:56 +00002636 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002637 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002638 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002639 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002640 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002641 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002642 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002643
2644 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002645 if (Mod->Parent) {
2646 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002647 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002648 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002649
2650 // Emit the definition of the block.
2651 {
2652 RecordData::value_type Record[] = {
2653 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2654 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2655 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2656 Mod->ConfigMacrosExhaustive};
2657 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2658 }
2659
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002660 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002661 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002662 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002663 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002664 }
2665
Douglas Gregor69021972011-11-30 17:33:56 +00002666 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002667 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002668 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002669 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2670 UmbrellaHeader.NameAsWritten);
2671 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002672 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2673 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002674 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002675 }
Richard Smith306d8922014-10-22 23:50:56 +00002676
Douglas Gregor69021972011-11-30 17:33:56 +00002677 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002678 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002679 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002680 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002681 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002682 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002683 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2684 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2685 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002686 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002687 Module::HK_PrivateTextual},
2688 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002689 };
2690 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002691 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002692 for (auto &H : Mod->Headers[HL.HeaderKind])
2693 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2694 }
2695
2696 // Emit the top headers.
2697 {
2698 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002699 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002700 for (auto *H : TopHeaders)
2701 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002702 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002703
2704 // Emit the imports.
2705 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002706 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002707 for (auto *I : Mod->Imports)
2708 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002709 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2710 }
2711
Douglas Gregor24bb9232011-12-02 18:58:38 +00002712 // Emit the exports.
2713 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002714 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002715 for (const auto &E : Mod->Exports) {
2716 // FIXME: This may fail; we don't require that all exported modules
2717 // are local or imported.
2718 Record.push_back(getSubmoduleID(E.getPointer()));
2719 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002720 }
2721 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2722 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002723
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002724 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2725 // Might be unnecessary as use declarations are only used to build the
2726 // module itself.
2727
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002728 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002729 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002730 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2731 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002732 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002733 }
2734
Douglas Gregorfb912652013-03-20 21:10:35 +00002735 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002736 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002737 // FIXME: This may fail; we don't require that all conflicting modules
2738 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002739 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2740 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002741 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002742 }
2743
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002744 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002745 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002746 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002747 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002748 }
2749
Richard Smithdc1f0422016-07-20 19:10:16 +00002750 // Emit the initializers, if any.
2751 RecordData Inits;
2752 for (Decl *D : Context->getModuleInitializers(Mod))
2753 Inits.push_back(GetDeclRef(D));
2754 if (!Inits.empty())
2755 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2756
Douglas Gregor69021972011-11-30 17:33:56 +00002757 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002758 for (auto *M : Mod->submodules())
2759 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002760 }
2761
2762 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002763
Richard Smith23d8d032015-05-14 00:45:20 +00002764 assert((NextSubmoduleID - FirstSubmoduleID ==
2765 getNumberOfModules(WritingModule)) &&
2766 "Wrong # of submodules; found a reference to a non-local, "
2767 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002768}
2769
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002770serialization::SubmoduleID
2771ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002772 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002773 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002774
2775 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002776 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002777 Module *OwningMod
2778 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002779 if (!OwningMod)
2780 return 0;
2781
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002782 // Check whether this submodule is part of our own module.
2783 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002784 return 0;
2785
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002786 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002787}
2788
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002789void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2790 bool isModule) {
2791 // Make sure set diagnostic pragmas don't affect the translation unit that
2792 // imports the module.
2793 // FIXME: Make diagnostic pragma sections work properly with modules.
2794 if (isModule)
2795 return;
2796
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002797 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2798 DiagStateIDMap;
2799 unsigned CurrID = 0;
2800 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002801 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002802 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002803 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2804 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002805 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002806 if (point.Loc.isInvalid())
2807 continue;
2808
Richard Smithb22a1d12016-03-27 20:13:24 +00002809 AddSourceLocation(point.Loc, Record);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002810 unsigned &DiagStateID = DiagStateIDMap[point.State];
2811 Record.push_back(DiagStateID);
2812
2813 if (DiagStateID == 0) {
2814 DiagStateID = ++CurrID;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002815 for (const auto &I : *(point.State)) {
2816 if (I.second.isPragma()) {
2817 Record.push_back(I.first);
2818 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002819 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002820 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002821 Record.push_back(-1); // mark the end of the diag/map pairs for this
2822 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002823 }
2824 }
2825
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002826 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002827 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002828}
2829
Douglas Gregorc5046832009-04-27 18:38:38 +00002830//===----------------------------------------------------------------------===//
2831// Type Serialization
2832//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002833
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002834/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002835void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002836 TypeIdx &IdxRef = TypeIdxs[T];
2837 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2838 IdxRef = TypeIdx(NextTypeID++);
2839 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002840
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002841 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002842
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002843 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002844
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002845 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002846 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002847 W.Visit(T);
2848 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002849
Richard Smith290d8012016-04-06 17:06:00 +00002850 // Record the offset for this type.
2851 unsigned Index = Idx.getIndex() - FirstTypeID;
2852 if (TypeOffsets.size() == Index)
2853 TypeOffsets.push_back(Offset);
2854 else if (TypeOffsets.size() < Index) {
2855 TypeOffsets.resize(Index + 1);
2856 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002857 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002858 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002859 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002860}
2861
Douglas Gregorc5046832009-04-27 18:38:38 +00002862//===----------------------------------------------------------------------===//
2863// Declaration Serialization
2864//===----------------------------------------------------------------------===//
2865
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002866/// \brief Write the block containing all of the declaration IDs
2867/// lexically declared within the given DeclContext.
2868///
2869/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2870/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002871uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002872 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002873 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002874 return 0;
2875
Douglas Gregor8f45df52009-04-16 22:23:12 +00002876 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002877 SmallVector<uint32_t, 128> KindDeclPairs;
2878 for (const auto *D : DC->decls()) {
2879 KindDeclPairs.push_back(D->getKind());
2880 KindDeclPairs.push_back(GetDeclRef(D));
2881 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002882
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002883 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002884 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002885 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2886 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002887 return Offset;
2888}
2889
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002890void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002891 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002892
2893 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002894 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002895 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002899 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002900 {
2901 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2902 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2903 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2904 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002905
2906 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002907 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002908 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002912 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002913 {
2914 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2915 FirstDeclID - NUM_PREDEF_DECL_IDS};
2916 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2917 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002918}
2919
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002920void ASTWriter::WriteFileDeclIDsMap() {
2921 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002922
Chandler Carruthb306d732015-03-27 00:31:20 +00002923 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2924 FileDeclIDs.begin(), FileDeclIDs.end());
2925 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2926 llvm::less_first());
2927
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002928 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002929 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2930 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2931 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2932 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2933 for (auto &LocDeclEntry : Info.DeclIDs)
2934 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002935 }
2936
David Blaikieb44f0bf2017-01-04 22:36:43 +00002937 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002938 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002941 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002942 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2943 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002944 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002945}
2946
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002947void ASTWriter::WriteComments() {
2948 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002949 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002950 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002951 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002952 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002953 AddSourceRange(I->getSourceRange(), Record);
2954 Record.push_back(I->getKind());
2955 Record.push_back(I->isTrailingComment());
2956 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002957 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2958 }
2959 Stream.ExitBlock();
2960}
2961
Douglas Gregorc5046832009-04-27 18:38:38 +00002962//===----------------------------------------------------------------------===//
2963// Global Method Pool and Selector Serialization
2964//===----------------------------------------------------------------------===//
2965
Douglas Gregore84a9da2009-04-20 20:36:09 +00002966namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002967
Douglas Gregorc78d3462009-04-24 21:10:55 +00002968// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002969class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002970 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002971
2972public:
2973 typedef Selector key_type;
2974 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002975
Sebastian Redl834bb972010-08-04 17:20:04 +00002976 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002977 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002978 ObjCMethodList Instance, Factory;
2979 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002980 typedef const data_type& data_type_ref;
2981
Justin Bogner25463f12014-04-18 20:27:24 +00002982 typedef unsigned hash_value_type;
2983 typedef unsigned offset_type;
2984
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002985 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002986
Justin Bogner25463f12014-04-18 20:27:24 +00002987 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002988 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002989 }
Mike Stump11289f42009-09-09 15:08:12 +00002990
2991 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002992 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002993 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002994 using namespace llvm::support;
2995 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002996 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002997 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002998 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2999 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003000 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003001 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003002 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003003 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003004 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003005 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003006 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003007 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003008 return std::make_pair(KeyLen, DataLen);
3009 }
Mike Stump11289f42009-09-09 15:08:12 +00003010
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003011 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003012 using namespace llvm::support;
3013 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003014 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003015 assert((Start >> 32) == 0 && "Selector key offset too large");
3016 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003017 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003018 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003019 if (N == 0)
3020 N = 1;
3021 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003022 LE.write<uint32_t>(
3023 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003024 }
Mike Stump11289f42009-09-09 15:08:12 +00003025
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003026 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003027 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003028 using namespace llvm::support;
3029 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003030 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003031 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003032 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003033 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003034 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003035 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003036 ++NumInstanceMethods;
3037
3038 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003039 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003040 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003041 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003042 ++NumFactoryMethods;
3043
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003044 unsigned InstanceBits = Methods.Instance.getBits();
3045 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003046 unsigned InstanceHasMoreThanOneDeclBit =
3047 Methods.Instance.hasMoreThanOneDecl();
3048 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3049 (InstanceHasMoreThanOneDeclBit << 2) |
3050 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003051 unsigned FactoryBits = Methods.Factory.getBits();
3052 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003053 unsigned FactoryHasMoreThanOneDeclBit =
3054 Methods.Factory.hasMoreThanOneDecl();
3055 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3056 (FactoryHasMoreThanOneDeclBit << 2) |
3057 FactoryBits;
3058 LE.write<uint16_t>(FullInstanceBits);
3059 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003060 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003061 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003062 if (Method->getMethod())
3063 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003064 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003065 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003066 if (Method->getMethod())
3067 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003068
3069 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003070 }
3071};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003072
Douglas Gregorc78d3462009-04-24 21:10:55 +00003073} // end anonymous namespace
3074
Sebastian Redla19a67f2010-08-03 21:58:15 +00003075/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003076///
3077/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003078/// in an on-disk hash table indexed by the selector. The hash table also
3079/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003080void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003081 using namespace llvm;
3082
Sebastian Redla19a67f2010-08-03 21:58:15 +00003083 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003084 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003085 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003086 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003087 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003088 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003089 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003090 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003091
Sebastian Redla19a67f2010-08-03 21:58:15 +00003092 // Create the on-disk hash table representation. We walk through every
3093 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003094 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003095 for (auto &SelectorAndID : SelectorIDs) {
3096 Selector S = SelectorAndID.first;
3097 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003098 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003099 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003100 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003101 ObjCMethodList(),
3102 ObjCMethodList()
3103 };
3104 if (F != SemaRef.MethodPool.end()) {
3105 Data.Instance = F->second.first;
3106 Data.Factory = F->second.second;
3107 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003108 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003109 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003110 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003111 // Selector already exists. Did it change?
3112 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003113 for (ObjCMethodList *M = &Data.Instance;
3114 !changed && M && M->getMethod(); M = M->getNext()) {
3115 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003116 changed = true;
3117 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003118 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003119 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003120 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003121 changed = true;
3122 }
3123 if (!changed)
3124 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003125 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003126 // A new method pool entry.
3127 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003128 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003129 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003130 }
3131
Douglas Gregorc78d3462009-04-24 21:10:55 +00003132 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003133 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003134 uint32_t BucketOffset;
3135 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003136 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003137 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003138 llvm::raw_svector_ostream Out(MethodPool);
3139 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003140 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003141 BucketOffset = Generator.Emit(Out, Trait);
3142 }
3143
3144 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003145 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003146 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003150 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003151
Douglas Gregor95c13f52009-04-25 17:48:32 +00003152 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003153 {
3154 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3155 NumTableEntries};
3156 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3157 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003158
3159 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003160 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003161 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003165 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003166
3167 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003168 {
3169 RecordData::value_type Record[] = {
3170 SELECTOR_OFFSETS, SelectorOffsets.size(),
3171 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3172 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3173 bytes(SelectorOffsets));
3174 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003175 }
3176}
3177
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003178/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003179void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003180 using namespace llvm;
3181 if (SemaRef.ReferencedSelectors.empty())
3182 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003183
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003184 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003185 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003187 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003188 // very tricky to fix, and given that @selector shouldn't really appear in
3189 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003190 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3191 Selector Sel = SelectorAndLocation.first;
3192 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003193 Writer.AddSelectorRef(Sel);
3194 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003195 }
Richard Smithe64e7452016-04-18 21:54:58 +00003196 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003197}
3198
Douglas Gregorc5046832009-04-27 18:38:38 +00003199//===----------------------------------------------------------------------===//
3200// Identifier Table Serialization
3201//===----------------------------------------------------------------------===//
3202
Richard Smithb3761912015-02-05 23:08:52 +00003203/// Determine the declaration that should be put into the name lookup table to
3204/// represent the given declaration in this module. This is usually D itself,
3205/// but if D was imported and merged into a local declaration, we want the most
3206/// recent local declaration instead. The chosen declaration will be the most
3207/// recent declaration in any module that imports this one.
3208static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3209 NamedDecl *D) {
3210 if (!LangOpts.Modules || !D->isFromASTFile())
3211 return D;
3212
3213 if (Decl *Redecl = D->getPreviousDecl()) {
3214 // For Redeclarable decls, a prior declaration might be local.
3215 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003216 // If we find a local decl, we're done.
3217 if (!Redecl->isFromASTFile()) {
3218 // Exception: in very rare cases (for injected-class-names), not all
3219 // redeclarations are in the same semantic context. Skip ones in a
3220 // different context. They don't go in this lookup table at all.
3221 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3222 D->getDeclContext()->getRedeclContext()))
3223 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003224 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003225 }
3226
Richard Smithfe620d22015-03-05 23:24:12 +00003227 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003228 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003229 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003230 break;
3231 }
3232 } else if (Decl *First = D->getCanonicalDecl()) {
3233 // For Mergeable decls, the first decl might be local.
3234 if (!First->isFromASTFile())
3235 return cast<NamedDecl>(First);
3236 }
3237
3238 // All declarations are imported. Our most recent declaration will also be
3239 // the most recent one in anyone who imports us.
3240 return D;
3241}
3242
Douglas Gregorc78d3462009-04-24 21:10:55 +00003243namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003244
Richard Smithcf97cf62015-04-19 01:34:23 +00003245class ASTIdentifierTableTrait {
3246 ASTWriter &Writer;
3247 Preprocessor &PP;
3248 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003249 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003250 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003251 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003252
3253 /// \brief Determines whether this is an "interesting" identifier that needs a
3254 /// full IdentifierInfo structure written into the hash table. Notably, this
3255 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3256 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003257 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003258 if (MacroOffset ||
3259 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003260 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003261 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003262 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003263 return true;
3264
3265 return false;
3266 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003267
Douglas Gregore84a9da2009-04-20 20:36:09 +00003268public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003269 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003270 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003271
Sebastian Redl539c5062010-08-18 23:57:32 +00003272 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003273 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003274
Justin Bogner25463f12014-04-18 20:27:24 +00003275 typedef unsigned hash_value_type;
3276 typedef unsigned offset_type;
3277
Richard Smithd7329392015-04-21 21:46:32 +00003278 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003279 IdentifierResolver &IdResolver, bool IsModule,
3280 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003281 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003282 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3283 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003284
Richard Smithd79514e2016-02-05 19:03:40 +00003285 bool needDecls() const { return NeedDecls; }
3286
Justin Bogner25463f12014-04-18 20:27:24 +00003287 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003288 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003289 }
Mike Stump11289f42009-09-09 15:08:12 +00003290
Richard Smith33e0f7e2015-07-22 02:08:40 +00003291 bool isInterestingIdentifier(const IdentifierInfo *II) {
3292 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3293 return isInterestingIdentifier(II, MacroOffset);
3294 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003295
Richard Smith9c254182015-07-19 21:41:12 +00003296 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3297 return isInterestingIdentifier(II, 0);
3298 }
3299
Mike Stump11289f42009-09-09 15:08:12 +00003300 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003301 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003302 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003303 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003304 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3305 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003306 DataLen += 2; // 2 bytes for builtin ID
3307 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003308 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003309 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003310
Richard Smitha534a312015-07-21 23:54:07 +00003311 if (NeedDecls) {
3312 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3313 DEnd = IdResolver.end();
3314 D != DEnd; ++D)
3315 DataLen += 4;
3316 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003317 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003318 using namespace llvm::support;
3319 endian::Writer<little> LE(Out);
3320
Richard Smithdf8a8312015-03-13 04:05:01 +00003321 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003322 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003323 // We emit the key length after the data length so that every
3324 // string is preceded by a 16-bit length. This matches the PTH
3325 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003326 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003327 return std::make_pair(KeyLen, DataLen);
3328 }
Mike Stump11289f42009-09-09 15:08:12 +00003329
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003330 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003331 unsigned KeyLen) {
3332 // Record the location of the key data. This is used when generating
3333 // the mapping from persistent IDs to strings.
3334 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003335
3336 // Emit the offset of the key/data length information to the interesting
3337 // identifiers table if necessary.
3338 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3339 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3340
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003341 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003342 }
Mike Stump11289f42009-09-09 15:08:12 +00003343
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003344 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003345 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003346 using namespace llvm::support;
3347 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003348
Richard Smithd7329392015-04-21 21:46:32 +00003349 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3350 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003351 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003352 return;
3353 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003354
Justin Bognere1c147c2014-03-28 22:03:19 +00003355 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003356 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3357 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003358 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003359 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003360 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003361 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003362 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3363 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003364 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003365 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003366 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003367 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003368
Richard Smithd7329392015-04-21 21:46:32 +00003369 if (HadMacroDefinition)
3370 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003371
Richard Smitha534a312015-07-21 23:54:07 +00003372 if (NeedDecls) {
3373 // Emit the declaration IDs in reverse order, because the
3374 // IdentifierResolver provides the declarations as they would be
3375 // visible (e.g., the function "stat" would come before the struct
3376 // "stat"), but the ASTReader adds declarations to the end of the list
3377 // (so we need to see the struct "stat" before the function "stat").
3378 // Only emit declarations that aren't from a chained PCH, though.
3379 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3380 IdResolver.end());
3381 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3382 DEnd = Decls.rend();
3383 D != DEnd; ++D)
3384 LE.write<uint32_t>(
3385 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3386 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003387 }
3388};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003389
Douglas Gregore84a9da2009-04-20 20:36:09 +00003390} // end anonymous namespace
3391
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003392/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003393///
3394/// The identifier table consists of a blob containing string data
3395/// (the actual identifiers themselves) and a separate "offsets" index
3396/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003397void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3398 IdentifierResolver &IdResolver,
3399 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003400 using namespace llvm;
3401
Richard Smith33e0f7e2015-07-22 02:08:40 +00003402 RecordData InterestingIdents;
3403
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003404 // Create and write out the blob that contains the identifier
3405 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003406 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003407 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003408 ASTIdentifierTableTrait Trait(
3409 *this, PP, IdResolver, IsModule,
3410 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003411
Douglas Gregore6648fb2009-04-28 20:33:11 +00003412 // Look for any identifiers that were named while processing the
3413 // headers, but are otherwise not needed. We add these to the hash
3414 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003415 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003416 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003417 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003418 for (const auto &ID : PP.getIdentifierTable())
3419 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003420 // Sort the identifiers lexicographically before getting them references so
3421 // that their order is stable.
3422 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3423 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003424 if (Trait.isInterestingNonMacroIdentifier(II))
3425 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003426
Sebastian Redlff4a2952010-07-23 23:49:55 +00003427 // Create the on-disk hash table representation. We only store offsets
3428 // for identifiers that appear here for the first time.
3429 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003430 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003431 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003432 IdentID ID = IdentIDPair.second;
3433 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003434 // Write out identifiers if either the ID is local or the identifier has
3435 // changed since it was loaded.
3436 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3437 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003438 (Trait.needDecls() &&
3439 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003440 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003441 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003442
Douglas Gregore84a9da2009-04-20 20:36:09 +00003443 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003444 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003445 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003446 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003447 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003448 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003449 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003450 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003451 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003452 }
3453
3454 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003455 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003456 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003459 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003460
3461 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003462 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003463 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003464 }
3465
3466 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003467 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003468 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003470 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003472 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003473
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003474#ifndef NDEBUG
3475 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3476 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3477#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003478
3479 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3480 IdentifierOffsets.size(),
3481 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003482 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003483 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003484
3485 // In C++, write the list of interesting identifiers (those that are
3486 // defined as macros, poisoned, or similar unusual things).
3487 if (!InterestingIdents.empty())
3488 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003489}
3490
Douglas Gregorc5046832009-04-27 18:38:38 +00003491//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003492// DeclContext's Name Lookup Table Serialization
3493//===----------------------------------------------------------------------===//
3494
3495namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003496
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003497// Trait used for the on-disk hash table used in the method pool.
3498class ASTDeclContextNameLookupTrait {
3499 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003500 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003501
3502public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003503 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003504 typedef key_type key_type_ref;
3505
Richard Smithd88a7f12015-09-01 20:35:42 +00003506 /// A start and end index into DeclIDs, representing a sequence of decls.
3507 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003508 typedef const data_type& data_type_ref;
3509
Justin Bogner25463f12014-04-18 20:27:24 +00003510 typedef unsigned hash_value_type;
3511 typedef unsigned offset_type;
3512
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003513 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3514
Richard Smithd88a7f12015-09-01 20:35:42 +00003515 template<typename Coll>
3516 data_type getData(const Coll &Decls) {
3517 unsigned Start = DeclIDs.size();
3518 for (NamedDecl *D : Decls) {
3519 DeclIDs.push_back(
3520 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3521 }
3522 return std::make_pair(Start, DeclIDs.size());
3523 }
3524
3525 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3526 unsigned Start = DeclIDs.size();
3527 for (auto ID : FromReader)
3528 DeclIDs.push_back(ID);
3529 return std::make_pair(Start, DeclIDs.size());
3530 }
3531
3532 static bool EqualKey(key_type_ref a, key_type_ref b) {
3533 return a == b;
3534 }
3535
Richard Smitha06c7e62015-08-26 23:55:49 +00003536 hash_value_type ComputeHash(DeclarationNameKey Name) {
3537 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003538 }
3539
Richard Smithd88a7f12015-09-01 20:35:42 +00003540 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3541 assert(Writer.hasChain() &&
3542 "have reference to loaded module file but no chain?");
3543
3544 using namespace llvm::support;
3545 endian::Writer<little>(Out)
3546 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3547 }
3548
Richard Smitha06c7e62015-08-26 23:55:49 +00003549 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3550 DeclarationNameKey Name,
3551 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003552 using namespace llvm::support;
3553 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003554 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003555 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003556 case DeclarationName::Identifier:
3557 case DeclarationName::ObjCZeroArgSelector:
3558 case DeclarationName::ObjCOneArgSelector:
3559 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003560 case DeclarationName::CXXLiteralOperatorName:
3561 KeyLen += 4;
3562 break;
3563 case DeclarationName::CXXOperatorName:
3564 KeyLen += 1;
3565 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003566 case DeclarationName::CXXConstructorName:
3567 case DeclarationName::CXXDestructorName:
3568 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003569 case DeclarationName::CXXUsingDirective:
3570 break;
3571 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003572 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003573
Richard Smithf02662d2015-07-30 03:17:16 +00003574 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003575 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3576 assert(uint16_t(DataLen) == DataLen &&
3577 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003578 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003579
3580 return std::make_pair(KeyLen, DataLen);
3581 }
3582
Richard Smitha06c7e62015-08-26 23:55:49 +00003583 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003584 using namespace llvm::support;
3585 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003586 LE.write<uint8_t>(Name.getKind());
3587 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003588 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003589 case DeclarationName::CXXLiteralOperatorName:
3590 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003591 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003592 case DeclarationName::ObjCZeroArgSelector:
3593 case DeclarationName::ObjCOneArgSelector:
3594 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003595 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003596 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003597 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003598 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003599 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003600 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003601 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003602 case DeclarationName::CXXConstructorName:
3603 case DeclarationName::CXXDestructorName:
3604 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003605 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003606 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003607 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003608
3609 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003610 }
3611
Richard Smitha06c7e62015-08-26 23:55:49 +00003612 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3613 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003614 using namespace llvm::support;
3615 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003616 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003617 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3618 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003619 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3620 }
3621};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003622
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003623} // end anonymous namespace
3624
Chandler Carruthe972c362015-03-26 03:11:40 +00003625bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3626 DeclContext *DC) {
3627 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3628}
3629
3630bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3631 DeclContext *DC) {
3632 for (auto *D : Result.getLookupResult())
3633 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3634 return false;
3635
3636 return true;
3637}
3638
Richard Smithd88a7f12015-09-01 20:35:42 +00003639void
Chandler Carruthe972c362015-03-26 03:11:40 +00003640ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003641 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003642 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3643 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003644 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003645
Chandler Carruthe972c362015-03-26 03:11:40 +00003646 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003647 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003648 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3649
3650 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003651 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3652 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003653 ASTDeclContextNameLookupTrait Trait(*this);
3654
Chandler Carruthe972c362015-03-26 03:11:40 +00003655 // The first step is to collect the declaration names which we need to
3656 // serialize into the name lookup table, and to collect them in a stable
3657 // order.
3658 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003659
Chandler Carruthe972c362015-03-26 03:11:40 +00003660 // We also build up small sets of the constructor and conversion function
3661 // names which are visible.
3662 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003663
Chandler Carruthe972c362015-03-26 03:11:40 +00003664 for (auto &Lookup : *DC->buildLookup()) {
3665 auto &Name = Lookup.first;
3666 auto &Result = Lookup.second;
3667
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003668 // If there are no local declarations in our lookup result, we
3669 // don't need to write an entry for the name at all. If we can't
3670 // write out a lookup set without performing more deserialization,
3671 // just skip this entry.
3672 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003673 isLookupResultEntirelyExternal(Result, DC))
3674 continue;
3675
3676 // We also skip empty results. If any of the results could be external and
3677 // the currently available results are empty, then all of the results are
3678 // external and we skip it above. So the only way we get here with an empty
3679 // results is when no results could have been external *and* we have
3680 // external results.
3681 //
3682 // FIXME: While we might want to start emitting on-disk entries for negative
3683 // lookups into a decl context as an optimization, today we *have* to skip
3684 // them because there are names with empty lookup results in decl contexts
3685 // which we can't emit in any stable ordering: we lookup constructors and
3686 // conversion functions in the enclosing namespace scope creating empty
3687 // results for them. This in almost certainly a bug in Clang's name lookup,
3688 // but that is likely to be hard or impossible to fix and so we tolerate it
3689 // here by omitting lookups with empty results.
3690 if (Lookup.second.getLookupResult().empty())
3691 continue;
3692
3693 switch (Lookup.first.getNameKind()) {
3694 default:
3695 Names.push_back(Lookup.first);
3696 break;
3697
Richard Smithcd45dbc2014-04-19 03:48:30 +00003698 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003699 assert(isa<CXXRecordDecl>(DC) &&
3700 "Cannot have a constructor name outside of a class!");
3701 ConstructorNameSet.insert(Name);
3702 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003703
3704 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003705 assert(isa<CXXRecordDecl>(DC) &&
3706 "Cannot have a conversion function name outside of a class!");
3707 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003708 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003709 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003710 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003711
Chandler Carruthe972c362015-03-26 03:11:40 +00003712 // Sort the names into a stable order.
3713 std::sort(Names.begin(), Names.end());
3714
Chandler Carruthffbf7052015-03-26 22:27:09 +00003715 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003716 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003717 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003718
Chandler Carruthffbf7052015-03-26 22:27:09 +00003719 // First we try the easy case by forming the current context's constructor
3720 // name and adding that name first. This is a very useful optimization to
3721 // avoid walking the lexical declarations in many cases, and it also
3722 // handles the only case where a constructor name can come from some other
3723 // lexical context -- when that name is an implicit constructor merged from
3724 // another declaration in the redecl chain. Any non-implicit constructor or
3725 // conversion function which doesn't occur in all the lexical contexts
3726 // would be an ODR violation.
3727 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3728 Context->getCanonicalType(Context->getRecordType(D)));
3729 if (ConstructorNameSet.erase(ImplicitCtorName))
3730 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003731
Chandler Carruthffbf7052015-03-26 22:27:09 +00003732 // If we still have constructors or conversion functions, we walk all the
3733 // names in the decl and add the constructors and conversion functions
3734 // which are visible in the order they lexically occur within the context.
3735 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3736 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3737 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3738 auto Name = ChildND->getDeclName();
3739 switch (Name.getNameKind()) {
3740 default:
3741 continue;
3742
3743 case DeclarationName::CXXConstructorName:
3744 if (ConstructorNameSet.erase(Name))
3745 Names.push_back(Name);
3746 break;
3747
3748 case DeclarationName::CXXConversionFunctionName:
3749 if (ConversionNameSet.erase(Name))
3750 Names.push_back(Name);
3751 break;
3752 }
3753
3754 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3755 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003756 }
3757
Chandler Carruthe972c362015-03-26 03:11:40 +00003758 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3759 "constructors by walking all the "
3760 "lexical members of the context.");
3761 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3762 "conversion functions by walking all "
3763 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003764 }
3765
Chandler Carruthe972c362015-03-26 03:11:40 +00003766 // Next we need to do a lookup with each name into this decl context to fully
3767 // populate any results from external sources. We don't actually use the
3768 // results of these lookups because we only want to use the results after all
3769 // results have been loaded and the pointers into them will be stable.
3770 for (auto &Name : Names)
3771 DC->lookup(Name);
3772
3773 // Now we need to insert the results for each name into the hash table. For
3774 // constructor names and conversion function names, we actually need to merge
3775 // all of the results for them into one list of results each and insert
3776 // those.
3777 SmallVector<NamedDecl *, 8> ConstructorDecls;
3778 SmallVector<NamedDecl *, 8> ConversionDecls;
3779
3780 // Now loop over the names, either inserting them or appending for the two
3781 // special cases.
3782 for (auto &Name : Names) {
3783 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3784
3785 switch (Name.getNameKind()) {
3786 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003787 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003788 break;
3789
3790 case DeclarationName::CXXConstructorName:
3791 ConstructorDecls.append(Result.begin(), Result.end());
3792 break;
3793
3794 case DeclarationName::CXXConversionFunctionName:
3795 ConversionDecls.append(Result.begin(), Result.end());
3796 break;
3797 }
3798 }
3799
3800 // Handle our two special cases if we ended up having any. We arbitrarily use
3801 // the first declaration's name here because the name itself isn't part of
3802 // the key, only the kind of name is used.
3803 if (!ConstructorDecls.empty())
3804 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003805 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003806 if (!ConversionDecls.empty())
3807 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003808 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003809
Richard Smithd88a7f12015-09-01 20:35:42 +00003810 // Create the on-disk hash table. Also emit the existing imported and
3811 // merged table if there is one.
3812 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3813 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003814}
3815
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003816/// \brief Write the block containing all of the declaration IDs
3817/// visible from the given DeclContext.
3818///
3819/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003820/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003821uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3822 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003823 // If we imported a key declaration of this namespace, write the visible
3824 // lookup results as an update record for it rather than including them
3825 // on this declaration. We will only look at key declarations on reload.
3826 if (isa<NamespaceDecl>(DC) && Chain &&
3827 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3828 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003829 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003830 Prev = Prev->getPreviousDecl())
3831 if (!Prev->isFromASTFile())
3832 return 0;
3833
3834 // Note that we need to emit an update record for the primary context.
3835 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3836
3837 // Make sure all visible decls are written. They will be recorded later. We
3838 // do this using a side data structure so we can sort the names into
3839 // a deterministic order.
3840 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3841 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3842 LookupResults;
3843 if (Map) {
3844 LookupResults.reserve(Map->size());
3845 for (auto &Entry : *Map)
3846 LookupResults.push_back(
3847 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3848 }
3849
3850 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3851 for (auto &NameAndResult : LookupResults) {
3852 DeclarationName Name = NameAndResult.first;
3853 DeclContext::lookup_result Result = NameAndResult.second;
3854 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3855 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3856 // We have to work around a name lookup bug here where negative lookup
3857 // results for these names get cached in namespace lookup tables (these
3858 // names should never be looked up in a namespace).
3859 assert(Result.empty() && "Cannot have a constructor or conversion "
3860 "function name in a namespace!");
3861 continue;
3862 }
3863
3864 for (NamedDecl *ND : Result)
3865 if (!ND->isFromASTFile())
3866 GetDeclRef(ND);
3867 }
3868
3869 return 0;
3870 }
3871
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003872 if (DC->getPrimaryContext() != DC)
3873 return 0;
3874
Chandler Carruthe972c362015-03-26 03:11:40 +00003875 // Skip contexts which don't support name lookup.
3876 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003877 return 0;
3878
3879 // If not in C++, we perform name lookup for the translation unit via the
3880 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003881 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003882 return 0;
3883
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003884 // Serialize the contents of the mapping used for lookup. Note that,
3885 // although we have two very different code paths, the serialized
3886 // representation is the same for both cases: a declaration name,
3887 // followed by a size, followed by references to the visible
3888 // declarations that have that name.
3889 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003890 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003891 if (!Map || Map->empty())
3892 return 0;
3893
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003894 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003895 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003896 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003897
3898 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003899 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003900 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003901 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003902 ++NumVisibleDeclContexts;
3903 return Offset;
3904}
3905
Sebastian Redla4071b42010-08-24 00:50:09 +00003906/// \brief Write an UPDATE_VISIBLE block for the given context.
3907///
3908/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3909/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003910/// (in C++), for namespaces, and for classes with forward-declared unscoped
3911/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003912void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003913 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003914 if (!Map || Map->empty())
3915 return;
3916
Sebastian Redla4071b42010-08-24 00:50:09 +00003917 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003918 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003919 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003920
Richard Smith5fc18a92015-07-12 23:43:21 +00003921 // If we're updating a namespace, select a key declaration as the key for the
3922 // update record; those are the only ones that will be checked on reload.
3923 if (isa<NamespaceDecl>(DC))
3924 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3925
Sebastian Redla4071b42010-08-24 00:50:09 +00003926 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003927 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003928 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003929}
3930
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003931/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3932void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003933 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003934 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3935}
3936
3937/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3938void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003939 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003940 return;
3941
3942 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3943 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00003944 for (const auto &I:Opts.OptMap) {
3945 AddString(I.getKey(), Record);
3946 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00003947 Record.push_back(V.Supported ? 1 : 0);
3948 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00003949 Record.push_back(V.Avail);
3950 Record.push_back(V.Core);
3951 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003952 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3953}
3954
Yaxun Liu5b746652016-12-18 05:18:55 +00003955void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
3956 if (!SemaRef.Context.getLangOpts().OpenCL)
3957 return;
3958
3959 RecordData Record;
3960 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
3961 Record.push_back(
3962 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
3963 Record.push_back(I.second.size());
3964 for (auto Ext : I.second)
3965 AddString(Ext, Record);
3966 }
3967 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
3968}
3969
3970void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
3971 if (!SemaRef.Context.getLangOpts().OpenCL)
3972 return;
3973
3974 RecordData Record;
3975 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
3976 Record.push_back(getDeclID(I.first));
3977 Record.push_back(static_cast<unsigned>(I.second.size()));
3978 for (auto Ext : I.second)
3979 AddString(Ext, Record);
3980 }
3981 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
3982}
3983
Justin Lebar67a78a62016-10-08 22:15:58 +00003984void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
3985 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
3986 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
3987 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
3988 }
3989}
3990
Douglas Gregor404cdde2012-01-27 01:47:08 +00003991void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003992 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003993 RecordData Categories;
3994
3995 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3996 unsigned Size = 0;
3997 unsigned StartIndex = Categories.size();
3998
3999 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4000
4001 // Allocate space for the size.
4002 Categories.push_back(0);
4003
4004 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004005 for (ObjCInterfaceDecl::known_categories_iterator
4006 Cat = Class->known_categories_begin(),
4007 CatEnd = Class->known_categories_end();
4008 Cat != CatEnd; ++Cat, ++Size) {
4009 assert(getDeclID(*Cat) != 0 && "Bogus category");
4010 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004011 }
4012
4013 // Update the size.
4014 Categories[StartIndex] = Size;
4015
4016 // Record this interface -> category map.
4017 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4018 CategoriesMap.push_back(CatInfo);
4019 }
4020
4021 // Sort the categories map by the definition ID, since the reader will be
4022 // performing binary searches on this information.
4023 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4024
4025 // Emit the categories map.
4026 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004027
David Blaikieb44f0bf2017-01-04 22:36:43 +00004028 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004029 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004032 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004033
4034 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4035 Stream.EmitRecordWithBlob(AbbrevID, Record,
4036 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004037 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004038
Douglas Gregor404cdde2012-01-27 01:47:08 +00004039 // Emit the category lists.
4040 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4041}
4042
Richard Smithe40f2ba2013-08-07 21:41:30 +00004043void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4044 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4045
4046 if (LPTMap.empty())
4047 return;
4048
4049 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004050 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004051 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004052 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004053 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004054 AddDeclRef(LPT.D, Record);
4055 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004056
Justin Lebar28f09c52016-10-10 16:26:08 +00004057 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004058 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004059 }
4060 }
4061 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4062}
4063
Dario Domizioli13a0a382014-05-23 12:13:25 +00004064/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4065void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4066 RecordData Record;
4067 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4068 AddSourceLocation(PragmaLoc, Record);
4069 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4070}
4071
Nico Weber779355f2016-03-02 23:22:00 +00004072/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4073void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4074 RecordData Record;
4075 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4076 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4077}
4078
Nico Weber42932312016-03-03 00:17:35 +00004079/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4080//module.
4081void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4082 RecordData Record;
4083 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4084 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4085 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4086}
4087
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004088void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4089 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004090 // Enter the extension block.
4091 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4092
4093 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004094 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004095 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4096 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
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::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004101 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004102
4103 // Emit the metadata record.
4104 RecordData Record;
4105 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4106 Record.push_back(EXTENSION_METADATA);
4107 Record.push_back(Metadata.MajorVersion);
4108 Record.push_back(Metadata.MinorVersion);
4109 Record.push_back(Metadata.BlockName.size());
4110 Record.push_back(Metadata.UserInfo.size());
4111 SmallString<64> Buffer;
4112 Buffer += Metadata.BlockName;
4113 Buffer += Metadata.UserInfo;
4114 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4115
4116 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004117 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004118
4119 // Exit the extension block.
4120 Stream.ExitBlock();
4121}
4122
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004123//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004124// General Serialization Routines
4125//===----------------------------------------------------------------------===//
4126
Richard Smith69c82bf2016-04-01 22:52:03 +00004127/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004128void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4129 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004130 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004131 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004132 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004133 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004134
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004135#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004136
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004137 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004138}
4139
John McCallf413f5e2013-05-03 00:10:13 +00004140void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4141 AddSourceLocation(Tok.getLocation(), Record);
4142 Record.push_back(Tok.getLength());
4143
4144 // FIXME: When reading literal tokens, reconstruct the literal pointer
4145 // if it is needed.
4146 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4147 // FIXME: Should translate token kind to a stable encoding.
4148 Record.push_back(Tok.getKind());
4149 // FIXME: Should translate token flags to a stable encoding.
4150 Record.push_back(Tok.getFlags());
4151}
4152
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004153void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004154 Record.push_back(Str.size());
4155 Record.insert(Record.end(), Str.begin(), Str.end());
4156}
4157
Richard Smith7ed1bc92014-12-05 22:42:13 +00004158bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004159 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004160
Richard Smith54cc3c22014-12-11 20:50:24 +00004161 bool Changed =
4162 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004163
4164 // Remove a prefix to make the path relative, if relevant.
4165 const char *PathBegin = Path.data();
4166 const char *PathPtr =
4167 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4168 if (PathPtr != PathBegin) {
4169 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4170 Changed = true;
4171 }
4172
4173 return Changed;
4174}
4175
4176void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4177 SmallString<128> FilePath(Path);
4178 PreparePathForOutput(FilePath);
4179 AddString(FilePath, Record);
4180}
4181
Mehdi Amini57a41912015-09-10 01:46:39 +00004182void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004183 StringRef Path) {
4184 SmallString<128> FilePath(Path);
4185 PreparePathForOutput(FilePath);
4186 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4187}
4188
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004189void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4190 RecordDataImpl &Record) {
4191 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004192 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004193 Record.push_back(*Minor + 1);
4194 else
4195 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004196 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004197 Record.push_back(*Subminor + 1);
4198 else
4199 Record.push_back(0);
4200}
4201
Douglas Gregore84a9da2009-04-20 20:36:09 +00004202/// \brief Note that the identifier II occurs at the given offset
4203/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004204void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004205 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004206 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004207 // up earlier in the chain and thus don't need an offset.
4208 if (ID >= FirstIdentID)
4209 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004210}
4211
Douglas Gregor95c13f52009-04-25 17:48:32 +00004212/// \brief Note that the selector Sel occurs at the given offset
4213/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004214void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004215 unsigned ID = SelectorIDs[Sel];
4216 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004217 // Don't record offsets for selectors that are also available in a different
4218 // file.
4219 if (ID < FirstSelectorID)
4220 return;
4221 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004222}
4223
David Blaikie61137e12017-01-05 18:23:18 +00004224ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4225 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4226 bool IncludeTimestamps)
David Blaikie95dd3622017-01-05 18:45:45 +00004227 : Stream(Stream), IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004228 for (const auto &Ext : Extensions) {
4229 if (auto Writer = Ext->createExtensionWriter(*this))
4230 ModuleFileExtensionWriters.push_back(std::move(Writer));
4231 }
4232}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004233
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004234ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004235 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004236}
4237
Richard Smithb3761912015-02-05 23:08:52 +00004238const LangOptions &ASTWriter::getLangOpts() const {
4239 assert(WritingAST && "can't determine lang opts when not writing AST");
4240 return Context->getLangOpts();
4241}
4242
Richard Smithe75ee0f2015-08-17 07:13:32 +00004243time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4244 return IncludeTimestamps ? E->getModificationTime() : 0;
4245}
4246
Adrian Prantladbd2b12015-09-22 23:26:31 +00004247uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4248 Module *WritingModule, StringRef isysroot,
4249 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004250 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004251
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004252 ASTHasCompilerErrors = hasErrors;
4253
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004254 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004255 Stream.Emit((unsigned)'C', 8);
4256 Stream.Emit((unsigned)'P', 8);
4257 Stream.Emit((unsigned)'C', 8);
4258 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004259
Chris Lattner28fa4e62009-04-26 22:26:21 +00004260 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004261
Douglas Gregoreda8e122011-08-09 15:13:55 +00004262 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004263 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004264 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004265 ASTFileSignature Signature =
4266 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004267 Context = nullptr;
4268 PP = nullptr;
4269 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004270 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004271
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004272 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004273 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004274}
4275
Douglas Gregora94a1542011-07-27 21:45:57 +00004276template<typename Vector>
4277static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4278 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004279 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4280 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004281 Writer.AddDeclRef(*I, Record);
4282 }
4283}
4284
Adrian Prantladbd2b12015-09-22 23:26:31 +00004285uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4286 const std::string &OutputFile,
4287 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004288 using namespace llvm;
4289
Craig Toppera13603a2014-05-22 05:54:18 +00004290 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004291
Douglas Gregorcf68c582011-12-01 22:20:10 +00004292 // Make sure that the AST reader knows to finalize itself.
4293 if (Chain)
4294 Chain->finalizeForWriting();
4295
Sebastian Redl143413f2010-07-12 22:02:52 +00004296 ASTContext &Context = SemaRef.Context;
4297 Preprocessor &PP = SemaRef.PP;
4298
Douglas Gregordab42432011-08-12 00:15:20 +00004299 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004300 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4301 if (D) {
4302 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4303 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004304 }
4305 };
4306 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4307 PREDEF_DECL_TRANSLATION_UNIT_ID);
4308 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4309 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4310 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4311 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4312 PREDEF_DECL_OBJC_PROTOCOL_ID);
4313 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4314 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4315 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4316 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4317 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004318 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004319 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4320 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004321 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004322 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4323 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004324 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4325 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004326 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4327 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004328 RegisterPredefDecl(Context.TypePackElementDecl,
4329 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004330
Chris Lattner0c797362009-09-08 18:19:27 +00004331 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004332 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004333 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004334 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004335 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004336
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004337 // Build a record containing all of the file scoped decls in this file.
4338 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004339 if (!isModule)
4340 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4341 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004342
Douglas Gregor851443c2011-08-12 01:39:19 +00004343 // Build a record containing all of the delegating constructors we still need
4344 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004345 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004346 if (!isModule)
4347 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004348
Douglas Gregor851443c2011-08-12 01:39:19 +00004349 // Write the set of weak, undeclared identifiers. We always write the
4350 // entire table, since later PCH files in a PCH chain are only interested in
4351 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004352 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004353 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4354 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4355 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4356 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4357 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4358 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4359 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004360 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004361
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004362 // Build a record containing all of the ext_vector declarations.
4363 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004364 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004365
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004366 // Build a record containing all of the VTable uses information.
4367 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004368 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004369 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4370 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4371 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4372 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4373 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004374 }
4375
Nico Weber72889432014-09-06 01:25:55 +00004376 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4377 RecordData UnusedLocalTypedefNameCandidates;
4378 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4379 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4380
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004381 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004382 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004383 for (const auto &I : SemaRef.PendingInstantiations) {
4384 AddDeclRef(I.first, PendingInstantiations);
4385 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004386 }
4387 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4388 "There are local ones at end of translation unit!");
4389
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004390 // Build a record containing some declaration references.
4391 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004392 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004393 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4394 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004395 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004396 }
4397
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004398 RecordData CUDASpecialDeclRefs;
4399 if (Context.getcudaConfigureCallDecl()) {
4400 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4401 }
4402
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004403 // Build a record containing all of the known namespaces.
4404 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004405 for (const auto &I : SemaRef.KnownNamespaces) {
4406 if (!I.second)
4407 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004408 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004409
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004410 // Build a record of all used, undefined objects that require definitions.
4411 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004412
4413 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004414 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004415 for (const auto &I : Undefined) {
4416 AddDeclRef(I.first, UndefinedButUsed);
4417 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004418 }
4419
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004420 // Build a record containing all delete-expressions that we would like to
4421 // analyze later in AST.
4422 RecordData DeleteExprsToAnalyze;
4423
4424 for (const auto &DeleteExprsInfo :
4425 SemaRef.getMismatchingDeleteExpressions()) {
4426 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4427 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4428 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4429 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4430 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4431 }
4432 }
4433
Douglas Gregor112b9072012-10-18 05:31:06 +00004434 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004435 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004436
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004437 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004438 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004439
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004440 // This is so that older clang versions, before the introduction
4441 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004442 {
4443 RecordData Record = {VERSION_MAJOR};
4444 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4445 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004446
Douglas Gregor851443c2011-08-12 01:39:19 +00004447 // Create a lexical update block containing all of the declarations in the
4448 // translation unit that do not come from other AST files.
4449 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004450 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4451 for (const auto *D : TU->noload_decls()) {
4452 if (!D->isFromASTFile()) {
4453 NewGlobalKindDeclPairs.push_back(D->getKind());
4454 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4455 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004456 }
4457
David Blaikieb44f0bf2017-01-04 22:36:43 +00004458 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004459 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4460 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004461 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004462 {
4463 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4464 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4465 bytes(NewGlobalKindDeclPairs));
4466 }
4467
Douglas Gregor851443c2011-08-12 01:39:19 +00004468 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004469 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004470 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4471 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004472 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004473 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004474 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004475
4476 // If we have any extern "C" names, write out a visible update for them.
4477 if (Context.ExternCContext)
4478 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004479
4480 // If the translation unit has an anonymous namespace, and we don't already
4481 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004482 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004483 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4484 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004485 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004486 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004487 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004488
Richard Smith5652c0f2014-03-21 01:48:23 +00004489 // Add update records for all mangling numbers and static local numbers.
4490 // These aren't really update records, but this is a convenient way of
4491 // tagging this rare extra data onto the declarations.
4492 for (const auto &Number : Context.MangleNumbers)
4493 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004494 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4495 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004496 for (const auto &Number : Context.StaticLocalNumbers)
4497 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004498 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4499 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004500
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004501 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004502 // an AST file, are registered for serialization. Likewise for template
4503 // specializations added to imported templates.
4504 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004505 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004506 }
4507
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004508 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004509 // serialization, if we're storing decls with identifiers.
4510 if (!WritingModule || !getLangOpts().CPlusPlus) {
4511 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004512 for (const auto &ID : PP.getIdentifierTable()) {
4513 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004514 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4515 IIs.push_back(II);
4516 }
4517 // Sort the identifiers to visit based on their name.
4518 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4519 for (const IdentifierInfo *II : IIs) {
4520 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4521 DEnd = SemaRef.IdResolver.end();
4522 D != DEnd; ++D) {
4523 GetDeclRef(*D);
4524 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004525 }
4526 }
4527
Manman Rena0f31a02016-04-29 19:04:05 +00004528 // For method pool in the module, if it contains an entry for a selector,
4529 // the entry should be complete, containing everything introduced by that
4530 // module and all modules it imports. It's possible that the entry is out of
4531 // date, so we need to pull in the new content here.
4532
4533 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4534 // safe, we copy all selectors out.
4535 llvm::SmallVector<Selector, 256> AllSelectors;
4536 for (auto &SelectorAndID : SelectorIDs)
4537 AllSelectors.push_back(SelectorAndID.first);
4538 for (auto &Selector : AllSelectors)
4539 SemaRef.updateOutOfDateSelector(Selector);
4540
Douglas Gregor5204bde2011-08-02 16:26:37 +00004541 // Form the record of special types.
4542 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004543 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004544 AddTypeRef(Context.getFILEType(), SpecialTypes);
4545 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4546 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4547 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4548 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004549 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004550 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004551
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004552 if (Chain) {
4553 // Write the mapping information describing our module dependencies and how
4554 // each of those modules were mapped into our own offset/ID space, so that
4555 // the reader can build the appropriate mapping to its own offset/ID space.
4556 // The map consists solely of a blob with the following format:
4557 // *(module-name-len:i16 module-name:len*i8
4558 // source-location-offset:i32
4559 // identifier-id:i32
4560 // preprocessed-entity-id:i32
4561 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004562 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004563 // selector-id:i32
4564 // declaration-id:i32
4565 // c++-base-specifiers-id:i32
4566 // type-id:i32)
4567 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004568 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004569 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004571 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004572 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004573 {
4574 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004575 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004576 using namespace llvm::support;
4577 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004578 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004579 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004580 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004581
Ben Langmuir785180e2014-10-20 16:27:30 +00004582 // Note: if a base ID was uint max, it would not be possible to load
4583 // another module after it or have more than one entity inside it.
4584 uint32_t None = std::numeric_limits<uint32_t>::max();
4585
4586 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4587 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4588 if (ShouldWrite)
4589 LE.write<uint32_t>(BaseID);
4590 else
4591 LE.write<uint32_t>(None);
4592 };
4593
Ben Langmuirfe971d92014-08-16 04:54:18 +00004594 // These values should be unique within a chain, since they will be read
4595 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004596 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4597 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4598 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4599 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4600 M->NumPreprocessedEntities);
4601 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4602 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4603 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4604 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004605 }
4606 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004607 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004608 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4609 Buffer.data(), Buffer.size());
4610 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004611
Richard Smithb9eab6d2014-03-20 19:44:17 +00004612 RecordData DeclUpdatesOffsetsRecord;
4613
Richard Smith59442b42014-03-20 20:07:19 +00004614 // Keep writing types, declarations, and declaration update records
4615 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004616 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4617 WriteTypeAbbrevs();
4618 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004619 do {
4620 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4621 while (!DeclTypesToEmit.empty()) {
4622 DeclOrType DOT = DeclTypesToEmit.front();
4623 DeclTypesToEmit.pop();
4624 if (DOT.isType())
4625 WriteType(DOT.getType());
4626 else
4627 WriteDecl(Context, DOT.getDecl());
4628 }
4629 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004630 Stream.ExitBlock();
4631
Richard Smithb9eab6d2014-03-20 19:44:17 +00004632 DoneWritingDeclsAndTypes = true;
4633
4634 // These things can only be done once we've written out decls and types.
4635 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004636 if (!DeclUpdatesOffsetsRecord.empty())
4637 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004638 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004639 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004640 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004641 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004642 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004643 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004644 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004645 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004646 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004647 WriteFPPragmaOptions(SemaRef.getFPOptions());
4648 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004649 WriteOpenCLExtensionTypes(SemaRef);
4650 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004651 WriteCUDAPragmas(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004652 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004653
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004654 // If we're emitting a module, write out the submodule information.
4655 if (WritingModule)
4656 WriteSubmodules(WritingModule);
Richard Smithdc1f0422016-07-20 19:10:16 +00004657 else if (!getLangOpts().CurrentModule.empty()) {
4658 // If we're building a PCH in the implementation of a module, we may need
4659 // the description of the current module.
4660 //
4661 // FIXME: We may need other modules that we did not load from an AST file,
4662 // such as if a module declares a 'conflicts' on a different module.
4663 Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
4664 getLangOpts().CurrentModule);
4665 if (M && !M->IsFromModuleFile)
4666 WriteSubmodules(M);
4667 }
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004668
Douglas Gregor5204bde2011-08-02 16:26:37 +00004669 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4670
Douglas Gregord4df8652009-04-22 22:02:47 +00004671 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004672 if (!EagerlyDeserializedDecls.empty())
4673 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004674
4675 // Write the record containing tentative definitions.
4676 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004677 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004678
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004679 // Write the record containing unused file scoped decls.
4680 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004681 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004682
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004683 // Write the record containing weak undeclared identifiers.
4684 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004685 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004686 WeakUndeclaredIdentifiers);
4687
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004688 // Write the record containing ext_vector type names.
4689 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004690 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004691
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004692 // Write the record containing VTable uses information.
4693 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004694 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004695
Nico Weber72889432014-09-06 01:25:55 +00004696 // Write the record containing potentially unused local typedefs.
4697 if (!UnusedLocalTypedefNameCandidates.empty())
4698 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4699 UnusedLocalTypedefNameCandidates);
4700
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004701 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004702 if (!PendingInstantiations.empty())
4703 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004704
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004705 // Write the record containing declaration references of Sema.
4706 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004707 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004708
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004709 // Write the record containing CUDA-specific declaration references.
4710 if (!CUDASpecialDeclRefs.empty())
4711 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004712
4713 // Write the delegating constructors.
4714 if (!DelegatingCtorDecls.empty())
4715 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004716
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004717 // Write the known namespaces.
4718 if (!KnownNamespaces.empty())
4719 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004720
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004721 // Write the undefined internal functions and variables, and inline functions.
4722 if (!UndefinedButUsed.empty())
4723 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004724
4725 if (!DeleteExprsToAnalyze.empty())
4726 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4727
Douglas Gregor851443c2011-08-12 01:39:19 +00004728 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004729 for (auto *DC : UpdatedDeclContexts)
4730 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004731
Douglas Gregor959bb062011-12-03 01:15:29 +00004732 if (!WritingModule) {
4733 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004734 struct ModuleInfo {
4735 uint64_t ID;
4736 Module *M;
4737 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4738 };
Richard Smith56be7542014-03-21 00:33:59 +00004739 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004740 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004741 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004742 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4743 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004744 }
Richard Smith56be7542014-03-21 00:33:59 +00004745
4746 if (!Imports.empty()) {
4747 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4748 return A.ID < B.ID;
4749 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004750 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4751 return A.ID == B.ID;
4752 };
Richard Smith56be7542014-03-21 00:33:59 +00004753
4754 // Sort and deduplicate module IDs.
4755 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004756 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004757 Imports.end());
4758
4759 RecordData ImportedModules;
4760 for (const auto &Import : Imports) {
4761 ImportedModules.push_back(Import.ID);
4762 // FIXME: If the module has macros imported then later has declarations
4763 // imported, this location won't be the right one as a location for the
4764 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004765 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004766 }
4767
Douglas Gregor959bb062011-12-03 01:15:29 +00004768 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4769 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004770 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004771
Douglas Gregor404cdde2012-01-27 01:47:08 +00004772 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004773 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004774 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004775 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004776 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004777 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004778
Douglas Gregor08f01292009-04-17 22:13:46 +00004779 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004780 RecordData::value_type Record[] = {
4781 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004782 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004783 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004784
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004785 // Write the module file extension blocks.
4786 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004787 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004788
Adrian Prantladbd2b12015-09-22 23:26:31 +00004789 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004790}
4791
Richard Smithb9eab6d2014-03-20 19:44:17 +00004792void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004793 if (DeclUpdates.empty())
4794 return;
4795
Richard Smith59442b42014-03-20 20:07:19 +00004796 DeclUpdateMap LocalUpdates;
4797 LocalUpdates.swap(DeclUpdates);
4798
Richard Smith6ef42932014-03-20 21:02:00 +00004799 for (auto &DeclUpdate : LocalUpdates) {
4800 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004801
Richard Smithd28ac5b2014-03-22 23:33:22 +00004802 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004803 RecordData RecordData;
4804 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004805 for (auto &Update : DeclUpdate.second) {
4806 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4807
Richard Smith69c82bf2016-04-01 22:52:03 +00004808 // An updated body is emitted last, so that the reader doesn't need
4809 // to skip over the lazy body to reach statements for other records.
4810 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4811 HasUpdatedBody = true;
4812 else
4813 Record.push_back(Kind);
4814
Richard Smith6ef42932014-03-20 21:02:00 +00004815 switch (Kind) {
4816 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4817 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4818 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004819 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004820 Record.push_back(GetDeclRef(Update.getDecl()));
4821 break;
4822
Richard Smith4d235792014-08-07 18:53:08 +00004823 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004824 break;
4825
Richard Smith4d235792014-08-07 18:53:08 +00004826 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004827 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004828 break;
4829
John McCall32791cc2016-01-06 22:34:54 +00004830 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004831 Record.AddStmt(const_cast<Expr *>(
4832 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004833 break;
4834
Richard Smith4b054b22016-08-24 21:25:37 +00004835 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4836 Record.AddStmt(
4837 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4838 break;
4839
Richard Smithcd45dbc2014-04-19 03:48:30 +00004840 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4841 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004842 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004843 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004844 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004845 *Context, const_cast<CXXRecordDecl *>(RD)));
4846
4847 // This state is sometimes updated by template instantiation, when we
4848 // switch from the specialization referring to the template declaration
4849 // to it referring to the template definition.
4850 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4851 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004852 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004853 } else {
4854 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4855 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004856 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004857
4858 // The instantiation might have been resolved to a partial
4859 // specialization. If so, record which one.
4860 auto From = Spec->getInstantiatedFrom();
4861 if (auto PartialSpec =
4862 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4863 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004864 Record.AddDeclRef(PartialSpec);
4865 Record.AddTemplateArgumentList(
4866 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004867 } else {
4868 Record.push_back(false);
4869 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004870 }
4871 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004872 Record.AddSourceLocation(RD->getLocation());
4873 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004874 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004875
4876 // Instantiation may change attributes; write them all out afresh.
4877 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004878 if (D->hasAttrs())
4879 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004880
4881 // FIXME: Ensure we don't get here for explicit instantiations.
4882 break;
4883 }
4884
Richard Smithf8134002015-03-10 01:41:22 +00004885 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004886 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004887 break;
4888
Richard Smith564417a2014-03-20 21:47:22 +00004889 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4890 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004891 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4892 Record);
4893 break;
4894
Richard Smith6ef42932014-03-20 21:02:00 +00004895 case UPD_CXX_DEDUCED_RETURN_TYPE:
4896 Record.push_back(GetOrCreateTypeID(Update.getType()));
4897 break;
4898
4899 case UPD_DECL_MARKED_USED:
4900 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004901
4902 case UPD_MANGLING_NUMBER:
4903 case UPD_STATIC_LOCAL_NUMBER:
4904 Record.push_back(Update.getNumber());
4905 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004906
Alexey Bataev97720002014-11-11 04:05:39 +00004907 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004908 Record.AddSourceRange(
4909 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004910 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004911
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004912 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4913 Record.AddSourceRange(
4914 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4915 break;
4916
Richard Smith65ebb4a2015-03-26 04:09:53 +00004917 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004918 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004919 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004920
4921 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004922 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004923 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004924 }
4925 }
4926
Richard Smithd28ac5b2014-03-22 23:33:22 +00004927 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004928 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004929 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004930 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004931 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004932 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004933 }
4934
Richard Smithcd45dbc2014-04-19 03:48:30 +00004935 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004936 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004937 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004938}
4939
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004940void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004941 uint32_t Raw = Loc.getRawEncoding();
4942 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004943}
4944
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004945void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004946 AddSourceLocation(Range.getBegin(), Record);
4947 AddSourceLocation(Range.getEnd(), Record);
4948}
4949
Richard Smithf144b542016-04-08 21:54:32 +00004950void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4951 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004952 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004953 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004954}
4955
Richard Smithf144b542016-04-08 21:54:32 +00004956void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4957 Record->push_back(Value.isUnsigned());
4958 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004959}
4960
Richard Smithf144b542016-04-08 21:54:32 +00004961void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4962 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00004963}
4964
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004965void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004966 Record.push_back(getIdentifierRef(II));
4967}
4968
Sebastian Redl539c5062010-08-18 23:57:32 +00004969IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004970 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004971 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004972
Sebastian Redl539c5062010-08-18 23:57:32 +00004973 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004974 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004975 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004976 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004977}
4978
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004979MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004980 // Don't emit builtin macros like __LINE__ to the AST file unless they
4981 // have been redefined by the header (in which case they are not
4982 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004983 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004984 return 0;
4985
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004986 MacroID &ID = MacroIDs[MI];
4987 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004988 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004989 MacroInfoToEmitData Info = { Name, MI, ID };
4990 MacroInfosToEmit.push_back(Info);
4991 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004992 return ID;
4993}
4994
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004995MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004996 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004997 return 0;
4998
4999 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5000 return MacroIDs[MI];
5001}
5002
5003uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005004 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005005}
5006
Richard Smithe64e7452016-04-18 21:54:58 +00005007void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5008 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005009}
5010
Sebastian Redl539c5062010-08-18 23:57:32 +00005011SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005012 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005013 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005014 }
5015
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005016 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005017 if (SID == 0 && Chain) {
5018 // This might trigger a ReadSelector callback, which will set the ID for
5019 // this selector.
5020 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005021 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005022 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005023 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005024 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005025 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005026 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005027 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005028}
5029
Richard Smithe64e7452016-04-18 21:54:58 +00005030void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5031 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005032}
5033
Richard Smith290d8012016-04-06 17:06:00 +00005034void ASTRecordWriter::AddTemplateArgumentLocInfo(
5035 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005036 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005037 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005038 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005039 break;
5040 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005041 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005042 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005043 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005044 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5045 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005046 break;
5047 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005048 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5049 AddSourceLocation(Arg.getTemplateNameLoc());
5050 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005051 break;
John McCall0ad16662009-10-29 08:12:44 +00005052 case TemplateArgument::Null:
5053 case TemplateArgument::Integral:
5054 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005055 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005056 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005057 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005058 break;
5059 }
5060}
5061
Richard Smith290d8012016-04-06 17:06:00 +00005062void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5063 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005064
5065 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5066 bool InfoHasSameExpr
5067 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005068 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005069 if (InfoHasSameExpr)
5070 return; // Avoid storing the same expr twice.
5071 }
Richard Smith290d8012016-04-06 17:06:00 +00005072 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005073}
5074
Richard Smith290d8012016-04-06 17:06:00 +00005075void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005076 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005077 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005078 return;
5079 }
5080
Richard Smith290d8012016-04-06 17:06:00 +00005081 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005082}
5083
Richard Smith290d8012016-04-06 17:06:00 +00005084void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5085 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005086
Richard Smith290d8012016-04-06 17:06:00 +00005087 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005088 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005089 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005090}
5091
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005092void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005093 Record.push_back(GetOrCreateTypeID(T));
5094}
5095
Richard Smith2095ffe2015-03-16 20:11:03 +00005096TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005097 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005098 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5099 if (T.isNull())
5100 return TypeIdx();
5101 assert(!T.getLocalFastQualifiers());
5102
5103 TypeIdx &Idx = TypeIdxs[T];
5104 if (Idx.getIndex() == 0) {
5105 if (DoneWritingDeclsAndTypes) {
5106 assert(0 && "New type seen after serializing all the types to emit!");
5107 return TypeIdx();
5108 }
5109
5110 // We haven't seen this type before. Assign it a new ID and put it
5111 // into the queue of types to emit.
5112 Idx = TypeIdx(NextTypeID++);
5113 DeclTypesToEmit.push(T);
5114 }
5115 return Idx;
5116 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005117}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005118
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005119TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005120 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005121 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5122 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005123 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005124 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005125
Richard Smith2095ffe2015-03-16 20:11:03 +00005126 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5127 assert(I != TypeIdxs.end() && "Type not emitted!");
5128 return I->second;
5129 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005130}
5131
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005132void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005133 Record.push_back(GetDeclRef(D));
5134}
5135
Sebastian Redl539c5062010-08-18 23:57:32 +00005136DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005137 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005138
5139 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005140 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005141 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005142
5143 // If D comes from an AST file, its declaration ID is already known and
5144 // fixed.
5145 if (D->isFromASTFile())
5146 return D->getGlobalID();
5147
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005148 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005149 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005150 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005151 if (DoneWritingDeclsAndTypes) {
5152 assert(0 && "New decl seen after serializing all the decls to emit!");
5153 return 0;
5154 }
5155
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005156 // We haven't seen this declaration before. Give it a new ID and
5157 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005158 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005159 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005160 }
5161
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005162 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005163}
5164
Sebastian Redl539c5062010-08-18 23:57:32 +00005165DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005166 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005167 return 0;
5168
Douglas Gregorb3163e52012-01-05 22:33:30 +00005169 // If D comes from an AST file, its declaration ID is already known and
5170 // fixed.
5171 if (D->isFromASTFile())
5172 return D->getGlobalID();
5173
Douglas Gregore84a9da2009-04-20 20:36:09 +00005174 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5175 return DeclIDs[D];
5176}
5177
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005178void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005179 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005180 assert(D);
5181
5182 SourceLocation Loc = D->getLocation();
5183 if (Loc.isInvalid())
5184 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005185
5186 // We only keep track of the file-level declarations of each file.
5187 if (!D->getLexicalDeclContext()->isFileContext())
5188 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005189 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5190 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005191 if (isa<ParmVarDecl>(D))
5192 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005193
5194 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005195 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005196 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005197 FileID FID;
5198 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005199 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005200 if (FID.isInvalid())
5201 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005202 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005203
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005204 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005205 if (!Info)
5206 Info = new DeclIDInFileInfo();
5207
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005208 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005209 LocDeclIDsTy &Decls = Info->DeclIDs;
5210
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005211 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005212 Decls.push_back(LocDecl);
5213 return;
5214 }
5215
Benjamin Kramer45025c02013-08-24 13:22:59 +00005216 LocDeclIDsTy::iterator I =
5217 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005218
5219 Decls.insert(I, LocDecl);
5220}
5221
Richard Smithe64e7452016-04-18 21:54:58 +00005222void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005223 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005224 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005225 switch (Name.getNameKind()) {
5226 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005227 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005228 break;
5229
5230 case DeclarationName::ObjCZeroArgSelector:
5231 case DeclarationName::ObjCOneArgSelector:
5232 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005233 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005234 break;
5235
5236 case DeclarationName::CXXConstructorName:
5237 case DeclarationName::CXXDestructorName:
5238 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005239 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005240 break;
5241
5242 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005243 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005244 break;
5245
Alexis Hunt3d221f22009-11-29 07:34:05 +00005246 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005247 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005248 break;
5249
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005250 case DeclarationName::CXXUsingDirective:
5251 // No extra data to emit
5252 break;
5253 }
5254}
Chris Lattnerca025db2010-05-07 21:43:38 +00005255
Richard Smithd08aeb62014-08-28 01:33:39 +00005256unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5257 assert(needsAnonymousDeclarationNumber(D) &&
5258 "expected an anonymous declaration");
5259
5260 // Number the anonymous declarations within this context, if we've not
5261 // already done so.
5262 auto It = AnonymousDeclarationNumbers.find(D);
5263 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005264 auto *DC = D->getLexicalDeclContext();
5265 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5266 AnonymousDeclarationNumbers[ND] = Number;
5267 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005268
5269 It = AnonymousDeclarationNumbers.find(D);
5270 assert(It != AnonymousDeclarationNumbers.end() &&
5271 "declaration not found within its lexical context");
5272 }
5273
5274 return It->second;
5275}
5276
Richard Smith290d8012016-04-06 17:06:00 +00005277void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5278 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005279 switch (Name.getNameKind()) {
5280 case DeclarationName::CXXConstructorName:
5281 case DeclarationName::CXXDestructorName:
5282 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005283 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005284 break;
5285
5286 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005287 AddSourceLocation(SourceLocation::getFromRawEncoding(
5288 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005289 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005290 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005291 break;
5292
5293 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005294 AddSourceLocation(SourceLocation::getFromRawEncoding(
5295 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005296 break;
5297
5298 case DeclarationName::Identifier:
5299 case DeclarationName::ObjCZeroArgSelector:
5300 case DeclarationName::ObjCOneArgSelector:
5301 case DeclarationName::ObjCMultiArgSelector:
5302 case DeclarationName::CXXUsingDirective:
5303 break;
5304 }
5305}
5306
Richard Smith290d8012016-04-06 17:06:00 +00005307void ASTRecordWriter::AddDeclarationNameInfo(
5308 const DeclarationNameInfo &NameInfo) {
5309 AddDeclarationName(NameInfo.getName());
5310 AddSourceLocation(NameInfo.getLoc());
5311 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005312}
5313
Richard Smith290d8012016-04-06 17:06:00 +00005314void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5315 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5316 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005317 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005318 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005319}
5320
Richard Smithe64e7452016-04-18 21:54:58 +00005321void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005322 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005323 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005324 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005325
5326 // Push each of the NNS's onto a stack for serialization in reverse order.
5327 while (NNS) {
5328 NestedNames.push_back(NNS);
5329 NNS = NNS->getPrefix();
5330 }
5331
Richard Smithe64e7452016-04-18 21:54:58 +00005332 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005333 while(!NestedNames.empty()) {
5334 NNS = NestedNames.pop_back_val();
5335 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005336 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005337 switch (Kind) {
5338 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005339 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005340 break;
5341
5342 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005343 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005344 break;
5345
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005346 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005347 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005348 break;
5349
Chris Lattnerca025db2010-05-07 21:43:38 +00005350 case NestedNameSpecifier::TypeSpec:
5351 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005352 AddTypeRef(QualType(NNS->getAsType(), 0));
5353 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005354 break;
5355
5356 case NestedNameSpecifier::Global:
5357 // Don't need to write an associated value.
5358 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005359
5360 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005361 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005362 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005363 }
5364 }
5365}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005366
Richard Smith290d8012016-04-06 17:06:00 +00005367void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005368 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005369 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005370 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005371
5372 // Push each of the nested-name-specifiers's onto a stack for
5373 // serialization in reverse order.
5374 while (NNS) {
5375 NestedNames.push_back(NNS);
5376 NNS = NNS.getPrefix();
5377 }
5378
Richard Smith290d8012016-04-06 17:06:00 +00005379 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005380 while(!NestedNames.empty()) {
5381 NNS = NestedNames.pop_back_val();
5382 NestedNameSpecifier::SpecifierKind Kind
5383 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005384 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005385 switch (Kind) {
5386 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005387 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5388 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005389 break;
5390
5391 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005392 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5393 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005394 break;
5395
5396 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005397 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5398 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005399 break;
5400
5401 case NestedNameSpecifier::TypeSpec:
5402 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005403 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5404 AddTypeLoc(NNS.getTypeLoc());
5405 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005406 break;
5407
5408 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005409 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005410 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005411
5412 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005413 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5414 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005415 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005416 }
5417 }
5418}
5419
Richard Smith290d8012016-04-06 17:06:00 +00005420void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005421 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005422 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005423 switch (Kind) {
5424 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005425 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005426 break;
5427
5428 case TemplateName::OverloadedTemplate: {
5429 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005430 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005431 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005432 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005433 break;
5434 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005435
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005436 case TemplateName::QualifiedTemplate: {
5437 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005438 AddNestedNameSpecifier(QualT->getQualifier());
5439 Record->push_back(QualT->hasTemplateKeyword());
5440 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005441 break;
5442 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005443
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005444 case TemplateName::DependentTemplate: {
5445 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005446 AddNestedNameSpecifier(DepT->getQualifier());
5447 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005448 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005449 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005450 else
Richard Smith290d8012016-04-06 17:06:00 +00005451 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005452 break;
5453 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005454
5455 case TemplateName::SubstTemplateTemplateParm: {
5456 SubstTemplateTemplateParmStorage *subst
5457 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005458 AddDeclRef(subst->getParameter());
5459 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005460 break;
5461 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005462
5463 case TemplateName::SubstTemplateTemplateParmPack: {
5464 SubstTemplateTemplateParmPackStorage *SubstPack
5465 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005466 AddDeclRef(SubstPack->getParameterPack());
5467 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005468 break;
5469 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005470 }
5471}
5472
Richard Smith290d8012016-04-06 17:06:00 +00005473void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5474 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005475 switch (Arg.getKind()) {
5476 case TemplateArgument::Null:
5477 break;
5478 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005479 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005480 break;
5481 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005482 AddDeclRef(Arg.getAsDecl());
5483 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005484 break;
5485 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005486 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005487 break;
5488 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005489 AddAPSInt(Arg.getAsIntegral());
5490 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005491 break;
5492 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005493 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005494 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005495 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005496 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005497 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005498 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005499 else
Richard Smith290d8012016-04-06 17:06:00 +00005500 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005501 break;
5502 case TemplateArgument::Expression:
5503 AddStmt(Arg.getAsExpr());
5504 break;
5505 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005506 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005507 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005508 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005509 break;
5510 }
5511}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005512
Richard Smithe64e7452016-04-18 21:54:58 +00005513void ASTRecordWriter::AddTemplateParameterList(
5514 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005515 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005516 AddSourceLocation(TemplateParams->getTemplateLoc());
5517 AddSourceLocation(TemplateParams->getLAngleLoc());
5518 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005519 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005520 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005521 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005522 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005523}
5524
5525/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005526void ASTRecordWriter::AddTemplateArgumentList(
5527 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005528 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005529 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005530 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005531 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005532}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005533
Richard Smith290d8012016-04-06 17:06:00 +00005534void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5535 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005536 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005537 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5538 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5539 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005540 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005541 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005542 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005543}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005544
Richard Smithe64e7452016-04-18 21:54:58 +00005545void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5546 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005547 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005548 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005549 AddDeclRef(I.getDecl());
5550 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005551 }
5552}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005553
Richard Smith290d8012016-04-06 17:06:00 +00005554// FIXME: Move this out of the main ASTRecordWriter interface.
5555void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5556 Record->push_back(Base.isVirtual());
5557 Record->push_back(Base.isBaseOfClass());
5558 Record->push_back(Base.getAccessSpecifierAsWritten());
5559 Record->push_back(Base.getInheritConstructors());
5560 AddTypeSourceInfo(Base.getTypeSourceInfo());
5561 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005562 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005563 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005564}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005565
Richard Smith645d2cf2016-04-14 00:29:55 +00005566static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5567 ArrayRef<CXXBaseSpecifier> Bases) {
5568 ASTWriter::RecordData Record;
5569 ASTRecordWriter Writer(W, Record);
5570 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005571
Richard Smith645d2cf2016-04-14 00:29:55 +00005572 for (auto &Base : Bases)
5573 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005574
Richard Smith645d2cf2016-04-14 00:29:55 +00005575 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005576}
5577
Richard Smith290d8012016-04-06 17:06:00 +00005578// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005579void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5580 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5581}
5582
Richard Smithaa165cf2016-04-13 21:57:08 +00005583static uint64_t
5584EmitCXXCtorInitializers(ASTWriter &W,
5585 ArrayRef<CXXCtorInitializer *> CtorInits) {
5586 ASTWriter::RecordData Record;
5587 ASTRecordWriter Writer(W, Record);
5588 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005589
Richard Smithaa165cf2016-04-13 21:57:08 +00005590 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005591 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005592 Writer.push_back(CTOR_INITIALIZER_BASE);
5593 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5594 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005595 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005596 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5597 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005598 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005599 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5600 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005601 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005602 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5603 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005604 }
Francois Pichetd583da02010-12-04 09:14:42 +00005605
Richard Smithaa165cf2016-04-13 21:57:08 +00005606 Writer.AddSourceLocation(Init->getMemberLocation());
5607 Writer.AddStmt(Init->getInit());
5608 Writer.AddSourceLocation(Init->getLParenLoc());
5609 Writer.AddSourceLocation(Init->getRParenLoc());
5610 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005611 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005612 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005613 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005614
5615 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005616}
5617
Richard Smithaa165cf2016-04-13 21:57:08 +00005618// FIXME: Move this out of the main ASTRecordWriter interface.
5619void ASTRecordWriter::AddCXXCtorInitializers(
5620 ArrayRef<CXXCtorInitializer *> CtorInits) {
5621 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005622}
5623
Richard Smith290d8012016-04-06 17:06:00 +00005624void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005625 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005626 Record->push_back(Data.IsLambda);
5627 Record->push_back(Data.UserDeclaredConstructor);
5628 Record->push_back(Data.UserDeclaredSpecialMembers);
5629 Record->push_back(Data.Aggregate);
5630 Record->push_back(Data.PlainOldData);
5631 Record->push_back(Data.Empty);
5632 Record->push_back(Data.Polymorphic);
5633 Record->push_back(Data.Abstract);
5634 Record->push_back(Data.IsStandardLayout);
5635 Record->push_back(Data.HasNoNonEmptyBases);
5636 Record->push_back(Data.HasPrivateFields);
5637 Record->push_back(Data.HasProtectedFields);
5638 Record->push_back(Data.HasPublicFields);
5639 Record->push_back(Data.HasMutableFields);
5640 Record->push_back(Data.HasVariantMembers);
5641 Record->push_back(Data.HasOnlyCMembers);
5642 Record->push_back(Data.HasInClassInitializer);
5643 Record->push_back(Data.HasUninitializedReferenceMember);
5644 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005645 Record->push_back(Data.HasInheritedConstructor);
5646 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005647 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5648 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5649 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5650 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5651 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5652 Record->push_back(Data.DefaultedDestructorIsDeleted);
5653 Record->push_back(Data.HasTrivialSpecialMembers);
5654 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5655 Record->push_back(Data.HasIrrelevantDestructor);
5656 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5657 Record->push_back(Data.HasDefaultedDefaultConstructor);
5658 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5659 Record->push_back(Data.HasConstexprDefaultConstructor);
5660 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5661 Record->push_back(Data.ComputedVisibleConversions);
5662 Record->push_back(Data.UserProvidedDefaultConstructor);
5663 Record->push_back(Data.DeclaredSpecialMembers);
5664 Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
5665 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5666 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5667 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005668 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005669
Richard Smith290d8012016-04-06 17:06:00 +00005670 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005671 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005672 AddCXXBaseSpecifiers(Data.bases());
5673
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005674 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005675 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005676 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005677 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005678
Richard Smith290d8012016-04-06 17:06:00 +00005679 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5680 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005681 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005682 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005683
5684 // Add lambda-specific data.
5685 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005686 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005687 Record->push_back(Lambda.Dependent);
5688 Record->push_back(Lambda.IsGenericLambda);
5689 Record->push_back(Lambda.CaptureDefault);
5690 Record->push_back(Lambda.NumCaptures);
5691 Record->push_back(Lambda.NumExplicitCaptures);
5692 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005693 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005694 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005695 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005696 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005697 AddSourceLocation(Capture.getLocation());
5698 Record->push_back(Capture.isImplicit());
5699 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005700 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005701 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005702 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005703 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005704 break;
5705 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005706 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005707 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005708 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005709 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005710 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005711 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005712 break;
5713 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005714 }
5715 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005716}
5717
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005718void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005719 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005720 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005721 assert(FirstDeclID == NextDeclID &&
5722 FirstTypeID == NextTypeID &&
5723 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005724 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005725 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005726 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005727 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005728
Sebastian Redl07a89a82010-07-30 00:29:29 +00005729 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005730
Richard Smith7f330cd2015-03-18 01:42:29 +00005731 // Note, this will get called multiple times, once one the reader starts up
5732 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005733 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5734 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5735 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005736 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005737 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005738 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005739 NextDeclID = FirstDeclID;
5740 NextTypeID = FirstTypeID;
5741 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005742 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005743 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005744 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005745}
5746
Sebastian Redl539c5062010-08-18 23:57:32 +00005747void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005748 // Always keep the highest ID. See \p TypeRead() for more information.
5749 IdentID &StoredID = IdentifierIDs[II];
5750 if (ID > StoredID)
5751 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005752}
5753
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005754void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005755 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005756 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005757 if (ID > StoredID)
5758 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005759}
5760
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005761void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005762 // Always take the highest-numbered type index. This copes with an interesting
5763 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005764 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005765 // keep the higher-numbered entry so that we can properly write it out to
5766 // the AST file.
5767 TypeIdx &StoredIdx = TypeIdxs[T];
5768 if (Idx.getIndex() >= StoredIdx.getIndex())
5769 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005770}
5771
Sebastian Redl539c5062010-08-18 23:57:32 +00005772void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005773 // Always keep the highest ID. See \p TypeRead() for more information.
5774 SelectorID &StoredID = SelectorIDs[S];
5775 if (ID > StoredID)
5776 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005777}
Douglas Gregor91096292010-10-02 19:29:26 +00005778
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005779void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005780 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005781 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005782 MacroDefinitions[MD] = ID;
5783}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005784
Douglas Gregore37a85a2011-12-02 17:30:13 +00005785void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5786 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5787 SubmoduleIDs[Mod] = ID;
5788}
5789
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005790void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005791 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005792 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005793 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005794 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005795 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005796 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005797 // A forward reference was mutated into a definition. Rewrite it.
5798 // FIXME: This happens during template instantiation, should we
5799 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005800 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5801 "completed a tag from another module but not by instantiation?");
5802 DeclUpdates[RD].push_back(
5803 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005804 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005805 }
5806}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005807
Richard Smithf983f7f2015-10-13 00:23:25 +00005808static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5809 if (D->isFromASTFile())
5810 return true;
5811
Richard Smithf983f7f2015-10-13 00:23:25 +00005812 // The predefined __va_list_tag struct is imported if we imported any decls.
5813 // FIXME: This is a gross hack.
5814 return D == D->getASTContext().getVaListTagDecl();
5815}
5816
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005817void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005818 if (Chain && Chain->isProcessingUpdateRecords()) return;
5819 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005820 "Should not add lookup results to non-lookup contexts!");
5821
Vassil Vassilev632eac32016-03-16 11:17:04 +00005822 // TU is handled elsewhere.
5823 if (isa<TranslationUnitDecl>(DC))
5824 return;
5825
5826 // Namespaces are handled elsewhere, except for template instantiations of
5827 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5828 // local instantiations are added to an imported context. Only happens when
5829 // adding ADL lookup candidates, for example templated friends.
5830 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5831 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005832 return;
5833
Richard Smithf983f7f2015-10-13 00:23:25 +00005834 // We're only interested in cases where a local declaration is added to an
5835 // imported context.
5836 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5837 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005838
Richard Smith0f4e2c42015-08-06 04:23:48 +00005839 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005840 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005841 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005842 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5843 // We're adding a visible declaration to a predefined decl context. Ensure
5844 // that we write out all of its lookup results so we don't get a nasty
5845 // surprise when we try to emit its lookup table.
5846 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005847 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005848 }
Richard Smithc26d9742016-10-06 20:30:51 +00005849 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005850}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005851
5852void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005853 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005854 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005855
5856 // We're only interested in cases where a local declaration is added to an
5857 // imported context.
5858 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5859 return;
5860
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005861 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005862 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005863
5864 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005865 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005866 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005867 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005868}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005869
Richard Smith564417a2014-03-20 21:47:22 +00005870void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005871 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005872 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5873 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005874 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005875 // If we don't already know the exception specification for this redecl
5876 // chain, add an update record for it.
5877 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5878 ->getType()
5879 ->castAs<FunctionProtoType>()
5880 ->getExceptionSpecType()))
5881 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5882 });
Richard Smith564417a2014-03-20 21:47:22 +00005883}
5884
Richard Smith1fa5d642013-05-11 05:45:24 +00005885void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005886 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005887 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005888 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005889 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005890 DeclUpdates[D].push_back(
5891 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5892 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005893}
5894
Richard Smithf8134002015-03-10 01:41:22 +00005895void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5896 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005897 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005898 assert(!WritingAST && "Already writing the AST!");
5899 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005900 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005901 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005902 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5903 });
Richard Smithf8134002015-03-10 01:41:22 +00005904}
5905
Sebastian Redlab238a72011-04-24 16:28:06 +00005906void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005907 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005908 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005909 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005910 return; // Declaration not imported from PCH.
5911
Richard Smith4d235792014-08-07 18:53:08 +00005912 // Implicit function decl from a PCH was defined.
5913 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005914}
5915
Richard Smithd28ac5b2014-03-22 23:33:22 +00005916void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005917 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005918 assert(!WritingAST && "Already writing the AST!");
5919 if (!D->isFromASTFile())
5920 return;
5921
Richard Smith9e2341d2015-03-23 03:25:59 +00005922 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005923}
5924
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005925void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005926 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005927 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005928 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005929 return;
5930
5931 // Since the actual instantiation is delayed, this really means that we need
5932 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005933 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005934 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5935 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005936}
5937
John McCall32791cc2016-01-06 22:34:54 +00005938void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005939 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005940 assert(!WritingAST && "Already writing the AST!");
5941 if (!D->isFromASTFile())
5942 return;
5943
5944 DeclUpdates[D].push_back(
5945 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5946}
5947
Richard Smith4b054b22016-08-24 21:25:37 +00005948void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5949 assert(!WritingAST && "Already writing the AST!");
5950 if (!D->isFromASTFile())
5951 return;
5952
5953 DeclUpdates[D].push_back(
5954 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5955}
5956
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005957void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5958 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005959 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005960 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005961 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005962 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005963
5964 assert(IFD->getDefinition() && "Category on a class without a definition?");
5965 ObjCClassesWithCategories.insert(
5966 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005967}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005968
Eli Friedman276dd182013-09-05 00:02:25 +00005969void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005970 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005971 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005972
5973 // If there is *any* declaration of the entity that's not from an AST file,
5974 // we can skip writing the update record. We make sure that isUsed() triggers
5975 // completion of the redeclaration chain of the entity.
5976 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5977 if (IsLocalDecl(Prev))
5978 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005979
Aaron Ballman4f45b712014-03-21 15:22:56 +00005980 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005981}
Alexey Bataev97720002014-11-11 04:05:39 +00005982
5983void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005984 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005985 assert(!WritingAST && "Already writing the AST!");
5986 if (!D->isFromASTFile())
5987 return;
5988
5989 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5990}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005991
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005992void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5993 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005994 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005995 assert(!WritingAST && "Already writing the AST!");
5996 if (!D->isFromASTFile())
5997 return;
5998
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005999 DeclUpdates[D].push_back(
6000 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006001}
6002
Richard Smith4caa4492015-05-15 02:34:32 +00006003void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006004 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006005 assert(!WritingAST && "Already writing the AST!");
6006 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006007 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006008}
Alex Denisovfde64952015-06-26 05:28:36 +00006009
6010void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6011 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006012 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006013 assert(!WritingAST && "Already writing the AST!");
6014 if (!Record->isFromASTFile())
6015 return;
6016 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6017}
Richard Smithc26d9742016-10-06 20:30:51 +00006018
6019void ASTWriter::AddedCXXTemplateSpecialization(
6020 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6021 assert(!WritingAST && "Already writing the AST!");
6022
6023 if (!TD->getFirstDecl()->isFromASTFile())
6024 return;
6025 if (Chain && Chain->isProcessingUpdateRecords())
6026 return;
6027
6028 DeclsToEmitEvenIfUnreferenced.push_back(D);
6029}
6030
6031void ASTWriter::AddedCXXTemplateSpecialization(
6032 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6033 assert(!WritingAST && "Already writing the AST!");
6034
6035 if (!TD->getFirstDecl()->isFromASTFile())
6036 return;
6037 if (Chain && Chain->isProcessingUpdateRecords())
6038 return;
6039
6040 DeclsToEmitEvenIfUnreferenced.push_back(D);
6041}
6042
6043void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6044 const FunctionDecl *D) {
6045 assert(!WritingAST && "Already writing the AST!");
6046
6047 if (!TD->getFirstDecl()->isFromASTFile())
6048 return;
6049 if (Chain && Chain->isProcessingUpdateRecords())
6050 return;
6051
6052 DeclsToEmitEvenIfUnreferenced.push_back(D);
6053}