blob: 23859c241d5280b916660cc6f29643c31164e915 [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
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002907 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002908 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002909}
2910
Douglas Gregorc5046832009-04-27 18:38:38 +00002911//===----------------------------------------------------------------------===//
2912// Type Serialization
2913//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002914
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002915/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002916void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002917 TypeIdx &IdxRef = TypeIdxs[T];
2918 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2919 IdxRef = TypeIdx(NextTypeID++);
2920 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002921
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002922 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002923
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002924 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002925
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002926 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002927 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002928 W.Visit(T);
2929 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002930
Richard Smith290d8012016-04-06 17:06:00 +00002931 // Record the offset for this type.
2932 unsigned Index = Idx.getIndex() - FirstTypeID;
2933 if (TypeOffsets.size() == Index)
2934 TypeOffsets.push_back(Offset);
2935 else if (TypeOffsets.size() < Index) {
2936 TypeOffsets.resize(Index + 1);
2937 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002938 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002939 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002940 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002941}
2942
Douglas Gregorc5046832009-04-27 18:38:38 +00002943//===----------------------------------------------------------------------===//
2944// Declaration Serialization
2945//===----------------------------------------------------------------------===//
2946
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002947/// \brief Write the block containing all of the declaration IDs
2948/// lexically declared within the given DeclContext.
2949///
2950/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2951/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002952uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002953 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002954 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002955 return 0;
2956
Douglas Gregor8f45df52009-04-16 22:23:12 +00002957 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002958 SmallVector<uint32_t, 128> KindDeclPairs;
2959 for (const auto *D : DC->decls()) {
2960 KindDeclPairs.push_back(D->getKind());
2961 KindDeclPairs.push_back(GetDeclRef(D));
2962 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002963
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002964 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002965 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002966 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2967 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002968 return Offset;
2969}
2970
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002971void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002972 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002973
2974 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002975 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002976 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002980 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002981 {
2982 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2983 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2984 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2985 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002986
2987 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002988 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002989 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002993 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002994 {
2995 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2996 FirstDeclID - NUM_PREDEF_DECL_IDS};
2997 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2998 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002999}
3000
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003001void ASTWriter::WriteFileDeclIDsMap() {
3002 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003003
Chandler Carruthb306d732015-03-27 00:31:20 +00003004 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3005 FileDeclIDs.begin(), FileDeclIDs.end());
3006 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3007 llvm::less_first());
3008
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003009 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003010 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3011 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3012 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3013 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3014 for (auto &LocDeclEntry : Info.DeclIDs)
3015 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003016 }
3017
David Blaikieb44f0bf2017-01-04 22:36:43 +00003018 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003019 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003022 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003023 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3024 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003025 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003026}
3027
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003028void ASTWriter::WriteComments() {
3029 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003030 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003031 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003032 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003033 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003034 AddSourceRange(I->getSourceRange(), Record);
3035 Record.push_back(I->getKind());
3036 Record.push_back(I->isTrailingComment());
3037 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003038 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3039 }
3040 Stream.ExitBlock();
3041}
3042
Douglas Gregorc5046832009-04-27 18:38:38 +00003043//===----------------------------------------------------------------------===//
3044// Global Method Pool and Selector Serialization
3045//===----------------------------------------------------------------------===//
3046
Douglas Gregore84a9da2009-04-20 20:36:09 +00003047namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003048
Douglas Gregorc78d3462009-04-24 21:10:55 +00003049// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003050class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003051 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003052
3053public:
3054 typedef Selector key_type;
3055 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003056
Sebastian Redl834bb972010-08-04 17:20:04 +00003057 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003058 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003059 ObjCMethodList Instance, Factory;
3060 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003061 typedef const data_type& data_type_ref;
3062
Justin Bogner25463f12014-04-18 20:27:24 +00003063 typedef unsigned hash_value_type;
3064 typedef unsigned offset_type;
3065
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003066 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003067
Justin Bogner25463f12014-04-18 20:27:24 +00003068 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003069 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003070 }
Mike Stump11289f42009-09-09 15:08:12 +00003071
3072 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003073 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003074 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003075 using namespace llvm::support;
3076 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003077 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003078 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003079 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3080 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003081 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003082 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003083 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003084 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003085 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003086 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003087 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003088 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003089 return std::make_pair(KeyLen, DataLen);
3090 }
Mike Stump11289f42009-09-09 15:08:12 +00003091
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003092 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003093 using namespace llvm::support;
3094 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003095 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003096 assert((Start >> 32) == 0 && "Selector key offset too large");
3097 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003098 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003099 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003100 if (N == 0)
3101 N = 1;
3102 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003103 LE.write<uint32_t>(
3104 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003105 }
Mike Stump11289f42009-09-09 15:08:12 +00003106
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003107 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003108 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003109 using namespace llvm::support;
3110 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003111 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003112 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003113 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003114 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003115 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003116 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003117 ++NumInstanceMethods;
3118
3119 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003120 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003121 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003122 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003123 ++NumFactoryMethods;
3124
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003125 unsigned InstanceBits = Methods.Instance.getBits();
3126 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003127 unsigned InstanceHasMoreThanOneDeclBit =
3128 Methods.Instance.hasMoreThanOneDecl();
3129 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3130 (InstanceHasMoreThanOneDeclBit << 2) |
3131 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003132 unsigned FactoryBits = Methods.Factory.getBits();
3133 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003134 unsigned FactoryHasMoreThanOneDeclBit =
3135 Methods.Factory.hasMoreThanOneDecl();
3136 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3137 (FactoryHasMoreThanOneDeclBit << 2) |
3138 FactoryBits;
3139 LE.write<uint16_t>(FullInstanceBits);
3140 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003141 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003142 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003143 if (Method->getMethod())
3144 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003145 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003146 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003147 if (Method->getMethod())
3148 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003149
3150 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003151 }
3152};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003153
Douglas Gregorc78d3462009-04-24 21:10:55 +00003154} // end anonymous namespace
3155
Sebastian Redla19a67f2010-08-03 21:58:15 +00003156/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003157///
3158/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003159/// in an on-disk hash table indexed by the selector. The hash table also
3160/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003161void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003162 using namespace llvm;
3163
Sebastian Redla19a67f2010-08-03 21:58:15 +00003164 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003165 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003166 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003167 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003168 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003169 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003170 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003171 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003172
Sebastian Redla19a67f2010-08-03 21:58:15 +00003173 // Create the on-disk hash table representation. We walk through every
3174 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003175 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003176 for (auto &SelectorAndID : SelectorIDs) {
3177 Selector S = SelectorAndID.first;
3178 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003179 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003180 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003181 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003182 ObjCMethodList(),
3183 ObjCMethodList()
3184 };
3185 if (F != SemaRef.MethodPool.end()) {
3186 Data.Instance = F->second.first;
3187 Data.Factory = F->second.second;
3188 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003189 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003190 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003191 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003192 // Selector already exists. Did it change?
3193 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003194 for (ObjCMethodList *M = &Data.Instance;
3195 !changed && M && M->getMethod(); M = M->getNext()) {
3196 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003197 changed = true;
3198 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003199 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003200 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003201 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003202 changed = true;
3203 }
3204 if (!changed)
3205 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003206 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003207 // A new method pool entry.
3208 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003209 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003210 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003211 }
3212
Douglas Gregorc78d3462009-04-24 21:10:55 +00003213 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003214 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003215 uint32_t BucketOffset;
3216 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003217 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003218 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003219 llvm::raw_svector_ostream Out(MethodPool);
3220 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003221 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003222 BucketOffset = Generator.Emit(Out, Trait);
3223 }
3224
3225 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003226 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003231 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003232
Douglas Gregor95c13f52009-04-25 17:48:32 +00003233 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003234 {
3235 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3236 NumTableEntries};
3237 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3238 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003239
3240 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003241 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003242 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003246 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003247
3248 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003249 {
3250 RecordData::value_type Record[] = {
3251 SELECTOR_OFFSETS, SelectorOffsets.size(),
3252 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3253 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3254 bytes(SelectorOffsets));
3255 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003256 }
3257}
3258
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003259/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003260void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003261 using namespace llvm;
3262 if (SemaRef.ReferencedSelectors.empty())
3263 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003264
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003265 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003266 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003267
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003268 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003269 // very tricky to fix, and given that @selector shouldn't really appear in
3270 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003271 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3272 Selector Sel = SelectorAndLocation.first;
3273 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003274 Writer.AddSelectorRef(Sel);
3275 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003276 }
Richard Smithe64e7452016-04-18 21:54:58 +00003277 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003278}
3279
Douglas Gregorc5046832009-04-27 18:38:38 +00003280//===----------------------------------------------------------------------===//
3281// Identifier Table Serialization
3282//===----------------------------------------------------------------------===//
3283
Richard Smithb3761912015-02-05 23:08:52 +00003284/// Determine the declaration that should be put into the name lookup table to
3285/// represent the given declaration in this module. This is usually D itself,
3286/// but if D was imported and merged into a local declaration, we want the most
3287/// recent local declaration instead. The chosen declaration will be the most
3288/// recent declaration in any module that imports this one.
3289static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3290 NamedDecl *D) {
3291 if (!LangOpts.Modules || !D->isFromASTFile())
3292 return D;
3293
3294 if (Decl *Redecl = D->getPreviousDecl()) {
3295 // For Redeclarable decls, a prior declaration might be local.
3296 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003297 // If we find a local decl, we're done.
3298 if (!Redecl->isFromASTFile()) {
3299 // Exception: in very rare cases (for injected-class-names), not all
3300 // redeclarations are in the same semantic context. Skip ones in a
3301 // different context. They don't go in this lookup table at all.
3302 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3303 D->getDeclContext()->getRedeclContext()))
3304 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003305 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003306 }
3307
Richard Smithfe620d22015-03-05 23:24:12 +00003308 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003309 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003310 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003311 break;
3312 }
3313 } else if (Decl *First = D->getCanonicalDecl()) {
3314 // For Mergeable decls, the first decl might be local.
3315 if (!First->isFromASTFile())
3316 return cast<NamedDecl>(First);
3317 }
3318
3319 // All declarations are imported. Our most recent declaration will also be
3320 // the most recent one in anyone who imports us.
3321 return D;
3322}
3323
Douglas Gregorc78d3462009-04-24 21:10:55 +00003324namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003325
Richard Smithcf97cf62015-04-19 01:34:23 +00003326class ASTIdentifierTableTrait {
3327 ASTWriter &Writer;
3328 Preprocessor &PP;
3329 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003330 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003331 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003332 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003333
3334 /// \brief Determines whether this is an "interesting" identifier that needs a
3335 /// full IdentifierInfo structure written into the hash table. Notably, this
3336 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3337 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003338 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003339 if (MacroOffset ||
3340 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003341 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003342 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003343 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003344 return true;
3345
3346 return false;
3347 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003348
Douglas Gregore84a9da2009-04-20 20:36:09 +00003349public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003350 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003351 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003352
Sebastian Redl539c5062010-08-18 23:57:32 +00003353 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003354 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003355
Justin Bogner25463f12014-04-18 20:27:24 +00003356 typedef unsigned hash_value_type;
3357 typedef unsigned offset_type;
3358
Richard Smithd7329392015-04-21 21:46:32 +00003359 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003360 IdentifierResolver &IdResolver, bool IsModule,
3361 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003362 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003363 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3364 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003365
Richard Smithd79514e2016-02-05 19:03:40 +00003366 bool needDecls() const { return NeedDecls; }
3367
Justin Bogner25463f12014-04-18 20:27:24 +00003368 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003369 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003370 }
Mike Stump11289f42009-09-09 15:08:12 +00003371
Richard Smith33e0f7e2015-07-22 02:08:40 +00003372 bool isInterestingIdentifier(const IdentifierInfo *II) {
3373 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3374 return isInterestingIdentifier(II, MacroOffset);
3375 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003376
Richard Smith9c254182015-07-19 21:41:12 +00003377 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3378 return isInterestingIdentifier(II, 0);
3379 }
3380
Mike Stump11289f42009-09-09 15:08:12 +00003381 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003382 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003383 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003384 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003385 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3386 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003387 DataLen += 2; // 2 bytes for builtin ID
3388 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003389 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003390 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003391
Richard Smitha534a312015-07-21 23:54:07 +00003392 if (NeedDecls) {
3393 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3394 DEnd = IdResolver.end();
3395 D != DEnd; ++D)
3396 DataLen += 4;
3397 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003398 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003399 using namespace llvm::support;
3400 endian::Writer<little> LE(Out);
3401
Richard Smithdf8a8312015-03-13 04:05:01 +00003402 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003403 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003404 // We emit the key length after the data length so that every
3405 // string is preceded by a 16-bit length. This matches the PTH
3406 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003407 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003408 return std::make_pair(KeyLen, DataLen);
3409 }
Mike Stump11289f42009-09-09 15:08:12 +00003410
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003411 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003412 unsigned KeyLen) {
3413 // Record the location of the key data. This is used when generating
3414 // the mapping from persistent IDs to strings.
3415 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003416
3417 // Emit the offset of the key/data length information to the interesting
3418 // identifiers table if necessary.
3419 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3420 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3421
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003422 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003423 }
Mike Stump11289f42009-09-09 15:08:12 +00003424
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003425 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003426 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003427 using namespace llvm::support;
3428 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003429
Richard Smithd7329392015-04-21 21:46:32 +00003430 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3431 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003432 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003433 return;
3434 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003435
Justin Bognere1c147c2014-03-28 22:03:19 +00003436 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003437 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3438 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003439 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003440 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003441 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003442 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003443 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3444 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003445 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003446 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003447 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003448 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003449
Richard Smithd7329392015-04-21 21:46:32 +00003450 if (HadMacroDefinition)
3451 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003452
Richard Smitha534a312015-07-21 23:54:07 +00003453 if (NeedDecls) {
3454 // Emit the declaration IDs in reverse order, because the
3455 // IdentifierResolver provides the declarations as they would be
3456 // visible (e.g., the function "stat" would come before the struct
3457 // "stat"), but the ASTReader adds declarations to the end of the list
3458 // (so we need to see the struct "stat" before the function "stat").
3459 // Only emit declarations that aren't from a chained PCH, though.
3460 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3461 IdResolver.end());
3462 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3463 DEnd = Decls.rend();
3464 D != DEnd; ++D)
3465 LE.write<uint32_t>(
3466 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3467 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003468 }
3469};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003470
Douglas Gregore84a9da2009-04-20 20:36:09 +00003471} // end anonymous namespace
3472
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003473/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003474///
3475/// The identifier table consists of a blob containing string data
3476/// (the actual identifiers themselves) and a separate "offsets" index
3477/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003478void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3479 IdentifierResolver &IdResolver,
3480 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003481 using namespace llvm;
3482
Richard Smith33e0f7e2015-07-22 02:08:40 +00003483 RecordData InterestingIdents;
3484
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003485 // Create and write out the blob that contains the identifier
3486 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003487 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003488 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003489 ASTIdentifierTableTrait Trait(
3490 *this, PP, IdResolver, IsModule,
3491 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003492
Douglas Gregore6648fb2009-04-28 20:33:11 +00003493 // Look for any identifiers that were named while processing the
3494 // headers, but are otherwise not needed. We add these to the hash
3495 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003496 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003497 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003498 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003499 for (const auto &ID : PP.getIdentifierTable())
3500 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003501 // Sort the identifiers lexicographically before getting them references so
3502 // that their order is stable.
3503 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3504 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003505 if (Trait.isInterestingNonMacroIdentifier(II))
3506 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003507
Sebastian Redlff4a2952010-07-23 23:49:55 +00003508 // Create the on-disk hash table representation. We only store offsets
3509 // for identifiers that appear here for the first time.
3510 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003511 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003512 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003513 IdentID ID = IdentIDPair.second;
3514 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003515 // Write out identifiers if either the ID is local or the identifier has
3516 // changed since it was loaded.
3517 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3518 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003519 (Trait.needDecls() &&
3520 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003521 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003522 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003523
Douglas Gregore84a9da2009-04-20 20:36:09 +00003524 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003525 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003526 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003527 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003528 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003529 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003530 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003531 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003532 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003533 }
3534
3535 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003536 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003537 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003540 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003541
3542 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003543 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003544 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003545 }
3546
3547 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003548 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003549 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003553 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003554
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003555#ifndef NDEBUG
3556 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3557 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3558#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003559
3560 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3561 IdentifierOffsets.size(),
3562 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003563 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003564 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003565
3566 // In C++, write the list of interesting identifiers (those that are
3567 // defined as macros, poisoned, or similar unusual things).
3568 if (!InterestingIdents.empty())
3569 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003570}
3571
Douglas Gregorc5046832009-04-27 18:38:38 +00003572//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003573// DeclContext's Name Lookup Table Serialization
3574//===----------------------------------------------------------------------===//
3575
3576namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003577
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003578// Trait used for the on-disk hash table used in the method pool.
3579class ASTDeclContextNameLookupTrait {
3580 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003581 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003582
3583public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003584 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003585 typedef key_type key_type_ref;
3586
Richard Smithd88a7f12015-09-01 20:35:42 +00003587 /// A start and end index into DeclIDs, representing a sequence of decls.
3588 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003589 typedef const data_type& data_type_ref;
3590
Justin Bogner25463f12014-04-18 20:27:24 +00003591 typedef unsigned hash_value_type;
3592 typedef unsigned offset_type;
3593
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003594 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3595
Richard Smithd88a7f12015-09-01 20:35:42 +00003596 template<typename Coll>
3597 data_type getData(const Coll &Decls) {
3598 unsigned Start = DeclIDs.size();
3599 for (NamedDecl *D : Decls) {
3600 DeclIDs.push_back(
3601 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3602 }
3603 return std::make_pair(Start, DeclIDs.size());
3604 }
3605
3606 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3607 unsigned Start = DeclIDs.size();
3608 for (auto ID : FromReader)
3609 DeclIDs.push_back(ID);
3610 return std::make_pair(Start, DeclIDs.size());
3611 }
3612
3613 static bool EqualKey(key_type_ref a, key_type_ref b) {
3614 return a == b;
3615 }
3616
Richard Smitha06c7e62015-08-26 23:55:49 +00003617 hash_value_type ComputeHash(DeclarationNameKey Name) {
3618 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003619 }
3620
Richard Smithd88a7f12015-09-01 20:35:42 +00003621 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3622 assert(Writer.hasChain() &&
3623 "have reference to loaded module file but no chain?");
3624
3625 using namespace llvm::support;
3626 endian::Writer<little>(Out)
3627 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3628 }
3629
Richard Smitha06c7e62015-08-26 23:55:49 +00003630 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3631 DeclarationNameKey Name,
3632 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003633 using namespace llvm::support;
3634 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003635 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003636 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003637 case DeclarationName::Identifier:
3638 case DeclarationName::ObjCZeroArgSelector:
3639 case DeclarationName::ObjCOneArgSelector:
3640 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003641 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003642 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003643 KeyLen += 4;
3644 break;
3645 case DeclarationName::CXXOperatorName:
3646 KeyLen += 1;
3647 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003648 case DeclarationName::CXXConstructorName:
3649 case DeclarationName::CXXDestructorName:
3650 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003651 case DeclarationName::CXXUsingDirective:
3652 break;
3653 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003654 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003655
Richard Smithf02662d2015-07-30 03:17:16 +00003656 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003657 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3658 assert(uint16_t(DataLen) == DataLen &&
3659 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003660 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003661
3662 return std::make_pair(KeyLen, DataLen);
3663 }
3664
Richard Smitha06c7e62015-08-26 23:55:49 +00003665 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003666 using namespace llvm::support;
3667 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003668 LE.write<uint8_t>(Name.getKind());
3669 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003670 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003671 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003672 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003673 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003674 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003675 case DeclarationName::ObjCZeroArgSelector:
3676 case DeclarationName::ObjCOneArgSelector:
3677 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003678 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003679 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003680 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003681 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003682 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003683 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003684 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003685 case DeclarationName::CXXConstructorName:
3686 case DeclarationName::CXXDestructorName:
3687 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003688 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003689 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003690 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003691
3692 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003693 }
3694
Richard Smitha06c7e62015-08-26 23:55:49 +00003695 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3696 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003697 using namespace llvm::support;
3698 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003699 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003700 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3701 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003702 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3703 }
3704};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003705
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003706} // end anonymous namespace
3707
Chandler Carruthe972c362015-03-26 03:11:40 +00003708bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3709 DeclContext *DC) {
3710 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3711}
3712
3713bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3714 DeclContext *DC) {
3715 for (auto *D : Result.getLookupResult())
3716 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3717 return false;
3718
3719 return true;
3720}
3721
Richard Smithd88a7f12015-09-01 20:35:42 +00003722void
Chandler Carruthe972c362015-03-26 03:11:40 +00003723ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003724 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003725 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3726 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003727 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003728
Chandler Carruthe972c362015-03-26 03:11:40 +00003729 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003730 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003731 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3732
3733 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003734 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3735 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003736 ASTDeclContextNameLookupTrait Trait(*this);
3737
Chandler Carruthe972c362015-03-26 03:11:40 +00003738 // The first step is to collect the declaration names which we need to
3739 // serialize into the name lookup table, and to collect them in a stable
3740 // order.
3741 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003742
Chandler Carruthe972c362015-03-26 03:11:40 +00003743 // We also build up small sets of the constructor and conversion function
3744 // names which are visible.
3745 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003746
Chandler Carruthe972c362015-03-26 03:11:40 +00003747 for (auto &Lookup : *DC->buildLookup()) {
3748 auto &Name = Lookup.first;
3749 auto &Result = Lookup.second;
3750
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003751 // If there are no local declarations in our lookup result, we
3752 // don't need to write an entry for the name at all. If we can't
3753 // write out a lookup set without performing more deserialization,
3754 // just skip this entry.
3755 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003756 isLookupResultEntirelyExternal(Result, DC))
3757 continue;
3758
3759 // We also skip empty results. If any of the results could be external and
3760 // the currently available results are empty, then all of the results are
3761 // external and we skip it above. So the only way we get here with an empty
3762 // results is when no results could have been external *and* we have
3763 // external results.
3764 //
3765 // FIXME: While we might want to start emitting on-disk entries for negative
3766 // lookups into a decl context as an optimization, today we *have* to skip
3767 // them because there are names with empty lookup results in decl contexts
3768 // which we can't emit in any stable ordering: we lookup constructors and
3769 // conversion functions in the enclosing namespace scope creating empty
3770 // results for them. This in almost certainly a bug in Clang's name lookup,
3771 // but that is likely to be hard or impossible to fix and so we tolerate it
3772 // here by omitting lookups with empty results.
3773 if (Lookup.second.getLookupResult().empty())
3774 continue;
3775
3776 switch (Lookup.first.getNameKind()) {
3777 default:
3778 Names.push_back(Lookup.first);
3779 break;
3780
Richard Smithcd45dbc2014-04-19 03:48:30 +00003781 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003782 assert(isa<CXXRecordDecl>(DC) &&
3783 "Cannot have a constructor name outside of a class!");
3784 ConstructorNameSet.insert(Name);
3785 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003786
3787 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003788 assert(isa<CXXRecordDecl>(DC) &&
3789 "Cannot have a conversion function name outside of a class!");
3790 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003791 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003792 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003793 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003794
Chandler Carruthe972c362015-03-26 03:11:40 +00003795 // Sort the names into a stable order.
3796 std::sort(Names.begin(), Names.end());
3797
Chandler Carruthffbf7052015-03-26 22:27:09 +00003798 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003799 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003800 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003801
Chandler Carruthffbf7052015-03-26 22:27:09 +00003802 // First we try the easy case by forming the current context's constructor
3803 // name and adding that name first. This is a very useful optimization to
3804 // avoid walking the lexical declarations in many cases, and it also
3805 // handles the only case where a constructor name can come from some other
3806 // lexical context -- when that name is an implicit constructor merged from
3807 // another declaration in the redecl chain. Any non-implicit constructor or
3808 // conversion function which doesn't occur in all the lexical contexts
3809 // would be an ODR violation.
3810 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3811 Context->getCanonicalType(Context->getRecordType(D)));
3812 if (ConstructorNameSet.erase(ImplicitCtorName))
3813 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003814
Chandler Carruthffbf7052015-03-26 22:27:09 +00003815 // If we still have constructors or conversion functions, we walk all the
3816 // names in the decl and add the constructors and conversion functions
3817 // which are visible in the order they lexically occur within the context.
3818 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3819 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3820 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3821 auto Name = ChildND->getDeclName();
3822 switch (Name.getNameKind()) {
3823 default:
3824 continue;
3825
3826 case DeclarationName::CXXConstructorName:
3827 if (ConstructorNameSet.erase(Name))
3828 Names.push_back(Name);
3829 break;
3830
3831 case DeclarationName::CXXConversionFunctionName:
3832 if (ConversionNameSet.erase(Name))
3833 Names.push_back(Name);
3834 break;
3835 }
3836
3837 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3838 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003839 }
3840
Chandler Carruthe972c362015-03-26 03:11:40 +00003841 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3842 "constructors by walking all the "
3843 "lexical members of the context.");
3844 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3845 "conversion functions by walking all "
3846 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003847 }
3848
Chandler Carruthe972c362015-03-26 03:11:40 +00003849 // Next we need to do a lookup with each name into this decl context to fully
3850 // populate any results from external sources. We don't actually use the
3851 // results of these lookups because we only want to use the results after all
3852 // results have been loaded and the pointers into them will be stable.
3853 for (auto &Name : Names)
3854 DC->lookup(Name);
3855
3856 // Now we need to insert the results for each name into the hash table. For
3857 // constructor names and conversion function names, we actually need to merge
3858 // all of the results for them into one list of results each and insert
3859 // those.
3860 SmallVector<NamedDecl *, 8> ConstructorDecls;
3861 SmallVector<NamedDecl *, 8> ConversionDecls;
3862
3863 // Now loop over the names, either inserting them or appending for the two
3864 // special cases.
3865 for (auto &Name : Names) {
3866 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3867
3868 switch (Name.getNameKind()) {
3869 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003870 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003871 break;
3872
3873 case DeclarationName::CXXConstructorName:
3874 ConstructorDecls.append(Result.begin(), Result.end());
3875 break;
3876
3877 case DeclarationName::CXXConversionFunctionName:
3878 ConversionDecls.append(Result.begin(), Result.end());
3879 break;
3880 }
3881 }
3882
3883 // Handle our two special cases if we ended up having any. We arbitrarily use
3884 // the first declaration's name here because the name itself isn't part of
3885 // the key, only the kind of name is used.
3886 if (!ConstructorDecls.empty())
3887 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003888 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003889 if (!ConversionDecls.empty())
3890 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003891 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003892
Richard Smithd88a7f12015-09-01 20:35:42 +00003893 // Create the on-disk hash table. Also emit the existing imported and
3894 // merged table if there is one.
3895 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3896 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003897}
3898
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003899/// \brief Write the block containing all of the declaration IDs
3900/// visible from the given DeclContext.
3901///
3902/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003903/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003904uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3905 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003906 // If we imported a key declaration of this namespace, write the visible
3907 // lookup results as an update record for it rather than including them
3908 // on this declaration. We will only look at key declarations on reload.
3909 if (isa<NamespaceDecl>(DC) && Chain &&
3910 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3911 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003912 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003913 Prev = Prev->getPreviousDecl())
3914 if (!Prev->isFromASTFile())
3915 return 0;
3916
3917 // Note that we need to emit an update record for the primary context.
3918 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3919
3920 // Make sure all visible decls are written. They will be recorded later. We
3921 // do this using a side data structure so we can sort the names into
3922 // a deterministic order.
3923 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3924 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3925 LookupResults;
3926 if (Map) {
3927 LookupResults.reserve(Map->size());
3928 for (auto &Entry : *Map)
3929 LookupResults.push_back(
3930 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3931 }
3932
3933 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3934 for (auto &NameAndResult : LookupResults) {
3935 DeclarationName Name = NameAndResult.first;
3936 DeclContext::lookup_result Result = NameAndResult.second;
3937 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3938 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3939 // We have to work around a name lookup bug here where negative lookup
3940 // results for these names get cached in namespace lookup tables (these
3941 // names should never be looked up in a namespace).
3942 assert(Result.empty() && "Cannot have a constructor or conversion "
3943 "function name in a namespace!");
3944 continue;
3945 }
3946
3947 for (NamedDecl *ND : Result)
3948 if (!ND->isFromASTFile())
3949 GetDeclRef(ND);
3950 }
3951
3952 return 0;
3953 }
3954
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003955 if (DC->getPrimaryContext() != DC)
3956 return 0;
3957
Chandler Carruthe972c362015-03-26 03:11:40 +00003958 // Skip contexts which don't support name lookup.
3959 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003960 return 0;
3961
3962 // If not in C++, we perform name lookup for the translation unit via the
3963 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003964 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003965 return 0;
3966
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003967 // Serialize the contents of the mapping used for lookup. Note that,
3968 // although we have two very different code paths, the serialized
3969 // representation is the same for both cases: a declaration name,
3970 // followed by a size, followed by references to the visible
3971 // declarations that have that name.
3972 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003973 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003974 if (!Map || Map->empty())
3975 return 0;
3976
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003977 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003978 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003979 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003980
3981 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003982 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003983 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003984 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003985 ++NumVisibleDeclContexts;
3986 return Offset;
3987}
3988
Sebastian Redla4071b42010-08-24 00:50:09 +00003989/// \brief Write an UPDATE_VISIBLE block for the given context.
3990///
3991/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3992/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003993/// (in C++), for namespaces, and for classes with forward-declared unscoped
3994/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003995void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003996 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003997 if (!Map || Map->empty())
3998 return;
3999
Sebastian Redla4071b42010-08-24 00:50:09 +00004000 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004001 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004002 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004003
Richard Smith5fc18a92015-07-12 23:43:21 +00004004 // If we're updating a namespace, select a key declaration as the key for the
4005 // update record; those are the only ones that will be checked on reload.
4006 if (isa<NamespaceDecl>(DC))
4007 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4008
Sebastian Redla4071b42010-08-24 00:50:09 +00004009 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004010 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004011 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004012}
4013
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004014/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4015void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004016 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004017 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4018}
4019
4020/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4021void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004022 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004023 return;
4024
4025 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4026 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004027 for (const auto &I:Opts.OptMap) {
4028 AddString(I.getKey(), Record);
4029 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004030 Record.push_back(V.Supported ? 1 : 0);
4031 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004032 Record.push_back(V.Avail);
4033 Record.push_back(V.Core);
4034 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004035 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4036}
4037
Yaxun Liu5b746652016-12-18 05:18:55 +00004038void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4039 if (!SemaRef.Context.getLangOpts().OpenCL)
4040 return;
4041
4042 RecordData Record;
4043 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4044 Record.push_back(
4045 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4046 Record.push_back(I.second.size());
4047 for (auto Ext : I.second)
4048 AddString(Ext, Record);
4049 }
4050 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4051}
4052
4053void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4054 if (!SemaRef.Context.getLangOpts().OpenCL)
4055 return;
4056
4057 RecordData Record;
4058 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4059 Record.push_back(getDeclID(I.first));
4060 Record.push_back(static_cast<unsigned>(I.second.size()));
4061 for (auto Ext : I.second)
4062 AddString(Ext, Record);
4063 }
4064 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4065}
4066
Justin Lebar67a78a62016-10-08 22:15:58 +00004067void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4068 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4069 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4070 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4071 }
4072}
4073
Douglas Gregor404cdde2012-01-27 01:47:08 +00004074void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004075 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004076 RecordData Categories;
4077
4078 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4079 unsigned Size = 0;
4080 unsigned StartIndex = Categories.size();
4081
4082 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4083
4084 // Allocate space for the size.
4085 Categories.push_back(0);
4086
4087 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004088 for (ObjCInterfaceDecl::known_categories_iterator
4089 Cat = Class->known_categories_begin(),
4090 CatEnd = Class->known_categories_end();
4091 Cat != CatEnd; ++Cat, ++Size) {
4092 assert(getDeclID(*Cat) != 0 && "Bogus category");
4093 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004094 }
4095
4096 // Update the size.
4097 Categories[StartIndex] = Size;
4098
4099 // Record this interface -> category map.
4100 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4101 CategoriesMap.push_back(CatInfo);
4102 }
4103
4104 // Sort the categories map by the definition ID, since the reader will be
4105 // performing binary searches on this information.
4106 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4107
4108 // Emit the categories map.
4109 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004110
David Blaikieb44f0bf2017-01-04 22:36:43 +00004111 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004112 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004115 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004116
4117 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4118 Stream.EmitRecordWithBlob(AbbrevID, Record,
4119 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004120 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004121
Douglas Gregor404cdde2012-01-27 01:47:08 +00004122 // Emit the category lists.
4123 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4124}
4125
Richard Smithe40f2ba2013-08-07 21:41:30 +00004126void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4127 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4128
4129 if (LPTMap.empty())
4130 return;
4131
4132 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004133 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004134 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004135 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004136 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004137 AddDeclRef(LPT.D, Record);
4138 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004139
Justin Lebar28f09c52016-10-10 16:26:08 +00004140 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004141 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004142 }
4143 }
4144 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4145}
4146
Dario Domizioli13a0a382014-05-23 12:13:25 +00004147/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4148void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4149 RecordData Record;
4150 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4151 AddSourceLocation(PragmaLoc, Record);
4152 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4153}
4154
Nico Weber779355f2016-03-02 23:22:00 +00004155/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4156void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4157 RecordData Record;
4158 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4159 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4160}
4161
Nico Weber42932312016-03-03 00:17:35 +00004162/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4163//module.
4164void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4165 RecordData Record;
4166 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4167 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4168 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4169}
4170
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004171/// \brief Write the state of 'pragma pack' at the end of the module.
4172void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
4173 RecordData Record;
4174 Record.push_back(SemaRef.PackStack.CurrentValue);
4175 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4176 Record.push_back(SemaRef.PackStack.Stack.size());
4177 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4178 Record.push_back(StackEntry.Value);
4179 AddSourceLocation(StackEntry.PragmaLocation, Record);
4180 AddString(StackEntry.StackSlotLabel, Record);
4181 }
4182 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4183}
4184
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004185void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4186 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004187 // Enter the extension block.
4188 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4189
4190 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004191 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004192 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
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::VBR, 6));
4197 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004198 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004199
4200 // Emit the metadata record.
4201 RecordData Record;
4202 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4203 Record.push_back(EXTENSION_METADATA);
4204 Record.push_back(Metadata.MajorVersion);
4205 Record.push_back(Metadata.MinorVersion);
4206 Record.push_back(Metadata.BlockName.size());
4207 Record.push_back(Metadata.UserInfo.size());
4208 SmallString<64> Buffer;
4209 Buffer += Metadata.BlockName;
4210 Buffer += Metadata.UserInfo;
4211 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4212
4213 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004214 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004215
4216 // Exit the extension block.
4217 Stream.ExitBlock();
4218}
4219
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004220//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004221// General Serialization Routines
4222//===----------------------------------------------------------------------===//
4223
Richard Smith69c82bf2016-04-01 22:52:03 +00004224/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004225void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4226 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004227 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004228 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004229 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004230 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004231
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004232#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004233
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004234 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004235}
4236
John McCallf413f5e2013-05-03 00:10:13 +00004237void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4238 AddSourceLocation(Tok.getLocation(), Record);
4239 Record.push_back(Tok.getLength());
4240
4241 // FIXME: When reading literal tokens, reconstruct the literal pointer
4242 // if it is needed.
4243 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4244 // FIXME: Should translate token kind to a stable encoding.
4245 Record.push_back(Tok.getKind());
4246 // FIXME: Should translate token flags to a stable encoding.
4247 Record.push_back(Tok.getFlags());
4248}
4249
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004250void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004251 Record.push_back(Str.size());
4252 Record.insert(Record.end(), Str.begin(), Str.end());
4253}
4254
Richard Smith7ed1bc92014-12-05 22:42:13 +00004255bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004256 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004257
Richard Smith54cc3c22014-12-11 20:50:24 +00004258 bool Changed =
4259 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004260
4261 // Remove a prefix to make the path relative, if relevant.
4262 const char *PathBegin = Path.data();
4263 const char *PathPtr =
4264 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4265 if (PathPtr != PathBegin) {
4266 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4267 Changed = true;
4268 }
4269
4270 return Changed;
4271}
4272
4273void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4274 SmallString<128> FilePath(Path);
4275 PreparePathForOutput(FilePath);
4276 AddString(FilePath, Record);
4277}
4278
Mehdi Amini57a41912015-09-10 01:46:39 +00004279void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004280 StringRef Path) {
4281 SmallString<128> FilePath(Path);
4282 PreparePathForOutput(FilePath);
4283 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4284}
4285
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004286void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4287 RecordDataImpl &Record) {
4288 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004289 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004290 Record.push_back(*Minor + 1);
4291 else
4292 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004293 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004294 Record.push_back(*Subminor + 1);
4295 else
4296 Record.push_back(0);
4297}
4298
Douglas Gregore84a9da2009-04-20 20:36:09 +00004299/// \brief Note that the identifier II occurs at the given offset
4300/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004301void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004302 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004303 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004304 // up earlier in the chain and thus don't need an offset.
4305 if (ID >= FirstIdentID)
4306 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004307}
4308
Douglas Gregor95c13f52009-04-25 17:48:32 +00004309/// \brief Note that the selector Sel occurs at the given offset
4310/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004311void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004312 unsigned ID = SelectorIDs[Sel];
4313 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004314 // Don't record offsets for selectors that are also available in a different
4315 // file.
4316 if (ID < FirstSelectorID)
4317 return;
4318 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004319}
4320
David Blaikie61137e12017-01-05 18:23:18 +00004321ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004322 SmallVectorImpl<char> &Buffer, MemoryBufferCache &PCMCache,
David Blaikie61137e12017-01-05 18:23:18 +00004323 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4324 bool IncludeTimestamps)
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004325 : Stream(Stream), Buffer(Buffer), PCMCache(PCMCache),
4326 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004327 for (const auto &Ext : Extensions) {
4328 if (auto Writer = Ext->createExtensionWriter(*this))
4329 ModuleFileExtensionWriters.push_back(std::move(Writer));
4330 }
4331}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004332
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004333ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004334 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004335}
4336
Richard Smithb3761912015-02-05 23:08:52 +00004337const LangOptions &ASTWriter::getLangOpts() const {
4338 assert(WritingAST && "can't determine lang opts when not writing AST");
4339 return Context->getLangOpts();
4340}
4341
Richard Smithe75ee0f2015-08-17 07:13:32 +00004342time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4343 return IncludeTimestamps ? E->getModificationTime() : 0;
4344}
4345
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004346ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4347 const std::string &OutputFile,
4348 Module *WritingModule, StringRef isysroot,
4349 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004350 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004351
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004352 ASTHasCompilerErrors = hasErrors;
4353
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004354 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004355 Stream.Emit((unsigned)'C', 8);
4356 Stream.Emit((unsigned)'P', 8);
4357 Stream.Emit((unsigned)'C', 8);
4358 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004359
Chris Lattner28fa4e62009-04-26 22:26:21 +00004360 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004361
Douglas Gregoreda8e122011-08-09 15:13:55 +00004362 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004363 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004364 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004365 ASTFileSignature Signature =
4366 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004367 Context = nullptr;
4368 PP = nullptr;
4369 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004370 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004371
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004372 WritingAST = false;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004373 if (SemaRef.Context.getLangOpts().ImplicitModules && WritingModule) {
4374 // Construct MemoryBuffer and update buffer manager.
4375 PCMCache.addBuffer(OutputFile,
4376 llvm::MemoryBuffer::getMemBufferCopy(
4377 StringRef(Buffer.begin(), Buffer.size())));
4378 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004379 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004380}
4381
Douglas Gregora94a1542011-07-27 21:45:57 +00004382template<typename Vector>
4383static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4384 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004385 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4386 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004387 Writer.AddDeclRef(*I, Record);
4388 }
4389}
4390
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004391ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4392 const std::string &OutputFile,
4393 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004394 using namespace llvm;
4395
Craig Toppera13603a2014-05-22 05:54:18 +00004396 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004397
Douglas Gregorcf68c582011-12-01 22:20:10 +00004398 // Make sure that the AST reader knows to finalize itself.
4399 if (Chain)
4400 Chain->finalizeForWriting();
4401
Sebastian Redl143413f2010-07-12 22:02:52 +00004402 ASTContext &Context = SemaRef.Context;
4403 Preprocessor &PP = SemaRef.PP;
4404
Douglas Gregordab42432011-08-12 00:15:20 +00004405 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004406 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4407 if (D) {
4408 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4409 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004410 }
4411 };
4412 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4413 PREDEF_DECL_TRANSLATION_UNIT_ID);
4414 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4415 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4416 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4417 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4418 PREDEF_DECL_OBJC_PROTOCOL_ID);
4419 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4420 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4421 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4422 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4423 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004424 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004425 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4426 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004427 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004428 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4429 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004430 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4431 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004432 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4433 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004434 RegisterPredefDecl(Context.TypePackElementDecl,
4435 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004436
Chris Lattner0c797362009-09-08 18:19:27 +00004437 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004438 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004439 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004440 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004441 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004442
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004443 // Build a record containing all of the file scoped decls in this file.
4444 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004445 if (!isModule)
4446 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4447 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004448
Douglas Gregor851443c2011-08-12 01:39:19 +00004449 // Build a record containing all of the delegating constructors we still need
4450 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004451 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004452 if (!isModule)
4453 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004454
Douglas Gregor851443c2011-08-12 01:39:19 +00004455 // Write the set of weak, undeclared identifiers. We always write the
4456 // entire table, since later PCH files in a PCH chain are only interested in
4457 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004458 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004459 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4460 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4461 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4462 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4463 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4464 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4465 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004466 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004467
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004468 // Build a record containing all of the ext_vector declarations.
4469 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004470 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004471
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004472 // Build a record containing all of the VTable uses information.
4473 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004474 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004475 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4476 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4477 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4478 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4479 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004480 }
4481
Nico Weber72889432014-09-06 01:25:55 +00004482 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4483 RecordData UnusedLocalTypedefNameCandidates;
4484 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4485 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4486
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004487 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004488 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004489 for (const auto &I : SemaRef.PendingInstantiations) {
4490 AddDeclRef(I.first, PendingInstantiations);
4491 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004492 }
4493 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4494 "There are local ones at end of translation unit!");
4495
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004496 // Build a record containing some declaration references.
4497 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004498 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004499 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4500 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004501 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004502 }
4503
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004504 RecordData CUDASpecialDeclRefs;
4505 if (Context.getcudaConfigureCallDecl()) {
4506 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4507 }
4508
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004509 // Build a record containing all of the known namespaces.
4510 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004511 for (const auto &I : SemaRef.KnownNamespaces) {
4512 if (!I.second)
4513 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004514 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004515
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004516 // Build a record of all used, undefined objects that require definitions.
4517 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004518
4519 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004520 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004521 for (const auto &I : Undefined) {
4522 AddDeclRef(I.first, UndefinedButUsed);
4523 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004524 }
4525
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004526 // Build a record containing all delete-expressions that we would like to
4527 // analyze later in AST.
4528 RecordData DeleteExprsToAnalyze;
4529
4530 for (const auto &DeleteExprsInfo :
4531 SemaRef.getMismatchingDeleteExpressions()) {
4532 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4533 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4534 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4535 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4536 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4537 }
4538 }
4539
Douglas Gregor112b9072012-10-18 05:31:06 +00004540 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004541 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004542
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004543 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004544 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004545
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004546 // This is so that older clang versions, before the introduction
4547 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004548 {
4549 RecordData Record = {VERSION_MAJOR};
4550 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4551 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004552
Douglas Gregor851443c2011-08-12 01:39:19 +00004553 // Create a lexical update block containing all of the declarations in the
4554 // translation unit that do not come from other AST files.
4555 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004556 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4557 for (const auto *D : TU->noload_decls()) {
4558 if (!D->isFromASTFile()) {
4559 NewGlobalKindDeclPairs.push_back(D->getKind());
4560 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4561 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004562 }
4563
David Blaikieb44f0bf2017-01-04 22:36:43 +00004564 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004565 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4566 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004567 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004568 {
4569 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4570 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4571 bytes(NewGlobalKindDeclPairs));
4572 }
4573
Douglas Gregor851443c2011-08-12 01:39:19 +00004574 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004575 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004576 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4577 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004578 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004579 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004580 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004581
4582 // If we have any extern "C" names, write out a visible update for them.
4583 if (Context.ExternCContext)
4584 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004585
4586 // If the translation unit has an anonymous namespace, and we don't already
4587 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004588 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004589 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4590 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004591 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004592 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004593 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004594
Richard Smith5652c0f2014-03-21 01:48:23 +00004595 // Add update records for all mangling numbers and static local numbers.
4596 // These aren't really update records, but this is a convenient way of
4597 // tagging this rare extra data onto the declarations.
4598 for (const auto &Number : Context.MangleNumbers)
4599 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004600 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4601 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004602 for (const auto &Number : Context.StaticLocalNumbers)
4603 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004604 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4605 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004606
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004607 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004608 // an AST file, are registered for serialization. Likewise for template
4609 // specializations added to imported templates.
4610 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004611 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004612 }
4613
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004614 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004615 // serialization, if we're storing decls with identifiers.
4616 if (!WritingModule || !getLangOpts().CPlusPlus) {
4617 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004618 for (const auto &ID : PP.getIdentifierTable()) {
4619 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004620 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4621 IIs.push_back(II);
4622 }
4623 // Sort the identifiers to visit based on their name.
4624 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4625 for (const IdentifierInfo *II : IIs) {
4626 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4627 DEnd = SemaRef.IdResolver.end();
4628 D != DEnd; ++D) {
4629 GetDeclRef(*D);
4630 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004631 }
4632 }
4633
Manman Rena0f31a02016-04-29 19:04:05 +00004634 // For method pool in the module, if it contains an entry for a selector,
4635 // the entry should be complete, containing everything introduced by that
4636 // module and all modules it imports. It's possible that the entry is out of
4637 // date, so we need to pull in the new content here.
4638
4639 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4640 // safe, we copy all selectors out.
4641 llvm::SmallVector<Selector, 256> AllSelectors;
4642 for (auto &SelectorAndID : SelectorIDs)
4643 AllSelectors.push_back(SelectorAndID.first);
4644 for (auto &Selector : AllSelectors)
4645 SemaRef.updateOutOfDateSelector(Selector);
4646
Douglas Gregor5204bde2011-08-02 16:26:37 +00004647 // Form the record of special types.
4648 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004649 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004650 AddTypeRef(Context.getFILEType(), SpecialTypes);
4651 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4652 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4653 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4654 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004655 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004656 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004657
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004658 if (Chain) {
4659 // Write the mapping information describing our module dependencies and how
4660 // each of those modules were mapped into our own offset/ID space, so that
4661 // the reader can build the appropriate mapping to its own offset/ID space.
4662 // The map consists solely of a blob with the following format:
4663 // *(module-name-len:i16 module-name:len*i8
4664 // source-location-offset:i32
4665 // identifier-id:i32
4666 // preprocessed-entity-id:i32
4667 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004668 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004669 // selector-id:i32
4670 // declaration-id:i32
4671 // c++-base-specifiers-id:i32
4672 // type-id:i32)
4673 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004674 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004675 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004677 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004678 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004679 {
4680 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004681 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004682 using namespace llvm::support;
4683 endian::Writer<little> LE(Out);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004684 StringRef FileName = M.FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004685 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004686 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004687
Ben Langmuir785180e2014-10-20 16:27:30 +00004688 // Note: if a base ID was uint max, it would not be possible to load
4689 // another module after it or have more than one entity inside it.
4690 uint32_t None = std::numeric_limits<uint32_t>::max();
4691
4692 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4693 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4694 if (ShouldWrite)
4695 LE.write<uint32_t>(BaseID);
4696 else
4697 LE.write<uint32_t>(None);
4698 };
4699
Ben Langmuirfe971d92014-08-16 04:54:18 +00004700 // These values should be unique within a chain, since they will be read
4701 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004702 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4703 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4704 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4705 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4706 M.NumPreprocessedEntities);
4707 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4708 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4709 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4710 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004711 }
4712 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004713 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004714 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4715 Buffer.data(), Buffer.size());
4716 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004717
Richard Smithb9eab6d2014-03-20 19:44:17 +00004718 RecordData DeclUpdatesOffsetsRecord;
4719
Richard Smith59442b42014-03-20 20:07:19 +00004720 // Keep writing types, declarations, and declaration update records
4721 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004722 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4723 WriteTypeAbbrevs();
4724 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004725 do {
4726 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4727 while (!DeclTypesToEmit.empty()) {
4728 DeclOrType DOT = DeclTypesToEmit.front();
4729 DeclTypesToEmit.pop();
4730 if (DOT.isType())
4731 WriteType(DOT.getType());
4732 else
4733 WriteDecl(Context, DOT.getDecl());
4734 }
4735 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004736 Stream.ExitBlock();
4737
Richard Smithb9eab6d2014-03-20 19:44:17 +00004738 DoneWritingDeclsAndTypes = true;
4739
4740 // These things can only be done once we've written out decls and types.
4741 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004742 if (!DeclUpdatesOffsetsRecord.empty())
4743 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004744 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004745 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004746 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004747 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004748 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004749 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004750 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004751 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004752 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004753 WriteFPPragmaOptions(SemaRef.getFPOptions());
4754 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004755 WriteOpenCLExtensionTypes(SemaRef);
4756 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004757 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004758
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004759 // If we're emitting a module, write out the submodule information.
4760 if (WritingModule)
4761 WriteSubmodules(WritingModule);
4762
Douglas Gregor5204bde2011-08-02 16:26:37 +00004763 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4764
Douglas Gregord4df8652009-04-22 22:02:47 +00004765 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004766 if (!EagerlyDeserializedDecls.empty())
4767 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004768
David Blaikie9ffe5a32017-01-30 05:00:26 +00004769 if (Context.getLangOpts().ModularCodegen)
4770 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4771
Douglas Gregord4df8652009-04-22 22:02:47 +00004772 // Write the record containing tentative definitions.
4773 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004774 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004775
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004776 // Write the record containing unused file scoped decls.
4777 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004778 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004779
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004780 // Write the record containing weak undeclared identifiers.
4781 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004782 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004783 WeakUndeclaredIdentifiers);
4784
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004785 // Write the record containing ext_vector type names.
4786 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004787 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004788
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004789 // Write the record containing VTable uses information.
4790 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004791 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004792
Nico Weber72889432014-09-06 01:25:55 +00004793 // Write the record containing potentially unused local typedefs.
4794 if (!UnusedLocalTypedefNameCandidates.empty())
4795 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4796 UnusedLocalTypedefNameCandidates);
4797
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004798 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004799 if (!PendingInstantiations.empty())
4800 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004801
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004802 // Write the record containing declaration references of Sema.
4803 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004804 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004805
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004806 // Write the record containing CUDA-specific declaration references.
4807 if (!CUDASpecialDeclRefs.empty())
4808 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004809
4810 // Write the delegating constructors.
4811 if (!DelegatingCtorDecls.empty())
4812 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004813
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004814 // Write the known namespaces.
4815 if (!KnownNamespaces.empty())
4816 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004817
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004818 // Write the undefined internal functions and variables, and inline functions.
4819 if (!UndefinedButUsed.empty())
4820 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004821
4822 if (!DeleteExprsToAnalyze.empty())
4823 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4824
Douglas Gregor851443c2011-08-12 01:39:19 +00004825 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004826 for (auto *DC : UpdatedDeclContexts)
4827 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004828
Douglas Gregor959bb062011-12-03 01:15:29 +00004829 if (!WritingModule) {
4830 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004831 struct ModuleInfo {
4832 uint64_t ID;
4833 Module *M;
4834 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4835 };
Richard Smith56be7542014-03-21 00:33:59 +00004836 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004837 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004838 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004839 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4840 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004841 }
Richard Smith56be7542014-03-21 00:33:59 +00004842
4843 if (!Imports.empty()) {
4844 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4845 return A.ID < B.ID;
4846 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004847 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4848 return A.ID == B.ID;
4849 };
Richard Smith56be7542014-03-21 00:33:59 +00004850
4851 // Sort and deduplicate module IDs.
4852 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004853 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004854 Imports.end());
4855
4856 RecordData ImportedModules;
4857 for (const auto &Import : Imports) {
4858 ImportedModules.push_back(Import.ID);
4859 // FIXME: If the module has macros imported then later has declarations
4860 // imported, this location won't be the right one as a location for the
4861 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004862 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004863 }
4864
Douglas Gregor959bb062011-12-03 01:15:29 +00004865 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4866 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004867 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004868
Douglas Gregor404cdde2012-01-27 01:47:08 +00004869 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004870 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004871 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004872 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004873 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004874 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004875 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004876
Douglas Gregor08f01292009-04-17 22:13:46 +00004877 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004878 RecordData::value_type Record[] = {
4879 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004880 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004881 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004882
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004883 // Write the module file extension blocks.
4884 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004885 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004886
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004887 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004888}
4889
Richard Smithb9eab6d2014-03-20 19:44:17 +00004890void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004891 if (DeclUpdates.empty())
4892 return;
4893
Richard Smith59442b42014-03-20 20:07:19 +00004894 DeclUpdateMap LocalUpdates;
4895 LocalUpdates.swap(DeclUpdates);
4896
Richard Smith6ef42932014-03-20 21:02:00 +00004897 for (auto &DeclUpdate : LocalUpdates) {
4898 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004899
Richard Smithd28ac5b2014-03-22 23:33:22 +00004900 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004901 RecordData RecordData;
4902 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004903 for (auto &Update : DeclUpdate.second) {
4904 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4905
Richard Smith69c82bf2016-04-01 22:52:03 +00004906 // An updated body is emitted last, so that the reader doesn't need
4907 // to skip over the lazy body to reach statements for other records.
4908 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4909 HasUpdatedBody = true;
4910 else
4911 Record.push_back(Kind);
4912
Richard Smith6ef42932014-03-20 21:02:00 +00004913 switch (Kind) {
4914 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4915 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4916 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004917 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004918 Record.push_back(GetDeclRef(Update.getDecl()));
4919 break;
4920
Richard Smith4d235792014-08-07 18:53:08 +00004921 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004922 break;
4923
Richard Smith4d235792014-08-07 18:53:08 +00004924 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004925 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004926 break;
4927
John McCall32791cc2016-01-06 22:34:54 +00004928 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004929 Record.AddStmt(const_cast<Expr *>(
4930 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004931 break;
4932
Richard Smith4b054b22016-08-24 21:25:37 +00004933 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4934 Record.AddStmt(
4935 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4936 break;
4937
Richard Smithcd45dbc2014-04-19 03:48:30 +00004938 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4939 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004940 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004941 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004942 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004943 *Context, const_cast<CXXRecordDecl *>(RD)));
4944
4945 // This state is sometimes updated by template instantiation, when we
4946 // switch from the specialization referring to the template declaration
4947 // to it referring to the template definition.
4948 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4949 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004950 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004951 } else {
4952 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4953 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004954 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004955
4956 // The instantiation might have been resolved to a partial
4957 // specialization. If so, record which one.
4958 auto From = Spec->getInstantiatedFrom();
4959 if (auto PartialSpec =
4960 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4961 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004962 Record.AddDeclRef(PartialSpec);
4963 Record.AddTemplateArgumentList(
4964 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004965 } else {
4966 Record.push_back(false);
4967 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004968 }
4969 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004970 Record.AddSourceLocation(RD->getLocation());
4971 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004972 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004973
4974 // Instantiation may change attributes; write them all out afresh.
4975 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004976 if (D->hasAttrs())
4977 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004978
4979 // FIXME: Ensure we don't get here for explicit instantiations.
4980 break;
4981 }
4982
Richard Smithf8134002015-03-10 01:41:22 +00004983 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004984 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004985 break;
4986
Richard Smith564417a2014-03-20 21:47:22 +00004987 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4988 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004989 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4990 Record);
4991 break;
4992
Richard Smith6ef42932014-03-20 21:02:00 +00004993 case UPD_CXX_DEDUCED_RETURN_TYPE:
4994 Record.push_back(GetOrCreateTypeID(Update.getType()));
4995 break;
4996
4997 case UPD_DECL_MARKED_USED:
4998 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004999
5000 case UPD_MANGLING_NUMBER:
5001 case UPD_STATIC_LOCAL_NUMBER:
5002 Record.push_back(Update.getNumber());
5003 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005004
Alexey Bataev97720002014-11-11 04:05:39 +00005005 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005006 Record.AddSourceRange(
5007 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005008 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005009
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005010 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5011 Record.AddSourceRange(
5012 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5013 break;
5014
Richard Smith65ebb4a2015-03-26 04:09:53 +00005015 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005016 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005017 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005018
5019 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005020 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005021 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005022 }
5023 }
5024
Richard Smithd28ac5b2014-03-22 23:33:22 +00005025 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005026 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005027 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005028 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005029 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005030 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005031 }
5032
Richard Smithcd45dbc2014-04-19 03:48:30 +00005033 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005034 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005035 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005036}
5037
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005038void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005039 uint32_t Raw = Loc.getRawEncoding();
5040 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005041}
5042
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005043void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005044 AddSourceLocation(Range.getBegin(), Record);
5045 AddSourceLocation(Range.getEnd(), Record);
5046}
5047
Richard Smithf144b542016-04-08 21:54:32 +00005048void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5049 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005050 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005051 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005052}
5053
Richard Smithf144b542016-04-08 21:54:32 +00005054void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5055 Record->push_back(Value.isUnsigned());
5056 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005057}
5058
Richard Smithf144b542016-04-08 21:54:32 +00005059void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5060 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005061}
5062
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005063void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005064 Record.push_back(getIdentifierRef(II));
5065}
5066
Sebastian Redl539c5062010-08-18 23:57:32 +00005067IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005068 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005069 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005070
Sebastian Redl539c5062010-08-18 23:57:32 +00005071 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005072 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005073 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005074 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005075}
5076
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005077MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005078 // Don't emit builtin macros like __LINE__ to the AST file unless they
5079 // have been redefined by the header (in which case they are not
5080 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005081 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005082 return 0;
5083
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005084 MacroID &ID = MacroIDs[MI];
5085 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005086 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005087 MacroInfoToEmitData Info = { Name, MI, ID };
5088 MacroInfosToEmit.push_back(Info);
5089 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005090 return ID;
5091}
5092
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005093MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005094 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005095 return 0;
5096
5097 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5098 return MacroIDs[MI];
5099}
5100
5101uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005102 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005103}
5104
Richard Smithe64e7452016-04-18 21:54:58 +00005105void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5106 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005107}
5108
Sebastian Redl539c5062010-08-18 23:57:32 +00005109SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005110 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005111 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005112 }
5113
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005114 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005115 if (SID == 0 && Chain) {
5116 // This might trigger a ReadSelector callback, which will set the ID for
5117 // this selector.
5118 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005119 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005120 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005121 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005122 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005123 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005124 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005125 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005126}
5127
Richard Smithe64e7452016-04-18 21:54:58 +00005128void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5129 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005130}
5131
Richard Smith290d8012016-04-06 17:06:00 +00005132void ASTRecordWriter::AddTemplateArgumentLocInfo(
5133 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005134 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005135 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005136 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005137 break;
5138 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005139 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005140 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005141 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005142 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5143 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005144 break;
5145 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005146 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5147 AddSourceLocation(Arg.getTemplateNameLoc());
5148 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005149 break;
John McCall0ad16662009-10-29 08:12:44 +00005150 case TemplateArgument::Null:
5151 case TemplateArgument::Integral:
5152 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005153 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005154 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005155 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005156 break;
5157 }
5158}
5159
Richard Smith290d8012016-04-06 17:06:00 +00005160void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5161 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005162
5163 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5164 bool InfoHasSameExpr
5165 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005166 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005167 if (InfoHasSameExpr)
5168 return; // Avoid storing the same expr twice.
5169 }
Richard Smith290d8012016-04-06 17:06:00 +00005170 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005171}
5172
Richard Smith290d8012016-04-06 17:06:00 +00005173void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005174 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005175 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005176 return;
5177 }
5178
Richard Smith290d8012016-04-06 17:06:00 +00005179 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005180}
5181
Richard Smith290d8012016-04-06 17:06:00 +00005182void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5183 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005184
Richard Smith290d8012016-04-06 17:06:00 +00005185 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005186 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005187 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005188}
5189
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005190void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005191 Record.push_back(GetOrCreateTypeID(T));
5192}
5193
Richard Smith2095ffe2015-03-16 20:11:03 +00005194TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005195 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005196 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5197 if (T.isNull())
5198 return TypeIdx();
5199 assert(!T.getLocalFastQualifiers());
5200
5201 TypeIdx &Idx = TypeIdxs[T];
5202 if (Idx.getIndex() == 0) {
5203 if (DoneWritingDeclsAndTypes) {
5204 assert(0 && "New type seen after serializing all the types to emit!");
5205 return TypeIdx();
5206 }
5207
5208 // We haven't seen this type before. Assign it a new ID and put it
5209 // into the queue of types to emit.
5210 Idx = TypeIdx(NextTypeID++);
5211 DeclTypesToEmit.push(T);
5212 }
5213 return Idx;
5214 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005215}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005216
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005217TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005218 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005219 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5220 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005221 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005222 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005223
Richard Smith2095ffe2015-03-16 20:11:03 +00005224 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5225 assert(I != TypeIdxs.end() && "Type not emitted!");
5226 return I->second;
5227 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005228}
5229
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005230void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005231 Record.push_back(GetDeclRef(D));
5232}
5233
Sebastian Redl539c5062010-08-18 23:57:32 +00005234DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005235 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005236
5237 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005238 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005239 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005240
5241 // If D comes from an AST file, its declaration ID is already known and
5242 // fixed.
5243 if (D->isFromASTFile())
5244 return D->getGlobalID();
5245
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005246 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005247 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005248 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005249 if (DoneWritingDeclsAndTypes) {
5250 assert(0 && "New decl seen after serializing all the decls to emit!");
5251 return 0;
5252 }
5253
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005254 // We haven't seen this declaration before. Give it a new ID and
5255 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005256 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005257 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005258 }
5259
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005260 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005261}
5262
Sebastian Redl539c5062010-08-18 23:57:32 +00005263DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005264 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005265 return 0;
5266
Douglas Gregorb3163e52012-01-05 22:33:30 +00005267 // If D comes from an AST file, its declaration ID is already known and
5268 // fixed.
5269 if (D->isFromASTFile())
5270 return D->getGlobalID();
5271
Douglas Gregore84a9da2009-04-20 20:36:09 +00005272 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5273 return DeclIDs[D];
5274}
5275
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005276void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005277 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005278 assert(D);
5279
5280 SourceLocation Loc = D->getLocation();
5281 if (Loc.isInvalid())
5282 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005283
5284 // We only keep track of the file-level declarations of each file.
5285 if (!D->getLexicalDeclContext()->isFileContext())
5286 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005287 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5288 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005289 if (isa<ParmVarDecl>(D))
5290 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005291
5292 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005293 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005294 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005295 FileID FID;
5296 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005297 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005298 if (FID.isInvalid())
5299 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005300 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005301
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005302 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005303 if (!Info)
5304 Info = new DeclIDInFileInfo();
5305
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005306 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005307 LocDeclIDsTy &Decls = Info->DeclIDs;
5308
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005309 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005310 Decls.push_back(LocDecl);
5311 return;
5312 }
5313
Benjamin Kramer45025c02013-08-24 13:22:59 +00005314 LocDeclIDsTy::iterator I =
5315 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005316
5317 Decls.insert(I, LocDecl);
5318}
5319
Richard Smithe64e7452016-04-18 21:54:58 +00005320void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005321 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005322 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005323 switch (Name.getNameKind()) {
5324 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005325 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005326 break;
5327
5328 case DeclarationName::ObjCZeroArgSelector:
5329 case DeclarationName::ObjCOneArgSelector:
5330 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005331 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005332 break;
5333
5334 case DeclarationName::CXXConstructorName:
5335 case DeclarationName::CXXDestructorName:
5336 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005337 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005338 break;
5339
Richard Smith35845152017-02-07 01:37:30 +00005340 case DeclarationName::CXXDeductionGuideName:
5341 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5342 break;
5343
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005344 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005345 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005346 break;
5347
Alexis Hunt3d221f22009-11-29 07:34:05 +00005348 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005349 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005350 break;
5351
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005352 case DeclarationName::CXXUsingDirective:
5353 // No extra data to emit
5354 break;
5355 }
5356}
Chris Lattnerca025db2010-05-07 21:43:38 +00005357
Richard Smithd08aeb62014-08-28 01:33:39 +00005358unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5359 assert(needsAnonymousDeclarationNumber(D) &&
5360 "expected an anonymous declaration");
5361
5362 // Number the anonymous declarations within this context, if we've not
5363 // already done so.
5364 auto It = AnonymousDeclarationNumbers.find(D);
5365 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005366 auto *DC = D->getLexicalDeclContext();
5367 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5368 AnonymousDeclarationNumbers[ND] = Number;
5369 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005370
5371 It = AnonymousDeclarationNumbers.find(D);
5372 assert(It != AnonymousDeclarationNumbers.end() &&
5373 "declaration not found within its lexical context");
5374 }
5375
5376 return It->second;
5377}
5378
Richard Smith290d8012016-04-06 17:06:00 +00005379void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5380 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005381 switch (Name.getNameKind()) {
5382 case DeclarationName::CXXConstructorName:
5383 case DeclarationName::CXXDestructorName:
5384 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005385 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005386 break;
5387
5388 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005389 AddSourceLocation(SourceLocation::getFromRawEncoding(
5390 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005391 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005392 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005393 break;
5394
5395 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005396 AddSourceLocation(SourceLocation::getFromRawEncoding(
5397 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005398 break;
5399
5400 case DeclarationName::Identifier:
5401 case DeclarationName::ObjCZeroArgSelector:
5402 case DeclarationName::ObjCOneArgSelector:
5403 case DeclarationName::ObjCMultiArgSelector:
5404 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005405 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005406 break;
5407 }
5408}
5409
Richard Smith290d8012016-04-06 17:06:00 +00005410void ASTRecordWriter::AddDeclarationNameInfo(
5411 const DeclarationNameInfo &NameInfo) {
5412 AddDeclarationName(NameInfo.getName());
5413 AddSourceLocation(NameInfo.getLoc());
5414 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005415}
5416
Richard Smith290d8012016-04-06 17:06:00 +00005417void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5418 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5419 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005420 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005421 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005422}
5423
Richard Smithe64e7452016-04-18 21:54:58 +00005424void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005425 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005426 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005427 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005428
5429 // Push each of the NNS's onto a stack for serialization in reverse order.
5430 while (NNS) {
5431 NestedNames.push_back(NNS);
5432 NNS = NNS->getPrefix();
5433 }
5434
Richard Smithe64e7452016-04-18 21:54:58 +00005435 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005436 while(!NestedNames.empty()) {
5437 NNS = NestedNames.pop_back_val();
5438 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005439 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005440 switch (Kind) {
5441 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005442 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005443 break;
5444
5445 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005446 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005447 break;
5448
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005449 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005450 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005451 break;
5452
Chris Lattnerca025db2010-05-07 21:43:38 +00005453 case NestedNameSpecifier::TypeSpec:
5454 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005455 AddTypeRef(QualType(NNS->getAsType(), 0));
5456 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005457 break;
5458
5459 case NestedNameSpecifier::Global:
5460 // Don't need to write an associated value.
5461 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005462
5463 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005464 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005465 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005466 }
5467 }
5468}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005469
Richard Smith290d8012016-04-06 17:06:00 +00005470void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005471 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005472 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005473 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005474
5475 // Push each of the nested-name-specifiers's onto a stack for
5476 // serialization in reverse order.
5477 while (NNS) {
5478 NestedNames.push_back(NNS);
5479 NNS = NNS.getPrefix();
5480 }
5481
Richard Smith290d8012016-04-06 17:06:00 +00005482 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005483 while(!NestedNames.empty()) {
5484 NNS = NestedNames.pop_back_val();
5485 NestedNameSpecifier::SpecifierKind Kind
5486 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005487 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005488 switch (Kind) {
5489 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005490 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5491 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005492 break;
5493
5494 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005495 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5496 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005497 break;
5498
5499 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005500 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5501 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005502 break;
5503
5504 case NestedNameSpecifier::TypeSpec:
5505 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005506 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5507 AddTypeLoc(NNS.getTypeLoc());
5508 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005509 break;
5510
5511 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005512 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005513 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005514
5515 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005516 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5517 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005518 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005519 }
5520 }
5521}
5522
Richard Smith290d8012016-04-06 17:06:00 +00005523void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005524 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005525 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005526 switch (Kind) {
5527 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005528 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005529 break;
5530
5531 case TemplateName::OverloadedTemplate: {
5532 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005533 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005534 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005535 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005536 break;
5537 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005538
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005539 case TemplateName::QualifiedTemplate: {
5540 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005541 AddNestedNameSpecifier(QualT->getQualifier());
5542 Record->push_back(QualT->hasTemplateKeyword());
5543 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005544 break;
5545 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005546
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005547 case TemplateName::DependentTemplate: {
5548 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005549 AddNestedNameSpecifier(DepT->getQualifier());
5550 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005551 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005552 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005553 else
Richard Smith290d8012016-04-06 17:06:00 +00005554 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005555 break;
5556 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005557
5558 case TemplateName::SubstTemplateTemplateParm: {
5559 SubstTemplateTemplateParmStorage *subst
5560 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005561 AddDeclRef(subst->getParameter());
5562 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005563 break;
5564 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005565
5566 case TemplateName::SubstTemplateTemplateParmPack: {
5567 SubstTemplateTemplateParmPackStorage *SubstPack
5568 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005569 AddDeclRef(SubstPack->getParameterPack());
5570 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005571 break;
5572 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005573 }
5574}
5575
Richard Smith290d8012016-04-06 17:06:00 +00005576void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5577 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005578 switch (Arg.getKind()) {
5579 case TemplateArgument::Null:
5580 break;
5581 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005582 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005583 break;
5584 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005585 AddDeclRef(Arg.getAsDecl());
5586 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005587 break;
5588 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005589 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005590 break;
5591 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005592 AddAPSInt(Arg.getAsIntegral());
5593 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005594 break;
5595 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005596 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005597 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005598 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005599 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005600 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005601 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005602 else
Richard Smith290d8012016-04-06 17:06:00 +00005603 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005604 break;
5605 case TemplateArgument::Expression:
5606 AddStmt(Arg.getAsExpr());
5607 break;
5608 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005609 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005610 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005611 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005612 break;
5613 }
5614}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005615
Richard Smithe64e7452016-04-18 21:54:58 +00005616void ASTRecordWriter::AddTemplateParameterList(
5617 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005618 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005619 AddSourceLocation(TemplateParams->getTemplateLoc());
5620 AddSourceLocation(TemplateParams->getLAngleLoc());
5621 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005622 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005623 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005624 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005625 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005626}
5627
5628/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005629void ASTRecordWriter::AddTemplateArgumentList(
5630 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005631 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005632 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005633 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005634 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005635}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005636
Richard Smith290d8012016-04-06 17:06:00 +00005637void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5638 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005639 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005640 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5641 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5642 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005643 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005644 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005645 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005646}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005647
Richard Smithe64e7452016-04-18 21:54:58 +00005648void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5649 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005650 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005651 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005652 AddDeclRef(I.getDecl());
5653 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005654 }
5655}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005656
Richard Smith290d8012016-04-06 17:06:00 +00005657// FIXME: Move this out of the main ASTRecordWriter interface.
5658void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5659 Record->push_back(Base.isVirtual());
5660 Record->push_back(Base.isBaseOfClass());
5661 Record->push_back(Base.getAccessSpecifierAsWritten());
5662 Record->push_back(Base.getInheritConstructors());
5663 AddTypeSourceInfo(Base.getTypeSourceInfo());
5664 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005665 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005666 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005667}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005668
Richard Smith645d2cf2016-04-14 00:29:55 +00005669static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5670 ArrayRef<CXXBaseSpecifier> Bases) {
5671 ASTWriter::RecordData Record;
5672 ASTRecordWriter Writer(W, Record);
5673 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005674
Richard Smith645d2cf2016-04-14 00:29:55 +00005675 for (auto &Base : Bases)
5676 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005677
Richard Smith645d2cf2016-04-14 00:29:55 +00005678 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005679}
5680
Richard Smith290d8012016-04-06 17:06:00 +00005681// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005682void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5683 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5684}
5685
Richard Smithaa165cf2016-04-13 21:57:08 +00005686static uint64_t
5687EmitCXXCtorInitializers(ASTWriter &W,
5688 ArrayRef<CXXCtorInitializer *> CtorInits) {
5689 ASTWriter::RecordData Record;
5690 ASTRecordWriter Writer(W, Record);
5691 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005692
Richard Smithaa165cf2016-04-13 21:57:08 +00005693 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005694 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005695 Writer.push_back(CTOR_INITIALIZER_BASE);
5696 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5697 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005698 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005699 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5700 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005701 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005702 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5703 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005704 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005705 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5706 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005707 }
Francois Pichetd583da02010-12-04 09:14:42 +00005708
Richard Smithaa165cf2016-04-13 21:57:08 +00005709 Writer.AddSourceLocation(Init->getMemberLocation());
5710 Writer.AddStmt(Init->getInit());
5711 Writer.AddSourceLocation(Init->getLParenLoc());
5712 Writer.AddSourceLocation(Init->getRParenLoc());
5713 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005714 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005715 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005716 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005717
5718 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005719}
5720
Richard Smithaa165cf2016-04-13 21:57:08 +00005721// FIXME: Move this out of the main ASTRecordWriter interface.
5722void ASTRecordWriter::AddCXXCtorInitializers(
5723 ArrayRef<CXXCtorInitializer *> CtorInits) {
5724 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005725}
5726
Richard Smith290d8012016-04-06 17:06:00 +00005727void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005728 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005729 Record->push_back(Data.IsLambda);
5730 Record->push_back(Data.UserDeclaredConstructor);
5731 Record->push_back(Data.UserDeclaredSpecialMembers);
5732 Record->push_back(Data.Aggregate);
5733 Record->push_back(Data.PlainOldData);
5734 Record->push_back(Data.Empty);
5735 Record->push_back(Data.Polymorphic);
5736 Record->push_back(Data.Abstract);
5737 Record->push_back(Data.IsStandardLayout);
5738 Record->push_back(Data.HasNoNonEmptyBases);
5739 Record->push_back(Data.HasPrivateFields);
5740 Record->push_back(Data.HasProtectedFields);
5741 Record->push_back(Data.HasPublicFields);
5742 Record->push_back(Data.HasMutableFields);
5743 Record->push_back(Data.HasVariantMembers);
5744 Record->push_back(Data.HasOnlyCMembers);
5745 Record->push_back(Data.HasInClassInitializer);
5746 Record->push_back(Data.HasUninitializedReferenceMember);
5747 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005748 Record->push_back(Data.HasInheritedConstructor);
5749 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005750 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5751 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5752 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5753 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5754 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5755 Record->push_back(Data.DefaultedDestructorIsDeleted);
5756 Record->push_back(Data.HasTrivialSpecialMembers);
5757 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5758 Record->push_back(Data.HasIrrelevantDestructor);
5759 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5760 Record->push_back(Data.HasDefaultedDefaultConstructor);
5761 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5762 Record->push_back(Data.HasConstexprDefaultConstructor);
5763 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5764 Record->push_back(Data.ComputedVisibleConversions);
5765 Record->push_back(Data.UserProvidedDefaultConstructor);
5766 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005767 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5768 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005769 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5770 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5771 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieub6adf542017-02-18 02:09:28 +00005772 Record->push_back(Data.ODRHash);
Richard Smith561fb152012-02-25 07:33:38 +00005773 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005774
Richard Smith290d8012016-04-06 17:06:00 +00005775 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005776 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005777 AddCXXBaseSpecifiers(Data.bases());
5778
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005779 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005780 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005781 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005782 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005783
Richard Smith290d8012016-04-06 17:06:00 +00005784 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5785 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005786 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005787 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005788
5789 // Add lambda-specific data.
5790 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005791 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005792 Record->push_back(Lambda.Dependent);
5793 Record->push_back(Lambda.IsGenericLambda);
5794 Record->push_back(Lambda.CaptureDefault);
5795 Record->push_back(Lambda.NumCaptures);
5796 Record->push_back(Lambda.NumExplicitCaptures);
5797 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005798 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005799 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005800 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005801 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005802 AddSourceLocation(Capture.getLocation());
5803 Record->push_back(Capture.isImplicit());
5804 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005805 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005806 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005807 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005808 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005809 break;
5810 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005811 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005812 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005813 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005814 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005815 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005816 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005817 break;
5818 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005819 }
5820 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005821}
5822
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005823void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005824 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005825 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005826 assert(FirstDeclID == NextDeclID &&
5827 FirstTypeID == NextTypeID &&
5828 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005829 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005830 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005831 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005832 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005833
Sebastian Redl07a89a82010-07-30 00:29:29 +00005834 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005835
Richard Smith7f330cd2015-03-18 01:42:29 +00005836 // Note, this will get called multiple times, once one the reader starts up
5837 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005838 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5839 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5840 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005841 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005842 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005843 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005844 NextDeclID = FirstDeclID;
5845 NextTypeID = FirstTypeID;
5846 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005847 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005848 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005849 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005850}
5851
Sebastian Redl539c5062010-08-18 23:57:32 +00005852void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005853 // Always keep the highest ID. See \p TypeRead() for more information.
5854 IdentID &StoredID = IdentifierIDs[II];
5855 if (ID > StoredID)
5856 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005857}
5858
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005859void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005860 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005861 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005862 if (ID > StoredID)
5863 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005864}
5865
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005866void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005867 // Always take the highest-numbered type index. This copes with an interesting
5868 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005869 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005870 // keep the higher-numbered entry so that we can properly write it out to
5871 // the AST file.
5872 TypeIdx &StoredIdx = TypeIdxs[T];
5873 if (Idx.getIndex() >= StoredIdx.getIndex())
5874 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005875}
5876
Sebastian Redl539c5062010-08-18 23:57:32 +00005877void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005878 // Always keep the highest ID. See \p TypeRead() for more information.
5879 SelectorID &StoredID = SelectorIDs[S];
5880 if (ID > StoredID)
5881 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005882}
Douglas Gregor91096292010-10-02 19:29:26 +00005883
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005884void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005885 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005886 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005887 MacroDefinitions[MD] = ID;
5888}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005889
Douglas Gregore37a85a2011-12-02 17:30:13 +00005890void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5891 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5892 SubmoduleIDs[Mod] = ID;
5893}
5894
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005895void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005896 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005897 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005898 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005899 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005900 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005901 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005902 // A forward reference was mutated into a definition. Rewrite it.
5903 // FIXME: This happens during template instantiation, should we
5904 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005905 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5906 "completed a tag from another module but not by instantiation?");
5907 DeclUpdates[RD].push_back(
5908 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005909 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005910 }
5911}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005912
Richard Smithf983f7f2015-10-13 00:23:25 +00005913static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5914 if (D->isFromASTFile())
5915 return true;
5916
Richard Smithf983f7f2015-10-13 00:23:25 +00005917 // The predefined __va_list_tag struct is imported if we imported any decls.
5918 // FIXME: This is a gross hack.
5919 return D == D->getASTContext().getVaListTagDecl();
5920}
5921
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005922void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005923 if (Chain && Chain->isProcessingUpdateRecords()) return;
5924 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005925 "Should not add lookup results to non-lookup contexts!");
5926
Vassil Vassilev632eac32016-03-16 11:17:04 +00005927 // TU is handled elsewhere.
5928 if (isa<TranslationUnitDecl>(DC))
5929 return;
5930
5931 // Namespaces are handled elsewhere, except for template instantiations of
5932 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5933 // local instantiations are added to an imported context. Only happens when
5934 // adding ADL lookup candidates, for example templated friends.
5935 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5936 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005937 return;
5938
Richard Smithf983f7f2015-10-13 00:23:25 +00005939 // We're only interested in cases where a local declaration is added to an
5940 // imported context.
5941 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5942 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005943
Richard Smith0f4e2c42015-08-06 04:23:48 +00005944 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005945 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005946 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005947 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5948 // We're adding a visible declaration to a predefined decl context. Ensure
5949 // that we write out all of its lookup results so we don't get a nasty
5950 // surprise when we try to emit its lookup table.
5951 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005952 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005953 }
Richard Smithc26d9742016-10-06 20:30:51 +00005954 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005955}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005956
5957void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005958 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005959 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005960
5961 // We're only interested in cases where a local declaration is added to an
5962 // imported context.
5963 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5964 return;
5965
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005966 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005967 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005968
5969 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005970 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005971 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005972 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005973}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005974
Richard Smith564417a2014-03-20 21:47:22 +00005975void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005976 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005977 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5978 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005979 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005980 // If we don't already know the exception specification for this redecl
5981 // chain, add an update record for it.
5982 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5983 ->getType()
5984 ->castAs<FunctionProtoType>()
5985 ->getExceptionSpecType()))
5986 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5987 });
Richard Smith564417a2014-03-20 21:47:22 +00005988}
5989
Richard Smith1fa5d642013-05-11 05:45:24 +00005990void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005991 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005992 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005993 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005994 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005995 DeclUpdates[D].push_back(
5996 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5997 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005998}
5999
Richard Smithf8134002015-03-10 01:41:22 +00006000void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6001 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006002 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006003 assert(!WritingAST && "Already writing the AST!");
6004 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006005 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006006 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006007 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6008 });
Richard Smithf8134002015-03-10 01:41:22 +00006009}
6010
Sebastian Redlab238a72011-04-24 16:28:06 +00006011void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006012 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006013 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006014 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006015 return; // Declaration not imported from PCH.
6016
Richard Smith4d235792014-08-07 18:53:08 +00006017 // Implicit function decl from a PCH was defined.
6018 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006019}
6020
Richard Smithd28ac5b2014-03-22 23:33:22 +00006021void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006022 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006023 assert(!WritingAST && "Already writing the AST!");
6024 if (!D->isFromASTFile())
6025 return;
6026
Richard Smith9e2341d2015-03-23 03:25:59 +00006027 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006028}
6029
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006030void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006031 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006032 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006033 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006034 return;
6035
6036 // Since the actual instantiation is delayed, this really means that we need
6037 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00006038 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00006039 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
6040 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006041}
6042
John McCall32791cc2016-01-06 22:34:54 +00006043void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006044 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006045 assert(!WritingAST && "Already writing the AST!");
6046 if (!D->isFromASTFile())
6047 return;
6048
6049 DeclUpdates[D].push_back(
6050 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6051}
6052
Richard Smith4b054b22016-08-24 21:25:37 +00006053void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6054 assert(!WritingAST && "Already writing the AST!");
6055 if (!D->isFromASTFile())
6056 return;
6057
6058 DeclUpdates[D].push_back(
6059 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6060}
6061
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006062void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6063 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006064 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006065 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006066 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006067 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006068
6069 assert(IFD->getDefinition() && "Category on a class without a definition?");
6070 ObjCClassesWithCategories.insert(
6071 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006072}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006073
Eli Friedman276dd182013-09-05 00:02:25 +00006074void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006075 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006076 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006077
6078 // If there is *any* declaration of the entity that's not from an AST file,
6079 // we can skip writing the update record. We make sure that isUsed() triggers
6080 // completion of the redeclaration chain of the entity.
6081 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6082 if (IsLocalDecl(Prev))
6083 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006084
Aaron Ballman4f45b712014-03-21 15:22:56 +00006085 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006086}
Alexey Bataev97720002014-11-11 04:05:39 +00006087
6088void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006089 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006090 assert(!WritingAST && "Already writing the AST!");
6091 if (!D->isFromASTFile())
6092 return;
6093
6094 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6095}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006096
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006097void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6098 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006099 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006100 assert(!WritingAST && "Already writing the AST!");
6101 if (!D->isFromASTFile())
6102 return;
6103
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006104 DeclUpdates[D].push_back(
6105 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006106}
6107
Richard Smith4caa4492015-05-15 02:34:32 +00006108void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006109 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006110 assert(!WritingAST && "Already writing the AST!");
6111 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006112 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006113}
Alex Denisovfde64952015-06-26 05:28:36 +00006114
6115void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6116 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006117 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006118 assert(!WritingAST && "Already writing the AST!");
6119 if (!Record->isFromASTFile())
6120 return;
6121 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6122}
Richard Smithc26d9742016-10-06 20:30:51 +00006123
6124void ASTWriter::AddedCXXTemplateSpecialization(
6125 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6126 assert(!WritingAST && "Already writing the AST!");
6127
6128 if (!TD->getFirstDecl()->isFromASTFile())
6129 return;
6130 if (Chain && Chain->isProcessingUpdateRecords())
6131 return;
6132
6133 DeclsToEmitEvenIfUnreferenced.push_back(D);
6134}
6135
6136void ASTWriter::AddedCXXTemplateSpecialization(
6137 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6138 assert(!WritingAST && "Already writing the AST!");
6139
6140 if (!TD->getFirstDecl()->isFromASTFile())
6141 return;
6142 if (Chain && Chain->isProcessingUpdateRecords())
6143 return;
6144
6145 DeclsToEmitEvenIfUnreferenced.push_back(D);
6146}
6147
6148void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6149 const FunctionDecl *D) {
6150 assert(!WritingAST && "Already writing the AST!");
6151
6152 if (!TD->getFirstDecl()->isFromASTFile())
6153 return;
6154 if (Chain && Chain->isProcessingUpdateRecords())
6155 return;
6156
6157 DeclsToEmitEvenIfUnreferenced.push_back(D);
6158}