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