blob: be93ca6f4d228f1eba25fe46320562489147e56b [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
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000484void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000485 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000486 Record.push_back(T->getTypeArgsAsWritten().size());
487 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000488 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000489 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000490 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000491 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000492 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000493 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000494}
495
Steve Narofffb4330f2009-06-17 22:40:22 +0000496void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000497ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000498 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000499 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000500}
501
Eli Friedman0dfb8892011-10-06 23:00:33 +0000502void
503ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000504 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000505 Code = TYPE_ATOMIC;
506}
507
Xiuli Pan9c14e282016-01-09 12:53:17 +0000508void
509ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000510 Record.AddTypeRef(T->getElementType());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000511 Code = TYPE_PIPE;
512}
513
John McCall8f115c62009-10-16 21:56:05 +0000514namespace {
515
516class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000517 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000518
519public:
Richard Smith290d8012016-04-06 17:06:00 +0000520 TypeLocWriter(ASTRecordWriter &Record)
521 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000522
John McCall17001972009-10-18 01:05:36 +0000523#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000524#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000525 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000526#include "clang/AST/TypeLocNodes.def"
527
John McCall17001972009-10-18 01:05:36 +0000528 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
529 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000530};
531
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000532} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000533
John McCall17001972009-10-18 01:05:36 +0000534void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
535 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000536}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000537
John McCall17001972009-10-18 01:05:36 +0000538void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000539 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000540 if (TL.needsExtraLocalData()) {
541 Record.push_back(TL.getWrittenTypeSpec());
542 Record.push_back(TL.getWrittenSignSpec());
543 Record.push_back(TL.getWrittenWidthSpec());
544 Record.push_back(TL.hasModeAttr());
545 }
John McCall8f115c62009-10-16 21:56:05 +0000546}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000547
John McCall17001972009-10-18 01:05:36 +0000548void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000549 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000550}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000551
John McCall17001972009-10-18 01:05:36 +0000552void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000553 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000554}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000555
Reid Kleckner8a365022013-06-24 17:51:48 +0000556void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
557 // nothing to do
558}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000559
Reid Kleckner0503a872013-12-05 01:23:43 +0000560void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
561 // nothing to do
562}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000563
John McCall17001972009-10-18 01:05:36 +0000564void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000565 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000566}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000567
John McCall17001972009-10-18 01:05:36 +0000568void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000569 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000570}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000571
John McCall17001972009-10-18 01:05:36 +0000572void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000573 Record.AddSourceLocation(TL.getAmpAmpLoc());
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::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000577 Record.AddSourceLocation(TL.getStarLoc());
578 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000579}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000580
John McCall17001972009-10-18 01:05:36 +0000581void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000582 Record.AddSourceLocation(TL.getLBracketLoc());
583 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000584 Record.push_back(TL.getSizeExpr() ? 1 : 0);
585 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000586 Record.AddStmt(TL.getSizeExpr());
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::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
590 VisitArrayTypeLoc(TL);
591}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000592
John McCall17001972009-10-18 01:05:36 +0000593void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
594 VisitArrayTypeLoc(TL);
595}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000596
John McCall17001972009-10-18 01:05:36 +0000597void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
598 VisitArrayTypeLoc(TL);
599}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000600
John McCall17001972009-10-18 01:05:36 +0000601void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
602 DependentSizedArrayTypeLoc TL) {
603 VisitArrayTypeLoc(TL);
604}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000605
John McCall17001972009-10-18 01:05:36 +0000606void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
607 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000608 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000609}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000610
John McCall17001972009-10-18 01:05:36 +0000611void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000612 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000613}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000614
John McCall17001972009-10-18 01:05:36 +0000615void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc 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::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000620 Record.AddSourceLocation(TL.getLocalRangeBegin());
621 Record.AddSourceLocation(TL.getLParenLoc());
622 Record.AddSourceLocation(TL.getRParenLoc());
623 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000624 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000625 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000626}
627void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
628 VisitFunctionTypeLoc(TL);
629}
630void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
631 VisitFunctionTypeLoc(TL);
632}
John McCallb96ec562009-12-04 22:46:56 +0000633void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000634 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000635}
John McCall17001972009-10-18 01:05:36 +0000636void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000637 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000638}
639void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000640 Record.AddSourceLocation(TL.getTypeofLoc());
641 Record.AddSourceLocation(TL.getLParenLoc());
642 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000643}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000644
John McCall17001972009-10-18 01:05:36 +0000645void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000646 Record.AddSourceLocation(TL.getTypeofLoc());
647 Record.AddSourceLocation(TL.getLParenLoc());
648 Record.AddSourceLocation(TL.getRParenLoc());
649 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000650}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000651
John McCall17001972009-10-18 01:05:36 +0000652void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000653 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000654}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000655
Alexis Hunte852b102011-05-24 22:41:36 +0000656void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000657 Record.AddSourceLocation(TL.getKWLoc());
658 Record.AddSourceLocation(TL.getLParenLoc());
659 Record.AddSourceLocation(TL.getRParenLoc());
660 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000661}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000662
Richard Smith30482bc2011-02-20 03:19:35 +0000663void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000664 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000665}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000666
John McCall17001972009-10-18 01:05:36 +0000667void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000668 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000669}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000670
John McCall17001972009-10-18 01:05:36 +0000671void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000672 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000673}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000674
John McCall81904512011-01-06 01:58:22 +0000675void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000676 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000677 if (TL.hasAttrOperand()) {
678 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000679 Record.AddSourceLocation(range.getBegin());
680 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000681 }
682 if (TL.hasAttrExprOperand()) {
683 Expr *operand = TL.getAttrExprOperand();
684 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000685 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000686 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000687 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000688 }
689}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000690
John McCall17001972009-10-18 01:05:36 +0000691void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000692 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000693}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000694
John McCallcebee162009-10-18 09:09:24 +0000695void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
696 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000697 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000698}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000699
Douglas Gregorada4b792011-01-14 02:55:32 +0000700void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
701 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000702 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000703}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000704
John McCall17001972009-10-18 01:05:36 +0000705void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
706 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000707 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
708 Record.AddSourceLocation(TL.getTemplateNameLoc());
709 Record.AddSourceLocation(TL.getLAngleLoc());
710 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000711 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000712 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
713 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000714}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000715
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000716void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000717 Record.AddSourceLocation(TL.getLParenLoc());
718 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000719}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000720
Abramo Bagnara6150c882010-05-11 21:36:43 +0000721void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000722 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
723 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000724}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000725
John McCalle78aac42010-03-10 03:28:59 +0000726void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000727 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000728}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000729
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000730void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000731 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
732 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
733 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000734}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000735
John McCallc392f372010-06-11 00:33:02 +0000736void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
737 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000738 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
739 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
740 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
741 Record.AddSourceLocation(TL.getTemplateNameLoc());
742 Record.AddSourceLocation(TL.getLAngleLoc());
743 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000744 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000745 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
746 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000747}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000748
Douglas Gregord2fa7662010-12-20 02:24:11 +0000749void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000750 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000751}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000752
John McCall17001972009-10-18 01:05:36 +0000753void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000754 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000755}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000756
John McCall8b07ec22010-05-15 11:32:37 +0000757void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
758 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000759 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
760 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000761 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000762 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
763 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
764 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000765 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000766 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000767}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000768
John McCallfc93cf92009-10-22 22:37:11 +0000769void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000770 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000771}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000772
Eli Friedman0dfb8892011-10-06 23:00:33 +0000773void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000774 Record.AddSourceLocation(TL.getKWLoc());
775 Record.AddSourceLocation(TL.getLParenLoc());
776 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000777}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000778
Xiuli Pan9c14e282016-01-09 12:53:17 +0000779void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000780 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000781}
John McCall8f115c62009-10-16 21:56:05 +0000782
Richard Smith01b2cb42014-07-26 06:37:51 +0000783void ASTWriter::WriteTypeAbbrevs() {
784 using namespace llvm;
785
786 BitCodeAbbrev *Abv;
787
788 // Abbreviation for TYPE_EXT_QUAL
789 Abv = new BitCodeAbbrev();
790 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
791 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
792 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
793 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
794
795 // Abbreviation for TYPE_FUNCTION_PROTO
796 Abv = new BitCodeAbbrev();
797 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
798 // FunctionType
799 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
800 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
801 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
802 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
803 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
804 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
805 // FunctionProtoType
806 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
807 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
808 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
809 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
810 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
811 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
812 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
813 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
814 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
815}
816
Chris Lattner19cea4e2009-04-22 05:57:30 +0000817//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000818// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000819//===----------------------------------------------------------------------===//
820
Chris Lattner28fa4e62009-04-26 22:26:21 +0000821static void EmitBlockID(unsigned ID, const char *Name,
822 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000823 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000824 Record.clear();
825 Record.push_back(ID);
826 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
827
828 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000829 if (!Name || Name[0] == 0)
830 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000831 Record.clear();
832 while (*Name)
833 Record.push_back(*Name++);
834 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
835}
836
837static void EmitRecordID(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 while (*Name)
843 Record.push_back(*Name++);
844 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000845}
846
847static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000848 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000849#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000850 RECORD(STMT_STOP);
851 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000852 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000853 RECORD(STMT_NULL);
854 RECORD(STMT_COMPOUND);
855 RECORD(STMT_CASE);
856 RECORD(STMT_DEFAULT);
857 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000858 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000859 RECORD(STMT_IF);
860 RECORD(STMT_SWITCH);
861 RECORD(STMT_WHILE);
862 RECORD(STMT_DO);
863 RECORD(STMT_FOR);
864 RECORD(STMT_GOTO);
865 RECORD(STMT_INDIRECT_GOTO);
866 RECORD(STMT_CONTINUE);
867 RECORD(STMT_BREAK);
868 RECORD(STMT_RETURN);
869 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000870 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000871 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000872 RECORD(EXPR_PREDEFINED);
873 RECORD(EXPR_DECL_REF);
874 RECORD(EXPR_INTEGER_LITERAL);
875 RECORD(EXPR_FLOATING_LITERAL);
876 RECORD(EXPR_IMAGINARY_LITERAL);
877 RECORD(EXPR_STRING_LITERAL);
878 RECORD(EXPR_CHARACTER_LITERAL);
879 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000880 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000881 RECORD(EXPR_UNARY_OPERATOR);
882 RECORD(EXPR_SIZEOF_ALIGN_OF);
883 RECORD(EXPR_ARRAY_SUBSCRIPT);
884 RECORD(EXPR_CALL);
885 RECORD(EXPR_MEMBER);
886 RECORD(EXPR_BINARY_OPERATOR);
887 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
888 RECORD(EXPR_CONDITIONAL_OPERATOR);
889 RECORD(EXPR_IMPLICIT_CAST);
890 RECORD(EXPR_CSTYLE_CAST);
891 RECORD(EXPR_COMPOUND_LITERAL);
892 RECORD(EXPR_EXT_VECTOR_ELEMENT);
893 RECORD(EXPR_INIT_LIST);
894 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000895 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000896 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000897 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000898 RECORD(EXPR_VA_ARG);
899 RECORD(EXPR_ADDR_LABEL);
900 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000901 RECORD(EXPR_CHOOSE);
902 RECORD(EXPR_GNU_NULL);
903 RECORD(EXPR_SHUFFLE_VECTOR);
904 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000905 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000906 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000907 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000908 RECORD(EXPR_OBJC_ARRAY_LITERAL);
909 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000910 RECORD(EXPR_OBJC_ENCODE);
911 RECORD(EXPR_OBJC_SELECTOR_EXPR);
912 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
913 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
914 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
915 RECORD(EXPR_OBJC_KVC_REF_EXPR);
916 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000917 RECORD(STMT_OBJC_FOR_COLLECTION);
918 RECORD(STMT_OBJC_CATCH);
919 RECORD(STMT_OBJC_FINALLY);
920 RECORD(STMT_OBJC_AT_TRY);
921 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
922 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000923 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000924 RECORD(STMT_CXX_CATCH);
925 RECORD(STMT_CXX_TRY);
926 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000927 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000928 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000929 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000930 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000931 RECORD(EXPR_CXX_STATIC_CAST);
932 RECORD(EXPR_CXX_DYNAMIC_CAST);
933 RECORD(EXPR_CXX_REINTERPRET_CAST);
934 RECORD(EXPR_CXX_CONST_CAST);
935 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000936 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000937 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000938 RECORD(EXPR_CXX_BOOL_LITERAL);
939 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000940 RECORD(EXPR_CXX_TYPEID_EXPR);
941 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000942 RECORD(EXPR_CXX_THIS);
943 RECORD(EXPR_CXX_THROW);
944 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000945 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000946 RECORD(EXPR_CXX_BIND_TEMPORARY);
947 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
948 RECORD(EXPR_CXX_NEW);
949 RECORD(EXPR_CXX_DELETE);
950 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
951 RECORD(EXPR_EXPR_WITH_CLEANUPS);
952 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
953 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
954 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
955 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
956 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000957 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000958 RECORD(EXPR_CXX_NOEXCEPT);
959 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000960 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
961 RECORD(EXPR_TYPE_TRAIT);
962 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000963 RECORD(EXPR_PACK_EXPANSION);
964 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000965 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000966 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000967 RECORD(EXPR_FUNCTION_PARM_PACK);
968 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000969 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000970 RECORD(EXPR_CXX_UUIDOF_EXPR);
971 RECORD(EXPR_CXX_UUIDOF_TYPE);
972 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000973#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000974}
Mike Stump11289f42009-09-09 15:08:12 +0000975
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000976void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000977 RecordData Record;
978 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000979
Sebastian Redl539c5062010-08-18 23:57:32 +0000980#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
981#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000982
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000983 // Control Block.
984 BLOCK(CONTROL_BLOCK);
985 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000986 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000987 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +0000988 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000989 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000990 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000991 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000992 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000993 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000994 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +0000995
996 BLOCK(OPTIONS_BLOCK);
997 RECORD(LANGUAGE_OPTIONS);
998 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000999 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001000 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001001 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001002 RECORD(PREPROCESSOR_OPTIONS);
1003
Douglas Gregor108cb222012-10-19 00:45:00 +00001004 BLOCK(INPUT_FILES_BLOCK);
1005 RECORD(INPUT_FILE);
1006
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001007 // AST Top-Level Block.
1008 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001009 RECORD(TYPE_OFFSET);
1010 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001011 RECORD(IDENTIFIER_OFFSET);
1012 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001013 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001014 RECORD(SPECIAL_TYPES);
1015 RECORD(STATISTICS);
1016 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001017 RECORD(SELECTOR_OFFSETS);
1018 RECORD(METHOD_POOL);
1019 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001020 RECORD(SOURCE_LOCATION_OFFSETS);
1021 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001022 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001023 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001024 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001025 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001026 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001027 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001028 RECORD(SEMA_DECL_REFS);
1029 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1030 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001031 RECORD(UPDATE_VISIBLE);
1032 RECORD(DECL_UPDATE_OFFSETS);
1033 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001034 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001035 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001036 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001037 RECORD(FP_PRAGMA_OPTIONS);
1038 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001039 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001040 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001041 RECORD(MODULE_OFFSET_MAP);
1042 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001043 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001044 RECORD(FILE_SORTED_DECLS);
1045 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001046 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001047 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001048 RECORD(INTERESTING_IDENTIFIERS);
1049 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001050 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001051 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001052 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001053 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001054 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001055 RECORD(DELETE_EXPRS_TO_ANALYZE);
Douglas Gregor358cd442012-01-15 16:58:34 +00001056
Chris Lattner28fa4e62009-04-26 22:26:21 +00001057 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001058 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001059 RECORD(SM_SLOC_FILE_ENTRY);
1060 RECORD(SM_SLOC_BUFFER_ENTRY);
1061 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001062 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001063 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattner28fa4e62009-04-26 22:26:21 +00001065 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001066 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001067 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001068 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001069 RECORD(PP_MACRO_OBJECT_LIKE);
1070 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001071 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001072
Richard Smithfa715652015-08-31 22:43:10 +00001073 // Submodule Block.
1074 BLOCK(SUBMODULE_BLOCK);
1075 RECORD(SUBMODULE_METADATA);
1076 RECORD(SUBMODULE_DEFINITION);
1077 RECORD(SUBMODULE_UMBRELLA_HEADER);
1078 RECORD(SUBMODULE_HEADER);
1079 RECORD(SUBMODULE_TOPHEADER);
1080 RECORD(SUBMODULE_UMBRELLA_DIR);
1081 RECORD(SUBMODULE_IMPORTS);
1082 RECORD(SUBMODULE_EXPORTS);
1083 RECORD(SUBMODULE_REQUIRES);
1084 RECORD(SUBMODULE_EXCLUDED_HEADER);
1085 RECORD(SUBMODULE_LINK_LIBRARY);
1086 RECORD(SUBMODULE_CONFIG_MACRO);
1087 RECORD(SUBMODULE_CONFLICT);
1088 RECORD(SUBMODULE_PRIVATE_HEADER);
1089 RECORD(SUBMODULE_TEXTUAL_HEADER);
1090 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001091 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001092
1093 // Comments Block.
1094 BLOCK(COMMENTS_BLOCK);
1095 RECORD(COMMENTS_RAW_COMMENT);
1096
Douglas Gregor12bfa382009-10-17 00:13:19 +00001097 // Decls and Types block.
1098 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001099 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001100 RECORD(TYPE_COMPLEX);
1101 RECORD(TYPE_POINTER);
1102 RECORD(TYPE_BLOCK_POINTER);
1103 RECORD(TYPE_LVALUE_REFERENCE);
1104 RECORD(TYPE_RVALUE_REFERENCE);
1105 RECORD(TYPE_MEMBER_POINTER);
1106 RECORD(TYPE_CONSTANT_ARRAY);
1107 RECORD(TYPE_INCOMPLETE_ARRAY);
1108 RECORD(TYPE_VARIABLE_ARRAY);
1109 RECORD(TYPE_VECTOR);
1110 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001111 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001112 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001113 RECORD(TYPE_TYPEDEF);
1114 RECORD(TYPE_TYPEOF_EXPR);
1115 RECORD(TYPE_TYPEOF);
1116 RECORD(TYPE_RECORD);
1117 RECORD(TYPE_ENUM);
1118 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001119 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001120 RECORD(TYPE_DECLTYPE);
1121 RECORD(TYPE_ELABORATED);
1122 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1123 RECORD(TYPE_UNRESOLVED_USING);
1124 RECORD(TYPE_INJECTED_CLASS_NAME);
1125 RECORD(TYPE_OBJC_OBJECT);
1126 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1127 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1128 RECORD(TYPE_DEPENDENT_NAME);
1129 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1130 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1131 RECORD(TYPE_PAREN);
1132 RECORD(TYPE_PACK_EXPANSION);
1133 RECORD(TYPE_ATTRIBUTED);
1134 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001135 RECORD(TYPE_AUTO);
1136 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001137 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001138 RECORD(TYPE_DECAYED);
1139 RECORD(TYPE_ADJUSTED);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001140 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001141 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001142 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001143 RECORD(DECL_ENUM);
1144 RECORD(DECL_RECORD);
1145 RECORD(DECL_ENUM_CONSTANT);
1146 RECORD(DECL_FUNCTION);
1147 RECORD(DECL_OBJC_METHOD);
1148 RECORD(DECL_OBJC_INTERFACE);
1149 RECORD(DECL_OBJC_PROTOCOL);
1150 RECORD(DECL_OBJC_IVAR);
1151 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001152 RECORD(DECL_OBJC_CATEGORY);
1153 RECORD(DECL_OBJC_CATEGORY_IMPL);
1154 RECORD(DECL_OBJC_IMPLEMENTATION);
1155 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1156 RECORD(DECL_OBJC_PROPERTY);
1157 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001158 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001159 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001160 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001161 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001162 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001163 RECORD(DECL_FILE_SCOPE_ASM);
1164 RECORD(DECL_BLOCK);
1165 RECORD(DECL_CONTEXT_LEXICAL);
1166 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001167 RECORD(DECL_NAMESPACE);
1168 RECORD(DECL_NAMESPACE_ALIAS);
1169 RECORD(DECL_USING);
1170 RECORD(DECL_USING_SHADOW);
1171 RECORD(DECL_USING_DIRECTIVE);
1172 RECORD(DECL_UNRESOLVED_USING_VALUE);
1173 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1174 RECORD(DECL_LINKAGE_SPEC);
1175 RECORD(DECL_CXX_RECORD);
1176 RECORD(DECL_CXX_METHOD);
1177 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001178 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001179 RECORD(DECL_CXX_DESTRUCTOR);
1180 RECORD(DECL_CXX_CONVERSION);
1181 RECORD(DECL_ACCESS_SPEC);
1182 RECORD(DECL_FRIEND);
1183 RECORD(DECL_FRIEND_TEMPLATE);
1184 RECORD(DECL_CLASS_TEMPLATE);
1185 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1186 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001187 RECORD(DECL_VAR_TEMPLATE);
1188 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1189 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001190 RECORD(DECL_FUNCTION_TEMPLATE);
1191 RECORD(DECL_TEMPLATE_TYPE_PARM);
1192 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1193 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001194 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001195 RECORD(DECL_STATIC_ASSERT);
1196 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001197 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001198 RECORD(DECL_INDIRECTFIELD);
1199 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001200 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1201 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1202 RECORD(DECL_IMPORT);
1203 RECORD(DECL_OMP_THREADPRIVATE);
1204 RECORD(DECL_EMPTY);
1205 RECORD(DECL_OBJC_TYPE_PARAM);
1206 RECORD(DECL_OMP_CAPTUREDEXPR);
1207 RECORD(DECL_PRAGMA_COMMENT);
1208 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1209 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001210
Douglas Gregor03412ba2011-06-03 02:27:19 +00001211 // Statements and Exprs can occur in the Decls and Types block.
1212 AddStmtsExprs(Stream, Record);
1213
Douglas Gregor92a96f52011-02-08 21:58:10 +00001214 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001215 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001216 RECORD(PPD_MACRO_DEFINITION);
1217 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001218
1219 // Decls and Types block.
1220 BLOCK(EXTENSION_BLOCK);
1221 RECORD(EXTENSION_METADATA);
1222
Chris Lattner28fa4e62009-04-26 22:26:21 +00001223#undef RECORD
1224#undef BLOCK
1225 Stream.ExitBlock();
1226}
1227
Richard Smith54cc3c22014-12-11 20:50:24 +00001228/// \brief Prepares a path for being written to an AST file by converting it
1229/// to an absolute path and removing nested './'s.
1230///
1231/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001232static bool cleanPathForOutput(FileManager &FileMgr,
1233 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001234 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001235 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001236}
1237
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001238/// \brief Adjusts the given filename to only write out the portion of the
1239/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001240///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001241/// \param Filename the file name to adjust.
1242///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001243/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1244/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001245///
1246/// \returns either the original filename (if it needs no adjustment) or the
1247/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001248static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001249adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001250 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001251
Richard Smith7ed1bc92014-12-05 22:42:13 +00001252 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001253 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001254
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001255 // Verify that the filename and the system root have the same prefix.
1256 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001257 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1258 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001259 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001260
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001261 // We hit the end of the filename before we hit the end of the system root.
1262 if (!Filename[Pos])
1263 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001264
Richard Smith7ed1bc92014-12-05 22:42:13 +00001265 // If there's not a path separator at the end of the base directory nor
1266 // immediately after it, then this isn't within the base directory.
1267 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1268 if (!llvm::sys::path::is_separator(BaseDir.back()))
1269 return Filename;
1270 } else {
1271 // If the file name has a '/' at the current position, skip over the '/'.
1272 // We distinguish relative paths from absolute paths by the
1273 // absence of '/' at the beginning of relative paths.
1274 //
1275 // FIXME: This is wrong. We distinguish them by asking if the path is
1276 // absolute, which isn't the same thing. And there might be multiple '/'s
1277 // in a row. Use a better mechanism to indicate whether we have emitted an
1278 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001279 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001282 return Filename + Pos;
1283}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001284
Ben Langmuir487ea142014-10-23 18:05:36 +00001285static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001286 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001287 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1288 return S;
1289 // Rely on GetRandomNumber to eventually return non-zero...
1290 }
1291}
1292
Douglas Gregor112b9072012-10-18 05:31:06 +00001293/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001294uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1295 ASTContext &Context,
1296 StringRef isysroot,
1297 const std::string &OutputFile) {
1298 ASTFileSignature Signature = 0;
1299
Douglas Gregorbfbde532009-04-10 21:16:55 +00001300 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001301 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001302 RecordData Record;
1303
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001304 // Metadata
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001305 auto *MetadataAbbrev = new BitCodeAbbrev();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001306 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1307 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1308 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1309 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1310 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1311 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001312 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001313 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1314 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1315 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001316 assert((!WritingModule || isysroot.empty()) &&
1317 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001318 {
1319 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1320 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1321 !isysroot.empty(), IncludeTimestamps,
1322 ASTHasCompilerErrors};
1323 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1324 getClangFullRepositoryVersion());
1325 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001326 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001327 // For implicit modules we output a signature that we can use to ensure
1328 // duplicate module builds don't collide in the cache as their output order
1329 // is non-deterministic.
1330 // FIXME: Remove this when output is deterministic.
1331 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001332 Signature = getSignature();
1333 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001334 Stream.EmitRecord(SIGNATURE, Record);
1335 }
1336
Richard Smith7ed1bc92014-12-05 22:42:13 +00001337 // Module name
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001338 auto *Abbrev = new BitCodeAbbrev();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001339 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1341 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00001342 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001343 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1344 }
1345
Richard Smith7ed1bc92014-12-05 22:42:13 +00001346 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001347 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001348 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001349
1350 // If the home of the module is the current working directory, then we
1351 // want to pick up the cwd of the build process loading the module, not
1352 // our cwd, when we load this module.
1353 if (!PP.getHeaderSearchInfo()
1354 .getHeaderSearchOpts()
1355 .ModuleMapFileHomeIsCwd ||
1356 WritingModule->Directory->getName() != StringRef(".")) {
1357 // Module directory.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001358 auto *Abbrev = new BitCodeAbbrev();
Richard Smithf7b41372015-08-13 23:47:44 +00001359 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1361 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1362
Mehdi Amini57a41912015-09-10 01:46:39 +00001363 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001364 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1365 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001366
1367 // Write out all other paths relative to the base directory if possible.
1368 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1369 } else if (!isysroot.empty()) {
1370 // Write out paths relative to the sysroot if possible.
1371 BaseDirectory = isysroot;
1372 }
1373
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001374 // Module map file
1375 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001376 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001377
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001378 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1379
1380 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001381 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001382
1383 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001384 if (auto *AdditionalModMaps =
1385 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001386 Record.push_back(AdditionalModMaps->size());
1387 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001388 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001389 } else {
1390 Record.push_back(0);
1391 }
1392
1393 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001394 }
1395
Douglas Gregor112b9072012-10-18 05:31:06 +00001396 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001397 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001398 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001399 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001400
Richard Smith7f330cd2015-03-18 01:42:29 +00001401 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001402 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001403 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001404 continue;
1405
Richard Smith7f330cd2015-03-18 01:42:29 +00001406 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1407 AddSourceLocation(M->ImportLoc, Record);
1408 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001409 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001410 Record.push_back(M->Signature);
1411 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001412 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001413 Stream.EmitRecord(IMPORTS, Record);
1414 }
Mike Stump11289f42009-09-09 15:08:12 +00001415
Richard Smith0516b182015-09-08 19:40:14 +00001416 // Write the options block.
1417 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1418
Douglas Gregor112b9072012-10-18 05:31:06 +00001419 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001420 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001421 const LangOptions &LangOpts = Context.getLangOpts();
1422#define LANGOPT(Name, Bits, Default, Description) \
1423 Record.push_back(LangOpts.Name);
1424#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1425 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001426#include "clang/Basic/LangOptions.def"
1427#define SANITIZER(NAME, ID) \
1428 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001429#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001430
Ben Langmuircd98cb72015-06-23 18:20:18 +00001431 Record.push_back(LangOpts.ModuleFeatures.size());
1432 for (StringRef Feature : LangOpts.ModuleFeatures)
1433 AddString(Feature, Record);
1434
Douglas Gregor112b9072012-10-18 05:31:06 +00001435 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1436 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001437
1438 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001439
1440 // Comment options.
1441 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001442 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1443 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001444 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001445 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001446
Samuel Antaoee8fb302016-01-06 13:42:12 +00001447 // OpenMP offloading options.
1448 Record.push_back(LangOpts.OMPTargetTriples.size());
1449 for (auto &T : LangOpts.OMPTargetTriples)
1450 AddString(T.getTriple(), Record);
1451
1452 AddString(LangOpts.OMPHostIRFile, Record);
1453
Douglas Gregor112b9072012-10-18 05:31:06 +00001454 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1455
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001456 // Target options.
1457 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001458 const TargetInfo &Target = Context.getTargetInfo();
1459 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001460 AddString(TargetOpts.Triple, Record);
1461 AddString(TargetOpts.CPU, Record);
1462 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001463 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1464 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1465 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1466 }
1467 Record.push_back(TargetOpts.Features.size());
1468 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1469 AddString(TargetOpts.Features[I], Record);
1470 }
1471 Stream.EmitRecord(TARGET_OPTIONS, Record);
1472
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001473 // Diagnostic options.
1474 Record.clear();
1475 const DiagnosticOptions &DiagOpts
1476 = Context.getDiagnostics().getDiagnosticOptions();
1477#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1478#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1479 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1480#include "clang/Basic/DiagnosticOptions.def"
1481 Record.push_back(DiagOpts.Warnings.size());
1482 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1483 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001484 Record.push_back(DiagOpts.Remarks.size());
1485 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1486 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001487 // Note: we don't serialize the log or serialization file names, because they
1488 // are generally transient files and will almost always be overridden.
1489 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1490
Douglas Gregorc6317db2012-10-24 15:49:58 +00001491 // File system options.
1492 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001493 const FileSystemOptions &FSOpts =
1494 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001495 AddString(FSOpts.WorkingDir, Record);
1496 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1497
Douglas Gregor2d302362012-10-24 16:50:34 +00001498 // Header search options.
1499 Record.clear();
1500 const HeaderSearchOptions &HSOpts
1501 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1502 AddString(HSOpts.Sysroot, Record);
1503
1504 // Include entries.
1505 Record.push_back(HSOpts.UserEntries.size());
1506 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1507 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1508 AddString(Entry.Path, Record);
1509 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001510 Record.push_back(Entry.IsFramework);
1511 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001512 }
1513
1514 // System header prefixes.
1515 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1516 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1517 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1518 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1519 }
1520
1521 AddString(HSOpts.ResourceDir, Record);
1522 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001523 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001524 Record.push_back(HSOpts.DisableModuleHash);
1525 Record.push_back(HSOpts.UseBuiltinIncludes);
1526 Record.push_back(HSOpts.UseStandardSystemIncludes);
1527 Record.push_back(HSOpts.UseStandardCXXIncludes);
1528 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001529 // Write out the specific module cache path that contains the module files.
1530 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001531 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1532
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001533 // Preprocessor options.
1534 Record.clear();
1535 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1536
1537 // Macro definitions.
1538 Record.push_back(PPOpts.Macros.size());
1539 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1540 AddString(PPOpts.Macros[I].first, Record);
1541 Record.push_back(PPOpts.Macros[I].second);
1542 }
1543
1544 // Includes
1545 Record.push_back(PPOpts.Includes.size());
1546 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1547 AddString(PPOpts.Includes[I], Record);
1548
1549 // Macro includes
1550 Record.push_back(PPOpts.MacroIncludes.size());
1551 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1552 AddString(PPOpts.MacroIncludes[I], Record);
1553
Douglas Gregorb6368752012-10-24 23:41:50 +00001554 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001555 // Detailed record is important since it is used for the module cache hash.
1556 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001557 AddString(PPOpts.ImplicitPCHInclude, Record);
1558 AddString(PPOpts.ImplicitPTHInclude, Record);
1559 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1560 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1561
Richard Smith0516b182015-09-08 19:40:14 +00001562 // Leave the options block.
1563 Stream.ExitBlock();
1564
Douglas Gregora3b20262011-05-06 21:43:30 +00001565 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001566 SourceManager &SM = Context.getSourceManager();
1567 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001568 auto *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001569 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1570 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001571 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1572 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1573
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001574 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001575 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001576 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001577 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001578 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001579
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001580 Record.clear();
1581 Record.push_back(SM.getMainFileID().getOpaqueValue());
1582 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1583
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001584 // Original PCH directory
1585 if (!OutputFile.empty() && OutputFile != "-") {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001586 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001587 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1589 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1590
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001591 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001592
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001593 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001594 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1595
Mehdi Amini57a41912015-09-10 01:46:39 +00001596 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001597 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1598 }
1599
Douglas Gregor49491f72013-03-15 22:15:07 +00001600 WriteInputFiles(Context.SourceMgr,
1601 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001602 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001603 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001604 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001605}
1606
Douglas Gregor49491f72013-03-15 22:15:07 +00001607namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001608
Douglas Gregor49491f72013-03-15 22:15:07 +00001609 /// \brief An input file.
1610 struct InputFileEntry {
1611 const FileEntry *File;
1612 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001613 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001614 bool BufferOverridden;
1615 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001616
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001617} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001618
1619void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1620 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001621 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001622 using namespace llvm;
1623 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001624
Douglas Gregor72be3902012-10-19 00:38:02 +00001625 // Create input-file abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001626 auto *IFAbbrev = new BitCodeAbbrev();
Douglas Gregor72be3902012-10-19 00:38:02 +00001627 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001628 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001629 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1630 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001631 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001632 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001633 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1634 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1635
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001636 // Get all ContentCache objects for files, sorted by whether the file is a
1637 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001638 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001639 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1640 // Get this source location entry.
1641 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001642 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001643
1644 // We only care about file entries that were not overridden.
1645 if (!SLoc->isFile())
1646 continue;
1647 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001648 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001649 continue;
1650
Douglas Gregor49491f72013-03-15 22:15:07 +00001651 InputFileEntry Entry;
1652 Entry.File = Cache->OrigEntry;
1653 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001654 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001655 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001656 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001657 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001658 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001659 SortedFiles.push_front(Entry);
1660 }
1661
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001662 unsigned UserFilesNum = 0;
1663 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001664 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001665 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001666 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001667 if (InputFileID != 0)
1668 continue; // already recorded this file.
1669
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001670 // Record this entry's offset.
1671 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001672
1673 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001674
Douglas Gregor49491f72013-03-15 22:15:07 +00001675 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001676 ++UserFilesNum;
1677
Douglas Gregor72be3902012-10-19 00:38:02 +00001678 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001679 // And whether this file was overridden.
1680 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001681 INPUT_FILE,
1682 InputFileOffsets.size(),
1683 (uint64_t)Entry.File->getSize(),
1684 (uint64_t)getTimestampForOutput(Entry.File),
1685 Entry.BufferOverridden,
1686 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001687
Richard Smith7ed1bc92014-12-05 22:42:13 +00001688 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1689 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001690
Douglas Gregor112b9072012-10-18 05:31:06 +00001691 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001692
1693 // Create input file offsets abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001694 auto *OffsetsAbbrev = new BitCodeAbbrev();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001695 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1696 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001697 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1698 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001699 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1700 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1701
1702 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001703 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1704 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001705 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001706}
1707
Douglas Gregora7f71a92009-04-10 03:52:48 +00001708//===----------------------------------------------------------------------===//
1709// Source Manager Serialization
1710//===----------------------------------------------------------------------===//
1711
1712/// \brief Create an abbreviation for the SLocEntry that refers to a
1713/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001714static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001715 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001716
1717 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001718 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1721 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1722 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001723 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001724 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001728 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001729}
1730
1731/// \brief Create an abbreviation for the SLocEntry that refers to a
1732/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001733static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001734 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001735
1736 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001737 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001743 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001744}
1745
1746/// \brief Create an abbreviation for the SLocEntry that refers to a
1747/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001748static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1749 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001750 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001751
1752 auto *Abbrev = new BitCodeAbbrev();
Richard Smithaada85c2016-02-06 02:06:43 +00001753 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1754 : SM_SLOC_BUFFER_BLOB));
1755 if (Compressed)
1756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001758 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001759}
1760
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001761/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1762/// expansion.
1763static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001764 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001765
1766 auto *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001767 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001773 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001774}
1775
Douglas Gregor09b69892011-02-10 17:09:37 +00001776namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001777
Douglas Gregor09b69892011-02-10 17:09:37 +00001778 // Trait used for the on-disk hash table of header search information.
1779 class HeaderFileInfoTrait {
1780 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001781 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001782
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001783 // Keep track of the framework names we've used during serialization.
1784 SmallVector<char, 128> FrameworkStringData;
1785 llvm::StringMap<unsigned> FrameworkNameOffset;
1786
Douglas Gregor09b69892011-02-10 17:09:37 +00001787 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001788 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1789 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001790
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001791 struct key_type {
1792 const FileEntry *FE;
1793 const char *Filename;
1794 };
1795 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001796
1797 typedef HeaderFileInfo data_type;
1798 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001799 typedef unsigned hash_value_type;
1800 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001801
Richard Smithe75ee0f2015-08-17 07:13:32 +00001802 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001803 // The hash is based only on size/time of the file, so that the reader can
1804 // match even when symlinking or excess path elements ("foo/../", "../")
1805 // change the form of the name. However, complete path is still the key.
1806 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001807 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001808 }
1809
1810 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001811 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001812 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001813 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001814 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001815 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001816 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001817 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1818 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1819 DataLen += 4;
1820 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001821 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001822 }
1823
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001824 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001825 using namespace llvm::support;
1826 endian::Writer<little> LE(Out);
1827 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001828 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001829 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001830 KeyLen -= 8;
1831 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001832 }
1833
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001834 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001835 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001836 using namespace llvm::support;
1837 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001838 uint64_t Start = Out.tell(); (void)Start;
1839
Richard Smith386bb072015-08-18 23:42:23 +00001840 unsigned char Flags = (Data.isImport << 4)
1841 | (Data.isPragmaOnce << 3)
1842 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001843 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001844 LE.write<uint8_t>(Flags);
1845 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001846
1847 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001848 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001849 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001850 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001851
1852 unsigned Offset = 0;
1853 if (!Data.Framework.empty()) {
1854 // If this header refers into a framework, save the framework name.
1855 llvm::StringMap<unsigned>::iterator Pos
1856 = FrameworkNameOffset.find(Data.Framework);
1857 if (Pos == FrameworkNameOffset.end()) {
1858 Offset = FrameworkStringData.size() + 1;
1859 FrameworkStringData.append(Data.Framework.begin(),
1860 Data.Framework.end());
1861 FrameworkStringData.push_back(0);
1862
1863 FrameworkNameOffset[Data.Framework] = Offset;
1864 } else
1865 Offset = Pos->second;
1866 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001867 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001868
Richard Smith386bb072015-08-18 23:42:23 +00001869 // FIXME: If the header is excluded, we should write out some
1870 // record of that fact.
1871 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1872 if (uint32_t ModID =
1873 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1874 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1875 assert((Value >> 2) == ModID && "overflow in header module info");
1876 LE.write<uint32_t>(Value);
1877 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001878 }
1879
Douglas Gregor09b69892011-02-10 17:09:37 +00001880 assert(Out.tell() - Start == DataLen && "Wrong data length");
1881 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001882
1883 const char *strings_begin() const { return FrameworkStringData.begin(); }
1884 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001885 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001886
Douglas Gregor09b69892011-02-10 17:09:37 +00001887} // end anonymous namespace
1888
1889/// \brief Write the header search block for the list of files that
1890///
1891/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001892void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001893 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001894 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1895
1896 if (FilesByUID.size() > HS.header_file_size())
1897 FilesByUID.resize(HS.header_file_size());
1898
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001899 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001900 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001901 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001902 unsigned NumHeaderSearchEntries = 0;
1903 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1904 const FileEntry *File = FilesByUID[UID];
1905 if (!File)
1906 continue;
1907
Richard Smith386bb072015-08-18 23:42:23 +00001908 // Get the file info. This will load info from the external source if
1909 // necessary. Skip emitting this file if we have no information on it
1910 // as a header file (in which case HFI will be null) or if it hasn't
1911 // changed since it was loaded. Also skip it if it's for a modular header
1912 // from a different module; in that case, we rely on the module(s)
1913 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001914 const HeaderFileInfo *HFI =
1915 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1916 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001917 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001918
Richard Smith7ed1bc92014-12-05 22:42:13 +00001919 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001920 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001921 SmallString<128> FilenameTmp(Filename);
1922 if (PreparePathForOutput(FilenameTmp)) {
1923 // If we performed any translation on the file name at all, we need to
1924 // save this string, since the generator will refer to it later.
1925 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001926 SavedStrings.push_back(Filename);
1927 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001928
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001929 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001930 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001931 ++NumHeaderSearchEntries;
1932 }
1933
1934 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001935 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001936 uint32_t BucketOffset;
1937 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001938 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001939 llvm::raw_svector_ostream Out(TableData);
1940 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001941 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001942 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1943 }
1944
1945 // Create a blob abbreviation
1946 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001947
1948 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor09b69892011-02-10 17:09:37 +00001949 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1954 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1955
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001956 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001957 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1958 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001959 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001960 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001961
1962 // Free all of the strings we had to duplicate.
1963 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001964 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001965}
1966
Douglas Gregora7f71a92009-04-10 03:52:48 +00001967/// \brief Writes the block containing the serialized form of the
1968/// source manager.
1969///
1970/// TODO: We should probably use an on-disk hash table (stored in a
1971/// blob), indexed based on the file name, so that we only create
1972/// entries for files that we actually need. In the common case (no
1973/// errors), we probably won't have to create file entries for any of
1974/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001975void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001976 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001977 RecordData Record;
1978
Chris Lattner0910e3b2009-04-10 17:16:57 +00001979 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00001980 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001981
1982 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001983 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1984 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00001985 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
1986 unsigned SLocBufferBlobCompressedAbbrv =
1987 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001988 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001989
Douglas Gregor258ae542009-04-27 06:38:32 +00001990 // Write out the source location entry table. We skip the first
1991 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001992 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001993 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001994 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1995 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001996 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001997 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001998 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001999 FileID FID = FileID::get(I);
2000 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002001
Douglas Gregor258ae542009-04-27 06:38:32 +00002002 // Record the offset of this source-location entry.
2003 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2004
2005 // Figure out which record code to use.
2006 unsigned Code;
2007 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002008 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2009 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002010 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002011 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002012 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002013 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002014 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002015 Record.clear();
2016 Record.push_back(Code);
2017
Douglas Gregor925296b2011-07-19 16:10:42 +00002018 // Starting offset of this entry within this module, so skip the dummy.
2019 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002020 if (SLoc->isFile()) {
2021 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002022 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002023 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2024 Record.push_back(File.hasLineDirectives());
2025
2026 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002027 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002028 if (Content->OrigEntry) {
2029 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002030 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002031
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002032 // The source location entry is a file. Emit input file ID.
2033 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2034 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002035
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002036 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002037
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002038 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002039 if (FDI != FileDeclIDs.end()) {
2040 Record.push_back(FDI->second->FirstDeclIndex);
2041 Record.push_back(FDI->second->DeclIDs.size());
2042 } else {
2043 Record.push_back(0);
2044 Record.push_back(0);
2045 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002046
2047 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2048
Richard Smithaada85c2016-02-06 02:06:43 +00002049 if (Content->BufferOverridden || Content->IsTransient)
2050 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002051 } else {
2052 // The source location entry is a buffer. The blob associated
2053 // with this entry contains the contents of the buffer.
2054
2055 // We add one to the size so that we capture the trailing NULL
2056 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2057 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002058 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002059 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00002060 const char *Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002061 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002062 StringRef(Name, strlen(Name) + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002063 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002064
Douglas Gregor925296b2011-07-19 16:10:42 +00002065 if (strcmp(Name, "<built-in>") == 0) {
2066 PreloadSLocs.push_back(SLocEntryOffsets.size());
2067 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002068 }
Richard Smithaada85c2016-02-06 02:06:43 +00002069
2070 if (EmitBlob) {
2071 // Include the implicit terminating null character in the on-disk buffer
2072 // if we're writing it uncompressed.
2073 const llvm::MemoryBuffer *Buffer =
2074 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2075 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2076
2077 // Compress the buffer if possible. We expect that almost all PCM
2078 // consumers will not want its contents.
2079 SmallString<0> CompressedBuffer;
2080 if (llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer) ==
2081 llvm::zlib::StatusOK) {
2082 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2083 Blob.size() - 1};
2084 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2085 CompressedBuffer);
2086 } else {
2087 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
2088 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2089 }
2090 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002091 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002092 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002093 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002094 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2095 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2096 AddSourceLocation(Expansion.isMacroArgExpansion()
2097 ? SourceLocation()
2098 : Expansion.getExpansionLocEnd(),
2099 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002100
2101 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002102 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002103 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002104 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002105 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002106 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002107 }
2108 }
2109
Douglas Gregor8f45df52009-04-16 22:23:12 +00002110 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002111
2112 if (SLocEntryOffsets.empty())
2113 return;
2114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002115 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002116 // table is used for lazily loading source-location information.
2117 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002118
2119 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002120 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2124 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002125 {
2126 RecordData::value_type Record[] = {
2127 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2128 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2129 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2130 bytes(SLocEntryOffsets));
2131 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002132 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002133 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002134 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002135
2136 // Write the line table. It depends on remapping working, so it must come
2137 // after the source location offsets.
2138 if (SourceMgr.hasLineTable()) {
2139 LineTableInfo &LineTable = SourceMgr.getLineTable();
2140
2141 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002142
2143 // Emit the needed file names.
2144 llvm::DenseMap<int, int> FilenameMap;
2145 for (const auto &L : LineTable) {
2146 if (L.first.ID < 0)
2147 continue;
2148 for (auto &LE : L.second) {
2149 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2150 FilenameMap.size())).second)
2151 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2152 }
2153 }
2154 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002155
2156 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002157 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002158 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002159 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002160 continue;
2161
2162 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002163 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002164
2165 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002166 Record.push_back(L.second.size());
2167 for (const auto &LE : L.second) {
2168 Record.push_back(LE.FileOffset);
2169 Record.push_back(LE.LineNo);
2170 Record.push_back(FilenameMap[LE.FilenameID]);
2171 Record.push_back((unsigned)LE.FileKind);
2172 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002173 }
2174 }
Richard Smith63078492015-09-01 07:41:55 +00002175
Douglas Gregor925296b2011-07-19 16:10:42 +00002176 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2177 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002178}
2179
Douglas Gregorc5046832009-04-27 18:38:38 +00002180//===----------------------------------------------------------------------===//
2181// Preprocessor Serialization
2182//===----------------------------------------------------------------------===//
2183
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002184static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2185 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002186 if (MacroInfo *MI = MD->getMacroInfo())
2187 if (MI->isBuiltinMacro())
2188 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002189
2190 if (IsModule) {
2191 SourceLocation Loc = MD->getLocation();
2192 if (Loc.isInvalid())
2193 return true;
2194 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2195 return true;
2196 }
2197
2198 return false;
2199}
2200
Chris Lattnereeffaef2009-04-10 17:15:23 +00002201/// \brief Writes the block containing the serialized form of the
2202/// preprocessor.
2203///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002204void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002205 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2206 if (PPRec)
2207 WritePreprocessorDetail(*PPRec);
2208
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002209 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002210 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002211
Chris Lattner0af3ba12009-04-13 01:29:17 +00002212 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2213 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002214 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002216 }
2217
2218 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002219 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002221 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002222 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002223 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002224 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002225
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002226 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002227 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002228
Richard Smithee977932015-05-01 21:22:17 +00002229 // Construct the list of identifiers with macro directives that need to be
2230 // serialized.
2231 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2232 for (auto &Id : PP.getIdentifierTable())
2233 if (Id.second->hadMacroDefinition() &&
2234 (!Id.second->isFromAST() ||
2235 Id.second->hasChangedSinceDeserialization()))
2236 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002237 // Sort the set of macro definitions that need to be serialized by the
2238 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002239 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2240 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002241
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002242 // Emit the macro directives as a list and associate the offset with the
2243 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002244 for (const IdentifierInfo *Name : MacroIdentifiers) {
2245 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002246 auto StartOffset = Stream.GetCurrentBitNo();
2247
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002248 // Emit the macro directives in reverse source order.
2249 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002250 // Once we hit an ignored macro, we're done: the rest of the chain
2251 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002252 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002253 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002254
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002255 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002256 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002257 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002258 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002259 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002260 Record.push_back(VisMD->isPublic());
2261 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002262 }
Richard Smithd7329392015-04-21 21:46:32 +00002263
Richard Smithb8b2ed62015-04-23 18:18:26 +00002264 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002265 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002266 // We write out exported module macros for PCH as well.
2267 auto Leafs = PP.getLeafModuleMacros(Name);
2268 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2269 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2270 while (!Worklist.empty()) {
2271 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002272
Manman Ren33d80292016-05-31 18:19:32 +00002273 // Emit a record indicating this submodule exports this macro.
2274 ModuleMacroRecord.push_back(
2275 getSubmoduleID(Macro->getOwningModule()));
2276 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2277 for (auto *M : Macro->overrides())
2278 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002279
Manman Ren33d80292016-05-31 18:19:32 +00002280 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2281 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002282
Manman Ren33d80292016-05-31 18:19:32 +00002283 // Enqueue overridden macros once we've visited all their ancestors.
2284 for (auto *M : Macro->overrides())
2285 if (++Visits[M] == M->getNumOverridingMacros())
2286 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002287
Manman Ren33d80292016-05-31 18:19:32 +00002288 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002289 }
Richard Smithd7329392015-04-21 21:46:32 +00002290
Richard Smithee977932015-05-01 21:22:17 +00002291 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002292 continue;
2293
Richard Smithd7329392015-04-21 21:46:32 +00002294 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002295 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2296 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002297 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002298
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002299 /// \brief Offsets of each of the macros into the bitstream, indexed by
2300 /// the local macro ID
2301 ///
2302 /// For each identifier that is associated with a macro, this map
2303 /// provides the offset into the bitstream where that macro is
2304 /// defined.
2305 std::vector<uint32_t> MacroOffsets;
2306
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002307 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2308 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2309 MacroInfo *MI = MacroInfosToEmit[I].MI;
2310 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002311
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002312 if (ID < FirstMacroID) {
2313 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2314 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002315 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002316
2317 // Record the local offset of this macro.
2318 unsigned Index = ID - FirstMacroID;
2319 if (Index == MacroOffsets.size())
2320 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2321 else {
2322 if (Index > MacroOffsets.size())
2323 MacroOffsets.resize(Index + 1);
2324
2325 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2326 }
2327
2328 AddIdentifierRef(Name, Record);
2329 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2330 AddSourceLocation(MI->getDefinitionLoc(), Record);
2331 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2332 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002333 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002334 unsigned Code;
2335 if (MI->isObjectLike()) {
2336 Code = PP_MACRO_OBJECT_LIKE;
2337 } else {
2338 Code = PP_MACRO_FUNCTION_LIKE;
2339
2340 Record.push_back(MI->isC99Varargs());
2341 Record.push_back(MI->isGNUVarargs());
2342 Record.push_back(MI->hasCommaPasting());
2343 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002344 for (const IdentifierInfo *Arg : MI->args())
2345 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002346 }
2347
2348 // If we have a detailed preprocessing record, record the macro definition
2349 // ID that corresponds to this macro.
2350 if (PPRec)
2351 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2352
2353 Stream.EmitRecord(Code, Record);
2354 Record.clear();
2355
2356 // Emit the tokens array.
2357 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2358 // Note that we know that the preprocessor does not have any annotation
2359 // tokens in it because they are created by the parser, and thus can't
2360 // be in a macro definition.
2361 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002362 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002363 Stream.EmitRecord(PP_TOKEN, Record);
2364 Record.clear();
2365 }
2366 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002367 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002368
Douglas Gregor92a96f52011-02-08 21:58:10 +00002369 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002370
2371 // Write the offsets table for macro IDs.
2372 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002373
Richard Smith13090a22015-04-10 02:02:24 +00002374 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002375 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2379
2380 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002381 {
2382 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2383 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2384 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2385 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002386}
2387
2388void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002389 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002390 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002391
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002392 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002393
Douglas Gregor92a96f52011-02-08 21:58:10 +00002394 // Enter the preprocessor block.
2395 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002396
Douglas Gregoraae92242010-03-19 21:51:54 +00002397 // If the preprocessor has a preprocessing record, emit it.
2398 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002399 using namespace llvm;
2400
2401 // Set up the abbreviation for
2402 unsigned InclusionAbbrev = 0;
2403 {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002404 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002405 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2411 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2412 }
2413
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002414 unsigned FirstPreprocessorEntityID
2415 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2416 + NUM_PREDEF_PP_ENTITY_IDS;
2417 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002418 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002419 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2420 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002421 E != EEnd;
2422 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002423 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002424
Richard Smith66a81862015-05-04 02:25:31 +00002425 PreprocessedEntityOffsets.push_back(
2426 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002427
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002428 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002429 // Record this macro definition's ID.
2430 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002431
Douglas Gregor92a96f52011-02-08 21:58:10 +00002432 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002433 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2434 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002435 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002436
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002437 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002438 Record.push_back(ME->isBuiltinMacro());
2439 if (ME->isBuiltinMacro())
2440 AddIdentifierRef(ME->getName(), Record);
2441 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002442 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002443 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002444 continue;
2445 }
2446
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002447 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002448 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002449 Record.push_back(ID->getFileName().size());
2450 Record.push_back(ID->wasInQuotes());
2451 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002452 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002453 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002454 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002455 // Check that the FileEntry is not null because it was not resolved and
2456 // we create a PCH even with compiler errors.
2457 if (ID->getFile())
2458 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002459 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002460 continue;
2461 }
2462
2463 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2464 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002465 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002466
Douglas Gregoraae92242010-03-19 21:51:54 +00002467 // Write the offsets table for the preprocessing record.
2468 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002469 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2470
Douglas Gregoraae92242010-03-19 21:51:54 +00002471 // Write the offsets table for identifier IDs.
2472 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002473
2474 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002475 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002478 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002479
Mehdi Amini57a41912015-09-10 01:46:39 +00002480 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2481 FirstPreprocessorEntityID -
2482 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002483 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002484 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002485 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002486}
2487
Richard Smith386bb072015-08-18 23:42:23 +00002488unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002489 if (!Mod)
2490 return 0;
2491
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002492 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2493 if (Known != SubmoduleIDs.end())
2494 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002495
Richard Smithdc1f0422016-07-20 19:10:16 +00002496 auto *Top = Mod->getTopLevelModule();
2497 if (Top != WritingModule &&
2498 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule)))
Richard Smith386bb072015-08-18 23:42:23 +00002499 return 0;
2500
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002501 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2502}
2503
Richard Smith386bb072015-08-18 23:42:23 +00002504unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2505 // FIXME: This can easily happen, if we have a reference to a submodule that
2506 // did not result in us loading a module file for that submodule. For
2507 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2508 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2509 assert((ID || !Mod) &&
2510 "asked for module ID for non-local, non-imported module");
2511 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002512}
2513
Douglas Gregor253eefe2011-12-01 00:59:36 +00002514/// \brief Compute the number of modules within the given tree (including the
2515/// given module).
2516static unsigned getNumberOfModules(Module *Mod) {
2517 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002518 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002519 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002520 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002521
2522 return ChildModules + 1;
2523}
2524
Douglas Gregorde3ef502011-11-30 23:21:26 +00002525void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002526 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002527 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002528
2529 // Write the abbreviations needed for the submodules block.
2530 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002531
2532 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor69021972011-11-30 17:33:56 +00002533 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2545 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2546
2547 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002548 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2550 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2551
2552 Abbrev = new BitCodeAbbrev();
2553 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2555 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002556
2557 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002558 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2560 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2561
2562 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002563 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2565 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2566
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002567 Abbrev = new BitCodeAbbrev();
2568 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002571 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2572
Douglas Gregor59527662012-10-15 06:28:11 +00002573 Abbrev = new BitCodeAbbrev();
2574 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2576 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2577
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002578 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002579 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2581 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2582
2583 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002584 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2586 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2587
2588 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002589 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2591 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2592
2593 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002594 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2595 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2596 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2597 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2598
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002599 Abbrev = new BitCodeAbbrev();
2600 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2602 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2603
Douglas Gregorfb912652013-03-20 21:10:35 +00002604 Abbrev = new BitCodeAbbrev();
2605 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2608 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2609
Douglas Gregor253eefe2011-12-01 00:59:36 +00002610 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002611 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2612 FirstSubmoduleID -
2613 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002614 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2615
Douglas Gregor69021972011-11-30 17:33:56 +00002616 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002617 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002618 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002619 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002620 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002621 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002622 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002623
2624 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002625 if (Mod->Parent) {
2626 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002627 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002628 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002629
2630 // Emit the definition of the block.
2631 {
2632 RecordData::value_type Record[] = {
2633 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2634 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2635 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2636 Mod->ConfigMacrosExhaustive};
2637 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2638 }
2639
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002640 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002641 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002642 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002643 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002644 }
2645
Douglas Gregor69021972011-11-30 17:33:56 +00002646 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002647 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002648 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002649 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2650 UmbrellaHeader.NameAsWritten);
2651 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002652 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2653 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002654 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002655 }
Richard Smith306d8922014-10-22 23:50:56 +00002656
Douglas Gregor69021972011-11-30 17:33:56 +00002657 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002658 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002659 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002660 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002661 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002662 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002663 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2664 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2665 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002666 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002667 Module::HK_PrivateTextual},
2668 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002669 };
2670 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002671 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002672 for (auto &H : Mod->Headers[HL.HeaderKind])
2673 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2674 }
2675
2676 // Emit the top headers.
2677 {
2678 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002679 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002680 for (auto *H : TopHeaders)
2681 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002682 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002683
2684 // Emit the imports.
2685 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002686 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002687 for (auto *I : Mod->Imports)
2688 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002689 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2690 }
2691
Douglas Gregor24bb9232011-12-02 18:58:38 +00002692 // Emit the exports.
2693 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002694 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002695 for (const auto &E : Mod->Exports) {
2696 // FIXME: This may fail; we don't require that all exported modules
2697 // are local or imported.
2698 Record.push_back(getSubmoduleID(E.getPointer()));
2699 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002700 }
2701 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2702 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002703
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002704 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2705 // Might be unnecessary as use declarations are only used to build the
2706 // module itself.
2707
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002708 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002709 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002710 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2711 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002712 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002713 }
2714
Douglas Gregorfb912652013-03-20 21:10:35 +00002715 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002716 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002717 // FIXME: This may fail; we don't require that all conflicting modules
2718 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002719 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2720 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002721 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002722 }
2723
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002724 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002725 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002726 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002727 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002728 }
2729
Richard Smithdc1f0422016-07-20 19:10:16 +00002730 // Emit the initializers, if any.
2731 RecordData Inits;
2732 for (Decl *D : Context->getModuleInitializers(Mod))
2733 Inits.push_back(GetDeclRef(D));
2734 if (!Inits.empty())
2735 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2736
Douglas Gregor69021972011-11-30 17:33:56 +00002737 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002738 for (auto *M : Mod->submodules())
2739 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002740 }
2741
2742 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002743
Richard Smith23d8d032015-05-14 00:45:20 +00002744 assert((NextSubmoduleID - FirstSubmoduleID ==
2745 getNumberOfModules(WritingModule)) &&
2746 "Wrong # of submodules; found a reference to a non-local, "
2747 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002748}
2749
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002750serialization::SubmoduleID
2751ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002752 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002753 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002754
2755 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002756 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002757 Module *OwningMod
2758 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002759 if (!OwningMod)
2760 return 0;
2761
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002762 // Check whether this submodule is part of our own module.
2763 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002764 return 0;
2765
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002766 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002767}
2768
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002769void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2770 bool isModule) {
2771 // Make sure set diagnostic pragmas don't affect the translation unit that
2772 // imports the module.
2773 // FIXME: Make diagnostic pragma sections work properly with modules.
2774 if (isModule)
2775 return;
2776
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002777 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2778 DiagStateIDMap;
2779 unsigned CurrID = 0;
2780 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002781 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002782 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002783 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2784 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002785 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002786 if (point.Loc.isInvalid())
2787 continue;
2788
Richard Smithb22a1d12016-03-27 20:13:24 +00002789 AddSourceLocation(point.Loc, Record);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002790 unsigned &DiagStateID = DiagStateIDMap[point.State];
2791 Record.push_back(DiagStateID);
2792
2793 if (DiagStateID == 0) {
2794 DiagStateID = ++CurrID;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002795 for (const auto &I : *(point.State)) {
2796 if (I.second.isPragma()) {
2797 Record.push_back(I.first);
2798 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002799 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002800 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002801 Record.push_back(-1); // mark the end of the diag/map pairs for this
2802 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002803 }
2804 }
2805
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002806 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002807 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002808}
2809
Douglas Gregorc5046832009-04-27 18:38:38 +00002810//===----------------------------------------------------------------------===//
2811// Type Serialization
2812//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002813
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002814/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002815void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002816 TypeIdx &IdxRef = TypeIdxs[T];
2817 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2818 IdxRef = TypeIdx(NextTypeID++);
2819 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002820
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002821 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002822
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002823 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002824
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002825 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002826 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002827 W.Visit(T);
2828 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002829
Richard Smith290d8012016-04-06 17:06:00 +00002830 // Record the offset for this type.
2831 unsigned Index = Idx.getIndex() - FirstTypeID;
2832 if (TypeOffsets.size() == Index)
2833 TypeOffsets.push_back(Offset);
2834 else if (TypeOffsets.size() < Index) {
2835 TypeOffsets.resize(Index + 1);
2836 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002837 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002838 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002839 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002840}
2841
Douglas Gregorc5046832009-04-27 18:38:38 +00002842//===----------------------------------------------------------------------===//
2843// Declaration Serialization
2844//===----------------------------------------------------------------------===//
2845
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002846/// \brief Write the block containing all of the declaration IDs
2847/// lexically declared within the given DeclContext.
2848///
2849/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2850/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002851uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002852 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002853 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002854 return 0;
2855
Douglas Gregor8f45df52009-04-16 22:23:12 +00002856 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002857 SmallVector<uint32_t, 128> KindDeclPairs;
2858 for (const auto *D : DC->decls()) {
2859 KindDeclPairs.push_back(D->getKind());
2860 KindDeclPairs.push_back(GetDeclRef(D));
2861 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002862
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002863 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002864 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002865 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2866 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002867 return Offset;
2868}
2869
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002870void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002871 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002872
2873 // Write the type offsets array
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002874 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002875 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2879 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002880 {
2881 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2882 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2883 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2884 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002885
2886 // Write the declaration offsets array
2887 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002888 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2892 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002893 {
2894 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2895 FirstDeclID - NUM_PREDEF_DECL_IDS};
2896 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2897 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002898}
2899
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002900void ASTWriter::WriteFileDeclIDsMap() {
2901 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002902
Chandler Carruthb306d732015-03-27 00:31:20 +00002903 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2904 FileDeclIDs.begin(), FileDeclIDs.end());
2905 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2906 llvm::less_first());
2907
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002908 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002909 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2910 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2911 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2912 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2913 for (auto &LocDeclEntry : Info.DeclIDs)
2914 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002915 }
2916
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002917 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002918 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2921 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002922 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2923 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002924 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002925}
2926
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002927void ASTWriter::WriteComments() {
2928 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002929 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002930 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002931 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002932 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002933 AddSourceRange(I->getSourceRange(), Record);
2934 Record.push_back(I->getKind());
2935 Record.push_back(I->isTrailingComment());
2936 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002937 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2938 }
2939 Stream.ExitBlock();
2940}
2941
Douglas Gregorc5046832009-04-27 18:38:38 +00002942//===----------------------------------------------------------------------===//
2943// Global Method Pool and Selector Serialization
2944//===----------------------------------------------------------------------===//
2945
Douglas Gregore84a9da2009-04-20 20:36:09 +00002946namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002947
Douglas Gregorc78d3462009-04-24 21:10:55 +00002948// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002949class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002950 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002951
2952public:
2953 typedef Selector key_type;
2954 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002955
Sebastian Redl834bb972010-08-04 17:20:04 +00002956 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002957 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002958 ObjCMethodList Instance, Factory;
2959 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002960 typedef const data_type& data_type_ref;
2961
Justin Bogner25463f12014-04-18 20:27:24 +00002962 typedef unsigned hash_value_type;
2963 typedef unsigned offset_type;
2964
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002965 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002966
Justin Bogner25463f12014-04-18 20:27:24 +00002967 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002968 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002969 }
Mike Stump11289f42009-09-09 15:08:12 +00002970
2971 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002972 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002973 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002974 using namespace llvm::support;
2975 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002976 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002977 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002978 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2979 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002980 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002981 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002982 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002983 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002984 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002985 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002986 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002987 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002988 return std::make_pair(KeyLen, DataLen);
2989 }
Mike Stump11289f42009-09-09 15:08:12 +00002990
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002991 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002992 using namespace llvm::support;
2993 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002994 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002995 assert((Start >> 32) == 0 && "Selector key offset too large");
2996 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002997 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002998 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002999 if (N == 0)
3000 N = 1;
3001 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003002 LE.write<uint32_t>(
3003 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003004 }
Mike Stump11289f42009-09-09 15:08:12 +00003005
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003006 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003007 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003008 using namespace llvm::support;
3009 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003010 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003011 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003012 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003013 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003014 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003015 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003016 ++NumInstanceMethods;
3017
3018 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003019 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003020 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003021 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003022 ++NumFactoryMethods;
3023
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003024 unsigned InstanceBits = Methods.Instance.getBits();
3025 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003026 unsigned InstanceHasMoreThanOneDeclBit =
3027 Methods.Instance.hasMoreThanOneDecl();
3028 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3029 (InstanceHasMoreThanOneDeclBit << 2) |
3030 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003031 unsigned FactoryBits = Methods.Factory.getBits();
3032 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003033 unsigned FactoryHasMoreThanOneDeclBit =
3034 Methods.Factory.hasMoreThanOneDecl();
3035 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3036 (FactoryHasMoreThanOneDeclBit << 2) |
3037 FactoryBits;
3038 LE.write<uint16_t>(FullInstanceBits);
3039 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003040 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003041 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003042 if (Method->getMethod())
3043 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003044 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003045 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003046 if (Method->getMethod())
3047 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003048
3049 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003050 }
3051};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003052
Douglas Gregorc78d3462009-04-24 21:10:55 +00003053} // end anonymous namespace
3054
Sebastian Redla19a67f2010-08-03 21:58:15 +00003055/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003056///
3057/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003058/// in an on-disk hash table indexed by the selector. The hash table also
3059/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003060void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003061 using namespace llvm;
3062
Sebastian Redla19a67f2010-08-03 21:58:15 +00003063 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003064 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003065 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003066 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003067 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003068 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003069 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003070 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003071
Sebastian Redla19a67f2010-08-03 21:58:15 +00003072 // Create the on-disk hash table representation. We walk through every
3073 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003074 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003075 for (auto &SelectorAndID : SelectorIDs) {
3076 Selector S = SelectorAndID.first;
3077 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003078 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003079 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003080 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003081 ObjCMethodList(),
3082 ObjCMethodList()
3083 };
3084 if (F != SemaRef.MethodPool.end()) {
3085 Data.Instance = F->second.first;
3086 Data.Factory = F->second.second;
3087 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003088 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003089 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003090 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003091 // Selector already exists. Did it change?
3092 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003093 for (ObjCMethodList *M = &Data.Instance;
3094 !changed && M && M->getMethod(); M = M->getNext()) {
3095 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003096 changed = true;
3097 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003098 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003099 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003100 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003101 changed = true;
3102 }
3103 if (!changed)
3104 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003105 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003106 // A new method pool entry.
3107 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003108 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003109 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003110 }
3111
Douglas Gregorc78d3462009-04-24 21:10:55 +00003112 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003113 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003114 uint32_t BucketOffset;
3115 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003116 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003117 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003118 llvm::raw_svector_ostream Out(MethodPool);
3119 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003120 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003121 BucketOffset = Generator.Emit(Out, Trait);
3122 }
3123
3124 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003125 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003126 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3130 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3131
Douglas Gregor95c13f52009-04-25 17:48:32 +00003132 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003133 {
3134 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3135 NumTableEntries};
3136 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3137 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003138
3139 // Create a blob abbreviation for the selector table offsets.
3140 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003141 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3145 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3146
3147 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003148 {
3149 RecordData::value_type Record[] = {
3150 SELECTOR_OFFSETS, SelectorOffsets.size(),
3151 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3152 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3153 bytes(SelectorOffsets));
3154 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003155 }
3156}
3157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003158/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003159void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003160 using namespace llvm;
3161 if (SemaRef.ReferencedSelectors.empty())
3162 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003163
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003164 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003165 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003167 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003168 // very tricky to fix, and given that @selector shouldn't really appear in
3169 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003170 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3171 Selector Sel = SelectorAndLocation.first;
3172 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003173 Writer.AddSelectorRef(Sel);
3174 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003175 }
Richard Smithe64e7452016-04-18 21:54:58 +00003176 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003177}
3178
Douglas Gregorc5046832009-04-27 18:38:38 +00003179//===----------------------------------------------------------------------===//
3180// Identifier Table Serialization
3181//===----------------------------------------------------------------------===//
3182
Richard Smithb3761912015-02-05 23:08:52 +00003183/// Determine the declaration that should be put into the name lookup table to
3184/// represent the given declaration in this module. This is usually D itself,
3185/// but if D was imported and merged into a local declaration, we want the most
3186/// recent local declaration instead. The chosen declaration will be the most
3187/// recent declaration in any module that imports this one.
3188static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3189 NamedDecl *D) {
3190 if (!LangOpts.Modules || !D->isFromASTFile())
3191 return D;
3192
3193 if (Decl *Redecl = D->getPreviousDecl()) {
3194 // For Redeclarable decls, a prior declaration might be local.
3195 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003196 // If we find a local decl, we're done.
3197 if (!Redecl->isFromASTFile()) {
3198 // Exception: in very rare cases (for injected-class-names), not all
3199 // redeclarations are in the same semantic context. Skip ones in a
3200 // different context. They don't go in this lookup table at all.
3201 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3202 D->getDeclContext()->getRedeclContext()))
3203 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003204 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003205 }
3206
Richard Smithfe620d22015-03-05 23:24:12 +00003207 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003208 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003209 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003210 break;
3211 }
3212 } else if (Decl *First = D->getCanonicalDecl()) {
3213 // For Mergeable decls, the first decl might be local.
3214 if (!First->isFromASTFile())
3215 return cast<NamedDecl>(First);
3216 }
3217
3218 // All declarations are imported. Our most recent declaration will also be
3219 // the most recent one in anyone who imports us.
3220 return D;
3221}
3222
Douglas Gregorc78d3462009-04-24 21:10:55 +00003223namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003224
Richard Smithcf97cf62015-04-19 01:34:23 +00003225class ASTIdentifierTableTrait {
3226 ASTWriter &Writer;
3227 Preprocessor &PP;
3228 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003229 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003230 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003231 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003232
3233 /// \brief Determines whether this is an "interesting" identifier that needs a
3234 /// full IdentifierInfo structure written into the hash table. Notably, this
3235 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3236 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003237 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003238 if (MacroOffset ||
3239 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003240 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003241 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003242 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003243 return true;
3244
3245 return false;
3246 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003247
Douglas Gregore84a9da2009-04-20 20:36:09 +00003248public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003249 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003250 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003251
Sebastian Redl539c5062010-08-18 23:57:32 +00003252 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003253 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003254
Justin Bogner25463f12014-04-18 20:27:24 +00003255 typedef unsigned hash_value_type;
3256 typedef unsigned offset_type;
3257
Richard Smithd7329392015-04-21 21:46:32 +00003258 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003259 IdentifierResolver &IdResolver, bool IsModule,
3260 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003261 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003262 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3263 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003264
Richard Smithd79514e2016-02-05 19:03:40 +00003265 bool needDecls() const { return NeedDecls; }
3266
Justin Bogner25463f12014-04-18 20:27:24 +00003267 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003268 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003269 }
Mike Stump11289f42009-09-09 15:08:12 +00003270
Richard Smith33e0f7e2015-07-22 02:08:40 +00003271 bool isInterestingIdentifier(const IdentifierInfo *II) {
3272 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3273 return isInterestingIdentifier(II, MacroOffset);
3274 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003275
Richard Smith9c254182015-07-19 21:41:12 +00003276 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3277 return isInterestingIdentifier(II, 0);
3278 }
3279
Mike Stump11289f42009-09-09 15:08:12 +00003280 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003281 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003282 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003283 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003284 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3285 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003286 DataLen += 2; // 2 bytes for builtin ID
3287 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003288 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003289 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003290
Richard Smitha534a312015-07-21 23:54:07 +00003291 if (NeedDecls) {
3292 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3293 DEnd = IdResolver.end();
3294 D != DEnd; ++D)
3295 DataLen += 4;
3296 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003297 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003298 using namespace llvm::support;
3299 endian::Writer<little> LE(Out);
3300
Richard Smithdf8a8312015-03-13 04:05:01 +00003301 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003302 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003303 // We emit the key length after the data length so that every
3304 // string is preceded by a 16-bit length. This matches the PTH
3305 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003306 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003307 return std::make_pair(KeyLen, DataLen);
3308 }
Mike Stump11289f42009-09-09 15:08:12 +00003309
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003310 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003311 unsigned KeyLen) {
3312 // Record the location of the key data. This is used when generating
3313 // the mapping from persistent IDs to strings.
3314 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003315
3316 // Emit the offset of the key/data length information to the interesting
3317 // identifiers table if necessary.
3318 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3319 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3320
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003321 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003322 }
Mike Stump11289f42009-09-09 15:08:12 +00003323
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003324 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003325 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003326 using namespace llvm::support;
3327 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003328
Richard Smithd7329392015-04-21 21:46:32 +00003329 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3330 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003331 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003332 return;
3333 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003334
Justin Bognere1c147c2014-03-28 22:03:19 +00003335 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003336 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3337 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003338 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003339 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003340 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003341 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003342 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3343 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003344 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003345 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003346 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003347 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003348
Richard Smithd7329392015-04-21 21:46:32 +00003349 if (HadMacroDefinition)
3350 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003351
Richard Smitha534a312015-07-21 23:54:07 +00003352 if (NeedDecls) {
3353 // Emit the declaration IDs in reverse order, because the
3354 // IdentifierResolver provides the declarations as they would be
3355 // visible (e.g., the function "stat" would come before the struct
3356 // "stat"), but the ASTReader adds declarations to the end of the list
3357 // (so we need to see the struct "stat" before the function "stat").
3358 // Only emit declarations that aren't from a chained PCH, though.
3359 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3360 IdResolver.end());
3361 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3362 DEnd = Decls.rend();
3363 D != DEnd; ++D)
3364 LE.write<uint32_t>(
3365 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3366 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003367 }
3368};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003369
Douglas Gregore84a9da2009-04-20 20:36:09 +00003370} // end anonymous namespace
3371
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003372/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003373///
3374/// The identifier table consists of a blob containing string data
3375/// (the actual identifiers themselves) and a separate "offsets" index
3376/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003377void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3378 IdentifierResolver &IdResolver,
3379 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003380 using namespace llvm;
3381
Richard Smith33e0f7e2015-07-22 02:08:40 +00003382 RecordData InterestingIdents;
3383
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003384 // Create and write out the blob that contains the identifier
3385 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003386 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003387 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003388 ASTIdentifierTableTrait Trait(
3389 *this, PP, IdResolver, IsModule,
3390 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003391
Douglas Gregore6648fb2009-04-28 20:33:11 +00003392 // Look for any identifiers that were named while processing the
3393 // headers, but are otherwise not needed. We add these to the hash
3394 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003395 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003396 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003397 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003398 for (const auto &ID : PP.getIdentifierTable())
3399 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003400 // Sort the identifiers lexicographically before getting them references so
3401 // that their order is stable.
3402 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3403 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003404 if (Trait.isInterestingNonMacroIdentifier(II))
3405 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003406
Sebastian Redlff4a2952010-07-23 23:49:55 +00003407 // Create the on-disk hash table representation. We only store offsets
3408 // for identifiers that appear here for the first time.
3409 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003410 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003411 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003412 IdentID ID = IdentIDPair.second;
3413 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003414 // Write out identifiers if either the ID is local or the identifier has
3415 // changed since it was loaded.
3416 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3417 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003418 (Trait.needDecls() &&
3419 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003420 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003421 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003422
Douglas Gregore84a9da2009-04-20 20:36:09 +00003423 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003424 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003425 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003426 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003427 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003428 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003429 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003430 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003431 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003432 }
3433
3434 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003435 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003436 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003439 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003440
3441 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003442 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003443 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003444 }
3445
3446 // Write the offsets table for identifier IDs.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003447 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003448 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003450 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3452 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3453
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003454#ifndef NDEBUG
3455 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3456 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3457#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003458
3459 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3460 IdentifierOffsets.size(),
3461 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003462 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003463 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003464
3465 // In C++, write the list of interesting identifiers (those that are
3466 // defined as macros, poisoned, or similar unusual things).
3467 if (!InterestingIdents.empty())
3468 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003469}
3470
Douglas Gregorc5046832009-04-27 18:38:38 +00003471//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003472// DeclContext's Name Lookup Table Serialization
3473//===----------------------------------------------------------------------===//
3474
3475namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003476
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003477// Trait used for the on-disk hash table used in the method pool.
3478class ASTDeclContextNameLookupTrait {
3479 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003480 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003481
3482public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003483 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003484 typedef key_type key_type_ref;
3485
Richard Smithd88a7f12015-09-01 20:35:42 +00003486 /// A start and end index into DeclIDs, representing a sequence of decls.
3487 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003488 typedef const data_type& data_type_ref;
3489
Justin Bogner25463f12014-04-18 20:27:24 +00003490 typedef unsigned hash_value_type;
3491 typedef unsigned offset_type;
3492
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003493 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3494
Richard Smithd88a7f12015-09-01 20:35:42 +00003495 template<typename Coll>
3496 data_type getData(const Coll &Decls) {
3497 unsigned Start = DeclIDs.size();
3498 for (NamedDecl *D : Decls) {
3499 DeclIDs.push_back(
3500 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3501 }
3502 return std::make_pair(Start, DeclIDs.size());
3503 }
3504
3505 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3506 unsigned Start = DeclIDs.size();
3507 for (auto ID : FromReader)
3508 DeclIDs.push_back(ID);
3509 return std::make_pair(Start, DeclIDs.size());
3510 }
3511
3512 static bool EqualKey(key_type_ref a, key_type_ref b) {
3513 return a == b;
3514 }
3515
Richard Smitha06c7e62015-08-26 23:55:49 +00003516 hash_value_type ComputeHash(DeclarationNameKey Name) {
3517 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003518 }
3519
Richard Smithd88a7f12015-09-01 20:35:42 +00003520 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3521 assert(Writer.hasChain() &&
3522 "have reference to loaded module file but no chain?");
3523
3524 using namespace llvm::support;
3525 endian::Writer<little>(Out)
3526 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3527 }
3528
Richard Smitha06c7e62015-08-26 23:55:49 +00003529 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3530 DeclarationNameKey Name,
3531 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003532 using namespace llvm::support;
3533 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003534 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003535 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003536 case DeclarationName::Identifier:
3537 case DeclarationName::ObjCZeroArgSelector:
3538 case DeclarationName::ObjCOneArgSelector:
3539 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003540 case DeclarationName::CXXLiteralOperatorName:
3541 KeyLen += 4;
3542 break;
3543 case DeclarationName::CXXOperatorName:
3544 KeyLen += 1;
3545 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003546 case DeclarationName::CXXConstructorName:
3547 case DeclarationName::CXXDestructorName:
3548 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003549 case DeclarationName::CXXUsingDirective:
3550 break;
3551 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003552 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003553
Richard Smithf02662d2015-07-30 03:17:16 +00003554 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003555 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3556 assert(uint16_t(DataLen) == DataLen &&
3557 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003558 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003559
3560 return std::make_pair(KeyLen, DataLen);
3561 }
3562
Richard Smitha06c7e62015-08-26 23:55:49 +00003563 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003564 using namespace llvm::support;
3565 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003566 LE.write<uint8_t>(Name.getKind());
3567 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003568 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003569 case DeclarationName::CXXLiteralOperatorName:
3570 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003571 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003572 case DeclarationName::ObjCZeroArgSelector:
3573 case DeclarationName::ObjCOneArgSelector:
3574 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003575 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003576 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003577 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003578 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003579 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003580 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003581 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003582 case DeclarationName::CXXConstructorName:
3583 case DeclarationName::CXXDestructorName:
3584 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003585 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003586 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003587 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003588
3589 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003590 }
3591
Richard Smitha06c7e62015-08-26 23:55:49 +00003592 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3593 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003594 using namespace llvm::support;
3595 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003596 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003597 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3598 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003599 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3600 }
3601};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003602
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003603} // end anonymous namespace
3604
Chandler Carruthe972c362015-03-26 03:11:40 +00003605bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3606 DeclContext *DC) {
3607 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3608}
3609
3610bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3611 DeclContext *DC) {
3612 for (auto *D : Result.getLookupResult())
3613 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3614 return false;
3615
3616 return true;
3617}
3618
Richard Smithd88a7f12015-09-01 20:35:42 +00003619void
Chandler Carruthe972c362015-03-26 03:11:40 +00003620ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003621 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003622 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3623 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003624 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003625
Chandler Carruthe972c362015-03-26 03:11:40 +00003626 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003627 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003628 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3629
3630 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003631 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3632 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003633 ASTDeclContextNameLookupTrait Trait(*this);
3634
Chandler Carruthe972c362015-03-26 03:11:40 +00003635 // The first step is to collect the declaration names which we need to
3636 // serialize into the name lookup table, and to collect them in a stable
3637 // order.
3638 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003639
Chandler Carruthe972c362015-03-26 03:11:40 +00003640 // We also build up small sets of the constructor and conversion function
3641 // names which are visible.
3642 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003643
Chandler Carruthe972c362015-03-26 03:11:40 +00003644 for (auto &Lookup : *DC->buildLookup()) {
3645 auto &Name = Lookup.first;
3646 auto &Result = Lookup.second;
3647
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003648 // If there are no local declarations in our lookup result, we
3649 // don't need to write an entry for the name at all. If we can't
3650 // write out a lookup set without performing more deserialization,
3651 // just skip this entry.
3652 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003653 isLookupResultEntirelyExternal(Result, DC))
3654 continue;
3655
3656 // We also skip empty results. If any of the results could be external and
3657 // the currently available results are empty, then all of the results are
3658 // external and we skip it above. So the only way we get here with an empty
3659 // results is when no results could have been external *and* we have
3660 // external results.
3661 //
3662 // FIXME: While we might want to start emitting on-disk entries for negative
3663 // lookups into a decl context as an optimization, today we *have* to skip
3664 // them because there are names with empty lookup results in decl contexts
3665 // which we can't emit in any stable ordering: we lookup constructors and
3666 // conversion functions in the enclosing namespace scope creating empty
3667 // results for them. This in almost certainly a bug in Clang's name lookup,
3668 // but that is likely to be hard or impossible to fix and so we tolerate it
3669 // here by omitting lookups with empty results.
3670 if (Lookup.second.getLookupResult().empty())
3671 continue;
3672
3673 switch (Lookup.first.getNameKind()) {
3674 default:
3675 Names.push_back(Lookup.first);
3676 break;
3677
Richard Smithcd45dbc2014-04-19 03:48:30 +00003678 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003679 assert(isa<CXXRecordDecl>(DC) &&
3680 "Cannot have a constructor name outside of a class!");
3681 ConstructorNameSet.insert(Name);
3682 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003683
3684 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003685 assert(isa<CXXRecordDecl>(DC) &&
3686 "Cannot have a conversion function name outside of a class!");
3687 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003688 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003689 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003690 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003691
Chandler Carruthe972c362015-03-26 03:11:40 +00003692 // Sort the names into a stable order.
3693 std::sort(Names.begin(), Names.end());
3694
Chandler Carruthffbf7052015-03-26 22:27:09 +00003695 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003696 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003697 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003698
Chandler Carruthffbf7052015-03-26 22:27:09 +00003699 // First we try the easy case by forming the current context's constructor
3700 // name and adding that name first. This is a very useful optimization to
3701 // avoid walking the lexical declarations in many cases, and it also
3702 // handles the only case where a constructor name can come from some other
3703 // lexical context -- when that name is an implicit constructor merged from
3704 // another declaration in the redecl chain. Any non-implicit constructor or
3705 // conversion function which doesn't occur in all the lexical contexts
3706 // would be an ODR violation.
3707 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3708 Context->getCanonicalType(Context->getRecordType(D)));
3709 if (ConstructorNameSet.erase(ImplicitCtorName))
3710 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003711
Chandler Carruthffbf7052015-03-26 22:27:09 +00003712 // If we still have constructors or conversion functions, we walk all the
3713 // names in the decl and add the constructors and conversion functions
3714 // which are visible in the order they lexically occur within the context.
3715 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3716 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3717 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3718 auto Name = ChildND->getDeclName();
3719 switch (Name.getNameKind()) {
3720 default:
3721 continue;
3722
3723 case DeclarationName::CXXConstructorName:
3724 if (ConstructorNameSet.erase(Name))
3725 Names.push_back(Name);
3726 break;
3727
3728 case DeclarationName::CXXConversionFunctionName:
3729 if (ConversionNameSet.erase(Name))
3730 Names.push_back(Name);
3731 break;
3732 }
3733
3734 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3735 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003736 }
3737
Chandler Carruthe972c362015-03-26 03:11:40 +00003738 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3739 "constructors by walking all the "
3740 "lexical members of the context.");
3741 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3742 "conversion functions by walking all "
3743 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003744 }
3745
Chandler Carruthe972c362015-03-26 03:11:40 +00003746 // Next we need to do a lookup with each name into this decl context to fully
3747 // populate any results from external sources. We don't actually use the
3748 // results of these lookups because we only want to use the results after all
3749 // results have been loaded and the pointers into them will be stable.
3750 for (auto &Name : Names)
3751 DC->lookup(Name);
3752
3753 // Now we need to insert the results for each name into the hash table. For
3754 // constructor names and conversion function names, we actually need to merge
3755 // all of the results for them into one list of results each and insert
3756 // those.
3757 SmallVector<NamedDecl *, 8> ConstructorDecls;
3758 SmallVector<NamedDecl *, 8> ConversionDecls;
3759
3760 // Now loop over the names, either inserting them or appending for the two
3761 // special cases.
3762 for (auto &Name : Names) {
3763 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3764
3765 switch (Name.getNameKind()) {
3766 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003767 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003768 break;
3769
3770 case DeclarationName::CXXConstructorName:
3771 ConstructorDecls.append(Result.begin(), Result.end());
3772 break;
3773
3774 case DeclarationName::CXXConversionFunctionName:
3775 ConversionDecls.append(Result.begin(), Result.end());
3776 break;
3777 }
3778 }
3779
3780 // Handle our two special cases if we ended up having any. We arbitrarily use
3781 // the first declaration's name here because the name itself isn't part of
3782 // the key, only the kind of name is used.
3783 if (!ConstructorDecls.empty())
3784 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003785 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003786 if (!ConversionDecls.empty())
3787 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003788 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003789
Richard Smithd88a7f12015-09-01 20:35:42 +00003790 // Create the on-disk hash table. Also emit the existing imported and
3791 // merged table if there is one.
3792 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3793 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003794}
3795
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003796/// \brief Write the block containing all of the declaration IDs
3797/// visible from the given DeclContext.
3798///
3799/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003800/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003801uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3802 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003803 // If we imported a key declaration of this namespace, write the visible
3804 // lookup results as an update record for it rather than including them
3805 // on this declaration. We will only look at key declarations on reload.
3806 if (isa<NamespaceDecl>(DC) && Chain &&
3807 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3808 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003809 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003810 Prev = Prev->getPreviousDecl())
3811 if (!Prev->isFromASTFile())
3812 return 0;
3813
3814 // Note that we need to emit an update record for the primary context.
3815 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3816
3817 // Make sure all visible decls are written. They will be recorded later. We
3818 // do this using a side data structure so we can sort the names into
3819 // a deterministic order.
3820 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3821 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3822 LookupResults;
3823 if (Map) {
3824 LookupResults.reserve(Map->size());
3825 for (auto &Entry : *Map)
3826 LookupResults.push_back(
3827 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3828 }
3829
3830 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3831 for (auto &NameAndResult : LookupResults) {
3832 DeclarationName Name = NameAndResult.first;
3833 DeclContext::lookup_result Result = NameAndResult.second;
3834 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3835 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3836 // We have to work around a name lookup bug here where negative lookup
3837 // results for these names get cached in namespace lookup tables (these
3838 // names should never be looked up in a namespace).
3839 assert(Result.empty() && "Cannot have a constructor or conversion "
3840 "function name in a namespace!");
3841 continue;
3842 }
3843
3844 for (NamedDecl *ND : Result)
3845 if (!ND->isFromASTFile())
3846 GetDeclRef(ND);
3847 }
3848
3849 return 0;
3850 }
3851
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003852 if (DC->getPrimaryContext() != DC)
3853 return 0;
3854
Chandler Carruthe972c362015-03-26 03:11:40 +00003855 // Skip contexts which don't support name lookup.
3856 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003857 return 0;
3858
3859 // If not in C++, we perform name lookup for the translation unit via the
3860 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003861 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003862 return 0;
3863
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003864 // Serialize the contents of the mapping used for lookup. Note that,
3865 // although we have two very different code paths, the serialized
3866 // representation is the same for both cases: a declaration name,
3867 // followed by a size, followed by references to the visible
3868 // declarations that have that name.
3869 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003870 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003871 if (!Map || Map->empty())
3872 return 0;
3873
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003874 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003875 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003876 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003877
3878 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003879 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003880 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003881 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003882 ++NumVisibleDeclContexts;
3883 return Offset;
3884}
3885
Sebastian Redla4071b42010-08-24 00:50:09 +00003886/// \brief Write an UPDATE_VISIBLE block for the given context.
3887///
3888/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3889/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003890/// (in C++), for namespaces, and for classes with forward-declared unscoped
3891/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003892void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003893 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003894 if (!Map || Map->empty())
3895 return;
3896
Sebastian Redla4071b42010-08-24 00:50:09 +00003897 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003898 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003899 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003900
Richard Smith5fc18a92015-07-12 23:43:21 +00003901 // If we're updating a namespace, select a key declaration as the key for the
3902 // update record; those are the only ones that will be checked on reload.
3903 if (isa<NamespaceDecl>(DC))
3904 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3905
Sebastian Redla4071b42010-08-24 00:50:09 +00003906 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003907 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003908 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003909}
3910
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003911/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3912void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003913 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003914 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3915}
3916
3917/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3918void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003919 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003920 return;
3921
3922 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3923 RecordData Record;
3924#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3925#include "clang/Basic/OpenCLExtensions.def"
3926 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3927}
3928
Douglas Gregor404cdde2012-01-27 01:47:08 +00003929void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003930 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003931 RecordData Categories;
3932
3933 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3934 unsigned Size = 0;
3935 unsigned StartIndex = Categories.size();
3936
3937 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3938
3939 // Allocate space for the size.
3940 Categories.push_back(0);
3941
3942 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003943 for (ObjCInterfaceDecl::known_categories_iterator
3944 Cat = Class->known_categories_begin(),
3945 CatEnd = Class->known_categories_end();
3946 Cat != CatEnd; ++Cat, ++Size) {
3947 assert(getDeclID(*Cat) != 0 && "Bogus category");
3948 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003949 }
3950
3951 // Update the size.
3952 Categories[StartIndex] = Size;
3953
3954 // Record this interface -> category map.
3955 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3956 CategoriesMap.push_back(CatInfo);
3957 }
3958
3959 // Sort the categories map by the definition ID, since the reader will be
3960 // performing binary searches on this information.
3961 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3962
3963 // Emit the categories map.
3964 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003965
3966 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003967 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3970 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00003971
3972 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
3973 Stream.EmitRecordWithBlob(AbbrevID, Record,
3974 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00003975 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00003976
Douglas Gregor404cdde2012-01-27 01:47:08 +00003977 // Emit the category lists.
3978 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3979}
3980
Richard Smithe40f2ba2013-08-07 21:41:30 +00003981void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3982 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3983
3984 if (LPTMap.empty())
3985 return;
3986
3987 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00003988 for (auto LPTMapEntry : LPTMap) {
3989 const FunctionDecl *FD = LPTMapEntry.first;
3990 LateParsedTemplate *LPT = LPTMapEntry.second;
3991 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003992 AddDeclRef(LPT->D, Record);
3993 Record.push_back(LPT->Toks.size());
3994
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003995 for (const auto &Tok : LPT->Toks) {
3996 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003997 }
3998 }
3999 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4000}
4001
Dario Domizioli13a0a382014-05-23 12:13:25 +00004002/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4003void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4004 RecordData Record;
4005 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4006 AddSourceLocation(PragmaLoc, Record);
4007 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4008}
4009
Nico Weber779355f2016-03-02 23:22:00 +00004010/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4011void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4012 RecordData Record;
4013 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4014 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4015}
4016
Nico Weber42932312016-03-03 00:17:35 +00004017/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4018//module.
4019void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4020 RecordData Record;
4021 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4022 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4023 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4024}
4025
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004026void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4027 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004028 // Enter the extension block.
4029 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4030
4031 // Emit the metadata record abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004032 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004033 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4034 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4035 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4036 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4037 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4038 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4039 unsigned Abbrev = Stream.EmitAbbrev(Abv);
4040
4041 // Emit the metadata record.
4042 RecordData Record;
4043 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4044 Record.push_back(EXTENSION_METADATA);
4045 Record.push_back(Metadata.MajorVersion);
4046 Record.push_back(Metadata.MinorVersion);
4047 Record.push_back(Metadata.BlockName.size());
4048 Record.push_back(Metadata.UserInfo.size());
4049 SmallString<64> Buffer;
4050 Buffer += Metadata.BlockName;
4051 Buffer += Metadata.UserInfo;
4052 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4053
4054 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004055 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004056
4057 // Exit the extension block.
4058 Stream.ExitBlock();
4059}
4060
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004061//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004062// General Serialization Routines
4063//===----------------------------------------------------------------------===//
4064
Richard Smith69c82bf2016-04-01 22:52:03 +00004065/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004066void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4067 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004068 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004069 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004070 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004071 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004072
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004073#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004074
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004075 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004076}
4077
John McCallf413f5e2013-05-03 00:10:13 +00004078void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4079 AddSourceLocation(Tok.getLocation(), Record);
4080 Record.push_back(Tok.getLength());
4081
4082 // FIXME: When reading literal tokens, reconstruct the literal pointer
4083 // if it is needed.
4084 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4085 // FIXME: Should translate token kind to a stable encoding.
4086 Record.push_back(Tok.getKind());
4087 // FIXME: Should translate token flags to a stable encoding.
4088 Record.push_back(Tok.getFlags());
4089}
4090
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004091void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004092 Record.push_back(Str.size());
4093 Record.insert(Record.end(), Str.begin(), Str.end());
4094}
4095
Richard Smith7ed1bc92014-12-05 22:42:13 +00004096bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004097 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004098
Richard Smith54cc3c22014-12-11 20:50:24 +00004099 bool Changed =
4100 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004101
4102 // Remove a prefix to make the path relative, if relevant.
4103 const char *PathBegin = Path.data();
4104 const char *PathPtr =
4105 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4106 if (PathPtr != PathBegin) {
4107 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4108 Changed = true;
4109 }
4110
4111 return Changed;
4112}
4113
4114void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4115 SmallString<128> FilePath(Path);
4116 PreparePathForOutput(FilePath);
4117 AddString(FilePath, Record);
4118}
4119
Mehdi Amini57a41912015-09-10 01:46:39 +00004120void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004121 StringRef Path) {
4122 SmallString<128> FilePath(Path);
4123 PreparePathForOutput(FilePath);
4124 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4125}
4126
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004127void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4128 RecordDataImpl &Record) {
4129 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004130 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004131 Record.push_back(*Minor + 1);
4132 else
4133 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004134 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004135 Record.push_back(*Subminor + 1);
4136 else
4137 Record.push_back(0);
4138}
4139
Douglas Gregore84a9da2009-04-20 20:36:09 +00004140/// \brief Note that the identifier II occurs at the given offset
4141/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004142void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004143 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004144 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004145 // up earlier in the chain and thus don't need an offset.
4146 if (ID >= FirstIdentID)
4147 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004148}
4149
Douglas Gregor95c13f52009-04-25 17:48:32 +00004150/// \brief Note that the selector Sel occurs at the given offset
4151/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004152void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004153 unsigned ID = SelectorIDs[Sel];
4154 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004155 // Don't record offsets for selectors that are also available in a different
4156 // file.
4157 if (ID < FirstSelectorID)
4158 return;
4159 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004160}
4161
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004162ASTWriter::ASTWriter(
4163 llvm::BitstreamWriter &Stream,
4164 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
4165 bool IncludeTimestamps)
Richard Smith01b2cb42014-07-26 06:37:51 +00004166 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004167 WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
4168 WritingAST(false), DoneWritingDeclsAndTypes(false),
4169 ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
4170 NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
4171 NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
4172 NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
4173 NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
Richard Smith01b2cb42014-07-26 06:37:51 +00004174 NextSubmoduleID(FirstSubmoduleID),
4175 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Richard Smith290d8012016-04-06 17:06:00 +00004176 NumStatements(0), NumMacros(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004177 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004178 TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004179 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004180 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004181 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004182 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4183 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004184 ExprImplicitCastAbbrev(0) {
4185 for (const auto &Ext : Extensions) {
4186 if (auto Writer = Ext->createExtensionWriter(*this))
4187 ModuleFileExtensionWriters.push_back(std::move(Writer));
4188 }
4189}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004190
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004191ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004192 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004193}
4194
Richard Smithb3761912015-02-05 23:08:52 +00004195const LangOptions &ASTWriter::getLangOpts() const {
4196 assert(WritingAST && "can't determine lang opts when not writing AST");
4197 return Context->getLangOpts();
4198}
4199
Richard Smithe75ee0f2015-08-17 07:13:32 +00004200time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4201 return IncludeTimestamps ? E->getModificationTime() : 0;
4202}
4203
Adrian Prantladbd2b12015-09-22 23:26:31 +00004204uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4205 Module *WritingModule, StringRef isysroot,
4206 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004207 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004208
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004209 ASTHasCompilerErrors = hasErrors;
4210
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004211 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004212 Stream.Emit((unsigned)'C', 8);
4213 Stream.Emit((unsigned)'P', 8);
4214 Stream.Emit((unsigned)'C', 8);
4215 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004216
Chris Lattner28fa4e62009-04-26 22:26:21 +00004217 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004218
Douglas Gregoreda8e122011-08-09 15:13:55 +00004219 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004220 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004221 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004222 ASTFileSignature Signature =
4223 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004224 Context = nullptr;
4225 PP = nullptr;
4226 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004227 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004228
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004229 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004230 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004231}
4232
Douglas Gregora94a1542011-07-27 21:45:57 +00004233template<typename Vector>
4234static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4235 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004236 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4237 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004238 Writer.AddDeclRef(*I, Record);
4239 }
4240}
4241
Adrian Prantladbd2b12015-09-22 23:26:31 +00004242uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4243 const std::string &OutputFile,
4244 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004245 using namespace llvm;
4246
Craig Toppera13603a2014-05-22 05:54:18 +00004247 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004248
Douglas Gregorcf68c582011-12-01 22:20:10 +00004249 // Make sure that the AST reader knows to finalize itself.
4250 if (Chain)
4251 Chain->finalizeForWriting();
4252
Sebastian Redl143413f2010-07-12 22:02:52 +00004253 ASTContext &Context = SemaRef.Context;
4254 Preprocessor &PP = SemaRef.PP;
4255
Douglas Gregordab42432011-08-12 00:15:20 +00004256 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004257 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4258 if (D) {
4259 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4260 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004261 }
4262 };
4263 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4264 PREDEF_DECL_TRANSLATION_UNIT_ID);
4265 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4266 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4267 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4268 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4269 PREDEF_DECL_OBJC_PROTOCOL_ID);
4270 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4271 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4272 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4273 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4274 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004275 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004276 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4277 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004278 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004279 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4280 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004281 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4282 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004283 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4284 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004285 RegisterPredefDecl(Context.TypePackElementDecl,
4286 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004287
Chris Lattner0c797362009-09-08 18:19:27 +00004288 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004289 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004290 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004291 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004292 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004293
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004294 // Build a record containing all of the file scoped decls in this file.
4295 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004296 if (!isModule)
4297 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4298 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004299
Douglas Gregor851443c2011-08-12 01:39:19 +00004300 // Build a record containing all of the delegating constructors we still need
4301 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004302 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004303 if (!isModule)
4304 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004305
Douglas Gregor851443c2011-08-12 01:39:19 +00004306 // Write the set of weak, undeclared identifiers. We always write the
4307 // entire table, since later PCH files in a PCH chain are only interested in
4308 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004309 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004310 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4311 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4312 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4313 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4314 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4315 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4316 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004317 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004318
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004319 // Build a record containing all of the ext_vector declarations.
4320 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004321 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004322
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004323 // Build a record containing all of the VTable uses information.
4324 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004325 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004326 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4327 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4328 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4329 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4330 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004331 }
4332
Nico Weber72889432014-09-06 01:25:55 +00004333 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4334 RecordData UnusedLocalTypedefNameCandidates;
4335 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4336 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4337
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004338 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004339 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004340 for (const auto &I : SemaRef.PendingInstantiations) {
4341 AddDeclRef(I.first, PendingInstantiations);
4342 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004343 }
4344 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4345 "There are local ones at end of translation unit!");
4346
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004347 // Build a record containing some declaration references.
4348 RecordData SemaDeclRefs;
4349 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4350 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4351 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4352 }
4353
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004354 RecordData CUDASpecialDeclRefs;
4355 if (Context.getcudaConfigureCallDecl()) {
4356 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4357 }
4358
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004359 // Build a record containing all of the known namespaces.
4360 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004361 for (const auto &I : SemaRef.KnownNamespaces) {
4362 if (!I.second)
4363 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004364 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004365
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004366 // Build a record of all used, undefined objects that require definitions.
4367 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004368
4369 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004370 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004371 for (const auto &I : Undefined) {
4372 AddDeclRef(I.first, UndefinedButUsed);
4373 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004374 }
4375
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004376 // Build a record containing all delete-expressions that we would like to
4377 // analyze later in AST.
4378 RecordData DeleteExprsToAnalyze;
4379
4380 for (const auto &DeleteExprsInfo :
4381 SemaRef.getMismatchingDeleteExpressions()) {
4382 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4383 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4384 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4385 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4386 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4387 }
4388 }
4389
Douglas Gregor112b9072012-10-18 05:31:06 +00004390 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004391 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004392
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004393 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004394 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004395
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004396 // This is so that older clang versions, before the introduction
4397 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004398 {
4399 RecordData Record = {VERSION_MAJOR};
4400 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4401 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004402
Douglas Gregor851443c2011-08-12 01:39:19 +00004403 // Create a lexical update block containing all of the declarations in the
4404 // translation unit that do not come from other AST files.
4405 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004406 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4407 for (const auto *D : TU->noload_decls()) {
4408 if (!D->isFromASTFile()) {
4409 NewGlobalKindDeclPairs.push_back(D->getKind());
4410 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4411 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004412 }
4413
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004414 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor851443c2011-08-12 01:39:19 +00004415 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4416 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4417 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
Mehdi Amini57a41912015-09-10 01:46:39 +00004418 {
4419 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4420 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4421 bytes(NewGlobalKindDeclPairs));
4422 }
4423
Douglas Gregor851443c2011-08-12 01:39:19 +00004424 // And a visible updates block for the translation unit.
4425 Abv = new llvm::BitCodeAbbrev();
4426 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4427 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004428 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4429 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4430 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004431
4432 // If we have any extern "C" names, write out a visible update for them.
4433 if (Context.ExternCContext)
4434 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004435
4436 // If the translation unit has an anonymous namespace, and we don't already
4437 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004438 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004439 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4440 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004441 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004442 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004443 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004444
Richard Smith5652c0f2014-03-21 01:48:23 +00004445 // Add update records for all mangling numbers and static local numbers.
4446 // These aren't really update records, but this is a convenient way of
4447 // tagging this rare extra data onto the declarations.
4448 for (const auto &Number : Context.MangleNumbers)
4449 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004450 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4451 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004452 for (const auto &Number : Context.StaticLocalNumbers)
4453 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004454 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4455 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004456
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004457 // Make sure visible decls, added to DeclContexts previously loaded from
4458 // an AST file, are registered for serialization.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004459 for (const auto *I : UpdatingVisibleDecls) {
4460 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004461 }
4462
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004463 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004464 // serialization, if we're storing decls with identifiers.
4465 if (!WritingModule || !getLangOpts().CPlusPlus) {
4466 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004467 for (const auto &ID : PP.getIdentifierTable()) {
4468 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004469 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4470 IIs.push_back(II);
4471 }
4472 // Sort the identifiers to visit based on their name.
4473 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4474 for (const IdentifierInfo *II : IIs) {
4475 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4476 DEnd = SemaRef.IdResolver.end();
4477 D != DEnd; ++D) {
4478 GetDeclRef(*D);
4479 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004480 }
4481 }
4482
Manman Rena0f31a02016-04-29 19:04:05 +00004483 // For method pool in the module, if it contains an entry for a selector,
4484 // the entry should be complete, containing everything introduced by that
4485 // module and all modules it imports. It's possible that the entry is out of
4486 // date, so we need to pull in the new content here.
4487
4488 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4489 // safe, we copy all selectors out.
4490 llvm::SmallVector<Selector, 256> AllSelectors;
4491 for (auto &SelectorAndID : SelectorIDs)
4492 AllSelectors.push_back(SelectorAndID.first);
4493 for (auto &Selector : AllSelectors)
4494 SemaRef.updateOutOfDateSelector(Selector);
4495
Douglas Gregor5204bde2011-08-02 16:26:37 +00004496 // Form the record of special types.
4497 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004498 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004499 AddTypeRef(Context.getFILEType(), SpecialTypes);
4500 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4501 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4502 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4503 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004504 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004505 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004506
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004507 if (Chain) {
4508 // Write the mapping information describing our module dependencies and how
4509 // each of those modules were mapped into our own offset/ID space, so that
4510 // the reader can build the appropriate mapping to its own offset/ID space.
4511 // The map consists solely of a blob with the following format:
4512 // *(module-name-len:i16 module-name:len*i8
4513 // source-location-offset:i32
4514 // identifier-id:i32
4515 // preprocessed-entity-id:i32
4516 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004517 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004518 // selector-id:i32
4519 // declaration-id:i32
4520 // c++-base-specifiers-id:i32
4521 // type-id:i32)
4522 //
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004523 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004524 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4526 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004527 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004528 {
4529 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004530 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004531 using namespace llvm::support;
4532 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004533 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004534 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004535 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004536
Ben Langmuir785180e2014-10-20 16:27:30 +00004537 // Note: if a base ID was uint max, it would not be possible to load
4538 // another module after it or have more than one entity inside it.
4539 uint32_t None = std::numeric_limits<uint32_t>::max();
4540
4541 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4542 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4543 if (ShouldWrite)
4544 LE.write<uint32_t>(BaseID);
4545 else
4546 LE.write<uint32_t>(None);
4547 };
4548
Ben Langmuirfe971d92014-08-16 04:54:18 +00004549 // These values should be unique within a chain, since they will be read
4550 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004551 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4552 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4553 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4554 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4555 M->NumPreprocessedEntities);
4556 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4557 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4558 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4559 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004560 }
4561 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004562 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004563 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4564 Buffer.data(), Buffer.size());
4565 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004566
Richard Smithb9eab6d2014-03-20 19:44:17 +00004567 RecordData DeclUpdatesOffsetsRecord;
4568
Richard Smith59442b42014-03-20 20:07:19 +00004569 // Keep writing types, declarations, and declaration update records
4570 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004571 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4572 WriteTypeAbbrevs();
4573 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004574 do {
4575 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4576 while (!DeclTypesToEmit.empty()) {
4577 DeclOrType DOT = DeclTypesToEmit.front();
4578 DeclTypesToEmit.pop();
4579 if (DOT.isType())
4580 WriteType(DOT.getType());
4581 else
4582 WriteDecl(Context, DOT.getDecl());
4583 }
4584 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004585 Stream.ExitBlock();
4586
Richard Smithb9eab6d2014-03-20 19:44:17 +00004587 DoneWritingDeclsAndTypes = true;
4588
4589 // These things can only be done once we've written out decls and types.
4590 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004591 if (!DeclUpdatesOffsetsRecord.empty())
4592 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004593 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004594 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004595 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004596 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004597 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004598 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004599 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004600 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004601 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004602 WriteFPPragmaOptions(SemaRef.getFPOptions());
4603 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004604 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004605
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004606 // If we're emitting a module, write out the submodule information.
4607 if (WritingModule)
4608 WriteSubmodules(WritingModule);
Richard Smithdc1f0422016-07-20 19:10:16 +00004609 else if (!getLangOpts().CurrentModule.empty()) {
4610 // If we're building a PCH in the implementation of a module, we may need
4611 // the description of the current module.
4612 //
4613 // FIXME: We may need other modules that we did not load from an AST file,
4614 // such as if a module declares a 'conflicts' on a different module.
4615 Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
4616 getLangOpts().CurrentModule);
4617 if (M && !M->IsFromModuleFile)
4618 WriteSubmodules(M);
4619 }
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004620
Douglas Gregor5204bde2011-08-02 16:26:37 +00004621 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4622
Douglas Gregord4df8652009-04-22 22:02:47 +00004623 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004624 if (!EagerlyDeserializedDecls.empty())
4625 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004626
4627 // Write the record containing tentative definitions.
4628 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004629 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004630
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004631 // Write the record containing unused file scoped decls.
4632 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004633 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004634
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004635 // Write the record containing weak undeclared identifiers.
4636 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004637 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004638 WeakUndeclaredIdentifiers);
4639
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004640 // Write the record containing ext_vector type names.
4641 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004642 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004643
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004644 // Write the record containing VTable uses information.
4645 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004646 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004647
Nico Weber72889432014-09-06 01:25:55 +00004648 // Write the record containing potentially unused local typedefs.
4649 if (!UnusedLocalTypedefNameCandidates.empty())
4650 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4651 UnusedLocalTypedefNameCandidates);
4652
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004653 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004654 if (!PendingInstantiations.empty())
4655 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004656
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004657 // Write the record containing declaration references of Sema.
4658 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004659 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004660
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004661 // Write the record containing CUDA-specific declaration references.
4662 if (!CUDASpecialDeclRefs.empty())
4663 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004664
4665 // Write the delegating constructors.
4666 if (!DelegatingCtorDecls.empty())
4667 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004668
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004669 // Write the known namespaces.
4670 if (!KnownNamespaces.empty())
4671 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004672
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004673 // Write the undefined internal functions and variables, and inline functions.
4674 if (!UndefinedButUsed.empty())
4675 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004676
4677 if (!DeleteExprsToAnalyze.empty())
4678 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4679
Douglas Gregor851443c2011-08-12 01:39:19 +00004680 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004681 for (auto *DC : UpdatedDeclContexts)
4682 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004683
Douglas Gregor959bb062011-12-03 01:15:29 +00004684 if (!WritingModule) {
4685 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004686 struct ModuleInfo {
4687 uint64_t ID;
4688 Module *M;
4689 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4690 };
Richard Smith56be7542014-03-21 00:33:59 +00004691 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004692 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004693 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004694 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4695 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004696 }
Richard Smith56be7542014-03-21 00:33:59 +00004697
4698 if (!Imports.empty()) {
4699 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4700 return A.ID < B.ID;
4701 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004702 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4703 return A.ID == B.ID;
4704 };
Richard Smith56be7542014-03-21 00:33:59 +00004705
4706 // Sort and deduplicate module IDs.
4707 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004708 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004709 Imports.end());
4710
4711 RecordData ImportedModules;
4712 for (const auto &Import : Imports) {
4713 ImportedModules.push_back(Import.ID);
4714 // FIXME: If the module has macros imported then later has declarations
4715 // imported, this location won't be the right one as a location for the
4716 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004717 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004718 }
4719
Douglas Gregor959bb062011-12-03 01:15:29 +00004720 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4721 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004722 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004723
Douglas Gregor404cdde2012-01-27 01:47:08 +00004724 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004725 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004726 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004727 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004728 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004729 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004730
Douglas Gregor08f01292009-04-17 22:13:46 +00004731 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004732 RecordData::value_type Record[] = {
4733 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004734 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004735 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004736
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004737 // Write the module file extension blocks.
4738 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004739 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004740
Adrian Prantladbd2b12015-09-22 23:26:31 +00004741 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004742}
4743
Richard Smithb9eab6d2014-03-20 19:44:17 +00004744void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004745 if (DeclUpdates.empty())
4746 return;
4747
Richard Smith59442b42014-03-20 20:07:19 +00004748 DeclUpdateMap LocalUpdates;
4749 LocalUpdates.swap(DeclUpdates);
4750
Richard Smith6ef42932014-03-20 21:02:00 +00004751 for (auto &DeclUpdate : LocalUpdates) {
4752 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004753
Richard Smithd28ac5b2014-03-22 23:33:22 +00004754 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004755 RecordData RecordData;
4756 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004757 for (auto &Update : DeclUpdate.second) {
4758 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4759
Richard Smith69c82bf2016-04-01 22:52:03 +00004760 // An updated body is emitted last, so that the reader doesn't need
4761 // to skip over the lazy body to reach statements for other records.
4762 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4763 HasUpdatedBody = true;
4764 else
4765 Record.push_back(Kind);
4766
Richard Smith6ef42932014-03-20 21:02:00 +00004767 switch (Kind) {
4768 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4769 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4770 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004771 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004772 Record.push_back(GetDeclRef(Update.getDecl()));
4773 break;
4774
Richard Smith4d235792014-08-07 18:53:08 +00004775 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004776 break;
4777
Richard Smith4d235792014-08-07 18:53:08 +00004778 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004779 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004780 break;
4781
John McCall32791cc2016-01-06 22:34:54 +00004782 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004783 Record.AddStmt(const_cast<Expr *>(
4784 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004785 break;
4786
Richard Smith4b054b22016-08-24 21:25:37 +00004787 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4788 Record.AddStmt(
4789 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4790 break;
4791
Richard Smithcd45dbc2014-04-19 03:48:30 +00004792 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4793 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004794 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004795 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004796 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004797 *Context, const_cast<CXXRecordDecl *>(RD)));
4798
4799 // This state is sometimes updated by template instantiation, when we
4800 // switch from the specialization referring to the template declaration
4801 // to it referring to the template definition.
4802 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4803 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004804 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004805 } else {
4806 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4807 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004808 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004809
4810 // The instantiation might have been resolved to a partial
4811 // specialization. If so, record which one.
4812 auto From = Spec->getInstantiatedFrom();
4813 if (auto PartialSpec =
4814 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4815 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004816 Record.AddDeclRef(PartialSpec);
4817 Record.AddTemplateArgumentList(
4818 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004819 } else {
4820 Record.push_back(false);
4821 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004822 }
4823 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004824 Record.AddSourceLocation(RD->getLocation());
4825 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004826 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004827
4828 // Instantiation may change attributes; write them all out afresh.
4829 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004830 if (D->hasAttrs())
4831 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004832
4833 // FIXME: Ensure we don't get here for explicit instantiations.
4834 break;
4835 }
4836
Richard Smithf8134002015-03-10 01:41:22 +00004837 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004838 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004839 break;
4840
Richard Smith564417a2014-03-20 21:47:22 +00004841 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4842 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004843 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4844 Record);
4845 break;
4846
Richard Smith6ef42932014-03-20 21:02:00 +00004847 case UPD_CXX_DEDUCED_RETURN_TYPE:
4848 Record.push_back(GetOrCreateTypeID(Update.getType()));
4849 break;
4850
4851 case UPD_DECL_MARKED_USED:
4852 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004853
4854 case UPD_MANGLING_NUMBER:
4855 case UPD_STATIC_LOCAL_NUMBER:
4856 Record.push_back(Update.getNumber());
4857 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004858
Alexey Bataev97720002014-11-11 04:05:39 +00004859 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004860 Record.AddSourceRange(
4861 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004862 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004863
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004864 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4865 Record.AddSourceRange(
4866 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4867 break;
4868
Richard Smith65ebb4a2015-03-26 04:09:53 +00004869 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004870 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004871 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004872
4873 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004874 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004875 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004876 }
4877 }
4878
Richard Smithd28ac5b2014-03-22 23:33:22 +00004879 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004880 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004881 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004882 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004883 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004884 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004885 }
4886
Richard Smithcd45dbc2014-04-19 03:48:30 +00004887 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004888 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004889 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004890}
4891
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004892void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004893 uint32_t Raw = Loc.getRawEncoding();
4894 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004895}
4896
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004897void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004898 AddSourceLocation(Range.getBegin(), Record);
4899 AddSourceLocation(Range.getEnd(), Record);
4900}
4901
Richard Smithf144b542016-04-08 21:54:32 +00004902void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4903 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004904 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004905 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004906}
4907
Richard Smithf144b542016-04-08 21:54:32 +00004908void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4909 Record->push_back(Value.isUnsigned());
4910 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004911}
4912
Richard Smithf144b542016-04-08 21:54:32 +00004913void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4914 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00004915}
4916
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004917void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004918 Record.push_back(getIdentifierRef(II));
4919}
4920
Sebastian Redl539c5062010-08-18 23:57:32 +00004921IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004922 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004923 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004924
Sebastian Redl539c5062010-08-18 23:57:32 +00004925 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004926 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004927 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004928 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004929}
4930
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004931MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004932 // Don't emit builtin macros like __LINE__ to the AST file unless they
4933 // have been redefined by the header (in which case they are not
4934 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004935 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004936 return 0;
4937
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004938 MacroID &ID = MacroIDs[MI];
4939 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004940 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004941 MacroInfoToEmitData Info = { Name, MI, ID };
4942 MacroInfosToEmit.push_back(Info);
4943 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004944 return ID;
4945}
4946
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004947MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004948 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004949 return 0;
4950
4951 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4952 return MacroIDs[MI];
4953}
4954
4955uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004956 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004957}
4958
Richard Smithe64e7452016-04-18 21:54:58 +00004959void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
4960 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00004961}
4962
Sebastian Redl539c5062010-08-18 23:57:32 +00004963SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004964 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004965 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004966 }
4967
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004968 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004969 if (SID == 0 && Chain) {
4970 // This might trigger a ReadSelector callback, which will set the ID for
4971 // this selector.
4972 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004973 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004974 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004975 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004976 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004977 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004978 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004979 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004980}
4981
Richard Smithe64e7452016-04-18 21:54:58 +00004982void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
4983 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00004984}
4985
Richard Smith290d8012016-04-06 17:06:00 +00004986void ASTRecordWriter::AddTemplateArgumentLocInfo(
4987 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004988 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004989 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004990 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004991 break;
4992 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00004993 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00004994 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004995 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00004996 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
4997 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004998 break;
4999 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005000 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5001 AddSourceLocation(Arg.getTemplateNameLoc());
5002 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005003 break;
John McCall0ad16662009-10-29 08:12:44 +00005004 case TemplateArgument::Null:
5005 case TemplateArgument::Integral:
5006 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005007 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005008 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005009 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005010 break;
5011 }
5012}
5013
Richard Smith290d8012016-04-06 17:06:00 +00005014void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5015 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005016
5017 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5018 bool InfoHasSameExpr
5019 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005020 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005021 if (InfoHasSameExpr)
5022 return; // Avoid storing the same expr twice.
5023 }
Richard Smith290d8012016-04-06 17:06:00 +00005024 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005025}
5026
Richard Smith290d8012016-04-06 17:06:00 +00005027void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005028 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005029 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005030 return;
5031 }
5032
Richard Smith290d8012016-04-06 17:06:00 +00005033 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005034}
5035
Richard Smith290d8012016-04-06 17:06:00 +00005036void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5037 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005038
Richard Smith290d8012016-04-06 17:06:00 +00005039 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005040 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005041 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005042}
5043
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005044void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005045 Record.push_back(GetOrCreateTypeID(T));
5046}
5047
Richard Smith2095ffe2015-03-16 20:11:03 +00005048TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005049 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005050 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5051 if (T.isNull())
5052 return TypeIdx();
5053 assert(!T.getLocalFastQualifiers());
5054
5055 TypeIdx &Idx = TypeIdxs[T];
5056 if (Idx.getIndex() == 0) {
5057 if (DoneWritingDeclsAndTypes) {
5058 assert(0 && "New type seen after serializing all the types to emit!");
5059 return TypeIdx();
5060 }
5061
5062 // We haven't seen this type before. Assign it a new ID and put it
5063 // into the queue of types to emit.
5064 Idx = TypeIdx(NextTypeID++);
5065 DeclTypesToEmit.push(T);
5066 }
5067 return Idx;
5068 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005069}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005070
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005071TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005072 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005073 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5074 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005075 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005076 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005077
Richard Smith2095ffe2015-03-16 20:11:03 +00005078 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5079 assert(I != TypeIdxs.end() && "Type not emitted!");
5080 return I->second;
5081 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005082}
5083
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005084void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005085 Record.push_back(GetDeclRef(D));
5086}
5087
Sebastian Redl539c5062010-08-18 23:57:32 +00005088DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005089 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005090
5091 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005092 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005093 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005094
5095 // If D comes from an AST file, its declaration ID is already known and
5096 // fixed.
5097 if (D->isFromASTFile())
5098 return D->getGlobalID();
5099
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005100 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005101 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005102 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005103 if (DoneWritingDeclsAndTypes) {
5104 assert(0 && "New decl seen after serializing all the decls to emit!");
5105 return 0;
5106 }
5107
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005108 // We haven't seen this declaration before. Give it a new ID and
5109 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005110 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005111 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005112 }
5113
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005114 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005115}
5116
Sebastian Redl539c5062010-08-18 23:57:32 +00005117DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005118 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005119 return 0;
5120
Douglas Gregorb3163e52012-01-05 22:33:30 +00005121 // If D comes from an AST file, its declaration ID is already known and
5122 // fixed.
5123 if (D->isFromASTFile())
5124 return D->getGlobalID();
5125
Douglas Gregore84a9da2009-04-20 20:36:09 +00005126 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5127 return DeclIDs[D];
5128}
5129
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005130void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005131 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005132 assert(D);
5133
5134 SourceLocation Loc = D->getLocation();
5135 if (Loc.isInvalid())
5136 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005137
5138 // We only keep track of the file-level declarations of each file.
5139 if (!D->getLexicalDeclContext()->isFileContext())
5140 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005141 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5142 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005143 if (isa<ParmVarDecl>(D))
5144 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005145
5146 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005147 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005148 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005149 FileID FID;
5150 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005151 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005152 if (FID.isInvalid())
5153 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005154 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005155
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005156 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005157 if (!Info)
5158 Info = new DeclIDInFileInfo();
5159
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005160 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005161 LocDeclIDsTy &Decls = Info->DeclIDs;
5162
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005163 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005164 Decls.push_back(LocDecl);
5165 return;
5166 }
5167
Benjamin Kramer45025c02013-08-24 13:22:59 +00005168 LocDeclIDsTy::iterator I =
5169 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005170
5171 Decls.insert(I, LocDecl);
5172}
5173
Richard Smithe64e7452016-04-18 21:54:58 +00005174void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005175 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005176 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005177 switch (Name.getNameKind()) {
5178 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005179 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005180 break;
5181
5182 case DeclarationName::ObjCZeroArgSelector:
5183 case DeclarationName::ObjCOneArgSelector:
5184 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005185 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005186 break;
5187
5188 case DeclarationName::CXXConstructorName:
5189 case DeclarationName::CXXDestructorName:
5190 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005191 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005192 break;
5193
5194 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005195 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005196 break;
5197
Alexis Hunt3d221f22009-11-29 07:34:05 +00005198 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005199 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005200 break;
5201
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005202 case DeclarationName::CXXUsingDirective:
5203 // No extra data to emit
5204 break;
5205 }
5206}
Chris Lattnerca025db2010-05-07 21:43:38 +00005207
Richard Smithd08aeb62014-08-28 01:33:39 +00005208unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5209 assert(needsAnonymousDeclarationNumber(D) &&
5210 "expected an anonymous declaration");
5211
5212 // Number the anonymous declarations within this context, if we've not
5213 // already done so.
5214 auto It = AnonymousDeclarationNumbers.find(D);
5215 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005216 auto *DC = D->getLexicalDeclContext();
5217 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5218 AnonymousDeclarationNumbers[ND] = Number;
5219 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005220
5221 It = AnonymousDeclarationNumbers.find(D);
5222 assert(It != AnonymousDeclarationNumbers.end() &&
5223 "declaration not found within its lexical context");
5224 }
5225
5226 return It->second;
5227}
5228
Richard Smith290d8012016-04-06 17:06:00 +00005229void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5230 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005231 switch (Name.getNameKind()) {
5232 case DeclarationName::CXXConstructorName:
5233 case DeclarationName::CXXDestructorName:
5234 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005235 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005236 break;
5237
5238 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005239 AddSourceLocation(SourceLocation::getFromRawEncoding(
5240 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005241 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005242 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005243 break;
5244
5245 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005246 AddSourceLocation(SourceLocation::getFromRawEncoding(
5247 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005248 break;
5249
5250 case DeclarationName::Identifier:
5251 case DeclarationName::ObjCZeroArgSelector:
5252 case DeclarationName::ObjCOneArgSelector:
5253 case DeclarationName::ObjCMultiArgSelector:
5254 case DeclarationName::CXXUsingDirective:
5255 break;
5256 }
5257}
5258
Richard Smith290d8012016-04-06 17:06:00 +00005259void ASTRecordWriter::AddDeclarationNameInfo(
5260 const DeclarationNameInfo &NameInfo) {
5261 AddDeclarationName(NameInfo.getName());
5262 AddSourceLocation(NameInfo.getLoc());
5263 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005264}
5265
Richard Smith290d8012016-04-06 17:06:00 +00005266void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5267 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5268 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005269 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005270 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005271}
5272
Richard Smithe64e7452016-04-18 21:54:58 +00005273void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005274 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005275 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005276 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005277
5278 // Push each of the NNS's onto a stack for serialization in reverse order.
5279 while (NNS) {
5280 NestedNames.push_back(NNS);
5281 NNS = NNS->getPrefix();
5282 }
5283
Richard Smithe64e7452016-04-18 21:54:58 +00005284 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005285 while(!NestedNames.empty()) {
5286 NNS = NestedNames.pop_back_val();
5287 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005288 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005289 switch (Kind) {
5290 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005291 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005292 break;
5293
5294 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005295 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005296 break;
5297
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005298 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005299 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005300 break;
5301
Chris Lattnerca025db2010-05-07 21:43:38 +00005302 case NestedNameSpecifier::TypeSpec:
5303 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005304 AddTypeRef(QualType(NNS->getAsType(), 0));
5305 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005306 break;
5307
5308 case NestedNameSpecifier::Global:
5309 // Don't need to write an associated value.
5310 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005311
5312 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005313 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005314 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005315 }
5316 }
5317}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005318
Richard Smith290d8012016-04-06 17:06:00 +00005319void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005320 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005321 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005322 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005323
5324 // Push each of the nested-name-specifiers's onto a stack for
5325 // serialization in reverse order.
5326 while (NNS) {
5327 NestedNames.push_back(NNS);
5328 NNS = NNS.getPrefix();
5329 }
5330
Richard Smith290d8012016-04-06 17:06:00 +00005331 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005332 while(!NestedNames.empty()) {
5333 NNS = NestedNames.pop_back_val();
5334 NestedNameSpecifier::SpecifierKind Kind
5335 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005336 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005337 switch (Kind) {
5338 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005339 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5340 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005341 break;
5342
5343 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005344 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5345 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005346 break;
5347
5348 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005349 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5350 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005351 break;
5352
5353 case NestedNameSpecifier::TypeSpec:
5354 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005355 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5356 AddTypeLoc(NNS.getTypeLoc());
5357 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005358 break;
5359
5360 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005361 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005362 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005363
5364 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005365 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5366 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005367 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005368 }
5369 }
5370}
5371
Richard Smith290d8012016-04-06 17:06:00 +00005372void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005373 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005374 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005375 switch (Kind) {
5376 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005377 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005378 break;
5379
5380 case TemplateName::OverloadedTemplate: {
5381 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005382 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005383 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005384 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005385 break;
5386 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005387
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005388 case TemplateName::QualifiedTemplate: {
5389 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005390 AddNestedNameSpecifier(QualT->getQualifier());
5391 Record->push_back(QualT->hasTemplateKeyword());
5392 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005393 break;
5394 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005395
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005396 case TemplateName::DependentTemplate: {
5397 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005398 AddNestedNameSpecifier(DepT->getQualifier());
5399 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005400 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005401 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005402 else
Richard Smith290d8012016-04-06 17:06:00 +00005403 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005404 break;
5405 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005406
5407 case TemplateName::SubstTemplateTemplateParm: {
5408 SubstTemplateTemplateParmStorage *subst
5409 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005410 AddDeclRef(subst->getParameter());
5411 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005412 break;
5413 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005414
5415 case TemplateName::SubstTemplateTemplateParmPack: {
5416 SubstTemplateTemplateParmPackStorage *SubstPack
5417 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005418 AddDeclRef(SubstPack->getParameterPack());
5419 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005420 break;
5421 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005422 }
5423}
5424
Richard Smith290d8012016-04-06 17:06:00 +00005425void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5426 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005427 switch (Arg.getKind()) {
5428 case TemplateArgument::Null:
5429 break;
5430 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005431 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005432 break;
5433 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005434 AddDeclRef(Arg.getAsDecl());
5435 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005436 break;
5437 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005438 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005439 break;
5440 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005441 AddAPSInt(Arg.getAsIntegral());
5442 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005443 break;
5444 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005445 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005446 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005447 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005448 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005449 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005450 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005451 else
Richard Smith290d8012016-04-06 17:06:00 +00005452 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005453 break;
5454 case TemplateArgument::Expression:
5455 AddStmt(Arg.getAsExpr());
5456 break;
5457 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005458 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005459 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005460 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005461 break;
5462 }
5463}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005464
Richard Smithe64e7452016-04-18 21:54:58 +00005465void ASTRecordWriter::AddTemplateParameterList(
5466 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005467 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005468 AddSourceLocation(TemplateParams->getTemplateLoc());
5469 AddSourceLocation(TemplateParams->getLAngleLoc());
5470 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005471 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005472 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005473 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005474 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005475}
5476
5477/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005478void ASTRecordWriter::AddTemplateArgumentList(
5479 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005480 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005481 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005482 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005483 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005484}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005485
Richard Smith290d8012016-04-06 17:06:00 +00005486void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5487 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005488 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005489 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5490 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5491 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005492 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005493 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005494 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005495}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005496
Richard Smithe64e7452016-04-18 21:54:58 +00005497void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5498 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005499 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005500 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005501 AddDeclRef(I.getDecl());
5502 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005503 }
5504}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005505
Richard Smith290d8012016-04-06 17:06:00 +00005506// FIXME: Move this out of the main ASTRecordWriter interface.
5507void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5508 Record->push_back(Base.isVirtual());
5509 Record->push_back(Base.isBaseOfClass());
5510 Record->push_back(Base.getAccessSpecifierAsWritten());
5511 Record->push_back(Base.getInheritConstructors());
5512 AddTypeSourceInfo(Base.getTypeSourceInfo());
5513 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005514 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005515 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005516}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005517
Richard Smith645d2cf2016-04-14 00:29:55 +00005518static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5519 ArrayRef<CXXBaseSpecifier> Bases) {
5520 ASTWriter::RecordData Record;
5521 ASTRecordWriter Writer(W, Record);
5522 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005523
Richard Smith645d2cf2016-04-14 00:29:55 +00005524 for (auto &Base : Bases)
5525 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005526
Richard Smith645d2cf2016-04-14 00:29:55 +00005527 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005528}
5529
Richard Smith290d8012016-04-06 17:06:00 +00005530// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005531void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5532 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5533}
5534
Richard Smithaa165cf2016-04-13 21:57:08 +00005535static uint64_t
5536EmitCXXCtorInitializers(ASTWriter &W,
5537 ArrayRef<CXXCtorInitializer *> CtorInits) {
5538 ASTWriter::RecordData Record;
5539 ASTRecordWriter Writer(W, Record);
5540 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005541
Richard Smithaa165cf2016-04-13 21:57:08 +00005542 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005543 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005544 Writer.push_back(CTOR_INITIALIZER_BASE);
5545 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5546 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005547 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005548 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5549 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005550 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005551 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5552 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005553 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005554 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5555 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005556 }
Francois Pichetd583da02010-12-04 09:14:42 +00005557
Richard Smithaa165cf2016-04-13 21:57:08 +00005558 Writer.AddSourceLocation(Init->getMemberLocation());
5559 Writer.AddStmt(Init->getInit());
5560 Writer.AddSourceLocation(Init->getLParenLoc());
5561 Writer.AddSourceLocation(Init->getRParenLoc());
5562 Writer.push_back(Init->isWritten());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005563 if (Init->isWritten()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005564 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005565 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005566 Writer.push_back(Init->getNumArrayIndices());
5567 for (auto *VD : Init->getArrayIndices())
5568 Writer.AddDeclRef(VD);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005569 }
5570 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005571
5572 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005573}
5574
Richard Smithaa165cf2016-04-13 21:57:08 +00005575// FIXME: Move this out of the main ASTRecordWriter interface.
5576void ASTRecordWriter::AddCXXCtorInitializers(
5577 ArrayRef<CXXCtorInitializer *> CtorInits) {
5578 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005579}
5580
Richard Smith290d8012016-04-06 17:06:00 +00005581void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005582 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005583 Record->push_back(Data.IsLambda);
5584 Record->push_back(Data.UserDeclaredConstructor);
5585 Record->push_back(Data.UserDeclaredSpecialMembers);
5586 Record->push_back(Data.Aggregate);
5587 Record->push_back(Data.PlainOldData);
5588 Record->push_back(Data.Empty);
5589 Record->push_back(Data.Polymorphic);
5590 Record->push_back(Data.Abstract);
5591 Record->push_back(Data.IsStandardLayout);
5592 Record->push_back(Data.HasNoNonEmptyBases);
5593 Record->push_back(Data.HasPrivateFields);
5594 Record->push_back(Data.HasProtectedFields);
5595 Record->push_back(Data.HasPublicFields);
5596 Record->push_back(Data.HasMutableFields);
5597 Record->push_back(Data.HasVariantMembers);
5598 Record->push_back(Data.HasOnlyCMembers);
5599 Record->push_back(Data.HasInClassInitializer);
5600 Record->push_back(Data.HasUninitializedReferenceMember);
5601 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005602 Record->push_back(Data.HasInheritedConstructor);
5603 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005604 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5605 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5606 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5607 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5608 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5609 Record->push_back(Data.DefaultedDestructorIsDeleted);
5610 Record->push_back(Data.HasTrivialSpecialMembers);
5611 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5612 Record->push_back(Data.HasIrrelevantDestructor);
5613 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5614 Record->push_back(Data.HasDefaultedDefaultConstructor);
5615 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5616 Record->push_back(Data.HasConstexprDefaultConstructor);
5617 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5618 Record->push_back(Data.ComputedVisibleConversions);
5619 Record->push_back(Data.UserProvidedDefaultConstructor);
5620 Record->push_back(Data.DeclaredSpecialMembers);
5621 Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
5622 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5623 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5624 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005625 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005626
Richard Smith290d8012016-04-06 17:06:00 +00005627 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005628 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005629 AddCXXBaseSpecifiers(Data.bases());
5630
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005631 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005632 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005633 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005634 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005635
Richard Smith290d8012016-04-06 17:06:00 +00005636 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5637 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005638 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005639 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005640
5641 // Add lambda-specific data.
5642 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005643 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005644 Record->push_back(Lambda.Dependent);
5645 Record->push_back(Lambda.IsGenericLambda);
5646 Record->push_back(Lambda.CaptureDefault);
5647 Record->push_back(Lambda.NumCaptures);
5648 Record->push_back(Lambda.NumExplicitCaptures);
5649 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005650 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005651 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005652 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005653 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005654 AddSourceLocation(Capture.getLocation());
5655 Record->push_back(Capture.isImplicit());
5656 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005657 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005658 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005659 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005660 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005661 break;
5662 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005663 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005664 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005665 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005666 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005667 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005668 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005669 break;
5670 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005671 }
5672 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005673}
5674
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005675void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005676 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005677 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005678 assert(FirstDeclID == NextDeclID &&
5679 FirstTypeID == NextTypeID &&
5680 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005681 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005682 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005683 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005684 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005685
Sebastian Redl07a89a82010-07-30 00:29:29 +00005686 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005687
Richard Smith7f330cd2015-03-18 01:42:29 +00005688 // Note, this will get called multiple times, once one the reader starts up
5689 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005690 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5691 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5692 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005693 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005694 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005695 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005696 NextDeclID = FirstDeclID;
5697 NextTypeID = FirstTypeID;
5698 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005699 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005700 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005701 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005702}
5703
Sebastian Redl539c5062010-08-18 23:57:32 +00005704void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005705 // Always keep the highest ID. See \p TypeRead() for more information.
5706 IdentID &StoredID = IdentifierIDs[II];
5707 if (ID > StoredID)
5708 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005709}
5710
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005711void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005712 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005713 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005714 if (ID > StoredID)
5715 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005716}
5717
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005718void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005719 // Always take the highest-numbered type index. This copes with an interesting
5720 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005721 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005722 // keep the higher-numbered entry so that we can properly write it out to
5723 // the AST file.
5724 TypeIdx &StoredIdx = TypeIdxs[T];
5725 if (Idx.getIndex() >= StoredIdx.getIndex())
5726 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005727}
5728
Sebastian Redl539c5062010-08-18 23:57:32 +00005729void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005730 // Always keep the highest ID. See \p TypeRead() for more information.
5731 SelectorID &StoredID = SelectorIDs[S];
5732 if (ID > StoredID)
5733 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005734}
Douglas Gregor91096292010-10-02 19:29:26 +00005735
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005736void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005737 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005738 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005739 MacroDefinitions[MD] = ID;
5740}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005741
Douglas Gregore37a85a2011-12-02 17:30:13 +00005742void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5743 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5744 SubmoduleIDs[Mod] = ID;
5745}
5746
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005747void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005748 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005749 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005750 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005751 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005752 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005753 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005754 // A forward reference was mutated into a definition. Rewrite it.
5755 // FIXME: This happens during template instantiation, should we
5756 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005757 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5758 "completed a tag from another module but not by instantiation?");
5759 DeclUpdates[RD].push_back(
5760 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005761 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005762 }
5763}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005764
Richard Smithf983f7f2015-10-13 00:23:25 +00005765static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5766 if (D->isFromASTFile())
5767 return true;
5768
Richard Smithf983f7f2015-10-13 00:23:25 +00005769 // The predefined __va_list_tag struct is imported if we imported any decls.
5770 // FIXME: This is a gross hack.
5771 return D == D->getASTContext().getVaListTagDecl();
5772}
5773
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005774void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005775 if (Chain && Chain->isProcessingUpdateRecords()) return;
5776 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005777 "Should not add lookup results to non-lookup contexts!");
5778
Vassil Vassilev632eac32016-03-16 11:17:04 +00005779 // TU is handled elsewhere.
5780 if (isa<TranslationUnitDecl>(DC))
5781 return;
5782
5783 // Namespaces are handled elsewhere, except for template instantiations of
5784 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5785 // local instantiations are added to an imported context. Only happens when
5786 // adding ADL lookup candidates, for example templated friends.
5787 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5788 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005789 return;
5790
Richard Smithf983f7f2015-10-13 00:23:25 +00005791 // We're only interested in cases where a local declaration is added to an
5792 // imported context.
5793 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5794 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005795
Richard Smith0f4e2c42015-08-06 04:23:48 +00005796 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005797 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005798 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005799 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5800 // We're adding a visible declaration to a predefined decl context. Ensure
5801 // that we write out all of its lookup results so we don't get a nasty
5802 // surprise when we try to emit its lookup table.
5803 for (auto *Child : DC->decls())
5804 UpdatingVisibleDecls.push_back(Child);
5805 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005806 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005807}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005808
5809void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005810 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005811 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005812
5813 // We're only interested in cases where a local declaration is added to an
5814 // imported context.
5815 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5816 return;
5817
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005818 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005819 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005820
5821 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005822 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005823 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005824 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005825}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005826
Richard Smith564417a2014-03-20 21:47:22 +00005827void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005828 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005829 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5830 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005831 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005832 // If we don't already know the exception specification for this redecl
5833 // chain, add an update record for it.
5834 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5835 ->getType()
5836 ->castAs<FunctionProtoType>()
5837 ->getExceptionSpecType()))
5838 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5839 });
Richard Smith564417a2014-03-20 21:47:22 +00005840}
5841
Richard Smith1fa5d642013-05-11 05:45:24 +00005842void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005843 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005844 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005845 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005846 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005847 DeclUpdates[D].push_back(
5848 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5849 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005850}
5851
Richard Smithf8134002015-03-10 01:41:22 +00005852void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5853 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005854 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005855 assert(!WritingAST && "Already writing the AST!");
5856 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005857 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005858 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005859 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5860 });
Richard Smithf8134002015-03-10 01:41:22 +00005861}
5862
Sebastian Redlab238a72011-04-24 16:28:06 +00005863void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005864 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005865 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005866 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005867 return; // Declaration not imported from PCH.
5868
Richard Smith4d235792014-08-07 18:53:08 +00005869 // Implicit function decl from a PCH was defined.
5870 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005871}
5872
Richard Smithd28ac5b2014-03-22 23:33:22 +00005873void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005874 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005875 assert(!WritingAST && "Already writing the AST!");
5876 if (!D->isFromASTFile())
5877 return;
5878
Richard Smith9e2341d2015-03-23 03:25:59 +00005879 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005880}
5881
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005882void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005883 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005884 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005885 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005886 return;
5887
5888 // Since the actual instantiation is delayed, this really means that we need
5889 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005890 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005891 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5892 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005893}
5894
John McCall32791cc2016-01-06 22:34:54 +00005895void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005896 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005897 assert(!WritingAST && "Already writing the AST!");
5898 if (!D->isFromASTFile())
5899 return;
5900
5901 DeclUpdates[D].push_back(
5902 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5903}
5904
Richard Smith4b054b22016-08-24 21:25:37 +00005905void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5906 assert(!WritingAST && "Already writing the AST!");
5907 if (!D->isFromASTFile())
5908 return;
5909
5910 DeclUpdates[D].push_back(
5911 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5912}
5913
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005914void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5915 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005916 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005917 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005918 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005919 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005920
5921 assert(IFD->getDefinition() && "Category on a class without a definition?");
5922 ObjCClassesWithCategories.insert(
5923 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005924}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005925
Eli Friedman276dd182013-09-05 00:02:25 +00005926void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005927 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005928 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005929
5930 // If there is *any* declaration of the entity that's not from an AST file,
5931 // we can skip writing the update record. We make sure that isUsed() triggers
5932 // completion of the redeclaration chain of the entity.
5933 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5934 if (IsLocalDecl(Prev))
5935 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005936
Aaron Ballman4f45b712014-03-21 15:22:56 +00005937 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005938}
Alexey Bataev97720002014-11-11 04:05:39 +00005939
5940void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005941 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005942 assert(!WritingAST && "Already writing the AST!");
5943 if (!D->isFromASTFile())
5944 return;
5945
5946 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5947}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005948
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005949void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5950 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005951 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005952 assert(!WritingAST && "Already writing the AST!");
5953 if (!D->isFromASTFile())
5954 return;
5955
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005956 DeclUpdates[D].push_back(
5957 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005958}
5959
Richard Smith4caa4492015-05-15 02:34:32 +00005960void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005961 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005962 assert(!WritingAST && "Already writing the AST!");
5963 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005964 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005965}
Alex Denisovfde64952015-06-26 05:28:36 +00005966
5967void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5968 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005969 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00005970 assert(!WritingAST && "Already writing the AST!");
5971 if (!Record->isFromASTFile())
5972 return;
5973 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5974}