blob: 524bc2dae730e908aa53612b07707f398253e093 [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"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000021#include "clang/AST/DeclCXX.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000022#include "clang/AST/DeclContextInternals.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"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000036#include "clang/Basic/LLVM.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000037#include "clang/Basic/LangOptions.h"
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000038#include "clang/Basic/MemoryBufferCache.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000039#include "clang/Basic/Module.h"
40#include "clang/Basic/ObjCRuntime.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000041#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000042#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000043#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000044#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000045#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000046#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000047#include "clang/Lex/HeaderSearch.h"
48#include "clang/Lex/HeaderSearchOptions.h"
49#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000050#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000051#include "clang/Lex/PreprocessingRecord.h"
52#include "clang/Lex/Preprocessor.h"
53#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000054#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000055#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000056#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000057#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000058#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000059#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000060#include "clang/Serialization/Module.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000061#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000062#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000063#include "llvm/ADT/APFloat.h"
64#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000065#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000066#include "llvm/ADT/IntrusiveRefCntPtr.h"
67#include "llvm/ADT/Optional.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000068#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000069#include "llvm/ADT/SmallSet.h"
70#include "llvm/ADT/SmallString.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000071#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000072#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000074#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000075#include "llvm/Support/Compression.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000076#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000077#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000078#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000079#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000080#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000081#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000082#include "llvm/Support/Process.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000083#include "llvm/Support/SHA1.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000084#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000085#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000086#include <cassert>
87#include <cstdint>
88#include <cstdlib>
89#include <cstring>
90#include <deque>
91#include <limits>
92#include <new>
93#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +000094#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000095
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000097using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098
Sebastian Redl3df5a082010-07-30 17:03:48 +000099template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +0000100static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000101 if (v.empty()) return StringRef();
102 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000103 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000104}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000105
106template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000107static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000108 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000109 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000110}
111
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112//===----------------------------------------------------------------------===//
113// Type serialization
114//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000115
Richard Smith290d8012016-04-06 17:06:00 +0000116namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000119 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000120 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +0000123 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +0000124 /// \brief Abbreviation to use for the record, if any.
125 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126
Richard Smith290d8012016-04-06 17:06:00 +0000127 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000128 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Richard Smith290d8012016-04-06 17:06:00 +0000129 : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
130
131 uint64_t Emit() {
132 return Record.Emit(Code, AbbrevToUse);
133 }
134
135 void Visit(QualType T) {
136 if (T.hasLocalNonFastQualifiers()) {
137 Qualifiers Qs = T.getLocalQualifiers();
138 Record.AddTypeRef(T.getLocalUnqualifiedType());
139 Record.push_back(Qs.getAsOpaqueValue());
140 Code = TYPE_EXT_QUAL;
141 AbbrevToUse = Writer.TypeExtQualAbbrev;
142 } else {
143 switch (T->getTypeClass()) {
144 // For all of the concrete, non-dependent types, call the
145 // appropriate visitor function.
146#define TYPE(Class, Base) \
147 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
148#define ABSTRACT_TYPE(Class, Base)
149#include "clang/AST/TypeNodes.def"
150 }
151 }
152 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153
154 void VisitArrayType(const ArrayType *T);
155 void VisitFunctionType(const FunctionType *T);
156 void VisitTagType(const TagType *T);
157
158#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
159#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160#include "clang/AST/TypeNodes.def"
161 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000162
Richard Smith290d8012016-04-06 17:06:00 +0000163} // end namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000166 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000170 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000171 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000172}
173
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000174void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000175 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000176 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177}
178
Reid Kleckner8a365022013-06-24 17:51:48 +0000179void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000180 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000181 Code = TYPE_DECAYED;
182}
183
Reid Kleckner0503a872013-12-05 01:23:43 +0000184void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000185 Record.AddTypeRef(T->getOriginalType());
186 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000187 Code = TYPE_ADJUSTED;
188}
189
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000190void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000191 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000192 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193}
194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000196 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000197 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000202 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000207 Record.AddTypeRef(T->getPointeeType());
208 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000209 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000210}
211
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000213 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000215 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216}
217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000220 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000221 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222}
223
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000226 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227}
228
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000231 Record.AddSourceLocation(T->getLBracketLoc());
232 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000233 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000234 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235}
236
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000237void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000238 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000240 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000246 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000247}
248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000250 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000251 FunctionType::ExtInfo C = T->getExtInfo();
252 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000253 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000254 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000255 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000256 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000257 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000258
259 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
260 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261}
262
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000263void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000264 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000265 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000266}
267
Richard Smith290d8012016-04-06 17:06:00 +0000268static void addExceptionSpec(const FunctionProtoType *T,
269 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000270 Record.push_back(T->getExceptionSpecType());
271 if (T->getExceptionSpecType() == EST_Dynamic) {
272 Record.push_back(T->getNumExceptions());
273 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000274 Record.AddTypeRef(T->getExceptionType(I));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000275 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
Richard Smith290d8012016-04-06 17:06:00 +0000276 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000277 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000278 Record.AddDeclRef(T->getExceptionSpecDecl());
279 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000280 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000281 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000282 }
Richard Smith564417a2014-03-20 21:47:22 +0000283}
284
285void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
286 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000287
Richard Smith564417a2014-03-20 21:47:22 +0000288 Record.push_back(T->isVariadic());
289 Record.push_back(T->hasTrailingReturn());
290 Record.push_back(T->getTypeQuals());
291 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000292 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000293
294 Record.push_back(T->getNumParams());
295 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000296 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000297
John McCall18afab72016-03-01 00:49:02 +0000298 if (T->hasExtParameterInfos()) {
299 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
300 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
301 }
302
Richard Smith01b2cb42014-07-26 06:37:51 +0000303 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000304 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
305 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000306 AbbrevToUse = 0;
307
Sebastian Redl539c5062010-08-18 23:57:32 +0000308 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000309}
310
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000311void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000312 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000313 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000314}
John McCallb96ec562009-12-04 22:46:56 +0000315
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000316void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000317 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000318 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000319 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000320 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000321}
322
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000323void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000324 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000325 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000326}
327
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000328void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000329 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000330 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000331}
332
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000333void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000334 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000335 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000337}
338
Alexis Hunte852b102011-05-24 22:41:36 +0000339void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000340 Record.AddTypeRef(T->getBaseType());
341 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000342 Record.push_back(T->getUTTKind());
343 Code = TYPE_UNARY_TRANSFORM;
344}
345
Richard Smith30482bc2011-02-20 03:19:35 +0000346void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000347 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000348 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000349 if (T->getDeducedType().isNull())
350 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000351 Code = TYPE_AUTO;
352}
353
Richard Smith600b5262017-01-26 20:40:47 +0000354void ASTTypeWriter::VisitDeducedTemplateSpecializationType(
355 const DeducedTemplateSpecializationType *T) {
356 Record.AddTemplateName(T->getTemplateName());
357 Record.AddTypeRef(T->getDeducedType());
358 if (T->getDeducedType().isNull())
359 Record.push_back(T->isDependentType());
360 Code = TYPE_DEDUCED_TEMPLATE_SPECIALIZATION;
361}
362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000363void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000364 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000365 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000366 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000367 "Cannot serialize in the middle of a type definition");
368}
369
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000370void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000371 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000372 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000373}
374
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000375void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000376 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000377 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000378}
379
John McCall81904512011-01-06 01:58:22 +0000380void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000381 Record.AddTypeRef(T->getModifiedType());
382 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000383 Record.push_back(T->getAttrKind());
384 Code = TYPE_ATTRIBUTED;
385}
386
Mike Stump11289f42009-09-09 15:08:12 +0000387void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000388ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000389 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000390 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
391 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000392 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000393}
394
395void
Douglas Gregorada4b792011-01-14 02:55:32 +0000396ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
397 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000398 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
399 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000400 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
401}
402
403void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000404ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000405 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000406 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000407 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000408 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000409 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000410 Record.AddTemplateArgument(ArgI);
411 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
412 : T->isCanonicalUnqualified()
413 ? QualType()
414 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000415 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000416}
417
418void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000419ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000420 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000421 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000422 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000423 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000424}
425
426void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000427ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000428 const DependentSizedExtVectorType *T) {
429 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000430 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000431}
432
433void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000434ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000435 Record.push_back(T->getDepth());
436 Record.push_back(T->getIndex());
437 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000438 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000439 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000440}
441
442void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000443ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000444 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000445 Record.AddNestedNameSpecifier(T->getQualifier());
446 Record.AddIdentifierRef(T->getIdentifier());
447 Record.AddTypeRef(
448 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000449 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000450}
451
452void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000453ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000454 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000455 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000456 Record.AddNestedNameSpecifier(T->getQualifier());
457 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000458 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000459 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000460 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000461 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000462}
463
Douglas Gregord2fa7662010-12-20 02:24:11 +0000464void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000465 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000466 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000467 Record.push_back(*NumExpansions + 1);
468 else
469 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000470 Code = TYPE_PACK_EXPANSION;
471}
472
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000473void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000474 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000475 Code = TYPE_PAREN;
476}
477
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000478void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000479 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000480 Record.AddNestedNameSpecifier(T->getQualifier());
481 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000482 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000483}
484
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000485void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000486 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
487 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000488 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000489}
490
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000491void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000492 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000493 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000494}
495
Manman Rene6be26c2016-09-13 17:25:08 +0000496void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
497 Record.AddDeclRef(T->getDecl());
498 Record.push_back(T->getNumProtocols());
499 for (const auto *I : T->quals())
500 Record.AddDeclRef(I);
501 Code = TYPE_OBJC_TYPE_PARAM;
502}
503
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000504void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000505 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000506 Record.push_back(T->getTypeArgsAsWritten().size());
507 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000508 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000509 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000510 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000511 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000512 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000513 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000514}
515
Steve Narofffb4330f2009-06-17 22:40:22 +0000516void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000517ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000518 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000519 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000520}
521
Eli Friedman0dfb8892011-10-06 23:00:33 +0000522void
523ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000524 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000525 Code = TYPE_ATOMIC;
526}
527
Xiuli Pan9c14e282016-01-09 12:53:17 +0000528void
529ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000530 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000531 Record.push_back(T->isReadOnly());
532 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000533}
534
John McCall8f115c62009-10-16 21:56:05 +0000535namespace {
536
537class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000538 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000539
540public:
Richard Smith290d8012016-04-06 17:06:00 +0000541 TypeLocWriter(ASTRecordWriter &Record)
542 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000543
John McCall17001972009-10-18 01:05:36 +0000544#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000545#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000546 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000547#include "clang/AST/TypeLocNodes.def"
548
John McCall17001972009-10-18 01:05:36 +0000549 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
550 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000551};
552
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000553} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000554
John McCall17001972009-10-18 01:05:36 +0000555void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
556 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000557}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000558
John McCall17001972009-10-18 01:05:36 +0000559void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000560 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000561 if (TL.needsExtraLocalData()) {
562 Record.push_back(TL.getWrittenTypeSpec());
563 Record.push_back(TL.getWrittenSignSpec());
564 Record.push_back(TL.getWrittenWidthSpec());
565 Record.push_back(TL.hasModeAttr());
566 }
John McCall8f115c62009-10-16 21:56:05 +0000567}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000568
John McCall17001972009-10-18 01:05:36 +0000569void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000570 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000571}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000572
John McCall17001972009-10-18 01:05:36 +0000573void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000574 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000575}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000576
Reid Kleckner8a365022013-06-24 17:51:48 +0000577void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
578 // nothing to do
579}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000580
Reid Kleckner0503a872013-12-05 01:23:43 +0000581void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
582 // nothing to do
583}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000584
John McCall17001972009-10-18 01:05:36 +0000585void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000586 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000587}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000588
John McCall17001972009-10-18 01:05:36 +0000589void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000590 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000591}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000592
John McCall17001972009-10-18 01:05:36 +0000593void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000594 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000595}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000596
John McCall17001972009-10-18 01:05:36 +0000597void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000598 Record.AddSourceLocation(TL.getStarLoc());
599 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000600}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000601
John McCall17001972009-10-18 01:05:36 +0000602void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000603 Record.AddSourceLocation(TL.getLBracketLoc());
604 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000605 Record.push_back(TL.getSizeExpr() ? 1 : 0);
606 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000607 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000608}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000609
John McCall17001972009-10-18 01:05:36 +0000610void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
611 VisitArrayTypeLoc(TL);
612}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000613
John McCall17001972009-10-18 01:05:36 +0000614void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
615 VisitArrayTypeLoc(TL);
616}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000617
John McCall17001972009-10-18 01:05:36 +0000618void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
619 VisitArrayTypeLoc(TL);
620}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000621
John McCall17001972009-10-18 01:05:36 +0000622void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
623 DependentSizedArrayTypeLoc TL) {
624 VisitArrayTypeLoc(TL);
625}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000626
John McCall17001972009-10-18 01:05:36 +0000627void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
628 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000629 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000630}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000631
John McCall17001972009-10-18 01:05:36 +0000632void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000633 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000634}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000635
John McCall17001972009-10-18 01:05:36 +0000636void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000637 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000638}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000639
John McCall17001972009-10-18 01:05:36 +0000640void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000641 Record.AddSourceLocation(TL.getLocalRangeBegin());
642 Record.AddSourceLocation(TL.getLParenLoc());
643 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000644 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000645 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000646 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000647 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000648}
649void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
650 VisitFunctionTypeLoc(TL);
651}
652void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
653 VisitFunctionTypeLoc(TL);
654}
John McCallb96ec562009-12-04 22:46:56 +0000655void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000656 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000657}
John McCall17001972009-10-18 01:05:36 +0000658void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000659 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000660}
Manman Rene6be26c2016-09-13 17:25:08 +0000661void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
662 if (TL.getNumProtocols()) {
663 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
664 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
665 }
666 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
667 Record.AddSourceLocation(TL.getProtocolLoc(i));
668}
John McCall17001972009-10-18 01:05:36 +0000669void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000670 Record.AddSourceLocation(TL.getTypeofLoc());
671 Record.AddSourceLocation(TL.getLParenLoc());
672 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000673}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000674
John McCall17001972009-10-18 01:05:36 +0000675void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000676 Record.AddSourceLocation(TL.getTypeofLoc());
677 Record.AddSourceLocation(TL.getLParenLoc());
678 Record.AddSourceLocation(TL.getRParenLoc());
679 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000680}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000681
John McCall17001972009-10-18 01:05:36 +0000682void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000683 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000684}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000685
Alexis Hunte852b102011-05-24 22:41:36 +0000686void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000687 Record.AddSourceLocation(TL.getKWLoc());
688 Record.AddSourceLocation(TL.getLParenLoc());
689 Record.AddSourceLocation(TL.getRParenLoc());
690 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000691}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000692
Richard Smith30482bc2011-02-20 03:19:35 +0000693void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000694 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000695}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000696
Richard Smith600b5262017-01-26 20:40:47 +0000697void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
698 DeducedTemplateSpecializationTypeLoc TL) {
699 Record.AddSourceLocation(TL.getTemplateNameLoc());
700}
701
John McCall17001972009-10-18 01:05:36 +0000702void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000703 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000704}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000705
John McCall17001972009-10-18 01:05:36 +0000706void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000707 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000708}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000709
John McCall81904512011-01-06 01:58:22 +0000710void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000711 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000712 if (TL.hasAttrOperand()) {
713 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000714 Record.AddSourceLocation(range.getBegin());
715 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000716 }
717 if (TL.hasAttrExprOperand()) {
718 Expr *operand = TL.getAttrExprOperand();
719 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000720 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000721 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000722 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000723 }
724}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000725
John McCall17001972009-10-18 01:05:36 +0000726void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000727 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000728}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000729
John McCallcebee162009-10-18 09:09:24 +0000730void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
731 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000732 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000733}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000734
Douglas Gregorada4b792011-01-14 02:55:32 +0000735void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
736 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000737 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000738}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000739
John McCall17001972009-10-18 01:05:36 +0000740void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
741 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000742 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
743 Record.AddSourceLocation(TL.getTemplateNameLoc());
744 Record.AddSourceLocation(TL.getLAngleLoc());
745 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000746 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000747 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
748 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000749}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000750
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000751void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000752 Record.AddSourceLocation(TL.getLParenLoc());
753 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000754}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000755
Abramo Bagnara6150c882010-05-11 21:36:43 +0000756void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000757 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
758 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000759}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000760
John McCalle78aac42010-03-10 03:28:59 +0000761void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000762 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000763}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000764
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000765void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000766 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
767 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
768 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000769}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000770
John McCallc392f372010-06-11 00:33:02 +0000771void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
772 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000773 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
774 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
775 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
776 Record.AddSourceLocation(TL.getTemplateNameLoc());
777 Record.AddSourceLocation(TL.getLAngleLoc());
778 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000779 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000780 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
781 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000782}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000783
Douglas Gregord2fa7662010-12-20 02:24:11 +0000784void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000785 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000786}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000787
John McCall17001972009-10-18 01:05:36 +0000788void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000789 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000790}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000791
John McCall8b07ec22010-05-15 11:32:37 +0000792void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
793 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000794 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
795 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000796 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000797 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
798 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
799 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000800 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000801 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000802}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000803
John McCallfc93cf92009-10-22 22:37:11 +0000804void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000805 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000806}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000807
Eli Friedman0dfb8892011-10-06 23:00:33 +0000808void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000809 Record.AddSourceLocation(TL.getKWLoc());
810 Record.AddSourceLocation(TL.getLParenLoc());
811 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000812}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000813
Xiuli Pan9c14e282016-01-09 12:53:17 +0000814void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000815 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000816}
John McCall8f115c62009-10-16 21:56:05 +0000817
Richard Smith01b2cb42014-07-26 06:37:51 +0000818void ASTWriter::WriteTypeAbbrevs() {
819 using namespace llvm;
820
David Blaikieb44f0bf2017-01-04 22:36:43 +0000821 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000822
823 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000824 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000825 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
826 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
827 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000828 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000829
830 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000831 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000832 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
833 // FunctionType
834 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
835 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
836 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
837 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
838 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
839 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
840 // FunctionProtoType
841 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
842 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
843 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
844 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
845 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
846 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
847 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
848 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000849 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000850}
851
Chris Lattner19cea4e2009-04-22 05:57:30 +0000852//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000853// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000854//===----------------------------------------------------------------------===//
855
Chris Lattner28fa4e62009-04-26 22:26:21 +0000856static void EmitBlockID(unsigned ID, const char *Name,
857 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000858 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000859 Record.clear();
860 Record.push_back(ID);
861 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
862
863 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000864 if (!Name || Name[0] == 0)
865 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000866 Record.clear();
867 while (*Name)
868 Record.push_back(*Name++);
869 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
870}
871
872static void EmitRecordID(unsigned ID, const char *Name,
873 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000874 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000875 Record.clear();
876 Record.push_back(ID);
877 while (*Name)
878 Record.push_back(*Name++);
879 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000880}
881
882static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000883 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000884#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000885 RECORD(STMT_STOP);
886 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000887 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000888 RECORD(STMT_NULL);
889 RECORD(STMT_COMPOUND);
890 RECORD(STMT_CASE);
891 RECORD(STMT_DEFAULT);
892 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000893 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000894 RECORD(STMT_IF);
895 RECORD(STMT_SWITCH);
896 RECORD(STMT_WHILE);
897 RECORD(STMT_DO);
898 RECORD(STMT_FOR);
899 RECORD(STMT_GOTO);
900 RECORD(STMT_INDIRECT_GOTO);
901 RECORD(STMT_CONTINUE);
902 RECORD(STMT_BREAK);
903 RECORD(STMT_RETURN);
904 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000905 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000906 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000907 RECORD(EXPR_PREDEFINED);
908 RECORD(EXPR_DECL_REF);
909 RECORD(EXPR_INTEGER_LITERAL);
910 RECORD(EXPR_FLOATING_LITERAL);
911 RECORD(EXPR_IMAGINARY_LITERAL);
912 RECORD(EXPR_STRING_LITERAL);
913 RECORD(EXPR_CHARACTER_LITERAL);
914 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000915 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000916 RECORD(EXPR_UNARY_OPERATOR);
917 RECORD(EXPR_SIZEOF_ALIGN_OF);
918 RECORD(EXPR_ARRAY_SUBSCRIPT);
919 RECORD(EXPR_CALL);
920 RECORD(EXPR_MEMBER);
921 RECORD(EXPR_BINARY_OPERATOR);
922 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
923 RECORD(EXPR_CONDITIONAL_OPERATOR);
924 RECORD(EXPR_IMPLICIT_CAST);
925 RECORD(EXPR_CSTYLE_CAST);
926 RECORD(EXPR_COMPOUND_LITERAL);
927 RECORD(EXPR_EXT_VECTOR_ELEMENT);
928 RECORD(EXPR_INIT_LIST);
929 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000930 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000931 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000932 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000933 RECORD(EXPR_VA_ARG);
934 RECORD(EXPR_ADDR_LABEL);
935 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000936 RECORD(EXPR_CHOOSE);
937 RECORD(EXPR_GNU_NULL);
938 RECORD(EXPR_SHUFFLE_VECTOR);
939 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000940 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000941 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000942 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000943 RECORD(EXPR_OBJC_ARRAY_LITERAL);
944 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000945 RECORD(EXPR_OBJC_ENCODE);
946 RECORD(EXPR_OBJC_SELECTOR_EXPR);
947 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
948 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
949 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
950 RECORD(EXPR_OBJC_KVC_REF_EXPR);
951 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000952 RECORD(STMT_OBJC_FOR_COLLECTION);
953 RECORD(STMT_OBJC_CATCH);
954 RECORD(STMT_OBJC_FINALLY);
955 RECORD(STMT_OBJC_AT_TRY);
956 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
957 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000958 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000959 RECORD(STMT_CXX_CATCH);
960 RECORD(STMT_CXX_TRY);
961 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000962 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000963 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000964 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000965 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000966 RECORD(EXPR_CXX_STATIC_CAST);
967 RECORD(EXPR_CXX_DYNAMIC_CAST);
968 RECORD(EXPR_CXX_REINTERPRET_CAST);
969 RECORD(EXPR_CXX_CONST_CAST);
970 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000971 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000972 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000973 RECORD(EXPR_CXX_BOOL_LITERAL);
974 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000975 RECORD(EXPR_CXX_TYPEID_EXPR);
976 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000977 RECORD(EXPR_CXX_THIS);
978 RECORD(EXPR_CXX_THROW);
979 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000980 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000981 RECORD(EXPR_CXX_BIND_TEMPORARY);
982 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
983 RECORD(EXPR_CXX_NEW);
984 RECORD(EXPR_CXX_DELETE);
985 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
986 RECORD(EXPR_EXPR_WITH_CLEANUPS);
987 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
988 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
989 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
990 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
991 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000992 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000993 RECORD(EXPR_CXX_NOEXCEPT);
994 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000995 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
996 RECORD(EXPR_TYPE_TRAIT);
997 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000998 RECORD(EXPR_PACK_EXPANSION);
999 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001000 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001001 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001002 RECORD(EXPR_FUNCTION_PARM_PACK);
1003 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001004 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001005 RECORD(EXPR_CXX_UUIDOF_EXPR);
1006 RECORD(EXPR_CXX_UUIDOF_TYPE);
1007 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001008#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001009}
Mike Stump11289f42009-09-09 15:08:12 +00001010
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001011void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001012 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001013 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001014
Sebastian Redl539c5062010-08-18 23:57:32 +00001015#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1016#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001017
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001018 // Control Block.
1019 BLOCK(CONTROL_BLOCK);
1020 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001021 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001022 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001023 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001024 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001025 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001026 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001027 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001028 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001029
1030 BLOCK(OPTIONS_BLOCK);
1031 RECORD(LANGUAGE_OPTIONS);
1032 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001033 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001034 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001035 RECORD(PREPROCESSOR_OPTIONS);
1036
Douglas Gregor108cb222012-10-19 00:45:00 +00001037 BLOCK(INPUT_FILES_BLOCK);
1038 RECORD(INPUT_FILE);
1039
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001040 // AST Top-Level Block.
1041 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001042 RECORD(TYPE_OFFSET);
1043 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001044 RECORD(IDENTIFIER_OFFSET);
1045 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001046 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001047 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001048 RECORD(SPECIAL_TYPES);
1049 RECORD(STATISTICS);
1050 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001051 RECORD(SELECTOR_OFFSETS);
1052 RECORD(METHOD_POOL);
1053 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001054 RECORD(SOURCE_LOCATION_OFFSETS);
1055 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001056 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001057 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001058 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001059 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001060 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001061 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001062 RECORD(SEMA_DECL_REFS);
1063 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1064 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001065 RECORD(UPDATE_VISIBLE);
1066 RECORD(DECL_UPDATE_OFFSETS);
1067 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001068 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001069 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001070 RECORD(FP_PRAGMA_OPTIONS);
1071 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001072 RECORD(OPENCL_EXTENSION_TYPES);
1073 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001074 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001075 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001076 RECORD(MODULE_OFFSET_MAP);
1077 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001078 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001079 RECORD(FILE_SORTED_DECLS);
1080 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001081 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001082 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001083 RECORD(INTERESTING_IDENTIFIERS);
1084 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001085 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001086 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001087 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001088 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001089 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001090 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001091 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001092
Chris Lattner28fa4e62009-04-26 22:26:21 +00001093 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001094 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001095 RECORD(SM_SLOC_FILE_ENTRY);
1096 RECORD(SM_SLOC_BUFFER_ENTRY);
1097 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001098 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001099 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattner28fa4e62009-04-26 22:26:21 +00001101 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001102 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001103 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001104 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001105 RECORD(PP_MACRO_OBJECT_LIKE);
1106 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001107 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001108
Richard Smithfa715652015-08-31 22:43:10 +00001109 // Submodule Block.
1110 BLOCK(SUBMODULE_BLOCK);
1111 RECORD(SUBMODULE_METADATA);
1112 RECORD(SUBMODULE_DEFINITION);
1113 RECORD(SUBMODULE_UMBRELLA_HEADER);
1114 RECORD(SUBMODULE_HEADER);
1115 RECORD(SUBMODULE_TOPHEADER);
1116 RECORD(SUBMODULE_UMBRELLA_DIR);
1117 RECORD(SUBMODULE_IMPORTS);
1118 RECORD(SUBMODULE_EXPORTS);
1119 RECORD(SUBMODULE_REQUIRES);
1120 RECORD(SUBMODULE_EXCLUDED_HEADER);
1121 RECORD(SUBMODULE_LINK_LIBRARY);
1122 RECORD(SUBMODULE_CONFIG_MACRO);
1123 RECORD(SUBMODULE_CONFLICT);
1124 RECORD(SUBMODULE_PRIVATE_HEADER);
1125 RECORD(SUBMODULE_TEXTUAL_HEADER);
1126 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001127 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001128
1129 // Comments Block.
1130 BLOCK(COMMENTS_BLOCK);
1131 RECORD(COMMENTS_RAW_COMMENT);
1132
Douglas Gregor12bfa382009-10-17 00:13:19 +00001133 // Decls and Types block.
1134 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001135 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001136 RECORD(TYPE_COMPLEX);
1137 RECORD(TYPE_POINTER);
1138 RECORD(TYPE_BLOCK_POINTER);
1139 RECORD(TYPE_LVALUE_REFERENCE);
1140 RECORD(TYPE_RVALUE_REFERENCE);
1141 RECORD(TYPE_MEMBER_POINTER);
1142 RECORD(TYPE_CONSTANT_ARRAY);
1143 RECORD(TYPE_INCOMPLETE_ARRAY);
1144 RECORD(TYPE_VARIABLE_ARRAY);
1145 RECORD(TYPE_VECTOR);
1146 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001147 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001148 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001149 RECORD(TYPE_TYPEDEF);
1150 RECORD(TYPE_TYPEOF_EXPR);
1151 RECORD(TYPE_TYPEOF);
1152 RECORD(TYPE_RECORD);
1153 RECORD(TYPE_ENUM);
1154 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001155 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001156 RECORD(TYPE_DECLTYPE);
1157 RECORD(TYPE_ELABORATED);
1158 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1159 RECORD(TYPE_UNRESOLVED_USING);
1160 RECORD(TYPE_INJECTED_CLASS_NAME);
1161 RECORD(TYPE_OBJC_OBJECT);
1162 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1163 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1164 RECORD(TYPE_DEPENDENT_NAME);
1165 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1166 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1167 RECORD(TYPE_PAREN);
1168 RECORD(TYPE_PACK_EXPANSION);
1169 RECORD(TYPE_ATTRIBUTED);
1170 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001171 RECORD(TYPE_AUTO);
1172 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001173 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001174 RECORD(TYPE_DECAYED);
1175 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001176 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001177 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001178 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001179 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001180 RECORD(DECL_ENUM);
1181 RECORD(DECL_RECORD);
1182 RECORD(DECL_ENUM_CONSTANT);
1183 RECORD(DECL_FUNCTION);
1184 RECORD(DECL_OBJC_METHOD);
1185 RECORD(DECL_OBJC_INTERFACE);
1186 RECORD(DECL_OBJC_PROTOCOL);
1187 RECORD(DECL_OBJC_IVAR);
1188 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001189 RECORD(DECL_OBJC_CATEGORY);
1190 RECORD(DECL_OBJC_CATEGORY_IMPL);
1191 RECORD(DECL_OBJC_IMPLEMENTATION);
1192 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1193 RECORD(DECL_OBJC_PROPERTY);
1194 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001195 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001196 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001197 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001198 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001199 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001200 RECORD(DECL_FILE_SCOPE_ASM);
1201 RECORD(DECL_BLOCK);
1202 RECORD(DECL_CONTEXT_LEXICAL);
1203 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001204 RECORD(DECL_NAMESPACE);
1205 RECORD(DECL_NAMESPACE_ALIAS);
1206 RECORD(DECL_USING);
1207 RECORD(DECL_USING_SHADOW);
1208 RECORD(DECL_USING_DIRECTIVE);
1209 RECORD(DECL_UNRESOLVED_USING_VALUE);
1210 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1211 RECORD(DECL_LINKAGE_SPEC);
1212 RECORD(DECL_CXX_RECORD);
1213 RECORD(DECL_CXX_METHOD);
1214 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001215 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001216 RECORD(DECL_CXX_DESTRUCTOR);
1217 RECORD(DECL_CXX_CONVERSION);
1218 RECORD(DECL_ACCESS_SPEC);
1219 RECORD(DECL_FRIEND);
1220 RECORD(DECL_FRIEND_TEMPLATE);
1221 RECORD(DECL_CLASS_TEMPLATE);
1222 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1223 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001224 RECORD(DECL_VAR_TEMPLATE);
1225 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1226 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001227 RECORD(DECL_FUNCTION_TEMPLATE);
1228 RECORD(DECL_TEMPLATE_TYPE_PARM);
1229 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1230 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001231 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001232 RECORD(DECL_STATIC_ASSERT);
1233 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001234 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001235 RECORD(DECL_INDIRECTFIELD);
1236 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001237 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1238 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1239 RECORD(DECL_IMPORT);
1240 RECORD(DECL_OMP_THREADPRIVATE);
1241 RECORD(DECL_EMPTY);
1242 RECORD(DECL_OBJC_TYPE_PARAM);
1243 RECORD(DECL_OMP_CAPTUREDEXPR);
1244 RECORD(DECL_PRAGMA_COMMENT);
1245 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1246 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001247
Douglas Gregor03412ba2011-06-03 02:27:19 +00001248 // Statements and Exprs can occur in the Decls and Types block.
1249 AddStmtsExprs(Stream, Record);
1250
Douglas Gregor92a96f52011-02-08 21:58:10 +00001251 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001252 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001253 RECORD(PPD_MACRO_DEFINITION);
1254 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001255
1256 // Decls and Types block.
1257 BLOCK(EXTENSION_BLOCK);
1258 RECORD(EXTENSION_METADATA);
1259
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001260 BLOCK(UNHASHED_CONTROL_BLOCK);
1261 RECORD(SIGNATURE);
1262 RECORD(DIAGNOSTIC_OPTIONS);
1263 RECORD(DIAG_PRAGMA_MAPPINGS);
1264
Chris Lattner28fa4e62009-04-26 22:26:21 +00001265#undef RECORD
1266#undef BLOCK
1267 Stream.ExitBlock();
1268}
1269
Richard Smith54cc3c22014-12-11 20:50:24 +00001270/// \brief Prepares a path for being written to an AST file by converting it
1271/// to an absolute path and removing nested './'s.
1272///
1273/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001274static bool cleanPathForOutput(FileManager &FileMgr,
1275 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001276 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001277 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001278}
1279
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001280/// \brief Adjusts the given filename to only write out the portion of the
1281/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001282///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001283/// \param Filename the file name to adjust.
1284///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001285/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1286/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001287///
1288/// \returns either the original filename (if it needs no adjustment) or the
1289/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001290static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001291adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001292 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001293
Richard Smith7ed1bc92014-12-05 22:42:13 +00001294 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001295 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001296
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001297 // Verify that the filename and the system root have the same prefix.
1298 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001299 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1300 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001301 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001302
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001303 // We hit the end of the filename before we hit the end of the system root.
1304 if (!Filename[Pos])
1305 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001306
Richard Smith7ed1bc92014-12-05 22:42:13 +00001307 // If there's not a path separator at the end of the base directory nor
1308 // immediately after it, then this isn't within the base directory.
1309 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1310 if (!llvm::sys::path::is_separator(BaseDir.back()))
1311 return Filename;
1312 } else {
1313 // If the file name has a '/' at the current position, skip over the '/'.
1314 // We distinguish relative paths from absolute paths by the
1315 // absence of '/' at the beginning of relative paths.
1316 //
1317 // FIXME: This is wrong. We distinguish them by asking if the path is
1318 // absolute, which isn't the same thing. And there might be multiple '/'s
1319 // in a row. Use a better mechanism to indicate whether we have emitted an
1320 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001321 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001322 }
Mike Stump11289f42009-09-09 15:08:12 +00001323
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001324 return Filename + Pos;
1325}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001326
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001327ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1328 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1329 llvm::SHA1 Hasher;
1330 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1331 auto Hash = Hasher.result();
1332
1333 // Convert to an array [5*i32].
1334 ASTFileSignature Signature;
1335 auto LShift = [&](unsigned char Val, unsigned Shift) {
1336 return (uint32_t)Val << Shift;
1337 };
1338 for (int I = 0; I != 5; ++I)
1339 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1340 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1341
1342 return Signature;
1343}
1344
1345ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1346 ASTContext &Context) {
1347 // Flush first to prepare the PCM hash (signature).
1348 Stream.FlushToWord();
1349 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1350
1351 // Enter the block and prepare to write records.
1352 RecordData Record;
1353 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1354
1355 // For implicit modules, write the hash of the PCM as its signature.
1356 ASTFileSignature Signature;
1357 if (WritingModule &&
1358 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1359 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1360 Record.append(Signature.begin(), Signature.end());
1361 Stream.EmitRecord(SIGNATURE, Record);
1362 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001363 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001364
1365 // Diagnostic options.
1366 const auto &Diags = Context.getDiagnostics();
1367 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1368#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1369#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1370 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1371#include "clang/Basic/DiagnosticOptions.def"
1372 Record.push_back(DiagOpts.Warnings.size());
1373 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1374 AddString(DiagOpts.Warnings[I], Record);
1375 Record.push_back(DiagOpts.Remarks.size());
1376 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1377 AddString(DiagOpts.Remarks[I], Record);
1378 // Note: we don't serialize the log or serialization file names, because they
1379 // are generally transient files and will almost always be overridden.
1380 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1381
1382 // Write out the diagnostic/pragma mappings.
1383 WritePragmaDiagnosticMappings(Diags, /* IsModule = */ WritingModule);
1384
1385 // Leave the options block.
1386 Stream.ExitBlock();
1387 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001388}
1389
Douglas Gregor112b9072012-10-18 05:31:06 +00001390/// \brief Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001391void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1392 StringRef isysroot,
1393 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001394 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001395 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001396 RecordData Record;
1397
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001398 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001399 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001400 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1401 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1402 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1403 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1404 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1405 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001406 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001407 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1408 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001409 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001410 assert((!WritingModule || isysroot.empty()) &&
1411 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001412 {
1413 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1414 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1415 !isysroot.empty(), IncludeTimestamps,
1416 ASTHasCompilerErrors};
1417 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1418 getClangFullRepositoryVersion());
1419 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001420 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001421
Richard Smith7ed1bc92014-12-05 22:42:13 +00001422 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001423 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001424 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001426 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001427 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001428 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1429 }
1430
Richard Smith7ed1bc92014-12-05 22:42:13 +00001431 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001432 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001433 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001434
1435 // If the home of the module is the current working directory, then we
1436 // want to pick up the cwd of the build process loading the module, not
1437 // our cwd, when we load this module.
1438 if (!PP.getHeaderSearchInfo()
1439 .getHeaderSearchOpts()
1440 .ModuleMapFileHomeIsCwd ||
1441 WritingModule->Directory->getName() != StringRef(".")) {
1442 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001443 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001444 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001446 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001447
Mehdi Amini57a41912015-09-10 01:46:39 +00001448 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001449 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1450 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001451
1452 // Write out all other paths relative to the base directory if possible.
1453 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1454 } else if (!isysroot.empty()) {
1455 // Write out paths relative to the sysroot if possible.
1456 BaseDirectory = isysroot;
1457 }
1458
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001459 // Module map file
1460 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001461 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001462
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001463 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1464
1465 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001466 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001467
1468 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001469 if (auto *AdditionalModMaps =
1470 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001471 Record.push_back(AdditionalModMaps->size());
1472 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001473 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001474 } else {
1475 Record.push_back(0);
1476 }
1477
1478 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001479 }
1480
Douglas Gregor112b9072012-10-18 05:31:06 +00001481 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001482 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001483 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001484 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001485
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001486 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001487 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001488 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001489 continue;
1490
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001491 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1492 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001493
1494 // If we have calculated signature, there is no need to store
1495 // the size or timestamp.
1496 Record.push_back(M.Signature ? 0 : M.File->getSize());
1497 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1498
1499 for (auto I : M.Signature)
1500 Record.push_back(I);
1501
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001502 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001503 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001504 Stream.EmitRecord(IMPORTS, Record);
1505 }
Mike Stump11289f42009-09-09 15:08:12 +00001506
Richard Smith0516b182015-09-08 19:40:14 +00001507 // Write the options block.
1508 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1509
Douglas Gregor112b9072012-10-18 05:31:06 +00001510 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001511 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001512 const LangOptions &LangOpts = Context.getLangOpts();
1513#define LANGOPT(Name, Bits, Default, Description) \
1514 Record.push_back(LangOpts.Name);
1515#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1516 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001517#include "clang/Basic/LangOptions.def"
1518#define SANITIZER(NAME, ID) \
1519 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001520#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001521
Ben Langmuircd98cb72015-06-23 18:20:18 +00001522 Record.push_back(LangOpts.ModuleFeatures.size());
1523 for (StringRef Feature : LangOpts.ModuleFeatures)
1524 AddString(Feature, Record);
1525
Douglas Gregor112b9072012-10-18 05:31:06 +00001526 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1527 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001528
1529 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001530
1531 // Comment options.
1532 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001533 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1534 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001535 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001536 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001537
Samuel Antaoee8fb302016-01-06 13:42:12 +00001538 // OpenMP offloading options.
1539 Record.push_back(LangOpts.OMPTargetTriples.size());
1540 for (auto &T : LangOpts.OMPTargetTriples)
1541 AddString(T.getTriple(), Record);
1542
1543 AddString(LangOpts.OMPHostIRFile, Record);
1544
Douglas Gregor112b9072012-10-18 05:31:06 +00001545 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1546
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001547 // Target options.
1548 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001549 const TargetInfo &Target = Context.getTargetInfo();
1550 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001551 AddString(TargetOpts.Triple, Record);
1552 AddString(TargetOpts.CPU, Record);
1553 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001554 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1555 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1556 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1557 }
1558 Record.push_back(TargetOpts.Features.size());
1559 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1560 AddString(TargetOpts.Features[I], Record);
1561 }
1562 Stream.EmitRecord(TARGET_OPTIONS, Record);
1563
Douglas Gregorc6317db2012-10-24 15:49:58 +00001564 // File system options.
1565 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001566 const FileSystemOptions &FSOpts =
1567 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001568 AddString(FSOpts.WorkingDir, Record);
1569 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1570
Douglas Gregor2d302362012-10-24 16:50:34 +00001571 // Header search options.
1572 Record.clear();
1573 const HeaderSearchOptions &HSOpts
1574 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1575 AddString(HSOpts.Sysroot, Record);
1576
1577 // Include entries.
1578 Record.push_back(HSOpts.UserEntries.size());
1579 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1580 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1581 AddString(Entry.Path, Record);
1582 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001583 Record.push_back(Entry.IsFramework);
1584 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001585 }
1586
1587 // System header prefixes.
1588 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1589 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1590 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1591 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1592 }
1593
1594 AddString(HSOpts.ResourceDir, Record);
1595 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001596 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001597 Record.push_back(HSOpts.DisableModuleHash);
1598 Record.push_back(HSOpts.UseBuiltinIncludes);
1599 Record.push_back(HSOpts.UseStandardSystemIncludes);
1600 Record.push_back(HSOpts.UseStandardCXXIncludes);
1601 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001602 // Write out the specific module cache path that contains the module files.
1603 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001604 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1605
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001606 // Preprocessor options.
1607 Record.clear();
1608 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1609
1610 // Macro definitions.
1611 Record.push_back(PPOpts.Macros.size());
1612 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1613 AddString(PPOpts.Macros[I].first, Record);
1614 Record.push_back(PPOpts.Macros[I].second);
1615 }
1616
1617 // Includes
1618 Record.push_back(PPOpts.Includes.size());
1619 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1620 AddString(PPOpts.Includes[I], Record);
1621
1622 // Macro includes
1623 Record.push_back(PPOpts.MacroIncludes.size());
1624 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1625 AddString(PPOpts.MacroIncludes[I], Record);
1626
Douglas Gregorb6368752012-10-24 23:41:50 +00001627 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001628 // Detailed record is important since it is used for the module cache hash.
1629 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001630 AddString(PPOpts.ImplicitPCHInclude, Record);
1631 AddString(PPOpts.ImplicitPTHInclude, Record);
1632 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1633 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1634
Richard Smith0516b182015-09-08 19:40:14 +00001635 // Leave the options block.
1636 Stream.ExitBlock();
1637
Douglas Gregora3b20262011-05-06 21:43:30 +00001638 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001639 SourceManager &SM = Context.getSourceManager();
1640 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001641 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001642 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1643 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001644 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001645 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001646
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001647 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001648 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001649 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001650 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001651 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001652
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001653 Record.clear();
1654 Record.push_back(SM.getMainFileID().getOpaqueValue());
1655 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1656
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001657 // Original PCH directory
1658 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001659 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001660 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1661 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001662 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001663
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001664 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001665
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001666 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001667 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1668
Mehdi Amini57a41912015-09-10 01:46:39 +00001669 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001670 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1671 }
1672
Douglas Gregor49491f72013-03-15 22:15:07 +00001673 WriteInputFiles(Context.SourceMgr,
1674 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001675 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001676 Stream.ExitBlock();
1677}
1678
Douglas Gregor49491f72013-03-15 22:15:07 +00001679namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001680
Douglas Gregor49491f72013-03-15 22:15:07 +00001681 /// \brief An input file.
1682 struct InputFileEntry {
1683 const FileEntry *File;
1684 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001685 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001686 bool BufferOverridden;
1687 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001688
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001689} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001690
1691void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1692 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001693 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001694 using namespace llvm;
1695 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001696
Douglas Gregor72be3902012-10-19 00:38:02 +00001697 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001698 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001699 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001700 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001701 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1702 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001703 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001704 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001705 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001706 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001707
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001708 // Get all ContentCache objects for files, sorted by whether the file is a
1709 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001710 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001711 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1712 // Get this source location entry.
1713 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001714 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001715
1716 // We only care about file entries that were not overridden.
1717 if (!SLoc->isFile())
1718 continue;
1719 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001720 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001721 continue;
1722
Douglas Gregor49491f72013-03-15 22:15:07 +00001723 InputFileEntry Entry;
1724 Entry.File = Cache->OrigEntry;
1725 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001726 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001727 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001728 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001729 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001730 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001731 SortedFiles.push_front(Entry);
1732 }
1733
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001734 unsigned UserFilesNum = 0;
1735 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001736 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001737 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001738 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001739 if (InputFileID != 0)
1740 continue; // already recorded this file.
1741
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001742 // Record this entry's offset.
1743 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001744
1745 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001746
Douglas Gregor49491f72013-03-15 22:15:07 +00001747 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001748 ++UserFilesNum;
1749
Douglas Gregor72be3902012-10-19 00:38:02 +00001750 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001751 // And whether this file was overridden.
1752 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001753 INPUT_FILE,
1754 InputFileOffsets.size(),
1755 (uint64_t)Entry.File->getSize(),
1756 (uint64_t)getTimestampForOutput(Entry.File),
1757 Entry.BufferOverridden,
1758 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001759
Richard Smith7ed1bc92014-12-05 22:42:13 +00001760 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1761 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001762
Douglas Gregor112b9072012-10-18 05:31:06 +00001763 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001764
1765 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001766 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001767 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1768 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001769 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1770 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001771 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001772 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001773
1774 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001775 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1776 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001777 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001778}
1779
Douglas Gregora7f71a92009-04-10 03:52:48 +00001780//===----------------------------------------------------------------------===//
1781// Source Manager Serialization
1782//===----------------------------------------------------------------------===//
1783
1784/// \brief Create an abbreviation for the SLocEntry that refers to a
1785/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001786static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001787 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001788
David Blaikieb44f0bf2017-01-04 22:36:43 +00001789 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001790 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001795 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001800 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001801}
1802
1803/// \brief Create an abbreviation for the SLocEntry that refers to a
1804/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001805static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001806 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001807
David Blaikieb44f0bf2017-01-04 22:36:43 +00001808 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001809 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001815 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001816}
1817
1818/// \brief Create an abbreviation for the SLocEntry that refers to a
1819/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001820static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1821 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001822 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001823
David Blaikieb44f0bf2017-01-04 22:36:43 +00001824 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001825 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1826 : SM_SLOC_BUFFER_BLOB));
1827 if (Compressed)
1828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001830 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001831}
1832
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001833/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1834/// expansion.
1835static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001836 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001837
David Blaikieb44f0bf2017-01-04 22:36:43 +00001838 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001839 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001845 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001846}
1847
Douglas Gregor09b69892011-02-10 17:09:37 +00001848namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001849
Douglas Gregor09b69892011-02-10 17:09:37 +00001850 // Trait used for the on-disk hash table of header search information.
1851 class HeaderFileInfoTrait {
1852 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001853 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001854
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001855 // Keep track of the framework names we've used during serialization.
1856 SmallVector<char, 128> FrameworkStringData;
1857 llvm::StringMap<unsigned> FrameworkNameOffset;
1858
Douglas Gregor09b69892011-02-10 17:09:37 +00001859 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001860 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1861 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001862
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001863 struct key_type {
1864 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001865 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001866 };
1867 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001868
1869 typedef HeaderFileInfo data_type;
1870 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001871 typedef unsigned hash_value_type;
1872 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001873
Richard Smithe75ee0f2015-08-17 07:13:32 +00001874 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001875 // The hash is based only on size/time of the file, so that the reader can
1876 // match even when symlinking or excess path elements ("foo/../", "../")
1877 // change the form of the name. However, complete path is still the key.
1878 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001879 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001880 }
1881
1882 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001883 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001884 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001885 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001886 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001887 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001888 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001889 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1890 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1891 DataLen += 4;
1892 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001893 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001894 }
1895
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001896 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001897 using namespace llvm::support;
1898 endian::Writer<little> LE(Out);
1899 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001900 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001901 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001902 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001903 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001904 }
1905
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001906 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001907 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001908 using namespace llvm::support;
1909 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001910 uint64_t Start = Out.tell(); (void)Start;
1911
Richard Smith386bb072015-08-18 23:42:23 +00001912 unsigned char Flags = (Data.isImport << 4)
1913 | (Data.isPragmaOnce << 3)
1914 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001915 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001916 LE.write<uint8_t>(Flags);
1917 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001918
1919 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001920 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001921 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001922 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001923
1924 unsigned Offset = 0;
1925 if (!Data.Framework.empty()) {
1926 // If this header refers into a framework, save the framework name.
1927 llvm::StringMap<unsigned>::iterator Pos
1928 = FrameworkNameOffset.find(Data.Framework);
1929 if (Pos == FrameworkNameOffset.end()) {
1930 Offset = FrameworkStringData.size() + 1;
1931 FrameworkStringData.append(Data.Framework.begin(),
1932 Data.Framework.end());
1933 FrameworkStringData.push_back(0);
1934
1935 FrameworkNameOffset[Data.Framework] = Offset;
1936 } else
1937 Offset = Pos->second;
1938 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001939 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001940
Richard Smith386bb072015-08-18 23:42:23 +00001941 // FIXME: If the header is excluded, we should write out some
1942 // record of that fact.
1943 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1944 if (uint32_t ModID =
1945 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1946 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1947 assert((Value >> 2) == ModID && "overflow in header module info");
1948 LE.write<uint32_t>(Value);
1949 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001950 }
1951
Douglas Gregor09b69892011-02-10 17:09:37 +00001952 assert(Out.tell() - Start == DataLen && "Wrong data length");
1953 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001954
1955 const char *strings_begin() const { return FrameworkStringData.begin(); }
1956 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001957 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001958
Douglas Gregor09b69892011-02-10 17:09:37 +00001959} // end anonymous namespace
1960
1961/// \brief Write the header search block for the list of files that
1962///
1963/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001964void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001965 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001966 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1967
1968 if (FilesByUID.size() > HS.header_file_size())
1969 FilesByUID.resize(HS.header_file_size());
1970
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001971 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001972 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001973 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001974 unsigned NumHeaderSearchEntries = 0;
1975 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1976 const FileEntry *File = FilesByUID[UID];
1977 if (!File)
1978 continue;
1979
Richard Smith386bb072015-08-18 23:42:23 +00001980 // Get the file info. This will load info from the external source if
1981 // necessary. Skip emitting this file if we have no information on it
1982 // as a header file (in which case HFI will be null) or if it hasn't
1983 // changed since it was loaded. Also skip it if it's for a modular header
1984 // from a different module; in that case, we rely on the module(s)
1985 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001986 const HeaderFileInfo *HFI =
1987 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1988 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001989 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001990
Richard Smith7ed1bc92014-12-05 22:42:13 +00001991 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001992 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001993 SmallString<128> FilenameTmp(Filename);
1994 if (PreparePathForOutput(FilenameTmp)) {
1995 // If we performed any translation on the file name at all, we need to
1996 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001997 Filename = StringRef(strdup(FilenameTmp.c_str()));
1998 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001999 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002000
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002001 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00002002 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002003 ++NumHeaderSearchEntries;
2004 }
2005
2006 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002007 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002008 uint32_t BucketOffset;
2009 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002010 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00002011 llvm::raw_svector_ostream Out(TableData);
2012 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002013 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00002014 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2015 }
2016
2017 // Create a blob abbreviation
2018 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002019
David Blaikieb44f0bf2017-01-04 22:36:43 +00002020 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002021 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002026 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00002027
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002028 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002029 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2030 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002031 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002032 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00002033
2034 // Free all of the strings we had to duplicate.
2035 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002036 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002037}
2038
George Rimarc39f5492017-01-17 15:45:31 +00002039static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2040 unsigned SLocBufferBlobCompressedAbbrv,
2041 unsigned SLocBufferBlobAbbrv) {
2042 typedef ASTWriter::RecordData::value_type RecordDataType;
2043
2044 // Compress the buffer if possible. We expect that almost all PCM
2045 // consumers will not want its contents.
2046 SmallString<0> CompressedBuffer;
2047 if (llvm::zlib::isAvailable()) {
2048 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2049 if (!E) {
2050 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2051 Blob.size() - 1};
2052 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2053 CompressedBuffer);
2054 return;
2055 }
2056 llvm::consumeError(std::move(E));
2057 }
2058
2059 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2060 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2061}
2062
Douglas Gregora7f71a92009-04-10 03:52:48 +00002063/// \brief Writes the block containing the serialized form of the
2064/// source manager.
2065///
2066/// TODO: We should probably use an on-disk hash table (stored in a
2067/// blob), indexed based on the file name, so that we only create
2068/// entries for files that we actually need. In the common case (no
2069/// errors), we probably won't have to create file entries for any of
2070/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002071void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002072 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002073 RecordData Record;
2074
Chris Lattner0910e3b2009-04-10 17:16:57 +00002075 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002076 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002077
2078 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002079 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2080 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002081 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2082 unsigned SLocBufferBlobCompressedAbbrv =
2083 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002084 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002085
Douglas Gregor258ae542009-04-27 06:38:32 +00002086 // Write out the source location entry table. We skip the first
2087 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002088 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002089 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002090 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2091 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002092 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002093 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002094 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002095 FileID FID = FileID::get(I);
2096 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002097
Douglas Gregor258ae542009-04-27 06:38:32 +00002098 // Record the offset of this source-location entry.
2099 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2100
2101 // Figure out which record code to use.
2102 unsigned Code;
2103 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002104 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2105 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002106 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002107 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002108 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002109 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002110 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002111 Record.clear();
2112 Record.push_back(Code);
2113
Douglas Gregor925296b2011-07-19 16:10:42 +00002114 // Starting offset of this entry within this module, so skip the dummy.
2115 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002116 if (SLoc->isFile()) {
2117 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002118 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002119 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2120 Record.push_back(File.hasLineDirectives());
2121
2122 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002123 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002124 if (Content->OrigEntry) {
2125 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002126 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002127
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002128 // The source location entry is a file. Emit input file ID.
2129 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2130 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002131
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002132 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002133
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002134 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002135 if (FDI != FileDeclIDs.end()) {
2136 Record.push_back(FDI->second->FirstDeclIndex);
2137 Record.push_back(FDI->second->DeclIDs.size());
2138 } else {
2139 Record.push_back(0);
2140 Record.push_back(0);
2141 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002142
2143 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2144
Richard Smithaada85c2016-02-06 02:06:43 +00002145 if (Content->BufferOverridden || Content->IsTransient)
2146 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002147 } else {
2148 // The source location entry is a buffer. The blob associated
2149 // with this entry contains the contents of the buffer.
2150
2151 // We add one to the size so that we capture the trailing NULL
2152 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2153 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002154 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002155 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002156 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002157 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002158 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002159 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002160
Mehdi Amini99d1b292016-10-01 16:38:28 +00002161 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002162 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002163 }
Richard Smithaada85c2016-02-06 02:06:43 +00002164
2165 if (EmitBlob) {
2166 // Include the implicit terminating null character in the on-disk buffer
2167 // if we're writing it uncompressed.
2168 const llvm::MemoryBuffer *Buffer =
2169 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2170 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002171 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2172 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002173 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002174 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002175 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002176 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002177 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2178 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2179 AddSourceLocation(Expansion.isMacroArgExpansion()
2180 ? SourceLocation()
2181 : Expansion.getExpansionLocEnd(),
2182 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002183
2184 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002185 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002186 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002187 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002188 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002189 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002190 }
2191 }
2192
Douglas Gregor8f45df52009-04-16 22:23:12 +00002193 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002194
2195 if (SLocEntryOffsets.empty())
2196 return;
2197
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002198 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002199 // table is used for lazily loading source-location information.
2200 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002201
David Blaikieb44f0bf2017-01-04 22:36:43 +00002202 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002203 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002207 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002208 {
2209 RecordData::value_type Record[] = {
2210 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2211 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2212 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2213 bytes(SLocEntryOffsets));
2214 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002215 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002216 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002217 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002218
2219 // Write the line table. It depends on remapping working, so it must come
2220 // after the source location offsets.
2221 if (SourceMgr.hasLineTable()) {
2222 LineTableInfo &LineTable = SourceMgr.getLineTable();
2223
2224 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002225
2226 // Emit the needed file names.
2227 llvm::DenseMap<int, int> FilenameMap;
2228 for (const auto &L : LineTable) {
2229 if (L.first.ID < 0)
2230 continue;
2231 for (auto &LE : L.second) {
2232 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2233 FilenameMap.size())).second)
2234 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2235 }
2236 }
2237 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002238
2239 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002240 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002241 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002242 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002243 continue;
2244
2245 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002246 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002247
2248 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002249 Record.push_back(L.second.size());
2250 for (const auto &LE : L.second) {
2251 Record.push_back(LE.FileOffset);
2252 Record.push_back(LE.LineNo);
2253 Record.push_back(FilenameMap[LE.FilenameID]);
2254 Record.push_back((unsigned)LE.FileKind);
2255 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002256 }
2257 }
Richard Smith63078492015-09-01 07:41:55 +00002258
Douglas Gregor925296b2011-07-19 16:10:42 +00002259 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2260 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002261}
2262
Douglas Gregorc5046832009-04-27 18:38:38 +00002263//===----------------------------------------------------------------------===//
2264// Preprocessor Serialization
2265//===----------------------------------------------------------------------===//
2266
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002267static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2268 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002269 if (MacroInfo *MI = MD->getMacroInfo())
2270 if (MI->isBuiltinMacro())
2271 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002272
2273 if (IsModule) {
2274 SourceLocation Loc = MD->getLocation();
2275 if (Loc.isInvalid())
2276 return true;
2277 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2278 return true;
2279 }
2280
2281 return false;
2282}
2283
Chris Lattnereeffaef2009-04-10 17:15:23 +00002284/// \brief Writes the block containing the serialized form of the
2285/// preprocessor.
2286///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002287void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002288 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2289 if (PPRec)
2290 WritePreprocessorDetail(*PPRec);
2291
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002292 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002293 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002294
Chris Lattner0af3ba12009-04-13 01:29:17 +00002295 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2296 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002297 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002298 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002299 }
2300
2301 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002302 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002303
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002304 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002305 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002306 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002307 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002308
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002309 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002310 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002311
Richard Smithee977932015-05-01 21:22:17 +00002312 // Construct the list of identifiers with macro directives that need to be
2313 // serialized.
2314 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2315 for (auto &Id : PP.getIdentifierTable())
2316 if (Id.second->hadMacroDefinition() &&
2317 (!Id.second->isFromAST() ||
2318 Id.second->hasChangedSinceDeserialization()))
2319 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002320 // Sort the set of macro definitions that need to be serialized by the
2321 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002322 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2323 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002324
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002325 // Emit the macro directives as a list and associate the offset with the
2326 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002327 for (const IdentifierInfo *Name : MacroIdentifiers) {
2328 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002329 auto StartOffset = Stream.GetCurrentBitNo();
2330
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002331 // Emit the macro directives in reverse source order.
2332 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002333 // Once we hit an ignored macro, we're done: the rest of the chain
2334 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002335 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002336 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002337
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002338 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002339 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002340 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002341 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002342 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002343 Record.push_back(VisMD->isPublic());
2344 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002345 }
Richard Smithd7329392015-04-21 21:46:32 +00002346
Richard Smithb8b2ed62015-04-23 18:18:26 +00002347 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002348 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002349 // We write out exported module macros for PCH as well.
2350 auto Leafs = PP.getLeafModuleMacros(Name);
2351 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2352 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2353 while (!Worklist.empty()) {
2354 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002355
Manman Ren33d80292016-05-31 18:19:32 +00002356 // Emit a record indicating this submodule exports this macro.
2357 ModuleMacroRecord.push_back(
2358 getSubmoduleID(Macro->getOwningModule()));
2359 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2360 for (auto *M : Macro->overrides())
2361 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002362
Manman Ren33d80292016-05-31 18:19:32 +00002363 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2364 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002365
Manman Ren33d80292016-05-31 18:19:32 +00002366 // Enqueue overridden macros once we've visited all their ancestors.
2367 for (auto *M : Macro->overrides())
2368 if (++Visits[M] == M->getNumOverridingMacros())
2369 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002370
Manman Ren33d80292016-05-31 18:19:32 +00002371 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002372 }
Richard Smithd7329392015-04-21 21:46:32 +00002373
Richard Smithee977932015-05-01 21:22:17 +00002374 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002375 continue;
2376
Richard Smithd7329392015-04-21 21:46:32 +00002377 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002378 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2379 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002380 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002381
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002382 /// \brief Offsets of each of the macros into the bitstream, indexed by
2383 /// the local macro ID
2384 ///
2385 /// For each identifier that is associated with a macro, this map
2386 /// provides the offset into the bitstream where that macro is
2387 /// defined.
2388 std::vector<uint32_t> MacroOffsets;
2389
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002390 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2391 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2392 MacroInfo *MI = MacroInfosToEmit[I].MI;
2393 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002394
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002395 if (ID < FirstMacroID) {
2396 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2397 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002398 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002399
2400 // Record the local offset of this macro.
2401 unsigned Index = ID - FirstMacroID;
2402 if (Index == MacroOffsets.size())
2403 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2404 else {
2405 if (Index > MacroOffsets.size())
2406 MacroOffsets.resize(Index + 1);
2407
2408 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2409 }
2410
2411 AddIdentifierRef(Name, Record);
2412 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2413 AddSourceLocation(MI->getDefinitionLoc(), Record);
2414 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2415 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002416 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002417 unsigned Code;
2418 if (MI->isObjectLike()) {
2419 Code = PP_MACRO_OBJECT_LIKE;
2420 } else {
2421 Code = PP_MACRO_FUNCTION_LIKE;
2422
2423 Record.push_back(MI->isC99Varargs());
2424 Record.push_back(MI->isGNUVarargs());
2425 Record.push_back(MI->hasCommaPasting());
2426 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002427 for (const IdentifierInfo *Arg : MI->args())
2428 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002429 }
2430
2431 // If we have a detailed preprocessing record, record the macro definition
2432 // ID that corresponds to this macro.
2433 if (PPRec)
2434 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2435
2436 Stream.EmitRecord(Code, Record);
2437 Record.clear();
2438
2439 // Emit the tokens array.
2440 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2441 // Note that we know that the preprocessor does not have any annotation
2442 // tokens in it because they are created by the parser, and thus can't
2443 // be in a macro definition.
2444 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002445 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002446 Stream.EmitRecord(PP_TOKEN, Record);
2447 Record.clear();
2448 }
2449 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002450 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002451
Douglas Gregor92a96f52011-02-08 21:58:10 +00002452 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002453
2454 // Write the offsets table for macro IDs.
2455 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002456
David Blaikieb44f0bf2017-01-04 22:36:43 +00002457 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002458 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2462
David Blaikieb44f0bf2017-01-04 22:36:43 +00002463 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002464 {
2465 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2466 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2467 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2468 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002469}
2470
2471void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002472 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002473 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002474
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002475 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002476
Douglas Gregor92a96f52011-02-08 21:58:10 +00002477 // Enter the preprocessor block.
2478 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002479
Douglas Gregoraae92242010-03-19 21:51:54 +00002480 // If the preprocessor has a preprocessing record, emit it.
2481 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002482 using namespace llvm;
2483
2484 // Set up the abbreviation for
2485 unsigned InclusionAbbrev = 0;
2486 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002487 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002488 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002489 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2491 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002493 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002494 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002495 }
2496
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002497 unsigned FirstPreprocessorEntityID
2498 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2499 + NUM_PREDEF_PP_ENTITY_IDS;
2500 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002501 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002502 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2503 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002504 E != EEnd;
2505 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002506 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002507
Richard Smith66a81862015-05-04 02:25:31 +00002508 PreprocessedEntityOffsets.push_back(
2509 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002510
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002511 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002512 // Record this macro definition's ID.
2513 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002514
Douglas Gregor92a96f52011-02-08 21:58:10 +00002515 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002516 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2517 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002518 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002519
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002520 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002521 Record.push_back(ME->isBuiltinMacro());
2522 if (ME->isBuiltinMacro())
2523 AddIdentifierRef(ME->getName(), Record);
2524 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002525 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002526 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002527 continue;
2528 }
2529
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002530 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002531 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002532 Record.push_back(ID->getFileName().size());
2533 Record.push_back(ID->wasInQuotes());
2534 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002535 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002536 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002537 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002538 // Check that the FileEntry is not null because it was not resolved and
2539 // we create a PCH even with compiler errors.
2540 if (ID->getFile())
2541 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002542 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002543 continue;
2544 }
2545
2546 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2547 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002548 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002549
Douglas Gregoraae92242010-03-19 21:51:54 +00002550 // Write the offsets table for the preprocessing record.
2551 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002552 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2553
Douglas Gregoraae92242010-03-19 21:51:54 +00002554 // Write the offsets table for identifier IDs.
2555 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002556
David Blaikieb44f0bf2017-01-04 22:36:43 +00002557 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002558 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002561 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002562
Mehdi Amini57a41912015-09-10 01:46:39 +00002563 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2564 FirstPreprocessorEntityID -
2565 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002566 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002567 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002568 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002569}
2570
Richard Smith386bb072015-08-18 23:42:23 +00002571unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002572 if (!Mod)
2573 return 0;
2574
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002575 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2576 if (Known != SubmoduleIDs.end())
2577 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002578
Richard Smithdc1f0422016-07-20 19:10:16 +00002579 auto *Top = Mod->getTopLevelModule();
2580 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002581 (getLangOpts().CompilingPCH ||
2582 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002583 return 0;
2584
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002585 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2586}
2587
Richard Smith386bb072015-08-18 23:42:23 +00002588unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2589 // FIXME: This can easily happen, if we have a reference to a submodule that
2590 // did not result in us loading a module file for that submodule. For
2591 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2592 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2593 assert((ID || !Mod) &&
2594 "asked for module ID for non-local, non-imported module");
2595 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002596}
2597
Douglas Gregor253eefe2011-12-01 00:59:36 +00002598/// \brief Compute the number of modules within the given tree (including the
2599/// given module).
2600static unsigned getNumberOfModules(Module *Mod) {
2601 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002602 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002603 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002604 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002605
2606 return ChildModules + 1;
2607}
2608
Douglas Gregorde3ef502011-11-30 23:21:26 +00002609void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002610 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002611 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002612
2613 // Write the abbreviations needed for the submodules block.
2614 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002615
David Blaikieb44f0bf2017-01-04 22:36:43 +00002616 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002617 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
David Blaikie4d923012017-01-31 21:28:19 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // WithCodegen
Douglas Gregor69021972011-11-30 17:33:56 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002630 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002631
David Blaikieb44f0bf2017-01-04 22:36:43 +00002632 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002633 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002635 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002636
David Blaikieb44f0bf2017-01-04 22:36:43 +00002637 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002638 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002640 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002641
David Blaikieb44f0bf2017-01-04 22:36:43 +00002642 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002643 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002645 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002646
David Blaikieb44f0bf2017-01-04 22:36:43 +00002647 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002648 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2649 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002650 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002651
David Blaikieb44f0bf2017-01-04 22:36:43 +00002652 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002653 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002656 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002657
David Blaikieb44f0bf2017-01-04 22:36:43 +00002658 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002659 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2660 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002661 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002662
David Blaikieb44f0bf2017-01-04 22:36:43 +00002663 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002664 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002666 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002667
David Blaikieb44f0bf2017-01-04 22:36:43 +00002668 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002669 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002671 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002672
David Blaikieb44f0bf2017-01-04 22:36:43 +00002673 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002674 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002676 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002677
David Blaikieb44f0bf2017-01-04 22:36:43 +00002678 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002679 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002682 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002683
David Blaikieb44f0bf2017-01-04 22:36:43 +00002684 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002685 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002687 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002688
David Blaikieb44f0bf2017-01-04 22:36:43 +00002689 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002690 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2692 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002693 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002694
Douglas Gregor253eefe2011-12-01 00:59:36 +00002695 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002696 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2697 FirstSubmoduleID -
2698 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002699 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2700
Douglas Gregor69021972011-11-30 17:33:56 +00002701 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002702 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002703 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002704 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002705 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002706 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002707 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002708
2709 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002710 if (Mod->Parent) {
2711 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002712 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002713 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002714
2715 // Emit the definition of the block.
2716 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002717 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2718 ID,
2719 ParentID,
2720 Mod->IsFramework,
2721 Mod->IsExplicit,
2722 Mod->IsSystem,
2723 Mod->IsExternC,
2724 Mod->InferSubmodules,
2725 Mod->InferExplicitSubmodules,
2726 Mod->InferExportWildcard,
2727 Mod->ConfigMacrosExhaustive,
David Blaikie4d923012017-01-31 21:28:19 +00002728 Context->getLangOpts().ModularCodegen && WritingModule};
Mehdi Amini57a41912015-09-10 01:46:39 +00002729 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2730 }
2731
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002732 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002733 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002734 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002735 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002736 }
2737
Douglas Gregor69021972011-11-30 17:33:56 +00002738 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002739 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002740 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002741 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2742 UmbrellaHeader.NameAsWritten);
2743 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002744 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2745 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002746 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002747 }
Richard Smith306d8922014-10-22 23:50:56 +00002748
Douglas Gregor69021972011-11-30 17:33:56 +00002749 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002750 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002751 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002752 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002753 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002754 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002755 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2756 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2757 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002758 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002759 Module::HK_PrivateTextual},
2760 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002761 };
2762 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002763 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002764 for (auto &H : Mod->Headers[HL.HeaderKind])
2765 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2766 }
2767
2768 // Emit the top headers.
2769 {
2770 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002771 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002772 for (auto *H : TopHeaders)
2773 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002774 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002775
2776 // Emit the imports.
2777 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002778 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002779 for (auto *I : Mod->Imports)
2780 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002781 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2782 }
2783
Douglas Gregor24bb9232011-12-02 18:58:38 +00002784 // Emit the exports.
2785 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002786 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002787 for (const auto &E : Mod->Exports) {
2788 // FIXME: This may fail; we don't require that all exported modules
2789 // are local or imported.
2790 Record.push_back(getSubmoduleID(E.getPointer()));
2791 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002792 }
2793 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2794 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002795
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002796 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2797 // Might be unnecessary as use declarations are only used to build the
2798 // module itself.
2799
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002800 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002801 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002802 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2803 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002804 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002805 }
2806
Douglas Gregorfb912652013-03-20 21:10:35 +00002807 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002808 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002809 // FIXME: This may fail; we don't require that all conflicting modules
2810 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002811 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2812 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002813 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002814 }
2815
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002816 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002817 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002818 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002819 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002820 }
2821
Richard Smithdc1f0422016-07-20 19:10:16 +00002822 // Emit the initializers, if any.
2823 RecordData Inits;
2824 for (Decl *D : Context->getModuleInitializers(Mod))
2825 Inits.push_back(GetDeclRef(D));
2826 if (!Inits.empty())
2827 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2828
Douglas Gregor69021972011-11-30 17:33:56 +00002829 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002830 for (auto *M : Mod->submodules())
2831 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002832 }
2833
2834 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002835
Richard Smith23d8d032015-05-14 00:45:20 +00002836 assert((NextSubmoduleID - FirstSubmoduleID ==
2837 getNumberOfModules(WritingModule)) &&
2838 "Wrong # of submodules; found a reference to a non-local, "
2839 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002840}
2841
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002842serialization::SubmoduleID
2843ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002844 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002845 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002846
2847 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002848 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002849 Module *OwningMod
2850 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002851 if (!OwningMod)
2852 return 0;
2853
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002854 // Check whether this submodule is part of our own module.
2855 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002856 return 0;
2857
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002858 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002859}
2860
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002861void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2862 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002863 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2864 DiagStateIDMap;
2865 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002866 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002867
Richard Smithd230de22017-01-26 01:01:01 +00002868 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2869 bool IncludeNonPragmaStates) {
2870 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002871 Record.push_back(DiagStateID);
Richard Smithd230de22017-01-26 01:01:01 +00002872
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002873 if (DiagStateID == 0) {
2874 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002875
2876 // Add a placeholder for the number of mappings.
2877 auto SizeIdx = Record.size();
2878 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00002879 for (const auto &I : *State) {
2880 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002881 Record.push_back(I.first);
2882 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002883 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002884 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002885 // Update the placeholder.
2886 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00002887 }
2888 };
2889
2890 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2891 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2892 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2893
2894 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2895 if (!FileIDAndFile.first.isValid() ||
2896 !FileIDAndFile.second.HasLocalTransitions)
2897 continue;
2898 AddSourceLocation(Diag.SourceMgr->getLocForStartOfFile(FileIDAndFile.first),
2899 Record);
2900 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2901 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2902 Record.push_back(StatePoint.Offset);
2903 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002904 }
2905 }
2906
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002907 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002908 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002909}
2910
Douglas Gregorc5046832009-04-27 18:38:38 +00002911//===----------------------------------------------------------------------===//
2912// Type Serialization
2913//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002914
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002915/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002916void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002917 TypeIdx &IdxRef = TypeIdxs[T];
2918 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2919 IdxRef = TypeIdx(NextTypeID++);
2920 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002921
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002922 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002923
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002924 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002925
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002926 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002927 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002928 W.Visit(T);
2929 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002930
Richard Smith290d8012016-04-06 17:06:00 +00002931 // Record the offset for this type.
2932 unsigned Index = Idx.getIndex() - FirstTypeID;
2933 if (TypeOffsets.size() == Index)
2934 TypeOffsets.push_back(Offset);
2935 else if (TypeOffsets.size() < Index) {
2936 TypeOffsets.resize(Index + 1);
2937 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002938 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002939 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002940 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002941}
2942
Douglas Gregorc5046832009-04-27 18:38:38 +00002943//===----------------------------------------------------------------------===//
2944// Declaration Serialization
2945//===----------------------------------------------------------------------===//
2946
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002947/// \brief Write the block containing all of the declaration IDs
2948/// lexically declared within the given DeclContext.
2949///
2950/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2951/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002952uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002953 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002954 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002955 return 0;
2956
Douglas Gregor8f45df52009-04-16 22:23:12 +00002957 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002958 SmallVector<uint32_t, 128> KindDeclPairs;
2959 for (const auto *D : DC->decls()) {
2960 KindDeclPairs.push_back(D->getKind());
2961 KindDeclPairs.push_back(GetDeclRef(D));
2962 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002963
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002964 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002965 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002966 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2967 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002968 return Offset;
2969}
2970
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002971void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002972 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002973
2974 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002975 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002976 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002980 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002981 {
2982 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2983 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2984 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2985 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002986
2987 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002988 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002989 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002993 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002994 {
2995 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2996 FirstDeclID - NUM_PREDEF_DECL_IDS};
2997 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2998 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002999}
3000
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003001void ASTWriter::WriteFileDeclIDsMap() {
3002 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003003
Chandler Carruthb306d732015-03-27 00:31:20 +00003004 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3005 FileDeclIDs.begin(), FileDeclIDs.end());
3006 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3007 llvm::less_first());
3008
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003009 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003010 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3011 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3012 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3013 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3014 for (auto &LocDeclEntry : Info.DeclIDs)
3015 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003016 }
3017
David Blaikieb44f0bf2017-01-04 22:36:43 +00003018 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003019 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003022 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003023 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3024 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003025 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003026}
3027
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003028void ASTWriter::WriteComments() {
3029 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003030 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003031 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003032 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003033 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003034 AddSourceRange(I->getSourceRange(), Record);
3035 Record.push_back(I->getKind());
3036 Record.push_back(I->isTrailingComment());
3037 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003038 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3039 }
3040 Stream.ExitBlock();
3041}
3042
Douglas Gregorc5046832009-04-27 18:38:38 +00003043//===----------------------------------------------------------------------===//
3044// Global Method Pool and Selector Serialization
3045//===----------------------------------------------------------------------===//
3046
Douglas Gregore84a9da2009-04-20 20:36:09 +00003047namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003048
Douglas Gregorc78d3462009-04-24 21:10:55 +00003049// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003050class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003051 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003052
3053public:
3054 typedef Selector key_type;
3055 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003056
Sebastian Redl834bb972010-08-04 17:20:04 +00003057 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003058 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003059 ObjCMethodList Instance, Factory;
3060 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003061 typedef const data_type& data_type_ref;
3062
Justin Bogner25463f12014-04-18 20:27:24 +00003063 typedef unsigned hash_value_type;
3064 typedef unsigned offset_type;
3065
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003066 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003067
Justin Bogner25463f12014-04-18 20:27:24 +00003068 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003069 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003070 }
Mike Stump11289f42009-09-09 15:08:12 +00003071
3072 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003073 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003074 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003075 using namespace llvm::support;
3076 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003077 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003078 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003079 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3080 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003081 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003082 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003083 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003084 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003085 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003086 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003087 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003088 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003089 return std::make_pair(KeyLen, DataLen);
3090 }
Mike Stump11289f42009-09-09 15:08:12 +00003091
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003092 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003093 using namespace llvm::support;
3094 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003095 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003096 assert((Start >> 32) == 0 && "Selector key offset too large");
3097 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003098 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003099 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003100 if (N == 0)
3101 N = 1;
3102 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003103 LE.write<uint32_t>(
3104 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003105 }
Mike Stump11289f42009-09-09 15:08:12 +00003106
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003107 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003108 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003109 using namespace llvm::support;
3110 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003111 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003112 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003113 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003114 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003115 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003116 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003117 ++NumInstanceMethods;
3118
3119 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003120 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003121 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003122 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003123 ++NumFactoryMethods;
3124
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003125 unsigned InstanceBits = Methods.Instance.getBits();
3126 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003127 unsigned InstanceHasMoreThanOneDeclBit =
3128 Methods.Instance.hasMoreThanOneDecl();
3129 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3130 (InstanceHasMoreThanOneDeclBit << 2) |
3131 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003132 unsigned FactoryBits = Methods.Factory.getBits();
3133 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003134 unsigned FactoryHasMoreThanOneDeclBit =
3135 Methods.Factory.hasMoreThanOneDecl();
3136 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3137 (FactoryHasMoreThanOneDeclBit << 2) |
3138 FactoryBits;
3139 LE.write<uint16_t>(FullInstanceBits);
3140 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003141 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003142 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003143 if (Method->getMethod())
3144 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003145 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003146 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003147 if (Method->getMethod())
3148 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003149
3150 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003151 }
3152};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003153
Douglas Gregorc78d3462009-04-24 21:10:55 +00003154} // end anonymous namespace
3155
Sebastian Redla19a67f2010-08-03 21:58:15 +00003156/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003157///
3158/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003159/// in an on-disk hash table indexed by the selector. The hash table also
3160/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003161void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003162 using namespace llvm;
3163
Sebastian Redla19a67f2010-08-03 21:58:15 +00003164 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003165 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003166 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003167 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003168 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003169 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003170 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003171 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003172
Sebastian Redla19a67f2010-08-03 21:58:15 +00003173 // Create the on-disk hash table representation. We walk through every
3174 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003175 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003176 for (auto &SelectorAndID : SelectorIDs) {
3177 Selector S = SelectorAndID.first;
3178 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003179 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003180 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003181 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003182 ObjCMethodList(),
3183 ObjCMethodList()
3184 };
3185 if (F != SemaRef.MethodPool.end()) {
3186 Data.Instance = F->second.first;
3187 Data.Factory = F->second.second;
3188 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003189 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003190 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003191 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003192 // Selector already exists. Did it change?
3193 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003194 for (ObjCMethodList *M = &Data.Instance;
3195 !changed && M && M->getMethod(); M = M->getNext()) {
3196 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003197 changed = true;
3198 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003199 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003200 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003201 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003202 changed = true;
3203 }
3204 if (!changed)
3205 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003206 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003207 // A new method pool entry.
3208 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003209 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003210 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003211 }
3212
Douglas Gregorc78d3462009-04-24 21:10:55 +00003213 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003214 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003215 uint32_t BucketOffset;
3216 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003217 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003218 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003219 llvm::raw_svector_ostream Out(MethodPool);
3220 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003221 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003222 BucketOffset = Generator.Emit(Out, Trait);
3223 }
3224
3225 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003226 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003231 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003232
Douglas Gregor95c13f52009-04-25 17:48:32 +00003233 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003234 {
3235 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3236 NumTableEntries};
3237 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3238 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003239
3240 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003241 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003242 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003246 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003247
3248 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003249 {
3250 RecordData::value_type Record[] = {
3251 SELECTOR_OFFSETS, SelectorOffsets.size(),
3252 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3253 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3254 bytes(SelectorOffsets));
3255 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003256 }
3257}
3258
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003259/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003260void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003261 using namespace llvm;
3262 if (SemaRef.ReferencedSelectors.empty())
3263 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003264
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003265 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003266 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003267
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003268 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003269 // very tricky to fix, and given that @selector shouldn't really appear in
3270 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003271 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3272 Selector Sel = SelectorAndLocation.first;
3273 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003274 Writer.AddSelectorRef(Sel);
3275 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003276 }
Richard Smithe64e7452016-04-18 21:54:58 +00003277 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003278}
3279
Douglas Gregorc5046832009-04-27 18:38:38 +00003280//===----------------------------------------------------------------------===//
3281// Identifier Table Serialization
3282//===----------------------------------------------------------------------===//
3283
Richard Smithb3761912015-02-05 23:08:52 +00003284/// Determine the declaration that should be put into the name lookup table to
3285/// represent the given declaration in this module. This is usually D itself,
3286/// but if D was imported and merged into a local declaration, we want the most
3287/// recent local declaration instead. The chosen declaration will be the most
3288/// recent declaration in any module that imports this one.
3289static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3290 NamedDecl *D) {
3291 if (!LangOpts.Modules || !D->isFromASTFile())
3292 return D;
3293
3294 if (Decl *Redecl = D->getPreviousDecl()) {
3295 // For Redeclarable decls, a prior declaration might be local.
3296 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003297 // If we find a local decl, we're done.
3298 if (!Redecl->isFromASTFile()) {
3299 // Exception: in very rare cases (for injected-class-names), not all
3300 // redeclarations are in the same semantic context. Skip ones in a
3301 // different context. They don't go in this lookup table at all.
3302 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3303 D->getDeclContext()->getRedeclContext()))
3304 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003305 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003306 }
3307
Richard Smithfe620d22015-03-05 23:24:12 +00003308 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003309 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003310 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003311 break;
3312 }
3313 } else if (Decl *First = D->getCanonicalDecl()) {
3314 // For Mergeable decls, the first decl might be local.
3315 if (!First->isFromASTFile())
3316 return cast<NamedDecl>(First);
3317 }
3318
3319 // All declarations are imported. Our most recent declaration will also be
3320 // the most recent one in anyone who imports us.
3321 return D;
3322}
3323
Douglas Gregorc78d3462009-04-24 21:10:55 +00003324namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003325
Richard Smithcf97cf62015-04-19 01:34:23 +00003326class ASTIdentifierTableTrait {
3327 ASTWriter &Writer;
3328 Preprocessor &PP;
3329 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003330 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003331 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003332 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003333
3334 /// \brief Determines whether this is an "interesting" identifier that needs a
3335 /// full IdentifierInfo structure written into the hash table. Notably, this
3336 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3337 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003338 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003339 if (MacroOffset ||
3340 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003341 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003342 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003343 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003344 return true;
3345
3346 return false;
3347 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003348
Douglas Gregore84a9da2009-04-20 20:36:09 +00003349public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003350 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003351 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003352
Sebastian Redl539c5062010-08-18 23:57:32 +00003353 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003354 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003355
Justin Bogner25463f12014-04-18 20:27:24 +00003356 typedef unsigned hash_value_type;
3357 typedef unsigned offset_type;
3358
Richard Smithd7329392015-04-21 21:46:32 +00003359 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003360 IdentifierResolver &IdResolver, bool IsModule,
3361 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003362 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003363 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3364 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003365
Richard Smithd79514e2016-02-05 19:03:40 +00003366 bool needDecls() const { return NeedDecls; }
3367
Justin Bogner25463f12014-04-18 20:27:24 +00003368 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003369 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003370 }
Mike Stump11289f42009-09-09 15:08:12 +00003371
Richard Smith33e0f7e2015-07-22 02:08:40 +00003372 bool isInterestingIdentifier(const IdentifierInfo *II) {
3373 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3374 return isInterestingIdentifier(II, MacroOffset);
3375 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003376
Richard Smith9c254182015-07-19 21:41:12 +00003377 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3378 return isInterestingIdentifier(II, 0);
3379 }
3380
Mike Stump11289f42009-09-09 15:08:12 +00003381 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003382 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003383 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003384 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003385 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3386 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003387 DataLen += 2; // 2 bytes for builtin ID
3388 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003389 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003390 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003391
Richard Smitha534a312015-07-21 23:54:07 +00003392 if (NeedDecls) {
3393 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3394 DEnd = IdResolver.end();
3395 D != DEnd; ++D)
3396 DataLen += 4;
3397 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003398 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003399 using namespace llvm::support;
3400 endian::Writer<little> LE(Out);
3401
Richard Smithdf8a8312015-03-13 04:05:01 +00003402 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003403 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003404 // We emit the key length after the data length so that every
3405 // string is preceded by a 16-bit length. This matches the PTH
3406 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003407 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003408 return std::make_pair(KeyLen, DataLen);
3409 }
Mike Stump11289f42009-09-09 15:08:12 +00003410
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003411 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003412 unsigned KeyLen) {
3413 // Record the location of the key data. This is used when generating
3414 // the mapping from persistent IDs to strings.
3415 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003416
3417 // Emit the offset of the key/data length information to the interesting
3418 // identifiers table if necessary.
3419 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3420 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3421
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003422 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003423 }
Mike Stump11289f42009-09-09 15:08:12 +00003424
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003425 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003426 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003427 using namespace llvm::support;
3428 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003429
Richard Smithd7329392015-04-21 21:46:32 +00003430 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3431 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003432 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003433 return;
3434 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003435
Justin Bognere1c147c2014-03-28 22:03:19 +00003436 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003437 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3438 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003439 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003440 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003441 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003442 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003443 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3444 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003445 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003446 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003447 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003448 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003449
Richard Smithd7329392015-04-21 21:46:32 +00003450 if (HadMacroDefinition)
3451 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003452
Richard Smitha534a312015-07-21 23:54:07 +00003453 if (NeedDecls) {
3454 // Emit the declaration IDs in reverse order, because the
3455 // IdentifierResolver provides the declarations as they would be
3456 // visible (e.g., the function "stat" would come before the struct
3457 // "stat"), but the ASTReader adds declarations to the end of the list
3458 // (so we need to see the struct "stat" before the function "stat").
3459 // Only emit declarations that aren't from a chained PCH, though.
3460 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3461 IdResolver.end());
3462 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3463 DEnd = Decls.rend();
3464 D != DEnd; ++D)
3465 LE.write<uint32_t>(
3466 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3467 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003468 }
3469};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003470
Douglas Gregore84a9da2009-04-20 20:36:09 +00003471} // end anonymous namespace
3472
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003473/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003474///
3475/// The identifier table consists of a blob containing string data
3476/// (the actual identifiers themselves) and a separate "offsets" index
3477/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003478void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3479 IdentifierResolver &IdResolver,
3480 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003481 using namespace llvm;
3482
Richard Smith33e0f7e2015-07-22 02:08:40 +00003483 RecordData InterestingIdents;
3484
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003485 // Create and write out the blob that contains the identifier
3486 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003487 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003488 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003489 ASTIdentifierTableTrait Trait(
3490 *this, PP, IdResolver, IsModule,
3491 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003492
Douglas Gregore6648fb2009-04-28 20:33:11 +00003493 // Look for any identifiers that were named while processing the
3494 // headers, but are otherwise not needed. We add these to the hash
3495 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003496 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003497 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003498 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003499 for (const auto &ID : PP.getIdentifierTable())
3500 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003501 // Sort the identifiers lexicographically before getting them references so
3502 // that their order is stable.
3503 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3504 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003505 if (Trait.isInterestingNonMacroIdentifier(II))
3506 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003507
Sebastian Redlff4a2952010-07-23 23:49:55 +00003508 // Create the on-disk hash table representation. We only store offsets
3509 // for identifiers that appear here for the first time.
3510 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003511 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003512 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003513 IdentID ID = IdentIDPair.second;
3514 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003515 // Write out identifiers if either the ID is local or the identifier has
3516 // changed since it was loaded.
3517 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3518 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003519 (Trait.needDecls() &&
3520 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003521 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003522 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003523
Douglas Gregore84a9da2009-04-20 20:36:09 +00003524 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003525 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003526 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003527 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003528 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003529 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003530 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003531 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003532 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003533 }
3534
3535 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003536 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003537 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003540 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003541
3542 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003543 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003544 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003545 }
3546
3547 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003548 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003549 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003553 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003554
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003555#ifndef NDEBUG
3556 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3557 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3558#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003559
3560 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3561 IdentifierOffsets.size(),
3562 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003563 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003564 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003565
3566 // In C++, write the list of interesting identifiers (those that are
3567 // defined as macros, poisoned, or similar unusual things).
3568 if (!InterestingIdents.empty())
3569 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003570}
3571
Douglas Gregorc5046832009-04-27 18:38:38 +00003572//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003573// DeclContext's Name Lookup Table Serialization
3574//===----------------------------------------------------------------------===//
3575
3576namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003577
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003578// Trait used for the on-disk hash table used in the method pool.
3579class ASTDeclContextNameLookupTrait {
3580 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003581 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003582
3583public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003584 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003585 typedef key_type key_type_ref;
3586
Richard Smithd88a7f12015-09-01 20:35:42 +00003587 /// A start and end index into DeclIDs, representing a sequence of decls.
3588 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003589 typedef const data_type& data_type_ref;
3590
Justin Bogner25463f12014-04-18 20:27:24 +00003591 typedef unsigned hash_value_type;
3592 typedef unsigned offset_type;
3593
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003594 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3595
Richard Smithd88a7f12015-09-01 20:35:42 +00003596 template<typename Coll>
3597 data_type getData(const Coll &Decls) {
3598 unsigned Start = DeclIDs.size();
3599 for (NamedDecl *D : Decls) {
3600 DeclIDs.push_back(
3601 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3602 }
3603 return std::make_pair(Start, DeclIDs.size());
3604 }
3605
3606 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3607 unsigned Start = DeclIDs.size();
3608 for (auto ID : FromReader)
3609 DeclIDs.push_back(ID);
3610 return std::make_pair(Start, DeclIDs.size());
3611 }
3612
3613 static bool EqualKey(key_type_ref a, key_type_ref b) {
3614 return a == b;
3615 }
3616
Richard Smitha06c7e62015-08-26 23:55:49 +00003617 hash_value_type ComputeHash(DeclarationNameKey Name) {
3618 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003619 }
3620
Richard Smithd88a7f12015-09-01 20:35:42 +00003621 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3622 assert(Writer.hasChain() &&
3623 "have reference to loaded module file but no chain?");
3624
3625 using namespace llvm::support;
3626 endian::Writer<little>(Out)
3627 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3628 }
3629
Richard Smitha06c7e62015-08-26 23:55:49 +00003630 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3631 DeclarationNameKey Name,
3632 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003633 using namespace llvm::support;
3634 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003635 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003636 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003637 case DeclarationName::Identifier:
3638 case DeclarationName::ObjCZeroArgSelector:
3639 case DeclarationName::ObjCOneArgSelector:
3640 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003641 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003642 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003643 KeyLen += 4;
3644 break;
3645 case DeclarationName::CXXOperatorName:
3646 KeyLen += 1;
3647 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003648 case DeclarationName::CXXConstructorName:
3649 case DeclarationName::CXXDestructorName:
3650 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003651 case DeclarationName::CXXUsingDirective:
3652 break;
3653 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003654 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003655
Richard Smithf02662d2015-07-30 03:17:16 +00003656 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003657 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3658 assert(uint16_t(DataLen) == DataLen &&
3659 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003660 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003661
3662 return std::make_pair(KeyLen, DataLen);
3663 }
3664
Richard Smitha06c7e62015-08-26 23:55:49 +00003665 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003666 using namespace llvm::support;
3667 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003668 LE.write<uint8_t>(Name.getKind());
3669 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003670 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003671 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003672 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003673 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003674 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003675 case DeclarationName::ObjCZeroArgSelector:
3676 case DeclarationName::ObjCOneArgSelector:
3677 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003678 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003679 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003680 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003681 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003682 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003683 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003684 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003685 case DeclarationName::CXXConstructorName:
3686 case DeclarationName::CXXDestructorName:
3687 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003688 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003689 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003690 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003691
3692 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003693 }
3694
Richard Smitha06c7e62015-08-26 23:55:49 +00003695 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3696 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003697 using namespace llvm::support;
3698 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003699 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003700 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3701 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003702 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3703 }
3704};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003705
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003706} // end anonymous namespace
3707
Chandler Carruthe972c362015-03-26 03:11:40 +00003708bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3709 DeclContext *DC) {
3710 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3711}
3712
3713bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3714 DeclContext *DC) {
3715 for (auto *D : Result.getLookupResult())
3716 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3717 return false;
3718
3719 return true;
3720}
3721
Richard Smithd88a7f12015-09-01 20:35:42 +00003722void
Chandler Carruthe972c362015-03-26 03:11:40 +00003723ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003724 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003725 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3726 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003727 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003728
Chandler Carruthe972c362015-03-26 03:11:40 +00003729 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003730 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003731 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3732
3733 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003734 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3735 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003736 ASTDeclContextNameLookupTrait Trait(*this);
3737
Chandler Carruthe972c362015-03-26 03:11:40 +00003738 // The first step is to collect the declaration names which we need to
3739 // serialize into the name lookup table, and to collect them in a stable
3740 // order.
3741 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003742
Chandler Carruthe972c362015-03-26 03:11:40 +00003743 // We also build up small sets of the constructor and conversion function
3744 // names which are visible.
3745 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003746
Chandler Carruthe972c362015-03-26 03:11:40 +00003747 for (auto &Lookup : *DC->buildLookup()) {
3748 auto &Name = Lookup.first;
3749 auto &Result = Lookup.second;
3750
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003751 // If there are no local declarations in our lookup result, we
3752 // don't need to write an entry for the name at all. If we can't
3753 // write out a lookup set without performing more deserialization,
3754 // just skip this entry.
3755 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003756 isLookupResultEntirelyExternal(Result, DC))
3757 continue;
3758
3759 // We also skip empty results. If any of the results could be external and
3760 // the currently available results are empty, then all of the results are
3761 // external and we skip it above. So the only way we get here with an empty
3762 // results is when no results could have been external *and* we have
3763 // external results.
3764 //
3765 // FIXME: While we might want to start emitting on-disk entries for negative
3766 // lookups into a decl context as an optimization, today we *have* to skip
3767 // them because there are names with empty lookup results in decl contexts
3768 // which we can't emit in any stable ordering: we lookup constructors and
3769 // conversion functions in the enclosing namespace scope creating empty
3770 // results for them. This in almost certainly a bug in Clang's name lookup,
3771 // but that is likely to be hard or impossible to fix and so we tolerate it
3772 // here by omitting lookups with empty results.
3773 if (Lookup.second.getLookupResult().empty())
3774 continue;
3775
3776 switch (Lookup.first.getNameKind()) {
3777 default:
3778 Names.push_back(Lookup.first);
3779 break;
3780
Richard Smithcd45dbc2014-04-19 03:48:30 +00003781 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003782 assert(isa<CXXRecordDecl>(DC) &&
3783 "Cannot have a constructor name outside of a class!");
3784 ConstructorNameSet.insert(Name);
3785 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003786
3787 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003788 assert(isa<CXXRecordDecl>(DC) &&
3789 "Cannot have a conversion function name outside of a class!");
3790 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003791 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003792 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003793 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003794
Chandler Carruthe972c362015-03-26 03:11:40 +00003795 // Sort the names into a stable order.
3796 std::sort(Names.begin(), Names.end());
3797
Chandler Carruthffbf7052015-03-26 22:27:09 +00003798 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003799 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003800 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003801
Chandler Carruthffbf7052015-03-26 22:27:09 +00003802 // First we try the easy case by forming the current context's constructor
3803 // name and adding that name first. This is a very useful optimization to
3804 // avoid walking the lexical declarations in many cases, and it also
3805 // handles the only case where a constructor name can come from some other
3806 // lexical context -- when that name is an implicit constructor merged from
3807 // another declaration in the redecl chain. Any non-implicit constructor or
3808 // conversion function which doesn't occur in all the lexical contexts
3809 // would be an ODR violation.
3810 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3811 Context->getCanonicalType(Context->getRecordType(D)));
3812 if (ConstructorNameSet.erase(ImplicitCtorName))
3813 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003814
Chandler Carruthffbf7052015-03-26 22:27:09 +00003815 // If we still have constructors or conversion functions, we walk all the
3816 // names in the decl and add the constructors and conversion functions
3817 // which are visible in the order they lexically occur within the context.
3818 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3819 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3820 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3821 auto Name = ChildND->getDeclName();
3822 switch (Name.getNameKind()) {
3823 default:
3824 continue;
3825
3826 case DeclarationName::CXXConstructorName:
3827 if (ConstructorNameSet.erase(Name))
3828 Names.push_back(Name);
3829 break;
3830
3831 case DeclarationName::CXXConversionFunctionName:
3832 if (ConversionNameSet.erase(Name))
3833 Names.push_back(Name);
3834 break;
3835 }
3836
3837 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3838 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003839 }
3840
Chandler Carruthe972c362015-03-26 03:11:40 +00003841 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3842 "constructors by walking all the "
3843 "lexical members of the context.");
3844 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3845 "conversion functions by walking all "
3846 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003847 }
3848
Chandler Carruthe972c362015-03-26 03:11:40 +00003849 // Next we need to do a lookup with each name into this decl context to fully
3850 // populate any results from external sources. We don't actually use the
3851 // results of these lookups because we only want to use the results after all
3852 // results have been loaded and the pointers into them will be stable.
3853 for (auto &Name : Names)
3854 DC->lookup(Name);
3855
3856 // Now we need to insert the results for each name into the hash table. For
3857 // constructor names and conversion function names, we actually need to merge
3858 // all of the results for them into one list of results each and insert
3859 // those.
3860 SmallVector<NamedDecl *, 8> ConstructorDecls;
3861 SmallVector<NamedDecl *, 8> ConversionDecls;
3862
3863 // Now loop over the names, either inserting them or appending for the two
3864 // special cases.
3865 for (auto &Name : Names) {
3866 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3867
3868 switch (Name.getNameKind()) {
3869 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003870 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003871 break;
3872
3873 case DeclarationName::CXXConstructorName:
3874 ConstructorDecls.append(Result.begin(), Result.end());
3875 break;
3876
3877 case DeclarationName::CXXConversionFunctionName:
3878 ConversionDecls.append(Result.begin(), Result.end());
3879 break;
3880 }
3881 }
3882
3883 // Handle our two special cases if we ended up having any. We arbitrarily use
3884 // the first declaration's name here because the name itself isn't part of
3885 // the key, only the kind of name is used.
3886 if (!ConstructorDecls.empty())
3887 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003888 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003889 if (!ConversionDecls.empty())
3890 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003891 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003892
Richard Smithd88a7f12015-09-01 20:35:42 +00003893 // Create the on-disk hash table. Also emit the existing imported and
3894 // merged table if there is one.
3895 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3896 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003897}
3898
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003899/// \brief Write the block containing all of the declaration IDs
3900/// visible from the given DeclContext.
3901///
3902/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003903/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003904uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3905 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003906 // If we imported a key declaration of this namespace, write the visible
3907 // lookup results as an update record for it rather than including them
3908 // on this declaration. We will only look at key declarations on reload.
3909 if (isa<NamespaceDecl>(DC) && Chain &&
3910 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3911 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003912 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003913 Prev = Prev->getPreviousDecl())
3914 if (!Prev->isFromASTFile())
3915 return 0;
3916
3917 // Note that we need to emit an update record for the primary context.
3918 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3919
3920 // Make sure all visible decls are written. They will be recorded later. We
3921 // do this using a side data structure so we can sort the names into
3922 // a deterministic order.
3923 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3924 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3925 LookupResults;
3926 if (Map) {
3927 LookupResults.reserve(Map->size());
3928 for (auto &Entry : *Map)
3929 LookupResults.push_back(
3930 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3931 }
3932
3933 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3934 for (auto &NameAndResult : LookupResults) {
3935 DeclarationName Name = NameAndResult.first;
3936 DeclContext::lookup_result Result = NameAndResult.second;
3937 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3938 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3939 // We have to work around a name lookup bug here where negative lookup
3940 // results for these names get cached in namespace lookup tables (these
3941 // names should never be looked up in a namespace).
3942 assert(Result.empty() && "Cannot have a constructor or conversion "
3943 "function name in a namespace!");
3944 continue;
3945 }
3946
3947 for (NamedDecl *ND : Result)
3948 if (!ND->isFromASTFile())
3949 GetDeclRef(ND);
3950 }
3951
3952 return 0;
3953 }
3954
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003955 if (DC->getPrimaryContext() != DC)
3956 return 0;
3957
Chandler Carruthe972c362015-03-26 03:11:40 +00003958 // Skip contexts which don't support name lookup.
3959 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003960 return 0;
3961
3962 // If not in C++, we perform name lookup for the translation unit via the
3963 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003964 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003965 return 0;
3966
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003967 // Serialize the contents of the mapping used for lookup. Note that,
3968 // although we have two very different code paths, the serialized
3969 // representation is the same for both cases: a declaration name,
3970 // followed by a size, followed by references to the visible
3971 // declarations that have that name.
3972 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003973 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003974 if (!Map || Map->empty())
3975 return 0;
3976
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003977 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003978 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003979 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003980
3981 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003982 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003983 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003984 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003985 ++NumVisibleDeclContexts;
3986 return Offset;
3987}
3988
Sebastian Redla4071b42010-08-24 00:50:09 +00003989/// \brief Write an UPDATE_VISIBLE block for the given context.
3990///
3991/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3992/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003993/// (in C++), for namespaces, and for classes with forward-declared unscoped
3994/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003995void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003996 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003997 if (!Map || Map->empty())
3998 return;
3999
Sebastian Redla4071b42010-08-24 00:50:09 +00004000 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004001 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004002 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004003
Richard Smith5fc18a92015-07-12 23:43:21 +00004004 // If we're updating a namespace, select a key declaration as the key for the
4005 // update record; those are the only ones that will be checked on reload.
4006 if (isa<NamespaceDecl>(DC))
4007 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4008
Sebastian Redla4071b42010-08-24 00:50:09 +00004009 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004010 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004011 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004012}
4013
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004014/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4015void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00004016 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004017 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4018}
4019
4020/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4021void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004022 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004023 return;
4024
4025 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4026 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004027 for (const auto &I:Opts.OptMap) {
4028 AddString(I.getKey(), Record);
4029 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004030 Record.push_back(V.Supported ? 1 : 0);
4031 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004032 Record.push_back(V.Avail);
4033 Record.push_back(V.Core);
4034 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004035 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4036}
4037
Yaxun Liu5b746652016-12-18 05:18:55 +00004038void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4039 if (!SemaRef.Context.getLangOpts().OpenCL)
4040 return;
4041
4042 RecordData Record;
4043 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4044 Record.push_back(
4045 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4046 Record.push_back(I.second.size());
4047 for (auto Ext : I.second)
4048 AddString(Ext, Record);
4049 }
4050 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4051}
4052
4053void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4054 if (!SemaRef.Context.getLangOpts().OpenCL)
4055 return;
4056
4057 RecordData Record;
4058 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4059 Record.push_back(getDeclID(I.first));
4060 Record.push_back(static_cast<unsigned>(I.second.size()));
4061 for (auto Ext : I.second)
4062 AddString(Ext, Record);
4063 }
4064 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4065}
4066
Justin Lebar67a78a62016-10-08 22:15:58 +00004067void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4068 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4069 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4070 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4071 }
4072}
4073
Douglas Gregor404cdde2012-01-27 01:47:08 +00004074void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004075 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004076 RecordData Categories;
4077
4078 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4079 unsigned Size = 0;
4080 unsigned StartIndex = Categories.size();
4081
4082 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4083
4084 // Allocate space for the size.
4085 Categories.push_back(0);
4086
4087 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004088 for (ObjCInterfaceDecl::known_categories_iterator
4089 Cat = Class->known_categories_begin(),
4090 CatEnd = Class->known_categories_end();
4091 Cat != CatEnd; ++Cat, ++Size) {
4092 assert(getDeclID(*Cat) != 0 && "Bogus category");
4093 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004094 }
4095
4096 // Update the size.
4097 Categories[StartIndex] = Size;
4098
4099 // Record this interface -> category map.
4100 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4101 CategoriesMap.push_back(CatInfo);
4102 }
4103
4104 // Sort the categories map by the definition ID, since the reader will be
4105 // performing binary searches on this information.
4106 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4107
4108 // Emit the categories map.
4109 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004110
David Blaikieb44f0bf2017-01-04 22:36:43 +00004111 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004112 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004115 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004116
4117 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4118 Stream.EmitRecordWithBlob(AbbrevID, Record,
4119 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004120 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004121
Douglas Gregor404cdde2012-01-27 01:47:08 +00004122 // Emit the category lists.
4123 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4124}
4125
Richard Smithe40f2ba2013-08-07 21:41:30 +00004126void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4127 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4128
4129 if (LPTMap.empty())
4130 return;
4131
4132 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004133 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004134 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004135 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004136 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004137 AddDeclRef(LPT.D, Record);
4138 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004139
Justin Lebar28f09c52016-10-10 16:26:08 +00004140 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004141 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004142 }
4143 }
4144 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4145}
4146
Dario Domizioli13a0a382014-05-23 12:13:25 +00004147/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4148void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4149 RecordData Record;
4150 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4151 AddSourceLocation(PragmaLoc, Record);
4152 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4153}
4154
Nico Weber779355f2016-03-02 23:22:00 +00004155/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4156void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4157 RecordData Record;
4158 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4159 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4160}
4161
Nico Weber42932312016-03-03 00:17:35 +00004162/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4163//module.
4164void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4165 RecordData Record;
4166 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4167 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4168 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4169}
4170
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004171void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4172 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004173 // Enter the extension block.
4174 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4175
4176 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004177 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004178 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4179 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4180 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4181 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4182 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4183 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004184 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004185
4186 // Emit the metadata record.
4187 RecordData Record;
4188 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4189 Record.push_back(EXTENSION_METADATA);
4190 Record.push_back(Metadata.MajorVersion);
4191 Record.push_back(Metadata.MinorVersion);
4192 Record.push_back(Metadata.BlockName.size());
4193 Record.push_back(Metadata.UserInfo.size());
4194 SmallString<64> Buffer;
4195 Buffer += Metadata.BlockName;
4196 Buffer += Metadata.UserInfo;
4197 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4198
4199 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004200 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004201
4202 // Exit the extension block.
4203 Stream.ExitBlock();
4204}
4205
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004206//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004207// General Serialization Routines
4208//===----------------------------------------------------------------------===//
4209
Richard Smith69c82bf2016-04-01 22:52:03 +00004210/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004211void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4212 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004213 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004214 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004215 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004216 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004217
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004218#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004219
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004220 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004221}
4222
John McCallf413f5e2013-05-03 00:10:13 +00004223void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4224 AddSourceLocation(Tok.getLocation(), Record);
4225 Record.push_back(Tok.getLength());
4226
4227 // FIXME: When reading literal tokens, reconstruct the literal pointer
4228 // if it is needed.
4229 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4230 // FIXME: Should translate token kind to a stable encoding.
4231 Record.push_back(Tok.getKind());
4232 // FIXME: Should translate token flags to a stable encoding.
4233 Record.push_back(Tok.getFlags());
4234}
4235
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004236void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004237 Record.push_back(Str.size());
4238 Record.insert(Record.end(), Str.begin(), Str.end());
4239}
4240
Richard Smith7ed1bc92014-12-05 22:42:13 +00004241bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004242 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004243
Richard Smith54cc3c22014-12-11 20:50:24 +00004244 bool Changed =
4245 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004246
4247 // Remove a prefix to make the path relative, if relevant.
4248 const char *PathBegin = Path.data();
4249 const char *PathPtr =
4250 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4251 if (PathPtr != PathBegin) {
4252 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4253 Changed = true;
4254 }
4255
4256 return Changed;
4257}
4258
4259void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4260 SmallString<128> FilePath(Path);
4261 PreparePathForOutput(FilePath);
4262 AddString(FilePath, Record);
4263}
4264
Mehdi Amini57a41912015-09-10 01:46:39 +00004265void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004266 StringRef Path) {
4267 SmallString<128> FilePath(Path);
4268 PreparePathForOutput(FilePath);
4269 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4270}
4271
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004272void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4273 RecordDataImpl &Record) {
4274 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004275 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004276 Record.push_back(*Minor + 1);
4277 else
4278 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004279 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004280 Record.push_back(*Subminor + 1);
4281 else
4282 Record.push_back(0);
4283}
4284
Douglas Gregore84a9da2009-04-20 20:36:09 +00004285/// \brief Note that the identifier II occurs at the given offset
4286/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004287void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004288 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004289 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004290 // up earlier in the chain and thus don't need an offset.
4291 if (ID >= FirstIdentID)
4292 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004293}
4294
Douglas Gregor95c13f52009-04-25 17:48:32 +00004295/// \brief Note that the selector Sel occurs at the given offset
4296/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004297void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004298 unsigned ID = SelectorIDs[Sel];
4299 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004300 // Don't record offsets for selectors that are also available in a different
4301 // file.
4302 if (ID < FirstSelectorID)
4303 return;
4304 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004305}
4306
David Blaikie61137e12017-01-05 18:23:18 +00004307ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004308 SmallVectorImpl<char> &Buffer, MemoryBufferCache &PCMCache,
David Blaikie61137e12017-01-05 18:23:18 +00004309 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4310 bool IncludeTimestamps)
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004311 : Stream(Stream), Buffer(Buffer), PCMCache(PCMCache),
4312 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004313 for (const auto &Ext : Extensions) {
4314 if (auto Writer = Ext->createExtensionWriter(*this))
4315 ModuleFileExtensionWriters.push_back(std::move(Writer));
4316 }
4317}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004318
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004319ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004320 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004321}
4322
Richard Smithb3761912015-02-05 23:08:52 +00004323const LangOptions &ASTWriter::getLangOpts() const {
4324 assert(WritingAST && "can't determine lang opts when not writing AST");
4325 return Context->getLangOpts();
4326}
4327
Richard Smithe75ee0f2015-08-17 07:13:32 +00004328time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4329 return IncludeTimestamps ? E->getModificationTime() : 0;
4330}
4331
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004332ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4333 const std::string &OutputFile,
4334 Module *WritingModule, StringRef isysroot,
4335 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004336 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004337
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004338 ASTHasCompilerErrors = hasErrors;
4339
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004340 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004341 Stream.Emit((unsigned)'C', 8);
4342 Stream.Emit((unsigned)'P', 8);
4343 Stream.Emit((unsigned)'C', 8);
4344 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004345
Chris Lattner28fa4e62009-04-26 22:26:21 +00004346 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004347
Douglas Gregoreda8e122011-08-09 15:13:55 +00004348 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004349 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004350 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004351 ASTFileSignature Signature =
4352 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004353 Context = nullptr;
4354 PP = nullptr;
4355 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004356 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004357
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004358 WritingAST = false;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004359 if (SemaRef.Context.getLangOpts().ImplicitModules && WritingModule) {
4360 // Construct MemoryBuffer and update buffer manager.
4361 PCMCache.addBuffer(OutputFile,
4362 llvm::MemoryBuffer::getMemBufferCopy(
4363 StringRef(Buffer.begin(), Buffer.size())));
4364 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004365 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004366}
4367
Douglas Gregora94a1542011-07-27 21:45:57 +00004368template<typename Vector>
4369static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4370 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004371 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4372 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004373 Writer.AddDeclRef(*I, Record);
4374 }
4375}
4376
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004377ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4378 const std::string &OutputFile,
4379 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004380 using namespace llvm;
4381
Craig Toppera13603a2014-05-22 05:54:18 +00004382 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004383
Douglas Gregorcf68c582011-12-01 22:20:10 +00004384 // Make sure that the AST reader knows to finalize itself.
4385 if (Chain)
4386 Chain->finalizeForWriting();
4387
Sebastian Redl143413f2010-07-12 22:02:52 +00004388 ASTContext &Context = SemaRef.Context;
4389 Preprocessor &PP = SemaRef.PP;
4390
Douglas Gregordab42432011-08-12 00:15:20 +00004391 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004392 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4393 if (D) {
4394 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4395 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004396 }
4397 };
4398 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4399 PREDEF_DECL_TRANSLATION_UNIT_ID);
4400 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4401 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4402 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4403 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4404 PREDEF_DECL_OBJC_PROTOCOL_ID);
4405 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4406 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4407 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4408 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4409 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004410 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004411 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4412 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004413 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004414 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4415 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004416 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4417 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004418 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4419 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004420 RegisterPredefDecl(Context.TypePackElementDecl,
4421 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004422
Chris Lattner0c797362009-09-08 18:19:27 +00004423 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004424 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004425 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004426 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004427 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004428
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004429 // Build a record containing all of the file scoped decls in this file.
4430 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004431 if (!isModule)
4432 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4433 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004434
Douglas Gregor851443c2011-08-12 01:39:19 +00004435 // Build a record containing all of the delegating constructors we still need
4436 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004437 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004438 if (!isModule)
4439 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004440
Douglas Gregor851443c2011-08-12 01:39:19 +00004441 // Write the set of weak, undeclared identifiers. We always write the
4442 // entire table, since later PCH files in a PCH chain are only interested in
4443 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004444 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004445 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4446 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4447 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4448 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4449 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4450 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4451 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004452 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004453
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004454 // Build a record containing all of the ext_vector declarations.
4455 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004456 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004457
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004458 // Build a record containing all of the VTable uses information.
4459 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004460 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004461 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4462 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4463 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4464 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4465 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004466 }
4467
Nico Weber72889432014-09-06 01:25:55 +00004468 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4469 RecordData UnusedLocalTypedefNameCandidates;
4470 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4471 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4472
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004473 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004474 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004475 for (const auto &I : SemaRef.PendingInstantiations) {
4476 AddDeclRef(I.first, PendingInstantiations);
4477 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004478 }
4479 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4480 "There are local ones at end of translation unit!");
4481
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004482 // Build a record containing some declaration references.
4483 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004484 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004485 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4486 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004487 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004488 }
4489
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004490 RecordData CUDASpecialDeclRefs;
4491 if (Context.getcudaConfigureCallDecl()) {
4492 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4493 }
4494
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004495 // Build a record containing all of the known namespaces.
4496 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004497 for (const auto &I : SemaRef.KnownNamespaces) {
4498 if (!I.second)
4499 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004500 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004501
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004502 // Build a record of all used, undefined objects that require definitions.
4503 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004504
4505 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004506 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004507 for (const auto &I : Undefined) {
4508 AddDeclRef(I.first, UndefinedButUsed);
4509 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004510 }
4511
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004512 // Build a record containing all delete-expressions that we would like to
4513 // analyze later in AST.
4514 RecordData DeleteExprsToAnalyze;
4515
4516 for (const auto &DeleteExprsInfo :
4517 SemaRef.getMismatchingDeleteExpressions()) {
4518 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4519 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4520 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4521 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4522 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4523 }
4524 }
4525
Douglas Gregor112b9072012-10-18 05:31:06 +00004526 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004527 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004528
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004529 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004530 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004531
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004532 // This is so that older clang versions, before the introduction
4533 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004534 {
4535 RecordData Record = {VERSION_MAJOR};
4536 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4537 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004538
Douglas Gregor851443c2011-08-12 01:39:19 +00004539 // Create a lexical update block containing all of the declarations in the
4540 // translation unit that do not come from other AST files.
4541 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004542 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4543 for (const auto *D : TU->noload_decls()) {
4544 if (!D->isFromASTFile()) {
4545 NewGlobalKindDeclPairs.push_back(D->getKind());
4546 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4547 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004548 }
4549
David Blaikieb44f0bf2017-01-04 22:36:43 +00004550 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004551 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4552 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004553 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004554 {
4555 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4556 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4557 bytes(NewGlobalKindDeclPairs));
4558 }
4559
Douglas Gregor851443c2011-08-12 01:39:19 +00004560 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004561 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004562 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4563 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004564 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004565 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004566 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004567
4568 // If we have any extern "C" names, write out a visible update for them.
4569 if (Context.ExternCContext)
4570 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004571
4572 // If the translation unit has an anonymous namespace, and we don't already
4573 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004574 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004575 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4576 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004577 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004578 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004579 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004580
Richard Smith5652c0f2014-03-21 01:48:23 +00004581 // Add update records for all mangling numbers and static local numbers.
4582 // These aren't really update records, but this is a convenient way of
4583 // tagging this rare extra data onto the declarations.
4584 for (const auto &Number : Context.MangleNumbers)
4585 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004586 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4587 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004588 for (const auto &Number : Context.StaticLocalNumbers)
4589 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004590 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4591 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004592
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004593 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004594 // an AST file, are registered for serialization. Likewise for template
4595 // specializations added to imported templates.
4596 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004597 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004598 }
4599
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004600 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004601 // serialization, if we're storing decls with identifiers.
4602 if (!WritingModule || !getLangOpts().CPlusPlus) {
4603 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004604 for (const auto &ID : PP.getIdentifierTable()) {
4605 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004606 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4607 IIs.push_back(II);
4608 }
4609 // Sort the identifiers to visit based on their name.
4610 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4611 for (const IdentifierInfo *II : IIs) {
4612 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4613 DEnd = SemaRef.IdResolver.end();
4614 D != DEnd; ++D) {
4615 GetDeclRef(*D);
4616 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004617 }
4618 }
4619
Manman Rena0f31a02016-04-29 19:04:05 +00004620 // For method pool in the module, if it contains an entry for a selector,
4621 // the entry should be complete, containing everything introduced by that
4622 // module and all modules it imports. It's possible that the entry is out of
4623 // date, so we need to pull in the new content here.
4624
4625 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4626 // safe, we copy all selectors out.
4627 llvm::SmallVector<Selector, 256> AllSelectors;
4628 for (auto &SelectorAndID : SelectorIDs)
4629 AllSelectors.push_back(SelectorAndID.first);
4630 for (auto &Selector : AllSelectors)
4631 SemaRef.updateOutOfDateSelector(Selector);
4632
Douglas Gregor5204bde2011-08-02 16:26:37 +00004633 // Form the record of special types.
4634 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004635 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004636 AddTypeRef(Context.getFILEType(), SpecialTypes);
4637 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4638 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4639 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4640 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004641 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004642 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004643
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004644 if (Chain) {
4645 // Write the mapping information describing our module dependencies and how
4646 // each of those modules were mapped into our own offset/ID space, so that
4647 // the reader can build the appropriate mapping to its own offset/ID space.
4648 // The map consists solely of a blob with the following format:
4649 // *(module-name-len:i16 module-name:len*i8
4650 // source-location-offset:i32
4651 // identifier-id:i32
4652 // preprocessed-entity-id:i32
4653 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004654 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004655 // selector-id:i32
4656 // declaration-id:i32
4657 // c++-base-specifiers-id:i32
4658 // type-id:i32)
4659 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004660 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004661 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4662 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004663 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004664 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004665 {
4666 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004667 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004668 using namespace llvm::support;
4669 endian::Writer<little> LE(Out);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004670 StringRef FileName = M.FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004671 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004672 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004673
Ben Langmuir785180e2014-10-20 16:27:30 +00004674 // Note: if a base ID was uint max, it would not be possible to load
4675 // another module after it or have more than one entity inside it.
4676 uint32_t None = std::numeric_limits<uint32_t>::max();
4677
4678 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4679 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4680 if (ShouldWrite)
4681 LE.write<uint32_t>(BaseID);
4682 else
4683 LE.write<uint32_t>(None);
4684 };
4685
Ben Langmuirfe971d92014-08-16 04:54:18 +00004686 // These values should be unique within a chain, since they will be read
4687 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004688 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4689 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4690 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4691 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4692 M.NumPreprocessedEntities);
4693 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4694 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4695 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4696 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004697 }
4698 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004699 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004700 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4701 Buffer.data(), Buffer.size());
4702 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004703
Richard Smithb9eab6d2014-03-20 19:44:17 +00004704 RecordData DeclUpdatesOffsetsRecord;
4705
Richard Smith59442b42014-03-20 20:07:19 +00004706 // Keep writing types, declarations, and declaration update records
4707 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004708 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4709 WriteTypeAbbrevs();
4710 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004711 do {
4712 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4713 while (!DeclTypesToEmit.empty()) {
4714 DeclOrType DOT = DeclTypesToEmit.front();
4715 DeclTypesToEmit.pop();
4716 if (DOT.isType())
4717 WriteType(DOT.getType());
4718 else
4719 WriteDecl(Context, DOT.getDecl());
4720 }
4721 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004722 Stream.ExitBlock();
4723
Richard Smithb9eab6d2014-03-20 19:44:17 +00004724 DoneWritingDeclsAndTypes = true;
4725
4726 // These things can only be done once we've written out decls and types.
4727 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004728 if (!DeclUpdatesOffsetsRecord.empty())
4729 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004730 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004731 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004732 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004733 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004734 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004735 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004736 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004737 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004738 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004739 WriteFPPragmaOptions(SemaRef.getFPOptions());
4740 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004741 WriteOpenCLExtensionTypes(SemaRef);
4742 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004743 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004744
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004745 // If we're emitting a module, write out the submodule information.
4746 if (WritingModule)
4747 WriteSubmodules(WritingModule);
4748
Douglas Gregor5204bde2011-08-02 16:26:37 +00004749 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4750
Douglas Gregord4df8652009-04-22 22:02:47 +00004751 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004752 if (!EagerlyDeserializedDecls.empty())
4753 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004754
David Blaikie9ffe5a32017-01-30 05:00:26 +00004755 if (Context.getLangOpts().ModularCodegen)
4756 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4757
Douglas Gregord4df8652009-04-22 22:02:47 +00004758 // Write the record containing tentative definitions.
4759 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004760 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004761
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004762 // Write the record containing unused file scoped decls.
4763 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004764 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004765
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004766 // Write the record containing weak undeclared identifiers.
4767 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004768 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004769 WeakUndeclaredIdentifiers);
4770
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004771 // Write the record containing ext_vector type names.
4772 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004773 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004774
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004775 // Write the record containing VTable uses information.
4776 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004777 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004778
Nico Weber72889432014-09-06 01:25:55 +00004779 // Write the record containing potentially unused local typedefs.
4780 if (!UnusedLocalTypedefNameCandidates.empty())
4781 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4782 UnusedLocalTypedefNameCandidates);
4783
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004784 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004785 if (!PendingInstantiations.empty())
4786 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004787
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004788 // Write the record containing declaration references of Sema.
4789 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004790 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004791
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004792 // Write the record containing CUDA-specific declaration references.
4793 if (!CUDASpecialDeclRefs.empty())
4794 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004795
4796 // Write the delegating constructors.
4797 if (!DelegatingCtorDecls.empty())
4798 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004799
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004800 // Write the known namespaces.
4801 if (!KnownNamespaces.empty())
4802 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004803
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004804 // Write the undefined internal functions and variables, and inline functions.
4805 if (!UndefinedButUsed.empty())
4806 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004807
4808 if (!DeleteExprsToAnalyze.empty())
4809 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4810
Douglas Gregor851443c2011-08-12 01:39:19 +00004811 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004812 for (auto *DC : UpdatedDeclContexts)
4813 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004814
Douglas Gregor959bb062011-12-03 01:15:29 +00004815 if (!WritingModule) {
4816 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004817 struct ModuleInfo {
4818 uint64_t ID;
4819 Module *M;
4820 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4821 };
Richard Smith56be7542014-03-21 00:33:59 +00004822 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004823 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004824 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004825 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4826 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004827 }
Richard Smith56be7542014-03-21 00:33:59 +00004828
4829 if (!Imports.empty()) {
4830 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4831 return A.ID < B.ID;
4832 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004833 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4834 return A.ID == B.ID;
4835 };
Richard Smith56be7542014-03-21 00:33:59 +00004836
4837 // Sort and deduplicate module IDs.
4838 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004839 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004840 Imports.end());
4841
4842 RecordData ImportedModules;
4843 for (const auto &Import : Imports) {
4844 ImportedModules.push_back(Import.ID);
4845 // FIXME: If the module has macros imported then later has declarations
4846 // imported, this location won't be the right one as a location for the
4847 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004848 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004849 }
4850
Douglas Gregor959bb062011-12-03 01:15:29 +00004851 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4852 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004853 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004854
Douglas Gregor404cdde2012-01-27 01:47:08 +00004855 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004856 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004857 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004858 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004859 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004860 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004861
Douglas Gregor08f01292009-04-17 22:13:46 +00004862 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004863 RecordData::value_type Record[] = {
4864 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004865 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004866 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004867
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004868 // Write the module file extension blocks.
4869 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004870 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004871
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004872 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004873}
4874
Richard Smithb9eab6d2014-03-20 19:44:17 +00004875void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004876 if (DeclUpdates.empty())
4877 return;
4878
Richard Smith59442b42014-03-20 20:07:19 +00004879 DeclUpdateMap LocalUpdates;
4880 LocalUpdates.swap(DeclUpdates);
4881
Richard Smith6ef42932014-03-20 21:02:00 +00004882 for (auto &DeclUpdate : LocalUpdates) {
4883 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004884
Richard Smithd28ac5b2014-03-22 23:33:22 +00004885 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004886 RecordData RecordData;
4887 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004888 for (auto &Update : DeclUpdate.second) {
4889 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4890
Richard Smith69c82bf2016-04-01 22:52:03 +00004891 // An updated body is emitted last, so that the reader doesn't need
4892 // to skip over the lazy body to reach statements for other records.
4893 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4894 HasUpdatedBody = true;
4895 else
4896 Record.push_back(Kind);
4897
Richard Smith6ef42932014-03-20 21:02:00 +00004898 switch (Kind) {
4899 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4900 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4901 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004902 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004903 Record.push_back(GetDeclRef(Update.getDecl()));
4904 break;
4905
Richard Smith4d235792014-08-07 18:53:08 +00004906 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004907 break;
4908
Richard Smith4d235792014-08-07 18:53:08 +00004909 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004910 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004911 break;
4912
John McCall32791cc2016-01-06 22:34:54 +00004913 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004914 Record.AddStmt(const_cast<Expr *>(
4915 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004916 break;
4917
Richard Smith4b054b22016-08-24 21:25:37 +00004918 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4919 Record.AddStmt(
4920 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4921 break;
4922
Richard Smithcd45dbc2014-04-19 03:48:30 +00004923 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4924 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004925 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004926 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004927 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004928 *Context, const_cast<CXXRecordDecl *>(RD)));
4929
4930 // This state is sometimes updated by template instantiation, when we
4931 // switch from the specialization referring to the template declaration
4932 // to it referring to the template definition.
4933 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4934 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004935 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004936 } else {
4937 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4938 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004939 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004940
4941 // The instantiation might have been resolved to a partial
4942 // specialization. If so, record which one.
4943 auto From = Spec->getInstantiatedFrom();
4944 if (auto PartialSpec =
4945 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4946 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004947 Record.AddDeclRef(PartialSpec);
4948 Record.AddTemplateArgumentList(
4949 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004950 } else {
4951 Record.push_back(false);
4952 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004953 }
4954 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004955 Record.AddSourceLocation(RD->getLocation());
4956 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004957 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004958
4959 // Instantiation may change attributes; write them all out afresh.
4960 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004961 if (D->hasAttrs())
4962 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004963
4964 // FIXME: Ensure we don't get here for explicit instantiations.
4965 break;
4966 }
4967
Richard Smithf8134002015-03-10 01:41:22 +00004968 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004969 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004970 break;
4971
Richard Smith564417a2014-03-20 21:47:22 +00004972 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4973 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004974 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4975 Record);
4976 break;
4977
Richard Smith6ef42932014-03-20 21:02:00 +00004978 case UPD_CXX_DEDUCED_RETURN_TYPE:
4979 Record.push_back(GetOrCreateTypeID(Update.getType()));
4980 break;
4981
4982 case UPD_DECL_MARKED_USED:
4983 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004984
4985 case UPD_MANGLING_NUMBER:
4986 case UPD_STATIC_LOCAL_NUMBER:
4987 Record.push_back(Update.getNumber());
4988 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004989
Alexey Bataev97720002014-11-11 04:05:39 +00004990 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004991 Record.AddSourceRange(
4992 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004993 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004994
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004995 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4996 Record.AddSourceRange(
4997 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4998 break;
4999
Richard Smith65ebb4a2015-03-26 04:09:53 +00005000 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005001 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005002 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005003
5004 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005005 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005006 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005007 }
5008 }
5009
Richard Smithd28ac5b2014-03-22 23:33:22 +00005010 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005011 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005012 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005013 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005014 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005015 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005016 }
5017
Richard Smithcd45dbc2014-04-19 03:48:30 +00005018 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005019 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005020 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005021}
5022
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005023void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005024 uint32_t Raw = Loc.getRawEncoding();
5025 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005026}
5027
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005028void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005029 AddSourceLocation(Range.getBegin(), Record);
5030 AddSourceLocation(Range.getEnd(), Record);
5031}
5032
Richard Smithf144b542016-04-08 21:54:32 +00005033void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5034 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005035 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005036 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005037}
5038
Richard Smithf144b542016-04-08 21:54:32 +00005039void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5040 Record->push_back(Value.isUnsigned());
5041 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005042}
5043
Richard Smithf144b542016-04-08 21:54:32 +00005044void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5045 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005046}
5047
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005048void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005049 Record.push_back(getIdentifierRef(II));
5050}
5051
Sebastian Redl539c5062010-08-18 23:57:32 +00005052IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005053 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005054 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005055
Sebastian Redl539c5062010-08-18 23:57:32 +00005056 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005057 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005058 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005059 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005060}
5061
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005062MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005063 // Don't emit builtin macros like __LINE__ to the AST file unless they
5064 // have been redefined by the header (in which case they are not
5065 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005066 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005067 return 0;
5068
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005069 MacroID &ID = MacroIDs[MI];
5070 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005071 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005072 MacroInfoToEmitData Info = { Name, MI, ID };
5073 MacroInfosToEmit.push_back(Info);
5074 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005075 return ID;
5076}
5077
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005078MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005079 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005080 return 0;
5081
5082 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5083 return MacroIDs[MI];
5084}
5085
5086uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005087 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005088}
5089
Richard Smithe64e7452016-04-18 21:54:58 +00005090void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5091 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005092}
5093
Sebastian Redl539c5062010-08-18 23:57:32 +00005094SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005095 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005096 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005097 }
5098
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005099 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005100 if (SID == 0 && Chain) {
5101 // This might trigger a ReadSelector callback, which will set the ID for
5102 // this selector.
5103 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005104 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005105 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005106 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005107 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005108 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005109 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005110 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005111}
5112
Richard Smithe64e7452016-04-18 21:54:58 +00005113void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5114 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005115}
5116
Richard Smith290d8012016-04-06 17:06:00 +00005117void ASTRecordWriter::AddTemplateArgumentLocInfo(
5118 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005119 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005120 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005121 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005122 break;
5123 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005124 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005125 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005126 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005127 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5128 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005129 break;
5130 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005131 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5132 AddSourceLocation(Arg.getTemplateNameLoc());
5133 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005134 break;
John McCall0ad16662009-10-29 08:12:44 +00005135 case TemplateArgument::Null:
5136 case TemplateArgument::Integral:
5137 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005138 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005139 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005140 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005141 break;
5142 }
5143}
5144
Richard Smith290d8012016-04-06 17:06:00 +00005145void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5146 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005147
5148 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5149 bool InfoHasSameExpr
5150 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005151 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005152 if (InfoHasSameExpr)
5153 return; // Avoid storing the same expr twice.
5154 }
Richard Smith290d8012016-04-06 17:06:00 +00005155 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005156}
5157
Richard Smith290d8012016-04-06 17:06:00 +00005158void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005159 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005160 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005161 return;
5162 }
5163
Richard Smith290d8012016-04-06 17:06:00 +00005164 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005165}
5166
Richard Smith290d8012016-04-06 17:06:00 +00005167void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5168 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005169
Richard Smith290d8012016-04-06 17:06:00 +00005170 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005171 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005172 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005173}
5174
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005175void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005176 Record.push_back(GetOrCreateTypeID(T));
5177}
5178
Richard Smith2095ffe2015-03-16 20:11:03 +00005179TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005180 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005181 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5182 if (T.isNull())
5183 return TypeIdx();
5184 assert(!T.getLocalFastQualifiers());
5185
5186 TypeIdx &Idx = TypeIdxs[T];
5187 if (Idx.getIndex() == 0) {
5188 if (DoneWritingDeclsAndTypes) {
5189 assert(0 && "New type seen after serializing all the types to emit!");
5190 return TypeIdx();
5191 }
5192
5193 // We haven't seen this type before. Assign it a new ID and put it
5194 // into the queue of types to emit.
5195 Idx = TypeIdx(NextTypeID++);
5196 DeclTypesToEmit.push(T);
5197 }
5198 return Idx;
5199 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005200}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005201
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005202TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005203 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005204 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5205 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005206 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005207 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005208
Richard Smith2095ffe2015-03-16 20:11:03 +00005209 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5210 assert(I != TypeIdxs.end() && "Type not emitted!");
5211 return I->second;
5212 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005213}
5214
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005215void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005216 Record.push_back(GetDeclRef(D));
5217}
5218
Sebastian Redl539c5062010-08-18 23:57:32 +00005219DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005220 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005221
5222 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005223 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005224 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005225
5226 // If D comes from an AST file, its declaration ID is already known and
5227 // fixed.
5228 if (D->isFromASTFile())
5229 return D->getGlobalID();
5230
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005231 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005232 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005233 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005234 if (DoneWritingDeclsAndTypes) {
5235 assert(0 && "New decl seen after serializing all the decls to emit!");
5236 return 0;
5237 }
5238
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005239 // We haven't seen this declaration before. Give it a new ID and
5240 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005241 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005242 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005243 }
5244
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005245 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005246}
5247
Sebastian Redl539c5062010-08-18 23:57:32 +00005248DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005249 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005250 return 0;
5251
Douglas Gregorb3163e52012-01-05 22:33:30 +00005252 // If D comes from an AST file, its declaration ID is already known and
5253 // fixed.
5254 if (D->isFromASTFile())
5255 return D->getGlobalID();
5256
Douglas Gregore84a9da2009-04-20 20:36:09 +00005257 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5258 return DeclIDs[D];
5259}
5260
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005261void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005262 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005263 assert(D);
5264
5265 SourceLocation Loc = D->getLocation();
5266 if (Loc.isInvalid())
5267 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005268
5269 // We only keep track of the file-level declarations of each file.
5270 if (!D->getLexicalDeclContext()->isFileContext())
5271 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005272 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5273 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005274 if (isa<ParmVarDecl>(D))
5275 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005276
5277 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005278 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005279 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005280 FileID FID;
5281 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005282 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005283 if (FID.isInvalid())
5284 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005285 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005286
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005287 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005288 if (!Info)
5289 Info = new DeclIDInFileInfo();
5290
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005291 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005292 LocDeclIDsTy &Decls = Info->DeclIDs;
5293
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005294 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005295 Decls.push_back(LocDecl);
5296 return;
5297 }
5298
Benjamin Kramer45025c02013-08-24 13:22:59 +00005299 LocDeclIDsTy::iterator I =
5300 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005301
5302 Decls.insert(I, LocDecl);
5303}
5304
Richard Smithe64e7452016-04-18 21:54:58 +00005305void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005306 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005307 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005308 switch (Name.getNameKind()) {
5309 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005310 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005311 break;
5312
5313 case DeclarationName::ObjCZeroArgSelector:
5314 case DeclarationName::ObjCOneArgSelector:
5315 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005316 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005317 break;
5318
5319 case DeclarationName::CXXConstructorName:
5320 case DeclarationName::CXXDestructorName:
5321 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005322 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005323 break;
5324
Richard Smith35845152017-02-07 01:37:30 +00005325 case DeclarationName::CXXDeductionGuideName:
5326 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5327 break;
5328
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005329 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005330 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005331 break;
5332
Alexis Hunt3d221f22009-11-29 07:34:05 +00005333 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005334 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005335 break;
5336
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005337 case DeclarationName::CXXUsingDirective:
5338 // No extra data to emit
5339 break;
5340 }
5341}
Chris Lattnerca025db2010-05-07 21:43:38 +00005342
Richard Smithd08aeb62014-08-28 01:33:39 +00005343unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5344 assert(needsAnonymousDeclarationNumber(D) &&
5345 "expected an anonymous declaration");
5346
5347 // Number the anonymous declarations within this context, if we've not
5348 // already done so.
5349 auto It = AnonymousDeclarationNumbers.find(D);
5350 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005351 auto *DC = D->getLexicalDeclContext();
5352 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5353 AnonymousDeclarationNumbers[ND] = Number;
5354 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005355
5356 It = AnonymousDeclarationNumbers.find(D);
5357 assert(It != AnonymousDeclarationNumbers.end() &&
5358 "declaration not found within its lexical context");
5359 }
5360
5361 return It->second;
5362}
5363
Richard Smith290d8012016-04-06 17:06:00 +00005364void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5365 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005366 switch (Name.getNameKind()) {
5367 case DeclarationName::CXXConstructorName:
5368 case DeclarationName::CXXDestructorName:
5369 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005370 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005371 break;
5372
5373 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005374 AddSourceLocation(SourceLocation::getFromRawEncoding(
5375 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005376 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005377 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005378 break;
5379
5380 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005381 AddSourceLocation(SourceLocation::getFromRawEncoding(
5382 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005383 break;
5384
5385 case DeclarationName::Identifier:
5386 case DeclarationName::ObjCZeroArgSelector:
5387 case DeclarationName::ObjCOneArgSelector:
5388 case DeclarationName::ObjCMultiArgSelector:
5389 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005390 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005391 break;
5392 }
5393}
5394
Richard Smith290d8012016-04-06 17:06:00 +00005395void ASTRecordWriter::AddDeclarationNameInfo(
5396 const DeclarationNameInfo &NameInfo) {
5397 AddDeclarationName(NameInfo.getName());
5398 AddSourceLocation(NameInfo.getLoc());
5399 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005400}
5401
Richard Smith290d8012016-04-06 17:06:00 +00005402void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5403 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5404 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005405 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005406 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005407}
5408
Richard Smithe64e7452016-04-18 21:54:58 +00005409void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005410 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005411 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005412 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005413
5414 // Push each of the NNS's onto a stack for serialization in reverse order.
5415 while (NNS) {
5416 NestedNames.push_back(NNS);
5417 NNS = NNS->getPrefix();
5418 }
5419
Richard Smithe64e7452016-04-18 21:54:58 +00005420 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005421 while(!NestedNames.empty()) {
5422 NNS = NestedNames.pop_back_val();
5423 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005424 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005425 switch (Kind) {
5426 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005427 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005428 break;
5429
5430 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005431 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005432 break;
5433
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005434 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005435 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005436 break;
5437
Chris Lattnerca025db2010-05-07 21:43:38 +00005438 case NestedNameSpecifier::TypeSpec:
5439 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005440 AddTypeRef(QualType(NNS->getAsType(), 0));
5441 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005442 break;
5443
5444 case NestedNameSpecifier::Global:
5445 // Don't need to write an associated value.
5446 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005447
5448 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005449 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005450 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005451 }
5452 }
5453}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005454
Richard Smith290d8012016-04-06 17:06:00 +00005455void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005456 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005457 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005458 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005459
5460 // Push each of the nested-name-specifiers's onto a stack for
5461 // serialization in reverse order.
5462 while (NNS) {
5463 NestedNames.push_back(NNS);
5464 NNS = NNS.getPrefix();
5465 }
5466
Richard Smith290d8012016-04-06 17:06:00 +00005467 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005468 while(!NestedNames.empty()) {
5469 NNS = NestedNames.pop_back_val();
5470 NestedNameSpecifier::SpecifierKind Kind
5471 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005472 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005473 switch (Kind) {
5474 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005475 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5476 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005477 break;
5478
5479 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005480 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5481 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005482 break;
5483
5484 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005485 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5486 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005487 break;
5488
5489 case NestedNameSpecifier::TypeSpec:
5490 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005491 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5492 AddTypeLoc(NNS.getTypeLoc());
5493 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005494 break;
5495
5496 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005497 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005498 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005499
5500 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005501 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5502 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005503 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005504 }
5505 }
5506}
5507
Richard Smith290d8012016-04-06 17:06:00 +00005508void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005509 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005510 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005511 switch (Kind) {
5512 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005513 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005514 break;
5515
5516 case TemplateName::OverloadedTemplate: {
5517 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005518 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005519 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005520 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005521 break;
5522 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005523
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005524 case TemplateName::QualifiedTemplate: {
5525 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005526 AddNestedNameSpecifier(QualT->getQualifier());
5527 Record->push_back(QualT->hasTemplateKeyword());
5528 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005529 break;
5530 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005531
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005532 case TemplateName::DependentTemplate: {
5533 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005534 AddNestedNameSpecifier(DepT->getQualifier());
5535 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005536 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005537 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005538 else
Richard Smith290d8012016-04-06 17:06:00 +00005539 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005540 break;
5541 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005542
5543 case TemplateName::SubstTemplateTemplateParm: {
5544 SubstTemplateTemplateParmStorage *subst
5545 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005546 AddDeclRef(subst->getParameter());
5547 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005548 break;
5549 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005550
5551 case TemplateName::SubstTemplateTemplateParmPack: {
5552 SubstTemplateTemplateParmPackStorage *SubstPack
5553 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005554 AddDeclRef(SubstPack->getParameterPack());
5555 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005556 break;
5557 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005558 }
5559}
5560
Richard Smith290d8012016-04-06 17:06:00 +00005561void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5562 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005563 switch (Arg.getKind()) {
5564 case TemplateArgument::Null:
5565 break;
5566 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005567 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005568 break;
5569 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005570 AddDeclRef(Arg.getAsDecl());
5571 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005572 break;
5573 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005574 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005575 break;
5576 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005577 AddAPSInt(Arg.getAsIntegral());
5578 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005579 break;
5580 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005581 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005582 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005583 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005584 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005585 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005586 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005587 else
Richard Smith290d8012016-04-06 17:06:00 +00005588 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005589 break;
5590 case TemplateArgument::Expression:
5591 AddStmt(Arg.getAsExpr());
5592 break;
5593 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005594 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005595 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005596 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005597 break;
5598 }
5599}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005600
Richard Smithe64e7452016-04-18 21:54:58 +00005601void ASTRecordWriter::AddTemplateParameterList(
5602 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005603 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005604 AddSourceLocation(TemplateParams->getTemplateLoc());
5605 AddSourceLocation(TemplateParams->getLAngleLoc());
5606 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005607 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005608 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005609 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005610 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005611}
5612
5613/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005614void ASTRecordWriter::AddTemplateArgumentList(
5615 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005616 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005617 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005618 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005619 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005620}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005621
Richard Smith290d8012016-04-06 17:06:00 +00005622void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5623 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005624 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005625 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5626 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5627 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005628 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005629 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005630 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005631}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005632
Richard Smithe64e7452016-04-18 21:54:58 +00005633void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5634 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005635 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005636 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005637 AddDeclRef(I.getDecl());
5638 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005639 }
5640}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005641
Richard Smith290d8012016-04-06 17:06:00 +00005642// FIXME: Move this out of the main ASTRecordWriter interface.
5643void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5644 Record->push_back(Base.isVirtual());
5645 Record->push_back(Base.isBaseOfClass());
5646 Record->push_back(Base.getAccessSpecifierAsWritten());
5647 Record->push_back(Base.getInheritConstructors());
5648 AddTypeSourceInfo(Base.getTypeSourceInfo());
5649 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005650 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005651 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005652}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005653
Richard Smith645d2cf2016-04-14 00:29:55 +00005654static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5655 ArrayRef<CXXBaseSpecifier> Bases) {
5656 ASTWriter::RecordData Record;
5657 ASTRecordWriter Writer(W, Record);
5658 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005659
Richard Smith645d2cf2016-04-14 00:29:55 +00005660 for (auto &Base : Bases)
5661 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005662
Richard Smith645d2cf2016-04-14 00:29:55 +00005663 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005664}
5665
Richard Smith290d8012016-04-06 17:06:00 +00005666// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005667void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5668 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5669}
5670
Richard Smithaa165cf2016-04-13 21:57:08 +00005671static uint64_t
5672EmitCXXCtorInitializers(ASTWriter &W,
5673 ArrayRef<CXXCtorInitializer *> CtorInits) {
5674 ASTWriter::RecordData Record;
5675 ASTRecordWriter Writer(W, Record);
5676 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005677
Richard Smithaa165cf2016-04-13 21:57:08 +00005678 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005679 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005680 Writer.push_back(CTOR_INITIALIZER_BASE);
5681 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5682 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005683 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005684 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5685 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005686 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005687 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5688 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005689 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005690 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5691 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005692 }
Francois Pichetd583da02010-12-04 09:14:42 +00005693
Richard Smithaa165cf2016-04-13 21:57:08 +00005694 Writer.AddSourceLocation(Init->getMemberLocation());
5695 Writer.AddStmt(Init->getInit());
5696 Writer.AddSourceLocation(Init->getLParenLoc());
5697 Writer.AddSourceLocation(Init->getRParenLoc());
5698 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005699 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005700 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005701 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005702
5703 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005704}
5705
Richard Smithaa165cf2016-04-13 21:57:08 +00005706// FIXME: Move this out of the main ASTRecordWriter interface.
5707void ASTRecordWriter::AddCXXCtorInitializers(
5708 ArrayRef<CXXCtorInitializer *> CtorInits) {
5709 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005710}
5711
Richard Smith290d8012016-04-06 17:06:00 +00005712void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005713 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005714 Record->push_back(Data.IsLambda);
5715 Record->push_back(Data.UserDeclaredConstructor);
5716 Record->push_back(Data.UserDeclaredSpecialMembers);
5717 Record->push_back(Data.Aggregate);
5718 Record->push_back(Data.PlainOldData);
5719 Record->push_back(Data.Empty);
5720 Record->push_back(Data.Polymorphic);
5721 Record->push_back(Data.Abstract);
5722 Record->push_back(Data.IsStandardLayout);
5723 Record->push_back(Data.HasNoNonEmptyBases);
5724 Record->push_back(Data.HasPrivateFields);
5725 Record->push_back(Data.HasProtectedFields);
5726 Record->push_back(Data.HasPublicFields);
5727 Record->push_back(Data.HasMutableFields);
5728 Record->push_back(Data.HasVariantMembers);
5729 Record->push_back(Data.HasOnlyCMembers);
5730 Record->push_back(Data.HasInClassInitializer);
5731 Record->push_back(Data.HasUninitializedReferenceMember);
5732 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005733 Record->push_back(Data.HasInheritedConstructor);
5734 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005735 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5736 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5737 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5738 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5739 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5740 Record->push_back(Data.DefaultedDestructorIsDeleted);
5741 Record->push_back(Data.HasTrivialSpecialMembers);
5742 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5743 Record->push_back(Data.HasIrrelevantDestructor);
5744 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5745 Record->push_back(Data.HasDefaultedDefaultConstructor);
5746 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5747 Record->push_back(Data.HasConstexprDefaultConstructor);
5748 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5749 Record->push_back(Data.ComputedVisibleConversions);
5750 Record->push_back(Data.UserProvidedDefaultConstructor);
5751 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005752 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5753 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005754 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5755 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5756 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieub6adf542017-02-18 02:09:28 +00005757 Record->push_back(Data.ODRHash);
Richard Smith561fb152012-02-25 07:33:38 +00005758 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005759
Richard Smith290d8012016-04-06 17:06:00 +00005760 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005761 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005762 AddCXXBaseSpecifiers(Data.bases());
5763
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005764 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005765 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005766 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005767 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005768
Richard Smith290d8012016-04-06 17:06:00 +00005769 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5770 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005771 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005772 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005773
5774 // Add lambda-specific data.
5775 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005776 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005777 Record->push_back(Lambda.Dependent);
5778 Record->push_back(Lambda.IsGenericLambda);
5779 Record->push_back(Lambda.CaptureDefault);
5780 Record->push_back(Lambda.NumCaptures);
5781 Record->push_back(Lambda.NumExplicitCaptures);
5782 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005783 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005784 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005785 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005786 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005787 AddSourceLocation(Capture.getLocation());
5788 Record->push_back(Capture.isImplicit());
5789 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005790 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005791 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005792 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005793 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005794 break;
5795 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005796 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005797 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005798 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005799 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005800 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005801 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005802 break;
5803 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005804 }
5805 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005806}
5807
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005808void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005809 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005810 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005811 assert(FirstDeclID == NextDeclID &&
5812 FirstTypeID == NextTypeID &&
5813 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005814 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005815 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005816 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005817 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005818
Sebastian Redl07a89a82010-07-30 00:29:29 +00005819 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005820
Richard Smith7f330cd2015-03-18 01:42:29 +00005821 // Note, this will get called multiple times, once one the reader starts up
5822 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005823 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5824 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5825 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005826 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005827 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005828 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005829 NextDeclID = FirstDeclID;
5830 NextTypeID = FirstTypeID;
5831 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005832 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005833 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005834 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005835}
5836
Sebastian Redl539c5062010-08-18 23:57:32 +00005837void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005838 // Always keep the highest ID. See \p TypeRead() for more information.
5839 IdentID &StoredID = IdentifierIDs[II];
5840 if (ID > StoredID)
5841 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005842}
5843
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005844void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005845 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005846 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005847 if (ID > StoredID)
5848 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005849}
5850
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005851void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005852 // Always take the highest-numbered type index. This copes with an interesting
5853 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005854 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005855 // keep the higher-numbered entry so that we can properly write it out to
5856 // the AST file.
5857 TypeIdx &StoredIdx = TypeIdxs[T];
5858 if (Idx.getIndex() >= StoredIdx.getIndex())
5859 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005860}
5861
Sebastian Redl539c5062010-08-18 23:57:32 +00005862void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005863 // Always keep the highest ID. See \p TypeRead() for more information.
5864 SelectorID &StoredID = SelectorIDs[S];
5865 if (ID > StoredID)
5866 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005867}
Douglas Gregor91096292010-10-02 19:29:26 +00005868
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005869void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005870 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005871 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005872 MacroDefinitions[MD] = ID;
5873}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005874
Douglas Gregore37a85a2011-12-02 17:30:13 +00005875void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5876 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5877 SubmoduleIDs[Mod] = ID;
5878}
5879
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005880void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005881 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005882 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005883 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005884 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005885 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005886 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005887 // A forward reference was mutated into a definition. Rewrite it.
5888 // FIXME: This happens during template instantiation, should we
5889 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005890 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5891 "completed a tag from another module but not by instantiation?");
5892 DeclUpdates[RD].push_back(
5893 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005894 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005895 }
5896}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005897
Richard Smithf983f7f2015-10-13 00:23:25 +00005898static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5899 if (D->isFromASTFile())
5900 return true;
5901
Richard Smithf983f7f2015-10-13 00:23:25 +00005902 // The predefined __va_list_tag struct is imported if we imported any decls.
5903 // FIXME: This is a gross hack.
5904 return D == D->getASTContext().getVaListTagDecl();
5905}
5906
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005907void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005908 if (Chain && Chain->isProcessingUpdateRecords()) return;
5909 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005910 "Should not add lookup results to non-lookup contexts!");
5911
Vassil Vassilev632eac32016-03-16 11:17:04 +00005912 // TU is handled elsewhere.
5913 if (isa<TranslationUnitDecl>(DC))
5914 return;
5915
5916 // Namespaces are handled elsewhere, except for template instantiations of
5917 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5918 // local instantiations are added to an imported context. Only happens when
5919 // adding ADL lookup candidates, for example templated friends.
5920 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5921 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005922 return;
5923
Richard Smithf983f7f2015-10-13 00:23:25 +00005924 // We're only interested in cases where a local declaration is added to an
5925 // imported context.
5926 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5927 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005928
Richard Smith0f4e2c42015-08-06 04:23:48 +00005929 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005930 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005931 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005932 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5933 // We're adding a visible declaration to a predefined decl context. Ensure
5934 // that we write out all of its lookup results so we don't get a nasty
5935 // surprise when we try to emit its lookup table.
5936 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005937 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005938 }
Richard Smithc26d9742016-10-06 20:30:51 +00005939 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005940}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005941
5942void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005943 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005944 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005945
5946 // We're only interested in cases where a local declaration is added to an
5947 // imported context.
5948 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5949 return;
5950
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005951 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005952 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005953
5954 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005955 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005956 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005957 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005958}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005959
Richard Smith564417a2014-03-20 21:47:22 +00005960void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005961 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005962 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5963 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005964 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005965 // If we don't already know the exception specification for this redecl
5966 // chain, add an update record for it.
5967 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5968 ->getType()
5969 ->castAs<FunctionProtoType>()
5970 ->getExceptionSpecType()))
5971 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5972 });
Richard Smith564417a2014-03-20 21:47:22 +00005973}
5974
Richard Smith1fa5d642013-05-11 05:45:24 +00005975void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005976 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005977 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005978 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005979 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005980 DeclUpdates[D].push_back(
5981 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5982 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005983}
5984
Richard Smithf8134002015-03-10 01:41:22 +00005985void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5986 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005987 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005988 assert(!WritingAST && "Already writing the AST!");
5989 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005990 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005991 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005992 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5993 });
Richard Smithf8134002015-03-10 01:41:22 +00005994}
5995
Sebastian Redlab238a72011-04-24 16:28:06 +00005996void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005997 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005998 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005999 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006000 return; // Declaration not imported from PCH.
6001
Richard Smith4d235792014-08-07 18:53:08 +00006002 // Implicit function decl from a PCH was defined.
6003 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006004}
6005
Richard Smithd28ac5b2014-03-22 23:33:22 +00006006void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006007 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006008 assert(!WritingAST && "Already writing the AST!");
6009 if (!D->isFromASTFile())
6010 return;
6011
Richard Smith9e2341d2015-03-23 03:25:59 +00006012 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006013}
6014
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006015void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006016 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006017 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006018 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006019 return;
6020
6021 // Since the actual instantiation is delayed, this really means that we need
6022 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00006023 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00006024 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
6025 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006026}
6027
John McCall32791cc2016-01-06 22:34:54 +00006028void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006029 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006030 assert(!WritingAST && "Already writing the AST!");
6031 if (!D->isFromASTFile())
6032 return;
6033
6034 DeclUpdates[D].push_back(
6035 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6036}
6037
Richard Smith4b054b22016-08-24 21:25:37 +00006038void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6039 assert(!WritingAST && "Already writing the AST!");
6040 if (!D->isFromASTFile())
6041 return;
6042
6043 DeclUpdates[D].push_back(
6044 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6045}
6046
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006047void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6048 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006049 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006050 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006051 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006052 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006053
6054 assert(IFD->getDefinition() && "Category on a class without a definition?");
6055 ObjCClassesWithCategories.insert(
6056 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006057}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006058
Eli Friedman276dd182013-09-05 00:02:25 +00006059void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006060 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006061 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006062
6063 // If there is *any* declaration of the entity that's not from an AST file,
6064 // we can skip writing the update record. We make sure that isUsed() triggers
6065 // completion of the redeclaration chain of the entity.
6066 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6067 if (IsLocalDecl(Prev))
6068 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006069
Aaron Ballman4f45b712014-03-21 15:22:56 +00006070 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006071}
Alexey Bataev97720002014-11-11 04:05:39 +00006072
6073void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006074 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006075 assert(!WritingAST && "Already writing the AST!");
6076 if (!D->isFromASTFile())
6077 return;
6078
6079 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6080}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006081
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006082void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6083 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006084 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006085 assert(!WritingAST && "Already writing the AST!");
6086 if (!D->isFromASTFile())
6087 return;
6088
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006089 DeclUpdates[D].push_back(
6090 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006091}
6092
Richard Smith4caa4492015-05-15 02:34:32 +00006093void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006094 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006095 assert(!WritingAST && "Already writing the AST!");
6096 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006097 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006098}
Alex Denisovfde64952015-06-26 05:28:36 +00006099
6100void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6101 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006102 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006103 assert(!WritingAST && "Already writing the AST!");
6104 if (!Record->isFromASTFile())
6105 return;
6106 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6107}
Richard Smithc26d9742016-10-06 20:30:51 +00006108
6109void ASTWriter::AddedCXXTemplateSpecialization(
6110 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6111 assert(!WritingAST && "Already writing the AST!");
6112
6113 if (!TD->getFirstDecl()->isFromASTFile())
6114 return;
6115 if (Chain && Chain->isProcessingUpdateRecords())
6116 return;
6117
6118 DeclsToEmitEvenIfUnreferenced.push_back(D);
6119}
6120
6121void ASTWriter::AddedCXXTemplateSpecialization(
6122 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6123 assert(!WritingAST && "Already writing the AST!");
6124
6125 if (!TD->getFirstDecl()->isFromASTFile())
6126 return;
6127 if (Chain && Chain->isProcessingUpdateRecords())
6128 return;
6129
6130 DeclsToEmitEvenIfUnreferenced.push_back(D);
6131}
6132
6133void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6134 const FunctionDecl *D) {
6135 assert(!WritingAST && "Already writing the AST!");
6136
6137 if (!TD->getFirstDecl()->isFromASTFile())
6138 return;
6139 if (Chain && Chain->isProcessingUpdateRecords())
6140 return;
6141
6142 DeclsToEmitEvenIfUnreferenced.push_back(D);
6143}