blob: be1945d5715beb3f483ca426a12ccdefefe81431 [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) {
Alex Lorenz00aee432017-03-22 10:04:48 +0000429 Record.AddTypeRef(T->getElementType());
430 Record.AddStmt(T->getSizeExpr());
431 Record.AddSourceLocation(T->getAttributeLoc());
432 Code = TYPE_DEPENDENT_SIZED_EXT_VECTOR;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000433}
434
435void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000436ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000437 Record.push_back(T->getDepth());
438 Record.push_back(T->getIndex());
439 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000440 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000441 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000442}
443
444void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000445ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000446 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000447 Record.AddNestedNameSpecifier(T->getQualifier());
448 Record.AddIdentifierRef(T->getIdentifier());
449 Record.AddTypeRef(
450 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000451 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000452}
453
454void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000455ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000456 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000457 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000458 Record.AddNestedNameSpecifier(T->getQualifier());
459 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000460 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000461 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000462 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000463 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000464}
465
Douglas Gregord2fa7662010-12-20 02:24:11 +0000466void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000467 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000468 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000469 Record.push_back(*NumExpansions + 1);
470 else
471 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000472 Code = TYPE_PACK_EXPANSION;
473}
474
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000475void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000476 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000477 Code = TYPE_PAREN;
478}
479
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000480void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000481 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000482 Record.AddNestedNameSpecifier(T->getQualifier());
483 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000484 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000485}
486
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000487void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000488 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
489 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000490 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000491}
492
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000493void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000494 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000495 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000496}
497
Manman Rene6be26c2016-09-13 17:25:08 +0000498void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
499 Record.AddDeclRef(T->getDecl());
500 Record.push_back(T->getNumProtocols());
501 for (const auto *I : T->quals())
502 Record.AddDeclRef(I);
503 Code = TYPE_OBJC_TYPE_PARAM;
504}
505
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000506void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000507 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000508 Record.push_back(T->getTypeArgsAsWritten().size());
509 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000510 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000511 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000512 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000513 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000514 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000515 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000516}
517
Steve Narofffb4330f2009-06-17 22:40:22 +0000518void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000519ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000520 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000521 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000522}
523
Eli Friedman0dfb8892011-10-06 23:00:33 +0000524void
525ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000526 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000527 Code = TYPE_ATOMIC;
528}
529
Xiuli Pan9c14e282016-01-09 12:53:17 +0000530void
531ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000532 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000533 Record.push_back(T->isReadOnly());
534 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000535}
536
John McCall8f115c62009-10-16 21:56:05 +0000537namespace {
538
539class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000540 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000541
542public:
Richard Smith290d8012016-04-06 17:06:00 +0000543 TypeLocWriter(ASTRecordWriter &Record)
544 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000545
John McCall17001972009-10-18 01:05:36 +0000546#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000547#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000548 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000549#include "clang/AST/TypeLocNodes.def"
550
John McCall17001972009-10-18 01:05:36 +0000551 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
552 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000553};
554
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000555} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000556
John McCall17001972009-10-18 01:05:36 +0000557void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
558 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000559}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000560
John McCall17001972009-10-18 01:05:36 +0000561void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000562 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000563 if (TL.needsExtraLocalData()) {
564 Record.push_back(TL.getWrittenTypeSpec());
565 Record.push_back(TL.getWrittenSignSpec());
566 Record.push_back(TL.getWrittenWidthSpec());
567 Record.push_back(TL.hasModeAttr());
568 }
John McCall8f115c62009-10-16 21:56:05 +0000569}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000570
John McCall17001972009-10-18 01:05:36 +0000571void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000572 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000573}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000574
John McCall17001972009-10-18 01:05:36 +0000575void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000576 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000577}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000578
Reid Kleckner8a365022013-06-24 17:51:48 +0000579void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
580 // nothing to do
581}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000582
Reid Kleckner0503a872013-12-05 01:23:43 +0000583void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
584 // nothing to do
585}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000586
John McCall17001972009-10-18 01:05:36 +0000587void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000588 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000589}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000590
John McCall17001972009-10-18 01:05:36 +0000591void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000592 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000593}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000594
John McCall17001972009-10-18 01:05:36 +0000595void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000596 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000597}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000598
John McCall17001972009-10-18 01:05:36 +0000599void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000600 Record.AddSourceLocation(TL.getStarLoc());
601 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000602}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000603
John McCall17001972009-10-18 01:05:36 +0000604void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000605 Record.AddSourceLocation(TL.getLBracketLoc());
606 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000607 Record.push_back(TL.getSizeExpr() ? 1 : 0);
608 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000609 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000610}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000611
John McCall17001972009-10-18 01:05:36 +0000612void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
613 VisitArrayTypeLoc(TL);
614}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000615
John McCall17001972009-10-18 01:05:36 +0000616void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
617 VisitArrayTypeLoc(TL);
618}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000619
John McCall17001972009-10-18 01:05:36 +0000620void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
621 VisitArrayTypeLoc(TL);
622}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000623
John McCall17001972009-10-18 01:05:36 +0000624void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
625 DependentSizedArrayTypeLoc TL) {
626 VisitArrayTypeLoc(TL);
627}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000628
John McCall17001972009-10-18 01:05:36 +0000629void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
630 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000631 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000632}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000633
John McCall17001972009-10-18 01:05:36 +0000634void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000635 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000636}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000637
John McCall17001972009-10-18 01:05:36 +0000638void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000639 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000640}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000641
John McCall17001972009-10-18 01:05:36 +0000642void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000643 Record.AddSourceLocation(TL.getLocalRangeBegin());
644 Record.AddSourceLocation(TL.getLParenLoc());
645 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000646 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000647 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000648 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000649 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000650}
651void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
652 VisitFunctionTypeLoc(TL);
653}
654void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
655 VisitFunctionTypeLoc(TL);
656}
John McCallb96ec562009-12-04 22:46:56 +0000657void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000658 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000659}
John McCall17001972009-10-18 01:05:36 +0000660void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000661 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000662}
Manman Rene6be26c2016-09-13 17:25:08 +0000663void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
664 if (TL.getNumProtocols()) {
665 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
666 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
667 }
668 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
669 Record.AddSourceLocation(TL.getProtocolLoc(i));
670}
John McCall17001972009-10-18 01:05:36 +0000671void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000672 Record.AddSourceLocation(TL.getTypeofLoc());
673 Record.AddSourceLocation(TL.getLParenLoc());
674 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000675}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000676
John McCall17001972009-10-18 01:05:36 +0000677void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000678 Record.AddSourceLocation(TL.getTypeofLoc());
679 Record.AddSourceLocation(TL.getLParenLoc());
680 Record.AddSourceLocation(TL.getRParenLoc());
681 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000682}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000683
John McCall17001972009-10-18 01:05:36 +0000684void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000685 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000686}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000687
Alexis Hunte852b102011-05-24 22:41:36 +0000688void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000689 Record.AddSourceLocation(TL.getKWLoc());
690 Record.AddSourceLocation(TL.getLParenLoc());
691 Record.AddSourceLocation(TL.getRParenLoc());
692 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000693}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000694
Richard Smith30482bc2011-02-20 03:19:35 +0000695void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000696 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000697}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000698
Richard Smith600b5262017-01-26 20:40:47 +0000699void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
700 DeducedTemplateSpecializationTypeLoc TL) {
701 Record.AddSourceLocation(TL.getTemplateNameLoc());
702}
703
John McCall17001972009-10-18 01:05:36 +0000704void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000705 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000706}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000707
John McCall17001972009-10-18 01:05:36 +0000708void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000709 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000710}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000711
John McCall81904512011-01-06 01:58:22 +0000712void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000713 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000714 if (TL.hasAttrOperand()) {
715 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000716 Record.AddSourceLocation(range.getBegin());
717 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000718 }
719 if (TL.hasAttrExprOperand()) {
720 Expr *operand = TL.getAttrExprOperand();
721 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000722 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000723 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000724 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000725 }
726}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000727
John McCall17001972009-10-18 01:05:36 +0000728void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000729 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000730}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000731
John McCallcebee162009-10-18 09:09:24 +0000732void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
733 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000734 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000735}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000736
Douglas Gregorada4b792011-01-14 02:55:32 +0000737void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
738 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000739 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000740}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000741
John McCall17001972009-10-18 01:05:36 +0000742void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
743 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000744 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
745 Record.AddSourceLocation(TL.getTemplateNameLoc());
746 Record.AddSourceLocation(TL.getLAngleLoc());
747 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000748 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000749 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
750 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000751}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000752
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000753void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000754 Record.AddSourceLocation(TL.getLParenLoc());
755 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000756}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000757
Abramo Bagnara6150c882010-05-11 21:36:43 +0000758void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000759 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
760 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000761}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000762
John McCalle78aac42010-03-10 03:28:59 +0000763void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000764 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000765}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000766
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000767void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000768 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
769 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
770 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000771}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000772
John McCallc392f372010-06-11 00:33:02 +0000773void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
774 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000775 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
776 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
777 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
778 Record.AddSourceLocation(TL.getTemplateNameLoc());
779 Record.AddSourceLocation(TL.getLAngleLoc());
780 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000781 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000782 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
783 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000784}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000785
Douglas Gregord2fa7662010-12-20 02:24:11 +0000786void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000787 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000788}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000789
John McCall17001972009-10-18 01:05:36 +0000790void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000791 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000792}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000793
John McCall8b07ec22010-05-15 11:32:37 +0000794void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
795 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000796 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
797 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000798 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000799 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
800 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
801 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000802 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000803 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000804}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000805
John McCallfc93cf92009-10-22 22:37:11 +0000806void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000807 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000808}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000809
Eli Friedman0dfb8892011-10-06 23:00:33 +0000810void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000811 Record.AddSourceLocation(TL.getKWLoc());
812 Record.AddSourceLocation(TL.getLParenLoc());
813 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000814}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000815
Xiuli Pan9c14e282016-01-09 12:53:17 +0000816void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000817 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000818}
John McCall8f115c62009-10-16 21:56:05 +0000819
Richard Smith01b2cb42014-07-26 06:37:51 +0000820void ASTWriter::WriteTypeAbbrevs() {
821 using namespace llvm;
822
David Blaikieb44f0bf2017-01-04 22:36:43 +0000823 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000824
825 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000826 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000827 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
828 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
829 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000830 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000831
832 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000833 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000834 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
835 // FunctionType
836 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
837 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
838 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
839 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
840 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
841 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
842 // FunctionProtoType
843 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
844 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
845 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
846 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
847 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
848 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
849 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
850 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000851 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000852}
853
Chris Lattner19cea4e2009-04-22 05:57:30 +0000854//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000855// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000856//===----------------------------------------------------------------------===//
857
Chris Lattner28fa4e62009-04-26 22:26:21 +0000858static void EmitBlockID(unsigned ID, const char *Name,
859 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000860 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000861 Record.clear();
862 Record.push_back(ID);
863 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
864
865 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000866 if (!Name || Name[0] == 0)
867 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000868 Record.clear();
869 while (*Name)
870 Record.push_back(*Name++);
871 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
872}
873
874static void EmitRecordID(unsigned ID, const char *Name,
875 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000876 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000877 Record.clear();
878 Record.push_back(ID);
879 while (*Name)
880 Record.push_back(*Name++);
881 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000882}
883
884static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000885 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000886#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000887 RECORD(STMT_STOP);
888 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000889 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000890 RECORD(STMT_NULL);
891 RECORD(STMT_COMPOUND);
892 RECORD(STMT_CASE);
893 RECORD(STMT_DEFAULT);
894 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000895 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000896 RECORD(STMT_IF);
897 RECORD(STMT_SWITCH);
898 RECORD(STMT_WHILE);
899 RECORD(STMT_DO);
900 RECORD(STMT_FOR);
901 RECORD(STMT_GOTO);
902 RECORD(STMT_INDIRECT_GOTO);
903 RECORD(STMT_CONTINUE);
904 RECORD(STMT_BREAK);
905 RECORD(STMT_RETURN);
906 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000907 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000908 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000909 RECORD(EXPR_PREDEFINED);
910 RECORD(EXPR_DECL_REF);
911 RECORD(EXPR_INTEGER_LITERAL);
912 RECORD(EXPR_FLOATING_LITERAL);
913 RECORD(EXPR_IMAGINARY_LITERAL);
914 RECORD(EXPR_STRING_LITERAL);
915 RECORD(EXPR_CHARACTER_LITERAL);
916 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000917 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000918 RECORD(EXPR_UNARY_OPERATOR);
919 RECORD(EXPR_SIZEOF_ALIGN_OF);
920 RECORD(EXPR_ARRAY_SUBSCRIPT);
921 RECORD(EXPR_CALL);
922 RECORD(EXPR_MEMBER);
923 RECORD(EXPR_BINARY_OPERATOR);
924 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
925 RECORD(EXPR_CONDITIONAL_OPERATOR);
926 RECORD(EXPR_IMPLICIT_CAST);
927 RECORD(EXPR_CSTYLE_CAST);
928 RECORD(EXPR_COMPOUND_LITERAL);
929 RECORD(EXPR_EXT_VECTOR_ELEMENT);
930 RECORD(EXPR_INIT_LIST);
931 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000932 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000933 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000934 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000935 RECORD(EXPR_VA_ARG);
936 RECORD(EXPR_ADDR_LABEL);
937 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000938 RECORD(EXPR_CHOOSE);
939 RECORD(EXPR_GNU_NULL);
940 RECORD(EXPR_SHUFFLE_VECTOR);
941 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000942 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000943 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000944 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000945 RECORD(EXPR_OBJC_ARRAY_LITERAL);
946 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000947 RECORD(EXPR_OBJC_ENCODE);
948 RECORD(EXPR_OBJC_SELECTOR_EXPR);
949 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
950 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
951 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
952 RECORD(EXPR_OBJC_KVC_REF_EXPR);
953 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000954 RECORD(STMT_OBJC_FOR_COLLECTION);
955 RECORD(STMT_OBJC_CATCH);
956 RECORD(STMT_OBJC_FINALLY);
957 RECORD(STMT_OBJC_AT_TRY);
958 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
959 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000960 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000961 RECORD(STMT_CXX_CATCH);
962 RECORD(STMT_CXX_TRY);
963 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000964 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000965 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000966 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000967 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000968 RECORD(EXPR_CXX_STATIC_CAST);
969 RECORD(EXPR_CXX_DYNAMIC_CAST);
970 RECORD(EXPR_CXX_REINTERPRET_CAST);
971 RECORD(EXPR_CXX_CONST_CAST);
972 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000973 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000974 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000975 RECORD(EXPR_CXX_BOOL_LITERAL);
976 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000977 RECORD(EXPR_CXX_TYPEID_EXPR);
978 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000979 RECORD(EXPR_CXX_THIS);
980 RECORD(EXPR_CXX_THROW);
981 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000982 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000983 RECORD(EXPR_CXX_BIND_TEMPORARY);
984 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
985 RECORD(EXPR_CXX_NEW);
986 RECORD(EXPR_CXX_DELETE);
987 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
988 RECORD(EXPR_EXPR_WITH_CLEANUPS);
989 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
990 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
991 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
992 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
993 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000994 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000995 RECORD(EXPR_CXX_NOEXCEPT);
996 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000997 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
998 RECORD(EXPR_TYPE_TRAIT);
999 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001000 RECORD(EXPR_PACK_EXPANSION);
1001 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001002 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001003 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001004 RECORD(EXPR_FUNCTION_PARM_PACK);
1005 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001006 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001007 RECORD(EXPR_CXX_UUIDOF_EXPR);
1008 RECORD(EXPR_CXX_UUIDOF_TYPE);
1009 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001010#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001011}
Mike Stump11289f42009-09-09 15:08:12 +00001012
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001013void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001014 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001015 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001016
Sebastian Redl539c5062010-08-18 23:57:32 +00001017#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1018#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001019
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001020 // Control Block.
1021 BLOCK(CONTROL_BLOCK);
1022 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001023 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001024 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001025 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001026 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001027 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001028 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001029 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001030 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001031
1032 BLOCK(OPTIONS_BLOCK);
1033 RECORD(LANGUAGE_OPTIONS);
1034 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001035 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001036 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001037 RECORD(PREPROCESSOR_OPTIONS);
1038
Douglas Gregor108cb222012-10-19 00:45:00 +00001039 BLOCK(INPUT_FILES_BLOCK);
1040 RECORD(INPUT_FILE);
1041
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001042 // AST Top-Level Block.
1043 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001044 RECORD(TYPE_OFFSET);
1045 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001046 RECORD(IDENTIFIER_OFFSET);
1047 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001048 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001049 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001050 RECORD(SPECIAL_TYPES);
1051 RECORD(STATISTICS);
1052 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001053 RECORD(SELECTOR_OFFSETS);
1054 RECORD(METHOD_POOL);
1055 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001056 RECORD(SOURCE_LOCATION_OFFSETS);
1057 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001058 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001059 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001060 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001061 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001062 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001063 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001064 RECORD(SEMA_DECL_REFS);
1065 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1066 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001067 RECORD(UPDATE_VISIBLE);
1068 RECORD(DECL_UPDATE_OFFSETS);
1069 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001070 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001071 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001072 RECORD(FP_PRAGMA_OPTIONS);
1073 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001074 RECORD(OPENCL_EXTENSION_TYPES);
1075 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001076 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001077 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001078 RECORD(MODULE_OFFSET_MAP);
1079 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001080 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001081 RECORD(FILE_SORTED_DECLS);
1082 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001083 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001084 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001085 RECORD(INTERESTING_IDENTIFIERS);
1086 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001087 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001088 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001089 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001090 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001091 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001092 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001093 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001094
Chris Lattner28fa4e62009-04-26 22:26:21 +00001095 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001096 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001097 RECORD(SM_SLOC_FILE_ENTRY);
1098 RECORD(SM_SLOC_BUFFER_ENTRY);
1099 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001100 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001101 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001102
Chris Lattner28fa4e62009-04-26 22:26:21 +00001103 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001104 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001105 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001106 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001107 RECORD(PP_MACRO_OBJECT_LIKE);
1108 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001109 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001110
Richard Smithfa715652015-08-31 22:43:10 +00001111 // Submodule Block.
1112 BLOCK(SUBMODULE_BLOCK);
1113 RECORD(SUBMODULE_METADATA);
1114 RECORD(SUBMODULE_DEFINITION);
1115 RECORD(SUBMODULE_UMBRELLA_HEADER);
1116 RECORD(SUBMODULE_HEADER);
1117 RECORD(SUBMODULE_TOPHEADER);
1118 RECORD(SUBMODULE_UMBRELLA_DIR);
1119 RECORD(SUBMODULE_IMPORTS);
1120 RECORD(SUBMODULE_EXPORTS);
1121 RECORD(SUBMODULE_REQUIRES);
1122 RECORD(SUBMODULE_EXCLUDED_HEADER);
1123 RECORD(SUBMODULE_LINK_LIBRARY);
1124 RECORD(SUBMODULE_CONFIG_MACRO);
1125 RECORD(SUBMODULE_CONFLICT);
1126 RECORD(SUBMODULE_PRIVATE_HEADER);
1127 RECORD(SUBMODULE_TEXTUAL_HEADER);
1128 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001129 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001130
1131 // Comments Block.
1132 BLOCK(COMMENTS_BLOCK);
1133 RECORD(COMMENTS_RAW_COMMENT);
1134
Douglas Gregor12bfa382009-10-17 00:13:19 +00001135 // Decls and Types block.
1136 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001137 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001138 RECORD(TYPE_COMPLEX);
1139 RECORD(TYPE_POINTER);
1140 RECORD(TYPE_BLOCK_POINTER);
1141 RECORD(TYPE_LVALUE_REFERENCE);
1142 RECORD(TYPE_RVALUE_REFERENCE);
1143 RECORD(TYPE_MEMBER_POINTER);
1144 RECORD(TYPE_CONSTANT_ARRAY);
1145 RECORD(TYPE_INCOMPLETE_ARRAY);
1146 RECORD(TYPE_VARIABLE_ARRAY);
1147 RECORD(TYPE_VECTOR);
1148 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001149 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001150 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001151 RECORD(TYPE_TYPEDEF);
1152 RECORD(TYPE_TYPEOF_EXPR);
1153 RECORD(TYPE_TYPEOF);
1154 RECORD(TYPE_RECORD);
1155 RECORD(TYPE_ENUM);
1156 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001157 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001158 RECORD(TYPE_DECLTYPE);
1159 RECORD(TYPE_ELABORATED);
1160 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1161 RECORD(TYPE_UNRESOLVED_USING);
1162 RECORD(TYPE_INJECTED_CLASS_NAME);
1163 RECORD(TYPE_OBJC_OBJECT);
1164 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1165 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1166 RECORD(TYPE_DEPENDENT_NAME);
1167 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1168 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1169 RECORD(TYPE_PAREN);
1170 RECORD(TYPE_PACK_EXPANSION);
1171 RECORD(TYPE_ATTRIBUTED);
1172 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001173 RECORD(TYPE_AUTO);
1174 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001175 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001176 RECORD(TYPE_DECAYED);
1177 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001178 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001179 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001180 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001181 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001182 RECORD(DECL_ENUM);
1183 RECORD(DECL_RECORD);
1184 RECORD(DECL_ENUM_CONSTANT);
1185 RECORD(DECL_FUNCTION);
1186 RECORD(DECL_OBJC_METHOD);
1187 RECORD(DECL_OBJC_INTERFACE);
1188 RECORD(DECL_OBJC_PROTOCOL);
1189 RECORD(DECL_OBJC_IVAR);
1190 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001191 RECORD(DECL_OBJC_CATEGORY);
1192 RECORD(DECL_OBJC_CATEGORY_IMPL);
1193 RECORD(DECL_OBJC_IMPLEMENTATION);
1194 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1195 RECORD(DECL_OBJC_PROPERTY);
1196 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001197 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001198 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001199 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001200 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001201 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001202 RECORD(DECL_FILE_SCOPE_ASM);
1203 RECORD(DECL_BLOCK);
1204 RECORD(DECL_CONTEXT_LEXICAL);
1205 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001206 RECORD(DECL_NAMESPACE);
1207 RECORD(DECL_NAMESPACE_ALIAS);
1208 RECORD(DECL_USING);
1209 RECORD(DECL_USING_SHADOW);
1210 RECORD(DECL_USING_DIRECTIVE);
1211 RECORD(DECL_UNRESOLVED_USING_VALUE);
1212 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1213 RECORD(DECL_LINKAGE_SPEC);
1214 RECORD(DECL_CXX_RECORD);
1215 RECORD(DECL_CXX_METHOD);
1216 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001217 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001218 RECORD(DECL_CXX_DESTRUCTOR);
1219 RECORD(DECL_CXX_CONVERSION);
1220 RECORD(DECL_ACCESS_SPEC);
1221 RECORD(DECL_FRIEND);
1222 RECORD(DECL_FRIEND_TEMPLATE);
1223 RECORD(DECL_CLASS_TEMPLATE);
1224 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1225 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001226 RECORD(DECL_VAR_TEMPLATE);
1227 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1228 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001229 RECORD(DECL_FUNCTION_TEMPLATE);
1230 RECORD(DECL_TEMPLATE_TYPE_PARM);
1231 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1232 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001233 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001234 RECORD(DECL_STATIC_ASSERT);
1235 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001236 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001237 RECORD(DECL_INDIRECTFIELD);
1238 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001239 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1240 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1241 RECORD(DECL_IMPORT);
1242 RECORD(DECL_OMP_THREADPRIVATE);
1243 RECORD(DECL_EMPTY);
1244 RECORD(DECL_OBJC_TYPE_PARAM);
1245 RECORD(DECL_OMP_CAPTUREDEXPR);
1246 RECORD(DECL_PRAGMA_COMMENT);
1247 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1248 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001249
Douglas Gregor03412ba2011-06-03 02:27:19 +00001250 // Statements and Exprs can occur in the Decls and Types block.
1251 AddStmtsExprs(Stream, Record);
1252
Douglas Gregor92a96f52011-02-08 21:58:10 +00001253 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001254 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001255 RECORD(PPD_MACRO_DEFINITION);
1256 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001257
1258 // Decls and Types block.
1259 BLOCK(EXTENSION_BLOCK);
1260 RECORD(EXTENSION_METADATA);
1261
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001262 BLOCK(UNHASHED_CONTROL_BLOCK);
1263 RECORD(SIGNATURE);
1264 RECORD(DIAGNOSTIC_OPTIONS);
1265 RECORD(DIAG_PRAGMA_MAPPINGS);
1266
Chris Lattner28fa4e62009-04-26 22:26:21 +00001267#undef RECORD
1268#undef BLOCK
1269 Stream.ExitBlock();
1270}
1271
Richard Smith54cc3c22014-12-11 20:50:24 +00001272/// \brief Prepares a path for being written to an AST file by converting it
1273/// to an absolute path and removing nested './'s.
1274///
1275/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001276static bool cleanPathForOutput(FileManager &FileMgr,
1277 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001278 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001279 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001280}
1281
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001282/// \brief Adjusts the given filename to only write out the portion of the
1283/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001284///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001285/// \param Filename the file name to adjust.
1286///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001287/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1288/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001289///
1290/// \returns either the original filename (if it needs no adjustment) or the
1291/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001292static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001293adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001294 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001295
Richard Smith7ed1bc92014-12-05 22:42:13 +00001296 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001297 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001299 // Verify that the filename and the system root have the same prefix.
1300 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001301 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1302 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001303 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001304
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001305 // We hit the end of the filename before we hit the end of the system root.
1306 if (!Filename[Pos])
1307 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001308
Richard Smith7ed1bc92014-12-05 22:42:13 +00001309 // If there's not a path separator at the end of the base directory nor
1310 // immediately after it, then this isn't within the base directory.
1311 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1312 if (!llvm::sys::path::is_separator(BaseDir.back()))
1313 return Filename;
1314 } else {
1315 // If the file name has a '/' at the current position, skip over the '/'.
1316 // We distinguish relative paths from absolute paths by the
1317 // absence of '/' at the beginning of relative paths.
1318 //
1319 // FIXME: This is wrong. We distinguish them by asking if the path is
1320 // absolute, which isn't the same thing. And there might be multiple '/'s
1321 // in a row. Use a better mechanism to indicate whether we have emitted an
1322 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001323 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001324 }
Mike Stump11289f42009-09-09 15:08:12 +00001325
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001326 return Filename + Pos;
1327}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001328
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001329ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1330 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1331 llvm::SHA1 Hasher;
1332 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1333 auto Hash = Hasher.result();
1334
1335 // Convert to an array [5*i32].
1336 ASTFileSignature Signature;
1337 auto LShift = [&](unsigned char Val, unsigned Shift) {
1338 return (uint32_t)Val << Shift;
1339 };
1340 for (int I = 0; I != 5; ++I)
1341 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1342 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1343
1344 return Signature;
1345}
1346
1347ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1348 ASTContext &Context) {
1349 // Flush first to prepare the PCM hash (signature).
1350 Stream.FlushToWord();
1351 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1352
1353 // Enter the block and prepare to write records.
1354 RecordData Record;
1355 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1356
1357 // For implicit modules, write the hash of the PCM as its signature.
1358 ASTFileSignature Signature;
1359 if (WritingModule &&
1360 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1361 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1362 Record.append(Signature.begin(), Signature.end());
1363 Stream.EmitRecord(SIGNATURE, Record);
1364 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001365 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001366
1367 // Diagnostic options.
1368 const auto &Diags = Context.getDiagnostics();
1369 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1370#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1371#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1372 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1373#include "clang/Basic/DiagnosticOptions.def"
1374 Record.push_back(DiagOpts.Warnings.size());
1375 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1376 AddString(DiagOpts.Warnings[I], Record);
1377 Record.push_back(DiagOpts.Remarks.size());
1378 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1379 AddString(DiagOpts.Remarks[I], Record);
1380 // Note: we don't serialize the log or serialization file names, because they
1381 // are generally transient files and will almost always be overridden.
1382 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1383
1384 // Write out the diagnostic/pragma mappings.
1385 WritePragmaDiagnosticMappings(Diags, /* IsModule = */ WritingModule);
1386
1387 // Leave the options block.
1388 Stream.ExitBlock();
1389 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001390}
1391
Douglas Gregor112b9072012-10-18 05:31:06 +00001392/// \brief Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001393void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1394 StringRef isysroot,
1395 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001396 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001397 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001398 RecordData Record;
1399
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001400 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001401 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001402 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1403 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1404 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1405 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1406 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1407 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001408 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001409 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1410 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001411 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001412 assert((!WritingModule || isysroot.empty()) &&
1413 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001414 {
1415 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1416 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1417 !isysroot.empty(), IncludeTimestamps,
1418 ASTHasCompilerErrors};
1419 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1420 getClangFullRepositoryVersion());
1421 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001422 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001423
Richard Smith7ed1bc92014-12-05 22:42:13 +00001424 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001425 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001426 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001428 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001429 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001430 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1431 }
1432
Richard Smith7ed1bc92014-12-05 22:42:13 +00001433 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001434 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001435 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001436
1437 // If the home of the module is the current working directory, then we
1438 // want to pick up the cwd of the build process loading the module, not
1439 // our cwd, when we load this module.
1440 if (!PP.getHeaderSearchInfo()
1441 .getHeaderSearchOpts()
1442 .ModuleMapFileHomeIsCwd ||
1443 WritingModule->Directory->getName() != StringRef(".")) {
1444 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001445 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001446 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001448 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001449
Mehdi Amini57a41912015-09-10 01:46:39 +00001450 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001451 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1452 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001453
1454 // Write out all other paths relative to the base directory if possible.
1455 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1456 } else if (!isysroot.empty()) {
1457 // Write out paths relative to the sysroot if possible.
1458 BaseDirectory = isysroot;
1459 }
1460
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001461 // Module map file
1462 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001463 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001464
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001465 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1466
1467 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001468 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001469
1470 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001471 if (auto *AdditionalModMaps =
1472 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001473 Record.push_back(AdditionalModMaps->size());
1474 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001475 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001476 } else {
1477 Record.push_back(0);
1478 }
1479
1480 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001481 }
1482
Douglas Gregor112b9072012-10-18 05:31:06 +00001483 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001484 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001485 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001486 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001487
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001488 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001489 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001490 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001491 continue;
1492
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001493 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1494 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001495
1496 // If we have calculated signature, there is no need to store
1497 // the size or timestamp.
1498 Record.push_back(M.Signature ? 0 : M.File->getSize());
1499 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1500
1501 for (auto I : M.Signature)
1502 Record.push_back(I);
1503
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001504 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001505 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001506 Stream.EmitRecord(IMPORTS, Record);
1507 }
Mike Stump11289f42009-09-09 15:08:12 +00001508
Richard Smith0516b182015-09-08 19:40:14 +00001509 // Write the options block.
1510 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1511
Douglas Gregor112b9072012-10-18 05:31:06 +00001512 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001513 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001514 const LangOptions &LangOpts = Context.getLangOpts();
1515#define LANGOPT(Name, Bits, Default, Description) \
1516 Record.push_back(LangOpts.Name);
1517#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1518 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001519#include "clang/Basic/LangOptions.def"
1520#define SANITIZER(NAME, ID) \
1521 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001522#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001523
Ben Langmuircd98cb72015-06-23 18:20:18 +00001524 Record.push_back(LangOpts.ModuleFeatures.size());
1525 for (StringRef Feature : LangOpts.ModuleFeatures)
1526 AddString(Feature, Record);
1527
Douglas Gregor112b9072012-10-18 05:31:06 +00001528 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1529 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001530
1531 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001532
1533 // Comment options.
1534 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001535 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1536 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001537 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001538 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001539
Samuel Antaoee8fb302016-01-06 13:42:12 +00001540 // OpenMP offloading options.
1541 Record.push_back(LangOpts.OMPTargetTriples.size());
1542 for (auto &T : LangOpts.OMPTargetTriples)
1543 AddString(T.getTriple(), Record);
1544
1545 AddString(LangOpts.OMPHostIRFile, Record);
1546
Douglas Gregor112b9072012-10-18 05:31:06 +00001547 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1548
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001549 // Target options.
1550 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001551 const TargetInfo &Target = Context.getTargetInfo();
1552 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001553 AddString(TargetOpts.Triple, Record);
1554 AddString(TargetOpts.CPU, Record);
1555 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001556 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1557 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1558 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1559 }
1560 Record.push_back(TargetOpts.Features.size());
1561 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1562 AddString(TargetOpts.Features[I], Record);
1563 }
1564 Stream.EmitRecord(TARGET_OPTIONS, Record);
1565
Douglas Gregorc6317db2012-10-24 15:49:58 +00001566 // File system options.
1567 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001568 const FileSystemOptions &FSOpts =
1569 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001570 AddString(FSOpts.WorkingDir, Record);
1571 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1572
Douglas Gregor2d302362012-10-24 16:50:34 +00001573 // Header search options.
1574 Record.clear();
1575 const HeaderSearchOptions &HSOpts
1576 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1577 AddString(HSOpts.Sysroot, Record);
1578
1579 // Include entries.
1580 Record.push_back(HSOpts.UserEntries.size());
1581 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1582 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1583 AddString(Entry.Path, Record);
1584 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001585 Record.push_back(Entry.IsFramework);
1586 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001587 }
1588
1589 // System header prefixes.
1590 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1591 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1592 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1593 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1594 }
1595
1596 AddString(HSOpts.ResourceDir, Record);
1597 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001598 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001599 Record.push_back(HSOpts.DisableModuleHash);
1600 Record.push_back(HSOpts.UseBuiltinIncludes);
1601 Record.push_back(HSOpts.UseStandardSystemIncludes);
1602 Record.push_back(HSOpts.UseStandardCXXIncludes);
1603 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001604 // Write out the specific module cache path that contains the module files.
1605 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001606 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1607
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001608 // Preprocessor options.
1609 Record.clear();
1610 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1611
1612 // Macro definitions.
1613 Record.push_back(PPOpts.Macros.size());
1614 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1615 AddString(PPOpts.Macros[I].first, Record);
1616 Record.push_back(PPOpts.Macros[I].second);
1617 }
1618
1619 // Includes
1620 Record.push_back(PPOpts.Includes.size());
1621 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1622 AddString(PPOpts.Includes[I], Record);
1623
1624 // Macro includes
1625 Record.push_back(PPOpts.MacroIncludes.size());
1626 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1627 AddString(PPOpts.MacroIncludes[I], Record);
1628
Douglas Gregorb6368752012-10-24 23:41:50 +00001629 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001630 // Detailed record is important since it is used for the module cache hash.
1631 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001632 AddString(PPOpts.ImplicitPCHInclude, Record);
1633 AddString(PPOpts.ImplicitPTHInclude, Record);
1634 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1635 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1636
Richard Smith0516b182015-09-08 19:40:14 +00001637 // Leave the options block.
1638 Stream.ExitBlock();
1639
Douglas Gregora3b20262011-05-06 21:43:30 +00001640 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001641 SourceManager &SM = Context.getSourceManager();
1642 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001643 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001644 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1645 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001646 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001647 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001648
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001649 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001650 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001651 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001652 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001653 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001654
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001655 Record.clear();
1656 Record.push_back(SM.getMainFileID().getOpaqueValue());
1657 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1658
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001659 // Original PCH directory
1660 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001661 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001662 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1663 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001664 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001665
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001666 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001667
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001668 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001669 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1670
Mehdi Amini57a41912015-09-10 01:46:39 +00001671 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001672 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1673 }
1674
Douglas Gregor49491f72013-03-15 22:15:07 +00001675 WriteInputFiles(Context.SourceMgr,
1676 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001677 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001678 Stream.ExitBlock();
1679}
1680
Douglas Gregor49491f72013-03-15 22:15:07 +00001681namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001682
Douglas Gregor49491f72013-03-15 22:15:07 +00001683 /// \brief An input file.
1684 struct InputFileEntry {
1685 const FileEntry *File;
1686 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001687 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001688 bool BufferOverridden;
1689 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001690
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001691} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001692
1693void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1694 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001695 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001696 using namespace llvm;
1697 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001698
Douglas Gregor72be3902012-10-19 00:38:02 +00001699 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001700 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001701 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001702 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001703 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1704 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001705 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001706 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001707 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001708 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001709
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001710 // Get all ContentCache objects for files, sorted by whether the file is a
1711 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001712 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001713 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1714 // Get this source location entry.
1715 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001716 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001717
1718 // We only care about file entries that were not overridden.
1719 if (!SLoc->isFile())
1720 continue;
1721 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001722 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001723 continue;
1724
Douglas Gregor49491f72013-03-15 22:15:07 +00001725 InputFileEntry Entry;
1726 Entry.File = Cache->OrigEntry;
1727 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001728 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001729 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001730 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001731 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001732 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001733 SortedFiles.push_front(Entry);
1734 }
1735
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001736 unsigned UserFilesNum = 0;
1737 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001738 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001739 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001740 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001741 if (InputFileID != 0)
1742 continue; // already recorded this file.
1743
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001744 // Record this entry's offset.
1745 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001746
1747 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001748
Douglas Gregor49491f72013-03-15 22:15:07 +00001749 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001750 ++UserFilesNum;
1751
Douglas Gregor72be3902012-10-19 00:38:02 +00001752 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001753 // And whether this file was overridden.
1754 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001755 INPUT_FILE,
1756 InputFileOffsets.size(),
1757 (uint64_t)Entry.File->getSize(),
1758 (uint64_t)getTimestampForOutput(Entry.File),
1759 Entry.BufferOverridden,
1760 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001761
Richard Smith7ed1bc92014-12-05 22:42:13 +00001762 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1763 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001764
Douglas Gregor112b9072012-10-18 05:31:06 +00001765 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001766
1767 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001768 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001769 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1770 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001771 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1772 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001773 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001774 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001775
1776 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001777 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1778 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001779 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001780}
1781
Douglas Gregora7f71a92009-04-10 03:52:48 +00001782//===----------------------------------------------------------------------===//
1783// Source Manager Serialization
1784//===----------------------------------------------------------------------===//
1785
1786/// \brief Create an abbreviation for the SLocEntry that refers to a
1787/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001788static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001789 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001790
David Blaikieb44f0bf2017-01-04 22:36:43 +00001791 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001792 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001797 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1801 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001802 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001803}
1804
1805/// \brief Create an abbreviation for the SLocEntry that refers to a
1806/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001807static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001808 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001809
David Blaikieb44f0bf2017-01-04 22:36:43 +00001810 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001811 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001817 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001818}
1819
1820/// \brief Create an abbreviation for the SLocEntry that refers to a
1821/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001822static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1823 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001824 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001825
David Blaikieb44f0bf2017-01-04 22:36:43 +00001826 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001827 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1828 : SM_SLOC_BUFFER_BLOB));
1829 if (Compressed)
1830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001832 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001833}
1834
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001835/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1836/// expansion.
1837static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001838 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001839
David Blaikieb44f0bf2017-01-04 22:36:43 +00001840 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001841 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001847 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001848}
1849
Douglas Gregor09b69892011-02-10 17:09:37 +00001850namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001851
Douglas Gregor09b69892011-02-10 17:09:37 +00001852 // Trait used for the on-disk hash table of header search information.
1853 class HeaderFileInfoTrait {
1854 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001855 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001856
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001857 // Keep track of the framework names we've used during serialization.
1858 SmallVector<char, 128> FrameworkStringData;
1859 llvm::StringMap<unsigned> FrameworkNameOffset;
1860
Douglas Gregor09b69892011-02-10 17:09:37 +00001861 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001862 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1863 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001864
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001865 struct key_type {
1866 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001867 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001868 };
1869 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001870
1871 typedef HeaderFileInfo data_type;
1872 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001873 typedef unsigned hash_value_type;
1874 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001875
Richard Smithe75ee0f2015-08-17 07:13:32 +00001876 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001877 // The hash is based only on size/time of the file, so that the reader can
1878 // match even when symlinking or excess path elements ("foo/../", "../")
1879 // change the form of the name. However, complete path is still the key.
1880 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001881 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001882 }
1883
1884 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001885 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001886 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001887 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001888 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001889 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001890 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001891 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1892 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1893 DataLen += 4;
1894 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001895 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001896 }
1897
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001898 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001899 using namespace llvm::support;
1900 endian::Writer<little> LE(Out);
1901 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001902 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001903 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001904 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001905 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001906 }
1907
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001908 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001909 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001910 using namespace llvm::support;
1911 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001912 uint64_t Start = Out.tell(); (void)Start;
1913
Richard Smith386bb072015-08-18 23:42:23 +00001914 unsigned char Flags = (Data.isImport << 4)
1915 | (Data.isPragmaOnce << 3)
1916 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001917 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001918 LE.write<uint8_t>(Flags);
1919 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001920
1921 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001922 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001923 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001924 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001925
1926 unsigned Offset = 0;
1927 if (!Data.Framework.empty()) {
1928 // If this header refers into a framework, save the framework name.
1929 llvm::StringMap<unsigned>::iterator Pos
1930 = FrameworkNameOffset.find(Data.Framework);
1931 if (Pos == FrameworkNameOffset.end()) {
1932 Offset = FrameworkStringData.size() + 1;
1933 FrameworkStringData.append(Data.Framework.begin(),
1934 Data.Framework.end());
1935 FrameworkStringData.push_back(0);
1936
1937 FrameworkNameOffset[Data.Framework] = Offset;
1938 } else
1939 Offset = Pos->second;
1940 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001941 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001942
Richard Smith386bb072015-08-18 23:42:23 +00001943 // FIXME: If the header is excluded, we should write out some
1944 // record of that fact.
1945 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1946 if (uint32_t ModID =
1947 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1948 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1949 assert((Value >> 2) == ModID && "overflow in header module info");
1950 LE.write<uint32_t>(Value);
1951 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001952 }
1953
Douglas Gregor09b69892011-02-10 17:09:37 +00001954 assert(Out.tell() - Start == DataLen && "Wrong data length");
1955 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001956
1957 const char *strings_begin() const { return FrameworkStringData.begin(); }
1958 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001959 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001960
Douglas Gregor09b69892011-02-10 17:09:37 +00001961} // end anonymous namespace
1962
1963/// \brief Write the header search block for the list of files that
1964///
1965/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001966void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001967 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001968 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1969
1970 if (FilesByUID.size() > HS.header_file_size())
1971 FilesByUID.resize(HS.header_file_size());
1972
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001973 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001974 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001975 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001976 unsigned NumHeaderSearchEntries = 0;
1977 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1978 const FileEntry *File = FilesByUID[UID];
1979 if (!File)
1980 continue;
1981
Richard Smith386bb072015-08-18 23:42:23 +00001982 // Get the file info. This will load info from the external source if
1983 // necessary. Skip emitting this file if we have no information on it
1984 // as a header file (in which case HFI will be null) or if it hasn't
1985 // changed since it was loaded. Also skip it if it's for a modular header
1986 // from a different module; in that case, we rely on the module(s)
1987 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001988 const HeaderFileInfo *HFI =
1989 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1990 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001991 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001992
Richard Smith7ed1bc92014-12-05 22:42:13 +00001993 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001994 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001995 SmallString<128> FilenameTmp(Filename);
1996 if (PreparePathForOutput(FilenameTmp)) {
1997 // If we performed any translation on the file name at all, we need to
1998 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001999 Filename = StringRef(strdup(FilenameTmp.c_str()));
2000 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00002001 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002002
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002003 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00002004 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002005 ++NumHeaderSearchEntries;
2006 }
2007
2008 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002009 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002010 uint32_t BucketOffset;
2011 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002012 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00002013 llvm::raw_svector_ostream Out(TableData);
2014 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002015 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00002016 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2017 }
2018
2019 // Create a blob abbreviation
2020 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002021
David Blaikieb44f0bf2017-01-04 22:36:43 +00002022 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002023 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002028 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00002029
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002030 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002031 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2032 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002033 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002034 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00002035
2036 // Free all of the strings we had to duplicate.
2037 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002038 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002039}
2040
George Rimarc39f5492017-01-17 15:45:31 +00002041static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2042 unsigned SLocBufferBlobCompressedAbbrv,
2043 unsigned SLocBufferBlobAbbrv) {
2044 typedef ASTWriter::RecordData::value_type RecordDataType;
2045
2046 // Compress the buffer if possible. We expect that almost all PCM
2047 // consumers will not want its contents.
2048 SmallString<0> CompressedBuffer;
2049 if (llvm::zlib::isAvailable()) {
2050 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2051 if (!E) {
2052 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2053 Blob.size() - 1};
2054 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2055 CompressedBuffer);
2056 return;
2057 }
2058 llvm::consumeError(std::move(E));
2059 }
2060
2061 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2062 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2063}
2064
Douglas Gregora7f71a92009-04-10 03:52:48 +00002065/// \brief Writes the block containing the serialized form of the
2066/// source manager.
2067///
2068/// TODO: We should probably use an on-disk hash table (stored in a
2069/// blob), indexed based on the file name, so that we only create
2070/// entries for files that we actually need. In the common case (no
2071/// errors), we probably won't have to create file entries for any of
2072/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002073void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002074 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002075 RecordData Record;
2076
Chris Lattner0910e3b2009-04-10 17:16:57 +00002077 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002078 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002079
2080 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002081 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2082 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002083 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2084 unsigned SLocBufferBlobCompressedAbbrv =
2085 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002086 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002087
Douglas Gregor258ae542009-04-27 06:38:32 +00002088 // Write out the source location entry table. We skip the first
2089 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002090 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002091 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002092 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2093 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002094 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002095 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002096 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002097 FileID FID = FileID::get(I);
2098 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002099
Douglas Gregor258ae542009-04-27 06:38:32 +00002100 // Record the offset of this source-location entry.
2101 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2102
2103 // Figure out which record code to use.
2104 unsigned Code;
2105 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002106 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2107 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002108 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002109 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002110 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002111 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002112 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002113 Record.clear();
2114 Record.push_back(Code);
2115
Douglas Gregor925296b2011-07-19 16:10:42 +00002116 // Starting offset of this entry within this module, so skip the dummy.
2117 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002118 if (SLoc->isFile()) {
2119 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002120 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002121 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2122 Record.push_back(File.hasLineDirectives());
2123
2124 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002125 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002126 if (Content->OrigEntry) {
2127 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002128 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002129
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002130 // The source location entry is a file. Emit input file ID.
2131 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2132 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002133
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002134 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002135
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002136 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002137 if (FDI != FileDeclIDs.end()) {
2138 Record.push_back(FDI->second->FirstDeclIndex);
2139 Record.push_back(FDI->second->DeclIDs.size());
2140 } else {
2141 Record.push_back(0);
2142 Record.push_back(0);
2143 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002144
2145 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2146
Richard Smithaada85c2016-02-06 02:06:43 +00002147 if (Content->BufferOverridden || Content->IsTransient)
2148 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002149 } else {
2150 // The source location entry is a buffer. The blob associated
2151 // with this entry contains the contents of the buffer.
2152
2153 // We add one to the size so that we capture the trailing NULL
2154 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2155 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002156 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002157 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002158 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002159 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002160 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002161 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002162
Mehdi Amini99d1b292016-10-01 16:38:28 +00002163 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002164 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002165 }
Richard Smithaada85c2016-02-06 02:06:43 +00002166
2167 if (EmitBlob) {
2168 // Include the implicit terminating null character in the on-disk buffer
2169 // if we're writing it uncompressed.
2170 const llvm::MemoryBuffer *Buffer =
2171 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2172 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002173 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2174 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002175 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002176 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002177 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002178 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002179 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2180 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2181 AddSourceLocation(Expansion.isMacroArgExpansion()
2182 ? SourceLocation()
2183 : Expansion.getExpansionLocEnd(),
2184 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002185
2186 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002187 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002188 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002189 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002190 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002191 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002192 }
2193 }
2194
Douglas Gregor8f45df52009-04-16 22:23:12 +00002195 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002196
2197 if (SLocEntryOffsets.empty())
2198 return;
2199
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002200 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002201 // table is used for lazily loading source-location information.
2202 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002203
David Blaikieb44f0bf2017-01-04 22:36:43 +00002204 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002205 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002209 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002210 {
2211 RecordData::value_type Record[] = {
2212 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2213 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2214 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2215 bytes(SLocEntryOffsets));
2216 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002217 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002218 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002219 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002220
2221 // Write the line table. It depends on remapping working, so it must come
2222 // after the source location offsets.
2223 if (SourceMgr.hasLineTable()) {
2224 LineTableInfo &LineTable = SourceMgr.getLineTable();
2225
2226 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002227
2228 // Emit the needed file names.
2229 llvm::DenseMap<int, int> FilenameMap;
2230 for (const auto &L : LineTable) {
2231 if (L.first.ID < 0)
2232 continue;
2233 for (auto &LE : L.second) {
2234 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2235 FilenameMap.size())).second)
2236 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2237 }
2238 }
2239 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002240
2241 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002242 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002243 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002244 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002245 continue;
2246
2247 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002248 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002249
2250 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002251 Record.push_back(L.second.size());
2252 for (const auto &LE : L.second) {
2253 Record.push_back(LE.FileOffset);
2254 Record.push_back(LE.LineNo);
2255 Record.push_back(FilenameMap[LE.FilenameID]);
2256 Record.push_back((unsigned)LE.FileKind);
2257 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002258 }
2259 }
Richard Smith63078492015-09-01 07:41:55 +00002260
Douglas Gregor925296b2011-07-19 16:10:42 +00002261 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2262 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002263}
2264
Douglas Gregorc5046832009-04-27 18:38:38 +00002265//===----------------------------------------------------------------------===//
2266// Preprocessor Serialization
2267//===----------------------------------------------------------------------===//
2268
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002269static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2270 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002271 if (MacroInfo *MI = MD->getMacroInfo())
2272 if (MI->isBuiltinMacro())
2273 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002274
2275 if (IsModule) {
2276 SourceLocation Loc = MD->getLocation();
2277 if (Loc.isInvalid())
2278 return true;
2279 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2280 return true;
2281 }
2282
2283 return false;
2284}
2285
Chris Lattnereeffaef2009-04-10 17:15:23 +00002286/// \brief Writes the block containing the serialized form of the
2287/// preprocessor.
2288///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002289void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002290 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2291 if (PPRec)
2292 WritePreprocessorDetail(*PPRec);
2293
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002294 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002295 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002296
Chris Lattner0af3ba12009-04-13 01:29:17 +00002297 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2298 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002299 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002300 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002301 }
2302
2303 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002304 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002305
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002306 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002307 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002308 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002309 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002310
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002311 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002312 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002313
Richard Smithee977932015-05-01 21:22:17 +00002314 // Construct the list of identifiers with macro directives that need to be
2315 // serialized.
2316 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2317 for (auto &Id : PP.getIdentifierTable())
2318 if (Id.second->hadMacroDefinition() &&
2319 (!Id.second->isFromAST() ||
2320 Id.second->hasChangedSinceDeserialization()))
2321 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002322 // Sort the set of macro definitions that need to be serialized by the
2323 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002324 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2325 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002326
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002327 // Emit the macro directives as a list and associate the offset with the
2328 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002329 for (const IdentifierInfo *Name : MacroIdentifiers) {
2330 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002331 auto StartOffset = Stream.GetCurrentBitNo();
2332
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002333 // Emit the macro directives in reverse source order.
2334 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002335 // Once we hit an ignored macro, we're done: the rest of the chain
2336 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002337 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002338 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002339
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002340 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002341 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002342 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002343 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002344 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002345 Record.push_back(VisMD->isPublic());
2346 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002347 }
Richard Smithd7329392015-04-21 21:46:32 +00002348
Richard Smithb8b2ed62015-04-23 18:18:26 +00002349 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002350 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002351 // We write out exported module macros for PCH as well.
2352 auto Leafs = PP.getLeafModuleMacros(Name);
2353 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2354 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2355 while (!Worklist.empty()) {
2356 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002357
Manman Ren33d80292016-05-31 18:19:32 +00002358 // Emit a record indicating this submodule exports this macro.
2359 ModuleMacroRecord.push_back(
2360 getSubmoduleID(Macro->getOwningModule()));
2361 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2362 for (auto *M : Macro->overrides())
2363 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002364
Manman Ren33d80292016-05-31 18:19:32 +00002365 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2366 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002367
Manman Ren33d80292016-05-31 18:19:32 +00002368 // Enqueue overridden macros once we've visited all their ancestors.
2369 for (auto *M : Macro->overrides())
2370 if (++Visits[M] == M->getNumOverridingMacros())
2371 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002372
Manman Ren33d80292016-05-31 18:19:32 +00002373 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002374 }
Richard Smithd7329392015-04-21 21:46:32 +00002375
Richard Smithee977932015-05-01 21:22:17 +00002376 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002377 continue;
2378
Richard Smithd7329392015-04-21 21:46:32 +00002379 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002380 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2381 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002382 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002383
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002384 /// \brief Offsets of each of the macros into the bitstream, indexed by
2385 /// the local macro ID
2386 ///
2387 /// For each identifier that is associated with a macro, this map
2388 /// provides the offset into the bitstream where that macro is
2389 /// defined.
2390 std::vector<uint32_t> MacroOffsets;
2391
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002392 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2393 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2394 MacroInfo *MI = MacroInfosToEmit[I].MI;
2395 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002396
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002397 if (ID < FirstMacroID) {
2398 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2399 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002400 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002401
2402 // Record the local offset of this macro.
2403 unsigned Index = ID - FirstMacroID;
2404 if (Index == MacroOffsets.size())
2405 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2406 else {
2407 if (Index > MacroOffsets.size())
2408 MacroOffsets.resize(Index + 1);
2409
2410 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2411 }
2412
2413 AddIdentifierRef(Name, Record);
2414 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2415 AddSourceLocation(MI->getDefinitionLoc(), Record);
2416 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2417 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002418 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002419 unsigned Code;
2420 if (MI->isObjectLike()) {
2421 Code = PP_MACRO_OBJECT_LIKE;
2422 } else {
2423 Code = PP_MACRO_FUNCTION_LIKE;
2424
2425 Record.push_back(MI->isC99Varargs());
2426 Record.push_back(MI->isGNUVarargs());
2427 Record.push_back(MI->hasCommaPasting());
2428 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002429 for (const IdentifierInfo *Arg : MI->args())
2430 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002431 }
2432
2433 // If we have a detailed preprocessing record, record the macro definition
2434 // ID that corresponds to this macro.
2435 if (PPRec)
2436 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2437
2438 Stream.EmitRecord(Code, Record);
2439 Record.clear();
2440
2441 // Emit the tokens array.
2442 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2443 // Note that we know that the preprocessor does not have any annotation
2444 // tokens in it because they are created by the parser, and thus can't
2445 // be in a macro definition.
2446 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002447 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002448 Stream.EmitRecord(PP_TOKEN, Record);
2449 Record.clear();
2450 }
2451 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002452 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002453
Douglas Gregor92a96f52011-02-08 21:58:10 +00002454 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002455
2456 // Write the offsets table for macro IDs.
2457 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002458
David Blaikieb44f0bf2017-01-04 22:36:43 +00002459 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002460 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2463 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2464
David Blaikieb44f0bf2017-01-04 22:36:43 +00002465 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002466 {
2467 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2468 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2469 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2470 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002471}
2472
2473void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002474 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002475 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002476
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002477 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002478
Douglas Gregor92a96f52011-02-08 21:58:10 +00002479 // Enter the preprocessor block.
2480 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002481
Douglas Gregoraae92242010-03-19 21:51:54 +00002482 // If the preprocessor has a preprocessing record, emit it.
2483 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002484 using namespace llvm;
2485
2486 // Set up the abbreviation for
2487 unsigned InclusionAbbrev = 0;
2488 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002489 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002490 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002491 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2493 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002496 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002497 }
2498
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002499 unsigned FirstPreprocessorEntityID
2500 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2501 + NUM_PREDEF_PP_ENTITY_IDS;
2502 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002503 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002504 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2505 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002506 E != EEnd;
2507 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002508 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002509
Richard Smith66a81862015-05-04 02:25:31 +00002510 PreprocessedEntityOffsets.push_back(
2511 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002512
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002513 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002514 // Record this macro definition's ID.
2515 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002516
Douglas Gregor92a96f52011-02-08 21:58:10 +00002517 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002518 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2519 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002520 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002521
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002522 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002523 Record.push_back(ME->isBuiltinMacro());
2524 if (ME->isBuiltinMacro())
2525 AddIdentifierRef(ME->getName(), Record);
2526 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002527 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002528 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002529 continue;
2530 }
2531
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002532 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002533 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002534 Record.push_back(ID->getFileName().size());
2535 Record.push_back(ID->wasInQuotes());
2536 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002537 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002538 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002539 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002540 // Check that the FileEntry is not null because it was not resolved and
2541 // we create a PCH even with compiler errors.
2542 if (ID->getFile())
2543 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002544 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002545 continue;
2546 }
2547
2548 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2549 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002550 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002551
Douglas Gregoraae92242010-03-19 21:51:54 +00002552 // Write the offsets table for the preprocessing record.
2553 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002554 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2555
Douglas Gregoraae92242010-03-19 21:51:54 +00002556 // Write the offsets table for identifier IDs.
2557 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002558
David Blaikieb44f0bf2017-01-04 22:36:43 +00002559 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002560 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002563 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002564
Mehdi Amini57a41912015-09-10 01:46:39 +00002565 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2566 FirstPreprocessorEntityID -
2567 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002568 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002569 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002570 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002571}
2572
Richard Smith386bb072015-08-18 23:42:23 +00002573unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002574 if (!Mod)
2575 return 0;
2576
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002577 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2578 if (Known != SubmoduleIDs.end())
2579 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002580
Richard Smithdc1f0422016-07-20 19:10:16 +00002581 auto *Top = Mod->getTopLevelModule();
2582 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002583 (getLangOpts().CompilingPCH ||
2584 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002585 return 0;
2586
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002587 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2588}
2589
Richard Smith386bb072015-08-18 23:42:23 +00002590unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2591 // FIXME: This can easily happen, if we have a reference to a submodule that
2592 // did not result in us loading a module file for that submodule. For
2593 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2594 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2595 assert((ID || !Mod) &&
2596 "asked for module ID for non-local, non-imported module");
2597 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002598}
2599
Douglas Gregor253eefe2011-12-01 00:59:36 +00002600/// \brief Compute the number of modules within the given tree (including the
2601/// given module).
2602static unsigned getNumberOfModules(Module *Mod) {
2603 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002604 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002605 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002606 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002607
2608 return ChildModules + 1;
2609}
2610
Douglas Gregorde3ef502011-11-30 23:21:26 +00002611void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002612 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002613 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002614
2615 // Write the abbreviations needed for the submodules block.
2616 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002617
David Blaikieb44f0bf2017-01-04 22:36:43 +00002618 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002619 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002631 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002632
David Blaikieb44f0bf2017-01-04 22:36:43 +00002633 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002634 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002636 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002637
David Blaikieb44f0bf2017-01-04 22:36:43 +00002638 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002639 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2640 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002641 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002642
David Blaikieb44f0bf2017-01-04 22:36:43 +00002643 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002644 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002646 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002647
David Blaikieb44f0bf2017-01-04 22:36:43 +00002648 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002649 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2650 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002651 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002652
David Blaikieb44f0bf2017-01-04 22:36:43 +00002653 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002654 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002657 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002658
David Blaikieb44f0bf2017-01-04 22:36:43 +00002659 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002660 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2661 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002662 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002663
David Blaikieb44f0bf2017-01-04 22:36:43 +00002664 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002665 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002667 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002668
David Blaikieb44f0bf2017-01-04 22:36:43 +00002669 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002670 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2671 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002672 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002673
David Blaikieb44f0bf2017-01-04 22:36:43 +00002674 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002675 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002677 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002678
David Blaikieb44f0bf2017-01-04 22:36:43 +00002679 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002680 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002683 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002684
David Blaikieb44f0bf2017-01-04 22:36:43 +00002685 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002686 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002688 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002689
David Blaikieb44f0bf2017-01-04 22:36:43 +00002690 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002691 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2692 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2693 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002694 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002695
Douglas Gregor253eefe2011-12-01 00:59:36 +00002696 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002697 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2698 FirstSubmoduleID -
2699 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002700 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2701
Douglas Gregor69021972011-11-30 17:33:56 +00002702 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002703 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002704 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002705 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002706 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002707 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002708 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002709
2710 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002711 if (Mod->Parent) {
2712 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002713 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002714 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002715
2716 // Emit the definition of the block.
2717 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002718 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2719 ID,
2720 ParentID,
2721 Mod->IsFramework,
2722 Mod->IsExplicit,
2723 Mod->IsSystem,
2724 Mod->IsExternC,
2725 Mod->InferSubmodules,
2726 Mod->InferExplicitSubmodules,
2727 Mod->InferExportWildcard,
David Blaikiee6b7c282017-04-11 20:46:34 +00002728 Mod->ConfigMacrosExhaustive};
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
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00002907 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002908}
2909
Douglas Gregorc5046832009-04-27 18:38:38 +00002910//===----------------------------------------------------------------------===//
2911// Type Serialization
2912//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002913
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002914/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002915void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002916 TypeIdx &IdxRef = TypeIdxs[T];
2917 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2918 IdxRef = TypeIdx(NextTypeID++);
2919 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002920
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002921 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002922
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002923 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002924
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002925 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002926 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002927 W.Visit(T);
2928 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002929
Richard Smith290d8012016-04-06 17:06:00 +00002930 // Record the offset for this type.
2931 unsigned Index = Idx.getIndex() - FirstTypeID;
2932 if (TypeOffsets.size() == Index)
2933 TypeOffsets.push_back(Offset);
2934 else if (TypeOffsets.size() < Index) {
2935 TypeOffsets.resize(Index + 1);
2936 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002937 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002938 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002939 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002940}
2941
Douglas Gregorc5046832009-04-27 18:38:38 +00002942//===----------------------------------------------------------------------===//
2943// Declaration Serialization
2944//===----------------------------------------------------------------------===//
2945
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002946/// \brief Write the block containing all of the declaration IDs
2947/// lexically declared within the given DeclContext.
2948///
2949/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2950/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002951uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002952 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002953 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002954 return 0;
2955
Douglas Gregor8f45df52009-04-16 22:23:12 +00002956 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002957 SmallVector<uint32_t, 128> KindDeclPairs;
2958 for (const auto *D : DC->decls()) {
2959 KindDeclPairs.push_back(D->getKind());
2960 KindDeclPairs.push_back(GetDeclRef(D));
2961 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002962
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002963 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002964 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002965 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2966 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002967 return Offset;
2968}
2969
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002970void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002971 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002972
2973 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002974 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002975 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002979 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002980 {
2981 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2982 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2983 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2984 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002985
2986 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002987 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002988 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002992 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002993 {
2994 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2995 FirstDeclID - NUM_PREDEF_DECL_IDS};
2996 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2997 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002998}
2999
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003000void ASTWriter::WriteFileDeclIDsMap() {
3001 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003002
Chandler Carruthb306d732015-03-27 00:31:20 +00003003 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3004 FileDeclIDs.begin(), FileDeclIDs.end());
3005 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3006 llvm::less_first());
3007
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003008 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003009 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3010 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3011 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3012 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3013 for (auto &LocDeclEntry : Info.DeclIDs)
3014 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003015 }
3016
David Blaikieb44f0bf2017-01-04 22:36:43 +00003017 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003018 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003021 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003022 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3023 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003024 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003025}
3026
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003027void ASTWriter::WriteComments() {
3028 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003029 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003030 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003031 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003032 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003033 AddSourceRange(I->getSourceRange(), Record);
3034 Record.push_back(I->getKind());
3035 Record.push_back(I->isTrailingComment());
3036 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003037 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3038 }
3039 Stream.ExitBlock();
3040}
3041
Douglas Gregorc5046832009-04-27 18:38:38 +00003042//===----------------------------------------------------------------------===//
3043// Global Method Pool and Selector Serialization
3044//===----------------------------------------------------------------------===//
3045
Douglas Gregore84a9da2009-04-20 20:36:09 +00003046namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003047
Douglas Gregorc78d3462009-04-24 21:10:55 +00003048// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003049class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003050 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003051
3052public:
3053 typedef Selector key_type;
3054 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003055
Sebastian Redl834bb972010-08-04 17:20:04 +00003056 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003057 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003058 ObjCMethodList Instance, Factory;
3059 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003060 typedef const data_type& data_type_ref;
3061
Justin Bogner25463f12014-04-18 20:27:24 +00003062 typedef unsigned hash_value_type;
3063 typedef unsigned offset_type;
3064
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003065 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003066
Justin Bogner25463f12014-04-18 20:27:24 +00003067 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003068 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003069 }
Mike Stump11289f42009-09-09 15:08:12 +00003070
3071 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003072 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003073 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003074 using namespace llvm::support;
3075 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003076 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003077 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003078 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3079 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003080 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003081 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003082 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003083 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003084 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003085 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003086 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003087 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003088 return std::make_pair(KeyLen, DataLen);
3089 }
Mike Stump11289f42009-09-09 15:08:12 +00003090
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003091 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003092 using namespace llvm::support;
3093 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003094 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003095 assert((Start >> 32) == 0 && "Selector key offset too large");
3096 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003097 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003098 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003099 if (N == 0)
3100 N = 1;
3101 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003102 LE.write<uint32_t>(
3103 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003104 }
Mike Stump11289f42009-09-09 15:08:12 +00003105
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003106 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003107 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003108 using namespace llvm::support;
3109 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003110 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003111 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003112 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003113 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003114 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003115 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003116 ++NumInstanceMethods;
3117
3118 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003119 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003120 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003121 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003122 ++NumFactoryMethods;
3123
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003124 unsigned InstanceBits = Methods.Instance.getBits();
3125 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003126 unsigned InstanceHasMoreThanOneDeclBit =
3127 Methods.Instance.hasMoreThanOneDecl();
3128 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3129 (InstanceHasMoreThanOneDeclBit << 2) |
3130 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003131 unsigned FactoryBits = Methods.Factory.getBits();
3132 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003133 unsigned FactoryHasMoreThanOneDeclBit =
3134 Methods.Factory.hasMoreThanOneDecl();
3135 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3136 (FactoryHasMoreThanOneDeclBit << 2) |
3137 FactoryBits;
3138 LE.write<uint16_t>(FullInstanceBits);
3139 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003140 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003141 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003142 if (Method->getMethod())
3143 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003144 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003145 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003146 if (Method->getMethod())
3147 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003148
3149 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003150 }
3151};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003152
Douglas Gregorc78d3462009-04-24 21:10:55 +00003153} // end anonymous namespace
3154
Sebastian Redla19a67f2010-08-03 21:58:15 +00003155/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003156///
3157/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003158/// in an on-disk hash table indexed by the selector. The hash table also
3159/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003160void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003161 using namespace llvm;
3162
Sebastian Redla19a67f2010-08-03 21:58:15 +00003163 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003164 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003165 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003166 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003167 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003168 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003169 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003170 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003171
Sebastian Redla19a67f2010-08-03 21:58:15 +00003172 // Create the on-disk hash table representation. We walk through every
3173 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003174 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003175 for (auto &SelectorAndID : SelectorIDs) {
3176 Selector S = SelectorAndID.first;
3177 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003178 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003179 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003180 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003181 ObjCMethodList(),
3182 ObjCMethodList()
3183 };
3184 if (F != SemaRef.MethodPool.end()) {
3185 Data.Instance = F->second.first;
3186 Data.Factory = F->second.second;
3187 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003188 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003189 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003190 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003191 // Selector already exists. Did it change?
3192 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003193 for (ObjCMethodList *M = &Data.Instance;
3194 !changed && M && M->getMethod(); M = M->getNext()) {
3195 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003196 changed = true;
3197 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003198 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003199 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003200 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003201 changed = true;
3202 }
3203 if (!changed)
3204 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003205 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003206 // A new method pool entry.
3207 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003208 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003209 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003210 }
3211
Douglas Gregorc78d3462009-04-24 21:10:55 +00003212 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003213 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003214 uint32_t BucketOffset;
3215 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003216 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003217 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003218 llvm::raw_svector_ostream Out(MethodPool);
3219 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003220 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003221 BucketOffset = Generator.Emit(Out, Trait);
3222 }
3223
3224 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003225 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003226 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003230 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003231
Douglas Gregor95c13f52009-04-25 17:48:32 +00003232 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003233 {
3234 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3235 NumTableEntries};
3236 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3237 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003238
3239 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003240 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003241 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003245 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003246
3247 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003248 {
3249 RecordData::value_type Record[] = {
3250 SELECTOR_OFFSETS, SelectorOffsets.size(),
3251 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3252 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3253 bytes(SelectorOffsets));
3254 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003255 }
3256}
3257
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003258/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003259void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003260 using namespace llvm;
3261 if (SemaRef.ReferencedSelectors.empty())
3262 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003263
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003264 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003265 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003266
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003267 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003268 // very tricky to fix, and given that @selector shouldn't really appear in
3269 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003270 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3271 Selector Sel = SelectorAndLocation.first;
3272 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003273 Writer.AddSelectorRef(Sel);
3274 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003275 }
Richard Smithe64e7452016-04-18 21:54:58 +00003276 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003277}
3278
Douglas Gregorc5046832009-04-27 18:38:38 +00003279//===----------------------------------------------------------------------===//
3280// Identifier Table Serialization
3281//===----------------------------------------------------------------------===//
3282
Richard Smithb3761912015-02-05 23:08:52 +00003283/// Determine the declaration that should be put into the name lookup table to
3284/// represent the given declaration in this module. This is usually D itself,
3285/// but if D was imported and merged into a local declaration, we want the most
3286/// recent local declaration instead. The chosen declaration will be the most
3287/// recent declaration in any module that imports this one.
3288static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3289 NamedDecl *D) {
3290 if (!LangOpts.Modules || !D->isFromASTFile())
3291 return D;
3292
3293 if (Decl *Redecl = D->getPreviousDecl()) {
3294 // For Redeclarable decls, a prior declaration might be local.
3295 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003296 // If we find a local decl, we're done.
3297 if (!Redecl->isFromASTFile()) {
3298 // Exception: in very rare cases (for injected-class-names), not all
3299 // redeclarations are in the same semantic context. Skip ones in a
3300 // different context. They don't go in this lookup table at all.
3301 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3302 D->getDeclContext()->getRedeclContext()))
3303 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003304 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003305 }
3306
Richard Smithfe620d22015-03-05 23:24:12 +00003307 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003308 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003309 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003310 break;
3311 }
3312 } else if (Decl *First = D->getCanonicalDecl()) {
3313 // For Mergeable decls, the first decl might be local.
3314 if (!First->isFromASTFile())
3315 return cast<NamedDecl>(First);
3316 }
3317
3318 // All declarations are imported. Our most recent declaration will also be
3319 // the most recent one in anyone who imports us.
3320 return D;
3321}
3322
Douglas Gregorc78d3462009-04-24 21:10:55 +00003323namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003324
Richard Smithcf97cf62015-04-19 01:34:23 +00003325class ASTIdentifierTableTrait {
3326 ASTWriter &Writer;
3327 Preprocessor &PP;
3328 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003329 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003330 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003331 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003332
3333 /// \brief Determines whether this is an "interesting" identifier that needs a
3334 /// full IdentifierInfo structure written into the hash table. Notably, this
3335 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3336 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003337 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003338 if (MacroOffset ||
3339 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003340 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003341 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003342 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003343 return true;
3344
3345 return false;
3346 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003347
Douglas Gregore84a9da2009-04-20 20:36:09 +00003348public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003349 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003350 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003351
Sebastian Redl539c5062010-08-18 23:57:32 +00003352 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003353 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003354
Justin Bogner25463f12014-04-18 20:27:24 +00003355 typedef unsigned hash_value_type;
3356 typedef unsigned offset_type;
3357
Richard Smithd7329392015-04-21 21:46:32 +00003358 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003359 IdentifierResolver &IdResolver, bool IsModule,
3360 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003361 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003362 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3363 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003364
Richard Smithd79514e2016-02-05 19:03:40 +00003365 bool needDecls() const { return NeedDecls; }
3366
Justin Bogner25463f12014-04-18 20:27:24 +00003367 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003368 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003369 }
Mike Stump11289f42009-09-09 15:08:12 +00003370
Richard Smith33e0f7e2015-07-22 02:08:40 +00003371 bool isInterestingIdentifier(const IdentifierInfo *II) {
3372 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3373 return isInterestingIdentifier(II, MacroOffset);
3374 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003375
Richard Smith9c254182015-07-19 21:41:12 +00003376 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3377 return isInterestingIdentifier(II, 0);
3378 }
3379
Mike Stump11289f42009-09-09 15:08:12 +00003380 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003381 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003382 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003383 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003384 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3385 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003386 DataLen += 2; // 2 bytes for builtin ID
3387 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003388 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003389 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003390
Richard Smitha534a312015-07-21 23:54:07 +00003391 if (NeedDecls) {
3392 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3393 DEnd = IdResolver.end();
3394 D != DEnd; ++D)
3395 DataLen += 4;
3396 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003397 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003398 using namespace llvm::support;
3399 endian::Writer<little> LE(Out);
3400
Richard Smithdf8a8312015-03-13 04:05:01 +00003401 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003402 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003403 // We emit the key length after the data length so that every
3404 // string is preceded by a 16-bit length. This matches the PTH
3405 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003406 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003407 return std::make_pair(KeyLen, DataLen);
3408 }
Mike Stump11289f42009-09-09 15:08:12 +00003409
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003410 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003411 unsigned KeyLen) {
3412 // Record the location of the key data. This is used when generating
3413 // the mapping from persistent IDs to strings.
3414 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003415
3416 // Emit the offset of the key/data length information to the interesting
3417 // identifiers table if necessary.
3418 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3419 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3420
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003421 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003422 }
Mike Stump11289f42009-09-09 15:08:12 +00003423
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003424 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003425 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003426 using namespace llvm::support;
3427 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003428
Richard Smithd7329392015-04-21 21:46:32 +00003429 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3430 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003431 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003432 return;
3433 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003434
Justin Bognere1c147c2014-03-28 22:03:19 +00003435 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003436 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3437 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003438 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003439 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003440 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003441 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003442 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3443 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003444 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003445 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003446 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003447 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003448
Richard Smithd7329392015-04-21 21:46:32 +00003449 if (HadMacroDefinition)
3450 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003451
Richard Smitha534a312015-07-21 23:54:07 +00003452 if (NeedDecls) {
3453 // Emit the declaration IDs in reverse order, because the
3454 // IdentifierResolver provides the declarations as they would be
3455 // visible (e.g., the function "stat" would come before the struct
3456 // "stat"), but the ASTReader adds declarations to the end of the list
3457 // (so we need to see the struct "stat" before the function "stat").
3458 // Only emit declarations that aren't from a chained PCH, though.
3459 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3460 IdResolver.end());
3461 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3462 DEnd = Decls.rend();
3463 D != DEnd; ++D)
3464 LE.write<uint32_t>(
3465 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3466 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003467 }
3468};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003469
Douglas Gregore84a9da2009-04-20 20:36:09 +00003470} // end anonymous namespace
3471
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003472/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003473///
3474/// The identifier table consists of a blob containing string data
3475/// (the actual identifiers themselves) and a separate "offsets" index
3476/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003477void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3478 IdentifierResolver &IdResolver,
3479 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003480 using namespace llvm;
3481
Richard Smith33e0f7e2015-07-22 02:08:40 +00003482 RecordData InterestingIdents;
3483
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003484 // Create and write out the blob that contains the identifier
3485 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003486 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003487 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003488 ASTIdentifierTableTrait Trait(
3489 *this, PP, IdResolver, IsModule,
3490 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003491
Douglas Gregore6648fb2009-04-28 20:33:11 +00003492 // Look for any identifiers that were named while processing the
3493 // headers, but are otherwise not needed. We add these to the hash
3494 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003495 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003496 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003497 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003498 for (const auto &ID : PP.getIdentifierTable())
3499 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003500 // Sort the identifiers lexicographically before getting them references so
3501 // that their order is stable.
3502 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3503 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003504 if (Trait.isInterestingNonMacroIdentifier(II))
3505 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003506
Sebastian Redlff4a2952010-07-23 23:49:55 +00003507 // Create the on-disk hash table representation. We only store offsets
3508 // for identifiers that appear here for the first time.
3509 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003510 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003511 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003512 IdentID ID = IdentIDPair.second;
3513 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003514 // Write out identifiers if either the ID is local or the identifier has
3515 // changed since it was loaded.
3516 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3517 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003518 (Trait.needDecls() &&
3519 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003520 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003521 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003522
Douglas Gregore84a9da2009-04-20 20:36:09 +00003523 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003524 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003525 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003526 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003527 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003528 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003529 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003530 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003531 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003532 }
3533
3534 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003535 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003536 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003539 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003540
3541 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003542 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003543 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003544 }
3545
3546 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003547 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003548 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003552 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003553
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003554#ifndef NDEBUG
3555 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3556 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3557#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003558
3559 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3560 IdentifierOffsets.size(),
3561 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003562 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003563 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003564
3565 // In C++, write the list of interesting identifiers (those that are
3566 // defined as macros, poisoned, or similar unusual things).
3567 if (!InterestingIdents.empty())
3568 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003569}
3570
Douglas Gregorc5046832009-04-27 18:38:38 +00003571//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003572// DeclContext's Name Lookup Table Serialization
3573//===----------------------------------------------------------------------===//
3574
3575namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003576
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003577// Trait used for the on-disk hash table used in the method pool.
3578class ASTDeclContextNameLookupTrait {
3579 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003580 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003581
3582public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003583 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003584 typedef key_type key_type_ref;
3585
Richard Smithd88a7f12015-09-01 20:35:42 +00003586 /// A start and end index into DeclIDs, representing a sequence of decls.
3587 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003588 typedef const data_type& data_type_ref;
3589
Justin Bogner25463f12014-04-18 20:27:24 +00003590 typedef unsigned hash_value_type;
3591 typedef unsigned offset_type;
3592
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3594
Richard Smithd88a7f12015-09-01 20:35:42 +00003595 template<typename Coll>
3596 data_type getData(const Coll &Decls) {
3597 unsigned Start = DeclIDs.size();
3598 for (NamedDecl *D : Decls) {
3599 DeclIDs.push_back(
3600 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3601 }
3602 return std::make_pair(Start, DeclIDs.size());
3603 }
3604
3605 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3606 unsigned Start = DeclIDs.size();
3607 for (auto ID : FromReader)
3608 DeclIDs.push_back(ID);
3609 return std::make_pair(Start, DeclIDs.size());
3610 }
3611
3612 static bool EqualKey(key_type_ref a, key_type_ref b) {
3613 return a == b;
3614 }
3615
Richard Smitha06c7e62015-08-26 23:55:49 +00003616 hash_value_type ComputeHash(DeclarationNameKey Name) {
3617 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003618 }
3619
Richard Smithd88a7f12015-09-01 20:35:42 +00003620 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3621 assert(Writer.hasChain() &&
3622 "have reference to loaded module file but no chain?");
3623
3624 using namespace llvm::support;
3625 endian::Writer<little>(Out)
3626 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3627 }
3628
Richard Smitha06c7e62015-08-26 23:55:49 +00003629 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3630 DeclarationNameKey Name,
3631 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003632 using namespace llvm::support;
3633 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003634 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003635 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003636 case DeclarationName::Identifier:
3637 case DeclarationName::ObjCZeroArgSelector:
3638 case DeclarationName::ObjCOneArgSelector:
3639 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003640 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003641 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003642 KeyLen += 4;
3643 break;
3644 case DeclarationName::CXXOperatorName:
3645 KeyLen += 1;
3646 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003647 case DeclarationName::CXXConstructorName:
3648 case DeclarationName::CXXDestructorName:
3649 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003650 case DeclarationName::CXXUsingDirective:
3651 break;
3652 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003653 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003654
Richard Smithf02662d2015-07-30 03:17:16 +00003655 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003656 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3657 assert(uint16_t(DataLen) == DataLen &&
3658 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003659 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003660
3661 return std::make_pair(KeyLen, DataLen);
3662 }
3663
Richard Smitha06c7e62015-08-26 23:55:49 +00003664 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003665 using namespace llvm::support;
3666 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003667 LE.write<uint8_t>(Name.getKind());
3668 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003669 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003670 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003671 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003672 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003673 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003674 case DeclarationName::ObjCZeroArgSelector:
3675 case DeclarationName::ObjCOneArgSelector:
3676 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003677 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003678 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003679 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003680 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003681 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003682 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003683 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003684 case DeclarationName::CXXConstructorName:
3685 case DeclarationName::CXXDestructorName:
3686 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003687 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003688 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003689 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003690
3691 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003692 }
3693
Richard Smitha06c7e62015-08-26 23:55:49 +00003694 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3695 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003696 using namespace llvm::support;
3697 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003698 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003699 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3700 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003701 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3702 }
3703};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003704
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003705} // end anonymous namespace
3706
Chandler Carruthe972c362015-03-26 03:11:40 +00003707bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3708 DeclContext *DC) {
3709 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3710}
3711
3712bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3713 DeclContext *DC) {
3714 for (auto *D : Result.getLookupResult())
3715 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3716 return false;
3717
3718 return true;
3719}
3720
Richard Smithd88a7f12015-09-01 20:35:42 +00003721void
Chandler Carruthe972c362015-03-26 03:11:40 +00003722ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003723 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003724 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3725 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003726 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003727
Chandler Carruthe972c362015-03-26 03:11:40 +00003728 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003729 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003730 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3731
3732 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003733 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3734 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003735 ASTDeclContextNameLookupTrait Trait(*this);
3736
Chandler Carruthe972c362015-03-26 03:11:40 +00003737 // The first step is to collect the declaration names which we need to
3738 // serialize into the name lookup table, and to collect them in a stable
3739 // order.
3740 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003741
Chandler Carruthe972c362015-03-26 03:11:40 +00003742 // We also build up small sets of the constructor and conversion function
3743 // names which are visible.
3744 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003745
Chandler Carruthe972c362015-03-26 03:11:40 +00003746 for (auto &Lookup : *DC->buildLookup()) {
3747 auto &Name = Lookup.first;
3748 auto &Result = Lookup.second;
3749
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003750 // If there are no local declarations in our lookup result, we
3751 // don't need to write an entry for the name at all. If we can't
3752 // write out a lookup set without performing more deserialization,
3753 // just skip this entry.
3754 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003755 isLookupResultEntirelyExternal(Result, DC))
3756 continue;
3757
3758 // We also skip empty results. If any of the results could be external and
3759 // the currently available results are empty, then all of the results are
3760 // external and we skip it above. So the only way we get here with an empty
3761 // results is when no results could have been external *and* we have
3762 // external results.
3763 //
3764 // FIXME: While we might want to start emitting on-disk entries for negative
3765 // lookups into a decl context as an optimization, today we *have* to skip
3766 // them because there are names with empty lookup results in decl contexts
3767 // which we can't emit in any stable ordering: we lookup constructors and
3768 // conversion functions in the enclosing namespace scope creating empty
3769 // results for them. This in almost certainly a bug in Clang's name lookup,
3770 // but that is likely to be hard or impossible to fix and so we tolerate it
3771 // here by omitting lookups with empty results.
3772 if (Lookup.second.getLookupResult().empty())
3773 continue;
3774
3775 switch (Lookup.first.getNameKind()) {
3776 default:
3777 Names.push_back(Lookup.first);
3778 break;
3779
Richard Smithcd45dbc2014-04-19 03:48:30 +00003780 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003781 assert(isa<CXXRecordDecl>(DC) &&
3782 "Cannot have a constructor name outside of a class!");
3783 ConstructorNameSet.insert(Name);
3784 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003785
3786 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003787 assert(isa<CXXRecordDecl>(DC) &&
3788 "Cannot have a conversion function name outside of a class!");
3789 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003790 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003791 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003792 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003793
Chandler Carruthe972c362015-03-26 03:11:40 +00003794 // Sort the names into a stable order.
3795 std::sort(Names.begin(), Names.end());
3796
Chandler Carruthffbf7052015-03-26 22:27:09 +00003797 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003798 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003799 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003800
Chandler Carruthffbf7052015-03-26 22:27:09 +00003801 // First we try the easy case by forming the current context's constructor
3802 // name and adding that name first. This is a very useful optimization to
3803 // avoid walking the lexical declarations in many cases, and it also
3804 // handles the only case where a constructor name can come from some other
3805 // lexical context -- when that name is an implicit constructor merged from
3806 // another declaration in the redecl chain. Any non-implicit constructor or
3807 // conversion function which doesn't occur in all the lexical contexts
3808 // would be an ODR violation.
3809 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3810 Context->getCanonicalType(Context->getRecordType(D)));
3811 if (ConstructorNameSet.erase(ImplicitCtorName))
3812 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003813
Chandler Carruthffbf7052015-03-26 22:27:09 +00003814 // If we still have constructors or conversion functions, we walk all the
3815 // names in the decl and add the constructors and conversion functions
3816 // which are visible in the order they lexically occur within the context.
3817 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3818 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3819 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3820 auto Name = ChildND->getDeclName();
3821 switch (Name.getNameKind()) {
3822 default:
3823 continue;
3824
3825 case DeclarationName::CXXConstructorName:
3826 if (ConstructorNameSet.erase(Name))
3827 Names.push_back(Name);
3828 break;
3829
3830 case DeclarationName::CXXConversionFunctionName:
3831 if (ConversionNameSet.erase(Name))
3832 Names.push_back(Name);
3833 break;
3834 }
3835
3836 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3837 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003838 }
3839
Chandler Carruthe972c362015-03-26 03:11:40 +00003840 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3841 "constructors by walking all the "
3842 "lexical members of the context.");
3843 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3844 "conversion functions by walking all "
3845 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003846 }
3847
Chandler Carruthe972c362015-03-26 03:11:40 +00003848 // Next we need to do a lookup with each name into this decl context to fully
3849 // populate any results from external sources. We don't actually use the
3850 // results of these lookups because we only want to use the results after all
3851 // results have been loaded and the pointers into them will be stable.
3852 for (auto &Name : Names)
3853 DC->lookup(Name);
3854
3855 // Now we need to insert the results for each name into the hash table. For
3856 // constructor names and conversion function names, we actually need to merge
3857 // all of the results for them into one list of results each and insert
3858 // those.
3859 SmallVector<NamedDecl *, 8> ConstructorDecls;
3860 SmallVector<NamedDecl *, 8> ConversionDecls;
3861
3862 // Now loop over the names, either inserting them or appending for the two
3863 // special cases.
3864 for (auto &Name : Names) {
3865 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3866
3867 switch (Name.getNameKind()) {
3868 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003869 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003870 break;
3871
3872 case DeclarationName::CXXConstructorName:
3873 ConstructorDecls.append(Result.begin(), Result.end());
3874 break;
3875
3876 case DeclarationName::CXXConversionFunctionName:
3877 ConversionDecls.append(Result.begin(), Result.end());
3878 break;
3879 }
3880 }
3881
3882 // Handle our two special cases if we ended up having any. We arbitrarily use
3883 // the first declaration's name here because the name itself isn't part of
3884 // the key, only the kind of name is used.
3885 if (!ConstructorDecls.empty())
3886 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003887 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003888 if (!ConversionDecls.empty())
3889 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003890 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003891
Richard Smithd88a7f12015-09-01 20:35:42 +00003892 // Create the on-disk hash table. Also emit the existing imported and
3893 // merged table if there is one.
3894 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3895 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003896}
3897
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003898/// \brief Write the block containing all of the declaration IDs
3899/// visible from the given DeclContext.
3900///
3901/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003902/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003903uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3904 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003905 // If we imported a key declaration of this namespace, write the visible
3906 // lookup results as an update record for it rather than including them
3907 // on this declaration. We will only look at key declarations on reload.
3908 if (isa<NamespaceDecl>(DC) && Chain &&
3909 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3910 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003911 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003912 Prev = Prev->getPreviousDecl())
3913 if (!Prev->isFromASTFile())
3914 return 0;
3915
3916 // Note that we need to emit an update record for the primary context.
3917 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3918
3919 // Make sure all visible decls are written. They will be recorded later. We
3920 // do this using a side data structure so we can sort the names into
3921 // a deterministic order.
3922 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3923 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3924 LookupResults;
3925 if (Map) {
3926 LookupResults.reserve(Map->size());
3927 for (auto &Entry : *Map)
3928 LookupResults.push_back(
3929 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3930 }
3931
3932 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3933 for (auto &NameAndResult : LookupResults) {
3934 DeclarationName Name = NameAndResult.first;
3935 DeclContext::lookup_result Result = NameAndResult.second;
3936 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3937 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3938 // We have to work around a name lookup bug here where negative lookup
3939 // results for these names get cached in namespace lookup tables (these
3940 // names should never be looked up in a namespace).
3941 assert(Result.empty() && "Cannot have a constructor or conversion "
3942 "function name in a namespace!");
3943 continue;
3944 }
3945
3946 for (NamedDecl *ND : Result)
3947 if (!ND->isFromASTFile())
3948 GetDeclRef(ND);
3949 }
3950
3951 return 0;
3952 }
3953
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003954 if (DC->getPrimaryContext() != DC)
3955 return 0;
3956
Chandler Carruthe972c362015-03-26 03:11:40 +00003957 // Skip contexts which don't support name lookup.
3958 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003959 return 0;
3960
3961 // If not in C++, we perform name lookup for the translation unit via the
3962 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003963 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003964 return 0;
3965
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003966 // Serialize the contents of the mapping used for lookup. Note that,
3967 // although we have two very different code paths, the serialized
3968 // representation is the same for both cases: a declaration name,
3969 // followed by a size, followed by references to the visible
3970 // declarations that have that name.
3971 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003972 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003973 if (!Map || Map->empty())
3974 return 0;
3975
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003976 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003977 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003978 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003979
3980 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003981 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003982 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003983 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003984 ++NumVisibleDeclContexts;
3985 return Offset;
3986}
3987
Sebastian Redla4071b42010-08-24 00:50:09 +00003988/// \brief Write an UPDATE_VISIBLE block for the given context.
3989///
3990/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3991/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003992/// (in C++), for namespaces, and for classes with forward-declared unscoped
3993/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003994void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003995 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003996 if (!Map || Map->empty())
3997 return;
3998
Sebastian Redla4071b42010-08-24 00:50:09 +00003999 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004000 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004001 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004002
Richard Smith5fc18a92015-07-12 23:43:21 +00004003 // If we're updating a namespace, select a key declaration as the key for the
4004 // update record; those are the only ones that will be checked on reload.
4005 if (isa<NamespaceDecl>(DC))
4006 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4007
Sebastian Redla4071b42010-08-24 00:50:09 +00004008 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004009 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004010 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004011}
4012
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004013/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4014void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004015 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004016 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4017}
4018
4019/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4020void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004021 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004022 return;
4023
4024 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4025 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004026 for (const auto &I:Opts.OptMap) {
4027 AddString(I.getKey(), Record);
4028 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004029 Record.push_back(V.Supported ? 1 : 0);
4030 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004031 Record.push_back(V.Avail);
4032 Record.push_back(V.Core);
4033 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004034 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4035}
4036
Yaxun Liu5b746652016-12-18 05:18:55 +00004037void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4038 if (!SemaRef.Context.getLangOpts().OpenCL)
4039 return;
4040
4041 RecordData Record;
4042 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4043 Record.push_back(
4044 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4045 Record.push_back(I.second.size());
4046 for (auto Ext : I.second)
4047 AddString(Ext, Record);
4048 }
4049 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4050}
4051
4052void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4053 if (!SemaRef.Context.getLangOpts().OpenCL)
4054 return;
4055
4056 RecordData Record;
4057 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4058 Record.push_back(getDeclID(I.first));
4059 Record.push_back(static_cast<unsigned>(I.second.size()));
4060 for (auto Ext : I.second)
4061 AddString(Ext, Record);
4062 }
4063 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4064}
4065
Justin Lebar67a78a62016-10-08 22:15:58 +00004066void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4067 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4068 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4069 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4070 }
4071}
4072
Douglas Gregor404cdde2012-01-27 01:47:08 +00004073void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004074 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004075 RecordData Categories;
4076
4077 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4078 unsigned Size = 0;
4079 unsigned StartIndex = Categories.size();
4080
4081 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4082
4083 // Allocate space for the size.
4084 Categories.push_back(0);
4085
4086 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004087 for (ObjCInterfaceDecl::known_categories_iterator
4088 Cat = Class->known_categories_begin(),
4089 CatEnd = Class->known_categories_end();
4090 Cat != CatEnd; ++Cat, ++Size) {
4091 assert(getDeclID(*Cat) != 0 && "Bogus category");
4092 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004093 }
4094
4095 // Update the size.
4096 Categories[StartIndex] = Size;
4097
4098 // Record this interface -> category map.
4099 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4100 CategoriesMap.push_back(CatInfo);
4101 }
4102
4103 // Sort the categories map by the definition ID, since the reader will be
4104 // performing binary searches on this information.
4105 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4106
4107 // Emit the categories map.
4108 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004109
David Blaikieb44f0bf2017-01-04 22:36:43 +00004110 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004111 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004114 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004115
4116 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4117 Stream.EmitRecordWithBlob(AbbrevID, Record,
4118 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004119 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004120
Douglas Gregor404cdde2012-01-27 01:47:08 +00004121 // Emit the category lists.
4122 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4123}
4124
Richard Smithe40f2ba2013-08-07 21:41:30 +00004125void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4126 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4127
4128 if (LPTMap.empty())
4129 return;
4130
4131 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004132 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004133 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004134 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004135 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004136 AddDeclRef(LPT.D, Record);
4137 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004138
Justin Lebar28f09c52016-10-10 16:26:08 +00004139 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004140 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004141 }
4142 }
4143 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4144}
4145
Dario Domizioli13a0a382014-05-23 12:13:25 +00004146/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4147void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4148 RecordData Record;
4149 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4150 AddSourceLocation(PragmaLoc, Record);
4151 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4152}
4153
Nico Weber779355f2016-03-02 23:22:00 +00004154/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4155void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4156 RecordData Record;
4157 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4158 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4159}
4160
Nico Weber42932312016-03-03 00:17:35 +00004161/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4162//module.
4163void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4164 RecordData Record;
4165 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4166 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4167 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4168}
4169
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004170/// \brief Write the state of 'pragma pack' at the end of the module.
4171void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4172 RecordData Record;
4173 Record.push_back(SemaRef.PackStack.CurrentValue);
4174 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4175 Record.push_back(SemaRef.PackStack.Stack.size());
4176 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4177 Record.push_back(StackEntry.Value);
4178 AddSourceLocation(StackEntry.PragmaLocation, Record);
4179 AddString(StackEntry.StackSlotLabel, Record);
4180 }
4181 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4182}
4183
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004184void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4185 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004186 // Enter the extension block.
4187 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4188
4189 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004190 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004191 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4192 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4193 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4194 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4195 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4196 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004197 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004198
4199 // Emit the metadata record.
4200 RecordData Record;
4201 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4202 Record.push_back(EXTENSION_METADATA);
4203 Record.push_back(Metadata.MajorVersion);
4204 Record.push_back(Metadata.MinorVersion);
4205 Record.push_back(Metadata.BlockName.size());
4206 Record.push_back(Metadata.UserInfo.size());
4207 SmallString<64> Buffer;
4208 Buffer += Metadata.BlockName;
4209 Buffer += Metadata.UserInfo;
4210 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4211
4212 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004213 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004214
4215 // Exit the extension block.
4216 Stream.ExitBlock();
4217}
4218
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004219//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004220// General Serialization Routines
4221//===----------------------------------------------------------------------===//
4222
Richard Smith69c82bf2016-04-01 22:52:03 +00004223/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004224void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4225 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004226 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004227 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004228 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004229 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004230
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004231#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004232
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004233 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004234}
4235
John McCallf413f5e2013-05-03 00:10:13 +00004236void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4237 AddSourceLocation(Tok.getLocation(), Record);
4238 Record.push_back(Tok.getLength());
4239
4240 // FIXME: When reading literal tokens, reconstruct the literal pointer
4241 // if it is needed.
4242 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4243 // FIXME: Should translate token kind to a stable encoding.
4244 Record.push_back(Tok.getKind());
4245 // FIXME: Should translate token flags to a stable encoding.
4246 Record.push_back(Tok.getFlags());
4247}
4248
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004249void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004250 Record.push_back(Str.size());
4251 Record.insert(Record.end(), Str.begin(), Str.end());
4252}
4253
Richard Smith7ed1bc92014-12-05 22:42:13 +00004254bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004255 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004256
Richard Smith54cc3c22014-12-11 20:50:24 +00004257 bool Changed =
4258 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004259
4260 // Remove a prefix to make the path relative, if relevant.
4261 const char *PathBegin = Path.data();
4262 const char *PathPtr =
4263 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4264 if (PathPtr != PathBegin) {
4265 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4266 Changed = true;
4267 }
4268
4269 return Changed;
4270}
4271
4272void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4273 SmallString<128> FilePath(Path);
4274 PreparePathForOutput(FilePath);
4275 AddString(FilePath, Record);
4276}
4277
Mehdi Amini57a41912015-09-10 01:46:39 +00004278void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004279 StringRef Path) {
4280 SmallString<128> FilePath(Path);
4281 PreparePathForOutput(FilePath);
4282 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4283}
4284
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004285void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4286 RecordDataImpl &Record) {
4287 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004288 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004289 Record.push_back(*Minor + 1);
4290 else
4291 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004292 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004293 Record.push_back(*Subminor + 1);
4294 else
4295 Record.push_back(0);
4296}
4297
Douglas Gregore84a9da2009-04-20 20:36:09 +00004298/// \brief Note that the identifier II occurs at the given offset
4299/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004300void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004301 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004302 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004303 // up earlier in the chain and thus don't need an offset.
4304 if (ID >= FirstIdentID)
4305 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004306}
4307
Douglas Gregor95c13f52009-04-25 17:48:32 +00004308/// \brief Note that the selector Sel occurs at the given offset
4309/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004310void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004311 unsigned ID = SelectorIDs[Sel];
4312 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004313 // Don't record offsets for selectors that are also available in a different
4314 // file.
4315 if (ID < FirstSelectorID)
4316 return;
4317 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004318}
4319
David Blaikie61137e12017-01-05 18:23:18 +00004320ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004321 SmallVectorImpl<char> &Buffer, MemoryBufferCache &PCMCache,
David Blaikie61137e12017-01-05 18:23:18 +00004322 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4323 bool IncludeTimestamps)
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004324 : Stream(Stream), Buffer(Buffer), PCMCache(PCMCache),
4325 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004326 for (const auto &Ext : Extensions) {
4327 if (auto Writer = Ext->createExtensionWriter(*this))
4328 ModuleFileExtensionWriters.push_back(std::move(Writer));
4329 }
4330}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004331
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004332ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004333 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004334}
4335
Richard Smithb3761912015-02-05 23:08:52 +00004336const LangOptions &ASTWriter::getLangOpts() const {
4337 assert(WritingAST && "can't determine lang opts when not writing AST");
4338 return Context->getLangOpts();
4339}
4340
Richard Smithe75ee0f2015-08-17 07:13:32 +00004341time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4342 return IncludeTimestamps ? E->getModificationTime() : 0;
4343}
4344
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004345ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4346 const std::string &OutputFile,
4347 Module *WritingModule, StringRef isysroot,
4348 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004349 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004350
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004351 ASTHasCompilerErrors = hasErrors;
4352
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004353 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004354 Stream.Emit((unsigned)'C', 8);
4355 Stream.Emit((unsigned)'P', 8);
4356 Stream.Emit((unsigned)'C', 8);
4357 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004358
Chris Lattner28fa4e62009-04-26 22:26:21 +00004359 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004360
Douglas Gregoreda8e122011-08-09 15:13:55 +00004361 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004362 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004363 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004364 ASTFileSignature Signature =
4365 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004366 Context = nullptr;
4367 PP = nullptr;
4368 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004369 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004370
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004371 WritingAST = false;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004372 if (SemaRef.Context.getLangOpts().ImplicitModules && WritingModule) {
4373 // Construct MemoryBuffer and update buffer manager.
4374 PCMCache.addBuffer(OutputFile,
4375 llvm::MemoryBuffer::getMemBufferCopy(
4376 StringRef(Buffer.begin(), Buffer.size())));
4377 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004378 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004379}
4380
Douglas Gregora94a1542011-07-27 21:45:57 +00004381template<typename Vector>
4382static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4383 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004384 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4385 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004386 Writer.AddDeclRef(*I, Record);
4387 }
4388}
4389
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004390ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4391 const std::string &OutputFile,
4392 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004393 using namespace llvm;
4394
Craig Toppera13603a2014-05-22 05:54:18 +00004395 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004396
Douglas Gregorcf68c582011-12-01 22:20:10 +00004397 // Make sure that the AST reader knows to finalize itself.
4398 if (Chain)
4399 Chain->finalizeForWriting();
4400
Sebastian Redl143413f2010-07-12 22:02:52 +00004401 ASTContext &Context = SemaRef.Context;
4402 Preprocessor &PP = SemaRef.PP;
4403
Douglas Gregordab42432011-08-12 00:15:20 +00004404 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004405 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4406 if (D) {
4407 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4408 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004409 }
4410 };
4411 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4412 PREDEF_DECL_TRANSLATION_UNIT_ID);
4413 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4414 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4415 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4416 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4417 PREDEF_DECL_OBJC_PROTOCOL_ID);
4418 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4419 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4420 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4421 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4422 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004423 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004424 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4425 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004426 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004427 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4428 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004429 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4430 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004431 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4432 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004433 RegisterPredefDecl(Context.TypePackElementDecl,
4434 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004435
Chris Lattner0c797362009-09-08 18:19:27 +00004436 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004437 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004438 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004439 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004440 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004441
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004442 // Build a record containing all of the file scoped decls in this file.
4443 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004444 if (!isModule)
4445 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4446 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004447
Douglas Gregor851443c2011-08-12 01:39:19 +00004448 // Build a record containing all of the delegating constructors we still need
4449 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004450 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004451 if (!isModule)
4452 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004453
Douglas Gregor851443c2011-08-12 01:39:19 +00004454 // Write the set of weak, undeclared identifiers. We always write the
4455 // entire table, since later PCH files in a PCH chain are only interested in
4456 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004457 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004458 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4459 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4460 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4461 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4462 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4463 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4464 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004465 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004466
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004467 // Build a record containing all of the ext_vector declarations.
4468 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004469 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004470
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004471 // Build a record containing all of the VTable uses information.
4472 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004473 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004474 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4475 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4476 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4477 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4478 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004479 }
4480
Nico Weber72889432014-09-06 01:25:55 +00004481 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4482 RecordData UnusedLocalTypedefNameCandidates;
4483 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4484 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4485
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004486 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004487 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004488 for (const auto &I : SemaRef.PendingInstantiations) {
4489 AddDeclRef(I.first, PendingInstantiations);
4490 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004491 }
4492 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4493 "There are local ones at end of translation unit!");
4494
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004495 // Build a record containing some declaration references.
4496 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004497 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004498 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4499 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004500 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004501 }
4502
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004503 RecordData CUDASpecialDeclRefs;
4504 if (Context.getcudaConfigureCallDecl()) {
4505 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4506 }
4507
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004508 // Build a record containing all of the known namespaces.
4509 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004510 for (const auto &I : SemaRef.KnownNamespaces) {
4511 if (!I.second)
4512 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004513 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004514
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004515 // Build a record of all used, undefined objects that require definitions.
4516 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004517
4518 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004519 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004520 for (const auto &I : Undefined) {
4521 AddDeclRef(I.first, UndefinedButUsed);
4522 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004523 }
4524
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004525 // Build a record containing all delete-expressions that we would like to
4526 // analyze later in AST.
4527 RecordData DeleteExprsToAnalyze;
4528
4529 for (const auto &DeleteExprsInfo :
4530 SemaRef.getMismatchingDeleteExpressions()) {
4531 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4532 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4533 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4534 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4535 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4536 }
4537 }
4538
Douglas Gregor112b9072012-10-18 05:31:06 +00004539 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004540 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004541
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004542 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004543 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004544
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004545 // This is so that older clang versions, before the introduction
4546 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004547 {
4548 RecordData Record = {VERSION_MAJOR};
4549 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4550 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004551
Douglas Gregor851443c2011-08-12 01:39:19 +00004552 // Create a lexical update block containing all of the declarations in the
4553 // translation unit that do not come from other AST files.
4554 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004555 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4556 for (const auto *D : TU->noload_decls()) {
4557 if (!D->isFromASTFile()) {
4558 NewGlobalKindDeclPairs.push_back(D->getKind());
4559 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4560 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004561 }
4562
David Blaikieb44f0bf2017-01-04 22:36:43 +00004563 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004564 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4565 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004566 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004567 {
4568 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4569 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4570 bytes(NewGlobalKindDeclPairs));
4571 }
4572
Douglas Gregor851443c2011-08-12 01:39:19 +00004573 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004574 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004575 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4576 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004577 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004578 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004579 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004580
4581 // If we have any extern "C" names, write out a visible update for them.
4582 if (Context.ExternCContext)
4583 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004584
4585 // If the translation unit has an anonymous namespace, and we don't already
4586 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004587 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004588 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4589 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004590 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004591 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004592 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004593
Richard Smith5652c0f2014-03-21 01:48:23 +00004594 // Add update records for all mangling numbers and static local numbers.
4595 // These aren't really update records, but this is a convenient way of
4596 // tagging this rare extra data onto the declarations.
4597 for (const auto &Number : Context.MangleNumbers)
4598 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004599 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4600 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004601 for (const auto &Number : Context.StaticLocalNumbers)
4602 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004603 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4604 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004605
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004606 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004607 // an AST file, are registered for serialization. Likewise for template
4608 // specializations added to imported templates.
4609 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004610 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004611 }
4612
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004613 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004614 // serialization, if we're storing decls with identifiers.
4615 if (!WritingModule || !getLangOpts().CPlusPlus) {
4616 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004617 for (const auto &ID : PP.getIdentifierTable()) {
4618 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004619 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4620 IIs.push_back(II);
4621 }
4622 // Sort the identifiers to visit based on their name.
4623 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4624 for (const IdentifierInfo *II : IIs) {
4625 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4626 DEnd = SemaRef.IdResolver.end();
4627 D != DEnd; ++D) {
4628 GetDeclRef(*D);
4629 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004630 }
4631 }
4632
Manman Rena0f31a02016-04-29 19:04:05 +00004633 // For method pool in the module, if it contains an entry for a selector,
4634 // the entry should be complete, containing everything introduced by that
4635 // module and all modules it imports. It's possible that the entry is out of
4636 // date, so we need to pull in the new content here.
4637
4638 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4639 // safe, we copy all selectors out.
4640 llvm::SmallVector<Selector, 256> AllSelectors;
4641 for (auto &SelectorAndID : SelectorIDs)
4642 AllSelectors.push_back(SelectorAndID.first);
4643 for (auto &Selector : AllSelectors)
4644 SemaRef.updateOutOfDateSelector(Selector);
4645
Douglas Gregor5204bde2011-08-02 16:26:37 +00004646 // Form the record of special types.
4647 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004648 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004649 AddTypeRef(Context.getFILEType(), SpecialTypes);
4650 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4651 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4652 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4653 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004654 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004655 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004656
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004657 if (Chain) {
4658 // Write the mapping information describing our module dependencies and how
4659 // each of those modules were mapped into our own offset/ID space, so that
4660 // the reader can build the appropriate mapping to its own offset/ID space.
4661 // The map consists solely of a blob with the following format:
4662 // *(module-name-len:i16 module-name:len*i8
4663 // source-location-offset:i32
4664 // identifier-id:i32
4665 // preprocessed-entity-id:i32
4666 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004667 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004668 // selector-id:i32
4669 // declaration-id:i32
4670 // c++-base-specifiers-id:i32
4671 // type-id:i32)
4672 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004673 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004674 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004676 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004677 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004678 {
4679 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004680 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004681 using namespace llvm::support;
4682 endian::Writer<little> LE(Out);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004683 StringRef FileName = M.FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004684 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004685 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004686
Ben Langmuir785180e2014-10-20 16:27:30 +00004687 // Note: if a base ID was uint max, it would not be possible to load
4688 // another module after it or have more than one entity inside it.
4689 uint32_t None = std::numeric_limits<uint32_t>::max();
4690
4691 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4692 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4693 if (ShouldWrite)
4694 LE.write<uint32_t>(BaseID);
4695 else
4696 LE.write<uint32_t>(None);
4697 };
4698
Ben Langmuirfe971d92014-08-16 04:54:18 +00004699 // These values should be unique within a chain, since they will be read
4700 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004701 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4702 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4703 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4704 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4705 M.NumPreprocessedEntities);
4706 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4707 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4708 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4709 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004710 }
4711 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004712 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004713 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4714 Buffer.data(), Buffer.size());
4715 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004716
Richard Smithb9eab6d2014-03-20 19:44:17 +00004717 RecordData DeclUpdatesOffsetsRecord;
4718
Richard Smith59442b42014-03-20 20:07:19 +00004719 // Keep writing types, declarations, and declaration update records
4720 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004721 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4722 WriteTypeAbbrevs();
4723 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004724 do {
4725 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4726 while (!DeclTypesToEmit.empty()) {
4727 DeclOrType DOT = DeclTypesToEmit.front();
4728 DeclTypesToEmit.pop();
4729 if (DOT.isType())
4730 WriteType(DOT.getType());
4731 else
4732 WriteDecl(Context, DOT.getDecl());
4733 }
4734 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004735 Stream.ExitBlock();
4736
Richard Smithb9eab6d2014-03-20 19:44:17 +00004737 DoneWritingDeclsAndTypes = true;
4738
4739 // These things can only be done once we've written out decls and types.
4740 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004741 if (!DeclUpdatesOffsetsRecord.empty())
4742 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004743 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004744 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004745 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004746 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004747 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004748 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004749 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004750 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004751 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004752 WriteFPPragmaOptions(SemaRef.getFPOptions());
4753 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004754 WriteOpenCLExtensionTypes(SemaRef);
4755 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004756 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004757
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004758 // If we're emitting a module, write out the submodule information.
4759 if (WritingModule)
4760 WriteSubmodules(WritingModule);
4761
Douglas Gregor5204bde2011-08-02 16:26:37 +00004762 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4763
Douglas Gregord4df8652009-04-22 22:02:47 +00004764 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004765 if (!EagerlyDeserializedDecls.empty())
4766 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004767
David Blaikie9ffe5a32017-01-30 05:00:26 +00004768 if (Context.getLangOpts().ModularCodegen)
4769 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4770
Douglas Gregord4df8652009-04-22 22:02:47 +00004771 // Write the record containing tentative definitions.
4772 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004773 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004774
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004775 // Write the record containing unused file scoped decls.
4776 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004777 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004778
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004779 // Write the record containing weak undeclared identifiers.
4780 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004781 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004782 WeakUndeclaredIdentifiers);
4783
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004784 // Write the record containing ext_vector type names.
4785 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004786 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004787
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004788 // Write the record containing VTable uses information.
4789 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004790 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004791
Nico Weber72889432014-09-06 01:25:55 +00004792 // Write the record containing potentially unused local typedefs.
4793 if (!UnusedLocalTypedefNameCandidates.empty())
4794 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4795 UnusedLocalTypedefNameCandidates);
4796
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004797 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004798 if (!PendingInstantiations.empty())
4799 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004800
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004801 // Write the record containing declaration references of Sema.
4802 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004803 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004804
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004805 // Write the record containing CUDA-specific declaration references.
4806 if (!CUDASpecialDeclRefs.empty())
4807 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004808
4809 // Write the delegating constructors.
4810 if (!DelegatingCtorDecls.empty())
4811 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004812
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004813 // Write the known namespaces.
4814 if (!KnownNamespaces.empty())
4815 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004816
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004817 // Write the undefined internal functions and variables, and inline functions.
4818 if (!UndefinedButUsed.empty())
4819 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004820
4821 if (!DeleteExprsToAnalyze.empty())
4822 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4823
Douglas Gregor851443c2011-08-12 01:39:19 +00004824 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004825 for (auto *DC : UpdatedDeclContexts)
4826 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004827
Douglas Gregor959bb062011-12-03 01:15:29 +00004828 if (!WritingModule) {
4829 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004830 struct ModuleInfo {
4831 uint64_t ID;
4832 Module *M;
4833 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4834 };
Richard Smith56be7542014-03-21 00:33:59 +00004835 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004836 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004837 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004838 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4839 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004840 }
Richard Smith56be7542014-03-21 00:33:59 +00004841
4842 if (!Imports.empty()) {
4843 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4844 return A.ID < B.ID;
4845 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004846 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4847 return A.ID == B.ID;
4848 };
Richard Smith56be7542014-03-21 00:33:59 +00004849
4850 // Sort and deduplicate module IDs.
4851 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004852 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004853 Imports.end());
4854
4855 RecordData ImportedModules;
4856 for (const auto &Import : Imports) {
4857 ImportedModules.push_back(Import.ID);
4858 // FIXME: If the module has macros imported then later has declarations
4859 // imported, this location won't be the right one as a location for the
4860 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004861 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004862 }
4863
Douglas Gregor959bb062011-12-03 01:15:29 +00004864 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4865 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004866 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004867
Douglas Gregor404cdde2012-01-27 01:47:08 +00004868 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004869 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004870 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004871 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004872 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004873 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004874 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004875
Douglas Gregor08f01292009-04-17 22:13:46 +00004876 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004877 RecordData::value_type Record[] = {
4878 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004879 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004880 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004881
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004882 // Write the module file extension blocks.
4883 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004884 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004885
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004886 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004887}
4888
Richard Smithb9eab6d2014-03-20 19:44:17 +00004889void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004890 if (DeclUpdates.empty())
4891 return;
4892
Richard Smith59442b42014-03-20 20:07:19 +00004893 DeclUpdateMap LocalUpdates;
4894 LocalUpdates.swap(DeclUpdates);
4895
Richard Smith6ef42932014-03-20 21:02:00 +00004896 for (auto &DeclUpdate : LocalUpdates) {
4897 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004898
Richard Smithd28ac5b2014-03-22 23:33:22 +00004899 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004900 RecordData RecordData;
4901 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004902 for (auto &Update : DeclUpdate.second) {
4903 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4904
Richard Smith69c82bf2016-04-01 22:52:03 +00004905 // An updated body is emitted last, so that the reader doesn't need
4906 // to skip over the lazy body to reach statements for other records.
4907 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4908 HasUpdatedBody = true;
4909 else
4910 Record.push_back(Kind);
4911
Richard Smith6ef42932014-03-20 21:02:00 +00004912 switch (Kind) {
4913 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4914 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4915 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004916 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004917 Record.push_back(GetDeclRef(Update.getDecl()));
4918 break;
4919
Richard Smith4d235792014-08-07 18:53:08 +00004920 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004921 break;
4922
Richard Smith4d235792014-08-07 18:53:08 +00004923 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004924 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004925 break;
4926
John McCall32791cc2016-01-06 22:34:54 +00004927 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004928 Record.AddStmt(const_cast<Expr *>(
4929 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004930 break;
4931
Richard Smith4b054b22016-08-24 21:25:37 +00004932 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4933 Record.AddStmt(
4934 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4935 break;
4936
Richard Smithcd45dbc2014-04-19 03:48:30 +00004937 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4938 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004939 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004940 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004941 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004942 *Context, const_cast<CXXRecordDecl *>(RD)));
4943
4944 // This state is sometimes updated by template instantiation, when we
4945 // switch from the specialization referring to the template declaration
4946 // to it referring to the template definition.
4947 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4948 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004949 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004950 } else {
4951 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4952 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004953 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004954
4955 // The instantiation might have been resolved to a partial
4956 // specialization. If so, record which one.
4957 auto From = Spec->getInstantiatedFrom();
4958 if (auto PartialSpec =
4959 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4960 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004961 Record.AddDeclRef(PartialSpec);
4962 Record.AddTemplateArgumentList(
4963 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004964 } else {
4965 Record.push_back(false);
4966 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004967 }
4968 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004969 Record.AddSourceLocation(RD->getLocation());
4970 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004971 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004972
4973 // Instantiation may change attributes; write them all out afresh.
4974 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004975 if (D->hasAttrs())
4976 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004977
4978 // FIXME: Ensure we don't get here for explicit instantiations.
4979 break;
4980 }
4981
Richard Smithf8134002015-03-10 01:41:22 +00004982 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004983 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004984 break;
4985
Richard Smith564417a2014-03-20 21:47:22 +00004986 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4987 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004988 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4989 Record);
4990 break;
4991
Richard Smith6ef42932014-03-20 21:02:00 +00004992 case UPD_CXX_DEDUCED_RETURN_TYPE:
4993 Record.push_back(GetOrCreateTypeID(Update.getType()));
4994 break;
4995
4996 case UPD_DECL_MARKED_USED:
4997 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004998
4999 case UPD_MANGLING_NUMBER:
5000 case UPD_STATIC_LOCAL_NUMBER:
5001 Record.push_back(Update.getNumber());
5002 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005003
Alexey Bataev97720002014-11-11 04:05:39 +00005004 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005005 Record.AddSourceRange(
5006 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005007 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005008
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005009 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5010 Record.AddSourceRange(
5011 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5012 break;
5013
Richard Smith65ebb4a2015-03-26 04:09:53 +00005014 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005015 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005016 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005017
5018 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005019 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005020 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005021 }
5022 }
5023
Richard Smithd28ac5b2014-03-22 23:33:22 +00005024 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005025 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005026 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005027 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005028 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005029 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005030 }
5031
Richard Smithcd45dbc2014-04-19 03:48:30 +00005032 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005033 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005034 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005035}
5036
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005037void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005038 uint32_t Raw = Loc.getRawEncoding();
5039 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005040}
5041
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005042void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005043 AddSourceLocation(Range.getBegin(), Record);
5044 AddSourceLocation(Range.getEnd(), Record);
5045}
5046
Richard Smithf144b542016-04-08 21:54:32 +00005047void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5048 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005049 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005050 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005051}
5052
Richard Smithf144b542016-04-08 21:54:32 +00005053void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5054 Record->push_back(Value.isUnsigned());
5055 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005056}
5057
Richard Smithf144b542016-04-08 21:54:32 +00005058void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5059 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005060}
5061
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005062void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005063 Record.push_back(getIdentifierRef(II));
5064}
5065
Sebastian Redl539c5062010-08-18 23:57:32 +00005066IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005067 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005068 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005069
Sebastian Redl539c5062010-08-18 23:57:32 +00005070 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005071 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005072 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005073 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005074}
5075
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005076MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005077 // Don't emit builtin macros like __LINE__ to the AST file unless they
5078 // have been redefined by the header (in which case they are not
5079 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005080 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005081 return 0;
5082
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005083 MacroID &ID = MacroIDs[MI];
5084 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005085 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005086 MacroInfoToEmitData Info = { Name, MI, ID };
5087 MacroInfosToEmit.push_back(Info);
5088 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005089 return ID;
5090}
5091
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005092MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005093 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005094 return 0;
5095
5096 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5097 return MacroIDs[MI];
5098}
5099
5100uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005101 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005102}
5103
Richard Smithe64e7452016-04-18 21:54:58 +00005104void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5105 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005106}
5107
Sebastian Redl539c5062010-08-18 23:57:32 +00005108SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005109 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005110 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005111 }
5112
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005113 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005114 if (SID == 0 && Chain) {
5115 // This might trigger a ReadSelector callback, which will set the ID for
5116 // this selector.
5117 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005118 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005119 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005120 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005121 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005122 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005123 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005124 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005125}
5126
Richard Smithe64e7452016-04-18 21:54:58 +00005127void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5128 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005129}
5130
Richard Smith290d8012016-04-06 17:06:00 +00005131void ASTRecordWriter::AddTemplateArgumentLocInfo(
5132 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005133 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005134 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005135 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005136 break;
5137 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005138 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005139 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005140 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005141 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5142 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005143 break;
5144 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005145 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5146 AddSourceLocation(Arg.getTemplateNameLoc());
5147 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005148 break;
John McCall0ad16662009-10-29 08:12:44 +00005149 case TemplateArgument::Null:
5150 case TemplateArgument::Integral:
5151 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005152 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005153 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005154 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005155 break;
5156 }
5157}
5158
Richard Smith290d8012016-04-06 17:06:00 +00005159void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5160 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005161
5162 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5163 bool InfoHasSameExpr
5164 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005165 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005166 if (InfoHasSameExpr)
5167 return; // Avoid storing the same expr twice.
5168 }
Richard Smith290d8012016-04-06 17:06:00 +00005169 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005170}
5171
Richard Smith290d8012016-04-06 17:06:00 +00005172void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005173 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005174 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005175 return;
5176 }
5177
Richard Smith290d8012016-04-06 17:06:00 +00005178 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005179}
5180
Richard Smith290d8012016-04-06 17:06:00 +00005181void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5182 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005183
Richard Smith290d8012016-04-06 17:06:00 +00005184 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005185 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005186 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005187}
5188
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005189void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005190 Record.push_back(GetOrCreateTypeID(T));
5191}
5192
Richard Smith2095ffe2015-03-16 20:11:03 +00005193TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005194 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005195 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5196 if (T.isNull())
5197 return TypeIdx();
5198 assert(!T.getLocalFastQualifiers());
5199
5200 TypeIdx &Idx = TypeIdxs[T];
5201 if (Idx.getIndex() == 0) {
5202 if (DoneWritingDeclsAndTypes) {
5203 assert(0 && "New type seen after serializing all the types to emit!");
5204 return TypeIdx();
5205 }
5206
5207 // We haven't seen this type before. Assign it a new ID and put it
5208 // into the queue of types to emit.
5209 Idx = TypeIdx(NextTypeID++);
5210 DeclTypesToEmit.push(T);
5211 }
5212 return Idx;
5213 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005214}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005215
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005216TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005217 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005218 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5219 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005220 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005221 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005222
Richard Smith2095ffe2015-03-16 20:11:03 +00005223 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5224 assert(I != TypeIdxs.end() && "Type not emitted!");
5225 return I->second;
5226 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005227}
5228
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005229void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005230 Record.push_back(GetDeclRef(D));
5231}
5232
Sebastian Redl539c5062010-08-18 23:57:32 +00005233DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005234 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005235
5236 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005237 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005238 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005239
5240 // If D comes from an AST file, its declaration ID is already known and
5241 // fixed.
5242 if (D->isFromASTFile())
5243 return D->getGlobalID();
5244
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005245 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005246 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005247 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005248 if (DoneWritingDeclsAndTypes) {
5249 assert(0 && "New decl seen after serializing all the decls to emit!");
5250 return 0;
5251 }
5252
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005253 // We haven't seen this declaration before. Give it a new ID and
5254 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005255 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005256 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005257 }
5258
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005259 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005260}
5261
Sebastian Redl539c5062010-08-18 23:57:32 +00005262DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005263 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005264 return 0;
5265
Douglas Gregorb3163e52012-01-05 22:33:30 +00005266 // If D comes from an AST file, its declaration ID is already known and
5267 // fixed.
5268 if (D->isFromASTFile())
5269 return D->getGlobalID();
5270
Douglas Gregore84a9da2009-04-20 20:36:09 +00005271 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5272 return DeclIDs[D];
5273}
5274
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005275void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005276 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005277 assert(D);
5278
5279 SourceLocation Loc = D->getLocation();
5280 if (Loc.isInvalid())
5281 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005282
5283 // We only keep track of the file-level declarations of each file.
5284 if (!D->getLexicalDeclContext()->isFileContext())
5285 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005286 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5287 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005288 if (isa<ParmVarDecl>(D))
5289 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005290
5291 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005292 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005293 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005294 FileID FID;
5295 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005296 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005297 if (FID.isInvalid())
5298 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005299 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005300
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005301 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005302 if (!Info)
5303 Info = new DeclIDInFileInfo();
5304
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005305 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005306 LocDeclIDsTy &Decls = Info->DeclIDs;
5307
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005308 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005309 Decls.push_back(LocDecl);
5310 return;
5311 }
5312
Benjamin Kramer45025c02013-08-24 13:22:59 +00005313 LocDeclIDsTy::iterator I =
5314 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005315
5316 Decls.insert(I, LocDecl);
5317}
5318
Richard Smithe64e7452016-04-18 21:54:58 +00005319void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005320 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005321 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005322 switch (Name.getNameKind()) {
5323 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005324 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005325 break;
5326
5327 case DeclarationName::ObjCZeroArgSelector:
5328 case DeclarationName::ObjCOneArgSelector:
5329 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005330 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005331 break;
5332
5333 case DeclarationName::CXXConstructorName:
5334 case DeclarationName::CXXDestructorName:
5335 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005336 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005337 break;
5338
Richard Smith35845152017-02-07 01:37:30 +00005339 case DeclarationName::CXXDeductionGuideName:
5340 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5341 break;
5342
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005343 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005344 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005345 break;
5346
Alexis Hunt3d221f22009-11-29 07:34:05 +00005347 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005348 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005349 break;
5350
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005351 case DeclarationName::CXXUsingDirective:
5352 // No extra data to emit
5353 break;
5354 }
5355}
Chris Lattnerca025db2010-05-07 21:43:38 +00005356
Richard Smithd08aeb62014-08-28 01:33:39 +00005357unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5358 assert(needsAnonymousDeclarationNumber(D) &&
5359 "expected an anonymous declaration");
5360
5361 // Number the anonymous declarations within this context, if we've not
5362 // already done so.
5363 auto It = AnonymousDeclarationNumbers.find(D);
5364 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005365 auto *DC = D->getLexicalDeclContext();
5366 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5367 AnonymousDeclarationNumbers[ND] = Number;
5368 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005369
5370 It = AnonymousDeclarationNumbers.find(D);
5371 assert(It != AnonymousDeclarationNumbers.end() &&
5372 "declaration not found within its lexical context");
5373 }
5374
5375 return It->second;
5376}
5377
Richard Smith290d8012016-04-06 17:06:00 +00005378void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5379 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005380 switch (Name.getNameKind()) {
5381 case DeclarationName::CXXConstructorName:
5382 case DeclarationName::CXXDestructorName:
5383 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005384 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005385 break;
5386
5387 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005388 AddSourceLocation(SourceLocation::getFromRawEncoding(
5389 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005390 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005391 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005392 break;
5393
5394 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005395 AddSourceLocation(SourceLocation::getFromRawEncoding(
5396 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005397 break;
5398
5399 case DeclarationName::Identifier:
5400 case DeclarationName::ObjCZeroArgSelector:
5401 case DeclarationName::ObjCOneArgSelector:
5402 case DeclarationName::ObjCMultiArgSelector:
5403 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005404 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005405 break;
5406 }
5407}
5408
Richard Smith290d8012016-04-06 17:06:00 +00005409void ASTRecordWriter::AddDeclarationNameInfo(
5410 const DeclarationNameInfo &NameInfo) {
5411 AddDeclarationName(NameInfo.getName());
5412 AddSourceLocation(NameInfo.getLoc());
5413 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005414}
5415
Richard Smith290d8012016-04-06 17:06:00 +00005416void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5417 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5418 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005419 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005420 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005421}
5422
Richard Smithe64e7452016-04-18 21:54:58 +00005423void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005424 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005425 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005426 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005427
5428 // Push each of the NNS's onto a stack for serialization in reverse order.
5429 while (NNS) {
5430 NestedNames.push_back(NNS);
5431 NNS = NNS->getPrefix();
5432 }
5433
Richard Smithe64e7452016-04-18 21:54:58 +00005434 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005435 while(!NestedNames.empty()) {
5436 NNS = NestedNames.pop_back_val();
5437 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005438 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005439 switch (Kind) {
5440 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005441 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005442 break;
5443
5444 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005445 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005446 break;
5447
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005448 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005449 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005450 break;
5451
Chris Lattnerca025db2010-05-07 21:43:38 +00005452 case NestedNameSpecifier::TypeSpec:
5453 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005454 AddTypeRef(QualType(NNS->getAsType(), 0));
5455 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005456 break;
5457
5458 case NestedNameSpecifier::Global:
5459 // Don't need to write an associated value.
5460 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005461
5462 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005463 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005464 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005465 }
5466 }
5467}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005468
Richard Smith290d8012016-04-06 17:06:00 +00005469void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005470 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005471 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005472 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005473
5474 // Push each of the nested-name-specifiers's onto a stack for
5475 // serialization in reverse order.
5476 while (NNS) {
5477 NestedNames.push_back(NNS);
5478 NNS = NNS.getPrefix();
5479 }
5480
Richard Smith290d8012016-04-06 17:06:00 +00005481 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005482 while(!NestedNames.empty()) {
5483 NNS = NestedNames.pop_back_val();
5484 NestedNameSpecifier::SpecifierKind Kind
5485 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005486 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005487 switch (Kind) {
5488 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005489 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5490 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005491 break;
5492
5493 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005494 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5495 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005496 break;
5497
5498 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005499 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5500 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005501 break;
5502
5503 case NestedNameSpecifier::TypeSpec:
5504 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005505 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5506 AddTypeLoc(NNS.getTypeLoc());
5507 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005508 break;
5509
5510 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005511 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005512 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005513
5514 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005515 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5516 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005517 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005518 }
5519 }
5520}
5521
Richard Smith290d8012016-04-06 17:06:00 +00005522void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005523 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005524 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005525 switch (Kind) {
5526 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005527 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005528 break;
5529
5530 case TemplateName::OverloadedTemplate: {
5531 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005532 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005533 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005534 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005535 break;
5536 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005537
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005538 case TemplateName::QualifiedTemplate: {
5539 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005540 AddNestedNameSpecifier(QualT->getQualifier());
5541 Record->push_back(QualT->hasTemplateKeyword());
5542 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005543 break;
5544 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005545
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005546 case TemplateName::DependentTemplate: {
5547 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005548 AddNestedNameSpecifier(DepT->getQualifier());
5549 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005550 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005551 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005552 else
Richard Smith290d8012016-04-06 17:06:00 +00005553 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005554 break;
5555 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005556
5557 case TemplateName::SubstTemplateTemplateParm: {
5558 SubstTemplateTemplateParmStorage *subst
5559 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005560 AddDeclRef(subst->getParameter());
5561 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005562 break;
5563 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005564
5565 case TemplateName::SubstTemplateTemplateParmPack: {
5566 SubstTemplateTemplateParmPackStorage *SubstPack
5567 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005568 AddDeclRef(SubstPack->getParameterPack());
5569 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005570 break;
5571 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005572 }
5573}
5574
Richard Smith290d8012016-04-06 17:06:00 +00005575void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5576 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005577 switch (Arg.getKind()) {
5578 case TemplateArgument::Null:
5579 break;
5580 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005581 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005582 break;
5583 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005584 AddDeclRef(Arg.getAsDecl());
5585 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005586 break;
5587 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005588 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005589 break;
5590 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005591 AddAPSInt(Arg.getAsIntegral());
5592 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005593 break;
5594 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005595 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005596 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005597 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005598 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005599 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005600 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005601 else
Richard Smith290d8012016-04-06 17:06:00 +00005602 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005603 break;
5604 case TemplateArgument::Expression:
5605 AddStmt(Arg.getAsExpr());
5606 break;
5607 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005608 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005609 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005610 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005611 break;
5612 }
5613}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005614
Richard Smithe64e7452016-04-18 21:54:58 +00005615void ASTRecordWriter::AddTemplateParameterList(
5616 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005617 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005618 AddSourceLocation(TemplateParams->getTemplateLoc());
5619 AddSourceLocation(TemplateParams->getLAngleLoc());
5620 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005621 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005622 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005623 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005624 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005625}
5626
5627/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005628void ASTRecordWriter::AddTemplateArgumentList(
5629 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005630 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005631 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005632 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005633 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005634}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005635
Richard Smith290d8012016-04-06 17:06:00 +00005636void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5637 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005638 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005639 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5640 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5641 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005642 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005643 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005644 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005645}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005646
Richard Smithe64e7452016-04-18 21:54:58 +00005647void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5648 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005649 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005650 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005651 AddDeclRef(I.getDecl());
5652 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005653 }
5654}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005655
Richard Smith290d8012016-04-06 17:06:00 +00005656// FIXME: Move this out of the main ASTRecordWriter interface.
5657void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5658 Record->push_back(Base.isVirtual());
5659 Record->push_back(Base.isBaseOfClass());
5660 Record->push_back(Base.getAccessSpecifierAsWritten());
5661 Record->push_back(Base.getInheritConstructors());
5662 AddTypeSourceInfo(Base.getTypeSourceInfo());
5663 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005664 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005665 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005666}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005667
Richard Smith645d2cf2016-04-14 00:29:55 +00005668static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5669 ArrayRef<CXXBaseSpecifier> Bases) {
5670 ASTWriter::RecordData Record;
5671 ASTRecordWriter Writer(W, Record);
5672 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005673
Richard Smith645d2cf2016-04-14 00:29:55 +00005674 for (auto &Base : Bases)
5675 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005676
Richard Smith645d2cf2016-04-14 00:29:55 +00005677 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005678}
5679
Richard Smith290d8012016-04-06 17:06:00 +00005680// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005681void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5682 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5683}
5684
Richard Smithaa165cf2016-04-13 21:57:08 +00005685static uint64_t
5686EmitCXXCtorInitializers(ASTWriter &W,
5687 ArrayRef<CXXCtorInitializer *> CtorInits) {
5688 ASTWriter::RecordData Record;
5689 ASTRecordWriter Writer(W, Record);
5690 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005691
Richard Smithaa165cf2016-04-13 21:57:08 +00005692 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005693 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005694 Writer.push_back(CTOR_INITIALIZER_BASE);
5695 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5696 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005697 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005698 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5699 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005700 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005701 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5702 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005703 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005704 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5705 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005706 }
Francois Pichetd583da02010-12-04 09:14:42 +00005707
Richard Smithaa165cf2016-04-13 21:57:08 +00005708 Writer.AddSourceLocation(Init->getMemberLocation());
5709 Writer.AddStmt(Init->getInit());
5710 Writer.AddSourceLocation(Init->getLParenLoc());
5711 Writer.AddSourceLocation(Init->getRParenLoc());
5712 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005713 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005714 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005715 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005716
5717 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005718}
5719
Richard Smithaa165cf2016-04-13 21:57:08 +00005720// FIXME: Move this out of the main ASTRecordWriter interface.
5721void ASTRecordWriter::AddCXXCtorInitializers(
5722 ArrayRef<CXXCtorInitializer *> CtorInits) {
5723 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005724}
5725
Richard Smith290d8012016-04-06 17:06:00 +00005726void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005727 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005728 Record->push_back(Data.IsLambda);
5729 Record->push_back(Data.UserDeclaredConstructor);
5730 Record->push_back(Data.UserDeclaredSpecialMembers);
5731 Record->push_back(Data.Aggregate);
5732 Record->push_back(Data.PlainOldData);
5733 Record->push_back(Data.Empty);
5734 Record->push_back(Data.Polymorphic);
5735 Record->push_back(Data.Abstract);
5736 Record->push_back(Data.IsStandardLayout);
5737 Record->push_back(Data.HasNoNonEmptyBases);
5738 Record->push_back(Data.HasPrivateFields);
5739 Record->push_back(Data.HasProtectedFields);
5740 Record->push_back(Data.HasPublicFields);
5741 Record->push_back(Data.HasMutableFields);
5742 Record->push_back(Data.HasVariantMembers);
5743 Record->push_back(Data.HasOnlyCMembers);
5744 Record->push_back(Data.HasInClassInitializer);
5745 Record->push_back(Data.HasUninitializedReferenceMember);
5746 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005747 Record->push_back(Data.HasInheritedConstructor);
5748 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005749 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5750 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5751 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5752 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5753 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5754 Record->push_back(Data.DefaultedDestructorIsDeleted);
5755 Record->push_back(Data.HasTrivialSpecialMembers);
5756 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5757 Record->push_back(Data.HasIrrelevantDestructor);
5758 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5759 Record->push_back(Data.HasDefaultedDefaultConstructor);
5760 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5761 Record->push_back(Data.HasConstexprDefaultConstructor);
5762 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5763 Record->push_back(Data.ComputedVisibleConversions);
5764 Record->push_back(Data.UserProvidedDefaultConstructor);
5765 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005766 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5767 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005768 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5769 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5770 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieufd1acbb2017-04-11 21:31:00 +00005771
5772 // getODRHash will compute the ODRHash if it has not been previously computed.
5773 Record->push_back(D->getODRHash());
5774
David Blaikie1ac9c982017-04-11 21:13:37 +00005775 bool ModularCodegen = Writer->Context->getLangOpts().ModularCodegen &&
5776 Writer->WritingModule && !D->isDependentType();
5777 Record->push_back(ModularCodegen);
5778 if (ModularCodegen)
5779 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
5780
Richard Smith561fb152012-02-25 07:33:38 +00005781 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005782
Richard Smith290d8012016-04-06 17:06:00 +00005783 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005784 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005785 AddCXXBaseSpecifiers(Data.bases());
5786
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005787 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005788 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005789 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005790 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005791
Richard Smith290d8012016-04-06 17:06:00 +00005792 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5793 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005794 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005795 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005796
5797 // Add lambda-specific data.
5798 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005799 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005800 Record->push_back(Lambda.Dependent);
5801 Record->push_back(Lambda.IsGenericLambda);
5802 Record->push_back(Lambda.CaptureDefault);
5803 Record->push_back(Lambda.NumCaptures);
5804 Record->push_back(Lambda.NumExplicitCaptures);
5805 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005806 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005807 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005808 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005809 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005810 AddSourceLocation(Capture.getLocation());
5811 Record->push_back(Capture.isImplicit());
5812 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005813 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005814 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005815 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005816 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005817 break;
5818 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005819 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005820 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005821 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005822 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005823 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005824 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005825 break;
5826 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005827 }
5828 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005829}
5830
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005831void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005832 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005833 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005834 assert(FirstDeclID == NextDeclID &&
5835 FirstTypeID == NextTypeID &&
5836 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005837 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005838 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005839 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005840 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005841
Sebastian Redl07a89a82010-07-30 00:29:29 +00005842 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005843
Richard Smith7f330cd2015-03-18 01:42:29 +00005844 // Note, this will get called multiple times, once one the reader starts up
5845 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005846 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5847 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5848 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005849 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005850 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005851 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005852 NextDeclID = FirstDeclID;
5853 NextTypeID = FirstTypeID;
5854 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005855 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005856 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005857 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005858}
5859
Sebastian Redl539c5062010-08-18 23:57:32 +00005860void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005861 // Always keep the highest ID. See \p TypeRead() for more information.
5862 IdentID &StoredID = IdentifierIDs[II];
5863 if (ID > StoredID)
5864 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005865}
5866
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005867void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005868 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005869 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005870 if (ID > StoredID)
5871 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005872}
5873
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005874void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005875 // Always take the highest-numbered type index. This copes with an interesting
5876 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005877 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005878 // keep the higher-numbered entry so that we can properly write it out to
5879 // the AST file.
5880 TypeIdx &StoredIdx = TypeIdxs[T];
5881 if (Idx.getIndex() >= StoredIdx.getIndex())
5882 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005883}
5884
Sebastian Redl539c5062010-08-18 23:57:32 +00005885void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005886 // Always keep the highest ID. See \p TypeRead() for more information.
5887 SelectorID &StoredID = SelectorIDs[S];
5888 if (ID > StoredID)
5889 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005890}
Douglas Gregor91096292010-10-02 19:29:26 +00005891
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005892void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005893 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005894 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005895 MacroDefinitions[MD] = ID;
5896}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005897
Douglas Gregore37a85a2011-12-02 17:30:13 +00005898void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5899 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5900 SubmoduleIDs[Mod] = ID;
5901}
5902
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005903void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005904 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005905 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005906 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005907 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005908 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005909 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005910 // A forward reference was mutated into a definition. Rewrite it.
5911 // FIXME: This happens during template instantiation, should we
5912 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005913 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5914 "completed a tag from another module but not by instantiation?");
5915 DeclUpdates[RD].push_back(
5916 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005917 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005918 }
5919}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005920
Richard Smithf983f7f2015-10-13 00:23:25 +00005921static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5922 if (D->isFromASTFile())
5923 return true;
5924
Richard Smithf983f7f2015-10-13 00:23:25 +00005925 // The predefined __va_list_tag struct is imported if we imported any decls.
5926 // FIXME: This is a gross hack.
5927 return D == D->getASTContext().getVaListTagDecl();
5928}
5929
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005930void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005931 if (Chain && Chain->isProcessingUpdateRecords()) return;
5932 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005933 "Should not add lookup results to non-lookup contexts!");
5934
Vassil Vassilev632eac32016-03-16 11:17:04 +00005935 // TU is handled elsewhere.
5936 if (isa<TranslationUnitDecl>(DC))
5937 return;
5938
5939 // Namespaces are handled elsewhere, except for template instantiations of
5940 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5941 // local instantiations are added to an imported context. Only happens when
5942 // adding ADL lookup candidates, for example templated friends.
5943 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5944 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005945 return;
5946
Richard Smithf983f7f2015-10-13 00:23:25 +00005947 // We're only interested in cases where a local declaration is added to an
5948 // imported context.
5949 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5950 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005951
Richard Smith0f4e2c42015-08-06 04:23:48 +00005952 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005953 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005954 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005955 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5956 // We're adding a visible declaration to a predefined decl context. Ensure
5957 // that we write out all of its lookup results so we don't get a nasty
5958 // surprise when we try to emit its lookup table.
5959 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005960 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005961 }
Richard Smithc26d9742016-10-06 20:30:51 +00005962 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005963}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005964
5965void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005966 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005967 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005968
5969 // We're only interested in cases where a local declaration is added to an
5970 // imported context.
5971 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5972 return;
5973
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005974 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005975 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005976
5977 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005978 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005979 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005980 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005981}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005982
Richard Smith564417a2014-03-20 21:47:22 +00005983void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005984 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005985 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5986 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005987 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005988 // If we don't already know the exception specification for this redecl
5989 // chain, add an update record for it.
5990 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5991 ->getType()
5992 ->castAs<FunctionProtoType>()
5993 ->getExceptionSpecType()))
5994 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5995 });
Richard Smith564417a2014-03-20 21:47:22 +00005996}
5997
Richard Smith1fa5d642013-05-11 05:45:24 +00005998void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005999 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00006000 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00006001 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006002 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006003 DeclUpdates[D].push_back(
6004 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6005 });
Richard Smith1fa5d642013-05-11 05:45:24 +00006006}
6007
Richard Smithf8134002015-03-10 01:41:22 +00006008void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6009 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006010 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006011 assert(!WritingAST && "Already writing the AST!");
6012 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006013 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006014 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006015 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6016 });
Richard Smithf8134002015-03-10 01:41:22 +00006017}
6018
Sebastian Redlab238a72011-04-24 16:28:06 +00006019void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006020 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006021 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006022 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006023 return; // Declaration not imported from PCH.
6024
Richard Smith4d235792014-08-07 18:53:08 +00006025 // Implicit function decl from a PCH was defined.
6026 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006027}
6028
Richard Smithd28ac5b2014-03-22 23:33:22 +00006029void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006030 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006031 assert(!WritingAST && "Already writing the AST!");
6032 if (!D->isFromASTFile())
6033 return;
6034
Richard Smith9e2341d2015-03-23 03:25:59 +00006035 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006036}
6037
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006038void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006039 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006040 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006041 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006042 return;
6043
6044 // Since the actual instantiation is delayed, this really means that we need
6045 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00006046 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00006047 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
6048 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006049}
6050
John McCall32791cc2016-01-06 22:34:54 +00006051void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006052 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006053 assert(!WritingAST && "Already writing the AST!");
6054 if (!D->isFromASTFile())
6055 return;
6056
6057 DeclUpdates[D].push_back(
6058 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6059}
6060
Richard Smith4b054b22016-08-24 21:25:37 +00006061void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6062 assert(!WritingAST && "Already writing the AST!");
6063 if (!D->isFromASTFile())
6064 return;
6065
6066 DeclUpdates[D].push_back(
6067 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6068}
6069
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006070void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6071 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006072 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006073 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006074 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006075 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006076
6077 assert(IFD->getDefinition() && "Category on a class without a definition?");
6078 ObjCClassesWithCategories.insert(
6079 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006080}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006081
Eli Friedman276dd182013-09-05 00:02:25 +00006082void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006083 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006084 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006085
6086 // If there is *any* declaration of the entity that's not from an AST file,
6087 // we can skip writing the update record. We make sure that isUsed() triggers
6088 // completion of the redeclaration chain of the entity.
6089 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6090 if (IsLocalDecl(Prev))
6091 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006092
Aaron Ballman4f45b712014-03-21 15:22:56 +00006093 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006094}
Alexey Bataev97720002014-11-11 04:05:39 +00006095
6096void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006097 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006098 assert(!WritingAST && "Already writing the AST!");
6099 if (!D->isFromASTFile())
6100 return;
6101
6102 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6103}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006104
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006105void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6106 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006107 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006108 assert(!WritingAST && "Already writing the AST!");
6109 if (!D->isFromASTFile())
6110 return;
6111
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006112 DeclUpdates[D].push_back(
6113 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006114}
6115
Richard Smith4caa4492015-05-15 02:34:32 +00006116void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006117 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006118 assert(!WritingAST && "Already writing the AST!");
6119 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006120 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006121}
Alex Denisovfde64952015-06-26 05:28:36 +00006122
6123void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6124 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006125 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006126 assert(!WritingAST && "Already writing the AST!");
6127 if (!Record->isFromASTFile())
6128 return;
6129 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6130}
Richard Smithc26d9742016-10-06 20:30:51 +00006131
6132void ASTWriter::AddedCXXTemplateSpecialization(
6133 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6134 assert(!WritingAST && "Already writing the AST!");
6135
6136 if (!TD->getFirstDecl()->isFromASTFile())
6137 return;
6138 if (Chain && Chain->isProcessingUpdateRecords())
6139 return;
6140
6141 DeclsToEmitEvenIfUnreferenced.push_back(D);
6142}
6143
6144void ASTWriter::AddedCXXTemplateSpecialization(
6145 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6146 assert(!WritingAST && "Already writing the AST!");
6147
6148 if (!TD->getFirstDecl()->isFromASTFile())
6149 return;
6150 if (Chain && Chain->isProcessingUpdateRecords())
6151 return;
6152
6153 DeclsToEmitEvenIfUnreferenced.push_back(D);
6154}
6155
6156void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6157 const FunctionDecl *D) {
6158 assert(!WritingAST && "Already writing the AST!");
6159
6160 if (!TD->getFirstDecl()->isFromASTFile())
6161 return;
6162 if (Chain && Chain->isProcessingUpdateRecords())
6163 return;
6164
6165 DeclsToEmitEvenIfUnreferenced.push_back(D);
6166}