blob: 9c906e46b5819a4dc8b92733c16e14075c1c0119 [file] [log] [blame]
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001//===--- ASTWriter.cpp - AST File Writer ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000016#include "ASTReaderInternals.h"
17#include "MultiOnDiskHashTable.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000019#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000022#include "clang/AST/DeclCXX.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000027#include "clang/AST/LambdaCapture.h"
28#include "clang/AST/NestedNameSpecifier.h"
29#include "clang/AST/RawCommentList.h"
30#include "clang/AST/TemplateName.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000031#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000032#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000033#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000035#include "clang/Basic/FileSystemOptions.h"
36#include "clang/Basic/LangOptions.h"
37#include "clang/Basic/LLVM.h"
38#include "clang/Basic/Module.h"
39#include "clang/Basic/ObjCRuntime.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000040#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000041#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000042#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000043#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000044#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000045#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000046#include "clang/Lex/HeaderSearch.h"
47#include "clang/Lex/HeaderSearchOptions.h"
48#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000049#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000050#include "clang/Lex/PreprocessingRecord.h"
51#include "clang/Lex/Preprocessor.h"
52#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000053#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000054#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000055#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000056#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000057#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000058#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000059#include "clang/Serialization/Module.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000060#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000061#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000062#include "llvm/ADT/APFloat.h"
63#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000064#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000065#include "llvm/ADT/IntrusiveRefCntPtr.h"
66#include "llvm/ADT/Optional.h"
67#include "llvm/ADT/SmallSet.h"
68#include "llvm/ADT/SmallString.h"
69#include "llvm/ADT/STLExtras.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000070#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000071#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000073#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000074#include "llvm/Support/Compression.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000075#include "llvm/Support/EndianStream.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000076#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000077#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000078#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000079#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000080#include "llvm/Support/Process.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000081#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000082#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000083#include <cassert>
84#include <cstdint>
85#include <cstdlib>
86#include <cstring>
87#include <deque>
88#include <limits>
89#include <new>
90#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +000091#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000092
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000094using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095
Sebastian Redl3df5a082010-07-30 17:03:48 +000096template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000097static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000098 if (v.empty()) return StringRef();
99 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000100 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000101}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000102
103template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000104static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000105 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000106 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000107}
108
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109//===----------------------------------------------------------------------===//
110// Type serialization
111//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000112
Richard Smith290d8012016-04-06 17:06:00 +0000113namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000116 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000117 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000118
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +0000120 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +0000121 /// \brief Abbreviation to use for the record, if any.
122 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000123
Richard Smith290d8012016-04-06 17:06:00 +0000124 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000125 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Richard Smith290d8012016-04-06 17:06:00 +0000126 : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
127
128 uint64_t Emit() {
129 return Record.Emit(Code, AbbrevToUse);
130 }
131
132 void Visit(QualType T) {
133 if (T.hasLocalNonFastQualifiers()) {
134 Qualifiers Qs = T.getLocalQualifiers();
135 Record.AddTypeRef(T.getLocalUnqualifiedType());
136 Record.push_back(Qs.getAsOpaqueValue());
137 Code = TYPE_EXT_QUAL;
138 AbbrevToUse = Writer.TypeExtQualAbbrev;
139 } else {
140 switch (T->getTypeClass()) {
141 // For all of the concrete, non-dependent types, call the
142 // appropriate visitor function.
143#define TYPE(Class, Base) \
144 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
145#define ABSTRACT_TYPE(Class, Base)
146#include "clang/AST/TypeNodes.def"
147 }
148 }
149 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150
151 void VisitArrayType(const ArrayType *T);
152 void VisitFunctionType(const FunctionType *T);
153 void VisitTagType(const TagType *T);
154
155#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
156#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157#include "clang/AST/TypeNodes.def"
158 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000159
Richard Smith290d8012016-04-06 17:06:00 +0000160} // end namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000163 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000167 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000168 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169}
170
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000172 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000173 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174}
175
Reid Kleckner8a365022013-06-24 17:51:48 +0000176void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000177 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000178 Code = TYPE_DECAYED;
179}
180
Reid Kleckner0503a872013-12-05 01:23:43 +0000181void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000182 Record.AddTypeRef(T->getOriginalType());
183 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000184 Code = TYPE_ADJUSTED;
185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000188 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000189 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190}
191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000193 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000194 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000195 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196}
197
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000198void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000199 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000200 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000201}
202
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000203void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000204 Record.AddTypeRef(T->getPointeeType());
205 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000206 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207}
208
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000210 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000211 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000212 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213}
214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000217 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000218 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000228 Record.AddSourceLocation(T->getLBracketLoc());
229 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000230 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000231 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000232}
233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000235 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000237 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000238 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239}
240
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000243 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244}
245
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000246void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000247 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000248 FunctionType::ExtInfo C = T->getExtInfo();
249 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000250 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000251 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000252 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000253 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000254 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000255
256 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
257 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Richard Smith290d8012016-04-06 17:06:00 +0000265static void addExceptionSpec(const FunctionProtoType *T,
266 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000267 Record.push_back(T->getExceptionSpecType());
268 if (T->getExceptionSpecType() == EST_Dynamic) {
269 Record.push_back(T->getNumExceptions());
270 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000271 Record.AddTypeRef(T->getExceptionType(I));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000272 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
Richard Smith290d8012016-04-06 17:06:00 +0000273 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000274 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000275 Record.AddDeclRef(T->getExceptionSpecDecl());
276 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000277 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000278 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000279 }
Richard Smith564417a2014-03-20 21:47:22 +0000280}
281
282void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
283 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000284
Richard Smith564417a2014-03-20 21:47:22 +0000285 Record.push_back(T->isVariadic());
286 Record.push_back(T->hasTrailingReturn());
287 Record.push_back(T->getTypeQuals());
288 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000289 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000290
291 Record.push_back(T->getNumParams());
292 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000293 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000294
John McCall18afab72016-03-01 00:49:02 +0000295 if (T->hasExtParameterInfos()) {
296 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
297 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
298 }
299
Richard Smith01b2cb42014-07-26 06:37:51 +0000300 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000301 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
302 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000303 AbbrevToUse = 0;
304
Sebastian Redl539c5062010-08-18 23:57:32 +0000305 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000306}
307
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000308void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000309 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000310 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000311}
John McCallb96ec562009-12-04 22:46:56 +0000312
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000313void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000314 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000315 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000316 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000317 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000318}
319
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000320void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000321 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000322 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000323}
324
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000325void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000326 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000327 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000328}
329
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000331 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000332 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000333 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000334}
335
Alexis Hunte852b102011-05-24 22:41:36 +0000336void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000337 Record.AddTypeRef(T->getBaseType());
338 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000339 Record.push_back(T->getUTTKind());
340 Code = TYPE_UNARY_TRANSFORM;
341}
342
Richard Smith30482bc2011-02-20 03:19:35 +0000343void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000344 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000345 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000346 if (T->getDeducedType().isNull())
347 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000348 Code = TYPE_AUTO;
349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000352 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000353 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000354 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000355 "Cannot serialize in the middle of a type definition");
356}
357
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000358void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000359 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000360 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000361}
362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000363void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000364 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000365 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000366}
367
John McCall81904512011-01-06 01:58:22 +0000368void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000369 Record.AddTypeRef(T->getModifiedType());
370 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000371 Record.push_back(T->getAttrKind());
372 Code = TYPE_ATTRIBUTED;
373}
374
Mike Stump11289f42009-09-09 15:08:12 +0000375void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000376ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000377 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000378 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
379 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000380 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000381}
382
383void
Douglas Gregorada4b792011-01-14 02:55:32 +0000384ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
385 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000386 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
387 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000388 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
389}
390
391void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000392ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000393 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000394 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000395 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000396 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000397 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000398 Record.AddTemplateArgument(ArgI);
399 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
400 : T->isCanonicalUnqualified()
401 ? QualType()
402 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000403 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000404}
405
406void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000407ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000408 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000409 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000410 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000411 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000412}
413
414void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000415ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000416 const DependentSizedExtVectorType *T) {
417 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000418 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000419}
420
421void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000422ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000423 Record.push_back(T->getDepth());
424 Record.push_back(T->getIndex());
425 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000426 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000427 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000428}
429
430void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000431ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000432 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000433 Record.AddNestedNameSpecifier(T->getQualifier());
434 Record.AddIdentifierRef(T->getIdentifier());
435 Record.AddTypeRef(
436 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000437 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000438}
439
440void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000441ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000442 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000443 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000444 Record.AddNestedNameSpecifier(T->getQualifier());
445 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000446 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000447 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000448 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000449 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000450}
451
Douglas Gregord2fa7662010-12-20 02:24:11 +0000452void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000453 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000454 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000455 Record.push_back(*NumExpansions + 1);
456 else
457 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000458 Code = TYPE_PACK_EXPANSION;
459}
460
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000461void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000462 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000463 Code = TYPE_PAREN;
464}
465
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000466void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000467 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000468 Record.AddNestedNameSpecifier(T->getQualifier());
469 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000470 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000471}
472
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000473void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000474 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
475 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000476 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000477}
478
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000479void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000480 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000481 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000482}
483
Manman Rene6be26c2016-09-13 17:25:08 +0000484void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
485 Record.AddDeclRef(T->getDecl());
486 Record.push_back(T->getNumProtocols());
487 for (const auto *I : T->quals())
488 Record.AddDeclRef(I);
489 Code = TYPE_OBJC_TYPE_PARAM;
490}
491
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000492void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000493 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000494 Record.push_back(T->getTypeArgsAsWritten().size());
495 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000496 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000497 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000498 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000499 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000500 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000501 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000502}
503
Steve Narofffb4330f2009-06-17 22:40:22 +0000504void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000505ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000506 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000507 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000508}
509
Eli Friedman0dfb8892011-10-06 23:00:33 +0000510void
511ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000512 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000513 Code = TYPE_ATOMIC;
514}
515
Xiuli Pan9c14e282016-01-09 12:53:17 +0000516void
517ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000518 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000519 Record.push_back(T->isReadOnly());
520 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000521}
522
John McCall8f115c62009-10-16 21:56:05 +0000523namespace {
524
525class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000526 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000527
528public:
Richard Smith290d8012016-04-06 17:06:00 +0000529 TypeLocWriter(ASTRecordWriter &Record)
530 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000531
John McCall17001972009-10-18 01:05:36 +0000532#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000533#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000534 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000535#include "clang/AST/TypeLocNodes.def"
536
John McCall17001972009-10-18 01:05:36 +0000537 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
538 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000539};
540
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000541} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000542
John McCall17001972009-10-18 01:05:36 +0000543void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
544 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000545}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000546
John McCall17001972009-10-18 01:05:36 +0000547void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000548 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000549 if (TL.needsExtraLocalData()) {
550 Record.push_back(TL.getWrittenTypeSpec());
551 Record.push_back(TL.getWrittenSignSpec());
552 Record.push_back(TL.getWrittenWidthSpec());
553 Record.push_back(TL.hasModeAttr());
554 }
John McCall8f115c62009-10-16 21:56:05 +0000555}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000556
John McCall17001972009-10-18 01:05:36 +0000557void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000558 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000559}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000560
John McCall17001972009-10-18 01:05:36 +0000561void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000562 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000563}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000564
Reid Kleckner8a365022013-06-24 17:51:48 +0000565void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
566 // nothing to do
567}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000568
Reid Kleckner0503a872013-12-05 01:23:43 +0000569void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
570 // nothing to do
571}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000572
John McCall17001972009-10-18 01:05:36 +0000573void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000574 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000575}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000576
John McCall17001972009-10-18 01:05:36 +0000577void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000578 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000579}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000580
John McCall17001972009-10-18 01:05:36 +0000581void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000582 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000583}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000584
John McCall17001972009-10-18 01:05:36 +0000585void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000586 Record.AddSourceLocation(TL.getStarLoc());
587 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000588}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000589
John McCall17001972009-10-18 01:05:36 +0000590void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000591 Record.AddSourceLocation(TL.getLBracketLoc());
592 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000593 Record.push_back(TL.getSizeExpr() ? 1 : 0);
594 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000595 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000596}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000597
John McCall17001972009-10-18 01:05:36 +0000598void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
599 VisitArrayTypeLoc(TL);
600}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000601
John McCall17001972009-10-18 01:05:36 +0000602void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
603 VisitArrayTypeLoc(TL);
604}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000605
John McCall17001972009-10-18 01:05:36 +0000606void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
607 VisitArrayTypeLoc(TL);
608}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000609
John McCall17001972009-10-18 01:05:36 +0000610void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
611 DependentSizedArrayTypeLoc TL) {
612 VisitArrayTypeLoc(TL);
613}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000614
John McCall17001972009-10-18 01:05:36 +0000615void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
616 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000617 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000618}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000619
John McCall17001972009-10-18 01:05:36 +0000620void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000621 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000622}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000623
John McCall17001972009-10-18 01:05:36 +0000624void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000625 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000626}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000627
John McCall17001972009-10-18 01:05:36 +0000628void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000629 Record.AddSourceLocation(TL.getLocalRangeBegin());
630 Record.AddSourceLocation(TL.getLParenLoc());
631 Record.AddSourceLocation(TL.getRParenLoc());
632 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000633 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000634 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000635}
636void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
637 VisitFunctionTypeLoc(TL);
638}
639void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
640 VisitFunctionTypeLoc(TL);
641}
John McCallb96ec562009-12-04 22:46:56 +0000642void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000643 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000644}
John McCall17001972009-10-18 01:05:36 +0000645void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000646 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000647}
Manman Rene6be26c2016-09-13 17:25:08 +0000648void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
649 if (TL.getNumProtocols()) {
650 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
651 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
652 }
653 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
654 Record.AddSourceLocation(TL.getProtocolLoc(i));
655}
John McCall17001972009-10-18 01:05:36 +0000656void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000657 Record.AddSourceLocation(TL.getTypeofLoc());
658 Record.AddSourceLocation(TL.getLParenLoc());
659 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000660}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000661
John McCall17001972009-10-18 01:05:36 +0000662void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000663 Record.AddSourceLocation(TL.getTypeofLoc());
664 Record.AddSourceLocation(TL.getLParenLoc());
665 Record.AddSourceLocation(TL.getRParenLoc());
666 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000667}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000668
John McCall17001972009-10-18 01:05:36 +0000669void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000670 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000671}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000672
Alexis Hunte852b102011-05-24 22:41:36 +0000673void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000674 Record.AddSourceLocation(TL.getKWLoc());
675 Record.AddSourceLocation(TL.getLParenLoc());
676 Record.AddSourceLocation(TL.getRParenLoc());
677 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000678}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000679
Richard Smith30482bc2011-02-20 03:19:35 +0000680void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000681 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000682}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000683
John McCall17001972009-10-18 01:05:36 +0000684void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000685 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000686}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000687
John McCall17001972009-10-18 01:05:36 +0000688void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000689 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000690}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000691
John McCall81904512011-01-06 01:58:22 +0000692void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000693 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000694 if (TL.hasAttrOperand()) {
695 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000696 Record.AddSourceLocation(range.getBegin());
697 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000698 }
699 if (TL.hasAttrExprOperand()) {
700 Expr *operand = TL.getAttrExprOperand();
701 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000702 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000703 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000704 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000705 }
706}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000707
John McCall17001972009-10-18 01:05:36 +0000708void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000709 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000710}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000711
John McCallcebee162009-10-18 09:09:24 +0000712void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
713 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000714 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000715}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000716
Douglas Gregorada4b792011-01-14 02:55:32 +0000717void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
718 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000719 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000720}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000721
John McCall17001972009-10-18 01:05:36 +0000722void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
723 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000724 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
725 Record.AddSourceLocation(TL.getTemplateNameLoc());
726 Record.AddSourceLocation(TL.getLAngleLoc());
727 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000728 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000729 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
730 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000731}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000732
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000733void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000734 Record.AddSourceLocation(TL.getLParenLoc());
735 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000736}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000737
Abramo Bagnara6150c882010-05-11 21:36:43 +0000738void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000739 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
740 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000741}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000742
John McCalle78aac42010-03-10 03:28:59 +0000743void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000744 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000745}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000746
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000747void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000748 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
749 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
750 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000751}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000752
John McCallc392f372010-06-11 00:33:02 +0000753void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
754 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000755 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
756 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
757 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
758 Record.AddSourceLocation(TL.getTemplateNameLoc());
759 Record.AddSourceLocation(TL.getLAngleLoc());
760 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000761 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000762 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
763 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000764}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000765
Douglas Gregord2fa7662010-12-20 02:24:11 +0000766void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000767 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000768}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000769
John McCall17001972009-10-18 01:05:36 +0000770void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000771 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000772}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000773
John McCall8b07ec22010-05-15 11:32:37 +0000774void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
775 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000776 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
777 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000778 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000779 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
780 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
781 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000782 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000783 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000784}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000785
John McCallfc93cf92009-10-22 22:37:11 +0000786void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000787 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000788}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000789
Eli Friedman0dfb8892011-10-06 23:00:33 +0000790void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000791 Record.AddSourceLocation(TL.getKWLoc());
792 Record.AddSourceLocation(TL.getLParenLoc());
793 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000794}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000795
Xiuli Pan9c14e282016-01-09 12:53:17 +0000796void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000797 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000798}
John McCall8f115c62009-10-16 21:56:05 +0000799
Richard Smith01b2cb42014-07-26 06:37:51 +0000800void ASTWriter::WriteTypeAbbrevs() {
801 using namespace llvm;
802
803 BitCodeAbbrev *Abv;
804
805 // Abbreviation for TYPE_EXT_QUAL
806 Abv = new BitCodeAbbrev();
807 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
808 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
809 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
810 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
811
812 // Abbreviation for TYPE_FUNCTION_PROTO
813 Abv = new BitCodeAbbrev();
814 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
815 // FunctionType
816 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
817 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
818 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
819 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
820 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
821 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
822 // FunctionProtoType
823 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
824 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
825 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
826 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
827 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
828 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
829 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
830 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
831 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
832}
833
Chris Lattner19cea4e2009-04-22 05:57:30 +0000834//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000835// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000836//===----------------------------------------------------------------------===//
837
Chris Lattner28fa4e62009-04-26 22:26:21 +0000838static void EmitBlockID(unsigned ID, const char *Name,
839 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000840 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000841 Record.clear();
842 Record.push_back(ID);
843 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
844
845 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000846 if (!Name || Name[0] == 0)
847 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000848 Record.clear();
849 while (*Name)
850 Record.push_back(*Name++);
851 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
852}
853
854static void EmitRecordID(unsigned ID, const char *Name,
855 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000856 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000857 Record.clear();
858 Record.push_back(ID);
859 while (*Name)
860 Record.push_back(*Name++);
861 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000862}
863
864static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000865 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000866#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000867 RECORD(STMT_STOP);
868 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000869 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000870 RECORD(STMT_NULL);
871 RECORD(STMT_COMPOUND);
872 RECORD(STMT_CASE);
873 RECORD(STMT_DEFAULT);
874 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000875 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000876 RECORD(STMT_IF);
877 RECORD(STMT_SWITCH);
878 RECORD(STMT_WHILE);
879 RECORD(STMT_DO);
880 RECORD(STMT_FOR);
881 RECORD(STMT_GOTO);
882 RECORD(STMT_INDIRECT_GOTO);
883 RECORD(STMT_CONTINUE);
884 RECORD(STMT_BREAK);
885 RECORD(STMT_RETURN);
886 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000887 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000888 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000889 RECORD(EXPR_PREDEFINED);
890 RECORD(EXPR_DECL_REF);
891 RECORD(EXPR_INTEGER_LITERAL);
892 RECORD(EXPR_FLOATING_LITERAL);
893 RECORD(EXPR_IMAGINARY_LITERAL);
894 RECORD(EXPR_STRING_LITERAL);
895 RECORD(EXPR_CHARACTER_LITERAL);
896 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000897 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000898 RECORD(EXPR_UNARY_OPERATOR);
899 RECORD(EXPR_SIZEOF_ALIGN_OF);
900 RECORD(EXPR_ARRAY_SUBSCRIPT);
901 RECORD(EXPR_CALL);
902 RECORD(EXPR_MEMBER);
903 RECORD(EXPR_BINARY_OPERATOR);
904 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
905 RECORD(EXPR_CONDITIONAL_OPERATOR);
906 RECORD(EXPR_IMPLICIT_CAST);
907 RECORD(EXPR_CSTYLE_CAST);
908 RECORD(EXPR_COMPOUND_LITERAL);
909 RECORD(EXPR_EXT_VECTOR_ELEMENT);
910 RECORD(EXPR_INIT_LIST);
911 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000912 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000913 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000914 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000915 RECORD(EXPR_VA_ARG);
916 RECORD(EXPR_ADDR_LABEL);
917 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000918 RECORD(EXPR_CHOOSE);
919 RECORD(EXPR_GNU_NULL);
920 RECORD(EXPR_SHUFFLE_VECTOR);
921 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000922 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000923 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000924 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000925 RECORD(EXPR_OBJC_ARRAY_LITERAL);
926 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000927 RECORD(EXPR_OBJC_ENCODE);
928 RECORD(EXPR_OBJC_SELECTOR_EXPR);
929 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
930 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
931 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
932 RECORD(EXPR_OBJC_KVC_REF_EXPR);
933 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000934 RECORD(STMT_OBJC_FOR_COLLECTION);
935 RECORD(STMT_OBJC_CATCH);
936 RECORD(STMT_OBJC_FINALLY);
937 RECORD(STMT_OBJC_AT_TRY);
938 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
939 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000940 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000941 RECORD(STMT_CXX_CATCH);
942 RECORD(STMT_CXX_TRY);
943 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000944 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000945 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000946 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000947 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000948 RECORD(EXPR_CXX_STATIC_CAST);
949 RECORD(EXPR_CXX_DYNAMIC_CAST);
950 RECORD(EXPR_CXX_REINTERPRET_CAST);
951 RECORD(EXPR_CXX_CONST_CAST);
952 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000953 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000954 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000955 RECORD(EXPR_CXX_BOOL_LITERAL);
956 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000957 RECORD(EXPR_CXX_TYPEID_EXPR);
958 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000959 RECORD(EXPR_CXX_THIS);
960 RECORD(EXPR_CXX_THROW);
961 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000962 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000963 RECORD(EXPR_CXX_BIND_TEMPORARY);
964 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
965 RECORD(EXPR_CXX_NEW);
966 RECORD(EXPR_CXX_DELETE);
967 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
968 RECORD(EXPR_EXPR_WITH_CLEANUPS);
969 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
970 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
971 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
972 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
973 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000974 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000975 RECORD(EXPR_CXX_NOEXCEPT);
976 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000977 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
978 RECORD(EXPR_TYPE_TRAIT);
979 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000980 RECORD(EXPR_PACK_EXPANSION);
981 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000982 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000983 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000984 RECORD(EXPR_FUNCTION_PARM_PACK);
985 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000986 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000987 RECORD(EXPR_CXX_UUIDOF_EXPR);
988 RECORD(EXPR_CXX_UUIDOF_TYPE);
989 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000990#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000991}
Mike Stump11289f42009-09-09 15:08:12 +0000992
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000993void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000994 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000995 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +0000996
Sebastian Redl539c5062010-08-18 23:57:32 +0000997#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
998#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000999
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001000 // Control Block.
1001 BLOCK(CONTROL_BLOCK);
1002 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +00001003 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001004 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001005 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001006 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001007 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001008 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001009 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001010 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001011 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001012
1013 BLOCK(OPTIONS_BLOCK);
1014 RECORD(LANGUAGE_OPTIONS);
1015 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001016 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001017 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001018 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001019 RECORD(PREPROCESSOR_OPTIONS);
1020
Douglas Gregor108cb222012-10-19 00:45:00 +00001021 BLOCK(INPUT_FILES_BLOCK);
1022 RECORD(INPUT_FILE);
1023
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001024 // AST Top-Level Block.
1025 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001026 RECORD(TYPE_OFFSET);
1027 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001028 RECORD(IDENTIFIER_OFFSET);
1029 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001030 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001031 RECORD(SPECIAL_TYPES);
1032 RECORD(STATISTICS);
1033 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001034 RECORD(SELECTOR_OFFSETS);
1035 RECORD(METHOD_POOL);
1036 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001037 RECORD(SOURCE_LOCATION_OFFSETS);
1038 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001039 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001040 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001041 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001042 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001043 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001044 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001045 RECORD(SEMA_DECL_REFS);
1046 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1047 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001048 RECORD(UPDATE_VISIBLE);
1049 RECORD(DECL_UPDATE_OFFSETS);
1050 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001051 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001052 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001053 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001054 RECORD(FP_PRAGMA_OPTIONS);
1055 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001056 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001057 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001058 RECORD(MODULE_OFFSET_MAP);
1059 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001060 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001061 RECORD(FILE_SORTED_DECLS);
1062 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001063 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001064 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001065 RECORD(INTERESTING_IDENTIFIERS);
1066 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001067 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001068 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001069 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001070 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001071 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001072 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001073 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001074
Chris Lattner28fa4e62009-04-26 22:26:21 +00001075 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001076 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001077 RECORD(SM_SLOC_FILE_ENTRY);
1078 RECORD(SM_SLOC_BUFFER_ENTRY);
1079 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001080 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001081 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattner28fa4e62009-04-26 22:26:21 +00001083 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001084 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001085 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001086 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001087 RECORD(PP_MACRO_OBJECT_LIKE);
1088 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001089 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001090
Richard Smithfa715652015-08-31 22:43:10 +00001091 // Submodule Block.
1092 BLOCK(SUBMODULE_BLOCK);
1093 RECORD(SUBMODULE_METADATA);
1094 RECORD(SUBMODULE_DEFINITION);
1095 RECORD(SUBMODULE_UMBRELLA_HEADER);
1096 RECORD(SUBMODULE_HEADER);
1097 RECORD(SUBMODULE_TOPHEADER);
1098 RECORD(SUBMODULE_UMBRELLA_DIR);
1099 RECORD(SUBMODULE_IMPORTS);
1100 RECORD(SUBMODULE_EXPORTS);
1101 RECORD(SUBMODULE_REQUIRES);
1102 RECORD(SUBMODULE_EXCLUDED_HEADER);
1103 RECORD(SUBMODULE_LINK_LIBRARY);
1104 RECORD(SUBMODULE_CONFIG_MACRO);
1105 RECORD(SUBMODULE_CONFLICT);
1106 RECORD(SUBMODULE_PRIVATE_HEADER);
1107 RECORD(SUBMODULE_TEXTUAL_HEADER);
1108 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001109 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001110
1111 // Comments Block.
1112 BLOCK(COMMENTS_BLOCK);
1113 RECORD(COMMENTS_RAW_COMMENT);
1114
Douglas Gregor12bfa382009-10-17 00:13:19 +00001115 // Decls and Types block.
1116 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001117 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001118 RECORD(TYPE_COMPLEX);
1119 RECORD(TYPE_POINTER);
1120 RECORD(TYPE_BLOCK_POINTER);
1121 RECORD(TYPE_LVALUE_REFERENCE);
1122 RECORD(TYPE_RVALUE_REFERENCE);
1123 RECORD(TYPE_MEMBER_POINTER);
1124 RECORD(TYPE_CONSTANT_ARRAY);
1125 RECORD(TYPE_INCOMPLETE_ARRAY);
1126 RECORD(TYPE_VARIABLE_ARRAY);
1127 RECORD(TYPE_VECTOR);
1128 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001129 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001130 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001131 RECORD(TYPE_TYPEDEF);
1132 RECORD(TYPE_TYPEOF_EXPR);
1133 RECORD(TYPE_TYPEOF);
1134 RECORD(TYPE_RECORD);
1135 RECORD(TYPE_ENUM);
1136 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001137 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001138 RECORD(TYPE_DECLTYPE);
1139 RECORD(TYPE_ELABORATED);
1140 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1141 RECORD(TYPE_UNRESOLVED_USING);
1142 RECORD(TYPE_INJECTED_CLASS_NAME);
1143 RECORD(TYPE_OBJC_OBJECT);
1144 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1145 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1146 RECORD(TYPE_DEPENDENT_NAME);
1147 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1148 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1149 RECORD(TYPE_PAREN);
1150 RECORD(TYPE_PACK_EXPANSION);
1151 RECORD(TYPE_ATTRIBUTED);
1152 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001153 RECORD(TYPE_AUTO);
1154 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001155 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001156 RECORD(TYPE_DECAYED);
1157 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001158 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001159 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001160 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001161 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001162 RECORD(DECL_ENUM);
1163 RECORD(DECL_RECORD);
1164 RECORD(DECL_ENUM_CONSTANT);
1165 RECORD(DECL_FUNCTION);
1166 RECORD(DECL_OBJC_METHOD);
1167 RECORD(DECL_OBJC_INTERFACE);
1168 RECORD(DECL_OBJC_PROTOCOL);
1169 RECORD(DECL_OBJC_IVAR);
1170 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001171 RECORD(DECL_OBJC_CATEGORY);
1172 RECORD(DECL_OBJC_CATEGORY_IMPL);
1173 RECORD(DECL_OBJC_IMPLEMENTATION);
1174 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1175 RECORD(DECL_OBJC_PROPERTY);
1176 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001177 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001178 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001179 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001180 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001181 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001182 RECORD(DECL_FILE_SCOPE_ASM);
1183 RECORD(DECL_BLOCK);
1184 RECORD(DECL_CONTEXT_LEXICAL);
1185 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001186 RECORD(DECL_NAMESPACE);
1187 RECORD(DECL_NAMESPACE_ALIAS);
1188 RECORD(DECL_USING);
1189 RECORD(DECL_USING_SHADOW);
1190 RECORD(DECL_USING_DIRECTIVE);
1191 RECORD(DECL_UNRESOLVED_USING_VALUE);
1192 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1193 RECORD(DECL_LINKAGE_SPEC);
1194 RECORD(DECL_CXX_RECORD);
1195 RECORD(DECL_CXX_METHOD);
1196 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001197 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001198 RECORD(DECL_CXX_DESTRUCTOR);
1199 RECORD(DECL_CXX_CONVERSION);
1200 RECORD(DECL_ACCESS_SPEC);
1201 RECORD(DECL_FRIEND);
1202 RECORD(DECL_FRIEND_TEMPLATE);
1203 RECORD(DECL_CLASS_TEMPLATE);
1204 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1205 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001206 RECORD(DECL_VAR_TEMPLATE);
1207 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1208 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001209 RECORD(DECL_FUNCTION_TEMPLATE);
1210 RECORD(DECL_TEMPLATE_TYPE_PARM);
1211 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1212 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001213 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001214 RECORD(DECL_STATIC_ASSERT);
1215 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001216 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001217 RECORD(DECL_INDIRECTFIELD);
1218 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001219 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1220 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1221 RECORD(DECL_IMPORT);
1222 RECORD(DECL_OMP_THREADPRIVATE);
1223 RECORD(DECL_EMPTY);
1224 RECORD(DECL_OBJC_TYPE_PARAM);
1225 RECORD(DECL_OMP_CAPTUREDEXPR);
1226 RECORD(DECL_PRAGMA_COMMENT);
1227 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1228 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001229
Douglas Gregor03412ba2011-06-03 02:27:19 +00001230 // Statements and Exprs can occur in the Decls and Types block.
1231 AddStmtsExprs(Stream, Record);
1232
Douglas Gregor92a96f52011-02-08 21:58:10 +00001233 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001234 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001235 RECORD(PPD_MACRO_DEFINITION);
1236 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001237
1238 // Decls and Types block.
1239 BLOCK(EXTENSION_BLOCK);
1240 RECORD(EXTENSION_METADATA);
1241
Chris Lattner28fa4e62009-04-26 22:26:21 +00001242#undef RECORD
1243#undef BLOCK
1244 Stream.ExitBlock();
1245}
1246
Richard Smith54cc3c22014-12-11 20:50:24 +00001247/// \brief Prepares a path for being written to an AST file by converting it
1248/// to an absolute path and removing nested './'s.
1249///
1250/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001251static bool cleanPathForOutput(FileManager &FileMgr,
1252 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001253 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001254 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001255}
1256
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001257/// \brief Adjusts the given filename to only write out the portion of the
1258/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001259///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001260/// \param Filename the file name to adjust.
1261///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001262/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1263/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001264///
1265/// \returns either the original filename (if it needs no adjustment) or the
1266/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001267static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001268adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001269 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001270
Richard Smith7ed1bc92014-12-05 22:42:13 +00001271 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001272 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001273
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001274 // Verify that the filename and the system root have the same prefix.
1275 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001276 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1277 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001278 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001279
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001280 // We hit the end of the filename before we hit the end of the system root.
1281 if (!Filename[Pos])
1282 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001283
Richard Smith7ed1bc92014-12-05 22:42:13 +00001284 // If there's not a path separator at the end of the base directory nor
1285 // immediately after it, then this isn't within the base directory.
1286 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1287 if (!llvm::sys::path::is_separator(BaseDir.back()))
1288 return Filename;
1289 } else {
1290 // If the file name has a '/' at the current position, skip over the '/'.
1291 // We distinguish relative paths from absolute paths by the
1292 // absence of '/' at the beginning of relative paths.
1293 //
1294 // FIXME: This is wrong. We distinguish them by asking if the path is
1295 // absolute, which isn't the same thing. And there might be multiple '/'s
1296 // in a row. Use a better mechanism to indicate whether we have emitted an
1297 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001298 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001299 }
Mike Stump11289f42009-09-09 15:08:12 +00001300
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001301 return Filename + Pos;
1302}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001303
Ben Langmuir487ea142014-10-23 18:05:36 +00001304static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001305 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001306 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1307 return S;
1308 // Rely on GetRandomNumber to eventually return non-zero...
1309 }
1310}
1311
Douglas Gregor112b9072012-10-18 05:31:06 +00001312/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001313uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1314 ASTContext &Context,
1315 StringRef isysroot,
1316 const std::string &OutputFile) {
1317 ASTFileSignature Signature = 0;
1318
Douglas Gregorbfbde532009-04-10 21:16:55 +00001319 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001320 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001321 RecordData Record;
1322
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001323 // Metadata
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001324 auto *MetadataAbbrev = new BitCodeAbbrev();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001325 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1326 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1327 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1328 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1329 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1330 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001331 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001332 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1333 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1334 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001335 assert((!WritingModule || isysroot.empty()) &&
1336 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001337 {
1338 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1339 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1340 !isysroot.empty(), IncludeTimestamps,
1341 ASTHasCompilerErrors};
1342 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1343 getClangFullRepositoryVersion());
1344 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001345 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001346 // For implicit modules we output a signature that we can use to ensure
1347 // duplicate module builds don't collide in the cache as their output order
1348 // is non-deterministic.
1349 // FIXME: Remove this when output is deterministic.
1350 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001351 Signature = getSignature();
1352 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001353 Stream.EmitRecord(SIGNATURE, Record);
1354 }
1355
Richard Smith7ed1bc92014-12-05 22:42:13 +00001356 // Module name
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001357 auto *Abbrev = new BitCodeAbbrev();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001358 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1360 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00001361 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001362 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1363 }
1364
Richard Smith7ed1bc92014-12-05 22:42:13 +00001365 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001366 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001367 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001368
1369 // If the home of the module is the current working directory, then we
1370 // want to pick up the cwd of the build process loading the module, not
1371 // our cwd, when we load this module.
1372 if (!PP.getHeaderSearchInfo()
1373 .getHeaderSearchOpts()
1374 .ModuleMapFileHomeIsCwd ||
1375 WritingModule->Directory->getName() != StringRef(".")) {
1376 // Module directory.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001377 auto *Abbrev = new BitCodeAbbrev();
Richard Smithf7b41372015-08-13 23:47:44 +00001378 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1380 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1381
Mehdi Amini57a41912015-09-10 01:46:39 +00001382 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001383 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1384 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001385
1386 // Write out all other paths relative to the base directory if possible.
1387 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1388 } else if (!isysroot.empty()) {
1389 // Write out paths relative to the sysroot if possible.
1390 BaseDirectory = isysroot;
1391 }
1392
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001393 // Module map file
1394 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001395 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001396
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001397 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1398
1399 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001400 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001401
1402 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001403 if (auto *AdditionalModMaps =
1404 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001405 Record.push_back(AdditionalModMaps->size());
1406 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001407 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001408 } else {
1409 Record.push_back(0);
1410 }
1411
1412 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001413 }
1414
Douglas Gregor112b9072012-10-18 05:31:06 +00001415 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001416 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001417 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001418 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001419
Richard Smith7f330cd2015-03-18 01:42:29 +00001420 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001421 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001422 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001423 continue;
1424
Richard Smith7f330cd2015-03-18 01:42:29 +00001425 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1426 AddSourceLocation(M->ImportLoc, Record);
1427 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001428 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001429 Record.push_back(M->Signature);
1430 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001431 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001432 Stream.EmitRecord(IMPORTS, Record);
1433 }
Mike Stump11289f42009-09-09 15:08:12 +00001434
Richard Smith0516b182015-09-08 19:40:14 +00001435 // Write the options block.
1436 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1437
Douglas Gregor112b9072012-10-18 05:31:06 +00001438 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001439 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001440 const LangOptions &LangOpts = Context.getLangOpts();
1441#define LANGOPT(Name, Bits, Default, Description) \
1442 Record.push_back(LangOpts.Name);
1443#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1444 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001445#include "clang/Basic/LangOptions.def"
1446#define SANITIZER(NAME, ID) \
1447 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001448#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001449
Ben Langmuircd98cb72015-06-23 18:20:18 +00001450 Record.push_back(LangOpts.ModuleFeatures.size());
1451 for (StringRef Feature : LangOpts.ModuleFeatures)
1452 AddString(Feature, Record);
1453
Douglas Gregor112b9072012-10-18 05:31:06 +00001454 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1455 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001456
1457 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001458
1459 // Comment options.
1460 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001461 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1462 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001463 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001464 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001465
Samuel Antaoee8fb302016-01-06 13:42:12 +00001466 // OpenMP offloading options.
1467 Record.push_back(LangOpts.OMPTargetTriples.size());
1468 for (auto &T : LangOpts.OMPTargetTriples)
1469 AddString(T.getTriple(), Record);
1470
1471 AddString(LangOpts.OMPHostIRFile, Record);
1472
Douglas Gregor112b9072012-10-18 05:31:06 +00001473 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1474
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001475 // Target options.
1476 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001477 const TargetInfo &Target = Context.getTargetInfo();
1478 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001479 AddString(TargetOpts.Triple, Record);
1480 AddString(TargetOpts.CPU, Record);
1481 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001482 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1483 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1484 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1485 }
1486 Record.push_back(TargetOpts.Features.size());
1487 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1488 AddString(TargetOpts.Features[I], Record);
1489 }
1490 Stream.EmitRecord(TARGET_OPTIONS, Record);
1491
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001492 // Diagnostic options.
1493 Record.clear();
1494 const DiagnosticOptions &DiagOpts
1495 = Context.getDiagnostics().getDiagnosticOptions();
1496#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1497#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1498 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1499#include "clang/Basic/DiagnosticOptions.def"
1500 Record.push_back(DiagOpts.Warnings.size());
1501 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1502 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001503 Record.push_back(DiagOpts.Remarks.size());
1504 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1505 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001506 // Note: we don't serialize the log or serialization file names, because they
1507 // are generally transient files and will almost always be overridden.
1508 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1509
Douglas Gregorc6317db2012-10-24 15:49:58 +00001510 // File system options.
1511 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001512 const FileSystemOptions &FSOpts =
1513 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001514 AddString(FSOpts.WorkingDir, Record);
1515 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1516
Douglas Gregor2d302362012-10-24 16:50:34 +00001517 // Header search options.
1518 Record.clear();
1519 const HeaderSearchOptions &HSOpts
1520 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1521 AddString(HSOpts.Sysroot, Record);
1522
1523 // Include entries.
1524 Record.push_back(HSOpts.UserEntries.size());
1525 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1526 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1527 AddString(Entry.Path, Record);
1528 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001529 Record.push_back(Entry.IsFramework);
1530 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001531 }
1532
1533 // System header prefixes.
1534 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1535 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1536 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1537 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1538 }
1539
1540 AddString(HSOpts.ResourceDir, Record);
1541 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001542 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001543 Record.push_back(HSOpts.DisableModuleHash);
1544 Record.push_back(HSOpts.UseBuiltinIncludes);
1545 Record.push_back(HSOpts.UseStandardSystemIncludes);
1546 Record.push_back(HSOpts.UseStandardCXXIncludes);
1547 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001548 // Write out the specific module cache path that contains the module files.
1549 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001550 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1551
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001552 // Preprocessor options.
1553 Record.clear();
1554 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1555
1556 // Macro definitions.
1557 Record.push_back(PPOpts.Macros.size());
1558 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1559 AddString(PPOpts.Macros[I].first, Record);
1560 Record.push_back(PPOpts.Macros[I].second);
1561 }
1562
1563 // Includes
1564 Record.push_back(PPOpts.Includes.size());
1565 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1566 AddString(PPOpts.Includes[I], Record);
1567
1568 // Macro includes
1569 Record.push_back(PPOpts.MacroIncludes.size());
1570 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1571 AddString(PPOpts.MacroIncludes[I], Record);
1572
Douglas Gregorb6368752012-10-24 23:41:50 +00001573 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001574 // Detailed record is important since it is used for the module cache hash.
1575 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001576 AddString(PPOpts.ImplicitPCHInclude, Record);
1577 AddString(PPOpts.ImplicitPTHInclude, Record);
1578 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1579 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1580
Richard Smith0516b182015-09-08 19:40:14 +00001581 // Leave the options block.
1582 Stream.ExitBlock();
1583
Douglas Gregora3b20262011-05-06 21:43:30 +00001584 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001585 SourceManager &SM = Context.getSourceManager();
1586 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001587 auto *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001588 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1589 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001590 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1591 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1592
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001593 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001594 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001595 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001596 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001597 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001598
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001599 Record.clear();
1600 Record.push_back(SM.getMainFileID().getOpaqueValue());
1601 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1602
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001603 // Original PCH directory
1604 if (!OutputFile.empty() && OutputFile != "-") {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001605 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001606 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1608 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1609
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001610 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001611
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001612 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001613 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1614
Mehdi Amini57a41912015-09-10 01:46:39 +00001615 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001616 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1617 }
1618
Douglas Gregor49491f72013-03-15 22:15:07 +00001619 WriteInputFiles(Context.SourceMgr,
1620 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001621 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001622 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001623 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001624}
1625
Douglas Gregor49491f72013-03-15 22:15:07 +00001626namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001627
Douglas Gregor49491f72013-03-15 22:15:07 +00001628 /// \brief An input file.
1629 struct InputFileEntry {
1630 const FileEntry *File;
1631 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001632 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001633 bool BufferOverridden;
1634 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001635
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001636} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001637
1638void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1639 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001640 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001641 using namespace llvm;
1642 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001643
Douglas Gregor72be3902012-10-19 00:38:02 +00001644 // Create input-file abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001645 auto *IFAbbrev = new BitCodeAbbrev();
Douglas Gregor72be3902012-10-19 00:38:02 +00001646 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001647 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001648 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1649 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001650 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001651 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001652 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1653 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1654
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001655 // Get all ContentCache objects for files, sorted by whether the file is a
1656 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001657 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001658 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1659 // Get this source location entry.
1660 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001661 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001662
1663 // We only care about file entries that were not overridden.
1664 if (!SLoc->isFile())
1665 continue;
1666 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001667 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001668 continue;
1669
Douglas Gregor49491f72013-03-15 22:15:07 +00001670 InputFileEntry Entry;
1671 Entry.File = Cache->OrigEntry;
1672 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001673 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001674 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001675 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001676 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001677 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001678 SortedFiles.push_front(Entry);
1679 }
1680
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001681 unsigned UserFilesNum = 0;
1682 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001683 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001684 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001685 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001686 if (InputFileID != 0)
1687 continue; // already recorded this file.
1688
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001689 // Record this entry's offset.
1690 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001691
1692 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001693
Douglas Gregor49491f72013-03-15 22:15:07 +00001694 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001695 ++UserFilesNum;
1696
Douglas Gregor72be3902012-10-19 00:38:02 +00001697 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001698 // And whether this file was overridden.
1699 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001700 INPUT_FILE,
1701 InputFileOffsets.size(),
1702 (uint64_t)Entry.File->getSize(),
1703 (uint64_t)getTimestampForOutput(Entry.File),
1704 Entry.BufferOverridden,
1705 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001706
Richard Smith7ed1bc92014-12-05 22:42:13 +00001707 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1708 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001709
Douglas Gregor112b9072012-10-18 05:31:06 +00001710 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001711
1712 // Create input file offsets abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001713 auto *OffsetsAbbrev = new BitCodeAbbrev();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001714 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1715 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001716 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1717 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001718 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1719 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1720
1721 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001722 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1723 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001724 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001725}
1726
Douglas Gregora7f71a92009-04-10 03:52:48 +00001727//===----------------------------------------------------------------------===//
1728// Source Manager Serialization
1729//===----------------------------------------------------------------------===//
1730
1731/// \brief Create an abbreviation for the SLocEntry that refers to a
1732/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001733static unsigned CreateSLocFileAbbrev(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_FILE_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
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001742 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001747 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001748}
1749
1750/// \brief Create an abbreviation for the SLocEntry that refers to a
1751/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001752static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001753 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001754
1755 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001756 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001762 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001763}
1764
1765/// \brief Create an abbreviation for the SLocEntry that refers to a
1766/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001767static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1768 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001769 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001770
1771 auto *Abbrev = new BitCodeAbbrev();
Richard Smithaada85c2016-02-06 02:06:43 +00001772 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1773 : SM_SLOC_BUFFER_BLOB));
1774 if (Compressed)
1775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001777 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001778}
1779
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001780/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1781/// expansion.
1782static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001783 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001784
1785 auto *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001786 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001792 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001793}
1794
Douglas Gregor09b69892011-02-10 17:09:37 +00001795namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001796
Douglas Gregor09b69892011-02-10 17:09:37 +00001797 // Trait used for the on-disk hash table of header search information.
1798 class HeaderFileInfoTrait {
1799 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001800 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001801
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001802 // Keep track of the framework names we've used during serialization.
1803 SmallVector<char, 128> FrameworkStringData;
1804 llvm::StringMap<unsigned> FrameworkNameOffset;
1805
Douglas Gregor09b69892011-02-10 17:09:37 +00001806 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001807 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1808 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001809
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001810 struct key_type {
1811 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001812 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001813 };
1814 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001815
1816 typedef HeaderFileInfo data_type;
1817 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001818 typedef unsigned hash_value_type;
1819 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001820
Richard Smithe75ee0f2015-08-17 07:13:32 +00001821 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001822 // The hash is based only on size/time of the file, so that the reader can
1823 // match even when symlinking or excess path elements ("foo/../", "../")
1824 // change the form of the name. However, complete path is still the key.
1825 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001826 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001827 }
1828
1829 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001830 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001831 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001832 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001833 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001834 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001835 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001836 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1837 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1838 DataLen += 4;
1839 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001840 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001841 }
1842
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001843 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001844 using namespace llvm::support;
1845 endian::Writer<little> LE(Out);
1846 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001847 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001848 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001849 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001850 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001851 }
1852
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001853 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001854 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001855 using namespace llvm::support;
1856 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001857 uint64_t Start = Out.tell(); (void)Start;
1858
Richard Smith386bb072015-08-18 23:42:23 +00001859 unsigned char Flags = (Data.isImport << 4)
1860 | (Data.isPragmaOnce << 3)
1861 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001862 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001863 LE.write<uint8_t>(Flags);
1864 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001865
1866 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001867 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001868 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001869 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001870
1871 unsigned Offset = 0;
1872 if (!Data.Framework.empty()) {
1873 // If this header refers into a framework, save the framework name.
1874 llvm::StringMap<unsigned>::iterator Pos
1875 = FrameworkNameOffset.find(Data.Framework);
1876 if (Pos == FrameworkNameOffset.end()) {
1877 Offset = FrameworkStringData.size() + 1;
1878 FrameworkStringData.append(Data.Framework.begin(),
1879 Data.Framework.end());
1880 FrameworkStringData.push_back(0);
1881
1882 FrameworkNameOffset[Data.Framework] = Offset;
1883 } else
1884 Offset = Pos->second;
1885 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001886 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001887
Richard Smith386bb072015-08-18 23:42:23 +00001888 // FIXME: If the header is excluded, we should write out some
1889 // record of that fact.
1890 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1891 if (uint32_t ModID =
1892 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1893 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1894 assert((Value >> 2) == ModID && "overflow in header module info");
1895 LE.write<uint32_t>(Value);
1896 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001897 }
1898
Douglas Gregor09b69892011-02-10 17:09:37 +00001899 assert(Out.tell() - Start == DataLen && "Wrong data length");
1900 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001901
1902 const char *strings_begin() const { return FrameworkStringData.begin(); }
1903 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001904 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001905
Douglas Gregor09b69892011-02-10 17:09:37 +00001906} // end anonymous namespace
1907
1908/// \brief Write the header search block for the list of files that
1909///
1910/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001911void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001912 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001913 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1914
1915 if (FilesByUID.size() > HS.header_file_size())
1916 FilesByUID.resize(HS.header_file_size());
1917
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001918 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001919 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001920 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001921 unsigned NumHeaderSearchEntries = 0;
1922 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1923 const FileEntry *File = FilesByUID[UID];
1924 if (!File)
1925 continue;
1926
Richard Smith386bb072015-08-18 23:42:23 +00001927 // Get the file info. This will load info from the external source if
1928 // necessary. Skip emitting this file if we have no information on it
1929 // as a header file (in which case HFI will be null) or if it hasn't
1930 // changed since it was loaded. Also skip it if it's for a modular header
1931 // from a different module; in that case, we rely on the module(s)
1932 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001933 const HeaderFileInfo *HFI =
1934 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1935 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001936 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001937
Richard Smith7ed1bc92014-12-05 22:42:13 +00001938 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001939 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001940 SmallString<128> FilenameTmp(Filename);
1941 if (PreparePathForOutput(FilenameTmp)) {
1942 // If we performed any translation on the file name at all, we need to
1943 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001944 Filename = StringRef(strdup(FilenameTmp.c_str()));
1945 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001946 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001947
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001948 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001949 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001950 ++NumHeaderSearchEntries;
1951 }
1952
1953 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001954 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001955 uint32_t BucketOffset;
1956 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001957 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001958 llvm::raw_svector_ostream Out(TableData);
1959 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001960 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001961 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1962 }
1963
1964 // Create a blob abbreviation
1965 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001966
1967 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor09b69892011-02-10 17:09:37 +00001968 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1973 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1974
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001975 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001976 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1977 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001978 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001979 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001980
1981 // Free all of the strings we had to duplicate.
1982 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001983 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001984}
1985
Douglas Gregora7f71a92009-04-10 03:52:48 +00001986/// \brief Writes the block containing the serialized form of the
1987/// source manager.
1988///
1989/// TODO: We should probably use an on-disk hash table (stored in a
1990/// blob), indexed based on the file name, so that we only create
1991/// entries for files that we actually need. In the common case (no
1992/// errors), we probably won't have to create file entries for any of
1993/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001994void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001995 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001996 RecordData Record;
1997
Chris Lattner0910e3b2009-04-10 17:16:57 +00001998 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00001999 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002000
2001 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002002 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2003 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002004 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2005 unsigned SLocBufferBlobCompressedAbbrv =
2006 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002007 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002008
Douglas Gregor258ae542009-04-27 06:38:32 +00002009 // Write out the source location entry table. We skip the first
2010 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002011 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002012 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002013 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2014 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002015 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002016 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002017 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002018 FileID FID = FileID::get(I);
2019 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002020
Douglas Gregor258ae542009-04-27 06:38:32 +00002021 // Record the offset of this source-location entry.
2022 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2023
2024 // Figure out which record code to use.
2025 unsigned Code;
2026 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002027 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2028 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002029 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002030 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002031 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002032 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002033 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002034 Record.clear();
2035 Record.push_back(Code);
2036
Douglas Gregor925296b2011-07-19 16:10:42 +00002037 // Starting offset of this entry within this module, so skip the dummy.
2038 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002039 if (SLoc->isFile()) {
2040 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002041 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002042 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2043 Record.push_back(File.hasLineDirectives());
2044
2045 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002046 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002047 if (Content->OrigEntry) {
2048 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002049 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002050
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002051 // The source location entry is a file. Emit input file ID.
2052 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2053 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002054
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002055 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002056
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002057 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002058 if (FDI != FileDeclIDs.end()) {
2059 Record.push_back(FDI->second->FirstDeclIndex);
2060 Record.push_back(FDI->second->DeclIDs.size());
2061 } else {
2062 Record.push_back(0);
2063 Record.push_back(0);
2064 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002065
2066 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2067
Richard Smithaada85c2016-02-06 02:06:43 +00002068 if (Content->BufferOverridden || Content->IsTransient)
2069 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002070 } else {
2071 // The source location entry is a buffer. The blob associated
2072 // with this entry contains the contents of the buffer.
2073
2074 // We add one to the size so that we capture the trailing NULL
2075 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2076 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002077 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002078 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002079 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002080 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002081 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002082 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002083
Mehdi Amini99d1b292016-10-01 16:38:28 +00002084 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002085 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002086 }
Richard Smithaada85c2016-02-06 02:06:43 +00002087
2088 if (EmitBlob) {
2089 // Include the implicit terminating null character in the on-disk buffer
2090 // if we're writing it uncompressed.
2091 const llvm::MemoryBuffer *Buffer =
2092 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2093 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
2094
2095 // Compress the buffer if possible. We expect that almost all PCM
2096 // consumers will not want its contents.
2097 SmallString<0> CompressedBuffer;
2098 if (llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer) ==
2099 llvm::zlib::StatusOK) {
2100 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2101 Blob.size() - 1};
2102 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2103 CompressedBuffer);
2104 } else {
2105 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
2106 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2107 }
2108 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002109 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002110 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002111 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002112 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2113 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2114 AddSourceLocation(Expansion.isMacroArgExpansion()
2115 ? SourceLocation()
2116 : Expansion.getExpansionLocEnd(),
2117 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002118
2119 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002120 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002121 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002122 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002123 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002124 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002125 }
2126 }
2127
Douglas Gregor8f45df52009-04-16 22:23:12 +00002128 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002129
2130 if (SLocEntryOffsets.empty())
2131 return;
2132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002133 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002134 // table is used for lazily loading source-location information.
2135 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002136
2137 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002138 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002140 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
2142 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002143 {
2144 RecordData::value_type Record[] = {
2145 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2146 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2147 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2148 bytes(SLocEntryOffsets));
2149 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002150 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002151 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002152 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002153
2154 // Write the line table. It depends on remapping working, so it must come
2155 // after the source location offsets.
2156 if (SourceMgr.hasLineTable()) {
2157 LineTableInfo &LineTable = SourceMgr.getLineTable();
2158
2159 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002160
2161 // Emit the needed file names.
2162 llvm::DenseMap<int, int> FilenameMap;
2163 for (const auto &L : LineTable) {
2164 if (L.first.ID < 0)
2165 continue;
2166 for (auto &LE : L.second) {
2167 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2168 FilenameMap.size())).second)
2169 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2170 }
2171 }
2172 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002173
2174 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002175 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002176 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002177 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002178 continue;
2179
2180 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002181 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002182
2183 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002184 Record.push_back(L.second.size());
2185 for (const auto &LE : L.second) {
2186 Record.push_back(LE.FileOffset);
2187 Record.push_back(LE.LineNo);
2188 Record.push_back(FilenameMap[LE.FilenameID]);
2189 Record.push_back((unsigned)LE.FileKind);
2190 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002191 }
2192 }
Richard Smith63078492015-09-01 07:41:55 +00002193
Douglas Gregor925296b2011-07-19 16:10:42 +00002194 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2195 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002196}
2197
Douglas Gregorc5046832009-04-27 18:38:38 +00002198//===----------------------------------------------------------------------===//
2199// Preprocessor Serialization
2200//===----------------------------------------------------------------------===//
2201
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002202static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2203 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002204 if (MacroInfo *MI = MD->getMacroInfo())
2205 if (MI->isBuiltinMacro())
2206 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002207
2208 if (IsModule) {
2209 SourceLocation Loc = MD->getLocation();
2210 if (Loc.isInvalid())
2211 return true;
2212 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2213 return true;
2214 }
2215
2216 return false;
2217}
2218
Chris Lattnereeffaef2009-04-10 17:15:23 +00002219/// \brief Writes the block containing the serialized form of the
2220/// preprocessor.
2221///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002222void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002223 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2224 if (PPRec)
2225 WritePreprocessorDetail(*PPRec);
2226
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002227 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002228 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002229
Chris Lattner0af3ba12009-04-13 01:29:17 +00002230 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2231 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002232 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002233 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002234 }
2235
2236 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002237 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002239 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002240 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002241 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002242 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002243
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002244 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002245 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002246
Richard Smithee977932015-05-01 21:22:17 +00002247 // Construct the list of identifiers with macro directives that need to be
2248 // serialized.
2249 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2250 for (auto &Id : PP.getIdentifierTable())
2251 if (Id.second->hadMacroDefinition() &&
2252 (!Id.second->isFromAST() ||
2253 Id.second->hasChangedSinceDeserialization()))
2254 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002255 // Sort the set of macro definitions that need to be serialized by the
2256 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002257 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2258 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002259
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002260 // Emit the macro directives as a list and associate the offset with the
2261 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002262 for (const IdentifierInfo *Name : MacroIdentifiers) {
2263 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002264 auto StartOffset = Stream.GetCurrentBitNo();
2265
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002266 // Emit the macro directives in reverse source order.
2267 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002268 // Once we hit an ignored macro, we're done: the rest of the chain
2269 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002270 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002271 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002272
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002273 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002274 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002275 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002276 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002277 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002278 Record.push_back(VisMD->isPublic());
2279 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002280 }
Richard Smithd7329392015-04-21 21:46:32 +00002281
Richard Smithb8b2ed62015-04-23 18:18:26 +00002282 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002283 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002284 // We write out exported module macros for PCH as well.
2285 auto Leafs = PP.getLeafModuleMacros(Name);
2286 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2287 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2288 while (!Worklist.empty()) {
2289 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002290
Manman Ren33d80292016-05-31 18:19:32 +00002291 // Emit a record indicating this submodule exports this macro.
2292 ModuleMacroRecord.push_back(
2293 getSubmoduleID(Macro->getOwningModule()));
2294 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2295 for (auto *M : Macro->overrides())
2296 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002297
Manman Ren33d80292016-05-31 18:19:32 +00002298 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2299 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002300
Manman Ren33d80292016-05-31 18:19:32 +00002301 // Enqueue overridden macros once we've visited all their ancestors.
2302 for (auto *M : Macro->overrides())
2303 if (++Visits[M] == M->getNumOverridingMacros())
2304 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002305
Manman Ren33d80292016-05-31 18:19:32 +00002306 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002307 }
Richard Smithd7329392015-04-21 21:46:32 +00002308
Richard Smithee977932015-05-01 21:22:17 +00002309 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002310 continue;
2311
Richard Smithd7329392015-04-21 21:46:32 +00002312 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002313 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2314 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002315 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002316
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002317 /// \brief Offsets of each of the macros into the bitstream, indexed by
2318 /// the local macro ID
2319 ///
2320 /// For each identifier that is associated with a macro, this map
2321 /// provides the offset into the bitstream where that macro is
2322 /// defined.
2323 std::vector<uint32_t> MacroOffsets;
2324
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002325 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2326 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2327 MacroInfo *MI = MacroInfosToEmit[I].MI;
2328 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002329
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002330 if (ID < FirstMacroID) {
2331 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2332 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002333 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002334
2335 // Record the local offset of this macro.
2336 unsigned Index = ID - FirstMacroID;
2337 if (Index == MacroOffsets.size())
2338 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2339 else {
2340 if (Index > MacroOffsets.size())
2341 MacroOffsets.resize(Index + 1);
2342
2343 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2344 }
2345
2346 AddIdentifierRef(Name, Record);
2347 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2348 AddSourceLocation(MI->getDefinitionLoc(), Record);
2349 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2350 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002351 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002352 unsigned Code;
2353 if (MI->isObjectLike()) {
2354 Code = PP_MACRO_OBJECT_LIKE;
2355 } else {
2356 Code = PP_MACRO_FUNCTION_LIKE;
2357
2358 Record.push_back(MI->isC99Varargs());
2359 Record.push_back(MI->isGNUVarargs());
2360 Record.push_back(MI->hasCommaPasting());
2361 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002362 for (const IdentifierInfo *Arg : MI->args())
2363 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002364 }
2365
2366 // If we have a detailed preprocessing record, record the macro definition
2367 // ID that corresponds to this macro.
2368 if (PPRec)
2369 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2370
2371 Stream.EmitRecord(Code, Record);
2372 Record.clear();
2373
2374 // Emit the tokens array.
2375 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2376 // Note that we know that the preprocessor does not have any annotation
2377 // tokens in it because they are created by the parser, and thus can't
2378 // be in a macro definition.
2379 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002380 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002381 Stream.EmitRecord(PP_TOKEN, Record);
2382 Record.clear();
2383 }
2384 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002385 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002386
Douglas Gregor92a96f52011-02-08 21:58:10 +00002387 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002388
2389 // Write the offsets table for macro IDs.
2390 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002391
Richard Smith13090a22015-04-10 02:02:24 +00002392 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002393 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2397
2398 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002399 {
2400 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2401 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2402 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2403 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002404}
2405
2406void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002407 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002408 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002409
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002410 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002411
Douglas Gregor92a96f52011-02-08 21:58:10 +00002412 // Enter the preprocessor block.
2413 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002414
Douglas Gregoraae92242010-03-19 21:51:54 +00002415 // If the preprocessor has a preprocessing record, emit it.
2416 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002417 using namespace llvm;
2418
2419 // Set up the abbreviation for
2420 unsigned InclusionAbbrev = 0;
2421 {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002422 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002423 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2429 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2430 }
2431
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002432 unsigned FirstPreprocessorEntityID
2433 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2434 + NUM_PREDEF_PP_ENTITY_IDS;
2435 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002436 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002437 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2438 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002439 E != EEnd;
2440 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002441 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002442
Richard Smith66a81862015-05-04 02:25:31 +00002443 PreprocessedEntityOffsets.push_back(
2444 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002445
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002446 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002447 // Record this macro definition's ID.
2448 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002449
Douglas Gregor92a96f52011-02-08 21:58:10 +00002450 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002451 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2452 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002453 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002454
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002455 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002456 Record.push_back(ME->isBuiltinMacro());
2457 if (ME->isBuiltinMacro())
2458 AddIdentifierRef(ME->getName(), Record);
2459 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002460 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002461 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002462 continue;
2463 }
2464
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002465 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002466 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002467 Record.push_back(ID->getFileName().size());
2468 Record.push_back(ID->wasInQuotes());
2469 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002470 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002471 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002472 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002473 // Check that the FileEntry is not null because it was not resolved and
2474 // we create a PCH even with compiler errors.
2475 if (ID->getFile())
2476 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002477 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002478 continue;
2479 }
2480
2481 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2482 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002483 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002484
Douglas Gregoraae92242010-03-19 21:51:54 +00002485 // Write the offsets table for the preprocessing record.
2486 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002487 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2488
Douglas Gregoraae92242010-03-19 21:51:54 +00002489 // Write the offsets table for identifier IDs.
2490 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002491
2492 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002493 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002496 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002497
Mehdi Amini57a41912015-09-10 01:46:39 +00002498 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2499 FirstPreprocessorEntityID -
2500 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002501 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002502 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002503 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002504}
2505
Richard Smith386bb072015-08-18 23:42:23 +00002506unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002507 if (!Mod)
2508 return 0;
2509
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002510 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2511 if (Known != SubmoduleIDs.end())
2512 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002513
Richard Smithdc1f0422016-07-20 19:10:16 +00002514 auto *Top = Mod->getTopLevelModule();
2515 if (Top != WritingModule &&
2516 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule)))
Richard Smith386bb072015-08-18 23:42:23 +00002517 return 0;
2518
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002519 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2520}
2521
Richard Smith386bb072015-08-18 23:42:23 +00002522unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2523 // FIXME: This can easily happen, if we have a reference to a submodule that
2524 // did not result in us loading a module file for that submodule. For
2525 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2526 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2527 assert((ID || !Mod) &&
2528 "asked for module ID for non-local, non-imported module");
2529 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002530}
2531
Douglas Gregor253eefe2011-12-01 00:59:36 +00002532/// \brief Compute the number of modules within the given tree (including the
2533/// given module).
2534static unsigned getNumberOfModules(Module *Mod) {
2535 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002536 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002537 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002538 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002539
2540 return ChildModules + 1;
2541}
2542
Douglas Gregorde3ef502011-11-30 23:21:26 +00002543void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002544 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002545 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002546
2547 // Write the abbreviations needed for the submodules block.
2548 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002549
2550 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor69021972011-11-30 17:33:56 +00002551 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2563 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2564
2565 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002566 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2568 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2569
2570 Abbrev = new BitCodeAbbrev();
2571 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2573 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002574
2575 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002576 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2578 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2579
2580 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002581 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2583 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2584
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002585 Abbrev = new BitCodeAbbrev();
2586 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002589 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2590
Douglas Gregor59527662012-10-15 06:28:11 +00002591 Abbrev = new BitCodeAbbrev();
2592 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2594 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2595
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002596 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002597 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2599 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2600
2601 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002602 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2604 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2605
2606 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002607 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2609 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2610
2611 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002612 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2615 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2616
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002617 Abbrev = new BitCodeAbbrev();
2618 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2620 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2621
Douglas Gregorfb912652013-03-20 21:10:35 +00002622 Abbrev = new BitCodeAbbrev();
2623 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2626 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2627
Douglas Gregor253eefe2011-12-01 00:59:36 +00002628 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002629 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2630 FirstSubmoduleID -
2631 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002632 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2633
Douglas Gregor69021972011-11-30 17:33:56 +00002634 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002635 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002636 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002637 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002638 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002639 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002640 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002641
2642 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002643 if (Mod->Parent) {
2644 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002645 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002646 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002647
2648 // Emit the definition of the block.
2649 {
2650 RecordData::value_type Record[] = {
2651 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2652 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2653 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2654 Mod->ConfigMacrosExhaustive};
2655 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2656 }
2657
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002658 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002659 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002660 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002661 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002662 }
2663
Douglas Gregor69021972011-11-30 17:33:56 +00002664 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002665 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002666 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002667 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2668 UmbrellaHeader.NameAsWritten);
2669 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002670 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2671 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002672 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002673 }
Richard Smith306d8922014-10-22 23:50:56 +00002674
Douglas Gregor69021972011-11-30 17:33:56 +00002675 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002676 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002677 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002678 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002679 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002680 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002681 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2682 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2683 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002684 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002685 Module::HK_PrivateTextual},
2686 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002687 };
2688 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002689 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002690 for (auto &H : Mod->Headers[HL.HeaderKind])
2691 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2692 }
2693
2694 // Emit the top headers.
2695 {
2696 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002697 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002698 for (auto *H : TopHeaders)
2699 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002700 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002701
2702 // Emit the imports.
2703 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002704 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002705 for (auto *I : Mod->Imports)
2706 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002707 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2708 }
2709
Douglas Gregor24bb9232011-12-02 18:58:38 +00002710 // Emit the exports.
2711 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002712 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002713 for (const auto &E : Mod->Exports) {
2714 // FIXME: This may fail; we don't require that all exported modules
2715 // are local or imported.
2716 Record.push_back(getSubmoduleID(E.getPointer()));
2717 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002718 }
2719 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2720 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002721
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002722 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2723 // Might be unnecessary as use declarations are only used to build the
2724 // module itself.
2725
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002726 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002727 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002728 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2729 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002730 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002731 }
2732
Douglas Gregorfb912652013-03-20 21:10:35 +00002733 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002734 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002735 // FIXME: This may fail; we don't require that all conflicting modules
2736 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002737 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2738 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002739 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002740 }
2741
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002742 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002743 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002744 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002745 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002746 }
2747
Richard Smithdc1f0422016-07-20 19:10:16 +00002748 // Emit the initializers, if any.
2749 RecordData Inits;
2750 for (Decl *D : Context->getModuleInitializers(Mod))
2751 Inits.push_back(GetDeclRef(D));
2752 if (!Inits.empty())
2753 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2754
Douglas Gregor69021972011-11-30 17:33:56 +00002755 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002756 for (auto *M : Mod->submodules())
2757 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002758 }
2759
2760 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002761
Richard Smith23d8d032015-05-14 00:45:20 +00002762 assert((NextSubmoduleID - FirstSubmoduleID ==
2763 getNumberOfModules(WritingModule)) &&
2764 "Wrong # of submodules; found a reference to a non-local, "
2765 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002766}
2767
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002768serialization::SubmoduleID
2769ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002770 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002771 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002772
2773 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002774 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002775 Module *OwningMod
2776 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002777 if (!OwningMod)
2778 return 0;
2779
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002780 // Check whether this submodule is part of our own module.
2781 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002782 return 0;
2783
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002784 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002785}
2786
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002787void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2788 bool isModule) {
2789 // Make sure set diagnostic pragmas don't affect the translation unit that
2790 // imports the module.
2791 // FIXME: Make diagnostic pragma sections work properly with modules.
2792 if (isModule)
2793 return;
2794
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002795 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2796 DiagStateIDMap;
2797 unsigned CurrID = 0;
2798 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002799 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002800 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002801 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2802 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002803 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002804 if (point.Loc.isInvalid())
2805 continue;
2806
Richard Smithb22a1d12016-03-27 20:13:24 +00002807 AddSourceLocation(point.Loc, Record);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002808 unsigned &DiagStateID = DiagStateIDMap[point.State];
2809 Record.push_back(DiagStateID);
2810
2811 if (DiagStateID == 0) {
2812 DiagStateID = ++CurrID;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002813 for (const auto &I : *(point.State)) {
2814 if (I.second.isPragma()) {
2815 Record.push_back(I.first);
2816 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002817 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002818 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002819 Record.push_back(-1); // mark the end of the diag/map pairs for this
2820 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002821 }
2822 }
2823
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002824 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002825 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002826}
2827
Douglas Gregorc5046832009-04-27 18:38:38 +00002828//===----------------------------------------------------------------------===//
2829// Type Serialization
2830//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002831
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002832/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002833void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002834 TypeIdx &IdxRef = TypeIdxs[T];
2835 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2836 IdxRef = TypeIdx(NextTypeID++);
2837 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002838
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002839 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002840
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002841 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002842
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002843 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002844 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002845 W.Visit(T);
2846 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002847
Richard Smith290d8012016-04-06 17:06:00 +00002848 // Record the offset for this type.
2849 unsigned Index = Idx.getIndex() - FirstTypeID;
2850 if (TypeOffsets.size() == Index)
2851 TypeOffsets.push_back(Offset);
2852 else if (TypeOffsets.size() < Index) {
2853 TypeOffsets.resize(Index + 1);
2854 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002855 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002856 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002857 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002858}
2859
Douglas Gregorc5046832009-04-27 18:38:38 +00002860//===----------------------------------------------------------------------===//
2861// Declaration Serialization
2862//===----------------------------------------------------------------------===//
2863
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002864/// \brief Write the block containing all of the declaration IDs
2865/// lexically declared within the given DeclContext.
2866///
2867/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2868/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002869uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002870 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002871 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002872 return 0;
2873
Douglas Gregor8f45df52009-04-16 22:23:12 +00002874 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002875 SmallVector<uint32_t, 128> KindDeclPairs;
2876 for (const auto *D : DC->decls()) {
2877 KindDeclPairs.push_back(D->getKind());
2878 KindDeclPairs.push_back(GetDeclRef(D));
2879 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002880
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002881 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002882 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002883 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2884 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002885 return Offset;
2886}
2887
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002888void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002889 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002890
2891 // Write the type offsets array
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002892 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002893 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2897 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002898 {
2899 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2900 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2901 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2902 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002903
2904 // Write the declaration offsets array
2905 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002906 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2910 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002911 {
2912 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2913 FirstDeclID - NUM_PREDEF_DECL_IDS};
2914 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2915 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002916}
2917
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002918void ASTWriter::WriteFileDeclIDsMap() {
2919 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002920
Chandler Carruthb306d732015-03-27 00:31:20 +00002921 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2922 FileDeclIDs.begin(), FileDeclIDs.end());
2923 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2924 llvm::less_first());
2925
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002926 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002927 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2928 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2929 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2930 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2931 for (auto &LocDeclEntry : Info.DeclIDs)
2932 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002933 }
2934
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002935 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002936 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2939 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002940 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2941 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002942 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002943}
2944
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002945void ASTWriter::WriteComments() {
2946 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002947 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002948 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002949 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002950 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002951 AddSourceRange(I->getSourceRange(), Record);
2952 Record.push_back(I->getKind());
2953 Record.push_back(I->isTrailingComment());
2954 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002955 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2956 }
2957 Stream.ExitBlock();
2958}
2959
Douglas Gregorc5046832009-04-27 18:38:38 +00002960//===----------------------------------------------------------------------===//
2961// Global Method Pool and Selector Serialization
2962//===----------------------------------------------------------------------===//
2963
Douglas Gregore84a9da2009-04-20 20:36:09 +00002964namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002965
Douglas Gregorc78d3462009-04-24 21:10:55 +00002966// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002967class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002968 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002969
2970public:
2971 typedef Selector key_type;
2972 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002973
Sebastian Redl834bb972010-08-04 17:20:04 +00002974 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002975 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002976 ObjCMethodList Instance, Factory;
2977 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002978 typedef const data_type& data_type_ref;
2979
Justin Bogner25463f12014-04-18 20:27:24 +00002980 typedef unsigned hash_value_type;
2981 typedef unsigned offset_type;
2982
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002983 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002984
Justin Bogner25463f12014-04-18 20:27:24 +00002985 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002986 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002987 }
Mike Stump11289f42009-09-09 15:08:12 +00002988
2989 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002990 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002991 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002992 using namespace llvm::support;
2993 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002994 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002995 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002996 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2997 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002998 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002999 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003000 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003001 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003002 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003003 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003004 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003005 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003006 return std::make_pair(KeyLen, DataLen);
3007 }
Mike Stump11289f42009-09-09 15:08:12 +00003008
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003009 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003010 using namespace llvm::support;
3011 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003012 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003013 assert((Start >> 32) == 0 && "Selector key offset too large");
3014 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003015 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003016 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003017 if (N == 0)
3018 N = 1;
3019 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003020 LE.write<uint32_t>(
3021 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003022 }
Mike Stump11289f42009-09-09 15:08:12 +00003023
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003024 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003025 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003026 using namespace llvm::support;
3027 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003028 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003029 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003030 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003031 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003032 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003033 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003034 ++NumInstanceMethods;
3035
3036 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003037 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003038 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003039 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003040 ++NumFactoryMethods;
3041
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003042 unsigned InstanceBits = Methods.Instance.getBits();
3043 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003044 unsigned InstanceHasMoreThanOneDeclBit =
3045 Methods.Instance.hasMoreThanOneDecl();
3046 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3047 (InstanceHasMoreThanOneDeclBit << 2) |
3048 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003049 unsigned FactoryBits = Methods.Factory.getBits();
3050 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003051 unsigned FactoryHasMoreThanOneDeclBit =
3052 Methods.Factory.hasMoreThanOneDecl();
3053 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3054 (FactoryHasMoreThanOneDeclBit << 2) |
3055 FactoryBits;
3056 LE.write<uint16_t>(FullInstanceBits);
3057 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003058 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003059 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003060 if (Method->getMethod())
3061 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003062 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003063 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003064 if (Method->getMethod())
3065 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003066
3067 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003068 }
3069};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003070
Douglas Gregorc78d3462009-04-24 21:10:55 +00003071} // end anonymous namespace
3072
Sebastian Redla19a67f2010-08-03 21:58:15 +00003073/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003074///
3075/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003076/// in an on-disk hash table indexed by the selector. The hash table also
3077/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003078void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003079 using namespace llvm;
3080
Sebastian Redla19a67f2010-08-03 21:58:15 +00003081 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003082 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003083 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003084 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003085 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003086 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003087 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003088 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003089
Sebastian Redla19a67f2010-08-03 21:58:15 +00003090 // Create the on-disk hash table representation. We walk through every
3091 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003092 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003093 for (auto &SelectorAndID : SelectorIDs) {
3094 Selector S = SelectorAndID.first;
3095 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003096 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003097 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003098 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003099 ObjCMethodList(),
3100 ObjCMethodList()
3101 };
3102 if (F != SemaRef.MethodPool.end()) {
3103 Data.Instance = F->second.first;
3104 Data.Factory = F->second.second;
3105 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003106 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003107 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003108 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003109 // Selector already exists. Did it change?
3110 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003111 for (ObjCMethodList *M = &Data.Instance;
3112 !changed && M && M->getMethod(); M = M->getNext()) {
3113 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003114 changed = true;
3115 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003116 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003117 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003118 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003119 changed = true;
3120 }
3121 if (!changed)
3122 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003123 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003124 // A new method pool entry.
3125 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003126 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003127 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003128 }
3129
Douglas Gregorc78d3462009-04-24 21:10:55 +00003130 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003131 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003132 uint32_t BucketOffset;
3133 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003134 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003135 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003136 llvm::raw_svector_ostream Out(MethodPool);
3137 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003138 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003139 BucketOffset = Generator.Emit(Out, Trait);
3140 }
3141
3142 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003143 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003144 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3148 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3149
Douglas Gregor95c13f52009-04-25 17:48:32 +00003150 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003151 {
3152 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3153 NumTableEntries};
3154 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3155 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003156
3157 // Create a blob abbreviation for the selector table offsets.
3158 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003159 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3163 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3164
3165 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003166 {
3167 RecordData::value_type Record[] = {
3168 SELECTOR_OFFSETS, SelectorOffsets.size(),
3169 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3170 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3171 bytes(SelectorOffsets));
3172 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003173 }
3174}
3175
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003176/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003177void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003178 using namespace llvm;
3179 if (SemaRef.ReferencedSelectors.empty())
3180 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003181
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003182 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003183 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003184
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003185 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003186 // very tricky to fix, and given that @selector shouldn't really appear in
3187 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003188 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3189 Selector Sel = SelectorAndLocation.first;
3190 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003191 Writer.AddSelectorRef(Sel);
3192 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003193 }
Richard Smithe64e7452016-04-18 21:54:58 +00003194 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003195}
3196
Douglas Gregorc5046832009-04-27 18:38:38 +00003197//===----------------------------------------------------------------------===//
3198// Identifier Table Serialization
3199//===----------------------------------------------------------------------===//
3200
Richard Smithb3761912015-02-05 23:08:52 +00003201/// Determine the declaration that should be put into the name lookup table to
3202/// represent the given declaration in this module. This is usually D itself,
3203/// but if D was imported and merged into a local declaration, we want the most
3204/// recent local declaration instead. The chosen declaration will be the most
3205/// recent declaration in any module that imports this one.
3206static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3207 NamedDecl *D) {
3208 if (!LangOpts.Modules || !D->isFromASTFile())
3209 return D;
3210
3211 if (Decl *Redecl = D->getPreviousDecl()) {
3212 // For Redeclarable decls, a prior declaration might be local.
3213 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003214 // If we find a local decl, we're done.
3215 if (!Redecl->isFromASTFile()) {
3216 // Exception: in very rare cases (for injected-class-names), not all
3217 // redeclarations are in the same semantic context. Skip ones in a
3218 // different context. They don't go in this lookup table at all.
3219 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3220 D->getDeclContext()->getRedeclContext()))
3221 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003222 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003223 }
3224
Richard Smithfe620d22015-03-05 23:24:12 +00003225 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003226 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003227 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003228 break;
3229 }
3230 } else if (Decl *First = D->getCanonicalDecl()) {
3231 // For Mergeable decls, the first decl might be local.
3232 if (!First->isFromASTFile())
3233 return cast<NamedDecl>(First);
3234 }
3235
3236 // All declarations are imported. Our most recent declaration will also be
3237 // the most recent one in anyone who imports us.
3238 return D;
3239}
3240
Douglas Gregorc78d3462009-04-24 21:10:55 +00003241namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003242
Richard Smithcf97cf62015-04-19 01:34:23 +00003243class ASTIdentifierTableTrait {
3244 ASTWriter &Writer;
3245 Preprocessor &PP;
3246 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003247 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003248 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003249 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003250
3251 /// \brief Determines whether this is an "interesting" identifier that needs a
3252 /// full IdentifierInfo structure written into the hash table. Notably, this
3253 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3254 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003255 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003256 if (MacroOffset ||
3257 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003258 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003259 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003260 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003261 return true;
3262
3263 return false;
3264 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003265
Douglas Gregore84a9da2009-04-20 20:36:09 +00003266public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003267 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003268 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003269
Sebastian Redl539c5062010-08-18 23:57:32 +00003270 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003271 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003272
Justin Bogner25463f12014-04-18 20:27:24 +00003273 typedef unsigned hash_value_type;
3274 typedef unsigned offset_type;
3275
Richard Smithd7329392015-04-21 21:46:32 +00003276 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003277 IdentifierResolver &IdResolver, bool IsModule,
3278 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003279 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003280 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3281 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003282
Richard Smithd79514e2016-02-05 19:03:40 +00003283 bool needDecls() const { return NeedDecls; }
3284
Justin Bogner25463f12014-04-18 20:27:24 +00003285 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003286 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003287 }
Mike Stump11289f42009-09-09 15:08:12 +00003288
Richard Smith33e0f7e2015-07-22 02:08:40 +00003289 bool isInterestingIdentifier(const IdentifierInfo *II) {
3290 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3291 return isInterestingIdentifier(II, MacroOffset);
3292 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003293
Richard Smith9c254182015-07-19 21:41:12 +00003294 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3295 return isInterestingIdentifier(II, 0);
3296 }
3297
Mike Stump11289f42009-09-09 15:08:12 +00003298 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003299 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003300 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003301 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003302 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3303 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003304 DataLen += 2; // 2 bytes for builtin ID
3305 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003306 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003307 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003308
Richard Smitha534a312015-07-21 23:54:07 +00003309 if (NeedDecls) {
3310 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3311 DEnd = IdResolver.end();
3312 D != DEnd; ++D)
3313 DataLen += 4;
3314 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003315 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003316 using namespace llvm::support;
3317 endian::Writer<little> LE(Out);
3318
Richard Smithdf8a8312015-03-13 04:05:01 +00003319 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003320 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003321 // We emit the key length after the data length so that every
3322 // string is preceded by a 16-bit length. This matches the PTH
3323 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003324 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003325 return std::make_pair(KeyLen, DataLen);
3326 }
Mike Stump11289f42009-09-09 15:08:12 +00003327
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003328 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003329 unsigned KeyLen) {
3330 // Record the location of the key data. This is used when generating
3331 // the mapping from persistent IDs to strings.
3332 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003333
3334 // Emit the offset of the key/data length information to the interesting
3335 // identifiers table if necessary.
3336 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3337 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3338
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003339 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003340 }
Mike Stump11289f42009-09-09 15:08:12 +00003341
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003342 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003343 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003344 using namespace llvm::support;
3345 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003346
Richard Smithd7329392015-04-21 21:46:32 +00003347 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3348 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003349 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003350 return;
3351 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003352
Justin Bognere1c147c2014-03-28 22:03:19 +00003353 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003354 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3355 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003356 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003357 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003358 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003359 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003360 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3361 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003362 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003363 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003364 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003365 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003366
Richard Smithd7329392015-04-21 21:46:32 +00003367 if (HadMacroDefinition)
3368 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003369
Richard Smitha534a312015-07-21 23:54:07 +00003370 if (NeedDecls) {
3371 // Emit the declaration IDs in reverse order, because the
3372 // IdentifierResolver provides the declarations as they would be
3373 // visible (e.g., the function "stat" would come before the struct
3374 // "stat"), but the ASTReader adds declarations to the end of the list
3375 // (so we need to see the struct "stat" before the function "stat").
3376 // Only emit declarations that aren't from a chained PCH, though.
3377 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3378 IdResolver.end());
3379 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3380 DEnd = Decls.rend();
3381 D != DEnd; ++D)
3382 LE.write<uint32_t>(
3383 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3384 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003385 }
3386};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003387
Douglas Gregore84a9da2009-04-20 20:36:09 +00003388} // end anonymous namespace
3389
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003390/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003391///
3392/// The identifier table consists of a blob containing string data
3393/// (the actual identifiers themselves) and a separate "offsets" index
3394/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003395void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3396 IdentifierResolver &IdResolver,
3397 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003398 using namespace llvm;
3399
Richard Smith33e0f7e2015-07-22 02:08:40 +00003400 RecordData InterestingIdents;
3401
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003402 // Create and write out the blob that contains the identifier
3403 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003404 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003405 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003406 ASTIdentifierTableTrait Trait(
3407 *this, PP, IdResolver, IsModule,
3408 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003409
Douglas Gregore6648fb2009-04-28 20:33:11 +00003410 // Look for any identifiers that were named while processing the
3411 // headers, but are otherwise not needed. We add these to the hash
3412 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003413 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003414 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003415 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003416 for (const auto &ID : PP.getIdentifierTable())
3417 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003418 // Sort the identifiers lexicographically before getting them references so
3419 // that their order is stable.
3420 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3421 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003422 if (Trait.isInterestingNonMacroIdentifier(II))
3423 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003424
Sebastian Redlff4a2952010-07-23 23:49:55 +00003425 // Create the on-disk hash table representation. We only store offsets
3426 // for identifiers that appear here for the first time.
3427 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003428 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003429 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003430 IdentID ID = IdentIDPair.second;
3431 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003432 // Write out identifiers if either the ID is local or the identifier has
3433 // changed since it was loaded.
3434 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3435 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003436 (Trait.needDecls() &&
3437 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003438 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003439 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003440
Douglas Gregore84a9da2009-04-20 20:36:09 +00003441 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003442 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003443 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003444 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003445 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003446 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003447 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003448 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003449 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003450 }
3451
3452 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003453 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003454 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003457 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003458
3459 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003460 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003461 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003462 }
3463
3464 // Write the offsets table for identifier IDs.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003465 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003466 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003467 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003468 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3470 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3471
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003472#ifndef NDEBUG
3473 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3474 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3475#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003476
3477 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3478 IdentifierOffsets.size(),
3479 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003480 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003481 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003482
3483 // In C++, write the list of interesting identifiers (those that are
3484 // defined as macros, poisoned, or similar unusual things).
3485 if (!InterestingIdents.empty())
3486 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003487}
3488
Douglas Gregorc5046832009-04-27 18:38:38 +00003489//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003490// DeclContext's Name Lookup Table Serialization
3491//===----------------------------------------------------------------------===//
3492
3493namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003494
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003495// Trait used for the on-disk hash table used in the method pool.
3496class ASTDeclContextNameLookupTrait {
3497 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003498 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003499
3500public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003501 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003502 typedef key_type key_type_ref;
3503
Richard Smithd88a7f12015-09-01 20:35:42 +00003504 /// A start and end index into DeclIDs, representing a sequence of decls.
3505 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003506 typedef const data_type& data_type_ref;
3507
Justin Bogner25463f12014-04-18 20:27:24 +00003508 typedef unsigned hash_value_type;
3509 typedef unsigned offset_type;
3510
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003511 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3512
Richard Smithd88a7f12015-09-01 20:35:42 +00003513 template<typename Coll>
3514 data_type getData(const Coll &Decls) {
3515 unsigned Start = DeclIDs.size();
3516 for (NamedDecl *D : Decls) {
3517 DeclIDs.push_back(
3518 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3519 }
3520 return std::make_pair(Start, DeclIDs.size());
3521 }
3522
3523 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3524 unsigned Start = DeclIDs.size();
3525 for (auto ID : FromReader)
3526 DeclIDs.push_back(ID);
3527 return std::make_pair(Start, DeclIDs.size());
3528 }
3529
3530 static bool EqualKey(key_type_ref a, key_type_ref b) {
3531 return a == b;
3532 }
3533
Richard Smitha06c7e62015-08-26 23:55:49 +00003534 hash_value_type ComputeHash(DeclarationNameKey Name) {
3535 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003536 }
3537
Richard Smithd88a7f12015-09-01 20:35:42 +00003538 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3539 assert(Writer.hasChain() &&
3540 "have reference to loaded module file but no chain?");
3541
3542 using namespace llvm::support;
3543 endian::Writer<little>(Out)
3544 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3545 }
3546
Richard Smitha06c7e62015-08-26 23:55:49 +00003547 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3548 DeclarationNameKey Name,
3549 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003550 using namespace llvm::support;
3551 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003552 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003553 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003554 case DeclarationName::Identifier:
3555 case DeclarationName::ObjCZeroArgSelector:
3556 case DeclarationName::ObjCOneArgSelector:
3557 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003558 case DeclarationName::CXXLiteralOperatorName:
3559 KeyLen += 4;
3560 break;
3561 case DeclarationName::CXXOperatorName:
3562 KeyLen += 1;
3563 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003564 case DeclarationName::CXXConstructorName:
3565 case DeclarationName::CXXDestructorName:
3566 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003567 case DeclarationName::CXXUsingDirective:
3568 break;
3569 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003570 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003571
Richard Smithf02662d2015-07-30 03:17:16 +00003572 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003573 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3574 assert(uint16_t(DataLen) == DataLen &&
3575 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003576 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003577
3578 return std::make_pair(KeyLen, DataLen);
3579 }
3580
Richard Smitha06c7e62015-08-26 23:55:49 +00003581 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003582 using namespace llvm::support;
3583 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003584 LE.write<uint8_t>(Name.getKind());
3585 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003586 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003587 case DeclarationName::CXXLiteralOperatorName:
3588 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003589 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003590 case DeclarationName::ObjCZeroArgSelector:
3591 case DeclarationName::ObjCOneArgSelector:
3592 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003593 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003594 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003595 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003596 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003597 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003598 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003599 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003600 case DeclarationName::CXXConstructorName:
3601 case DeclarationName::CXXDestructorName:
3602 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003603 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003604 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003605 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003606
3607 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003608 }
3609
Richard Smitha06c7e62015-08-26 23:55:49 +00003610 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3611 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003612 using namespace llvm::support;
3613 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003614 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003615 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3616 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003617 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3618 }
3619};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003620
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003621} // end anonymous namespace
3622
Chandler Carruthe972c362015-03-26 03:11:40 +00003623bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3624 DeclContext *DC) {
3625 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3626}
3627
3628bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3629 DeclContext *DC) {
3630 for (auto *D : Result.getLookupResult())
3631 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3632 return false;
3633
3634 return true;
3635}
3636
Richard Smithd88a7f12015-09-01 20:35:42 +00003637void
Chandler Carruthe972c362015-03-26 03:11:40 +00003638ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003639 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003640 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3641 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003642 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003643
Chandler Carruthe972c362015-03-26 03:11:40 +00003644 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003645 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003646 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3647
3648 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003649 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3650 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003651 ASTDeclContextNameLookupTrait Trait(*this);
3652
Chandler Carruthe972c362015-03-26 03:11:40 +00003653 // The first step is to collect the declaration names which we need to
3654 // serialize into the name lookup table, and to collect them in a stable
3655 // order.
3656 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003657
Chandler Carruthe972c362015-03-26 03:11:40 +00003658 // We also build up small sets of the constructor and conversion function
3659 // names which are visible.
3660 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003661
Chandler Carruthe972c362015-03-26 03:11:40 +00003662 for (auto &Lookup : *DC->buildLookup()) {
3663 auto &Name = Lookup.first;
3664 auto &Result = Lookup.second;
3665
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003666 // If there are no local declarations in our lookup result, we
3667 // don't need to write an entry for the name at all. If we can't
3668 // write out a lookup set without performing more deserialization,
3669 // just skip this entry.
3670 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003671 isLookupResultEntirelyExternal(Result, DC))
3672 continue;
3673
3674 // We also skip empty results. If any of the results could be external and
3675 // the currently available results are empty, then all of the results are
3676 // external and we skip it above. So the only way we get here with an empty
3677 // results is when no results could have been external *and* we have
3678 // external results.
3679 //
3680 // FIXME: While we might want to start emitting on-disk entries for negative
3681 // lookups into a decl context as an optimization, today we *have* to skip
3682 // them because there are names with empty lookup results in decl contexts
3683 // which we can't emit in any stable ordering: we lookup constructors and
3684 // conversion functions in the enclosing namespace scope creating empty
3685 // results for them. This in almost certainly a bug in Clang's name lookup,
3686 // but that is likely to be hard or impossible to fix and so we tolerate it
3687 // here by omitting lookups with empty results.
3688 if (Lookup.second.getLookupResult().empty())
3689 continue;
3690
3691 switch (Lookup.first.getNameKind()) {
3692 default:
3693 Names.push_back(Lookup.first);
3694 break;
3695
Richard Smithcd45dbc2014-04-19 03:48:30 +00003696 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003697 assert(isa<CXXRecordDecl>(DC) &&
3698 "Cannot have a constructor name outside of a class!");
3699 ConstructorNameSet.insert(Name);
3700 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003701
3702 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003703 assert(isa<CXXRecordDecl>(DC) &&
3704 "Cannot have a conversion function name outside of a class!");
3705 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003706 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003707 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003708 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003709
Chandler Carruthe972c362015-03-26 03:11:40 +00003710 // Sort the names into a stable order.
3711 std::sort(Names.begin(), Names.end());
3712
Chandler Carruthffbf7052015-03-26 22:27:09 +00003713 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003714 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003715 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003716
Chandler Carruthffbf7052015-03-26 22:27:09 +00003717 // First we try the easy case by forming the current context's constructor
3718 // name and adding that name first. This is a very useful optimization to
3719 // avoid walking the lexical declarations in many cases, and it also
3720 // handles the only case where a constructor name can come from some other
3721 // lexical context -- when that name is an implicit constructor merged from
3722 // another declaration in the redecl chain. Any non-implicit constructor or
3723 // conversion function which doesn't occur in all the lexical contexts
3724 // would be an ODR violation.
3725 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3726 Context->getCanonicalType(Context->getRecordType(D)));
3727 if (ConstructorNameSet.erase(ImplicitCtorName))
3728 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003729
Chandler Carruthffbf7052015-03-26 22:27:09 +00003730 // If we still have constructors or conversion functions, we walk all the
3731 // names in the decl and add the constructors and conversion functions
3732 // which are visible in the order they lexically occur within the context.
3733 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3734 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3735 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3736 auto Name = ChildND->getDeclName();
3737 switch (Name.getNameKind()) {
3738 default:
3739 continue;
3740
3741 case DeclarationName::CXXConstructorName:
3742 if (ConstructorNameSet.erase(Name))
3743 Names.push_back(Name);
3744 break;
3745
3746 case DeclarationName::CXXConversionFunctionName:
3747 if (ConversionNameSet.erase(Name))
3748 Names.push_back(Name);
3749 break;
3750 }
3751
3752 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3753 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003754 }
3755
Chandler Carruthe972c362015-03-26 03:11:40 +00003756 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3757 "constructors by walking all the "
3758 "lexical members of the context.");
3759 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3760 "conversion functions by walking all "
3761 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003762 }
3763
Chandler Carruthe972c362015-03-26 03:11:40 +00003764 // Next we need to do a lookup with each name into this decl context to fully
3765 // populate any results from external sources. We don't actually use the
3766 // results of these lookups because we only want to use the results after all
3767 // results have been loaded and the pointers into them will be stable.
3768 for (auto &Name : Names)
3769 DC->lookup(Name);
3770
3771 // Now we need to insert the results for each name into the hash table. For
3772 // constructor names and conversion function names, we actually need to merge
3773 // all of the results for them into one list of results each and insert
3774 // those.
3775 SmallVector<NamedDecl *, 8> ConstructorDecls;
3776 SmallVector<NamedDecl *, 8> ConversionDecls;
3777
3778 // Now loop over the names, either inserting them or appending for the two
3779 // special cases.
3780 for (auto &Name : Names) {
3781 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3782
3783 switch (Name.getNameKind()) {
3784 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003785 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003786 break;
3787
3788 case DeclarationName::CXXConstructorName:
3789 ConstructorDecls.append(Result.begin(), Result.end());
3790 break;
3791
3792 case DeclarationName::CXXConversionFunctionName:
3793 ConversionDecls.append(Result.begin(), Result.end());
3794 break;
3795 }
3796 }
3797
3798 // Handle our two special cases if we ended up having any. We arbitrarily use
3799 // the first declaration's name here because the name itself isn't part of
3800 // the key, only the kind of name is used.
3801 if (!ConstructorDecls.empty())
3802 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003803 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003804 if (!ConversionDecls.empty())
3805 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003806 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003807
Richard Smithd88a7f12015-09-01 20:35:42 +00003808 // Create the on-disk hash table. Also emit the existing imported and
3809 // merged table if there is one.
3810 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3811 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003812}
3813
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003814/// \brief Write the block containing all of the declaration IDs
3815/// visible from the given DeclContext.
3816///
3817/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003818/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003819uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3820 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003821 // If we imported a key declaration of this namespace, write the visible
3822 // lookup results as an update record for it rather than including them
3823 // on this declaration. We will only look at key declarations on reload.
3824 if (isa<NamespaceDecl>(DC) && Chain &&
3825 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3826 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003827 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003828 Prev = Prev->getPreviousDecl())
3829 if (!Prev->isFromASTFile())
3830 return 0;
3831
3832 // Note that we need to emit an update record for the primary context.
3833 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3834
3835 // Make sure all visible decls are written. They will be recorded later. We
3836 // do this using a side data structure so we can sort the names into
3837 // a deterministic order.
3838 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3839 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3840 LookupResults;
3841 if (Map) {
3842 LookupResults.reserve(Map->size());
3843 for (auto &Entry : *Map)
3844 LookupResults.push_back(
3845 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3846 }
3847
3848 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3849 for (auto &NameAndResult : LookupResults) {
3850 DeclarationName Name = NameAndResult.first;
3851 DeclContext::lookup_result Result = NameAndResult.second;
3852 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3853 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3854 // We have to work around a name lookup bug here where negative lookup
3855 // results for these names get cached in namespace lookup tables (these
3856 // names should never be looked up in a namespace).
3857 assert(Result.empty() && "Cannot have a constructor or conversion "
3858 "function name in a namespace!");
3859 continue;
3860 }
3861
3862 for (NamedDecl *ND : Result)
3863 if (!ND->isFromASTFile())
3864 GetDeclRef(ND);
3865 }
3866
3867 return 0;
3868 }
3869
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003870 if (DC->getPrimaryContext() != DC)
3871 return 0;
3872
Chandler Carruthe972c362015-03-26 03:11:40 +00003873 // Skip contexts which don't support name lookup.
3874 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003875 return 0;
3876
3877 // If not in C++, we perform name lookup for the translation unit via the
3878 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003879 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003880 return 0;
3881
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003882 // Serialize the contents of the mapping used for lookup. Note that,
3883 // although we have two very different code paths, the serialized
3884 // representation is the same for both cases: a declaration name,
3885 // followed by a size, followed by references to the visible
3886 // declarations that have that name.
3887 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003888 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003889 if (!Map || Map->empty())
3890 return 0;
3891
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003892 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003893 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003894 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003895
3896 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003897 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003898 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003899 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003900 ++NumVisibleDeclContexts;
3901 return Offset;
3902}
3903
Sebastian Redla4071b42010-08-24 00:50:09 +00003904/// \brief Write an UPDATE_VISIBLE block for the given context.
3905///
3906/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3907/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003908/// (in C++), for namespaces, and for classes with forward-declared unscoped
3909/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003910void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003911 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003912 if (!Map || Map->empty())
3913 return;
3914
Sebastian Redla4071b42010-08-24 00:50:09 +00003915 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003916 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003917 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003918
Richard Smith5fc18a92015-07-12 23:43:21 +00003919 // If we're updating a namespace, select a key declaration as the key for the
3920 // update record; those are the only ones that will be checked on reload.
3921 if (isa<NamespaceDecl>(DC))
3922 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3923
Sebastian Redla4071b42010-08-24 00:50:09 +00003924 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003925 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003926 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003927}
3928
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003929/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3930void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003931 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003932 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3933}
3934
3935/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3936void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003937 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003938 return;
3939
3940 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3941 RecordData Record;
3942#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3943#include "clang/Basic/OpenCLExtensions.def"
3944 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3945}
3946
Justin Lebar67a78a62016-10-08 22:15:58 +00003947void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
3948 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
3949 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
3950 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
3951 }
3952}
3953
Douglas Gregor404cdde2012-01-27 01:47:08 +00003954void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003955 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003956 RecordData Categories;
3957
3958 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3959 unsigned Size = 0;
3960 unsigned StartIndex = Categories.size();
3961
3962 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3963
3964 // Allocate space for the size.
3965 Categories.push_back(0);
3966
3967 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003968 for (ObjCInterfaceDecl::known_categories_iterator
3969 Cat = Class->known_categories_begin(),
3970 CatEnd = Class->known_categories_end();
3971 Cat != CatEnd; ++Cat, ++Size) {
3972 assert(getDeclID(*Cat) != 0 && "Bogus category");
3973 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003974 }
3975
3976 // Update the size.
3977 Categories[StartIndex] = Size;
3978
3979 // Record this interface -> category map.
3980 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3981 CategoriesMap.push_back(CatInfo);
3982 }
3983
3984 // Sort the categories map by the definition ID, since the reader will be
3985 // performing binary searches on this information.
3986 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3987
3988 // Emit the categories map.
3989 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003990
3991 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003992 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3995 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00003996
3997 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
3998 Stream.EmitRecordWithBlob(AbbrevID, Record,
3999 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004000 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004001
Douglas Gregor404cdde2012-01-27 01:47:08 +00004002 // Emit the category lists.
4003 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4004}
4005
Richard Smithe40f2ba2013-08-07 21:41:30 +00004006void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4007 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4008
4009 if (LPTMap.empty())
4010 return;
4011
4012 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004013 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004014 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004015 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004016 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004017 AddDeclRef(LPT.D, Record);
4018 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004019
Justin Lebar28f09c52016-10-10 16:26:08 +00004020 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004021 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004022 }
4023 }
4024 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4025}
4026
Dario Domizioli13a0a382014-05-23 12:13:25 +00004027/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4028void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4029 RecordData Record;
4030 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4031 AddSourceLocation(PragmaLoc, Record);
4032 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4033}
4034
Nico Weber779355f2016-03-02 23:22:00 +00004035/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4036void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4037 RecordData Record;
4038 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4039 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4040}
4041
Nico Weber42932312016-03-03 00:17:35 +00004042/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4043//module.
4044void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4045 RecordData Record;
4046 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4047 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4048 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4049}
4050
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004051void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4052 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004053 // Enter the extension block.
4054 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4055
4056 // Emit the metadata record abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004057 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004058 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4059 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4060 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4061 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4062 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4063 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4064 unsigned Abbrev = Stream.EmitAbbrev(Abv);
4065
4066 // Emit the metadata record.
4067 RecordData Record;
4068 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4069 Record.push_back(EXTENSION_METADATA);
4070 Record.push_back(Metadata.MajorVersion);
4071 Record.push_back(Metadata.MinorVersion);
4072 Record.push_back(Metadata.BlockName.size());
4073 Record.push_back(Metadata.UserInfo.size());
4074 SmallString<64> Buffer;
4075 Buffer += Metadata.BlockName;
4076 Buffer += Metadata.UserInfo;
4077 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4078
4079 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004080 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004081
4082 // Exit the extension block.
4083 Stream.ExitBlock();
4084}
4085
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004086//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004087// General Serialization Routines
4088//===----------------------------------------------------------------------===//
4089
Richard Smith69c82bf2016-04-01 22:52:03 +00004090/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004091void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4092 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004093 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004094 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004095 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004096 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004097
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004098#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004099
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004100 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004101}
4102
John McCallf413f5e2013-05-03 00:10:13 +00004103void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4104 AddSourceLocation(Tok.getLocation(), Record);
4105 Record.push_back(Tok.getLength());
4106
4107 // FIXME: When reading literal tokens, reconstruct the literal pointer
4108 // if it is needed.
4109 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4110 // FIXME: Should translate token kind to a stable encoding.
4111 Record.push_back(Tok.getKind());
4112 // FIXME: Should translate token flags to a stable encoding.
4113 Record.push_back(Tok.getFlags());
4114}
4115
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004116void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004117 Record.push_back(Str.size());
4118 Record.insert(Record.end(), Str.begin(), Str.end());
4119}
4120
Richard Smith7ed1bc92014-12-05 22:42:13 +00004121bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004122 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004123
Richard Smith54cc3c22014-12-11 20:50:24 +00004124 bool Changed =
4125 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004126
4127 // Remove a prefix to make the path relative, if relevant.
4128 const char *PathBegin = Path.data();
4129 const char *PathPtr =
4130 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4131 if (PathPtr != PathBegin) {
4132 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4133 Changed = true;
4134 }
4135
4136 return Changed;
4137}
4138
4139void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4140 SmallString<128> FilePath(Path);
4141 PreparePathForOutput(FilePath);
4142 AddString(FilePath, Record);
4143}
4144
Mehdi Amini57a41912015-09-10 01:46:39 +00004145void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004146 StringRef Path) {
4147 SmallString<128> FilePath(Path);
4148 PreparePathForOutput(FilePath);
4149 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4150}
4151
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004152void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4153 RecordDataImpl &Record) {
4154 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004155 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004156 Record.push_back(*Minor + 1);
4157 else
4158 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004159 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004160 Record.push_back(*Subminor + 1);
4161 else
4162 Record.push_back(0);
4163}
4164
Douglas Gregore84a9da2009-04-20 20:36:09 +00004165/// \brief Note that the identifier II occurs at the given offset
4166/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004167void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004168 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004169 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004170 // up earlier in the chain and thus don't need an offset.
4171 if (ID >= FirstIdentID)
4172 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004173}
4174
Douglas Gregor95c13f52009-04-25 17:48:32 +00004175/// \brief Note that the selector Sel occurs at the given offset
4176/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004177void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004178 unsigned ID = SelectorIDs[Sel];
4179 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004180 // Don't record offsets for selectors that are also available in a different
4181 // file.
4182 if (ID < FirstSelectorID)
4183 return;
4184 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004185}
4186
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004187ASTWriter::ASTWriter(
4188 llvm::BitstreamWriter &Stream,
4189 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
4190 bool IncludeTimestamps)
Richard Smith01b2cb42014-07-26 06:37:51 +00004191 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004192 WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
4193 WritingAST(false), DoneWritingDeclsAndTypes(false),
4194 ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
4195 NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
4196 NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
4197 NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
4198 NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
Richard Smith01b2cb42014-07-26 06:37:51 +00004199 NextSubmoduleID(FirstSubmoduleID),
4200 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Richard Smith290d8012016-04-06 17:06:00 +00004201 NumStatements(0), NumMacros(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004202 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004203 TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004204 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004205 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004206 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004207 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4208 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004209 ExprImplicitCastAbbrev(0) {
4210 for (const auto &Ext : Extensions) {
4211 if (auto Writer = Ext->createExtensionWriter(*this))
4212 ModuleFileExtensionWriters.push_back(std::move(Writer));
4213 }
4214}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004215
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004216ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004217 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004218}
4219
Richard Smithb3761912015-02-05 23:08:52 +00004220const LangOptions &ASTWriter::getLangOpts() const {
4221 assert(WritingAST && "can't determine lang opts when not writing AST");
4222 return Context->getLangOpts();
4223}
4224
Richard Smithe75ee0f2015-08-17 07:13:32 +00004225time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4226 return IncludeTimestamps ? E->getModificationTime() : 0;
4227}
4228
Adrian Prantladbd2b12015-09-22 23:26:31 +00004229uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4230 Module *WritingModule, StringRef isysroot,
4231 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004232 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004233
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004234 ASTHasCompilerErrors = hasErrors;
4235
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004236 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004237 Stream.Emit((unsigned)'C', 8);
4238 Stream.Emit((unsigned)'P', 8);
4239 Stream.Emit((unsigned)'C', 8);
4240 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004241
Chris Lattner28fa4e62009-04-26 22:26:21 +00004242 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004243
Douglas Gregoreda8e122011-08-09 15:13:55 +00004244 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004245 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004246 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004247 ASTFileSignature Signature =
4248 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004249 Context = nullptr;
4250 PP = nullptr;
4251 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004252 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004253
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004254 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004255 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004256}
4257
Douglas Gregora94a1542011-07-27 21:45:57 +00004258template<typename Vector>
4259static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4260 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004261 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4262 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004263 Writer.AddDeclRef(*I, Record);
4264 }
4265}
4266
Adrian Prantladbd2b12015-09-22 23:26:31 +00004267uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4268 const std::string &OutputFile,
4269 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004270 using namespace llvm;
4271
Craig Toppera13603a2014-05-22 05:54:18 +00004272 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004273
Douglas Gregorcf68c582011-12-01 22:20:10 +00004274 // Make sure that the AST reader knows to finalize itself.
4275 if (Chain)
4276 Chain->finalizeForWriting();
4277
Sebastian Redl143413f2010-07-12 22:02:52 +00004278 ASTContext &Context = SemaRef.Context;
4279 Preprocessor &PP = SemaRef.PP;
4280
Douglas Gregordab42432011-08-12 00:15:20 +00004281 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004282 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4283 if (D) {
4284 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4285 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004286 }
4287 };
4288 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4289 PREDEF_DECL_TRANSLATION_UNIT_ID);
4290 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4291 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4292 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4293 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4294 PREDEF_DECL_OBJC_PROTOCOL_ID);
4295 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4296 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4297 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4298 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4299 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004300 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004301 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4302 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004303 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004304 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4305 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004306 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4307 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004308 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4309 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004310 RegisterPredefDecl(Context.TypePackElementDecl,
4311 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004312
Chris Lattner0c797362009-09-08 18:19:27 +00004313 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004314 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004315 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004316 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004317 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004318
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004319 // Build a record containing all of the file scoped decls in this file.
4320 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004321 if (!isModule)
4322 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4323 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004324
Douglas Gregor851443c2011-08-12 01:39:19 +00004325 // Build a record containing all of the delegating constructors we still need
4326 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004327 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004328 if (!isModule)
4329 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004330
Douglas Gregor851443c2011-08-12 01:39:19 +00004331 // Write the set of weak, undeclared identifiers. We always write the
4332 // entire table, since later PCH files in a PCH chain are only interested in
4333 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004334 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004335 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4336 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4337 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4338 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4339 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4340 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4341 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004342 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004343
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004344 // Build a record containing all of the ext_vector declarations.
4345 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004346 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004347
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004348 // Build a record containing all of the VTable uses information.
4349 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004350 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004351 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4352 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4353 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4354 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4355 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004356 }
4357
Nico Weber72889432014-09-06 01:25:55 +00004358 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4359 RecordData UnusedLocalTypedefNameCandidates;
4360 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4361 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4362
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004363 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004364 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004365 for (const auto &I : SemaRef.PendingInstantiations) {
4366 AddDeclRef(I.first, PendingInstantiations);
4367 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004368 }
4369 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4370 "There are local ones at end of translation unit!");
4371
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004372 // Build a record containing some declaration references.
4373 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004374 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004375 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4376 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004377 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004378 }
4379
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004380 RecordData CUDASpecialDeclRefs;
4381 if (Context.getcudaConfigureCallDecl()) {
4382 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4383 }
4384
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004385 // Build a record containing all of the known namespaces.
4386 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004387 for (const auto &I : SemaRef.KnownNamespaces) {
4388 if (!I.second)
4389 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004390 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004391
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004392 // Build a record of all used, undefined objects that require definitions.
4393 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004394
4395 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004396 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004397 for (const auto &I : Undefined) {
4398 AddDeclRef(I.first, UndefinedButUsed);
4399 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004400 }
4401
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004402 // Build a record containing all delete-expressions that we would like to
4403 // analyze later in AST.
4404 RecordData DeleteExprsToAnalyze;
4405
4406 for (const auto &DeleteExprsInfo :
4407 SemaRef.getMismatchingDeleteExpressions()) {
4408 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4409 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4410 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4411 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4412 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4413 }
4414 }
4415
Douglas Gregor112b9072012-10-18 05:31:06 +00004416 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004417 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004418
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004419 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004420 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004421
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004422 // This is so that older clang versions, before the introduction
4423 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004424 {
4425 RecordData Record = {VERSION_MAJOR};
4426 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4427 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004428
Douglas Gregor851443c2011-08-12 01:39:19 +00004429 // Create a lexical update block containing all of the declarations in the
4430 // translation unit that do not come from other AST files.
4431 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004432 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4433 for (const auto *D : TU->noload_decls()) {
4434 if (!D->isFromASTFile()) {
4435 NewGlobalKindDeclPairs.push_back(D->getKind());
4436 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4437 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004438 }
4439
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004440 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor851443c2011-08-12 01:39:19 +00004441 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4442 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4443 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
Mehdi Amini57a41912015-09-10 01:46:39 +00004444 {
4445 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4446 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4447 bytes(NewGlobalKindDeclPairs));
4448 }
4449
Douglas Gregor851443c2011-08-12 01:39:19 +00004450 // And a visible updates block for the translation unit.
4451 Abv = new llvm::BitCodeAbbrev();
4452 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4453 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004454 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4455 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4456 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004457
4458 // If we have any extern "C" names, write out a visible update for them.
4459 if (Context.ExternCContext)
4460 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004461
4462 // If the translation unit has an anonymous namespace, and we don't already
4463 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004464 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004465 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4466 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004467 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004468 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004469 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004470
Richard Smith5652c0f2014-03-21 01:48:23 +00004471 // Add update records for all mangling numbers and static local numbers.
4472 // These aren't really update records, but this is a convenient way of
4473 // tagging this rare extra data onto the declarations.
4474 for (const auto &Number : Context.MangleNumbers)
4475 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004476 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4477 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004478 for (const auto &Number : Context.StaticLocalNumbers)
4479 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004480 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4481 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004482
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004483 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004484 // an AST file, are registered for serialization. Likewise for template
4485 // specializations added to imported templates.
4486 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004487 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004488 }
4489
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004490 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004491 // serialization, if we're storing decls with identifiers.
4492 if (!WritingModule || !getLangOpts().CPlusPlus) {
4493 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004494 for (const auto &ID : PP.getIdentifierTable()) {
4495 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004496 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4497 IIs.push_back(II);
4498 }
4499 // Sort the identifiers to visit based on their name.
4500 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4501 for (const IdentifierInfo *II : IIs) {
4502 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4503 DEnd = SemaRef.IdResolver.end();
4504 D != DEnd; ++D) {
4505 GetDeclRef(*D);
4506 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004507 }
4508 }
4509
Manman Rena0f31a02016-04-29 19:04:05 +00004510 // For method pool in the module, if it contains an entry for a selector,
4511 // the entry should be complete, containing everything introduced by that
4512 // module and all modules it imports. It's possible that the entry is out of
4513 // date, so we need to pull in the new content here.
4514
4515 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4516 // safe, we copy all selectors out.
4517 llvm::SmallVector<Selector, 256> AllSelectors;
4518 for (auto &SelectorAndID : SelectorIDs)
4519 AllSelectors.push_back(SelectorAndID.first);
4520 for (auto &Selector : AllSelectors)
4521 SemaRef.updateOutOfDateSelector(Selector);
4522
Douglas Gregor5204bde2011-08-02 16:26:37 +00004523 // Form the record of special types.
4524 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004525 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004526 AddTypeRef(Context.getFILEType(), SpecialTypes);
4527 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4528 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4529 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4530 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004531 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004532 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004533
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004534 if (Chain) {
4535 // Write the mapping information describing our module dependencies and how
4536 // each of those modules were mapped into our own offset/ID space, so that
4537 // the reader can build the appropriate mapping to its own offset/ID space.
4538 // The map consists solely of a blob with the following format:
4539 // *(module-name-len:i16 module-name:len*i8
4540 // source-location-offset:i32
4541 // identifier-id:i32
4542 // preprocessed-entity-id:i32
4543 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004544 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004545 // selector-id:i32
4546 // declaration-id:i32
4547 // c++-base-specifiers-id:i32
4548 // type-id:i32)
4549 //
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004550 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004551 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4553 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004554 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004555 {
4556 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004557 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004558 using namespace llvm::support;
4559 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004560 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004561 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004562 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004563
Ben Langmuir785180e2014-10-20 16:27:30 +00004564 // Note: if a base ID was uint max, it would not be possible to load
4565 // another module after it or have more than one entity inside it.
4566 uint32_t None = std::numeric_limits<uint32_t>::max();
4567
4568 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4569 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4570 if (ShouldWrite)
4571 LE.write<uint32_t>(BaseID);
4572 else
4573 LE.write<uint32_t>(None);
4574 };
4575
Ben Langmuirfe971d92014-08-16 04:54:18 +00004576 // These values should be unique within a chain, since they will be read
4577 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004578 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4579 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4580 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4581 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4582 M->NumPreprocessedEntities);
4583 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4584 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4585 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4586 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004587 }
4588 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004589 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004590 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4591 Buffer.data(), Buffer.size());
4592 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004593
Richard Smithb9eab6d2014-03-20 19:44:17 +00004594 RecordData DeclUpdatesOffsetsRecord;
4595
Richard Smith59442b42014-03-20 20:07:19 +00004596 // Keep writing types, declarations, and declaration update records
4597 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004598 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4599 WriteTypeAbbrevs();
4600 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004601 do {
4602 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4603 while (!DeclTypesToEmit.empty()) {
4604 DeclOrType DOT = DeclTypesToEmit.front();
4605 DeclTypesToEmit.pop();
4606 if (DOT.isType())
4607 WriteType(DOT.getType());
4608 else
4609 WriteDecl(Context, DOT.getDecl());
4610 }
4611 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004612 Stream.ExitBlock();
4613
Richard Smithb9eab6d2014-03-20 19:44:17 +00004614 DoneWritingDeclsAndTypes = true;
4615
4616 // These things can only be done once we've written out decls and types.
4617 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004618 if (!DeclUpdatesOffsetsRecord.empty())
4619 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004620 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004621 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004622 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004623 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004624 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004625 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004626 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004627 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004628 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004629 WriteFPPragmaOptions(SemaRef.getFPOptions());
4630 WriteOpenCLExtensions(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004631 WriteCUDAPragmas(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004632 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004633
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004634 // If we're emitting a module, write out the submodule information.
4635 if (WritingModule)
4636 WriteSubmodules(WritingModule);
Richard Smithdc1f0422016-07-20 19:10:16 +00004637 else if (!getLangOpts().CurrentModule.empty()) {
4638 // If we're building a PCH in the implementation of a module, we may need
4639 // the description of the current module.
4640 //
4641 // FIXME: We may need other modules that we did not load from an AST file,
4642 // such as if a module declares a 'conflicts' on a different module.
4643 Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
4644 getLangOpts().CurrentModule);
4645 if (M && !M->IsFromModuleFile)
4646 WriteSubmodules(M);
4647 }
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004648
Douglas Gregor5204bde2011-08-02 16:26:37 +00004649 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4650
Douglas Gregord4df8652009-04-22 22:02:47 +00004651 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004652 if (!EagerlyDeserializedDecls.empty())
4653 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004654
4655 // Write the record containing tentative definitions.
4656 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004657 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004658
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004659 // Write the record containing unused file scoped decls.
4660 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004661 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004662
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004663 // Write the record containing weak undeclared identifiers.
4664 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004665 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004666 WeakUndeclaredIdentifiers);
4667
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004668 // Write the record containing ext_vector type names.
4669 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004670 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004671
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004672 // Write the record containing VTable uses information.
4673 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004674 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004675
Nico Weber72889432014-09-06 01:25:55 +00004676 // Write the record containing potentially unused local typedefs.
4677 if (!UnusedLocalTypedefNameCandidates.empty())
4678 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4679 UnusedLocalTypedefNameCandidates);
4680
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004681 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004682 if (!PendingInstantiations.empty())
4683 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004684
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004685 // Write the record containing declaration references of Sema.
4686 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004687 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004688
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004689 // Write the record containing CUDA-specific declaration references.
4690 if (!CUDASpecialDeclRefs.empty())
4691 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004692
4693 // Write the delegating constructors.
4694 if (!DelegatingCtorDecls.empty())
4695 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004696
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004697 // Write the known namespaces.
4698 if (!KnownNamespaces.empty())
4699 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004700
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004701 // Write the undefined internal functions and variables, and inline functions.
4702 if (!UndefinedButUsed.empty())
4703 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004704
4705 if (!DeleteExprsToAnalyze.empty())
4706 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4707
Douglas Gregor851443c2011-08-12 01:39:19 +00004708 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004709 for (auto *DC : UpdatedDeclContexts)
4710 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004711
Douglas Gregor959bb062011-12-03 01:15:29 +00004712 if (!WritingModule) {
4713 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004714 struct ModuleInfo {
4715 uint64_t ID;
4716 Module *M;
4717 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4718 };
Richard Smith56be7542014-03-21 00:33:59 +00004719 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004720 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004721 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004722 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4723 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004724 }
Richard Smith56be7542014-03-21 00:33:59 +00004725
4726 if (!Imports.empty()) {
4727 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4728 return A.ID < B.ID;
4729 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004730 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4731 return A.ID == B.ID;
4732 };
Richard Smith56be7542014-03-21 00:33:59 +00004733
4734 // Sort and deduplicate module IDs.
4735 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004736 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004737 Imports.end());
4738
4739 RecordData ImportedModules;
4740 for (const auto &Import : Imports) {
4741 ImportedModules.push_back(Import.ID);
4742 // FIXME: If the module has macros imported then later has declarations
4743 // imported, this location won't be the right one as a location for the
4744 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004745 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004746 }
4747
Douglas Gregor959bb062011-12-03 01:15:29 +00004748 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4749 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004750 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004751
Douglas Gregor404cdde2012-01-27 01:47:08 +00004752 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004753 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004754 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004755 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004756 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004757 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004758
Douglas Gregor08f01292009-04-17 22:13:46 +00004759 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004760 RecordData::value_type Record[] = {
4761 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004762 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004763 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004764
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004765 // Write the module file extension blocks.
4766 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004767 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004768
Adrian Prantladbd2b12015-09-22 23:26:31 +00004769 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004770}
4771
Richard Smithb9eab6d2014-03-20 19:44:17 +00004772void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004773 if (DeclUpdates.empty())
4774 return;
4775
Richard Smith59442b42014-03-20 20:07:19 +00004776 DeclUpdateMap LocalUpdates;
4777 LocalUpdates.swap(DeclUpdates);
4778
Richard Smith6ef42932014-03-20 21:02:00 +00004779 for (auto &DeclUpdate : LocalUpdates) {
4780 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004781
Richard Smithd28ac5b2014-03-22 23:33:22 +00004782 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004783 RecordData RecordData;
4784 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004785 for (auto &Update : DeclUpdate.second) {
4786 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4787
Richard Smith69c82bf2016-04-01 22:52:03 +00004788 // An updated body is emitted last, so that the reader doesn't need
4789 // to skip over the lazy body to reach statements for other records.
4790 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4791 HasUpdatedBody = true;
4792 else
4793 Record.push_back(Kind);
4794
Richard Smith6ef42932014-03-20 21:02:00 +00004795 switch (Kind) {
4796 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4797 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4798 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004799 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004800 Record.push_back(GetDeclRef(Update.getDecl()));
4801 break;
4802
Richard Smith4d235792014-08-07 18:53:08 +00004803 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004804 break;
4805
Richard Smith4d235792014-08-07 18:53:08 +00004806 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004807 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004808 break;
4809
John McCall32791cc2016-01-06 22:34:54 +00004810 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004811 Record.AddStmt(const_cast<Expr *>(
4812 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004813 break;
4814
Richard Smith4b054b22016-08-24 21:25:37 +00004815 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4816 Record.AddStmt(
4817 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4818 break;
4819
Richard Smithcd45dbc2014-04-19 03:48:30 +00004820 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4821 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004822 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004823 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004824 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004825 *Context, const_cast<CXXRecordDecl *>(RD)));
4826
4827 // This state is sometimes updated by template instantiation, when we
4828 // switch from the specialization referring to the template declaration
4829 // to it referring to the template definition.
4830 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4831 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004832 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004833 } else {
4834 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4835 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004836 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004837
4838 // The instantiation might have been resolved to a partial
4839 // specialization. If so, record which one.
4840 auto From = Spec->getInstantiatedFrom();
4841 if (auto PartialSpec =
4842 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4843 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004844 Record.AddDeclRef(PartialSpec);
4845 Record.AddTemplateArgumentList(
4846 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004847 } else {
4848 Record.push_back(false);
4849 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004850 }
4851 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004852 Record.AddSourceLocation(RD->getLocation());
4853 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004854 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004855
4856 // Instantiation may change attributes; write them all out afresh.
4857 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004858 if (D->hasAttrs())
4859 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004860
4861 // FIXME: Ensure we don't get here for explicit instantiations.
4862 break;
4863 }
4864
Richard Smithf8134002015-03-10 01:41:22 +00004865 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004866 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004867 break;
4868
Richard Smith564417a2014-03-20 21:47:22 +00004869 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4870 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004871 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4872 Record);
4873 break;
4874
Richard Smith6ef42932014-03-20 21:02:00 +00004875 case UPD_CXX_DEDUCED_RETURN_TYPE:
4876 Record.push_back(GetOrCreateTypeID(Update.getType()));
4877 break;
4878
4879 case UPD_DECL_MARKED_USED:
4880 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004881
4882 case UPD_MANGLING_NUMBER:
4883 case UPD_STATIC_LOCAL_NUMBER:
4884 Record.push_back(Update.getNumber());
4885 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004886
Alexey Bataev97720002014-11-11 04:05:39 +00004887 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004888 Record.AddSourceRange(
4889 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004890 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004891
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004892 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4893 Record.AddSourceRange(
4894 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4895 break;
4896
Richard Smith65ebb4a2015-03-26 04:09:53 +00004897 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004898 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004899 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004900
4901 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004902 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004903 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004904 }
4905 }
4906
Richard Smithd28ac5b2014-03-22 23:33:22 +00004907 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004908 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004909 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004910 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004911 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004912 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004913 }
4914
Richard Smithcd45dbc2014-04-19 03:48:30 +00004915 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004916 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004917 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004918}
4919
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004920void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004921 uint32_t Raw = Loc.getRawEncoding();
4922 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004923}
4924
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004925void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004926 AddSourceLocation(Range.getBegin(), Record);
4927 AddSourceLocation(Range.getEnd(), Record);
4928}
4929
Richard Smithf144b542016-04-08 21:54:32 +00004930void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4931 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004932 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004933 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004934}
4935
Richard Smithf144b542016-04-08 21:54:32 +00004936void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4937 Record->push_back(Value.isUnsigned());
4938 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004939}
4940
Richard Smithf144b542016-04-08 21:54:32 +00004941void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4942 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00004943}
4944
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004945void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004946 Record.push_back(getIdentifierRef(II));
4947}
4948
Sebastian Redl539c5062010-08-18 23:57:32 +00004949IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004950 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004951 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004952
Sebastian Redl539c5062010-08-18 23:57:32 +00004953 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004954 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004955 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004956 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004957}
4958
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004959MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004960 // Don't emit builtin macros like __LINE__ to the AST file unless they
4961 // have been redefined by the header (in which case they are not
4962 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004963 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004964 return 0;
4965
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004966 MacroID &ID = MacroIDs[MI];
4967 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004968 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004969 MacroInfoToEmitData Info = { Name, MI, ID };
4970 MacroInfosToEmit.push_back(Info);
4971 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004972 return ID;
4973}
4974
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004975MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004976 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004977 return 0;
4978
4979 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4980 return MacroIDs[MI];
4981}
4982
4983uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004984 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004985}
4986
Richard Smithe64e7452016-04-18 21:54:58 +00004987void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
4988 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00004989}
4990
Sebastian Redl539c5062010-08-18 23:57:32 +00004991SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004992 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004993 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004994 }
4995
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004996 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004997 if (SID == 0 && Chain) {
4998 // This might trigger a ReadSelector callback, which will set the ID for
4999 // this selector.
5000 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005001 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005002 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005003 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005004 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005005 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005006 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005007 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005008}
5009
Richard Smithe64e7452016-04-18 21:54:58 +00005010void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5011 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005012}
5013
Richard Smith290d8012016-04-06 17:06:00 +00005014void ASTRecordWriter::AddTemplateArgumentLocInfo(
5015 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005016 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005017 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005018 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005019 break;
5020 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005021 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005022 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005023 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005024 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5025 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005026 break;
5027 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005028 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5029 AddSourceLocation(Arg.getTemplateNameLoc());
5030 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005031 break;
John McCall0ad16662009-10-29 08:12:44 +00005032 case TemplateArgument::Null:
5033 case TemplateArgument::Integral:
5034 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005035 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005036 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005037 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005038 break;
5039 }
5040}
5041
Richard Smith290d8012016-04-06 17:06:00 +00005042void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5043 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005044
5045 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5046 bool InfoHasSameExpr
5047 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005048 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005049 if (InfoHasSameExpr)
5050 return; // Avoid storing the same expr twice.
5051 }
Richard Smith290d8012016-04-06 17:06:00 +00005052 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005053}
5054
Richard Smith290d8012016-04-06 17:06:00 +00005055void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005056 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005057 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005058 return;
5059 }
5060
Richard Smith290d8012016-04-06 17:06:00 +00005061 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005062}
5063
Richard Smith290d8012016-04-06 17:06:00 +00005064void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5065 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005066
Richard Smith290d8012016-04-06 17:06:00 +00005067 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005068 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005069 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005070}
5071
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005072void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005073 Record.push_back(GetOrCreateTypeID(T));
5074}
5075
Richard Smith2095ffe2015-03-16 20:11:03 +00005076TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005077 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005078 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5079 if (T.isNull())
5080 return TypeIdx();
5081 assert(!T.getLocalFastQualifiers());
5082
5083 TypeIdx &Idx = TypeIdxs[T];
5084 if (Idx.getIndex() == 0) {
5085 if (DoneWritingDeclsAndTypes) {
5086 assert(0 && "New type seen after serializing all the types to emit!");
5087 return TypeIdx();
5088 }
5089
5090 // We haven't seen this type before. Assign it a new ID and put it
5091 // into the queue of types to emit.
5092 Idx = TypeIdx(NextTypeID++);
5093 DeclTypesToEmit.push(T);
5094 }
5095 return Idx;
5096 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005097}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005098
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005099TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005100 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005101 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5102 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005103 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005104 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005105
Richard Smith2095ffe2015-03-16 20:11:03 +00005106 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5107 assert(I != TypeIdxs.end() && "Type not emitted!");
5108 return I->second;
5109 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005110}
5111
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005112void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005113 Record.push_back(GetDeclRef(D));
5114}
5115
Sebastian Redl539c5062010-08-18 23:57:32 +00005116DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005117 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005118
5119 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005120 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005121 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005122
5123 // If D comes from an AST file, its declaration ID is already known and
5124 // fixed.
5125 if (D->isFromASTFile())
5126 return D->getGlobalID();
5127
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005128 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005129 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005130 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005131 if (DoneWritingDeclsAndTypes) {
5132 assert(0 && "New decl seen after serializing all the decls to emit!");
5133 return 0;
5134 }
5135
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005136 // We haven't seen this declaration before. Give it a new ID and
5137 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005138 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005139 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005140 }
5141
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005142 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005143}
5144
Sebastian Redl539c5062010-08-18 23:57:32 +00005145DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005146 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005147 return 0;
5148
Douglas Gregorb3163e52012-01-05 22:33:30 +00005149 // If D comes from an AST file, its declaration ID is already known and
5150 // fixed.
5151 if (D->isFromASTFile())
5152 return D->getGlobalID();
5153
Douglas Gregore84a9da2009-04-20 20:36:09 +00005154 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5155 return DeclIDs[D];
5156}
5157
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005158void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005159 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005160 assert(D);
5161
5162 SourceLocation Loc = D->getLocation();
5163 if (Loc.isInvalid())
5164 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005165
5166 // We only keep track of the file-level declarations of each file.
5167 if (!D->getLexicalDeclContext()->isFileContext())
5168 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005169 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5170 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005171 if (isa<ParmVarDecl>(D))
5172 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005173
5174 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005175 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005176 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005177 FileID FID;
5178 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005179 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005180 if (FID.isInvalid())
5181 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005182 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005183
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005184 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005185 if (!Info)
5186 Info = new DeclIDInFileInfo();
5187
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005188 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005189 LocDeclIDsTy &Decls = Info->DeclIDs;
5190
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005191 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005192 Decls.push_back(LocDecl);
5193 return;
5194 }
5195
Benjamin Kramer45025c02013-08-24 13:22:59 +00005196 LocDeclIDsTy::iterator I =
5197 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005198
5199 Decls.insert(I, LocDecl);
5200}
5201
Richard Smithe64e7452016-04-18 21:54:58 +00005202void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005203 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005204 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005205 switch (Name.getNameKind()) {
5206 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005207 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005208 break;
5209
5210 case DeclarationName::ObjCZeroArgSelector:
5211 case DeclarationName::ObjCOneArgSelector:
5212 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005213 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005214 break;
5215
5216 case DeclarationName::CXXConstructorName:
5217 case DeclarationName::CXXDestructorName:
5218 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005219 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005220 break;
5221
5222 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005223 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005224 break;
5225
Alexis Hunt3d221f22009-11-29 07:34:05 +00005226 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005227 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005228 break;
5229
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005230 case DeclarationName::CXXUsingDirective:
5231 // No extra data to emit
5232 break;
5233 }
5234}
Chris Lattnerca025db2010-05-07 21:43:38 +00005235
Richard Smithd08aeb62014-08-28 01:33:39 +00005236unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5237 assert(needsAnonymousDeclarationNumber(D) &&
5238 "expected an anonymous declaration");
5239
5240 // Number the anonymous declarations within this context, if we've not
5241 // already done so.
5242 auto It = AnonymousDeclarationNumbers.find(D);
5243 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005244 auto *DC = D->getLexicalDeclContext();
5245 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5246 AnonymousDeclarationNumbers[ND] = Number;
5247 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005248
5249 It = AnonymousDeclarationNumbers.find(D);
5250 assert(It != AnonymousDeclarationNumbers.end() &&
5251 "declaration not found within its lexical context");
5252 }
5253
5254 return It->second;
5255}
5256
Richard Smith290d8012016-04-06 17:06:00 +00005257void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5258 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005259 switch (Name.getNameKind()) {
5260 case DeclarationName::CXXConstructorName:
5261 case DeclarationName::CXXDestructorName:
5262 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005263 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005264 break;
5265
5266 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005267 AddSourceLocation(SourceLocation::getFromRawEncoding(
5268 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005269 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005270 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005271 break;
5272
5273 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005274 AddSourceLocation(SourceLocation::getFromRawEncoding(
5275 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005276 break;
5277
5278 case DeclarationName::Identifier:
5279 case DeclarationName::ObjCZeroArgSelector:
5280 case DeclarationName::ObjCOneArgSelector:
5281 case DeclarationName::ObjCMultiArgSelector:
5282 case DeclarationName::CXXUsingDirective:
5283 break;
5284 }
5285}
5286
Richard Smith290d8012016-04-06 17:06:00 +00005287void ASTRecordWriter::AddDeclarationNameInfo(
5288 const DeclarationNameInfo &NameInfo) {
5289 AddDeclarationName(NameInfo.getName());
5290 AddSourceLocation(NameInfo.getLoc());
5291 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005292}
5293
Richard Smith290d8012016-04-06 17:06:00 +00005294void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5295 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5296 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005297 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005298 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005299}
5300
Richard Smithe64e7452016-04-18 21:54:58 +00005301void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005302 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005303 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005304 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005305
5306 // Push each of the NNS's onto a stack for serialization in reverse order.
5307 while (NNS) {
5308 NestedNames.push_back(NNS);
5309 NNS = NNS->getPrefix();
5310 }
5311
Richard Smithe64e7452016-04-18 21:54:58 +00005312 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005313 while(!NestedNames.empty()) {
5314 NNS = NestedNames.pop_back_val();
5315 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005316 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005317 switch (Kind) {
5318 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005319 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005320 break;
5321
5322 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005323 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005324 break;
5325
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005326 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005327 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005328 break;
5329
Chris Lattnerca025db2010-05-07 21:43:38 +00005330 case NestedNameSpecifier::TypeSpec:
5331 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005332 AddTypeRef(QualType(NNS->getAsType(), 0));
5333 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005334 break;
5335
5336 case NestedNameSpecifier::Global:
5337 // Don't need to write an associated value.
5338 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005339
5340 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005341 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005342 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005343 }
5344 }
5345}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005346
Richard Smith290d8012016-04-06 17:06:00 +00005347void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005348 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005349 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005350 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005351
5352 // Push each of the nested-name-specifiers's onto a stack for
5353 // serialization in reverse order.
5354 while (NNS) {
5355 NestedNames.push_back(NNS);
5356 NNS = NNS.getPrefix();
5357 }
5358
Richard Smith290d8012016-04-06 17:06:00 +00005359 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005360 while(!NestedNames.empty()) {
5361 NNS = NestedNames.pop_back_val();
5362 NestedNameSpecifier::SpecifierKind Kind
5363 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005364 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005365 switch (Kind) {
5366 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005367 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5368 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005369 break;
5370
5371 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005372 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5373 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005374 break;
5375
5376 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005377 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5378 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005379 break;
5380
5381 case NestedNameSpecifier::TypeSpec:
5382 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005383 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5384 AddTypeLoc(NNS.getTypeLoc());
5385 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005386 break;
5387
5388 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005389 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005390 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005391
5392 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005393 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5394 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005395 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005396 }
5397 }
5398}
5399
Richard Smith290d8012016-04-06 17:06:00 +00005400void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005401 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005402 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005403 switch (Kind) {
5404 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005405 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005406 break;
5407
5408 case TemplateName::OverloadedTemplate: {
5409 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005410 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005411 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005412 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005413 break;
5414 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005415
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005416 case TemplateName::QualifiedTemplate: {
5417 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005418 AddNestedNameSpecifier(QualT->getQualifier());
5419 Record->push_back(QualT->hasTemplateKeyword());
5420 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005421 break;
5422 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005423
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005424 case TemplateName::DependentTemplate: {
5425 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005426 AddNestedNameSpecifier(DepT->getQualifier());
5427 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005428 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005429 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005430 else
Richard Smith290d8012016-04-06 17:06:00 +00005431 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005432 break;
5433 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005434
5435 case TemplateName::SubstTemplateTemplateParm: {
5436 SubstTemplateTemplateParmStorage *subst
5437 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005438 AddDeclRef(subst->getParameter());
5439 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005440 break;
5441 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005442
5443 case TemplateName::SubstTemplateTemplateParmPack: {
5444 SubstTemplateTemplateParmPackStorage *SubstPack
5445 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005446 AddDeclRef(SubstPack->getParameterPack());
5447 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005448 break;
5449 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005450 }
5451}
5452
Richard Smith290d8012016-04-06 17:06:00 +00005453void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5454 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005455 switch (Arg.getKind()) {
5456 case TemplateArgument::Null:
5457 break;
5458 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005459 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005460 break;
5461 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005462 AddDeclRef(Arg.getAsDecl());
5463 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005464 break;
5465 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005466 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005467 break;
5468 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005469 AddAPSInt(Arg.getAsIntegral());
5470 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005471 break;
5472 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005473 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005474 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005475 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005476 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005477 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005478 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005479 else
Richard Smith290d8012016-04-06 17:06:00 +00005480 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005481 break;
5482 case TemplateArgument::Expression:
5483 AddStmt(Arg.getAsExpr());
5484 break;
5485 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005486 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005487 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005488 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005489 break;
5490 }
5491}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005492
Richard Smithe64e7452016-04-18 21:54:58 +00005493void ASTRecordWriter::AddTemplateParameterList(
5494 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005495 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005496 AddSourceLocation(TemplateParams->getTemplateLoc());
5497 AddSourceLocation(TemplateParams->getLAngleLoc());
5498 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005499 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005500 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005501 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005502 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005503}
5504
5505/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005506void ASTRecordWriter::AddTemplateArgumentList(
5507 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005508 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005509 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005510 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005511 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005512}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005513
Richard Smith290d8012016-04-06 17:06:00 +00005514void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5515 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005516 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005517 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5518 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5519 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005520 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005521 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005522 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005523}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005524
Richard Smithe64e7452016-04-18 21:54:58 +00005525void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5526 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005527 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005528 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005529 AddDeclRef(I.getDecl());
5530 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005531 }
5532}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005533
Richard Smith290d8012016-04-06 17:06:00 +00005534// FIXME: Move this out of the main ASTRecordWriter interface.
5535void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5536 Record->push_back(Base.isVirtual());
5537 Record->push_back(Base.isBaseOfClass());
5538 Record->push_back(Base.getAccessSpecifierAsWritten());
5539 Record->push_back(Base.getInheritConstructors());
5540 AddTypeSourceInfo(Base.getTypeSourceInfo());
5541 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005542 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005543 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005544}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005545
Richard Smith645d2cf2016-04-14 00:29:55 +00005546static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5547 ArrayRef<CXXBaseSpecifier> Bases) {
5548 ASTWriter::RecordData Record;
5549 ASTRecordWriter Writer(W, Record);
5550 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005551
Richard Smith645d2cf2016-04-14 00:29:55 +00005552 for (auto &Base : Bases)
5553 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005554
Richard Smith645d2cf2016-04-14 00:29:55 +00005555 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005556}
5557
Richard Smith290d8012016-04-06 17:06:00 +00005558// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005559void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5560 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5561}
5562
Richard Smithaa165cf2016-04-13 21:57:08 +00005563static uint64_t
5564EmitCXXCtorInitializers(ASTWriter &W,
5565 ArrayRef<CXXCtorInitializer *> CtorInits) {
5566 ASTWriter::RecordData Record;
5567 ASTRecordWriter Writer(W, Record);
5568 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005569
Richard Smithaa165cf2016-04-13 21:57:08 +00005570 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005571 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005572 Writer.push_back(CTOR_INITIALIZER_BASE);
5573 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5574 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005575 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005576 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5577 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005578 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005579 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5580 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005581 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005582 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5583 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005584 }
Francois Pichetd583da02010-12-04 09:14:42 +00005585
Richard Smithaa165cf2016-04-13 21:57:08 +00005586 Writer.AddSourceLocation(Init->getMemberLocation());
5587 Writer.AddStmt(Init->getInit());
5588 Writer.AddSourceLocation(Init->getLParenLoc());
5589 Writer.AddSourceLocation(Init->getRParenLoc());
5590 Writer.push_back(Init->isWritten());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005591 if (Init->isWritten()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005592 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005593 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005594 Writer.push_back(Init->getNumArrayIndices());
5595 for (auto *VD : Init->getArrayIndices())
5596 Writer.AddDeclRef(VD);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005597 }
5598 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005599
5600 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005601}
5602
Richard Smithaa165cf2016-04-13 21:57:08 +00005603// FIXME: Move this out of the main ASTRecordWriter interface.
5604void ASTRecordWriter::AddCXXCtorInitializers(
5605 ArrayRef<CXXCtorInitializer *> CtorInits) {
5606 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005607}
5608
Richard Smith290d8012016-04-06 17:06:00 +00005609void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005610 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005611 Record->push_back(Data.IsLambda);
5612 Record->push_back(Data.UserDeclaredConstructor);
5613 Record->push_back(Data.UserDeclaredSpecialMembers);
5614 Record->push_back(Data.Aggregate);
5615 Record->push_back(Data.PlainOldData);
5616 Record->push_back(Data.Empty);
5617 Record->push_back(Data.Polymorphic);
5618 Record->push_back(Data.Abstract);
5619 Record->push_back(Data.IsStandardLayout);
5620 Record->push_back(Data.HasNoNonEmptyBases);
5621 Record->push_back(Data.HasPrivateFields);
5622 Record->push_back(Data.HasProtectedFields);
5623 Record->push_back(Data.HasPublicFields);
5624 Record->push_back(Data.HasMutableFields);
5625 Record->push_back(Data.HasVariantMembers);
5626 Record->push_back(Data.HasOnlyCMembers);
5627 Record->push_back(Data.HasInClassInitializer);
5628 Record->push_back(Data.HasUninitializedReferenceMember);
5629 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005630 Record->push_back(Data.HasInheritedConstructor);
5631 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005632 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5633 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5634 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5635 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5636 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5637 Record->push_back(Data.DefaultedDestructorIsDeleted);
5638 Record->push_back(Data.HasTrivialSpecialMembers);
5639 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5640 Record->push_back(Data.HasIrrelevantDestructor);
5641 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5642 Record->push_back(Data.HasDefaultedDefaultConstructor);
5643 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5644 Record->push_back(Data.HasConstexprDefaultConstructor);
5645 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5646 Record->push_back(Data.ComputedVisibleConversions);
5647 Record->push_back(Data.UserProvidedDefaultConstructor);
5648 Record->push_back(Data.DeclaredSpecialMembers);
5649 Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
5650 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5651 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5652 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005653 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005654
Richard Smith290d8012016-04-06 17:06:00 +00005655 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005656 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005657 AddCXXBaseSpecifiers(Data.bases());
5658
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005659 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005660 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005661 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005662 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005663
Richard Smith290d8012016-04-06 17:06:00 +00005664 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5665 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005666 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005667 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005668
5669 // Add lambda-specific data.
5670 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005671 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005672 Record->push_back(Lambda.Dependent);
5673 Record->push_back(Lambda.IsGenericLambda);
5674 Record->push_back(Lambda.CaptureDefault);
5675 Record->push_back(Lambda.NumCaptures);
5676 Record->push_back(Lambda.NumExplicitCaptures);
5677 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005678 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005679 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005680 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005681 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005682 AddSourceLocation(Capture.getLocation());
5683 Record->push_back(Capture.isImplicit());
5684 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005685 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005686 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005687 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005688 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005689 break;
5690 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005691 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005692 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005693 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005694 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005695 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005696 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005697 break;
5698 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005699 }
5700 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005701}
5702
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005703void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005704 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005705 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005706 assert(FirstDeclID == NextDeclID &&
5707 FirstTypeID == NextTypeID &&
5708 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005709 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005710 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005711 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005712 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005713
Sebastian Redl07a89a82010-07-30 00:29:29 +00005714 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005715
Richard Smith7f330cd2015-03-18 01:42:29 +00005716 // Note, this will get called multiple times, once one the reader starts up
5717 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005718 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5719 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5720 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005721 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005722 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005723 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005724 NextDeclID = FirstDeclID;
5725 NextTypeID = FirstTypeID;
5726 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005727 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005728 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005729 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005730}
5731
Sebastian Redl539c5062010-08-18 23:57:32 +00005732void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005733 // Always keep the highest ID. See \p TypeRead() for more information.
5734 IdentID &StoredID = IdentifierIDs[II];
5735 if (ID > StoredID)
5736 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005737}
5738
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005739void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005740 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005741 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005742 if (ID > StoredID)
5743 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005744}
5745
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005746void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005747 // Always take the highest-numbered type index. This copes with an interesting
5748 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005749 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005750 // keep the higher-numbered entry so that we can properly write it out to
5751 // the AST file.
5752 TypeIdx &StoredIdx = TypeIdxs[T];
5753 if (Idx.getIndex() >= StoredIdx.getIndex())
5754 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005755}
5756
Sebastian Redl539c5062010-08-18 23:57:32 +00005757void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005758 // Always keep the highest ID. See \p TypeRead() for more information.
5759 SelectorID &StoredID = SelectorIDs[S];
5760 if (ID > StoredID)
5761 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005762}
Douglas Gregor91096292010-10-02 19:29:26 +00005763
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005764void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005765 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005766 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005767 MacroDefinitions[MD] = ID;
5768}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005769
Douglas Gregore37a85a2011-12-02 17:30:13 +00005770void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5771 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5772 SubmoduleIDs[Mod] = ID;
5773}
5774
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005775void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005776 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005777 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005778 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005779 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005780 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005781 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005782 // A forward reference was mutated into a definition. Rewrite it.
5783 // FIXME: This happens during template instantiation, should we
5784 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005785 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5786 "completed a tag from another module but not by instantiation?");
5787 DeclUpdates[RD].push_back(
5788 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005789 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005790 }
5791}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005792
Richard Smithf983f7f2015-10-13 00:23:25 +00005793static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5794 if (D->isFromASTFile())
5795 return true;
5796
Richard Smithf983f7f2015-10-13 00:23:25 +00005797 // The predefined __va_list_tag struct is imported if we imported any decls.
5798 // FIXME: This is a gross hack.
5799 return D == D->getASTContext().getVaListTagDecl();
5800}
5801
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005802void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005803 if (Chain && Chain->isProcessingUpdateRecords()) return;
5804 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005805 "Should not add lookup results to non-lookup contexts!");
5806
Vassil Vassilev632eac32016-03-16 11:17:04 +00005807 // TU is handled elsewhere.
5808 if (isa<TranslationUnitDecl>(DC))
5809 return;
5810
5811 // Namespaces are handled elsewhere, except for template instantiations of
5812 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5813 // local instantiations are added to an imported context. Only happens when
5814 // adding ADL lookup candidates, for example templated friends.
5815 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5816 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005817 return;
5818
Richard Smithf983f7f2015-10-13 00:23:25 +00005819 // We're only interested in cases where a local declaration is added to an
5820 // imported context.
5821 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5822 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005823
Richard Smith0f4e2c42015-08-06 04:23:48 +00005824 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005825 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005826 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005827 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5828 // We're adding a visible declaration to a predefined decl context. Ensure
5829 // that we write out all of its lookup results so we don't get a nasty
5830 // surprise when we try to emit its lookup table.
5831 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005832 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005833 }
Richard Smithc26d9742016-10-06 20:30:51 +00005834 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005835}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005836
5837void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005838 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005839 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005840
5841 // We're only interested in cases where a local declaration is added to an
5842 // imported context.
5843 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5844 return;
5845
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005846 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005847 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005848
5849 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005850 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005851 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005852 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005853}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005854
Richard Smith564417a2014-03-20 21:47:22 +00005855void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005856 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005857 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5858 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005859 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005860 // If we don't already know the exception specification for this redecl
5861 // chain, add an update record for it.
5862 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5863 ->getType()
5864 ->castAs<FunctionProtoType>()
5865 ->getExceptionSpecType()))
5866 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5867 });
Richard Smith564417a2014-03-20 21:47:22 +00005868}
5869
Richard Smith1fa5d642013-05-11 05:45:24 +00005870void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005871 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005872 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005873 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005874 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005875 DeclUpdates[D].push_back(
5876 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5877 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005878}
5879
Richard Smithf8134002015-03-10 01:41:22 +00005880void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5881 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005882 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005883 assert(!WritingAST && "Already writing the AST!");
5884 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005885 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005886 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005887 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5888 });
Richard Smithf8134002015-03-10 01:41:22 +00005889}
5890
Sebastian Redlab238a72011-04-24 16:28:06 +00005891void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005892 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005893 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005894 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005895 return; // Declaration not imported from PCH.
5896
Richard Smith4d235792014-08-07 18:53:08 +00005897 // Implicit function decl from a PCH was defined.
5898 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005899}
5900
Richard Smithd28ac5b2014-03-22 23:33:22 +00005901void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005902 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005903 assert(!WritingAST && "Already writing the AST!");
5904 if (!D->isFromASTFile())
5905 return;
5906
Richard Smith9e2341d2015-03-23 03:25:59 +00005907 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005908}
5909
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005910void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005911 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005912 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005913 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005914 return;
5915
5916 // Since the actual instantiation is delayed, this really means that we need
5917 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005918 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005919 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5920 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005921}
5922
John McCall32791cc2016-01-06 22:34:54 +00005923void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005924 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005925 assert(!WritingAST && "Already writing the AST!");
5926 if (!D->isFromASTFile())
5927 return;
5928
5929 DeclUpdates[D].push_back(
5930 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5931}
5932
Richard Smith4b054b22016-08-24 21:25:37 +00005933void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5934 assert(!WritingAST && "Already writing the AST!");
5935 if (!D->isFromASTFile())
5936 return;
5937
5938 DeclUpdates[D].push_back(
5939 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5940}
5941
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005942void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5943 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005944 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005945 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005946 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005947 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005948
5949 assert(IFD->getDefinition() && "Category on a class without a definition?");
5950 ObjCClassesWithCategories.insert(
5951 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005952}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005953
Eli Friedman276dd182013-09-05 00:02:25 +00005954void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005955 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005956 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005957
5958 // If there is *any* declaration of the entity that's not from an AST file,
5959 // we can skip writing the update record. We make sure that isUsed() triggers
5960 // completion of the redeclaration chain of the entity.
5961 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5962 if (IsLocalDecl(Prev))
5963 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005964
Aaron Ballman4f45b712014-03-21 15:22:56 +00005965 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005966}
Alexey Bataev97720002014-11-11 04:05:39 +00005967
5968void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005969 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005970 assert(!WritingAST && "Already writing the AST!");
5971 if (!D->isFromASTFile())
5972 return;
5973
5974 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5975}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005976
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005977void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
5978 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005979 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005980 assert(!WritingAST && "Already writing the AST!");
5981 if (!D->isFromASTFile())
5982 return;
5983
Dmitry Polukhind69b5052016-05-09 14:59:13 +00005984 DeclUpdates[D].push_back(
5985 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005986}
5987
Richard Smith4caa4492015-05-15 02:34:32 +00005988void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005989 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005990 assert(!WritingAST && "Already writing the AST!");
5991 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005992 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005993}
Alex Denisovfde64952015-06-26 05:28:36 +00005994
5995void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5996 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005997 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00005998 assert(!WritingAST && "Already writing the AST!");
5999 if (!Record->isFromASTFile())
6000 return;
6001 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6002}
Richard Smithc26d9742016-10-06 20:30:51 +00006003
6004void ASTWriter::AddedCXXTemplateSpecialization(
6005 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6006 assert(!WritingAST && "Already writing the AST!");
6007
6008 if (!TD->getFirstDecl()->isFromASTFile())
6009 return;
6010 if (Chain && Chain->isProcessingUpdateRecords())
6011 return;
6012
6013 DeclsToEmitEvenIfUnreferenced.push_back(D);
6014}
6015
6016void ASTWriter::AddedCXXTemplateSpecialization(
6017 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6018 assert(!WritingAST && "Already writing the AST!");
6019
6020 if (!TD->getFirstDecl()->isFromASTFile())
6021 return;
6022 if (Chain && Chain->isProcessingUpdateRecords())
6023 return;
6024
6025 DeclsToEmitEvenIfUnreferenced.push_back(D);
6026}
6027
6028void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6029 const FunctionDecl *D) {
6030 assert(!WritingAST && "Already writing the AST!");
6031
6032 if (!TD->getFirstDecl()->isFromASTFile())
6033 return;
6034 if (Chain && Chain->isProcessingUpdateRecords())
6035 return;
6036
6037 DeclsToEmitEvenIfUnreferenced.push_back(D);
6038}