blob: b2237a5fc1969da2eb3af2a0323d9a2de114b4ed [file] [log] [blame]
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001//===--- ASTWriter.cpp - AST File Writer ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000016#include "ASTReaderInternals.h"
17#include "MultiOnDiskHashTable.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000019#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000022#include "clang/AST/DeclCXX.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000027#include "clang/AST/LambdaCapture.h"
28#include "clang/AST/NestedNameSpecifier.h"
29#include "clang/AST/RawCommentList.h"
30#include "clang/AST/TemplateName.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000031#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000032#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000033#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000035#include "clang/Basic/FileSystemOptions.h"
36#include "clang/Basic/LangOptions.h"
37#include "clang/Basic/LLVM.h"
38#include "clang/Basic/Module.h"
39#include "clang/Basic/ObjCRuntime.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000040#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000041#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000042#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000043#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000044#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000045#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000046#include "clang/Lex/HeaderSearch.h"
47#include "clang/Lex/HeaderSearchOptions.h"
48#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000049#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000050#include "clang/Lex/PreprocessingRecord.h"
51#include "clang/Lex/Preprocessor.h"
52#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000053#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000054#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000055#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000056#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000057#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000058#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000059#include "clang/Serialization/Module.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000060#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000061#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000062#include "llvm/ADT/APFloat.h"
63#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000064#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000065#include "llvm/ADT/IntrusiveRefCntPtr.h"
66#include "llvm/ADT/Optional.h"
67#include "llvm/ADT/SmallSet.h"
68#include "llvm/ADT/SmallString.h"
69#include "llvm/ADT/STLExtras.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000070#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000071#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000073#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000074#include "llvm/Support/Compression.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000075#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000076#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000077#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000078#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000079#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000080#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000081#include "llvm/Support/Process.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000082#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000083#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000084#include <cassert>
85#include <cstdint>
86#include <cstdlib>
87#include <cstring>
88#include <deque>
89#include <limits>
90#include <new>
91#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +000092#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000093
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000095using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096
Sebastian Redl3df5a082010-07-30 17:03:48 +000097template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000098static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000099 if (v.empty()) return StringRef();
100 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000101 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000102}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000103
104template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000105static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000106 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000107 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000108}
109
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110//===----------------------------------------------------------------------===//
111// Type serialization
112//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000113
Richard Smith290d8012016-04-06 17:06:00 +0000114namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000115
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000116 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000117 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000118 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +0000121 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +0000122 /// \brief Abbreviation to use for the record, if any.
123 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000124
Richard Smith290d8012016-04-06 17:06:00 +0000125 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000126 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Richard Smith290d8012016-04-06 17:06:00 +0000127 : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
128
129 uint64_t Emit() {
130 return Record.Emit(Code, AbbrevToUse);
131 }
132
133 void Visit(QualType T) {
134 if (T.hasLocalNonFastQualifiers()) {
135 Qualifiers Qs = T.getLocalQualifiers();
136 Record.AddTypeRef(T.getLocalUnqualifiedType());
137 Record.push_back(Qs.getAsOpaqueValue());
138 Code = TYPE_EXT_QUAL;
139 AbbrevToUse = Writer.TypeExtQualAbbrev;
140 } else {
141 switch (T->getTypeClass()) {
142 // For all of the concrete, non-dependent types, call the
143 // appropriate visitor function.
144#define TYPE(Class, Base) \
145 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
146#define ABSTRACT_TYPE(Class, Base)
147#include "clang/AST/TypeNodes.def"
148 }
149 }
150 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151
152 void VisitArrayType(const ArrayType *T);
153 void VisitFunctionType(const FunctionType *T);
154 void VisitTagType(const TagType *T);
155
156#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
157#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158#include "clang/AST/TypeNodes.def"
159 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000160
Richard Smith290d8012016-04-06 17:06:00 +0000161} // end namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000164 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000168 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000173 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000174 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175}
176
Reid Kleckner8a365022013-06-24 17:51:48 +0000177void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000178 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000179 Code = TYPE_DECAYED;
180}
181
Reid Kleckner0503a872013-12-05 01:23:43 +0000182void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000183 Record.AddTypeRef(T->getOriginalType());
184 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000185 Code = TYPE_ADJUSTED;
186}
187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000188void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000189 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000190 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000191}
192
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000193void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000194 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000195 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000200 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202}
203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000205 Record.AddTypeRef(T->getPointeeType());
206 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000207 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000211 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000213 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000218 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000229 Record.AddSourceLocation(T->getLBracketLoc());
230 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000231 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000232 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000233}
234
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000235void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000236 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000238 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000239 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240}
241
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000242void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000244 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245}
246
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000248 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000249 FunctionType::ExtInfo C = T->getExtInfo();
250 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000251 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000252 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000253 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000254 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000255 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000256
257 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
258 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000259}
260
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000261void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000263 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000264}
265
Richard Smith290d8012016-04-06 17:06:00 +0000266static void addExceptionSpec(const FunctionProtoType *T,
267 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000268 Record.push_back(T->getExceptionSpecType());
269 if (T->getExceptionSpecType() == EST_Dynamic) {
270 Record.push_back(T->getNumExceptions());
271 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000272 Record.AddTypeRef(T->getExceptionType(I));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000273 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
Richard Smith290d8012016-04-06 17:06:00 +0000274 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000275 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000276 Record.AddDeclRef(T->getExceptionSpecDecl());
277 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000278 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000279 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000280 }
Richard Smith564417a2014-03-20 21:47:22 +0000281}
282
283void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
284 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000285
Richard Smith564417a2014-03-20 21:47:22 +0000286 Record.push_back(T->isVariadic());
287 Record.push_back(T->hasTrailingReturn());
288 Record.push_back(T->getTypeQuals());
289 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000290 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000291
292 Record.push_back(T->getNumParams());
293 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000294 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000295
John McCall18afab72016-03-01 00:49:02 +0000296 if (T->hasExtParameterInfos()) {
297 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
298 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
299 }
300
Richard Smith01b2cb42014-07-26 06:37:51 +0000301 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000302 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
303 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000304 AbbrevToUse = 0;
305
Sebastian Redl539c5062010-08-18 23:57:32 +0000306 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000307}
308
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000309void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000310 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000311 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000312}
John McCallb96ec562009-12-04 22:46:56 +0000313
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000314void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000315 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000316 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000317 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000319}
320
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000321void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000322 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000323 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000324}
325
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000326void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000327 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000328 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000329}
330
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000331void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000332 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000333 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000334 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000335}
336
Alexis Hunte852b102011-05-24 22:41:36 +0000337void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000338 Record.AddTypeRef(T->getBaseType());
339 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000340 Record.push_back(T->getUTTKind());
341 Code = TYPE_UNARY_TRANSFORM;
342}
343
Richard Smith30482bc2011-02-20 03:19:35 +0000344void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000345 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000346 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000347 if (T->getDeducedType().isNull())
348 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000349 Code = TYPE_AUTO;
350}
351
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000352void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000353 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000354 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000355 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000356 "Cannot serialize in the middle of a type definition");
357}
358
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000359void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000360 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000361 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000362}
363
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000364void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000365 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000367}
368
John McCall81904512011-01-06 01:58:22 +0000369void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000370 Record.AddTypeRef(T->getModifiedType());
371 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000372 Record.push_back(T->getAttrKind());
373 Code = TYPE_ATTRIBUTED;
374}
375
Mike Stump11289f42009-09-09 15:08:12 +0000376void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000377ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000378 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000379 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
380 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000381 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000382}
383
384void
Douglas Gregorada4b792011-01-14 02:55:32 +0000385ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
386 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000387 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
388 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000389 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
390}
391
392void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000393ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000394 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000395 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000396 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000397 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000398 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000399 Record.AddTemplateArgument(ArgI);
400 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
401 : T->isCanonicalUnqualified()
402 ? QualType()
403 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000404 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000405}
406
407void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000408ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000409 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000410 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000411 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000412 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000413}
414
415void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000416ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000417 const DependentSizedExtVectorType *T) {
418 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000419 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000420}
421
422void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000423ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000424 Record.push_back(T->getDepth());
425 Record.push_back(T->getIndex());
426 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000427 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000428 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000429}
430
431void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000432ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000433 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000434 Record.AddNestedNameSpecifier(T->getQualifier());
435 Record.AddIdentifierRef(T->getIdentifier());
436 Record.AddTypeRef(
437 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000438 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000439}
440
441void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000442ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000443 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000444 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000445 Record.AddNestedNameSpecifier(T->getQualifier());
446 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000447 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000448 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000449 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000450 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000451}
452
Douglas Gregord2fa7662010-12-20 02:24:11 +0000453void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000454 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000455 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000456 Record.push_back(*NumExpansions + 1);
457 else
458 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000459 Code = TYPE_PACK_EXPANSION;
460}
461
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000462void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000463 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000464 Code = TYPE_PAREN;
465}
466
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000467void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000468 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000469 Record.AddNestedNameSpecifier(T->getQualifier());
470 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000471 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000472}
473
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000474void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000475 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
476 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000477 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000478}
479
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000480void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000481 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000482 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000483}
484
Manman Rene6be26c2016-09-13 17:25:08 +0000485void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
486 Record.AddDeclRef(T->getDecl());
487 Record.push_back(T->getNumProtocols());
488 for (const auto *I : T->quals())
489 Record.AddDeclRef(I);
490 Code = TYPE_OBJC_TYPE_PARAM;
491}
492
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000493void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000494 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000495 Record.push_back(T->getTypeArgsAsWritten().size());
496 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000497 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000498 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000499 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000500 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000501 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000502 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000503}
504
Steve Narofffb4330f2009-06-17 22:40:22 +0000505void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000506ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000507 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000508 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000509}
510
Eli Friedman0dfb8892011-10-06 23:00:33 +0000511void
512ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000513 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000514 Code = TYPE_ATOMIC;
515}
516
Xiuli Pan9c14e282016-01-09 12:53:17 +0000517void
518ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000519 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000520 Record.push_back(T->isReadOnly());
521 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000522}
523
John McCall8f115c62009-10-16 21:56:05 +0000524namespace {
525
526class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000527 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000528
529public:
Richard Smith290d8012016-04-06 17:06:00 +0000530 TypeLocWriter(ASTRecordWriter &Record)
531 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000532
John McCall17001972009-10-18 01:05:36 +0000533#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000534#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000535 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000536#include "clang/AST/TypeLocNodes.def"
537
John McCall17001972009-10-18 01:05:36 +0000538 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
539 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000540};
541
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000542} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000543
John McCall17001972009-10-18 01:05:36 +0000544void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
545 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000546}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000547
John McCall17001972009-10-18 01:05:36 +0000548void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000549 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000550 if (TL.needsExtraLocalData()) {
551 Record.push_back(TL.getWrittenTypeSpec());
552 Record.push_back(TL.getWrittenSignSpec());
553 Record.push_back(TL.getWrittenWidthSpec());
554 Record.push_back(TL.hasModeAttr());
555 }
John McCall8f115c62009-10-16 21:56:05 +0000556}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000557
John McCall17001972009-10-18 01:05:36 +0000558void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000559 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000560}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000561
John McCall17001972009-10-18 01:05:36 +0000562void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000563 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000564}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000565
Reid Kleckner8a365022013-06-24 17:51:48 +0000566void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
567 // nothing to do
568}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000569
Reid Kleckner0503a872013-12-05 01:23:43 +0000570void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
571 // nothing to do
572}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000573
John McCall17001972009-10-18 01:05:36 +0000574void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000575 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000576}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000577
John McCall17001972009-10-18 01:05:36 +0000578void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000579 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000580}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000581
John McCall17001972009-10-18 01:05:36 +0000582void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000583 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000584}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000585
John McCall17001972009-10-18 01:05:36 +0000586void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000587 Record.AddSourceLocation(TL.getStarLoc());
588 Record.AddTypeSourceInfo(TL.getClassTInfo());
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::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000592 Record.AddSourceLocation(TL.getLBracketLoc());
593 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000594 Record.push_back(TL.getSizeExpr() ? 1 : 0);
595 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000596 Record.AddStmt(TL.getSizeExpr());
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::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
600 VisitArrayTypeLoc(TL);
601}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000602
John McCall17001972009-10-18 01:05:36 +0000603void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
604 VisitArrayTypeLoc(TL);
605}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000606
John McCall17001972009-10-18 01:05:36 +0000607void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
608 VisitArrayTypeLoc(TL);
609}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000610
John McCall17001972009-10-18 01:05:36 +0000611void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
612 DependentSizedArrayTypeLoc TL) {
613 VisitArrayTypeLoc(TL);
614}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000615
John McCall17001972009-10-18 01:05:36 +0000616void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
617 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000618 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000619}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000620
John McCall17001972009-10-18 01:05:36 +0000621void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000622 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000623}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000624
John McCall17001972009-10-18 01:05:36 +0000625void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000626 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000627}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000628
John McCall17001972009-10-18 01:05:36 +0000629void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000630 Record.AddSourceLocation(TL.getLocalRangeBegin());
631 Record.AddSourceLocation(TL.getLParenLoc());
632 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000633 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000634 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000635 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000636 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000637}
638void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
639 VisitFunctionTypeLoc(TL);
640}
641void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
642 VisitFunctionTypeLoc(TL);
643}
John McCallb96ec562009-12-04 22:46:56 +0000644void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000645 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000646}
John McCall17001972009-10-18 01:05:36 +0000647void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000648 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000649}
Manman Rene6be26c2016-09-13 17:25:08 +0000650void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
651 if (TL.getNumProtocols()) {
652 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
653 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
654 }
655 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
656 Record.AddSourceLocation(TL.getProtocolLoc(i));
657}
John McCall17001972009-10-18 01:05:36 +0000658void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000659 Record.AddSourceLocation(TL.getTypeofLoc());
660 Record.AddSourceLocation(TL.getLParenLoc());
661 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000662}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000663
John McCall17001972009-10-18 01:05:36 +0000664void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000665 Record.AddSourceLocation(TL.getTypeofLoc());
666 Record.AddSourceLocation(TL.getLParenLoc());
667 Record.AddSourceLocation(TL.getRParenLoc());
668 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000669}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000670
John McCall17001972009-10-18 01:05:36 +0000671void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000672 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000673}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000674
Alexis Hunte852b102011-05-24 22:41:36 +0000675void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000676 Record.AddSourceLocation(TL.getKWLoc());
677 Record.AddSourceLocation(TL.getLParenLoc());
678 Record.AddSourceLocation(TL.getRParenLoc());
679 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000680}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000681
Richard Smith30482bc2011-02-20 03:19:35 +0000682void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000683 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000684}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000685
John McCall17001972009-10-18 01:05:36 +0000686void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000687 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000688}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000689
John McCall17001972009-10-18 01:05:36 +0000690void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000691 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000692}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000693
John McCall81904512011-01-06 01:58:22 +0000694void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000695 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000696 if (TL.hasAttrOperand()) {
697 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000698 Record.AddSourceLocation(range.getBegin());
699 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000700 }
701 if (TL.hasAttrExprOperand()) {
702 Expr *operand = TL.getAttrExprOperand();
703 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000704 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000705 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000706 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000707 }
708}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000709
John McCall17001972009-10-18 01:05:36 +0000710void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000711 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000712}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000713
John McCallcebee162009-10-18 09:09:24 +0000714void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
715 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000716 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000717}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000718
Douglas Gregorada4b792011-01-14 02:55:32 +0000719void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
720 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000721 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000722}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000723
John McCall17001972009-10-18 01:05:36 +0000724void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
725 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000726 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
727 Record.AddSourceLocation(TL.getTemplateNameLoc());
728 Record.AddSourceLocation(TL.getLAngleLoc());
729 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000730 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000731 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
732 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000733}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000734
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000735void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000736 Record.AddSourceLocation(TL.getLParenLoc());
737 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000738}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000739
Abramo Bagnara6150c882010-05-11 21:36:43 +0000740void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000741 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
742 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000743}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000744
John McCalle78aac42010-03-10 03:28:59 +0000745void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000746 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000747}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000748
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000749void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000750 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
751 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
752 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000753}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000754
John McCallc392f372010-06-11 00:33:02 +0000755void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
756 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000757 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
758 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
759 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
760 Record.AddSourceLocation(TL.getTemplateNameLoc());
761 Record.AddSourceLocation(TL.getLAngleLoc());
762 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000763 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000764 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
765 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000766}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000767
Douglas Gregord2fa7662010-12-20 02:24:11 +0000768void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000769 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000770}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000771
John McCall17001972009-10-18 01:05:36 +0000772void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000773 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000774}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000775
John McCall8b07ec22010-05-15 11:32:37 +0000776void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
777 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000778 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
779 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000780 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000781 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
782 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
783 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000784 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000785 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000786}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000787
John McCallfc93cf92009-10-22 22:37:11 +0000788void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000789 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000790}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000791
Eli Friedman0dfb8892011-10-06 23:00:33 +0000792void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000793 Record.AddSourceLocation(TL.getKWLoc());
794 Record.AddSourceLocation(TL.getLParenLoc());
795 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000796}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000797
Xiuli Pan9c14e282016-01-09 12:53:17 +0000798void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000799 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000800}
John McCall8f115c62009-10-16 21:56:05 +0000801
Richard Smith01b2cb42014-07-26 06:37:51 +0000802void ASTWriter::WriteTypeAbbrevs() {
803 using namespace llvm;
804
David Blaikieb44f0bf2017-01-04 22:36:43 +0000805 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000806
807 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000808 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000809 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
810 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
811 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000812 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000813
814 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000815 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000816 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
817 // FunctionType
818 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
819 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
820 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
821 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
822 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
823 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
824 // FunctionProtoType
825 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
826 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
827 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
828 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
829 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
830 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
831 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
832 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000833 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000834}
835
Chris Lattner19cea4e2009-04-22 05:57:30 +0000836//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000837// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000838//===----------------------------------------------------------------------===//
839
Chris Lattner28fa4e62009-04-26 22:26:21 +0000840static void EmitBlockID(unsigned ID, const char *Name,
841 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000842 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000843 Record.clear();
844 Record.push_back(ID);
845 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
846
847 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000848 if (!Name || Name[0] == 0)
849 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000850 Record.clear();
851 while (*Name)
852 Record.push_back(*Name++);
853 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
854}
855
856static void EmitRecordID(unsigned ID, const char *Name,
857 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000858 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000859 Record.clear();
860 Record.push_back(ID);
861 while (*Name)
862 Record.push_back(*Name++);
863 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000864}
865
866static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000867 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000868#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000869 RECORD(STMT_STOP);
870 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000871 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000872 RECORD(STMT_NULL);
873 RECORD(STMT_COMPOUND);
874 RECORD(STMT_CASE);
875 RECORD(STMT_DEFAULT);
876 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000877 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000878 RECORD(STMT_IF);
879 RECORD(STMT_SWITCH);
880 RECORD(STMT_WHILE);
881 RECORD(STMT_DO);
882 RECORD(STMT_FOR);
883 RECORD(STMT_GOTO);
884 RECORD(STMT_INDIRECT_GOTO);
885 RECORD(STMT_CONTINUE);
886 RECORD(STMT_BREAK);
887 RECORD(STMT_RETURN);
888 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000889 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000890 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000891 RECORD(EXPR_PREDEFINED);
892 RECORD(EXPR_DECL_REF);
893 RECORD(EXPR_INTEGER_LITERAL);
894 RECORD(EXPR_FLOATING_LITERAL);
895 RECORD(EXPR_IMAGINARY_LITERAL);
896 RECORD(EXPR_STRING_LITERAL);
897 RECORD(EXPR_CHARACTER_LITERAL);
898 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000899 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000900 RECORD(EXPR_UNARY_OPERATOR);
901 RECORD(EXPR_SIZEOF_ALIGN_OF);
902 RECORD(EXPR_ARRAY_SUBSCRIPT);
903 RECORD(EXPR_CALL);
904 RECORD(EXPR_MEMBER);
905 RECORD(EXPR_BINARY_OPERATOR);
906 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
907 RECORD(EXPR_CONDITIONAL_OPERATOR);
908 RECORD(EXPR_IMPLICIT_CAST);
909 RECORD(EXPR_CSTYLE_CAST);
910 RECORD(EXPR_COMPOUND_LITERAL);
911 RECORD(EXPR_EXT_VECTOR_ELEMENT);
912 RECORD(EXPR_INIT_LIST);
913 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000914 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000915 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000916 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000917 RECORD(EXPR_VA_ARG);
918 RECORD(EXPR_ADDR_LABEL);
919 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000920 RECORD(EXPR_CHOOSE);
921 RECORD(EXPR_GNU_NULL);
922 RECORD(EXPR_SHUFFLE_VECTOR);
923 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000924 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000925 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000926 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000927 RECORD(EXPR_OBJC_ARRAY_LITERAL);
928 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000929 RECORD(EXPR_OBJC_ENCODE);
930 RECORD(EXPR_OBJC_SELECTOR_EXPR);
931 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
932 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
933 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
934 RECORD(EXPR_OBJC_KVC_REF_EXPR);
935 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000936 RECORD(STMT_OBJC_FOR_COLLECTION);
937 RECORD(STMT_OBJC_CATCH);
938 RECORD(STMT_OBJC_FINALLY);
939 RECORD(STMT_OBJC_AT_TRY);
940 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
941 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000942 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000943 RECORD(STMT_CXX_CATCH);
944 RECORD(STMT_CXX_TRY);
945 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000946 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000947 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000948 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000949 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000950 RECORD(EXPR_CXX_STATIC_CAST);
951 RECORD(EXPR_CXX_DYNAMIC_CAST);
952 RECORD(EXPR_CXX_REINTERPRET_CAST);
953 RECORD(EXPR_CXX_CONST_CAST);
954 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000955 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000956 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000957 RECORD(EXPR_CXX_BOOL_LITERAL);
958 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000959 RECORD(EXPR_CXX_TYPEID_EXPR);
960 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000961 RECORD(EXPR_CXX_THIS);
962 RECORD(EXPR_CXX_THROW);
963 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000964 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000965 RECORD(EXPR_CXX_BIND_TEMPORARY);
966 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
967 RECORD(EXPR_CXX_NEW);
968 RECORD(EXPR_CXX_DELETE);
969 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
970 RECORD(EXPR_EXPR_WITH_CLEANUPS);
971 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
972 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
973 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
974 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
975 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000976 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000977 RECORD(EXPR_CXX_NOEXCEPT);
978 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000979 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
980 RECORD(EXPR_TYPE_TRAIT);
981 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000982 RECORD(EXPR_PACK_EXPANSION);
983 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000984 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000985 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000986 RECORD(EXPR_FUNCTION_PARM_PACK);
987 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000988 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000989 RECORD(EXPR_CXX_UUIDOF_EXPR);
990 RECORD(EXPR_CXX_UUIDOF_TYPE);
991 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000992#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000993}
Mike Stump11289f42009-09-09 15:08:12 +0000994
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000995void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000996 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +0000997 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +0000998
Sebastian Redl539c5062010-08-18 23:57:32 +0000999#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1000#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001001
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001002 // Control Block.
1003 BLOCK(CONTROL_BLOCK);
1004 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +00001005 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001006 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001007 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001008 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001009 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001010 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001011 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001012 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001013 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001014
1015 BLOCK(OPTIONS_BLOCK);
1016 RECORD(LANGUAGE_OPTIONS);
1017 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001018 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001019 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001020 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001021 RECORD(PREPROCESSOR_OPTIONS);
1022
Douglas Gregor108cb222012-10-19 00:45:00 +00001023 BLOCK(INPUT_FILES_BLOCK);
1024 RECORD(INPUT_FILE);
1025
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001026 // AST Top-Level Block.
1027 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001028 RECORD(TYPE_OFFSET);
1029 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001030 RECORD(IDENTIFIER_OFFSET);
1031 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001032 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001033 RECORD(SPECIAL_TYPES);
1034 RECORD(STATISTICS);
1035 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001036 RECORD(SELECTOR_OFFSETS);
1037 RECORD(METHOD_POOL);
1038 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001039 RECORD(SOURCE_LOCATION_OFFSETS);
1040 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001041 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001042 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001043 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001044 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001045 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001046 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001047 RECORD(SEMA_DECL_REFS);
1048 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1049 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001050 RECORD(UPDATE_VISIBLE);
1051 RECORD(DECL_UPDATE_OFFSETS);
1052 RECORD(DECL_UPDATES);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001053 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001054 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001055 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001056 RECORD(FP_PRAGMA_OPTIONS);
1057 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001058 RECORD(OPENCL_EXTENSION_TYPES);
1059 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001060 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001061 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001062 RECORD(MODULE_OFFSET_MAP);
1063 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001064 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001065 RECORD(FILE_SORTED_DECLS);
1066 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001067 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001068 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001069 RECORD(INTERESTING_IDENTIFIERS);
1070 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001071 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001072 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001073 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001074 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001075 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001076 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001077 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001078
Chris Lattner28fa4e62009-04-26 22:26:21 +00001079 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001080 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001081 RECORD(SM_SLOC_FILE_ENTRY);
1082 RECORD(SM_SLOC_BUFFER_ENTRY);
1083 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001084 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001085 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattner28fa4e62009-04-26 22:26:21 +00001087 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001088 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001089 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001090 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001091 RECORD(PP_MACRO_OBJECT_LIKE);
1092 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001093 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001094
Richard Smithfa715652015-08-31 22:43:10 +00001095 // Submodule Block.
1096 BLOCK(SUBMODULE_BLOCK);
1097 RECORD(SUBMODULE_METADATA);
1098 RECORD(SUBMODULE_DEFINITION);
1099 RECORD(SUBMODULE_UMBRELLA_HEADER);
1100 RECORD(SUBMODULE_HEADER);
1101 RECORD(SUBMODULE_TOPHEADER);
1102 RECORD(SUBMODULE_UMBRELLA_DIR);
1103 RECORD(SUBMODULE_IMPORTS);
1104 RECORD(SUBMODULE_EXPORTS);
1105 RECORD(SUBMODULE_REQUIRES);
1106 RECORD(SUBMODULE_EXCLUDED_HEADER);
1107 RECORD(SUBMODULE_LINK_LIBRARY);
1108 RECORD(SUBMODULE_CONFIG_MACRO);
1109 RECORD(SUBMODULE_CONFLICT);
1110 RECORD(SUBMODULE_PRIVATE_HEADER);
1111 RECORD(SUBMODULE_TEXTUAL_HEADER);
1112 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001113 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001114
1115 // Comments Block.
1116 BLOCK(COMMENTS_BLOCK);
1117 RECORD(COMMENTS_RAW_COMMENT);
1118
Douglas Gregor12bfa382009-10-17 00:13:19 +00001119 // Decls and Types block.
1120 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001121 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001122 RECORD(TYPE_COMPLEX);
1123 RECORD(TYPE_POINTER);
1124 RECORD(TYPE_BLOCK_POINTER);
1125 RECORD(TYPE_LVALUE_REFERENCE);
1126 RECORD(TYPE_RVALUE_REFERENCE);
1127 RECORD(TYPE_MEMBER_POINTER);
1128 RECORD(TYPE_CONSTANT_ARRAY);
1129 RECORD(TYPE_INCOMPLETE_ARRAY);
1130 RECORD(TYPE_VARIABLE_ARRAY);
1131 RECORD(TYPE_VECTOR);
1132 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001133 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001134 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001135 RECORD(TYPE_TYPEDEF);
1136 RECORD(TYPE_TYPEOF_EXPR);
1137 RECORD(TYPE_TYPEOF);
1138 RECORD(TYPE_RECORD);
1139 RECORD(TYPE_ENUM);
1140 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001141 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001142 RECORD(TYPE_DECLTYPE);
1143 RECORD(TYPE_ELABORATED);
1144 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1145 RECORD(TYPE_UNRESOLVED_USING);
1146 RECORD(TYPE_INJECTED_CLASS_NAME);
1147 RECORD(TYPE_OBJC_OBJECT);
1148 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1149 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1150 RECORD(TYPE_DEPENDENT_NAME);
1151 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1152 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1153 RECORD(TYPE_PAREN);
1154 RECORD(TYPE_PACK_EXPANSION);
1155 RECORD(TYPE_ATTRIBUTED);
1156 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001157 RECORD(TYPE_AUTO);
1158 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001159 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001160 RECORD(TYPE_DECAYED);
1161 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001162 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001163 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001164 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001165 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001166 RECORD(DECL_ENUM);
1167 RECORD(DECL_RECORD);
1168 RECORD(DECL_ENUM_CONSTANT);
1169 RECORD(DECL_FUNCTION);
1170 RECORD(DECL_OBJC_METHOD);
1171 RECORD(DECL_OBJC_INTERFACE);
1172 RECORD(DECL_OBJC_PROTOCOL);
1173 RECORD(DECL_OBJC_IVAR);
1174 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001175 RECORD(DECL_OBJC_CATEGORY);
1176 RECORD(DECL_OBJC_CATEGORY_IMPL);
1177 RECORD(DECL_OBJC_IMPLEMENTATION);
1178 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1179 RECORD(DECL_OBJC_PROPERTY);
1180 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001181 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001182 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001183 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001184 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001185 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001186 RECORD(DECL_FILE_SCOPE_ASM);
1187 RECORD(DECL_BLOCK);
1188 RECORD(DECL_CONTEXT_LEXICAL);
1189 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001190 RECORD(DECL_NAMESPACE);
1191 RECORD(DECL_NAMESPACE_ALIAS);
1192 RECORD(DECL_USING);
1193 RECORD(DECL_USING_SHADOW);
1194 RECORD(DECL_USING_DIRECTIVE);
1195 RECORD(DECL_UNRESOLVED_USING_VALUE);
1196 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1197 RECORD(DECL_LINKAGE_SPEC);
1198 RECORD(DECL_CXX_RECORD);
1199 RECORD(DECL_CXX_METHOD);
1200 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001201 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001202 RECORD(DECL_CXX_DESTRUCTOR);
1203 RECORD(DECL_CXX_CONVERSION);
1204 RECORD(DECL_ACCESS_SPEC);
1205 RECORD(DECL_FRIEND);
1206 RECORD(DECL_FRIEND_TEMPLATE);
1207 RECORD(DECL_CLASS_TEMPLATE);
1208 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1209 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001210 RECORD(DECL_VAR_TEMPLATE);
1211 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1212 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001213 RECORD(DECL_FUNCTION_TEMPLATE);
1214 RECORD(DECL_TEMPLATE_TYPE_PARM);
1215 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1216 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001217 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001218 RECORD(DECL_STATIC_ASSERT);
1219 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001220 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001221 RECORD(DECL_INDIRECTFIELD);
1222 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001223 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1224 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1225 RECORD(DECL_IMPORT);
1226 RECORD(DECL_OMP_THREADPRIVATE);
1227 RECORD(DECL_EMPTY);
1228 RECORD(DECL_OBJC_TYPE_PARAM);
1229 RECORD(DECL_OMP_CAPTUREDEXPR);
1230 RECORD(DECL_PRAGMA_COMMENT);
1231 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1232 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001233
Douglas Gregor03412ba2011-06-03 02:27:19 +00001234 // Statements and Exprs can occur in the Decls and Types block.
1235 AddStmtsExprs(Stream, Record);
1236
Douglas Gregor92a96f52011-02-08 21:58:10 +00001237 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001238 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001239 RECORD(PPD_MACRO_DEFINITION);
1240 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001241
1242 // Decls and Types block.
1243 BLOCK(EXTENSION_BLOCK);
1244 RECORD(EXTENSION_METADATA);
1245
Chris Lattner28fa4e62009-04-26 22:26:21 +00001246#undef RECORD
1247#undef BLOCK
1248 Stream.ExitBlock();
1249}
1250
Richard Smith54cc3c22014-12-11 20:50:24 +00001251/// \brief Prepares a path for being written to an AST file by converting it
1252/// to an absolute path and removing nested './'s.
1253///
1254/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001255static bool cleanPathForOutput(FileManager &FileMgr,
1256 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001257 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001258 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001259}
1260
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001261/// \brief Adjusts the given filename to only write out the portion of the
1262/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001263///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001264/// \param Filename the file name to adjust.
1265///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001266/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1267/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001268///
1269/// \returns either the original filename (if it needs no adjustment) or the
1270/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001271static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001272adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001273 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001274
Richard Smith7ed1bc92014-12-05 22:42:13 +00001275 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001276 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001277
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001278 // Verify that the filename and the system root have the same prefix.
1279 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001280 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1281 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001282 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001284 // We hit the end of the filename before we hit the end of the system root.
1285 if (!Filename[Pos])
1286 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001287
Richard Smith7ed1bc92014-12-05 22:42:13 +00001288 // If there's not a path separator at the end of the base directory nor
1289 // immediately after it, then this isn't within the base directory.
1290 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1291 if (!llvm::sys::path::is_separator(BaseDir.back()))
1292 return Filename;
1293 } else {
1294 // If the file name has a '/' at the current position, skip over the '/'.
1295 // We distinguish relative paths from absolute paths by the
1296 // absence of '/' at the beginning of relative paths.
1297 //
1298 // FIXME: This is wrong. We distinguish them by asking if the path is
1299 // absolute, which isn't the same thing. And there might be multiple '/'s
1300 // in a row. Use a better mechanism to indicate whether we have emitted an
1301 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001302 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001303 }
Mike Stump11289f42009-09-09 15:08:12 +00001304
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001305 return Filename + Pos;
1306}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001307
Ben Langmuir487ea142014-10-23 18:05:36 +00001308static ASTFileSignature getSignature() {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001309 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00001310 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1311 return S;
1312 // Rely on GetRandomNumber to eventually return non-zero...
1313 }
1314}
1315
Douglas Gregor112b9072012-10-18 05:31:06 +00001316/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001317uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1318 ASTContext &Context,
1319 StringRef isysroot,
1320 const std::string &OutputFile) {
1321 ASTFileSignature Signature = 0;
1322
Douglas Gregorbfbde532009-04-10 21:16:55 +00001323 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001324 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001325 RecordData Record;
1326
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001327 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001328 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001329 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1330 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1331 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1332 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1333 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1334 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001335 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001336 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1337 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001338 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001339 assert((!WritingModule || isysroot.empty()) &&
1340 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001341 {
1342 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1343 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1344 !isysroot.empty(), IncludeTimestamps,
1345 ASTHasCompilerErrors};
1346 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1347 getClangFullRepositoryVersion());
1348 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001349 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001350 // For implicit modules we output a signature that we can use to ensure
1351 // duplicate module builds don't collide in the cache as their output order
1352 // is non-deterministic.
1353 // FIXME: Remove this when output is deterministic.
1354 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001355 Signature = getSignature();
1356 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001357 Stream.EmitRecord(SIGNATURE, Record);
1358 }
1359
Richard Smith7ed1bc92014-12-05 22:42:13 +00001360 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001361 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001362 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001364 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001365 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001366 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1367 }
1368
Richard Smith7ed1bc92014-12-05 22:42:13 +00001369 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001370 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001371 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001372
1373 // If the home of the module is the current working directory, then we
1374 // want to pick up the cwd of the build process loading the module, not
1375 // our cwd, when we load this module.
1376 if (!PP.getHeaderSearchInfo()
1377 .getHeaderSearchOpts()
1378 .ModuleMapFileHomeIsCwd ||
1379 WritingModule->Directory->getName() != StringRef(".")) {
1380 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001381 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001382 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001384 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001385
Mehdi Amini57a41912015-09-10 01:46:39 +00001386 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001387 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1388 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001389
1390 // Write out all other paths relative to the base directory if possible.
1391 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1392 } else if (!isysroot.empty()) {
1393 // Write out paths relative to the sysroot if possible.
1394 BaseDirectory = isysroot;
1395 }
1396
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001397 // Module map file
1398 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001399 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001400
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001401 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1402
1403 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001404 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001405
1406 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001407 if (auto *AdditionalModMaps =
1408 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001409 Record.push_back(AdditionalModMaps->size());
1410 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001411 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001412 } else {
1413 Record.push_back(0);
1414 }
1415
1416 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001417 }
1418
Douglas Gregor112b9072012-10-18 05:31:06 +00001419 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001420 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001421 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001422 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001423
Richard Smith7f330cd2015-03-18 01:42:29 +00001424 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001425 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001426 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001427 continue;
1428
Richard Smith7f330cd2015-03-18 01:42:29 +00001429 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1430 AddSourceLocation(M->ImportLoc, Record);
1431 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001432 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001433 Record.push_back(M->Signature);
1434 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001435 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001436 Stream.EmitRecord(IMPORTS, Record);
1437 }
Mike Stump11289f42009-09-09 15:08:12 +00001438
Richard Smith0516b182015-09-08 19:40:14 +00001439 // Write the options block.
1440 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1441
Douglas Gregor112b9072012-10-18 05:31:06 +00001442 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001443 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001444 const LangOptions &LangOpts = Context.getLangOpts();
1445#define LANGOPT(Name, Bits, Default, Description) \
1446 Record.push_back(LangOpts.Name);
1447#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1448 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001449#include "clang/Basic/LangOptions.def"
1450#define SANITIZER(NAME, ID) \
1451 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001452#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001453
Ben Langmuircd98cb72015-06-23 18:20:18 +00001454 Record.push_back(LangOpts.ModuleFeatures.size());
1455 for (StringRef Feature : LangOpts.ModuleFeatures)
1456 AddString(Feature, Record);
1457
Douglas Gregor112b9072012-10-18 05:31:06 +00001458 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1459 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001460
1461 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001462
1463 // Comment options.
1464 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001465 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1466 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001467 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001468 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001469
Samuel Antaoee8fb302016-01-06 13:42:12 +00001470 // OpenMP offloading options.
1471 Record.push_back(LangOpts.OMPTargetTriples.size());
1472 for (auto &T : LangOpts.OMPTargetTriples)
1473 AddString(T.getTriple(), Record);
1474
1475 AddString(LangOpts.OMPHostIRFile, Record);
1476
Douglas Gregor112b9072012-10-18 05:31:06 +00001477 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1478
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001479 // Target options.
1480 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001481 const TargetInfo &Target = Context.getTargetInfo();
1482 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001483 AddString(TargetOpts.Triple, Record);
1484 AddString(TargetOpts.CPU, Record);
1485 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001486 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1487 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1488 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1489 }
1490 Record.push_back(TargetOpts.Features.size());
1491 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1492 AddString(TargetOpts.Features[I], Record);
1493 }
1494 Stream.EmitRecord(TARGET_OPTIONS, Record);
1495
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001496 // Diagnostic options.
1497 Record.clear();
1498 const DiagnosticOptions &DiagOpts
1499 = Context.getDiagnostics().getDiagnosticOptions();
1500#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1501#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1502 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1503#include "clang/Basic/DiagnosticOptions.def"
1504 Record.push_back(DiagOpts.Warnings.size());
1505 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1506 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001507 Record.push_back(DiagOpts.Remarks.size());
1508 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1509 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001510 // Note: we don't serialize the log or serialization file names, because they
1511 // are generally transient files and will almost always be overridden.
1512 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1513
Douglas Gregorc6317db2012-10-24 15:49:58 +00001514 // File system options.
1515 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001516 const FileSystemOptions &FSOpts =
1517 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001518 AddString(FSOpts.WorkingDir, Record);
1519 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1520
Douglas Gregor2d302362012-10-24 16:50:34 +00001521 // Header search options.
1522 Record.clear();
1523 const HeaderSearchOptions &HSOpts
1524 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1525 AddString(HSOpts.Sysroot, Record);
1526
1527 // Include entries.
1528 Record.push_back(HSOpts.UserEntries.size());
1529 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1530 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1531 AddString(Entry.Path, Record);
1532 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001533 Record.push_back(Entry.IsFramework);
1534 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001535 }
1536
1537 // System header prefixes.
1538 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1539 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1540 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1541 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1542 }
1543
1544 AddString(HSOpts.ResourceDir, Record);
1545 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001546 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001547 Record.push_back(HSOpts.DisableModuleHash);
1548 Record.push_back(HSOpts.UseBuiltinIncludes);
1549 Record.push_back(HSOpts.UseStandardSystemIncludes);
1550 Record.push_back(HSOpts.UseStandardCXXIncludes);
1551 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001552 // Write out the specific module cache path that contains the module files.
1553 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001554 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1555
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001556 // Preprocessor options.
1557 Record.clear();
1558 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1559
1560 // Macro definitions.
1561 Record.push_back(PPOpts.Macros.size());
1562 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1563 AddString(PPOpts.Macros[I].first, Record);
1564 Record.push_back(PPOpts.Macros[I].second);
1565 }
1566
1567 // Includes
1568 Record.push_back(PPOpts.Includes.size());
1569 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1570 AddString(PPOpts.Includes[I], Record);
1571
1572 // Macro includes
1573 Record.push_back(PPOpts.MacroIncludes.size());
1574 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1575 AddString(PPOpts.MacroIncludes[I], Record);
1576
Douglas Gregorb6368752012-10-24 23:41:50 +00001577 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001578 // Detailed record is important since it is used for the module cache hash.
1579 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001580 AddString(PPOpts.ImplicitPCHInclude, Record);
1581 AddString(PPOpts.ImplicitPTHInclude, Record);
1582 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1583 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1584
Richard Smith0516b182015-09-08 19:40:14 +00001585 // Leave the options block.
1586 Stream.ExitBlock();
1587
Douglas Gregora3b20262011-05-06 21:43:30 +00001588 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001589 SourceManager &SM = Context.getSourceManager();
1590 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001591 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001592 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1593 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001594 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001595 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001596
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001597 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001598 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001599 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001600 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001601 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001602
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001603 Record.clear();
1604 Record.push_back(SM.getMainFileID().getOpaqueValue());
1605 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1606
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001607 // Original PCH directory
1608 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001609 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001610 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001612 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001613
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001614 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001615
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001616 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001617 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1618
Mehdi Amini57a41912015-09-10 01:46:39 +00001619 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001620 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1621 }
1622
Douglas Gregor49491f72013-03-15 22:15:07 +00001623 WriteInputFiles(Context.SourceMgr,
1624 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001625 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001626 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001627 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001628}
1629
Douglas Gregor49491f72013-03-15 22:15:07 +00001630namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001631
Douglas Gregor49491f72013-03-15 22:15:07 +00001632 /// \brief An input file.
1633 struct InputFileEntry {
1634 const FileEntry *File;
1635 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001636 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001637 bool BufferOverridden;
1638 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001639
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001640} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001641
1642void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1643 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001644 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001645 using namespace llvm;
1646 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001647
Douglas Gregor72be3902012-10-19 00:38:02 +00001648 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001649 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001650 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001651 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001652 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1653 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001654 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001655 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001656 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001657 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001658
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001659 // Get all ContentCache objects for files, sorted by whether the file is a
1660 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001661 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001662 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1663 // Get this source location entry.
1664 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001665 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001666
1667 // We only care about file entries that were not overridden.
1668 if (!SLoc->isFile())
1669 continue;
1670 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001671 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001672 continue;
1673
Douglas Gregor49491f72013-03-15 22:15:07 +00001674 InputFileEntry Entry;
1675 Entry.File = Cache->OrigEntry;
1676 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001677 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001678 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001679 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001680 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001681 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001682 SortedFiles.push_front(Entry);
1683 }
1684
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001685 unsigned UserFilesNum = 0;
1686 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001687 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001688 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001689 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001690 if (InputFileID != 0)
1691 continue; // already recorded this file.
1692
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001693 // Record this entry's offset.
1694 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001695
1696 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001697
Douglas Gregor49491f72013-03-15 22:15:07 +00001698 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001699 ++UserFilesNum;
1700
Douglas Gregor72be3902012-10-19 00:38:02 +00001701 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001702 // And whether this file was overridden.
1703 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001704 INPUT_FILE,
1705 InputFileOffsets.size(),
1706 (uint64_t)Entry.File->getSize(),
1707 (uint64_t)getTimestampForOutput(Entry.File),
1708 Entry.BufferOverridden,
1709 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001710
Richard Smith7ed1bc92014-12-05 22:42:13 +00001711 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1712 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001713
Douglas Gregor112b9072012-10-18 05:31:06 +00001714 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001715
1716 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001717 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001718 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1719 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001720 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1721 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001722 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001723 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001724
1725 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001726 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1727 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001728 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001729}
1730
Douglas Gregora7f71a92009-04-10 03:52:48 +00001731//===----------------------------------------------------------------------===//
1732// Source Manager Serialization
1733//===----------------------------------------------------------------------===//
1734
1735/// \brief Create an abbreviation for the SLocEntry that refers to a
1736/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001737static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001738 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001739
David Blaikieb44f0bf2017-01-04 22:36:43 +00001740 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001741 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001746 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001751 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001752}
1753
1754/// \brief Create an abbreviation for the SLocEntry that refers to a
1755/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001756static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001757 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001758
David Blaikieb44f0bf2017-01-04 22:36:43 +00001759 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001760 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001766 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001767}
1768
1769/// \brief Create an abbreviation for the SLocEntry that refers to a
1770/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001771static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1772 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001773 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001774
David Blaikieb44f0bf2017-01-04 22:36:43 +00001775 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001776 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1777 : SM_SLOC_BUFFER_BLOB));
1778 if (Compressed)
1779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001781 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001782}
1783
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001784/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1785/// expansion.
1786static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001787 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001788
David Blaikieb44f0bf2017-01-04 22:36:43 +00001789 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001790 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001796 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001797}
1798
Douglas Gregor09b69892011-02-10 17:09:37 +00001799namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001800
Douglas Gregor09b69892011-02-10 17:09:37 +00001801 // Trait used for the on-disk hash table of header search information.
1802 class HeaderFileInfoTrait {
1803 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001804 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001805
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001806 // Keep track of the framework names we've used during serialization.
1807 SmallVector<char, 128> FrameworkStringData;
1808 llvm::StringMap<unsigned> FrameworkNameOffset;
1809
Douglas Gregor09b69892011-02-10 17:09:37 +00001810 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001811 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1812 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001813
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001814 struct key_type {
1815 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001816 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001817 };
1818 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001819
1820 typedef HeaderFileInfo data_type;
1821 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001822 typedef unsigned hash_value_type;
1823 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001824
Richard Smithe75ee0f2015-08-17 07:13:32 +00001825 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001826 // The hash is based only on size/time of the file, so that the reader can
1827 // match even when symlinking or excess path elements ("foo/../", "../")
1828 // change the form of the name. However, complete path is still the key.
1829 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001830 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001831 }
1832
1833 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001834 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001835 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001836 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001837 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001838 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001839 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001840 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1841 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1842 DataLen += 4;
1843 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001844 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001845 }
1846
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001847 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001848 using namespace llvm::support;
1849 endian::Writer<little> LE(Out);
1850 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001851 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001852 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001853 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001854 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001855 }
1856
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001857 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001858 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001859 using namespace llvm::support;
1860 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001861 uint64_t Start = Out.tell(); (void)Start;
1862
Richard Smith386bb072015-08-18 23:42:23 +00001863 unsigned char Flags = (Data.isImport << 4)
1864 | (Data.isPragmaOnce << 3)
1865 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001866 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001867 LE.write<uint8_t>(Flags);
1868 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001869
1870 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001871 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001872 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001873 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001874
1875 unsigned Offset = 0;
1876 if (!Data.Framework.empty()) {
1877 // If this header refers into a framework, save the framework name.
1878 llvm::StringMap<unsigned>::iterator Pos
1879 = FrameworkNameOffset.find(Data.Framework);
1880 if (Pos == FrameworkNameOffset.end()) {
1881 Offset = FrameworkStringData.size() + 1;
1882 FrameworkStringData.append(Data.Framework.begin(),
1883 Data.Framework.end());
1884 FrameworkStringData.push_back(0);
1885
1886 FrameworkNameOffset[Data.Framework] = Offset;
1887 } else
1888 Offset = Pos->second;
1889 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001890 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001891
Richard Smith386bb072015-08-18 23:42:23 +00001892 // FIXME: If the header is excluded, we should write out some
1893 // record of that fact.
1894 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1895 if (uint32_t ModID =
1896 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1897 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1898 assert((Value >> 2) == ModID && "overflow in header module info");
1899 LE.write<uint32_t>(Value);
1900 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001901 }
1902
Douglas Gregor09b69892011-02-10 17:09:37 +00001903 assert(Out.tell() - Start == DataLen && "Wrong data length");
1904 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001905
1906 const char *strings_begin() const { return FrameworkStringData.begin(); }
1907 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001908 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001909
Douglas Gregor09b69892011-02-10 17:09:37 +00001910} // end anonymous namespace
1911
1912/// \brief Write the header search block for the list of files that
1913///
1914/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001915void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001916 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001917 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1918
1919 if (FilesByUID.size() > HS.header_file_size())
1920 FilesByUID.resize(HS.header_file_size());
1921
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001922 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001923 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001924 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001925 unsigned NumHeaderSearchEntries = 0;
1926 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1927 const FileEntry *File = FilesByUID[UID];
1928 if (!File)
1929 continue;
1930
Richard Smith386bb072015-08-18 23:42:23 +00001931 // Get the file info. This will load info from the external source if
1932 // necessary. Skip emitting this file if we have no information on it
1933 // as a header file (in which case HFI will be null) or if it hasn't
1934 // changed since it was loaded. Also skip it if it's for a modular header
1935 // from a different module; in that case, we rely on the module(s)
1936 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001937 const HeaderFileInfo *HFI =
1938 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1939 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001940 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001941
Richard Smith7ed1bc92014-12-05 22:42:13 +00001942 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001943 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001944 SmallString<128> FilenameTmp(Filename);
1945 if (PreparePathForOutput(FilenameTmp)) {
1946 // If we performed any translation on the file name at all, we need to
1947 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001948 Filename = StringRef(strdup(FilenameTmp.c_str()));
1949 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001950 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001951
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001952 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001953 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001954 ++NumHeaderSearchEntries;
1955 }
1956
1957 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001958 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001959 uint32_t BucketOffset;
1960 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001961 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001962 llvm::raw_svector_ostream Out(TableData);
1963 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001964 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001965 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1966 }
1967
1968 // Create a blob abbreviation
1969 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001970
David Blaikieb44f0bf2017-01-04 22:36:43 +00001971 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00001972 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00001977 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00001978
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001979 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001980 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1981 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001982 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001983 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001984
1985 // Free all of the strings we had to duplicate.
1986 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001987 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001988}
1989
George Rimarc39f5492017-01-17 15:45:31 +00001990static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
1991 unsigned SLocBufferBlobCompressedAbbrv,
1992 unsigned SLocBufferBlobAbbrv) {
1993 typedef ASTWriter::RecordData::value_type RecordDataType;
1994
1995 // Compress the buffer if possible. We expect that almost all PCM
1996 // consumers will not want its contents.
1997 SmallString<0> CompressedBuffer;
1998 if (llvm::zlib::isAvailable()) {
1999 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2000 if (!E) {
2001 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2002 Blob.size() - 1};
2003 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2004 CompressedBuffer);
2005 return;
2006 }
2007 llvm::consumeError(std::move(E));
2008 }
2009
2010 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2011 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2012}
2013
Douglas Gregora7f71a92009-04-10 03:52:48 +00002014/// \brief Writes the block containing the serialized form of the
2015/// source manager.
2016///
2017/// TODO: We should probably use an on-disk hash table (stored in a
2018/// blob), indexed based on the file name, so that we only create
2019/// entries for files that we actually need. In the common case (no
2020/// errors), we probably won't have to create file entries for any of
2021/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002022void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002023 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002024 RecordData Record;
2025
Chris Lattner0910e3b2009-04-10 17:16:57 +00002026 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002027 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002028
2029 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002030 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2031 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002032 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2033 unsigned SLocBufferBlobCompressedAbbrv =
2034 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002035 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002036
Douglas Gregor258ae542009-04-27 06:38:32 +00002037 // Write out the source location entry table. We skip the first
2038 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002039 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002040 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002041 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2042 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002043 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002044 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002045 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002046 FileID FID = FileID::get(I);
2047 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002048
Douglas Gregor258ae542009-04-27 06:38:32 +00002049 // Record the offset of this source-location entry.
2050 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2051
2052 // Figure out which record code to use.
2053 unsigned Code;
2054 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002055 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2056 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002057 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002058 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002059 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002060 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002061 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002062 Record.clear();
2063 Record.push_back(Code);
2064
Douglas Gregor925296b2011-07-19 16:10:42 +00002065 // Starting offset of this entry within this module, so skip the dummy.
2066 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002067 if (SLoc->isFile()) {
2068 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002069 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002070 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2071 Record.push_back(File.hasLineDirectives());
2072
2073 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002074 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002075 if (Content->OrigEntry) {
2076 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002077 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002078
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002079 // The source location entry is a file. Emit input file ID.
2080 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2081 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002082
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002083 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002084
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002085 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002086 if (FDI != FileDeclIDs.end()) {
2087 Record.push_back(FDI->second->FirstDeclIndex);
2088 Record.push_back(FDI->second->DeclIDs.size());
2089 } else {
2090 Record.push_back(0);
2091 Record.push_back(0);
2092 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002093
2094 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2095
Richard Smithaada85c2016-02-06 02:06:43 +00002096 if (Content->BufferOverridden || Content->IsTransient)
2097 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002098 } else {
2099 // The source location entry is a buffer. The blob associated
2100 // with this entry contains the contents of the buffer.
2101
2102 // We add one to the size so that we capture the trailing NULL
2103 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2104 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002105 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002106 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002107 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002108 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002109 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002110 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002111
Mehdi Amini99d1b292016-10-01 16:38:28 +00002112 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002113 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002114 }
Richard Smithaada85c2016-02-06 02:06:43 +00002115
2116 if (EmitBlob) {
2117 // Include the implicit terminating null character in the on-disk buffer
2118 // if we're writing it uncompressed.
2119 const llvm::MemoryBuffer *Buffer =
2120 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2121 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002122 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2123 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002124 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002125 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002126 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002127 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002128 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2129 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2130 AddSourceLocation(Expansion.isMacroArgExpansion()
2131 ? SourceLocation()
2132 : Expansion.getExpansionLocEnd(),
2133 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002134
2135 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002136 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002137 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002138 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002139 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002140 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002141 }
2142 }
2143
Douglas Gregor8f45df52009-04-16 22:23:12 +00002144 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002145
2146 if (SLocEntryOffsets.empty())
2147 return;
2148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002149 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002150 // table is used for lazily loading source-location information.
2151 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002152
David Blaikieb44f0bf2017-01-04 22:36:43 +00002153 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002154 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002158 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002159 {
2160 RecordData::value_type Record[] = {
2161 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2162 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2163 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2164 bytes(SLocEntryOffsets));
2165 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002166 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002167 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002168 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002169
2170 // Write the line table. It depends on remapping working, so it must come
2171 // after the source location offsets.
2172 if (SourceMgr.hasLineTable()) {
2173 LineTableInfo &LineTable = SourceMgr.getLineTable();
2174
2175 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002176
2177 // Emit the needed file names.
2178 llvm::DenseMap<int, int> FilenameMap;
2179 for (const auto &L : LineTable) {
2180 if (L.first.ID < 0)
2181 continue;
2182 for (auto &LE : L.second) {
2183 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2184 FilenameMap.size())).second)
2185 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2186 }
2187 }
2188 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002189
2190 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002191 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002192 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002193 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002194 continue;
2195
2196 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002197 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002198
2199 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002200 Record.push_back(L.second.size());
2201 for (const auto &LE : L.second) {
2202 Record.push_back(LE.FileOffset);
2203 Record.push_back(LE.LineNo);
2204 Record.push_back(FilenameMap[LE.FilenameID]);
2205 Record.push_back((unsigned)LE.FileKind);
2206 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002207 }
2208 }
Richard Smith63078492015-09-01 07:41:55 +00002209
Douglas Gregor925296b2011-07-19 16:10:42 +00002210 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2211 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002212}
2213
Douglas Gregorc5046832009-04-27 18:38:38 +00002214//===----------------------------------------------------------------------===//
2215// Preprocessor Serialization
2216//===----------------------------------------------------------------------===//
2217
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002218static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2219 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002220 if (MacroInfo *MI = MD->getMacroInfo())
2221 if (MI->isBuiltinMacro())
2222 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002223
2224 if (IsModule) {
2225 SourceLocation Loc = MD->getLocation();
2226 if (Loc.isInvalid())
2227 return true;
2228 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2229 return true;
2230 }
2231
2232 return false;
2233}
2234
Chris Lattnereeffaef2009-04-10 17:15:23 +00002235/// \brief Writes the block containing the serialized form of the
2236/// preprocessor.
2237///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002238void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002239 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2240 if (PPRec)
2241 WritePreprocessorDetail(*PPRec);
2242
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002243 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002244 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002245
Chris Lattner0af3ba12009-04-13 01:29:17 +00002246 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2247 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002248 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002249 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002250 }
2251
2252 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002253 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002254
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002255 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002256 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002257 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002258 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002259
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002260 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002261 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002262
Richard Smithee977932015-05-01 21:22:17 +00002263 // Construct the list of identifiers with macro directives that need to be
2264 // serialized.
2265 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2266 for (auto &Id : PP.getIdentifierTable())
2267 if (Id.second->hadMacroDefinition() &&
2268 (!Id.second->isFromAST() ||
2269 Id.second->hasChangedSinceDeserialization()))
2270 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002271 // Sort the set of macro definitions that need to be serialized by the
2272 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002273 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2274 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002275
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002276 // Emit the macro directives as a list and associate the offset with the
2277 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002278 for (const IdentifierInfo *Name : MacroIdentifiers) {
2279 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002280 auto StartOffset = Stream.GetCurrentBitNo();
2281
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002282 // Emit the macro directives in reverse source order.
2283 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002284 // Once we hit an ignored macro, we're done: the rest of the chain
2285 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002286 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002287 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002288
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002289 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002290 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002291 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002292 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002293 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002294 Record.push_back(VisMD->isPublic());
2295 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002296 }
Richard Smithd7329392015-04-21 21:46:32 +00002297
Richard Smithb8b2ed62015-04-23 18:18:26 +00002298 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002299 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002300 // We write out exported module macros for PCH as well.
2301 auto Leafs = PP.getLeafModuleMacros(Name);
2302 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2303 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2304 while (!Worklist.empty()) {
2305 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002306
Manman Ren33d80292016-05-31 18:19:32 +00002307 // Emit a record indicating this submodule exports this macro.
2308 ModuleMacroRecord.push_back(
2309 getSubmoduleID(Macro->getOwningModule()));
2310 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2311 for (auto *M : Macro->overrides())
2312 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002313
Manman Ren33d80292016-05-31 18:19:32 +00002314 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2315 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002316
Manman Ren33d80292016-05-31 18:19:32 +00002317 // Enqueue overridden macros once we've visited all their ancestors.
2318 for (auto *M : Macro->overrides())
2319 if (++Visits[M] == M->getNumOverridingMacros())
2320 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002321
Manman Ren33d80292016-05-31 18:19:32 +00002322 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002323 }
Richard Smithd7329392015-04-21 21:46:32 +00002324
Richard Smithee977932015-05-01 21:22:17 +00002325 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002326 continue;
2327
Richard Smithd7329392015-04-21 21:46:32 +00002328 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002329 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2330 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002331 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002332
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002333 /// \brief Offsets of each of the macros into the bitstream, indexed by
2334 /// the local macro ID
2335 ///
2336 /// For each identifier that is associated with a macro, this map
2337 /// provides the offset into the bitstream where that macro is
2338 /// defined.
2339 std::vector<uint32_t> MacroOffsets;
2340
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002341 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2342 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2343 MacroInfo *MI = MacroInfosToEmit[I].MI;
2344 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002345
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002346 if (ID < FirstMacroID) {
2347 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2348 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002349 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002350
2351 // Record the local offset of this macro.
2352 unsigned Index = ID - FirstMacroID;
2353 if (Index == MacroOffsets.size())
2354 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2355 else {
2356 if (Index > MacroOffsets.size())
2357 MacroOffsets.resize(Index + 1);
2358
2359 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2360 }
2361
2362 AddIdentifierRef(Name, Record);
2363 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2364 AddSourceLocation(MI->getDefinitionLoc(), Record);
2365 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2366 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002367 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002368 unsigned Code;
2369 if (MI->isObjectLike()) {
2370 Code = PP_MACRO_OBJECT_LIKE;
2371 } else {
2372 Code = PP_MACRO_FUNCTION_LIKE;
2373
2374 Record.push_back(MI->isC99Varargs());
2375 Record.push_back(MI->isGNUVarargs());
2376 Record.push_back(MI->hasCommaPasting());
2377 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002378 for (const IdentifierInfo *Arg : MI->args())
2379 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002380 }
2381
2382 // If we have a detailed preprocessing record, record the macro definition
2383 // ID that corresponds to this macro.
2384 if (PPRec)
2385 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2386
2387 Stream.EmitRecord(Code, Record);
2388 Record.clear();
2389
2390 // Emit the tokens array.
2391 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2392 // Note that we know that the preprocessor does not have any annotation
2393 // tokens in it because they are created by the parser, and thus can't
2394 // be in a macro definition.
2395 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002396 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002397 Stream.EmitRecord(PP_TOKEN, Record);
2398 Record.clear();
2399 }
2400 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002401 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002402
Douglas Gregor92a96f52011-02-08 21:58:10 +00002403 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002404
2405 // Write the offsets table for macro IDs.
2406 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002407
David Blaikieb44f0bf2017-01-04 22:36:43 +00002408 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002409 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2413
David Blaikieb44f0bf2017-01-04 22:36:43 +00002414 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002415 {
2416 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2417 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2418 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2419 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002420}
2421
2422void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002423 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002424 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002425
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002426 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002427
Douglas Gregor92a96f52011-02-08 21:58:10 +00002428 // Enter the preprocessor block.
2429 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002430
Douglas Gregoraae92242010-03-19 21:51:54 +00002431 // If the preprocessor has a preprocessing record, emit it.
2432 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002433 using namespace llvm;
2434
2435 // Set up the abbreviation for
2436 unsigned InclusionAbbrev = 0;
2437 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002438 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002439 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002440 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002445 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002446 }
2447
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002448 unsigned FirstPreprocessorEntityID
2449 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2450 + NUM_PREDEF_PP_ENTITY_IDS;
2451 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002452 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002453 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2454 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002455 E != EEnd;
2456 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002457 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002458
Richard Smith66a81862015-05-04 02:25:31 +00002459 PreprocessedEntityOffsets.push_back(
2460 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002461
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002462 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002463 // Record this macro definition's ID.
2464 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002465
Douglas Gregor92a96f52011-02-08 21:58:10 +00002466 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002467 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2468 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002469 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002470
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002471 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002472 Record.push_back(ME->isBuiltinMacro());
2473 if (ME->isBuiltinMacro())
2474 AddIdentifierRef(ME->getName(), Record);
2475 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002476 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002477 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002478 continue;
2479 }
2480
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002481 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002482 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002483 Record.push_back(ID->getFileName().size());
2484 Record.push_back(ID->wasInQuotes());
2485 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002486 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002487 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002488 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002489 // Check that the FileEntry is not null because it was not resolved and
2490 // we create a PCH even with compiler errors.
2491 if (ID->getFile())
2492 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002493 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002494 continue;
2495 }
2496
2497 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2498 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002499 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002500
Douglas Gregoraae92242010-03-19 21:51:54 +00002501 // Write the offsets table for the preprocessing record.
2502 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002503 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2504
Douglas Gregoraae92242010-03-19 21:51:54 +00002505 // Write the offsets table for identifier IDs.
2506 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002507
David Blaikieb44f0bf2017-01-04 22:36:43 +00002508 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002509 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002512 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002513
Mehdi Amini57a41912015-09-10 01:46:39 +00002514 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2515 FirstPreprocessorEntityID -
2516 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002517 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002518 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002519 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002520}
2521
Richard Smith386bb072015-08-18 23:42:23 +00002522unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002523 if (!Mod)
2524 return 0;
2525
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002526 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2527 if (Known != SubmoduleIDs.end())
2528 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002529
Richard Smithdc1f0422016-07-20 19:10:16 +00002530 auto *Top = Mod->getTopLevelModule();
2531 if (Top != WritingModule &&
2532 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule)))
Richard Smith386bb072015-08-18 23:42:23 +00002533 return 0;
2534
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002535 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2536}
2537
Richard Smith386bb072015-08-18 23:42:23 +00002538unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2539 // FIXME: This can easily happen, if we have a reference to a submodule that
2540 // did not result in us loading a module file for that submodule. For
2541 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2542 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2543 assert((ID || !Mod) &&
2544 "asked for module ID for non-local, non-imported module");
2545 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002546}
2547
Douglas Gregor253eefe2011-12-01 00:59:36 +00002548/// \brief Compute the number of modules within the given tree (including the
2549/// given module).
2550static unsigned getNumberOfModules(Module *Mod) {
2551 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002552 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002553 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002554 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002555
2556 return ChildModules + 1;
2557}
2558
Douglas Gregorde3ef502011-11-30 23:21:26 +00002559void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002560 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002561 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002562
2563 // Write the abbreviations needed for the submodules block.
2564 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002565
David Blaikieb44f0bf2017-01-04 22:36:43 +00002566 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002567 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002579 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002580
David Blaikieb44f0bf2017-01-04 22:36:43 +00002581 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002582 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002584 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002585
David Blaikieb44f0bf2017-01-04 22:36:43 +00002586 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002587 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002589 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002590
David Blaikieb44f0bf2017-01-04 22:36:43 +00002591 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002592 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002594 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002595
David Blaikieb44f0bf2017-01-04 22:36:43 +00002596 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002597 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002599 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002600
David Blaikieb44f0bf2017-01-04 22:36:43 +00002601 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002602 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002605 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002606
David Blaikieb44f0bf2017-01-04 22:36:43 +00002607 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002608 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002610 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002611
David Blaikieb44f0bf2017-01-04 22:36:43 +00002612 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002613 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002615 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002616
David Blaikieb44f0bf2017-01-04 22:36:43 +00002617 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002618 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002620 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002621
David Blaikieb44f0bf2017-01-04 22:36:43 +00002622 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002623 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002625 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002626
David Blaikieb44f0bf2017-01-04 22:36:43 +00002627 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002628 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002631 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002632
David Blaikieb44f0bf2017-01-04 22:36:43 +00002633 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002634 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002636 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002637
David Blaikieb44f0bf2017-01-04 22:36:43 +00002638 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002639 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2640 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002642 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002643
Douglas Gregor253eefe2011-12-01 00:59:36 +00002644 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002645 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2646 FirstSubmoduleID -
2647 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002648 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2649
Douglas Gregor69021972011-11-30 17:33:56 +00002650 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002651 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002652 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002653 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002654 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002655 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002656 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002657
2658 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002659 if (Mod->Parent) {
2660 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002661 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002662 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002663
2664 // Emit the definition of the block.
2665 {
2666 RecordData::value_type Record[] = {
2667 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2668 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2669 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2670 Mod->ConfigMacrosExhaustive};
2671 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2672 }
2673
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002674 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002675 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002676 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002677 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002678 }
2679
Douglas Gregor69021972011-11-30 17:33:56 +00002680 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002681 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002682 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002683 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2684 UmbrellaHeader.NameAsWritten);
2685 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002686 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2687 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002688 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002689 }
Richard Smith306d8922014-10-22 23:50:56 +00002690
Douglas Gregor69021972011-11-30 17:33:56 +00002691 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002692 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002693 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002694 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002695 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002696 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002697 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2698 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2699 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002700 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002701 Module::HK_PrivateTextual},
2702 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002703 };
2704 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002705 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002706 for (auto &H : Mod->Headers[HL.HeaderKind])
2707 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2708 }
2709
2710 // Emit the top headers.
2711 {
2712 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002713 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002714 for (auto *H : TopHeaders)
2715 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002716 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002717
2718 // Emit the imports.
2719 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002720 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002721 for (auto *I : Mod->Imports)
2722 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002723 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2724 }
2725
Douglas Gregor24bb9232011-12-02 18:58:38 +00002726 // Emit the exports.
2727 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002728 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002729 for (const auto &E : Mod->Exports) {
2730 // FIXME: This may fail; we don't require that all exported modules
2731 // are local or imported.
2732 Record.push_back(getSubmoduleID(E.getPointer()));
2733 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002734 }
2735 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2736 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002737
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002738 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2739 // Might be unnecessary as use declarations are only used to build the
2740 // module itself.
2741
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002742 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002743 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002744 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2745 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002746 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002747 }
2748
Douglas Gregorfb912652013-03-20 21:10:35 +00002749 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002750 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002751 // FIXME: This may fail; we don't require that all conflicting modules
2752 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002753 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2754 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002755 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002756 }
2757
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002758 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002759 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002760 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002761 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002762 }
2763
Richard Smithdc1f0422016-07-20 19:10:16 +00002764 // Emit the initializers, if any.
2765 RecordData Inits;
2766 for (Decl *D : Context->getModuleInitializers(Mod))
2767 Inits.push_back(GetDeclRef(D));
2768 if (!Inits.empty())
2769 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2770
Douglas Gregor69021972011-11-30 17:33:56 +00002771 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002772 for (auto *M : Mod->submodules())
2773 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002774 }
2775
2776 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002777
Richard Smith23d8d032015-05-14 00:45:20 +00002778 assert((NextSubmoduleID - FirstSubmoduleID ==
2779 getNumberOfModules(WritingModule)) &&
2780 "Wrong # of submodules; found a reference to a non-local, "
2781 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002782}
2783
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002784serialization::SubmoduleID
2785ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002786 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002787 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002788
2789 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002790 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002791 Module *OwningMod
2792 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002793 if (!OwningMod)
2794 return 0;
2795
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002796 // Check whether this submodule is part of our own module.
2797 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002798 return 0;
2799
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002800 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002801}
2802
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002803void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2804 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002805 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2806 DiagStateIDMap;
2807 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002808 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002809
Richard Smithd230de22017-01-26 01:01:01 +00002810 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2811 bool IncludeNonPragmaStates) {
2812 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002813 Record.push_back(DiagStateID);
Richard Smithd230de22017-01-26 01:01:01 +00002814
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002815 if (DiagStateID == 0) {
2816 DiagStateID = ++CurrID;
Richard Smithd230de22017-01-26 01:01:01 +00002817 for (const auto &I : *State) {
2818 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002819 Record.push_back(I.first);
2820 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002821 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002822 }
Richard Smithd230de22017-01-26 01:01:01 +00002823 // Add a sentinel to mark the end of the diag IDs.
2824 Record.push_back(unsigned(-1));
2825 }
2826 };
2827
2828 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2829 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2830 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2831
2832 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2833 if (!FileIDAndFile.first.isValid() ||
2834 !FileIDAndFile.second.HasLocalTransitions)
2835 continue;
2836 AddSourceLocation(Diag.SourceMgr->getLocForStartOfFile(FileIDAndFile.first),
2837 Record);
2838 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2839 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2840 Record.push_back(StatePoint.Offset);
2841 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002842 }
2843 }
2844
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002845 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002846 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002847}
2848
Douglas Gregorc5046832009-04-27 18:38:38 +00002849//===----------------------------------------------------------------------===//
2850// Type Serialization
2851//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002852
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002853/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002854void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002855 TypeIdx &IdxRef = TypeIdxs[T];
2856 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2857 IdxRef = TypeIdx(NextTypeID++);
2858 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002859
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002860 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002861
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002862 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002863
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002864 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002865 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002866 W.Visit(T);
2867 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002868
Richard Smith290d8012016-04-06 17:06:00 +00002869 // Record the offset for this type.
2870 unsigned Index = Idx.getIndex() - FirstTypeID;
2871 if (TypeOffsets.size() == Index)
2872 TypeOffsets.push_back(Offset);
2873 else if (TypeOffsets.size() < Index) {
2874 TypeOffsets.resize(Index + 1);
2875 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002876 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002877 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002878 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002879}
2880
Douglas Gregorc5046832009-04-27 18:38:38 +00002881//===----------------------------------------------------------------------===//
2882// Declaration Serialization
2883//===----------------------------------------------------------------------===//
2884
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002885/// \brief Write the block containing all of the declaration IDs
2886/// lexically declared within the given DeclContext.
2887///
2888/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2889/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002890uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002891 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002892 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002893 return 0;
2894
Douglas Gregor8f45df52009-04-16 22:23:12 +00002895 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002896 SmallVector<uint32_t, 128> KindDeclPairs;
2897 for (const auto *D : DC->decls()) {
2898 KindDeclPairs.push_back(D->getKind());
2899 KindDeclPairs.push_back(GetDeclRef(D));
2900 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002901
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002902 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002903 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002904 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2905 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002906 return Offset;
2907}
2908
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002909void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002910 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002911
2912 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002913 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002914 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002918 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002919 {
2920 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2921 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2922 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2923 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002924
2925 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002926 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002927 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002931 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002932 {
2933 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2934 FirstDeclID - NUM_PREDEF_DECL_IDS};
2935 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2936 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002937}
2938
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002939void ASTWriter::WriteFileDeclIDsMap() {
2940 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002941
Chandler Carruthb306d732015-03-27 00:31:20 +00002942 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2943 FileDeclIDs.begin(), FileDeclIDs.end());
2944 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2945 llvm::less_first());
2946
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002947 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002948 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2949 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2950 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2951 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2952 for (auto &LocDeclEntry : Info.DeclIDs)
2953 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002954 }
2955
David Blaikieb44f0bf2017-01-04 22:36:43 +00002956 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002957 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002960 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002961 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2962 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002963 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002964}
2965
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002966void ASTWriter::WriteComments() {
2967 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002968 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002969 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002970 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002971 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002972 AddSourceRange(I->getSourceRange(), Record);
2973 Record.push_back(I->getKind());
2974 Record.push_back(I->isTrailingComment());
2975 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002976 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2977 }
2978 Stream.ExitBlock();
2979}
2980
Douglas Gregorc5046832009-04-27 18:38:38 +00002981//===----------------------------------------------------------------------===//
2982// Global Method Pool and Selector Serialization
2983//===----------------------------------------------------------------------===//
2984
Douglas Gregore84a9da2009-04-20 20:36:09 +00002985namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002986
Douglas Gregorc78d3462009-04-24 21:10:55 +00002987// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002988class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002989 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002990
2991public:
2992 typedef Selector key_type;
2993 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002994
Sebastian Redl834bb972010-08-04 17:20:04 +00002995 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002996 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002997 ObjCMethodList Instance, Factory;
2998 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002999 typedef const data_type& data_type_ref;
3000
Justin Bogner25463f12014-04-18 20:27:24 +00003001 typedef unsigned hash_value_type;
3002 typedef unsigned offset_type;
3003
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003004 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003005
Justin Bogner25463f12014-04-18 20:27:24 +00003006 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003007 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003008 }
Mike Stump11289f42009-09-09 15:08:12 +00003009
3010 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003011 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003012 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003013 using namespace llvm::support;
3014 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003015 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003016 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003017 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3018 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003019 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003020 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003021 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003022 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003023 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003024 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003025 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003026 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003027 return std::make_pair(KeyLen, DataLen);
3028 }
Mike Stump11289f42009-09-09 15:08:12 +00003029
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003030 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003031 using namespace llvm::support;
3032 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003033 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003034 assert((Start >> 32) == 0 && "Selector key offset too large");
3035 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003036 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003037 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003038 if (N == 0)
3039 N = 1;
3040 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003041 LE.write<uint32_t>(
3042 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003043 }
Mike Stump11289f42009-09-09 15:08:12 +00003044
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003045 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003046 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003047 using namespace llvm::support;
3048 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003049 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003050 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003051 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003052 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003053 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003054 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003055 ++NumInstanceMethods;
3056
3057 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003058 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003059 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003060 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003061 ++NumFactoryMethods;
3062
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003063 unsigned InstanceBits = Methods.Instance.getBits();
3064 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003065 unsigned InstanceHasMoreThanOneDeclBit =
3066 Methods.Instance.hasMoreThanOneDecl();
3067 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3068 (InstanceHasMoreThanOneDeclBit << 2) |
3069 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003070 unsigned FactoryBits = Methods.Factory.getBits();
3071 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003072 unsigned FactoryHasMoreThanOneDeclBit =
3073 Methods.Factory.hasMoreThanOneDecl();
3074 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3075 (FactoryHasMoreThanOneDeclBit << 2) |
3076 FactoryBits;
3077 LE.write<uint16_t>(FullInstanceBits);
3078 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003079 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003080 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003081 if (Method->getMethod())
3082 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003083 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003084 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003085 if (Method->getMethod())
3086 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003087
3088 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003089 }
3090};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003091
Douglas Gregorc78d3462009-04-24 21:10:55 +00003092} // end anonymous namespace
3093
Sebastian Redla19a67f2010-08-03 21:58:15 +00003094/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003095///
3096/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003097/// in an on-disk hash table indexed by the selector. The hash table also
3098/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003099void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003100 using namespace llvm;
3101
Sebastian Redla19a67f2010-08-03 21:58:15 +00003102 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003103 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003104 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003105 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003106 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003107 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003108 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003109 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003110
Sebastian Redla19a67f2010-08-03 21:58:15 +00003111 // Create the on-disk hash table representation. We walk through every
3112 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003113 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003114 for (auto &SelectorAndID : SelectorIDs) {
3115 Selector S = SelectorAndID.first;
3116 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003117 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003118 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003119 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003120 ObjCMethodList(),
3121 ObjCMethodList()
3122 };
3123 if (F != SemaRef.MethodPool.end()) {
3124 Data.Instance = F->second.first;
3125 Data.Factory = F->second.second;
3126 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003127 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003128 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003129 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003130 // Selector already exists. Did it change?
3131 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003132 for (ObjCMethodList *M = &Data.Instance;
3133 !changed && M && M->getMethod(); M = M->getNext()) {
3134 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003135 changed = true;
3136 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003137 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003138 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003139 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003140 changed = true;
3141 }
3142 if (!changed)
3143 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003144 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003145 // A new method pool entry.
3146 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003147 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003148 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003149 }
3150
Douglas Gregorc78d3462009-04-24 21:10:55 +00003151 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003152 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003153 uint32_t BucketOffset;
3154 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003155 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003156 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003157 llvm::raw_svector_ostream Out(MethodPool);
3158 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003159 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003160 BucketOffset = Generator.Emit(Out, Trait);
3161 }
3162
3163 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003164 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003165 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003169 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003170
Douglas Gregor95c13f52009-04-25 17:48:32 +00003171 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003172 {
3173 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3174 NumTableEntries};
3175 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3176 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003177
3178 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003179 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003180 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003184 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003185
3186 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003187 {
3188 RecordData::value_type Record[] = {
3189 SELECTOR_OFFSETS, SelectorOffsets.size(),
3190 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3191 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3192 bytes(SelectorOffsets));
3193 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003194 }
3195}
3196
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003197/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003198void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003199 using namespace llvm;
3200 if (SemaRef.ReferencedSelectors.empty())
3201 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003202
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003203 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003204 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003206 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003207 // very tricky to fix, and given that @selector shouldn't really appear in
3208 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003209 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3210 Selector Sel = SelectorAndLocation.first;
3211 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003212 Writer.AddSelectorRef(Sel);
3213 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003214 }
Richard Smithe64e7452016-04-18 21:54:58 +00003215 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003216}
3217
Douglas Gregorc5046832009-04-27 18:38:38 +00003218//===----------------------------------------------------------------------===//
3219// Identifier Table Serialization
3220//===----------------------------------------------------------------------===//
3221
Richard Smithb3761912015-02-05 23:08:52 +00003222/// Determine the declaration that should be put into the name lookup table to
3223/// represent the given declaration in this module. This is usually D itself,
3224/// but if D was imported and merged into a local declaration, we want the most
3225/// recent local declaration instead. The chosen declaration will be the most
3226/// recent declaration in any module that imports this one.
3227static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3228 NamedDecl *D) {
3229 if (!LangOpts.Modules || !D->isFromASTFile())
3230 return D;
3231
3232 if (Decl *Redecl = D->getPreviousDecl()) {
3233 // For Redeclarable decls, a prior declaration might be local.
3234 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003235 // If we find a local decl, we're done.
3236 if (!Redecl->isFromASTFile()) {
3237 // Exception: in very rare cases (for injected-class-names), not all
3238 // redeclarations are in the same semantic context. Skip ones in a
3239 // different context. They don't go in this lookup table at all.
3240 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3241 D->getDeclContext()->getRedeclContext()))
3242 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003243 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003244 }
3245
Richard Smithfe620d22015-03-05 23:24:12 +00003246 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003247 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003248 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003249 break;
3250 }
3251 } else if (Decl *First = D->getCanonicalDecl()) {
3252 // For Mergeable decls, the first decl might be local.
3253 if (!First->isFromASTFile())
3254 return cast<NamedDecl>(First);
3255 }
3256
3257 // All declarations are imported. Our most recent declaration will also be
3258 // the most recent one in anyone who imports us.
3259 return D;
3260}
3261
Douglas Gregorc78d3462009-04-24 21:10:55 +00003262namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003263
Richard Smithcf97cf62015-04-19 01:34:23 +00003264class ASTIdentifierTableTrait {
3265 ASTWriter &Writer;
3266 Preprocessor &PP;
3267 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003268 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003269 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003270 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003271
3272 /// \brief Determines whether this is an "interesting" identifier that needs a
3273 /// full IdentifierInfo structure written into the hash table. Notably, this
3274 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3275 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003276 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003277 if (MacroOffset ||
3278 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003279 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003280 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003281 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003282 return true;
3283
3284 return false;
3285 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003286
Douglas Gregore84a9da2009-04-20 20:36:09 +00003287public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003288 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003289 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003290
Sebastian Redl539c5062010-08-18 23:57:32 +00003291 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003292 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003293
Justin Bogner25463f12014-04-18 20:27:24 +00003294 typedef unsigned hash_value_type;
3295 typedef unsigned offset_type;
3296
Richard Smithd7329392015-04-21 21:46:32 +00003297 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003298 IdentifierResolver &IdResolver, bool IsModule,
3299 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003300 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003301 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3302 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003303
Richard Smithd79514e2016-02-05 19:03:40 +00003304 bool needDecls() const { return NeedDecls; }
3305
Justin Bogner25463f12014-04-18 20:27:24 +00003306 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003307 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003308 }
Mike Stump11289f42009-09-09 15:08:12 +00003309
Richard Smith33e0f7e2015-07-22 02:08:40 +00003310 bool isInterestingIdentifier(const IdentifierInfo *II) {
3311 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3312 return isInterestingIdentifier(II, MacroOffset);
3313 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003314
Richard Smith9c254182015-07-19 21:41:12 +00003315 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3316 return isInterestingIdentifier(II, 0);
3317 }
3318
Mike Stump11289f42009-09-09 15:08:12 +00003319 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003320 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003321 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003322 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003323 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3324 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003325 DataLen += 2; // 2 bytes for builtin ID
3326 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003327 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003328 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003329
Richard Smitha534a312015-07-21 23:54:07 +00003330 if (NeedDecls) {
3331 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3332 DEnd = IdResolver.end();
3333 D != DEnd; ++D)
3334 DataLen += 4;
3335 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003336 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003337 using namespace llvm::support;
3338 endian::Writer<little> LE(Out);
3339
Richard Smithdf8a8312015-03-13 04:05:01 +00003340 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003341 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003342 // We emit the key length after the data length so that every
3343 // string is preceded by a 16-bit length. This matches the PTH
3344 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003345 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003346 return std::make_pair(KeyLen, DataLen);
3347 }
Mike Stump11289f42009-09-09 15:08:12 +00003348
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003349 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003350 unsigned KeyLen) {
3351 // Record the location of the key data. This is used when generating
3352 // the mapping from persistent IDs to strings.
3353 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003354
3355 // Emit the offset of the key/data length information to the interesting
3356 // identifiers table if necessary.
3357 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3358 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3359
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003360 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003361 }
Mike Stump11289f42009-09-09 15:08:12 +00003362
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003363 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003364 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003365 using namespace llvm::support;
3366 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003367
Richard Smithd7329392015-04-21 21:46:32 +00003368 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3369 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003370 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003371 return;
3372 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003373
Justin Bognere1c147c2014-03-28 22:03:19 +00003374 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003375 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3376 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003377 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003378 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003379 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003380 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003381 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3382 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003383 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003384 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003385 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003386 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003387
Richard Smithd7329392015-04-21 21:46:32 +00003388 if (HadMacroDefinition)
3389 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003390
Richard Smitha534a312015-07-21 23:54:07 +00003391 if (NeedDecls) {
3392 // Emit the declaration IDs in reverse order, because the
3393 // IdentifierResolver provides the declarations as they would be
3394 // visible (e.g., the function "stat" would come before the struct
3395 // "stat"), but the ASTReader adds declarations to the end of the list
3396 // (so we need to see the struct "stat" before the function "stat").
3397 // Only emit declarations that aren't from a chained PCH, though.
3398 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3399 IdResolver.end());
3400 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3401 DEnd = Decls.rend();
3402 D != DEnd; ++D)
3403 LE.write<uint32_t>(
3404 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3405 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003406 }
3407};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003408
Douglas Gregore84a9da2009-04-20 20:36:09 +00003409} // end anonymous namespace
3410
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003411/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003412///
3413/// The identifier table consists of a blob containing string data
3414/// (the actual identifiers themselves) and a separate "offsets" index
3415/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003416void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3417 IdentifierResolver &IdResolver,
3418 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003419 using namespace llvm;
3420
Richard Smith33e0f7e2015-07-22 02:08:40 +00003421 RecordData InterestingIdents;
3422
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003423 // Create and write out the blob that contains the identifier
3424 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003425 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003426 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003427 ASTIdentifierTableTrait Trait(
3428 *this, PP, IdResolver, IsModule,
3429 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003430
Douglas Gregore6648fb2009-04-28 20:33:11 +00003431 // Look for any identifiers that were named while processing the
3432 // headers, but are otherwise not needed. We add these to the hash
3433 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003434 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003435 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003436 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003437 for (const auto &ID : PP.getIdentifierTable())
3438 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003439 // Sort the identifiers lexicographically before getting them references so
3440 // that their order is stable.
3441 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3442 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003443 if (Trait.isInterestingNonMacroIdentifier(II))
3444 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003445
Sebastian Redlff4a2952010-07-23 23:49:55 +00003446 // Create the on-disk hash table representation. We only store offsets
3447 // for identifiers that appear here for the first time.
3448 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003449 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003450 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003451 IdentID ID = IdentIDPair.second;
3452 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003453 // Write out identifiers if either the ID is local or the identifier has
3454 // changed since it was loaded.
3455 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3456 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003457 (Trait.needDecls() &&
3458 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003459 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003460 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003461
Douglas Gregore84a9da2009-04-20 20:36:09 +00003462 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003463 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003464 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003465 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003466 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003467 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003468 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003469 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003470 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003471 }
3472
3473 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003474 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003475 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003478 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003479
3480 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003481 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003482 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003483 }
3484
3485 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003486 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003487 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003489 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003491 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003492
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003493#ifndef NDEBUG
3494 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3495 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3496#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003497
3498 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3499 IdentifierOffsets.size(),
3500 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003501 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003502 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003503
3504 // In C++, write the list of interesting identifiers (those that are
3505 // defined as macros, poisoned, or similar unusual things).
3506 if (!InterestingIdents.empty())
3507 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003508}
3509
Douglas Gregorc5046832009-04-27 18:38:38 +00003510//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003511// DeclContext's Name Lookup Table Serialization
3512//===----------------------------------------------------------------------===//
3513
3514namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003515
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003516// Trait used for the on-disk hash table used in the method pool.
3517class ASTDeclContextNameLookupTrait {
3518 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003519 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003520
3521public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003522 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003523 typedef key_type key_type_ref;
3524
Richard Smithd88a7f12015-09-01 20:35:42 +00003525 /// A start and end index into DeclIDs, representing a sequence of decls.
3526 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003527 typedef const data_type& data_type_ref;
3528
Justin Bogner25463f12014-04-18 20:27:24 +00003529 typedef unsigned hash_value_type;
3530 typedef unsigned offset_type;
3531
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003532 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3533
Richard Smithd88a7f12015-09-01 20:35:42 +00003534 template<typename Coll>
3535 data_type getData(const Coll &Decls) {
3536 unsigned Start = DeclIDs.size();
3537 for (NamedDecl *D : Decls) {
3538 DeclIDs.push_back(
3539 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3540 }
3541 return std::make_pair(Start, DeclIDs.size());
3542 }
3543
3544 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3545 unsigned Start = DeclIDs.size();
3546 for (auto ID : FromReader)
3547 DeclIDs.push_back(ID);
3548 return std::make_pair(Start, DeclIDs.size());
3549 }
3550
3551 static bool EqualKey(key_type_ref a, key_type_ref b) {
3552 return a == b;
3553 }
3554
Richard Smitha06c7e62015-08-26 23:55:49 +00003555 hash_value_type ComputeHash(DeclarationNameKey Name) {
3556 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003557 }
3558
Richard Smithd88a7f12015-09-01 20:35:42 +00003559 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3560 assert(Writer.hasChain() &&
3561 "have reference to loaded module file but no chain?");
3562
3563 using namespace llvm::support;
3564 endian::Writer<little>(Out)
3565 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3566 }
3567
Richard Smitha06c7e62015-08-26 23:55:49 +00003568 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3569 DeclarationNameKey Name,
3570 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003571 using namespace llvm::support;
3572 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003573 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003574 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003575 case DeclarationName::Identifier:
3576 case DeclarationName::ObjCZeroArgSelector:
3577 case DeclarationName::ObjCOneArgSelector:
3578 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003579 case DeclarationName::CXXLiteralOperatorName:
3580 KeyLen += 4;
3581 break;
3582 case DeclarationName::CXXOperatorName:
3583 KeyLen += 1;
3584 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003585 case DeclarationName::CXXConstructorName:
3586 case DeclarationName::CXXDestructorName:
3587 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003588 case DeclarationName::CXXUsingDirective:
3589 break;
3590 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003591 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003592
Richard Smithf02662d2015-07-30 03:17:16 +00003593 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003594 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3595 assert(uint16_t(DataLen) == DataLen &&
3596 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003597 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003598
3599 return std::make_pair(KeyLen, DataLen);
3600 }
3601
Richard Smitha06c7e62015-08-26 23:55:49 +00003602 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003603 using namespace llvm::support;
3604 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003605 LE.write<uint8_t>(Name.getKind());
3606 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003607 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003608 case DeclarationName::CXXLiteralOperatorName:
3609 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003610 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003611 case DeclarationName::ObjCZeroArgSelector:
3612 case DeclarationName::ObjCOneArgSelector:
3613 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003614 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003615 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003616 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003617 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003618 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003619 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003620 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003621 case DeclarationName::CXXConstructorName:
3622 case DeclarationName::CXXDestructorName:
3623 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003624 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003625 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003626 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003627
3628 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003629 }
3630
Richard Smitha06c7e62015-08-26 23:55:49 +00003631 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3632 unsigned DataLen) {
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 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003636 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3637 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003638 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3639 }
3640};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003641
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003642} // end anonymous namespace
3643
Chandler Carruthe972c362015-03-26 03:11:40 +00003644bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3645 DeclContext *DC) {
3646 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3647}
3648
3649bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3650 DeclContext *DC) {
3651 for (auto *D : Result.getLookupResult())
3652 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3653 return false;
3654
3655 return true;
3656}
3657
Richard Smithd88a7f12015-09-01 20:35:42 +00003658void
Chandler Carruthe972c362015-03-26 03:11:40 +00003659ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003660 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003661 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3662 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003663 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003664
Chandler Carruthe972c362015-03-26 03:11:40 +00003665 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003666 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003667 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3668
3669 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003670 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3671 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003672 ASTDeclContextNameLookupTrait Trait(*this);
3673
Chandler Carruthe972c362015-03-26 03:11:40 +00003674 // The first step is to collect the declaration names which we need to
3675 // serialize into the name lookup table, and to collect them in a stable
3676 // order.
3677 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003678
Chandler Carruthe972c362015-03-26 03:11:40 +00003679 // We also build up small sets of the constructor and conversion function
3680 // names which are visible.
3681 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003682
Chandler Carruthe972c362015-03-26 03:11:40 +00003683 for (auto &Lookup : *DC->buildLookup()) {
3684 auto &Name = Lookup.first;
3685 auto &Result = Lookup.second;
3686
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003687 // If there are no local declarations in our lookup result, we
3688 // don't need to write an entry for the name at all. If we can't
3689 // write out a lookup set without performing more deserialization,
3690 // just skip this entry.
3691 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003692 isLookupResultEntirelyExternal(Result, DC))
3693 continue;
3694
3695 // We also skip empty results. If any of the results could be external and
3696 // the currently available results are empty, then all of the results are
3697 // external and we skip it above. So the only way we get here with an empty
3698 // results is when no results could have been external *and* we have
3699 // external results.
3700 //
3701 // FIXME: While we might want to start emitting on-disk entries for negative
3702 // lookups into a decl context as an optimization, today we *have* to skip
3703 // them because there are names with empty lookup results in decl contexts
3704 // which we can't emit in any stable ordering: we lookup constructors and
3705 // conversion functions in the enclosing namespace scope creating empty
3706 // results for them. This in almost certainly a bug in Clang's name lookup,
3707 // but that is likely to be hard or impossible to fix and so we tolerate it
3708 // here by omitting lookups with empty results.
3709 if (Lookup.second.getLookupResult().empty())
3710 continue;
3711
3712 switch (Lookup.first.getNameKind()) {
3713 default:
3714 Names.push_back(Lookup.first);
3715 break;
3716
Richard Smithcd45dbc2014-04-19 03:48:30 +00003717 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003718 assert(isa<CXXRecordDecl>(DC) &&
3719 "Cannot have a constructor name outside of a class!");
3720 ConstructorNameSet.insert(Name);
3721 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003722
3723 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003724 assert(isa<CXXRecordDecl>(DC) &&
3725 "Cannot have a conversion function name outside of a class!");
3726 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003727 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003728 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003729 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003730
Chandler Carruthe972c362015-03-26 03:11:40 +00003731 // Sort the names into a stable order.
3732 std::sort(Names.begin(), Names.end());
3733
Chandler Carruthffbf7052015-03-26 22:27:09 +00003734 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003735 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003736 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003737
Chandler Carruthffbf7052015-03-26 22:27:09 +00003738 // First we try the easy case by forming the current context's constructor
3739 // name and adding that name first. This is a very useful optimization to
3740 // avoid walking the lexical declarations in many cases, and it also
3741 // handles the only case where a constructor name can come from some other
3742 // lexical context -- when that name is an implicit constructor merged from
3743 // another declaration in the redecl chain. Any non-implicit constructor or
3744 // conversion function which doesn't occur in all the lexical contexts
3745 // would be an ODR violation.
3746 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3747 Context->getCanonicalType(Context->getRecordType(D)));
3748 if (ConstructorNameSet.erase(ImplicitCtorName))
3749 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003750
Chandler Carruthffbf7052015-03-26 22:27:09 +00003751 // If we still have constructors or conversion functions, we walk all the
3752 // names in the decl and add the constructors and conversion functions
3753 // which are visible in the order they lexically occur within the context.
3754 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3755 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3756 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3757 auto Name = ChildND->getDeclName();
3758 switch (Name.getNameKind()) {
3759 default:
3760 continue;
3761
3762 case DeclarationName::CXXConstructorName:
3763 if (ConstructorNameSet.erase(Name))
3764 Names.push_back(Name);
3765 break;
3766
3767 case DeclarationName::CXXConversionFunctionName:
3768 if (ConversionNameSet.erase(Name))
3769 Names.push_back(Name);
3770 break;
3771 }
3772
3773 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3774 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003775 }
3776
Chandler Carruthe972c362015-03-26 03:11:40 +00003777 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3778 "constructors by walking all the "
3779 "lexical members of the context.");
3780 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3781 "conversion functions by walking all "
3782 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003783 }
3784
Chandler Carruthe972c362015-03-26 03:11:40 +00003785 // Next we need to do a lookup with each name into this decl context to fully
3786 // populate any results from external sources. We don't actually use the
3787 // results of these lookups because we only want to use the results after all
3788 // results have been loaded and the pointers into them will be stable.
3789 for (auto &Name : Names)
3790 DC->lookup(Name);
3791
3792 // Now we need to insert the results for each name into the hash table. For
3793 // constructor names and conversion function names, we actually need to merge
3794 // all of the results for them into one list of results each and insert
3795 // those.
3796 SmallVector<NamedDecl *, 8> ConstructorDecls;
3797 SmallVector<NamedDecl *, 8> ConversionDecls;
3798
3799 // Now loop over the names, either inserting them or appending for the two
3800 // special cases.
3801 for (auto &Name : Names) {
3802 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3803
3804 switch (Name.getNameKind()) {
3805 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003806 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003807 break;
3808
3809 case DeclarationName::CXXConstructorName:
3810 ConstructorDecls.append(Result.begin(), Result.end());
3811 break;
3812
3813 case DeclarationName::CXXConversionFunctionName:
3814 ConversionDecls.append(Result.begin(), Result.end());
3815 break;
3816 }
3817 }
3818
3819 // Handle our two special cases if we ended up having any. We arbitrarily use
3820 // the first declaration's name here because the name itself isn't part of
3821 // the key, only the kind of name is used.
3822 if (!ConstructorDecls.empty())
3823 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003824 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003825 if (!ConversionDecls.empty())
3826 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003827 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003828
Richard Smithd88a7f12015-09-01 20:35:42 +00003829 // Create the on-disk hash table. Also emit the existing imported and
3830 // merged table if there is one.
3831 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3832 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003833}
3834
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003835/// \brief Write the block containing all of the declaration IDs
3836/// visible from the given DeclContext.
3837///
3838/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003839/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003840uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3841 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003842 // If we imported a key declaration of this namespace, write the visible
3843 // lookup results as an update record for it rather than including them
3844 // on this declaration. We will only look at key declarations on reload.
3845 if (isa<NamespaceDecl>(DC) && Chain &&
3846 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3847 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003848 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003849 Prev = Prev->getPreviousDecl())
3850 if (!Prev->isFromASTFile())
3851 return 0;
3852
3853 // Note that we need to emit an update record for the primary context.
3854 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3855
3856 // Make sure all visible decls are written. They will be recorded later. We
3857 // do this using a side data structure so we can sort the names into
3858 // a deterministic order.
3859 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3860 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3861 LookupResults;
3862 if (Map) {
3863 LookupResults.reserve(Map->size());
3864 for (auto &Entry : *Map)
3865 LookupResults.push_back(
3866 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3867 }
3868
3869 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3870 for (auto &NameAndResult : LookupResults) {
3871 DeclarationName Name = NameAndResult.first;
3872 DeclContext::lookup_result Result = NameAndResult.second;
3873 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3874 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3875 // We have to work around a name lookup bug here where negative lookup
3876 // results for these names get cached in namespace lookup tables (these
3877 // names should never be looked up in a namespace).
3878 assert(Result.empty() && "Cannot have a constructor or conversion "
3879 "function name in a namespace!");
3880 continue;
3881 }
3882
3883 for (NamedDecl *ND : Result)
3884 if (!ND->isFromASTFile())
3885 GetDeclRef(ND);
3886 }
3887
3888 return 0;
3889 }
3890
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003891 if (DC->getPrimaryContext() != DC)
3892 return 0;
3893
Chandler Carruthe972c362015-03-26 03:11:40 +00003894 // Skip contexts which don't support name lookup.
3895 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003896 return 0;
3897
3898 // If not in C++, we perform name lookup for the translation unit via the
3899 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003900 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003901 return 0;
3902
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003903 // Serialize the contents of the mapping used for lookup. Note that,
3904 // although we have two very different code paths, the serialized
3905 // representation is the same for both cases: a declaration name,
3906 // followed by a size, followed by references to the visible
3907 // declarations that have that name.
3908 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003909 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003910 if (!Map || Map->empty())
3911 return 0;
3912
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003913 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003914 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003915 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003916
3917 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003918 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003919 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003920 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003921 ++NumVisibleDeclContexts;
3922 return Offset;
3923}
3924
Sebastian Redla4071b42010-08-24 00:50:09 +00003925/// \brief Write an UPDATE_VISIBLE block for the given context.
3926///
3927/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3928/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003929/// (in C++), for namespaces, and for classes with forward-declared unscoped
3930/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003931void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003932 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003933 if (!Map || Map->empty())
3934 return;
3935
Sebastian Redla4071b42010-08-24 00:50:09 +00003936 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003937 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003938 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003939
Richard Smith5fc18a92015-07-12 23:43:21 +00003940 // If we're updating a namespace, select a key declaration as the key for the
3941 // update record; those are the only ones that will be checked on reload.
3942 if (isa<NamespaceDecl>(DC))
3943 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3944
Sebastian Redla4071b42010-08-24 00:50:09 +00003945 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003946 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003947 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003948}
3949
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003950/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3951void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003952 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003953 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3954}
3955
3956/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3957void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003958 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003959 return;
3960
3961 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3962 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00003963 for (const auto &I:Opts.OptMap) {
3964 AddString(I.getKey(), Record);
3965 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00003966 Record.push_back(V.Supported ? 1 : 0);
3967 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00003968 Record.push_back(V.Avail);
3969 Record.push_back(V.Core);
3970 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003971 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3972}
3973
Yaxun Liu5b746652016-12-18 05:18:55 +00003974void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
3975 if (!SemaRef.Context.getLangOpts().OpenCL)
3976 return;
3977
3978 RecordData Record;
3979 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
3980 Record.push_back(
3981 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
3982 Record.push_back(I.second.size());
3983 for (auto Ext : I.second)
3984 AddString(Ext, Record);
3985 }
3986 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
3987}
3988
3989void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
3990 if (!SemaRef.Context.getLangOpts().OpenCL)
3991 return;
3992
3993 RecordData Record;
3994 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
3995 Record.push_back(getDeclID(I.first));
3996 Record.push_back(static_cast<unsigned>(I.second.size()));
3997 for (auto Ext : I.second)
3998 AddString(Ext, Record);
3999 }
4000 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4001}
4002
Justin Lebar67a78a62016-10-08 22:15:58 +00004003void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4004 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4005 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4006 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4007 }
4008}
4009
Douglas Gregor404cdde2012-01-27 01:47:08 +00004010void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004011 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004012 RecordData Categories;
4013
4014 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4015 unsigned Size = 0;
4016 unsigned StartIndex = Categories.size();
4017
4018 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4019
4020 // Allocate space for the size.
4021 Categories.push_back(0);
4022
4023 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004024 for (ObjCInterfaceDecl::known_categories_iterator
4025 Cat = Class->known_categories_begin(),
4026 CatEnd = Class->known_categories_end();
4027 Cat != CatEnd; ++Cat, ++Size) {
4028 assert(getDeclID(*Cat) != 0 && "Bogus category");
4029 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004030 }
4031
4032 // Update the size.
4033 Categories[StartIndex] = Size;
4034
4035 // Record this interface -> category map.
4036 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4037 CategoriesMap.push_back(CatInfo);
4038 }
4039
4040 // Sort the categories map by the definition ID, since the reader will be
4041 // performing binary searches on this information.
4042 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4043
4044 // Emit the categories map.
4045 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004046
David Blaikieb44f0bf2017-01-04 22:36:43 +00004047 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004048 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004051 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004052
4053 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4054 Stream.EmitRecordWithBlob(AbbrevID, Record,
4055 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004056 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004057
Douglas Gregor404cdde2012-01-27 01:47:08 +00004058 // Emit the category lists.
4059 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4060}
4061
Richard Smithe40f2ba2013-08-07 21:41:30 +00004062void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4063 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4064
4065 if (LPTMap.empty())
4066 return;
4067
4068 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004069 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004070 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004071 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004072 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004073 AddDeclRef(LPT.D, Record);
4074 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004075
Justin Lebar28f09c52016-10-10 16:26:08 +00004076 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004077 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004078 }
4079 }
4080 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4081}
4082
Dario Domizioli13a0a382014-05-23 12:13:25 +00004083/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4084void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4085 RecordData Record;
4086 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4087 AddSourceLocation(PragmaLoc, Record);
4088 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4089}
4090
Nico Weber779355f2016-03-02 23:22:00 +00004091/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4092void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4093 RecordData Record;
4094 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4095 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4096}
4097
Nico Weber42932312016-03-03 00:17:35 +00004098/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4099//module.
4100void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4101 RecordData Record;
4102 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4103 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4104 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4105}
4106
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004107void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4108 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004109 // Enter the extension block.
4110 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4111
4112 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004113 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004114 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4115 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4116 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4117 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4118 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4119 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004120 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004121
4122 // Emit the metadata record.
4123 RecordData Record;
4124 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4125 Record.push_back(EXTENSION_METADATA);
4126 Record.push_back(Metadata.MajorVersion);
4127 Record.push_back(Metadata.MinorVersion);
4128 Record.push_back(Metadata.BlockName.size());
4129 Record.push_back(Metadata.UserInfo.size());
4130 SmallString<64> Buffer;
4131 Buffer += Metadata.BlockName;
4132 Buffer += Metadata.UserInfo;
4133 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4134
4135 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004136 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004137
4138 // Exit the extension block.
4139 Stream.ExitBlock();
4140}
4141
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004142//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004143// General Serialization Routines
4144//===----------------------------------------------------------------------===//
4145
Richard Smith69c82bf2016-04-01 22:52:03 +00004146/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004147void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4148 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004149 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004150 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004151 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004152 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004153
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004154#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004155
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004156 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004157}
4158
John McCallf413f5e2013-05-03 00:10:13 +00004159void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4160 AddSourceLocation(Tok.getLocation(), Record);
4161 Record.push_back(Tok.getLength());
4162
4163 // FIXME: When reading literal tokens, reconstruct the literal pointer
4164 // if it is needed.
4165 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4166 // FIXME: Should translate token kind to a stable encoding.
4167 Record.push_back(Tok.getKind());
4168 // FIXME: Should translate token flags to a stable encoding.
4169 Record.push_back(Tok.getFlags());
4170}
4171
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004172void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004173 Record.push_back(Str.size());
4174 Record.insert(Record.end(), Str.begin(), Str.end());
4175}
4176
Richard Smith7ed1bc92014-12-05 22:42:13 +00004177bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004178 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004179
Richard Smith54cc3c22014-12-11 20:50:24 +00004180 bool Changed =
4181 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004182
4183 // Remove a prefix to make the path relative, if relevant.
4184 const char *PathBegin = Path.data();
4185 const char *PathPtr =
4186 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4187 if (PathPtr != PathBegin) {
4188 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4189 Changed = true;
4190 }
4191
4192 return Changed;
4193}
4194
4195void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4196 SmallString<128> FilePath(Path);
4197 PreparePathForOutput(FilePath);
4198 AddString(FilePath, Record);
4199}
4200
Mehdi Amini57a41912015-09-10 01:46:39 +00004201void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004202 StringRef Path) {
4203 SmallString<128> FilePath(Path);
4204 PreparePathForOutput(FilePath);
4205 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4206}
4207
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004208void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4209 RecordDataImpl &Record) {
4210 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004211 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004212 Record.push_back(*Minor + 1);
4213 else
4214 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004215 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004216 Record.push_back(*Subminor + 1);
4217 else
4218 Record.push_back(0);
4219}
4220
Douglas Gregore84a9da2009-04-20 20:36:09 +00004221/// \brief Note that the identifier II occurs at the given offset
4222/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004223void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004224 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004225 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004226 // up earlier in the chain and thus don't need an offset.
4227 if (ID >= FirstIdentID)
4228 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004229}
4230
Douglas Gregor95c13f52009-04-25 17:48:32 +00004231/// \brief Note that the selector Sel occurs at the given offset
4232/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004233void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004234 unsigned ID = SelectorIDs[Sel];
4235 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004236 // Don't record offsets for selectors that are also available in a different
4237 // file.
4238 if (ID < FirstSelectorID)
4239 return;
4240 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004241}
4242
David Blaikie61137e12017-01-05 18:23:18 +00004243ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
4244 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4245 bool IncludeTimestamps)
David Blaikie95dd3622017-01-05 18:45:45 +00004246 : Stream(Stream), IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004247 for (const auto &Ext : Extensions) {
4248 if (auto Writer = Ext->createExtensionWriter(*this))
4249 ModuleFileExtensionWriters.push_back(std::move(Writer));
4250 }
4251}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004252
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004253ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004254 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004255}
4256
Richard Smithb3761912015-02-05 23:08:52 +00004257const LangOptions &ASTWriter::getLangOpts() const {
4258 assert(WritingAST && "can't determine lang opts when not writing AST");
4259 return Context->getLangOpts();
4260}
4261
Richard Smithe75ee0f2015-08-17 07:13:32 +00004262time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4263 return IncludeTimestamps ? E->getModificationTime() : 0;
4264}
4265
Adrian Prantladbd2b12015-09-22 23:26:31 +00004266uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4267 Module *WritingModule, StringRef isysroot,
4268 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004269 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004270
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004271 ASTHasCompilerErrors = hasErrors;
4272
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004273 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004274 Stream.Emit((unsigned)'C', 8);
4275 Stream.Emit((unsigned)'P', 8);
4276 Stream.Emit((unsigned)'C', 8);
4277 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004278
Chris Lattner28fa4e62009-04-26 22:26:21 +00004279 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004280
Douglas Gregoreda8e122011-08-09 15:13:55 +00004281 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004282 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004283 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004284 ASTFileSignature Signature =
4285 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004286 Context = nullptr;
4287 PP = nullptr;
4288 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004289 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004290
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004291 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004292 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004293}
4294
Douglas Gregora94a1542011-07-27 21:45:57 +00004295template<typename Vector>
4296static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4297 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004298 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4299 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004300 Writer.AddDeclRef(*I, Record);
4301 }
4302}
4303
Adrian Prantladbd2b12015-09-22 23:26:31 +00004304uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4305 const std::string &OutputFile,
4306 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004307 using namespace llvm;
4308
Craig Toppera13603a2014-05-22 05:54:18 +00004309 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004310
Douglas Gregorcf68c582011-12-01 22:20:10 +00004311 // Make sure that the AST reader knows to finalize itself.
4312 if (Chain)
4313 Chain->finalizeForWriting();
4314
Sebastian Redl143413f2010-07-12 22:02:52 +00004315 ASTContext &Context = SemaRef.Context;
4316 Preprocessor &PP = SemaRef.PP;
4317
Douglas Gregordab42432011-08-12 00:15:20 +00004318 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004319 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4320 if (D) {
4321 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4322 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004323 }
4324 };
4325 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4326 PREDEF_DECL_TRANSLATION_UNIT_ID);
4327 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4328 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4329 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4330 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4331 PREDEF_DECL_OBJC_PROTOCOL_ID);
4332 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4333 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4334 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4335 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4336 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004337 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004338 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4339 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004340 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004341 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4342 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004343 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4344 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004345 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4346 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004347 RegisterPredefDecl(Context.TypePackElementDecl,
4348 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004349
Chris Lattner0c797362009-09-08 18:19:27 +00004350 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004351 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004352 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004353 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004354 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004355
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004356 // Build a record containing all of the file scoped decls in this file.
4357 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004358 if (!isModule)
4359 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4360 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004361
Douglas Gregor851443c2011-08-12 01:39:19 +00004362 // Build a record containing all of the delegating constructors we still need
4363 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004364 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004365 if (!isModule)
4366 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004367
Douglas Gregor851443c2011-08-12 01:39:19 +00004368 // Write the set of weak, undeclared identifiers. We always write the
4369 // entire table, since later PCH files in a PCH chain are only interested in
4370 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004371 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004372 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4373 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4374 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4375 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4376 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4377 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4378 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004379 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004380
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004381 // Build a record containing all of the ext_vector declarations.
4382 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004383 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004384
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004385 // Build a record containing all of the VTable uses information.
4386 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004387 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004388 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4389 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4390 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4391 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4392 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004393 }
4394
Nico Weber72889432014-09-06 01:25:55 +00004395 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4396 RecordData UnusedLocalTypedefNameCandidates;
4397 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4398 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4399
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004400 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004401 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004402 for (const auto &I : SemaRef.PendingInstantiations) {
4403 AddDeclRef(I.first, PendingInstantiations);
4404 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004405 }
4406 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4407 "There are local ones at end of translation unit!");
4408
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004409 // Build a record containing some declaration references.
4410 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004411 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004412 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4413 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004414 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004415 }
4416
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004417 RecordData CUDASpecialDeclRefs;
4418 if (Context.getcudaConfigureCallDecl()) {
4419 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4420 }
4421
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004422 // Build a record containing all of the known namespaces.
4423 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004424 for (const auto &I : SemaRef.KnownNamespaces) {
4425 if (!I.second)
4426 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004427 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004428
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004429 // Build a record of all used, undefined objects that require definitions.
4430 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004431
4432 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004433 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004434 for (const auto &I : Undefined) {
4435 AddDeclRef(I.first, UndefinedButUsed);
4436 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004437 }
4438
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004439 // Build a record containing all delete-expressions that we would like to
4440 // analyze later in AST.
4441 RecordData DeleteExprsToAnalyze;
4442
4443 for (const auto &DeleteExprsInfo :
4444 SemaRef.getMismatchingDeleteExpressions()) {
4445 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4446 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4447 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4448 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4449 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4450 }
4451 }
4452
Douglas Gregor112b9072012-10-18 05:31:06 +00004453 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004454 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004455
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004456 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004457 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004458
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004459 // This is so that older clang versions, before the introduction
4460 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004461 {
4462 RecordData Record = {VERSION_MAJOR};
4463 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4464 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004465
Douglas Gregor851443c2011-08-12 01:39:19 +00004466 // Create a lexical update block containing all of the declarations in the
4467 // translation unit that do not come from other AST files.
4468 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004469 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4470 for (const auto *D : TU->noload_decls()) {
4471 if (!D->isFromASTFile()) {
4472 NewGlobalKindDeclPairs.push_back(D->getKind());
4473 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4474 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004475 }
4476
David Blaikieb44f0bf2017-01-04 22:36:43 +00004477 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004478 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4479 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004480 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004481 {
4482 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4483 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4484 bytes(NewGlobalKindDeclPairs));
4485 }
4486
Douglas Gregor851443c2011-08-12 01:39:19 +00004487 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004488 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004489 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4490 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004491 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004492 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004493 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004494
4495 // If we have any extern "C" names, write out a visible update for them.
4496 if (Context.ExternCContext)
4497 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004498
4499 // If the translation unit has an anonymous namespace, and we don't already
4500 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004501 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004502 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4503 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004504 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004505 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004506 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004507
Richard Smith5652c0f2014-03-21 01:48:23 +00004508 // Add update records for all mangling numbers and static local numbers.
4509 // These aren't really update records, but this is a convenient way of
4510 // tagging this rare extra data onto the declarations.
4511 for (const auto &Number : Context.MangleNumbers)
4512 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004513 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4514 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004515 for (const auto &Number : Context.StaticLocalNumbers)
4516 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004517 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4518 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004519
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004520 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004521 // an AST file, are registered for serialization. Likewise for template
4522 // specializations added to imported templates.
4523 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004524 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004525 }
4526
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004527 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004528 // serialization, if we're storing decls with identifiers.
4529 if (!WritingModule || !getLangOpts().CPlusPlus) {
4530 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004531 for (const auto &ID : PP.getIdentifierTable()) {
4532 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004533 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4534 IIs.push_back(II);
4535 }
4536 // Sort the identifiers to visit based on their name.
4537 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4538 for (const IdentifierInfo *II : IIs) {
4539 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4540 DEnd = SemaRef.IdResolver.end();
4541 D != DEnd; ++D) {
4542 GetDeclRef(*D);
4543 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004544 }
4545 }
4546
Manman Rena0f31a02016-04-29 19:04:05 +00004547 // For method pool in the module, if it contains an entry for a selector,
4548 // the entry should be complete, containing everything introduced by that
4549 // module and all modules it imports. It's possible that the entry is out of
4550 // date, so we need to pull in the new content here.
4551
4552 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4553 // safe, we copy all selectors out.
4554 llvm::SmallVector<Selector, 256> AllSelectors;
4555 for (auto &SelectorAndID : SelectorIDs)
4556 AllSelectors.push_back(SelectorAndID.first);
4557 for (auto &Selector : AllSelectors)
4558 SemaRef.updateOutOfDateSelector(Selector);
4559
Douglas Gregor5204bde2011-08-02 16:26:37 +00004560 // Form the record of special types.
4561 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004562 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004563 AddTypeRef(Context.getFILEType(), SpecialTypes);
4564 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4565 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4566 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4567 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004568 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004569 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004570
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004571 if (Chain) {
4572 // Write the mapping information describing our module dependencies and how
4573 // each of those modules were mapped into our own offset/ID space, so that
4574 // the reader can build the appropriate mapping to its own offset/ID space.
4575 // The map consists solely of a blob with the following format:
4576 // *(module-name-len:i16 module-name:len*i8
4577 // source-location-offset:i32
4578 // identifier-id:i32
4579 // preprocessed-entity-id:i32
4580 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004581 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004582 // selector-id:i32
4583 // declaration-id:i32
4584 // c++-base-specifiers-id:i32
4585 // type-id:i32)
4586 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004587 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004588 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004590 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004591 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004592 {
4593 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004594 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004595 using namespace llvm::support;
4596 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004597 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004598 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004599 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004600
Ben Langmuir785180e2014-10-20 16:27:30 +00004601 // Note: if a base ID was uint max, it would not be possible to load
4602 // another module after it or have more than one entity inside it.
4603 uint32_t None = std::numeric_limits<uint32_t>::max();
4604
4605 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4606 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4607 if (ShouldWrite)
4608 LE.write<uint32_t>(BaseID);
4609 else
4610 LE.write<uint32_t>(None);
4611 };
4612
Ben Langmuirfe971d92014-08-16 04:54:18 +00004613 // These values should be unique within a chain, since they will be read
4614 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004615 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4616 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4617 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4618 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4619 M->NumPreprocessedEntities);
4620 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4621 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4622 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4623 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004624 }
4625 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004626 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004627 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4628 Buffer.data(), Buffer.size());
4629 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004630
Richard Smithb9eab6d2014-03-20 19:44:17 +00004631 RecordData DeclUpdatesOffsetsRecord;
4632
Richard Smith59442b42014-03-20 20:07:19 +00004633 // Keep writing types, declarations, and declaration update records
4634 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004635 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4636 WriteTypeAbbrevs();
4637 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004638 do {
4639 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4640 while (!DeclTypesToEmit.empty()) {
4641 DeclOrType DOT = DeclTypesToEmit.front();
4642 DeclTypesToEmit.pop();
4643 if (DOT.isType())
4644 WriteType(DOT.getType());
4645 else
4646 WriteDecl(Context, DOT.getDecl());
4647 }
4648 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004649 Stream.ExitBlock();
4650
Richard Smithb9eab6d2014-03-20 19:44:17 +00004651 DoneWritingDeclsAndTypes = true;
4652
4653 // These things can only be done once we've written out decls and types.
4654 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004655 if (!DeclUpdatesOffsetsRecord.empty())
4656 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004657 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004658 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004659 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004660 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004661 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004662 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004663 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004664 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004665 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004666 WriteFPPragmaOptions(SemaRef.getFPOptions());
4667 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004668 WriteOpenCLExtensionTypes(SemaRef);
4669 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004670 WriteCUDAPragmas(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004671 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004672
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004673 // If we're emitting a module, write out the submodule information.
4674 if (WritingModule)
4675 WriteSubmodules(WritingModule);
4676
Douglas Gregor5204bde2011-08-02 16:26:37 +00004677 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4678
Douglas Gregord4df8652009-04-22 22:02:47 +00004679 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004680 if (!EagerlyDeserializedDecls.empty())
4681 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004682
4683 // Write the record containing tentative definitions.
4684 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004685 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004686
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004687 // Write the record containing unused file scoped decls.
4688 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004689 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004690
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004691 // Write the record containing weak undeclared identifiers.
4692 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004693 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004694 WeakUndeclaredIdentifiers);
4695
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004696 // Write the record containing ext_vector type names.
4697 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004698 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004699
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004700 // Write the record containing VTable uses information.
4701 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004702 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004703
Nico Weber72889432014-09-06 01:25:55 +00004704 // Write the record containing potentially unused local typedefs.
4705 if (!UnusedLocalTypedefNameCandidates.empty())
4706 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4707 UnusedLocalTypedefNameCandidates);
4708
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004709 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004710 if (!PendingInstantiations.empty())
4711 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004712
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004713 // Write the record containing declaration references of Sema.
4714 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004715 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004716
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004717 // Write the record containing CUDA-specific declaration references.
4718 if (!CUDASpecialDeclRefs.empty())
4719 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004720
4721 // Write the delegating constructors.
4722 if (!DelegatingCtorDecls.empty())
4723 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004724
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004725 // Write the known namespaces.
4726 if (!KnownNamespaces.empty())
4727 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004728
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004729 // Write the undefined internal functions and variables, and inline functions.
4730 if (!UndefinedButUsed.empty())
4731 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004732
4733 if (!DeleteExprsToAnalyze.empty())
4734 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4735
Douglas Gregor851443c2011-08-12 01:39:19 +00004736 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004737 for (auto *DC : UpdatedDeclContexts)
4738 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004739
Douglas Gregor959bb062011-12-03 01:15:29 +00004740 if (!WritingModule) {
4741 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004742 struct ModuleInfo {
4743 uint64_t ID;
4744 Module *M;
4745 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4746 };
Richard Smith56be7542014-03-21 00:33:59 +00004747 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004748 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004749 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004750 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4751 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004752 }
Richard Smith56be7542014-03-21 00:33:59 +00004753
4754 if (!Imports.empty()) {
4755 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4756 return A.ID < B.ID;
4757 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004758 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4759 return A.ID == B.ID;
4760 };
Richard Smith56be7542014-03-21 00:33:59 +00004761
4762 // Sort and deduplicate module IDs.
4763 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004764 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004765 Imports.end());
4766
4767 RecordData ImportedModules;
4768 for (const auto &Import : Imports) {
4769 ImportedModules.push_back(Import.ID);
4770 // FIXME: If the module has macros imported then later has declarations
4771 // imported, this location won't be the right one as a location for the
4772 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004773 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004774 }
4775
Douglas Gregor959bb062011-12-03 01:15:29 +00004776 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4777 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004778 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004779
Douglas Gregor404cdde2012-01-27 01:47:08 +00004780 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004781 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004782 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004783 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004784 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004785 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004786
Douglas Gregor08f01292009-04-17 22:13:46 +00004787 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004788 RecordData::value_type Record[] = {
4789 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004790 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004791 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004792
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004793 // Write the module file extension blocks.
4794 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004795 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004796
Adrian Prantladbd2b12015-09-22 23:26:31 +00004797 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004798}
4799
Richard Smithb9eab6d2014-03-20 19:44:17 +00004800void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004801 if (DeclUpdates.empty())
4802 return;
4803
Richard Smith59442b42014-03-20 20:07:19 +00004804 DeclUpdateMap LocalUpdates;
4805 LocalUpdates.swap(DeclUpdates);
4806
Richard Smith6ef42932014-03-20 21:02:00 +00004807 for (auto &DeclUpdate : LocalUpdates) {
4808 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004809
Richard Smithd28ac5b2014-03-22 23:33:22 +00004810 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004811 RecordData RecordData;
4812 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004813 for (auto &Update : DeclUpdate.second) {
4814 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4815
Richard Smith69c82bf2016-04-01 22:52:03 +00004816 // An updated body is emitted last, so that the reader doesn't need
4817 // to skip over the lazy body to reach statements for other records.
4818 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4819 HasUpdatedBody = true;
4820 else
4821 Record.push_back(Kind);
4822
Richard Smith6ef42932014-03-20 21:02:00 +00004823 switch (Kind) {
4824 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4825 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4826 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004827 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004828 Record.push_back(GetDeclRef(Update.getDecl()));
4829 break;
4830
Richard Smith4d235792014-08-07 18:53:08 +00004831 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004832 break;
4833
Richard Smith4d235792014-08-07 18:53:08 +00004834 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004835 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004836 break;
4837
John McCall32791cc2016-01-06 22:34:54 +00004838 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004839 Record.AddStmt(const_cast<Expr *>(
4840 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004841 break;
4842
Richard Smith4b054b22016-08-24 21:25:37 +00004843 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4844 Record.AddStmt(
4845 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4846 break;
4847
Richard Smithcd45dbc2014-04-19 03:48:30 +00004848 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4849 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004850 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004851 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004852 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004853 *Context, const_cast<CXXRecordDecl *>(RD)));
4854
4855 // This state is sometimes updated by template instantiation, when we
4856 // switch from the specialization referring to the template declaration
4857 // to it referring to the template definition.
4858 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4859 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004860 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004861 } else {
4862 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4863 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004864 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004865
4866 // The instantiation might have been resolved to a partial
4867 // specialization. If so, record which one.
4868 auto From = Spec->getInstantiatedFrom();
4869 if (auto PartialSpec =
4870 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4871 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004872 Record.AddDeclRef(PartialSpec);
4873 Record.AddTemplateArgumentList(
4874 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004875 } else {
4876 Record.push_back(false);
4877 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004878 }
4879 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004880 Record.AddSourceLocation(RD->getLocation());
4881 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004882 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004883
4884 // Instantiation may change attributes; write them all out afresh.
4885 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004886 if (D->hasAttrs())
4887 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004888
4889 // FIXME: Ensure we don't get here for explicit instantiations.
4890 break;
4891 }
4892
Richard Smithf8134002015-03-10 01:41:22 +00004893 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004894 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004895 break;
4896
Richard Smith564417a2014-03-20 21:47:22 +00004897 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4898 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004899 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4900 Record);
4901 break;
4902
Richard Smith6ef42932014-03-20 21:02:00 +00004903 case UPD_CXX_DEDUCED_RETURN_TYPE:
4904 Record.push_back(GetOrCreateTypeID(Update.getType()));
4905 break;
4906
4907 case UPD_DECL_MARKED_USED:
4908 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004909
4910 case UPD_MANGLING_NUMBER:
4911 case UPD_STATIC_LOCAL_NUMBER:
4912 Record.push_back(Update.getNumber());
4913 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004914
Alexey Bataev97720002014-11-11 04:05:39 +00004915 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004916 Record.AddSourceRange(
4917 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004918 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004919
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004920 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4921 Record.AddSourceRange(
4922 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4923 break;
4924
Richard Smith65ebb4a2015-03-26 04:09:53 +00004925 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004926 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004927 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004928
4929 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004930 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004931 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004932 }
4933 }
4934
Richard Smithd28ac5b2014-03-22 23:33:22 +00004935 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004936 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004937 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004938 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00004939 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00004940 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004941 }
4942
Richard Smithcd45dbc2014-04-19 03:48:30 +00004943 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00004944 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004945 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004946}
4947
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004948void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00004949 uint32_t Raw = Loc.getRawEncoding();
4950 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004951}
4952
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004953void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004954 AddSourceLocation(Range.getBegin(), Record);
4955 AddSourceLocation(Range.getEnd(), Record);
4956}
4957
Richard Smithf144b542016-04-08 21:54:32 +00004958void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
4959 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004960 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00004961 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004962}
4963
Richard Smithf144b542016-04-08 21:54:32 +00004964void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
4965 Record->push_back(Value.isUnsigned());
4966 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00004967}
4968
Richard Smithf144b542016-04-08 21:54:32 +00004969void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
4970 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00004971}
4972
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004973void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004974 Record.push_back(getIdentifierRef(II));
4975}
4976
Sebastian Redl539c5062010-08-18 23:57:32 +00004977IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004978 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004979 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004980
Sebastian Redl539c5062010-08-18 23:57:32 +00004981 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004982 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004983 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004984 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004985}
4986
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004987MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004988 // Don't emit builtin macros like __LINE__ to the AST file unless they
4989 // have been redefined by the header (in which case they are not
4990 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004991 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004992 return 0;
4993
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004994 MacroID &ID = MacroIDs[MI];
4995 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004996 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004997 MacroInfoToEmitData Info = { Name, MI, ID };
4998 MacroInfosToEmit.push_back(Info);
4999 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005000 return ID;
5001}
5002
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005003MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005004 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005005 return 0;
5006
5007 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5008 return MacroIDs[MI];
5009}
5010
5011uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005012 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005013}
5014
Richard Smithe64e7452016-04-18 21:54:58 +00005015void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5016 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005017}
5018
Sebastian Redl539c5062010-08-18 23:57:32 +00005019SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005020 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005021 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005022 }
5023
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005024 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005025 if (SID == 0 && Chain) {
5026 // This might trigger a ReadSelector callback, which will set the ID for
5027 // this selector.
5028 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005029 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005030 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005031 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005032 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005033 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005034 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005035 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005036}
5037
Richard Smithe64e7452016-04-18 21:54:58 +00005038void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5039 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005040}
5041
Richard Smith290d8012016-04-06 17:06:00 +00005042void ASTRecordWriter::AddTemplateArgumentLocInfo(
5043 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005044 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005045 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005046 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005047 break;
5048 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005049 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005050 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005051 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005052 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5053 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005054 break;
5055 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005056 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5057 AddSourceLocation(Arg.getTemplateNameLoc());
5058 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005059 break;
John McCall0ad16662009-10-29 08:12:44 +00005060 case TemplateArgument::Null:
5061 case TemplateArgument::Integral:
5062 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005063 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005064 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005065 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005066 break;
5067 }
5068}
5069
Richard Smith290d8012016-04-06 17:06:00 +00005070void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5071 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005072
5073 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5074 bool InfoHasSameExpr
5075 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005076 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005077 if (InfoHasSameExpr)
5078 return; // Avoid storing the same expr twice.
5079 }
Richard Smith290d8012016-04-06 17:06:00 +00005080 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005081}
5082
Richard Smith290d8012016-04-06 17:06:00 +00005083void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005084 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005085 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005086 return;
5087 }
5088
Richard Smith290d8012016-04-06 17:06:00 +00005089 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005090}
5091
Richard Smith290d8012016-04-06 17:06:00 +00005092void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5093 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005094
Richard Smith290d8012016-04-06 17:06:00 +00005095 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005096 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005097 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005098}
5099
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005100void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005101 Record.push_back(GetOrCreateTypeID(T));
5102}
5103
Richard Smith2095ffe2015-03-16 20:11:03 +00005104TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005105 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005106 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5107 if (T.isNull())
5108 return TypeIdx();
5109 assert(!T.getLocalFastQualifiers());
5110
5111 TypeIdx &Idx = TypeIdxs[T];
5112 if (Idx.getIndex() == 0) {
5113 if (DoneWritingDeclsAndTypes) {
5114 assert(0 && "New type seen after serializing all the types to emit!");
5115 return TypeIdx();
5116 }
5117
5118 // We haven't seen this type before. Assign it a new ID and put it
5119 // into the queue of types to emit.
5120 Idx = TypeIdx(NextTypeID++);
5121 DeclTypesToEmit.push(T);
5122 }
5123 return Idx;
5124 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005125}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005126
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005127TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005128 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005129 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5130 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005131 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005132 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005133
Richard Smith2095ffe2015-03-16 20:11:03 +00005134 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5135 assert(I != TypeIdxs.end() && "Type not emitted!");
5136 return I->second;
5137 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005138}
5139
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005140void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005141 Record.push_back(GetDeclRef(D));
5142}
5143
Sebastian Redl539c5062010-08-18 23:57:32 +00005144DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005145 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005146
5147 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005148 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005149 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005150
5151 // If D comes from an AST file, its declaration ID is already known and
5152 // fixed.
5153 if (D->isFromASTFile())
5154 return D->getGlobalID();
5155
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005156 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005157 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005158 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005159 if (DoneWritingDeclsAndTypes) {
5160 assert(0 && "New decl seen after serializing all the decls to emit!");
5161 return 0;
5162 }
5163
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005164 // We haven't seen this declaration before. Give it a new ID and
5165 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005166 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005167 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005168 }
5169
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005170 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005171}
5172
Sebastian Redl539c5062010-08-18 23:57:32 +00005173DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005174 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005175 return 0;
5176
Douglas Gregorb3163e52012-01-05 22:33:30 +00005177 // If D comes from an AST file, its declaration ID is already known and
5178 // fixed.
5179 if (D->isFromASTFile())
5180 return D->getGlobalID();
5181
Douglas Gregore84a9da2009-04-20 20:36:09 +00005182 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5183 return DeclIDs[D];
5184}
5185
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005186void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005187 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005188 assert(D);
5189
5190 SourceLocation Loc = D->getLocation();
5191 if (Loc.isInvalid())
5192 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005193
5194 // We only keep track of the file-level declarations of each file.
5195 if (!D->getLexicalDeclContext()->isFileContext())
5196 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005197 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5198 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005199 if (isa<ParmVarDecl>(D))
5200 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005201
5202 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005203 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005204 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005205 FileID FID;
5206 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005207 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005208 if (FID.isInvalid())
5209 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005210 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005211
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005212 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005213 if (!Info)
5214 Info = new DeclIDInFileInfo();
5215
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005216 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005217 LocDeclIDsTy &Decls = Info->DeclIDs;
5218
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005219 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005220 Decls.push_back(LocDecl);
5221 return;
5222 }
5223
Benjamin Kramer45025c02013-08-24 13:22:59 +00005224 LocDeclIDsTy::iterator I =
5225 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005226
5227 Decls.insert(I, LocDecl);
5228}
5229
Richard Smithe64e7452016-04-18 21:54:58 +00005230void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005231 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005232 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005233 switch (Name.getNameKind()) {
5234 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005235 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005236 break;
5237
5238 case DeclarationName::ObjCZeroArgSelector:
5239 case DeclarationName::ObjCOneArgSelector:
5240 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005241 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005242 break;
5243
5244 case DeclarationName::CXXConstructorName:
5245 case DeclarationName::CXXDestructorName:
5246 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005247 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005248 break;
5249
5250 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005251 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005252 break;
5253
Alexis Hunt3d221f22009-11-29 07:34:05 +00005254 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005255 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005256 break;
5257
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005258 case DeclarationName::CXXUsingDirective:
5259 // No extra data to emit
5260 break;
5261 }
5262}
Chris Lattnerca025db2010-05-07 21:43:38 +00005263
Richard Smithd08aeb62014-08-28 01:33:39 +00005264unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5265 assert(needsAnonymousDeclarationNumber(D) &&
5266 "expected an anonymous declaration");
5267
5268 // Number the anonymous declarations within this context, if we've not
5269 // already done so.
5270 auto It = AnonymousDeclarationNumbers.find(D);
5271 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005272 auto *DC = D->getLexicalDeclContext();
5273 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5274 AnonymousDeclarationNumbers[ND] = Number;
5275 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005276
5277 It = AnonymousDeclarationNumbers.find(D);
5278 assert(It != AnonymousDeclarationNumbers.end() &&
5279 "declaration not found within its lexical context");
5280 }
5281
5282 return It->second;
5283}
5284
Richard Smith290d8012016-04-06 17:06:00 +00005285void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5286 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005287 switch (Name.getNameKind()) {
5288 case DeclarationName::CXXConstructorName:
5289 case DeclarationName::CXXDestructorName:
5290 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005291 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005292 break;
5293
5294 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005295 AddSourceLocation(SourceLocation::getFromRawEncoding(
5296 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005297 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005298 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005299 break;
5300
5301 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005302 AddSourceLocation(SourceLocation::getFromRawEncoding(
5303 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005304 break;
5305
5306 case DeclarationName::Identifier:
5307 case DeclarationName::ObjCZeroArgSelector:
5308 case DeclarationName::ObjCOneArgSelector:
5309 case DeclarationName::ObjCMultiArgSelector:
5310 case DeclarationName::CXXUsingDirective:
5311 break;
5312 }
5313}
5314
Richard Smith290d8012016-04-06 17:06:00 +00005315void ASTRecordWriter::AddDeclarationNameInfo(
5316 const DeclarationNameInfo &NameInfo) {
5317 AddDeclarationName(NameInfo.getName());
5318 AddSourceLocation(NameInfo.getLoc());
5319 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005320}
5321
Richard Smith290d8012016-04-06 17:06:00 +00005322void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5323 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5324 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005325 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005326 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005327}
5328
Richard Smithe64e7452016-04-18 21:54:58 +00005329void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005330 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005331 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005332 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005333
5334 // Push each of the NNS's onto a stack for serialization in reverse order.
5335 while (NNS) {
5336 NestedNames.push_back(NNS);
5337 NNS = NNS->getPrefix();
5338 }
5339
Richard Smithe64e7452016-04-18 21:54:58 +00005340 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005341 while(!NestedNames.empty()) {
5342 NNS = NestedNames.pop_back_val();
5343 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005344 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005345 switch (Kind) {
5346 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005347 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005348 break;
5349
5350 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005351 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005352 break;
5353
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005354 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005355 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005356 break;
5357
Chris Lattnerca025db2010-05-07 21:43:38 +00005358 case NestedNameSpecifier::TypeSpec:
5359 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005360 AddTypeRef(QualType(NNS->getAsType(), 0));
5361 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005362 break;
5363
5364 case NestedNameSpecifier::Global:
5365 // Don't need to write an associated value.
5366 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005367
5368 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005369 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005370 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005371 }
5372 }
5373}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005374
Richard Smith290d8012016-04-06 17:06:00 +00005375void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005376 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005377 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005378 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005379
5380 // Push each of the nested-name-specifiers's onto a stack for
5381 // serialization in reverse order.
5382 while (NNS) {
5383 NestedNames.push_back(NNS);
5384 NNS = NNS.getPrefix();
5385 }
5386
Richard Smith290d8012016-04-06 17:06:00 +00005387 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005388 while(!NestedNames.empty()) {
5389 NNS = NestedNames.pop_back_val();
5390 NestedNameSpecifier::SpecifierKind Kind
5391 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005392 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005393 switch (Kind) {
5394 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005395 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5396 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005397 break;
5398
5399 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005400 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5401 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005402 break;
5403
5404 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005405 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5406 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005407 break;
5408
5409 case NestedNameSpecifier::TypeSpec:
5410 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005411 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5412 AddTypeLoc(NNS.getTypeLoc());
5413 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005414 break;
5415
5416 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005417 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005418 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005419
5420 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005421 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5422 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005423 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005424 }
5425 }
5426}
5427
Richard Smith290d8012016-04-06 17:06:00 +00005428void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005429 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005430 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005431 switch (Kind) {
5432 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005433 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005434 break;
5435
5436 case TemplateName::OverloadedTemplate: {
5437 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005438 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005439 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005440 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005441 break;
5442 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005443
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005444 case TemplateName::QualifiedTemplate: {
5445 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005446 AddNestedNameSpecifier(QualT->getQualifier());
5447 Record->push_back(QualT->hasTemplateKeyword());
5448 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005449 break;
5450 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005451
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005452 case TemplateName::DependentTemplate: {
5453 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005454 AddNestedNameSpecifier(DepT->getQualifier());
5455 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005456 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005457 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005458 else
Richard Smith290d8012016-04-06 17:06:00 +00005459 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005460 break;
5461 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005462
5463 case TemplateName::SubstTemplateTemplateParm: {
5464 SubstTemplateTemplateParmStorage *subst
5465 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005466 AddDeclRef(subst->getParameter());
5467 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005468 break;
5469 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005470
5471 case TemplateName::SubstTemplateTemplateParmPack: {
5472 SubstTemplateTemplateParmPackStorage *SubstPack
5473 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005474 AddDeclRef(SubstPack->getParameterPack());
5475 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005476 break;
5477 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005478 }
5479}
5480
Richard Smith290d8012016-04-06 17:06:00 +00005481void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5482 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005483 switch (Arg.getKind()) {
5484 case TemplateArgument::Null:
5485 break;
5486 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005487 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005488 break;
5489 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005490 AddDeclRef(Arg.getAsDecl());
5491 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005492 break;
5493 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005494 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005495 break;
5496 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005497 AddAPSInt(Arg.getAsIntegral());
5498 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005499 break;
5500 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005501 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005502 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005503 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005504 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005505 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005506 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005507 else
Richard Smith290d8012016-04-06 17:06:00 +00005508 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005509 break;
5510 case TemplateArgument::Expression:
5511 AddStmt(Arg.getAsExpr());
5512 break;
5513 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005514 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005515 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005516 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005517 break;
5518 }
5519}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005520
Richard Smithe64e7452016-04-18 21:54:58 +00005521void ASTRecordWriter::AddTemplateParameterList(
5522 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005523 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005524 AddSourceLocation(TemplateParams->getTemplateLoc());
5525 AddSourceLocation(TemplateParams->getLAngleLoc());
5526 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005527 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005528 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005529 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005530 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005531}
5532
5533/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005534void ASTRecordWriter::AddTemplateArgumentList(
5535 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005536 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005537 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005538 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005539 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005540}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005541
Richard Smith290d8012016-04-06 17:06:00 +00005542void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5543 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005544 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005545 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5546 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5547 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005548 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005549 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005550 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005551}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005552
Richard Smithe64e7452016-04-18 21:54:58 +00005553void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5554 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005555 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005556 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005557 AddDeclRef(I.getDecl());
5558 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005559 }
5560}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005561
Richard Smith290d8012016-04-06 17:06:00 +00005562// FIXME: Move this out of the main ASTRecordWriter interface.
5563void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5564 Record->push_back(Base.isVirtual());
5565 Record->push_back(Base.isBaseOfClass());
5566 Record->push_back(Base.getAccessSpecifierAsWritten());
5567 Record->push_back(Base.getInheritConstructors());
5568 AddTypeSourceInfo(Base.getTypeSourceInfo());
5569 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005570 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005571 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005572}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005573
Richard Smith645d2cf2016-04-14 00:29:55 +00005574static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5575 ArrayRef<CXXBaseSpecifier> Bases) {
5576 ASTWriter::RecordData Record;
5577 ASTRecordWriter Writer(W, Record);
5578 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005579
Richard Smith645d2cf2016-04-14 00:29:55 +00005580 for (auto &Base : Bases)
5581 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005582
Richard Smith645d2cf2016-04-14 00:29:55 +00005583 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005584}
5585
Richard Smith290d8012016-04-06 17:06:00 +00005586// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005587void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5588 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5589}
5590
Richard Smithaa165cf2016-04-13 21:57:08 +00005591static uint64_t
5592EmitCXXCtorInitializers(ASTWriter &W,
5593 ArrayRef<CXXCtorInitializer *> CtorInits) {
5594 ASTWriter::RecordData Record;
5595 ASTRecordWriter Writer(W, Record);
5596 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005597
Richard Smithaa165cf2016-04-13 21:57:08 +00005598 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005599 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005600 Writer.push_back(CTOR_INITIALIZER_BASE);
5601 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5602 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005603 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005604 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5605 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005606 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005607 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5608 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005609 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005610 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5611 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005612 }
Francois Pichetd583da02010-12-04 09:14:42 +00005613
Richard Smithaa165cf2016-04-13 21:57:08 +00005614 Writer.AddSourceLocation(Init->getMemberLocation());
5615 Writer.AddStmt(Init->getInit());
5616 Writer.AddSourceLocation(Init->getLParenLoc());
5617 Writer.AddSourceLocation(Init->getRParenLoc());
5618 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005619 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005620 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005621 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005622
5623 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005624}
5625
Richard Smithaa165cf2016-04-13 21:57:08 +00005626// FIXME: Move this out of the main ASTRecordWriter interface.
5627void ASTRecordWriter::AddCXXCtorInitializers(
5628 ArrayRef<CXXCtorInitializer *> CtorInits) {
5629 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005630}
5631
Richard Smith290d8012016-04-06 17:06:00 +00005632void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005633 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005634 Record->push_back(Data.IsLambda);
5635 Record->push_back(Data.UserDeclaredConstructor);
5636 Record->push_back(Data.UserDeclaredSpecialMembers);
5637 Record->push_back(Data.Aggregate);
5638 Record->push_back(Data.PlainOldData);
5639 Record->push_back(Data.Empty);
5640 Record->push_back(Data.Polymorphic);
5641 Record->push_back(Data.Abstract);
5642 Record->push_back(Data.IsStandardLayout);
5643 Record->push_back(Data.HasNoNonEmptyBases);
5644 Record->push_back(Data.HasPrivateFields);
5645 Record->push_back(Data.HasProtectedFields);
5646 Record->push_back(Data.HasPublicFields);
5647 Record->push_back(Data.HasMutableFields);
5648 Record->push_back(Data.HasVariantMembers);
5649 Record->push_back(Data.HasOnlyCMembers);
5650 Record->push_back(Data.HasInClassInitializer);
5651 Record->push_back(Data.HasUninitializedReferenceMember);
5652 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005653 Record->push_back(Data.HasInheritedConstructor);
5654 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005655 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5656 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5657 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5658 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5659 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5660 Record->push_back(Data.DefaultedDestructorIsDeleted);
5661 Record->push_back(Data.HasTrivialSpecialMembers);
5662 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5663 Record->push_back(Data.HasIrrelevantDestructor);
5664 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5665 Record->push_back(Data.HasDefaultedDefaultConstructor);
5666 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5667 Record->push_back(Data.HasConstexprDefaultConstructor);
5668 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5669 Record->push_back(Data.ComputedVisibleConversions);
5670 Record->push_back(Data.UserProvidedDefaultConstructor);
5671 Record->push_back(Data.DeclaredSpecialMembers);
5672 Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
5673 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5674 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5675 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005676 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005677
Richard Smith290d8012016-04-06 17:06:00 +00005678 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005679 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005680 AddCXXBaseSpecifiers(Data.bases());
5681
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005682 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005683 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005684 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005685 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005686
Richard Smith290d8012016-04-06 17:06:00 +00005687 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5688 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005689 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005690 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005691
5692 // Add lambda-specific data.
5693 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005694 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005695 Record->push_back(Lambda.Dependent);
5696 Record->push_back(Lambda.IsGenericLambda);
5697 Record->push_back(Lambda.CaptureDefault);
5698 Record->push_back(Lambda.NumCaptures);
5699 Record->push_back(Lambda.NumExplicitCaptures);
5700 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005701 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005702 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005703 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005704 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005705 AddSourceLocation(Capture.getLocation());
5706 Record->push_back(Capture.isImplicit());
5707 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005708 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005709 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005710 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005711 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005712 break;
5713 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005714 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005715 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005716 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005717 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005718 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005719 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005720 break;
5721 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005722 }
5723 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005724}
5725
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005726void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005727 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005728 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005729 assert(FirstDeclID == NextDeclID &&
5730 FirstTypeID == NextTypeID &&
5731 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005732 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005733 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005734 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005735 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005736
Sebastian Redl07a89a82010-07-30 00:29:29 +00005737 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005738
Richard Smith7f330cd2015-03-18 01:42:29 +00005739 // Note, this will get called multiple times, once one the reader starts up
5740 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005741 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5742 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5743 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005744 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005745 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005746 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005747 NextDeclID = FirstDeclID;
5748 NextTypeID = FirstTypeID;
5749 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005750 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005751 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005752 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005753}
5754
Sebastian Redl539c5062010-08-18 23:57:32 +00005755void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005756 // Always keep the highest ID. See \p TypeRead() for more information.
5757 IdentID &StoredID = IdentifierIDs[II];
5758 if (ID > StoredID)
5759 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005760}
5761
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005762void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005763 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005764 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005765 if (ID > StoredID)
5766 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005767}
5768
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005769void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005770 // Always take the highest-numbered type index. This copes with an interesting
5771 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005772 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005773 // keep the higher-numbered entry so that we can properly write it out to
5774 // the AST file.
5775 TypeIdx &StoredIdx = TypeIdxs[T];
5776 if (Idx.getIndex() >= StoredIdx.getIndex())
5777 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005778}
5779
Sebastian Redl539c5062010-08-18 23:57:32 +00005780void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005781 // Always keep the highest ID. See \p TypeRead() for more information.
5782 SelectorID &StoredID = SelectorIDs[S];
5783 if (ID > StoredID)
5784 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005785}
Douglas Gregor91096292010-10-02 19:29:26 +00005786
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005787void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005788 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005789 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005790 MacroDefinitions[MD] = ID;
5791}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005792
Douglas Gregore37a85a2011-12-02 17:30:13 +00005793void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5794 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5795 SubmoduleIDs[Mod] = ID;
5796}
5797
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005798void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005799 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005800 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005801 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005802 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005803 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005804 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005805 // A forward reference was mutated into a definition. Rewrite it.
5806 // FIXME: This happens during template instantiation, should we
5807 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005808 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5809 "completed a tag from another module but not by instantiation?");
5810 DeclUpdates[RD].push_back(
5811 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005812 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005813 }
5814}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005815
Richard Smithf983f7f2015-10-13 00:23:25 +00005816static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5817 if (D->isFromASTFile())
5818 return true;
5819
Richard Smithf983f7f2015-10-13 00:23:25 +00005820 // The predefined __va_list_tag struct is imported if we imported any decls.
5821 // FIXME: This is a gross hack.
5822 return D == D->getASTContext().getVaListTagDecl();
5823}
5824
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005825void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005826 if (Chain && Chain->isProcessingUpdateRecords()) return;
5827 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005828 "Should not add lookup results to non-lookup contexts!");
5829
Vassil Vassilev632eac32016-03-16 11:17:04 +00005830 // TU is handled elsewhere.
5831 if (isa<TranslationUnitDecl>(DC))
5832 return;
5833
5834 // Namespaces are handled elsewhere, except for template instantiations of
5835 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5836 // local instantiations are added to an imported context. Only happens when
5837 // adding ADL lookup candidates, for example templated friends.
5838 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5839 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005840 return;
5841
Richard Smithf983f7f2015-10-13 00:23:25 +00005842 // We're only interested in cases where a local declaration is added to an
5843 // imported context.
5844 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5845 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005846
Richard Smith0f4e2c42015-08-06 04:23:48 +00005847 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005848 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005849 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005850 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5851 // We're adding a visible declaration to a predefined decl context. Ensure
5852 // that we write out all of its lookup results so we don't get a nasty
5853 // surprise when we try to emit its lookup table.
5854 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005855 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005856 }
Richard Smithc26d9742016-10-06 20:30:51 +00005857 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005858}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005859
5860void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005861 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005862 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005863
5864 // We're only interested in cases where a local declaration is added to an
5865 // imported context.
5866 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5867 return;
5868
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005869 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005870 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005871
5872 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005873 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005874 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005875 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005876}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005877
Richard Smith564417a2014-03-20 21:47:22 +00005878void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005879 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005880 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5881 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005882 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005883 // If we don't already know the exception specification for this redecl
5884 // chain, add an update record for it.
5885 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5886 ->getType()
5887 ->castAs<FunctionProtoType>()
5888 ->getExceptionSpecType()))
5889 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5890 });
Richard Smith564417a2014-03-20 21:47:22 +00005891}
5892
Richard Smith1fa5d642013-05-11 05:45:24 +00005893void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005894 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005895 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005896 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005897 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005898 DeclUpdates[D].push_back(
5899 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5900 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005901}
5902
Richard Smithf8134002015-03-10 01:41:22 +00005903void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5904 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005905 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005906 assert(!WritingAST && "Already writing the AST!");
5907 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005908 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005909 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005910 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5911 });
Richard Smithf8134002015-03-10 01:41:22 +00005912}
5913
Sebastian Redlab238a72011-04-24 16:28:06 +00005914void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005915 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005916 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005917 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005918 return; // Declaration not imported from PCH.
5919
Richard Smith4d235792014-08-07 18:53:08 +00005920 // Implicit function decl from a PCH was defined.
5921 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005922}
5923
Richard Smithd28ac5b2014-03-22 23:33:22 +00005924void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005925 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005926 assert(!WritingAST && "Already writing the AST!");
5927 if (!D->isFromASTFile())
5928 return;
5929
Richard Smith9e2341d2015-03-23 03:25:59 +00005930 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005931}
5932
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005933void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005934 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005935 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005936 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005937 return;
5938
5939 // Since the actual instantiation is delayed, this really means that we need
5940 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005941 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005942 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5943 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005944}
5945
John McCall32791cc2016-01-06 22:34:54 +00005946void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005947 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00005948 assert(!WritingAST && "Already writing the AST!");
5949 if (!D->isFromASTFile())
5950 return;
5951
5952 DeclUpdates[D].push_back(
5953 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5954}
5955
Richard Smith4b054b22016-08-24 21:25:37 +00005956void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
5957 assert(!WritingAST && "Already writing the AST!");
5958 if (!D->isFromASTFile())
5959 return;
5960
5961 DeclUpdates[D].push_back(
5962 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
5963}
5964
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005965void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5966 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005967 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005968 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005969 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005970 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005971
5972 assert(IFD->getDefinition() && "Category on a class without a definition?");
5973 ObjCClassesWithCategories.insert(
5974 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005975}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005976
Eli Friedman276dd182013-09-05 00:02:25 +00005977void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005978 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00005979 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00005980
5981 // If there is *any* declaration of the entity that's not from an AST file,
5982 // we can skip writing the update record. We make sure that isUsed() triggers
5983 // completion of the redeclaration chain of the entity.
5984 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
5985 if (IsLocalDecl(Prev))
5986 return;
Eli Friedman276dd182013-09-05 00:02:25 +00005987
Aaron Ballman4f45b712014-03-21 15:22:56 +00005988 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005989}
Alexey Bataev97720002014-11-11 04:05:39 +00005990
5991void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005992 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00005993 assert(!WritingAST && "Already writing the AST!");
5994 if (!D->isFromASTFile())
5995 return;
5996
5997 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5998}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005999
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006000void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6001 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006002 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006003 assert(!WritingAST && "Already writing the AST!");
6004 if (!D->isFromASTFile())
6005 return;
6006
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006007 DeclUpdates[D].push_back(
6008 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006009}
6010
Richard Smith4caa4492015-05-15 02:34:32 +00006011void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006012 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006013 assert(!WritingAST && "Already writing the AST!");
6014 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006015 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006016}
Alex Denisovfde64952015-06-26 05:28:36 +00006017
6018void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6019 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006020 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006021 assert(!WritingAST && "Already writing the AST!");
6022 if (!Record->isFromASTFile())
6023 return;
6024 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6025}
Richard Smithc26d9742016-10-06 20:30:51 +00006026
6027void ASTWriter::AddedCXXTemplateSpecialization(
6028 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6029 assert(!WritingAST && "Already writing the AST!");
6030
6031 if (!TD->getFirstDecl()->isFromASTFile())
6032 return;
6033 if (Chain && Chain->isProcessingUpdateRecords())
6034 return;
6035
6036 DeclsToEmitEvenIfUnreferenced.push_back(D);
6037}
6038
6039void ASTWriter::AddedCXXTemplateSpecialization(
6040 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6041 assert(!WritingAST && "Already writing the AST!");
6042
6043 if (!TD->getFirstDecl()->isFromASTFile())
6044 return;
6045 if (Chain && Chain->isProcessingUpdateRecords())
6046 return;
6047
6048 DeclsToEmitEvenIfUnreferenced.push_back(D);
6049}
6050
6051void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6052 const FunctionDecl *D) {
6053 assert(!WritingAST && "Already writing the AST!");
6054
6055 if (!TD->getFirstDecl()->isFromASTFile())
6056 return;
6057 if (Chain && Chain->isProcessingUpdateRecords())
6058 return;
6059
6060 DeclsToEmitEvenIfUnreferenced.push_back(D);
6061}