blob: a590e188eca9e18bf77f311c9aecefaab9212123 [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"
Douglas Gregor6623e1f2015-11-03 18:33:07 +000015#include "clang/Serialization/ModuleFileExtension.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000017#include "ASTReaderInternals.h"
18#include "MultiOnDiskHashTable.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Richard Smith961eae52014-03-25 01:14:22 +000023#include "clang/AST/DeclLookups.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"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000027#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000028#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000029#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000031#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000033#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000034#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000035#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000036#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000037#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000038#include "clang/Lex/HeaderSearch.h"
39#include "clang/Lex/HeaderSearchOptions.h"
40#include "clang/Lex/MacroInfo.h"
41#include "clang/Lex/PreprocessingRecord.h"
42#include "clang/Lex/Preprocessor.h"
43#include "clang/Lex/PreprocessorOptions.h"
44#include "clang/Sema/IdentifierResolver.h"
45#include "clang/Sema/Sema.h"
46#include "clang/Serialization/ASTReader.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000047#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000048#include "llvm/ADT/APFloat.h"
49#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000050#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000051#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000053#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000054#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000055#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000056#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000057#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000058#include "llvm/Support/Process.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000059#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000060#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000061#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000062#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000063
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000065using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066
Sebastian Redl3df5a082010-07-30 17:03:48 +000067template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000068static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000069 if (v.empty()) return StringRef();
70 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000071 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000072}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000073
74template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +000075static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000076 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000077 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000078}
79
Douglas Gregoref84c4b2009-04-09 22:27:44 +000080//===----------------------------------------------------------------------===//
81// Type serialization
82//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000083
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000085 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000086 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000087 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088
89 public:
90 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000091 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +000092 /// \brief Abbreviation to use for the record, if any.
93 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000095 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000096 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097
98 void VisitArrayType(const ArrayType *T);
99 void VisitFunctionType(const FunctionType *T);
100 void VisitTagType(const TagType *T);
101
102#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
103#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104#include "clang/AST/TypeNodes.def"
105 };
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000106} // end anonymous namespace
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000109 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000118 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Reid Kleckner8a365022013-06-24 17:51:48 +0000122void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
123 Writer.AddTypeRef(T->getOriginalType(), Record);
124 Code = TYPE_DECAYED;
125}
126
Reid Kleckner0503a872013-12-05 01:23:43 +0000127void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
128 Writer.AddTypeRef(T->getOriginalType(), Record);
129 Writer.AddTypeRef(T->getAdjustedType(), Record);
130 Code = TYPE_ADJUSTED;
131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000134 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000139 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
140 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000145 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000146 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147}
148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000150 Writer.AddTypeRef(T->getPointeeType(), Record);
151 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000152 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153}
154
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000158 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitArrayType(T);
163 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000164 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000174 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
175 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000176 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000177 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178}
179
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181 Writer.AddTypeRef(T->getElementType(), Record);
182 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000183 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000188 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000189 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190}
191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000193 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000194 FunctionType::ExtInfo C = T->getExtInfo();
195 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000196 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000197 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000198 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000199 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000200 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000201
202 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
203 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Richard Smith564417a2014-03-20 21:47:22 +0000211static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
212 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000213 Record.push_back(T->getExceptionSpecType());
214 if (T->getExceptionSpecType() == EST_Dynamic) {
215 Record.push_back(T->getNumExceptions());
216 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
217 Writer.AddTypeRef(T->getExceptionType(I), Record);
218 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
219 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000220 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
221 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
222 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000223 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
224 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000225 }
Richard Smith564417a2014-03-20 21:47:22 +0000226}
227
228void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
229 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000230
Richard Smith564417a2014-03-20 21:47:22 +0000231 Record.push_back(T->isVariadic());
232 Record.push_back(T->hasTrailingReturn());
233 Record.push_back(T->getTypeQuals());
234 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
235 addExceptionSpec(Writer, T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000236
237 Record.push_back(T->getNumParams());
238 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
239 Writer.AddTypeRef(T->getParamType(I), Record);
240
241 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
242 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
243 AbbrevToUse = 0;
244
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246}
247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000249 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000250 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000251}
John McCallb96ec562009-12-04 22:46:56 +0000252
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000253void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000254 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000255 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
256 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000257 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000261 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000266 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000267 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000268}
269
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000271 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000272 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000273 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000274}
275
Alexis Hunte852b102011-05-24 22:41:36 +0000276void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
277 Writer.AddTypeRef(T->getBaseType(), Record);
278 Writer.AddTypeRef(T->getUnderlyingType(), Record);
279 Record.push_back(T->getUTTKind());
280 Code = TYPE_UNARY_TRANSFORM;
281}
282
Richard Smith30482bc2011-02-20 03:19:35 +0000283void ASTTypeWriter::VisitAutoType(const AutoType *T) {
284 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smithe301ba22015-11-11 02:02:15 +0000285 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000286 if (T->getDeducedType().isNull())
287 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000288 Code = TYPE_AUTO;
289}
290
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000291void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000292 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000293 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000294 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000295 "Cannot serialize in the middle of a type definition");
296}
297
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000299 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000301}
302
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000303void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000304 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000305 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000306}
307
John McCall81904512011-01-06 01:58:22 +0000308void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
309 Writer.AddTypeRef(T->getModifiedType(), Record);
310 Writer.AddTypeRef(T->getEquivalentType(), Record);
311 Record.push_back(T->getAttrKind());
312 Code = TYPE_ATTRIBUTED;
313}
314
Mike Stump11289f42009-09-09 15:08:12 +0000315void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000316ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000317 const SubstTemplateTypeParmType *T) {
318 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
319 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000320 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000321}
322
323void
Douglas Gregorada4b792011-01-14 02:55:32 +0000324ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
325 const SubstTemplateTypeParmPackType *T) {
326 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
327 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
328 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
329}
330
331void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000332ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000333 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000334 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000335 Writer.AddTemplateName(T->getTemplateName(), Record);
336 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000337 for (const auto &ArgI : *T)
338 Writer.AddTemplateArgument(ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000339 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
340 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000341 : T->getCanonicalTypeInternal(),
342 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000343 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000344}
345
346void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000347ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000348 VisitArrayType(T);
349 Writer.AddStmt(T->getSizeExpr());
350 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000351 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000352}
353
354void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000355ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000356 const DependentSizedExtVectorType *T) {
357 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000358 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000359}
360
361void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000362ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000363 Record.push_back(T->getDepth());
364 Record.push_back(T->getIndex());
365 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000366 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000367 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000368}
369
370void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000371ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000372 Record.push_back(T->getKeyword());
373 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
374 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000375 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
376 : T->getCanonicalTypeInternal(),
377 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000378 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000379}
380
381void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000382ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000383 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000384 Record.push_back(T->getKeyword());
385 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
386 Writer.AddIdentifierRef(T->getIdentifier(), Record);
387 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000388 for (const auto &I : *T)
389 Writer.AddTemplateArgument(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000390 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000391}
392
Douglas Gregord2fa7662010-12-20 02:24:11 +0000393void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
394 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000395 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000396 Record.push_back(*NumExpansions + 1);
397 else
398 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000399 Code = TYPE_PACK_EXPANSION;
400}
401
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000402void ASTTypeWriter::VisitParenType(const ParenType *T) {
403 Writer.AddTypeRef(T->getInnerType(), Record);
404 Code = TYPE_PAREN;
405}
406
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000407void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000408 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000409 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
410 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000411 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000412}
413
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000414void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000415 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000416 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000417 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000418}
419
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000420void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000421 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000422 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000423}
424
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000425void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000426 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregore83b9562015-07-07 03:57:53 +0000427 Record.push_back(T->getTypeArgsAsWritten().size());
428 for (auto TypeArg : T->getTypeArgsAsWritten())
Douglas Gregore9d95f12015-07-07 03:57:35 +0000429 Writer.AddTypeRef(TypeArg, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000430 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000431 for (const auto *I : T->quals())
432 Writer.AddDeclRef(I, Record);
Douglas Gregorab209d82015-07-07 03:58:42 +0000433 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000434 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000435}
436
Steve Narofffb4330f2009-06-17 22:40:22 +0000437void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000438ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000439 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000440 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000441}
442
Eli Friedman0dfb8892011-10-06 23:00:33 +0000443void
444ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
445 Writer.AddTypeRef(T->getValueType(), Record);
446 Code = TYPE_ATOMIC;
447}
448
Xiuli Pan9c14e282016-01-09 12:53:17 +0000449void
450ASTTypeWriter::VisitPipeType(const PipeType *T) {
451 Writer.AddTypeRef(T->getElementType(), Record);
452 Code = TYPE_PIPE;
453}
454
John McCall8f115c62009-10-16 21:56:05 +0000455namespace {
456
457class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000458 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000459 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000460
461public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000462 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000463 : Writer(Writer), Record(Record) { }
464
John McCall17001972009-10-18 01:05:36 +0000465#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000466#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000467 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000468#include "clang/AST/TypeLocNodes.def"
469
John McCall17001972009-10-18 01:05:36 +0000470 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
471 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000472};
473
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000474} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000475
John McCall17001972009-10-18 01:05:36 +0000476void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
477 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000478}
John McCall17001972009-10-18 01:05:36 +0000479void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000480 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
481 if (TL.needsExtraLocalData()) {
482 Record.push_back(TL.getWrittenTypeSpec());
483 Record.push_back(TL.getWrittenSignSpec());
484 Record.push_back(TL.getWrittenWidthSpec());
485 Record.push_back(TL.hasModeAttr());
486 }
John McCall8f115c62009-10-16 21:56:05 +0000487}
John McCall17001972009-10-18 01:05:36 +0000488void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000490}
John McCall17001972009-10-18 01:05:36 +0000491void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000493}
Reid Kleckner8a365022013-06-24 17:51:48 +0000494void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
495 // nothing to do
496}
Reid Kleckner0503a872013-12-05 01:23:43 +0000497void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
498 // nothing to do
499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000502}
John McCall17001972009-10-18 01:05:36 +0000503void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000505}
John McCall17001972009-10-18 01:05:36 +0000506void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000508}
John McCall17001972009-10-18 01:05:36 +0000509void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000511 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000512}
John McCall17001972009-10-18 01:05:36 +0000513void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
515 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
516 Record.push_back(TL.getSizeExpr() ? 1 : 0);
517 if (TL.getSizeExpr())
518 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000519}
John McCall17001972009-10-18 01:05:36 +0000520void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
521 VisitArrayTypeLoc(TL);
522}
523void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
524 VisitArrayTypeLoc(TL);
525}
526void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
527 VisitArrayTypeLoc(TL);
528}
529void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
530 DependentSizedArrayTypeLoc TL) {
531 VisitArrayTypeLoc(TL);
532}
533void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
534 DependentSizedExtVectorTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
537void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
540void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
541 Writer.AddSourceLocation(TL.getNameLoc(), Record);
542}
543void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000544 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000545 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
546 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000547 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000548 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
549 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000550}
551void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
552 VisitFunctionTypeLoc(TL);
553}
554void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
555 VisitFunctionTypeLoc(TL);
556}
John McCallb96ec562009-12-04 22:46:56 +0000557void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
John McCall17001972009-10-18 01:05:36 +0000560void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
562}
563void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000564 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
565 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
566 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000567}
568void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000569 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
570 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
571 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
572 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000573}
574void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getNameLoc(), Record);
576}
Alexis Hunte852b102011-05-24 22:41:36 +0000577void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
578 Writer.AddSourceLocation(TL.getKWLoc(), Record);
579 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
580 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
581 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
582}
Richard Smith30482bc2011-02-20 03:19:35 +0000583void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
John McCall17001972009-10-18 01:05:36 +0000586void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
587 Writer.AddSourceLocation(TL.getNameLoc(), Record);
588}
589void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getNameLoc(), Record);
591}
John McCall81904512011-01-06 01:58:22 +0000592void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
593 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
594 if (TL.hasAttrOperand()) {
595 SourceRange range = TL.getAttrOperandParensRange();
596 Writer.AddSourceLocation(range.getBegin(), Record);
597 Writer.AddSourceLocation(range.getEnd(), Record);
598 }
599 if (TL.hasAttrExprOperand()) {
600 Expr *operand = TL.getAttrExprOperand();
601 Record.push_back(operand ? 1 : 0);
602 if (operand) Writer.AddStmt(operand);
603 } else if (TL.hasAttrEnumOperand()) {
604 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
605 }
606}
John McCall17001972009-10-18 01:05:36 +0000607void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
608 Writer.AddSourceLocation(TL.getNameLoc(), Record);
609}
John McCallcebee162009-10-18 09:09:24 +0000610void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
611 SubstTemplateTypeParmTypeLoc TL) {
612 Writer.AddSourceLocation(TL.getNameLoc(), Record);
613}
Douglas Gregorada4b792011-01-14 02:55:32 +0000614void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
615 SubstTemplateTypeParmPackTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getNameLoc(), Record);
617}
John McCall17001972009-10-18 01:05:36 +0000618void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
619 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000620 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000621 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
622 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
623 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
624 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000625 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
626 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000627}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000628void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
629 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
630 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
631}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000632void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000633 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000634 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000635}
John McCalle78aac42010-03-10 03:28:59 +0000636void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
637 Writer.AddSourceLocation(TL.getNameLoc(), Record);
638}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000639void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000640 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000641 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000642 Writer.AddSourceLocation(TL.getNameLoc(), Record);
643}
John McCallc392f372010-06-11 00:33:02 +0000644void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
645 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000646 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000647 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000648 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000649 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000650 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
651 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
652 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000653 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
654 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000655}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000656void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
657 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
658}
John McCall17001972009-10-18 01:05:36 +0000659void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
660 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000661}
662void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
663 Record.push_back(TL.hasBaseTypeAsWritten());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000664 Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record);
665 Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record);
666 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
667 Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record);
668 Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record);
669 Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000670 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
671 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000672}
John McCallfc93cf92009-10-22 22:37:11 +0000673void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
674 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000675}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000676void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
677 Writer.AddSourceLocation(TL.getKWLoc(), Record);
678 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
679 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
680}
Xiuli Pan9c14e282016-01-09 12:53:17 +0000681void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
682 Writer.AddSourceLocation(TL.getKWLoc(), Record);
683}
John McCall8f115c62009-10-16 21:56:05 +0000684
Richard Smith01b2cb42014-07-26 06:37:51 +0000685void ASTWriter::WriteTypeAbbrevs() {
686 using namespace llvm;
687
688 BitCodeAbbrev *Abv;
689
690 // Abbreviation for TYPE_EXT_QUAL
691 Abv = new BitCodeAbbrev();
692 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
694 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
695 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
696
697 // Abbreviation for TYPE_FUNCTION_PROTO
698 Abv = new BitCodeAbbrev();
699 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
700 // FunctionType
701 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
702 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
703 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
704 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
705 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
706 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
707 // FunctionProtoType
708 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
709 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
710 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
711 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
712 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
713 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
714 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
715 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
716 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
717}
718
Chris Lattner19cea4e2009-04-22 05:57:30 +0000719//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000720// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000721//===----------------------------------------------------------------------===//
722
Chris Lattner28fa4e62009-04-26 22:26:21 +0000723static void EmitBlockID(unsigned ID, const char *Name,
724 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000725 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000726 Record.clear();
727 Record.push_back(ID);
728 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
729
730 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000731 if (!Name || Name[0] == 0)
732 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000733 Record.clear();
734 while (*Name)
735 Record.push_back(*Name++);
736 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
737}
738
739static void EmitRecordID(unsigned ID, const char *Name,
740 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000741 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000742 Record.clear();
743 Record.push_back(ID);
744 while (*Name)
745 Record.push_back(*Name++);
746 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000747}
748
749static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000750 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000751#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000752 RECORD(STMT_STOP);
753 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000754 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000755 RECORD(STMT_NULL);
756 RECORD(STMT_COMPOUND);
757 RECORD(STMT_CASE);
758 RECORD(STMT_DEFAULT);
759 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000760 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000761 RECORD(STMT_IF);
762 RECORD(STMT_SWITCH);
763 RECORD(STMT_WHILE);
764 RECORD(STMT_DO);
765 RECORD(STMT_FOR);
766 RECORD(STMT_GOTO);
767 RECORD(STMT_INDIRECT_GOTO);
768 RECORD(STMT_CONTINUE);
769 RECORD(STMT_BREAK);
770 RECORD(STMT_RETURN);
771 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000772 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000773 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000774 RECORD(EXPR_PREDEFINED);
775 RECORD(EXPR_DECL_REF);
776 RECORD(EXPR_INTEGER_LITERAL);
777 RECORD(EXPR_FLOATING_LITERAL);
778 RECORD(EXPR_IMAGINARY_LITERAL);
779 RECORD(EXPR_STRING_LITERAL);
780 RECORD(EXPR_CHARACTER_LITERAL);
781 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000782 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000783 RECORD(EXPR_UNARY_OPERATOR);
784 RECORD(EXPR_SIZEOF_ALIGN_OF);
785 RECORD(EXPR_ARRAY_SUBSCRIPT);
786 RECORD(EXPR_CALL);
787 RECORD(EXPR_MEMBER);
788 RECORD(EXPR_BINARY_OPERATOR);
789 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
790 RECORD(EXPR_CONDITIONAL_OPERATOR);
791 RECORD(EXPR_IMPLICIT_CAST);
792 RECORD(EXPR_CSTYLE_CAST);
793 RECORD(EXPR_COMPOUND_LITERAL);
794 RECORD(EXPR_EXT_VECTOR_ELEMENT);
795 RECORD(EXPR_INIT_LIST);
796 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000797 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000798 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000799 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000800 RECORD(EXPR_VA_ARG);
801 RECORD(EXPR_ADDR_LABEL);
802 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000803 RECORD(EXPR_CHOOSE);
804 RECORD(EXPR_GNU_NULL);
805 RECORD(EXPR_SHUFFLE_VECTOR);
806 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000807 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000808 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000809 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000810 RECORD(EXPR_OBJC_ARRAY_LITERAL);
811 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000812 RECORD(EXPR_OBJC_ENCODE);
813 RECORD(EXPR_OBJC_SELECTOR_EXPR);
814 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
815 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
816 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
817 RECORD(EXPR_OBJC_KVC_REF_EXPR);
818 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000819 RECORD(STMT_OBJC_FOR_COLLECTION);
820 RECORD(STMT_OBJC_CATCH);
821 RECORD(STMT_OBJC_FINALLY);
822 RECORD(STMT_OBJC_AT_TRY);
823 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
824 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000825 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000826 RECORD(STMT_CXX_CATCH);
827 RECORD(STMT_CXX_TRY);
828 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000829 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000830 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000831 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000832 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000833 RECORD(EXPR_CXX_STATIC_CAST);
834 RECORD(EXPR_CXX_DYNAMIC_CAST);
835 RECORD(EXPR_CXX_REINTERPRET_CAST);
836 RECORD(EXPR_CXX_CONST_CAST);
837 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000838 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000839 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000840 RECORD(EXPR_CXX_BOOL_LITERAL);
841 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000842 RECORD(EXPR_CXX_TYPEID_EXPR);
843 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000844 RECORD(EXPR_CXX_THIS);
845 RECORD(EXPR_CXX_THROW);
846 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000847 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000848 RECORD(EXPR_CXX_BIND_TEMPORARY);
849 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
850 RECORD(EXPR_CXX_NEW);
851 RECORD(EXPR_CXX_DELETE);
852 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
853 RECORD(EXPR_EXPR_WITH_CLEANUPS);
854 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
855 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
856 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
857 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
858 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000859 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000860 RECORD(EXPR_CXX_NOEXCEPT);
861 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000862 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
863 RECORD(EXPR_TYPE_TRAIT);
864 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000865 RECORD(EXPR_PACK_EXPANSION);
866 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000867 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000868 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000869 RECORD(EXPR_FUNCTION_PARM_PACK);
870 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000871 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000872 RECORD(EXPR_CXX_UUIDOF_EXPR);
873 RECORD(EXPR_CXX_UUIDOF_TYPE);
874 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000875#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000876}
Mike Stump11289f42009-09-09 15:08:12 +0000877
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000878void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000879 RecordData Record;
880 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000881
Sebastian Redl539c5062010-08-18 23:57:32 +0000882#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
883#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000884
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000885 // Control Block.
886 BLOCK(CONTROL_BLOCK);
887 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000888 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000889 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +0000890 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000891 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000892 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000893 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000894 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000895 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000896 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +0000897
898 BLOCK(OPTIONS_BLOCK);
899 RECORD(LANGUAGE_OPTIONS);
900 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000901 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000902 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000903 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000904 RECORD(PREPROCESSOR_OPTIONS);
905
Douglas Gregor108cb222012-10-19 00:45:00 +0000906 BLOCK(INPUT_FILES_BLOCK);
907 RECORD(INPUT_FILE);
908
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000909 // AST Top-Level Block.
910 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000911 RECORD(TYPE_OFFSET);
912 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000913 RECORD(IDENTIFIER_OFFSET);
914 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000915 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000916 RECORD(SPECIAL_TYPES);
917 RECORD(STATISTICS);
918 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000919 RECORD(SELECTOR_OFFSETS);
920 RECORD(METHOD_POOL);
921 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000922 RECORD(SOURCE_LOCATION_OFFSETS);
923 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000924 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +0000925 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000926 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +0000927 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000928 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000929 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000930 RECORD(SEMA_DECL_REFS);
931 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
932 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
933 RECORD(DECL_REPLACEMENTS);
934 RECORD(UPDATE_VISIBLE);
935 RECORD(DECL_UPDATE_OFFSETS);
936 RECORD(DECL_UPDATES);
937 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
938 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000939 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000940 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000941 RECORD(FP_PRAGMA_OPTIONS);
942 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000943 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000944 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000945 RECORD(MODULE_OFFSET_MAP);
946 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000947 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000948 RECORD(FILE_SORTED_DECLS);
949 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000950 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000951 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +0000952 RECORD(INTERESTING_IDENTIFIERS);
953 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000954 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000955 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +0000956 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
957 RECORD(CXX_CTOR_INITIALIZERS_OFFSETS);
958 RECORD(DELETE_EXPRS_TO_ANALYZE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000959
Chris Lattner28fa4e62009-04-26 22:26:21 +0000960 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000961 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000962 RECORD(SM_SLOC_FILE_ENTRY);
963 RECORD(SM_SLOC_BUFFER_ENTRY);
964 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000965 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Chris Lattner28fa4e62009-04-26 22:26:21 +0000967 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000968 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000969 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000970 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +0000971 RECORD(PP_MACRO_OBJECT_LIKE);
972 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000973 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000974
Richard Smithfa715652015-08-31 22:43:10 +0000975 // Submodule Block.
976 BLOCK(SUBMODULE_BLOCK);
977 RECORD(SUBMODULE_METADATA);
978 RECORD(SUBMODULE_DEFINITION);
979 RECORD(SUBMODULE_UMBRELLA_HEADER);
980 RECORD(SUBMODULE_HEADER);
981 RECORD(SUBMODULE_TOPHEADER);
982 RECORD(SUBMODULE_UMBRELLA_DIR);
983 RECORD(SUBMODULE_IMPORTS);
984 RECORD(SUBMODULE_EXPORTS);
985 RECORD(SUBMODULE_REQUIRES);
986 RECORD(SUBMODULE_EXCLUDED_HEADER);
987 RECORD(SUBMODULE_LINK_LIBRARY);
988 RECORD(SUBMODULE_CONFIG_MACRO);
989 RECORD(SUBMODULE_CONFLICT);
990 RECORD(SUBMODULE_PRIVATE_HEADER);
991 RECORD(SUBMODULE_TEXTUAL_HEADER);
992 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
993
994 // Comments Block.
995 BLOCK(COMMENTS_BLOCK);
996 RECORD(COMMENTS_RAW_COMMENT);
997
Douglas Gregor12bfa382009-10-17 00:13:19 +0000998 // Decls and Types block.
999 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001000 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001001 RECORD(TYPE_COMPLEX);
1002 RECORD(TYPE_POINTER);
1003 RECORD(TYPE_BLOCK_POINTER);
1004 RECORD(TYPE_LVALUE_REFERENCE);
1005 RECORD(TYPE_RVALUE_REFERENCE);
1006 RECORD(TYPE_MEMBER_POINTER);
1007 RECORD(TYPE_CONSTANT_ARRAY);
1008 RECORD(TYPE_INCOMPLETE_ARRAY);
1009 RECORD(TYPE_VARIABLE_ARRAY);
1010 RECORD(TYPE_VECTOR);
1011 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001012 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001013 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001014 RECORD(TYPE_TYPEDEF);
1015 RECORD(TYPE_TYPEOF_EXPR);
1016 RECORD(TYPE_TYPEOF);
1017 RECORD(TYPE_RECORD);
1018 RECORD(TYPE_ENUM);
1019 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001020 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001021 RECORD(TYPE_DECLTYPE);
1022 RECORD(TYPE_ELABORATED);
1023 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1024 RECORD(TYPE_UNRESOLVED_USING);
1025 RECORD(TYPE_INJECTED_CLASS_NAME);
1026 RECORD(TYPE_OBJC_OBJECT);
1027 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1028 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1029 RECORD(TYPE_DEPENDENT_NAME);
1030 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1031 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1032 RECORD(TYPE_PAREN);
1033 RECORD(TYPE_PACK_EXPANSION);
1034 RECORD(TYPE_ATTRIBUTED);
1035 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001036 RECORD(TYPE_AUTO);
1037 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001038 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001039 RECORD(TYPE_DECAYED);
1040 RECORD(TYPE_ADJUSTED);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001041 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001042 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001043 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001044 RECORD(DECL_ENUM);
1045 RECORD(DECL_RECORD);
1046 RECORD(DECL_ENUM_CONSTANT);
1047 RECORD(DECL_FUNCTION);
1048 RECORD(DECL_OBJC_METHOD);
1049 RECORD(DECL_OBJC_INTERFACE);
1050 RECORD(DECL_OBJC_PROTOCOL);
1051 RECORD(DECL_OBJC_IVAR);
1052 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001053 RECORD(DECL_OBJC_CATEGORY);
1054 RECORD(DECL_OBJC_CATEGORY_IMPL);
1055 RECORD(DECL_OBJC_IMPLEMENTATION);
1056 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1057 RECORD(DECL_OBJC_PROPERTY);
1058 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001059 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001060 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001061 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001062 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001063 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001064 RECORD(DECL_FILE_SCOPE_ASM);
1065 RECORD(DECL_BLOCK);
1066 RECORD(DECL_CONTEXT_LEXICAL);
1067 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001068 RECORD(DECL_NAMESPACE);
1069 RECORD(DECL_NAMESPACE_ALIAS);
1070 RECORD(DECL_USING);
1071 RECORD(DECL_USING_SHADOW);
1072 RECORD(DECL_USING_DIRECTIVE);
1073 RECORD(DECL_UNRESOLVED_USING_VALUE);
1074 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1075 RECORD(DECL_LINKAGE_SPEC);
1076 RECORD(DECL_CXX_RECORD);
1077 RECORD(DECL_CXX_METHOD);
1078 RECORD(DECL_CXX_CONSTRUCTOR);
1079 RECORD(DECL_CXX_DESTRUCTOR);
1080 RECORD(DECL_CXX_CONVERSION);
1081 RECORD(DECL_ACCESS_SPEC);
1082 RECORD(DECL_FRIEND);
1083 RECORD(DECL_FRIEND_TEMPLATE);
1084 RECORD(DECL_CLASS_TEMPLATE);
1085 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1086 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001087 RECORD(DECL_VAR_TEMPLATE);
1088 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1089 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001090 RECORD(DECL_FUNCTION_TEMPLATE);
1091 RECORD(DECL_TEMPLATE_TYPE_PARM);
1092 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1093 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1094 RECORD(DECL_STATIC_ASSERT);
1095 RECORD(DECL_CXX_BASE_SPECIFIERS);
1096 RECORD(DECL_INDIRECTFIELD);
1097 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1098
Douglas Gregor03412ba2011-06-03 02:27:19 +00001099 // Statements and Exprs can occur in the Decls and Types block.
1100 AddStmtsExprs(Stream, Record);
1101
Douglas Gregor92a96f52011-02-08 21:58:10 +00001102 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001103 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001104 RECORD(PPD_MACRO_DEFINITION);
1105 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001106
1107 // Decls and Types block.
1108 BLOCK(EXTENSION_BLOCK);
1109 RECORD(EXTENSION_METADATA);
1110
Chris Lattner28fa4e62009-04-26 22:26:21 +00001111#undef RECORD
1112#undef BLOCK
1113 Stream.ExitBlock();
1114}
1115
Richard Smith54cc3c22014-12-11 20:50:24 +00001116/// \brief Prepares a path for being written to an AST file by converting it
1117/// to an absolute path and removing nested './'s.
1118///
1119/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001120static bool cleanPathForOutput(FileManager &FileMgr,
1121 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001122 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001123 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001124}
1125
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001126/// \brief Adjusts the given filename to only write out the portion of the
1127/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001128///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001129/// \param Filename the file name to adjust.
1130///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001131/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1132/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001133///
1134/// \returns either the original filename (if it needs no adjustment) or the
1135/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001136static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001137adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001138 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001139
Richard Smith7ed1bc92014-12-05 22:42:13 +00001140 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001141 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001142
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001143 // Verify that the filename and the system root have the same prefix.
1144 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001145 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1146 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001147 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001149 // We hit the end of the filename before we hit the end of the system root.
1150 if (!Filename[Pos])
1151 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001152
Richard Smith7ed1bc92014-12-05 22:42:13 +00001153 // If there's not a path separator at the end of the base directory nor
1154 // immediately after it, then this isn't within the base directory.
1155 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1156 if (!llvm::sys::path::is_separator(BaseDir.back()))
1157 return Filename;
1158 } else {
1159 // If the file name has a '/' at the current position, skip over the '/'.
1160 // We distinguish relative paths from absolute paths by the
1161 // absence of '/' at the beginning of relative paths.
1162 //
1163 // FIXME: This is wrong. We distinguish them by asking if the path is
1164 // absolute, which isn't the same thing. And there might be multiple '/'s
1165 // in a row. Use a better mechanism to indicate whether we have emitted an
1166 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001167 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001168 }
Mike Stump11289f42009-09-09 15:08:12 +00001169
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001170 return Filename + Pos;
1171}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001172
Ben Langmuir487ea142014-10-23 18:05:36 +00001173static ASTFileSignature getSignature() {
1174 while (1) {
1175 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1176 return S;
1177 // Rely on GetRandomNumber to eventually return non-zero...
1178 }
1179}
1180
Douglas Gregor112b9072012-10-18 05:31:06 +00001181/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001182uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1183 ASTContext &Context,
1184 StringRef isysroot,
1185 const std::string &OutputFile) {
1186 ASTFileSignature Signature = 0;
1187
Douglas Gregorbfbde532009-04-10 21:16:55 +00001188 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001189 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001190 RecordData Record;
1191
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001192 // Metadata
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001193 auto *MetadataAbbrev = new BitCodeAbbrev();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001194 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1195 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1196 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1197 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1198 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1199 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001200 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001201 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1202 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1203 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001204 assert((!WritingModule || isysroot.empty()) &&
1205 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001206 {
1207 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1208 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1209 !isysroot.empty(), IncludeTimestamps,
1210 ASTHasCompilerErrors};
1211 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1212 getClangFullRepositoryVersion());
1213 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001214 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001215 // For implicit modules we output a signature that we can use to ensure
1216 // duplicate module builds don't collide in the cache as their output order
1217 // is non-deterministic.
1218 // FIXME: Remove this when output is deterministic.
1219 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001220 Signature = getSignature();
1221 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001222 Stream.EmitRecord(SIGNATURE, Record);
1223 }
1224
Richard Smith7ed1bc92014-12-05 22:42:13 +00001225 // Module name
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001226 auto *Abbrev = new BitCodeAbbrev();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001227 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1229 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00001230 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001231 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1232 }
1233
Richard Smith7ed1bc92014-12-05 22:42:13 +00001234 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001235 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001236 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001237
1238 // If the home of the module is the current working directory, then we
1239 // want to pick up the cwd of the build process loading the module, not
1240 // our cwd, when we load this module.
1241 if (!PP.getHeaderSearchInfo()
1242 .getHeaderSearchOpts()
1243 .ModuleMapFileHomeIsCwd ||
1244 WritingModule->Directory->getName() != StringRef(".")) {
1245 // Module directory.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001246 auto *Abbrev = new BitCodeAbbrev();
Richard Smithf7b41372015-08-13 23:47:44 +00001247 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1249 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1250
Mehdi Amini57a41912015-09-10 01:46:39 +00001251 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001252 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1253 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001254
1255 // Write out all other paths relative to the base directory if possible.
1256 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1257 } else if (!isysroot.empty()) {
1258 // Write out paths relative to the sysroot if possible.
1259 BaseDirectory = isysroot;
1260 }
1261
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001262 // Module map file
1263 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001264 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001265
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001266 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1267
1268 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001269 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001270
1271 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001272 if (auto *AdditionalModMaps =
1273 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001274 Record.push_back(AdditionalModMaps->size());
1275 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001276 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001277 } else {
1278 Record.push_back(0);
1279 }
1280
1281 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001282 }
1283
Douglas Gregor112b9072012-10-18 05:31:06 +00001284 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001285 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001286 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001287 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001288
Richard Smith7f330cd2015-03-18 01:42:29 +00001289 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001290 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001291 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001292 continue;
1293
Richard Smith7f330cd2015-03-18 01:42:29 +00001294 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1295 AddSourceLocation(M->ImportLoc, Record);
1296 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001297 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001298 Record.push_back(M->Signature);
1299 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001300 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001301 Stream.EmitRecord(IMPORTS, Record);
1302 }
Mike Stump11289f42009-09-09 15:08:12 +00001303
Richard Smith0516b182015-09-08 19:40:14 +00001304 // Write the options block.
1305 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1306
Douglas Gregor112b9072012-10-18 05:31:06 +00001307 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001308 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001309 const LangOptions &LangOpts = Context.getLangOpts();
1310#define LANGOPT(Name, Bits, Default, Description) \
1311 Record.push_back(LangOpts.Name);
1312#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1313 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001314#include "clang/Basic/LangOptions.def"
1315#define SANITIZER(NAME, ID) \
1316 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001317#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001318
Ben Langmuircd98cb72015-06-23 18:20:18 +00001319 Record.push_back(LangOpts.ModuleFeatures.size());
1320 for (StringRef Feature : LangOpts.ModuleFeatures)
1321 AddString(Feature, Record);
1322
Douglas Gregor112b9072012-10-18 05:31:06 +00001323 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1324 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001325
1326 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001327
1328 // Comment options.
1329 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001330 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1331 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001332 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001333 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001334
Samuel Antaoee8fb302016-01-06 13:42:12 +00001335 // OpenMP offloading options.
1336 Record.push_back(LangOpts.OMPTargetTriples.size());
1337 for (auto &T : LangOpts.OMPTargetTriples)
1338 AddString(T.getTriple(), Record);
1339
1340 AddString(LangOpts.OMPHostIRFile, Record);
1341
Douglas Gregor112b9072012-10-18 05:31:06 +00001342 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1343
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001344 // Target options.
1345 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001346 const TargetInfo &Target = Context.getTargetInfo();
1347 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001348 AddString(TargetOpts.Triple, Record);
1349 AddString(TargetOpts.CPU, Record);
1350 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001351 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1352 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1353 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1354 }
1355 Record.push_back(TargetOpts.Features.size());
1356 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1357 AddString(TargetOpts.Features[I], Record);
1358 }
1359 Stream.EmitRecord(TARGET_OPTIONS, Record);
1360
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001361 // Diagnostic options.
1362 Record.clear();
1363 const DiagnosticOptions &DiagOpts
1364 = Context.getDiagnostics().getDiagnosticOptions();
1365#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1366#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1367 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1368#include "clang/Basic/DiagnosticOptions.def"
1369 Record.push_back(DiagOpts.Warnings.size());
1370 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1371 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001372 Record.push_back(DiagOpts.Remarks.size());
1373 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1374 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001375 // Note: we don't serialize the log or serialization file names, because they
1376 // are generally transient files and will almost always be overridden.
1377 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1378
Douglas Gregorc6317db2012-10-24 15:49:58 +00001379 // File system options.
1380 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001381 const FileSystemOptions &FSOpts =
1382 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001383 AddString(FSOpts.WorkingDir, Record);
1384 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1385
Douglas Gregor2d302362012-10-24 16:50:34 +00001386 // Header search options.
1387 Record.clear();
1388 const HeaderSearchOptions &HSOpts
1389 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1390 AddString(HSOpts.Sysroot, Record);
1391
1392 // Include entries.
1393 Record.push_back(HSOpts.UserEntries.size());
1394 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1395 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1396 AddString(Entry.Path, Record);
1397 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001398 Record.push_back(Entry.IsFramework);
1399 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001400 }
1401
1402 // System header prefixes.
1403 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1404 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1405 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1406 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1407 }
1408
1409 AddString(HSOpts.ResourceDir, Record);
1410 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001411 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001412 Record.push_back(HSOpts.DisableModuleHash);
1413 Record.push_back(HSOpts.UseBuiltinIncludes);
1414 Record.push_back(HSOpts.UseStandardSystemIncludes);
1415 Record.push_back(HSOpts.UseStandardCXXIncludes);
1416 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001417 // Write out the specific module cache path that contains the module files.
1418 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001419 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1420
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001421 // Preprocessor options.
1422 Record.clear();
1423 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1424
1425 // Macro definitions.
1426 Record.push_back(PPOpts.Macros.size());
1427 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1428 AddString(PPOpts.Macros[I].first, Record);
1429 Record.push_back(PPOpts.Macros[I].second);
1430 }
1431
1432 // Includes
1433 Record.push_back(PPOpts.Includes.size());
1434 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1435 AddString(PPOpts.Includes[I], Record);
1436
1437 // Macro includes
1438 Record.push_back(PPOpts.MacroIncludes.size());
1439 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1440 AddString(PPOpts.MacroIncludes[I], Record);
1441
Douglas Gregorb6368752012-10-24 23:41:50 +00001442 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001443 // Detailed record is important since it is used for the module cache hash.
1444 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001445 AddString(PPOpts.ImplicitPCHInclude, Record);
1446 AddString(PPOpts.ImplicitPTHInclude, Record);
1447 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1448 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1449
Richard Smith0516b182015-09-08 19:40:14 +00001450 // Leave the options block.
1451 Stream.ExitBlock();
1452
Douglas Gregora3b20262011-05-06 21:43:30 +00001453 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001454 SourceManager &SM = Context.getSourceManager();
1455 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001456 auto *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001457 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1458 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001459 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1460 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1461
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001462 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001463 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001464 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001465 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001466 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001467
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001468 Record.clear();
1469 Record.push_back(SM.getMainFileID().getOpaqueValue());
1470 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1471
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001472 // Original PCH directory
1473 if (!OutputFile.empty() && OutputFile != "-") {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001474 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001475 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1477 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1478
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001479 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001480
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001481 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001482 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1483
Mehdi Amini57a41912015-09-10 01:46:39 +00001484 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001485 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1486 }
1487
Douglas Gregor49491f72013-03-15 22:15:07 +00001488 WriteInputFiles(Context.SourceMgr,
1489 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001490 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001491 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001492 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001493}
1494
Douglas Gregor49491f72013-03-15 22:15:07 +00001495namespace {
1496 /// \brief An input file.
1497 struct InputFileEntry {
1498 const FileEntry *File;
1499 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001500 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001501 bool BufferOverridden;
1502 };
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001503} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001504
1505void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1506 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001507 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001508 using namespace llvm;
1509 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001510
Douglas Gregor72be3902012-10-19 00:38:02 +00001511 // Create input-file abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001512 auto *IFAbbrev = new BitCodeAbbrev();
Douglas Gregor72be3902012-10-19 00:38:02 +00001513 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001514 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001515 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1516 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001517 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001518 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001519 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1520 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1521
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001522 // Get all ContentCache objects for files, sorted by whether the file is a
1523 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001524 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001525 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1526 // Get this source location entry.
1527 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001528 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001529
1530 // We only care about file entries that were not overridden.
1531 if (!SLoc->isFile())
1532 continue;
1533 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001534 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001535 continue;
1536
Douglas Gregor49491f72013-03-15 22:15:07 +00001537 InputFileEntry Entry;
1538 Entry.File = Cache->OrigEntry;
1539 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001540 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001541 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001542 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001543 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001544 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001545 SortedFiles.push_front(Entry);
1546 }
1547
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001548 unsigned UserFilesNum = 0;
1549 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001550 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001551 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001552 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001553 if (InputFileID != 0)
1554 continue; // already recorded this file.
1555
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001556 // Record this entry's offset.
1557 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001558
1559 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001560
Douglas Gregor49491f72013-03-15 22:15:07 +00001561 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001562 ++UserFilesNum;
1563
Douglas Gregor72be3902012-10-19 00:38:02 +00001564 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001565 // And whether this file was overridden.
1566 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001567 INPUT_FILE,
1568 InputFileOffsets.size(),
1569 (uint64_t)Entry.File->getSize(),
1570 (uint64_t)getTimestampForOutput(Entry.File),
1571 Entry.BufferOverridden,
1572 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001573
Richard Smith7ed1bc92014-12-05 22:42:13 +00001574 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1575 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001576
Douglas Gregor112b9072012-10-18 05:31:06 +00001577 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001578
1579 // Create input file offsets abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001580 auto *OffsetsAbbrev = new BitCodeAbbrev();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001581 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1582 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001583 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1584 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001585 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1586 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1587
1588 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001589 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1590 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001591 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001592}
1593
Douglas Gregora7f71a92009-04-10 03:52:48 +00001594//===----------------------------------------------------------------------===//
1595// Source Manager Serialization
1596//===----------------------------------------------------------------------===//
1597
1598/// \brief Create an abbreviation for the SLocEntry that refers to a
1599/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001600static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001601 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001602
1603 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001604 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001609 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001614 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001615}
1616
1617/// \brief Create an abbreviation for the SLocEntry that refers to a
1618/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001619static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001620 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001621
1622 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001623 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001629 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001630}
1631
1632/// \brief Create an abbreviation for the SLocEntry that refers to a
1633/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001634static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001635 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001636
1637 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001638 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001640 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001641}
1642
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001643/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1644/// expansion.
1645static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001646 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001647
1648 auto *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001649 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001650 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1653 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001655 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001656}
1657
Douglas Gregor09b69892011-02-10 17:09:37 +00001658namespace {
1659 // Trait used for the on-disk hash table of header search information.
1660 class HeaderFileInfoTrait {
1661 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001662 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001663
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001664 // Keep track of the framework names we've used during serialization.
1665 SmallVector<char, 128> FrameworkStringData;
1666 llvm::StringMap<unsigned> FrameworkNameOffset;
1667
Douglas Gregor09b69892011-02-10 17:09:37 +00001668 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001669 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1670 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001671
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001672 struct key_type {
1673 const FileEntry *FE;
1674 const char *Filename;
1675 };
1676 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001677
1678 typedef HeaderFileInfo data_type;
1679 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001680 typedef unsigned hash_value_type;
1681 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001682
Richard Smithe75ee0f2015-08-17 07:13:32 +00001683 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001684 // The hash is based only on size/time of the file, so that the reader can
1685 // match even when symlinking or excess path elements ("foo/../", "../")
1686 // change the form of the name. However, complete path is still the key.
1687 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001688 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001689 }
1690
1691 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001692 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001693 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001694 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001695 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001696 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001697 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001698 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1699 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1700 DataLen += 4;
1701 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001702 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001703 }
1704
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001705 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001706 using namespace llvm::support;
1707 endian::Writer<little> LE(Out);
1708 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001709 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001710 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001711 KeyLen -= 8;
1712 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001713 }
1714
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001715 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001716 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001717 using namespace llvm::support;
1718 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001719 uint64_t Start = Out.tell(); (void)Start;
1720
Richard Smith386bb072015-08-18 23:42:23 +00001721 unsigned char Flags = (Data.isImport << 4)
1722 | (Data.isPragmaOnce << 3)
1723 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001724 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001725 LE.write<uint8_t>(Flags);
1726 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001727
1728 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001729 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001730 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001731 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001732
1733 unsigned Offset = 0;
1734 if (!Data.Framework.empty()) {
1735 // If this header refers into a framework, save the framework name.
1736 llvm::StringMap<unsigned>::iterator Pos
1737 = FrameworkNameOffset.find(Data.Framework);
1738 if (Pos == FrameworkNameOffset.end()) {
1739 Offset = FrameworkStringData.size() + 1;
1740 FrameworkStringData.append(Data.Framework.begin(),
1741 Data.Framework.end());
1742 FrameworkStringData.push_back(0);
1743
1744 FrameworkNameOffset[Data.Framework] = Offset;
1745 } else
1746 Offset = Pos->second;
1747 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001748 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001749
Richard Smith386bb072015-08-18 23:42:23 +00001750 // FIXME: If the header is excluded, we should write out some
1751 // record of that fact.
1752 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1753 if (uint32_t ModID =
1754 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1755 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1756 assert((Value >> 2) == ModID && "overflow in header module info");
1757 LE.write<uint32_t>(Value);
1758 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001759 }
1760
Douglas Gregor09b69892011-02-10 17:09:37 +00001761 assert(Out.tell() - Start == DataLen && "Wrong data length");
1762 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001763
1764 const char *strings_begin() const { return FrameworkStringData.begin(); }
1765 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001766 };
1767} // end anonymous namespace
1768
1769/// \brief Write the header search block for the list of files that
1770///
1771/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001772void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001773 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001774 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1775
1776 if (FilesByUID.size() > HS.header_file_size())
1777 FilesByUID.resize(HS.header_file_size());
1778
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001779 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001780 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001781 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001782 unsigned NumHeaderSearchEntries = 0;
1783 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1784 const FileEntry *File = FilesByUID[UID];
1785 if (!File)
1786 continue;
1787
Richard Smith386bb072015-08-18 23:42:23 +00001788 // Get the file info. This will load info from the external source if
1789 // necessary. Skip emitting this file if we have no information on it
1790 // as a header file (in which case HFI will be null) or if it hasn't
1791 // changed since it was loaded. Also skip it if it's for a modular header
1792 // from a different module; in that case, we rely on the module(s)
1793 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001794 const HeaderFileInfo *HFI =
1795 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1796 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001797 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001798
Richard Smith7ed1bc92014-12-05 22:42:13 +00001799 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001800 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001801 SmallString<128> FilenameTmp(Filename);
1802 if (PreparePathForOutput(FilenameTmp)) {
1803 // If we performed any translation on the file name at all, we need to
1804 // save this string, since the generator will refer to it later.
1805 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001806 SavedStrings.push_back(Filename);
1807 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001808
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001809 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001810 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001811 ++NumHeaderSearchEntries;
1812 }
1813
1814 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001815 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001816 uint32_t BucketOffset;
1817 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001818 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001819 llvm::raw_svector_ostream Out(TableData);
1820 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001821 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001822 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1823 }
1824
1825 // Create a blob abbreviation
1826 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001827
1828 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor09b69892011-02-10 17:09:37 +00001829 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1834 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1835
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001836 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001837 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1838 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001839 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001840 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001841
1842 // Free all of the strings we had to duplicate.
1843 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001844 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001845}
1846
Douglas Gregora7f71a92009-04-10 03:52:48 +00001847/// \brief Writes the block containing the serialized form of the
1848/// source manager.
1849///
1850/// TODO: We should probably use an on-disk hash table (stored in a
1851/// blob), indexed based on the file name, so that we only create
1852/// entries for files that we actually need. In the common case (no
1853/// errors), we probably won't have to create file entries for any of
1854/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001855void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001856 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001857 RecordData Record;
1858
Chris Lattner0910e3b2009-04-10 17:16:57 +00001859 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001860 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001861
1862 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001863 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1864 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1865 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001866 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001867
Douglas Gregor258ae542009-04-27 06:38:32 +00001868 // Write out the source location entry table. We skip the first
1869 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001870 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001871 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001872 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1873 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001874 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001875 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001876 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001877 FileID FID = FileID::get(I);
1878 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001879
Douglas Gregor258ae542009-04-27 06:38:32 +00001880 // Record the offset of this source-location entry.
1881 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1882
1883 // Figure out which record code to use.
1884 unsigned Code;
1885 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001886 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1887 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001888 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001889 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001890 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001891 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001892 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001893 Record.clear();
1894 Record.push_back(Code);
1895
Douglas Gregor925296b2011-07-19 16:10:42 +00001896 // Starting offset of this entry within this module, so skip the dummy.
1897 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001898 if (SLoc->isFile()) {
1899 const SrcMgr::FileInfo &File = SLoc->getFile();
1900 Record.push_back(File.getIncludeLoc().getRawEncoding());
1901 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1902 Record.push_back(File.hasLineDirectives());
1903
1904 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001905 if (Content->OrigEntry) {
1906 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001907 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001908
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001909 // The source location entry is a file. Emit input file ID.
1910 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1911 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001912
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001913 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001914
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001915 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001916 if (FDI != FileDeclIDs.end()) {
1917 Record.push_back(FDI->second->FirstDeclIndex);
1918 Record.push_back(FDI->second->DeclIDs.size());
1919 } else {
1920 Record.push_back(0);
1921 Record.push_back(0);
1922 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001923
1924 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
1925
Richard Smitha8cfffa2015-11-26 02:04:16 +00001926 if (Content->BufferOverridden || Content->IsTransient) {
Mehdi Amini57a41912015-09-10 01:46:39 +00001927 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
Douglas Gregor9dc32122011-11-16 20:05:18 +00001928 const llvm::MemoryBuffer *Buffer
1929 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1930 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1931 StringRef(Buffer->getBufferStart(),
1932 Buffer->getBufferSize() + 1));
1933 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001934 } else {
1935 // The source location entry is a buffer. The blob associated
1936 // with this entry contains the contents of the buffer.
1937
1938 // We add one to the size so that we capture the trailing NULL
1939 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1940 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001941 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001942 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001943 const char *Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001944 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001945 StringRef(Name, strlen(Name) + 1));
Mehdi Amini57a41912015-09-10 01:46:39 +00001946 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
Douglas Gregor258ae542009-04-27 06:38:32 +00001947 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001948 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001949 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001950
Douglas Gregor925296b2011-07-19 16:10:42 +00001951 if (strcmp(Name, "<built-in>") == 0) {
1952 PreloadSLocs.push_back(SLocEntryOffsets.size());
1953 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001954 }
1955 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001956 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001957 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001958 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1959 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001960 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1961 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001962
1963 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001964 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001965 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001966 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001967 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001968 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001969 }
1970 }
1971
Douglas Gregor8f45df52009-04-16 22:23:12 +00001972 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001973
1974 if (SLocEntryOffsets.empty())
1975 return;
1976
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001977 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001978 // table is used for lazily loading source-location information.
1979 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001980
1981 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001982 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1986 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00001987 {
1988 RecordData::value_type Record[] = {
1989 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
1990 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
1991 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
1992 bytes(SLocEntryOffsets));
1993 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001994 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001995 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001996 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001997
1998 // Write the line table. It depends on remapping working, so it must come
1999 // after the source location offsets.
2000 if (SourceMgr.hasLineTable()) {
2001 LineTableInfo &LineTable = SourceMgr.getLineTable();
2002
2003 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002004
2005 // Emit the needed file names.
2006 llvm::DenseMap<int, int> FilenameMap;
2007 for (const auto &L : LineTable) {
2008 if (L.first.ID < 0)
2009 continue;
2010 for (auto &LE : L.second) {
2011 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2012 FilenameMap.size())).second)
2013 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2014 }
2015 }
2016 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002017
2018 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002019 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002020 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002021 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002022 continue;
2023
2024 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002025 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002026
2027 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002028 Record.push_back(L.second.size());
2029 for (const auto &LE : L.second) {
2030 Record.push_back(LE.FileOffset);
2031 Record.push_back(LE.LineNo);
2032 Record.push_back(FilenameMap[LE.FilenameID]);
2033 Record.push_back((unsigned)LE.FileKind);
2034 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002035 }
2036 }
Richard Smith63078492015-09-01 07:41:55 +00002037
Douglas Gregor925296b2011-07-19 16:10:42 +00002038 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2039 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002040}
2041
Douglas Gregorc5046832009-04-27 18:38:38 +00002042//===----------------------------------------------------------------------===//
2043// Preprocessor Serialization
2044//===----------------------------------------------------------------------===//
2045
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002046static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2047 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002048 if (MacroInfo *MI = MD->getMacroInfo())
2049 if (MI->isBuiltinMacro())
2050 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002051
2052 if (IsModule) {
2053 SourceLocation Loc = MD->getLocation();
2054 if (Loc.isInvalid())
2055 return true;
2056 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2057 return true;
2058 }
2059
2060 return false;
2061}
2062
Chris Lattnereeffaef2009-04-10 17:15:23 +00002063/// \brief Writes the block containing the serialized form of the
2064/// preprocessor.
2065///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002066void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002067 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2068 if (PPRec)
2069 WritePreprocessorDetail(*PPRec);
2070
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002071 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002072 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002073
Chris Lattner0af3ba12009-04-13 01:29:17 +00002074 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2075 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002076 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002077 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002078 }
2079
2080 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002081 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002082
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002083 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002084 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002085 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002086 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002087
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002088 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002089 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002090
Richard Smithee977932015-05-01 21:22:17 +00002091 // Construct the list of identifiers with macro directives that need to be
2092 // serialized.
2093 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2094 for (auto &Id : PP.getIdentifierTable())
2095 if (Id.second->hadMacroDefinition() &&
2096 (!Id.second->isFromAST() ||
2097 Id.second->hasChangedSinceDeserialization()))
2098 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002099 // Sort the set of macro definitions that need to be serialized by the
2100 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002101 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2102 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002103
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002104 // Emit the macro directives as a list and associate the offset with the
2105 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002106 for (const IdentifierInfo *Name : MacroIdentifiers) {
2107 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002108 auto StartOffset = Stream.GetCurrentBitNo();
2109
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002110 // Emit the macro directives in reverse source order.
2111 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002112 // Once we hit an ignored macro, we're done: the rest of the chain
2113 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002114 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002115 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002116
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002117 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002118 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002119 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002120 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002121 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002122 Record.push_back(VisMD->isPublic());
2123 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002124 }
Richard Smithd7329392015-04-21 21:46:32 +00002125
Richard Smithb8b2ed62015-04-23 18:18:26 +00002126 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002127 bool EmittedModuleMacros = false;
Richard Smithb8b2ed62015-04-23 18:18:26 +00002128 if (IsModule) {
2129 auto Leafs = PP.getLeafModuleMacros(Name);
2130 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2131 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2132 while (!Worklist.empty()) {
2133 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002134
Richard Smithb8b2ed62015-04-23 18:18:26 +00002135 // Emit a record indicating this submodule exports this macro.
2136 ModuleMacroRecord.push_back(
2137 getSubmoduleID(Macro->getOwningModule()));
Richard Smithee977932015-05-01 21:22:17 +00002138 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
Richard Smithb8b2ed62015-04-23 18:18:26 +00002139 for (auto *M : Macro->overrides())
2140 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002141
Richard Smithb8b2ed62015-04-23 18:18:26 +00002142 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2143 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002144
Richard Smithb8b2ed62015-04-23 18:18:26 +00002145 // Enqueue overridden macros once we've visited all their ancestors.
2146 for (auto *M : Macro->overrides())
2147 if (++Visits[M] == M->getNumOverridingMacros())
2148 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002149
2150 EmittedModuleMacros = true;
Richard Smithd7329392015-04-21 21:46:32 +00002151 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002152 }
Richard Smithd7329392015-04-21 21:46:32 +00002153
Richard Smithee977932015-05-01 21:22:17 +00002154 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002155 continue;
2156
Richard Smithd7329392015-04-21 21:46:32 +00002157 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002158 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2159 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002160 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002161
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002162 /// \brief Offsets of each of the macros into the bitstream, indexed by
2163 /// the local macro ID
2164 ///
2165 /// For each identifier that is associated with a macro, this map
2166 /// provides the offset into the bitstream where that macro is
2167 /// defined.
2168 std::vector<uint32_t> MacroOffsets;
2169
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002170 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2171 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2172 MacroInfo *MI = MacroInfosToEmit[I].MI;
2173 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002174
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002175 if (ID < FirstMacroID) {
2176 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2177 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002178 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002179
2180 // Record the local offset of this macro.
2181 unsigned Index = ID - FirstMacroID;
2182 if (Index == MacroOffsets.size())
2183 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2184 else {
2185 if (Index > MacroOffsets.size())
2186 MacroOffsets.resize(Index + 1);
2187
2188 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2189 }
2190
2191 AddIdentifierRef(Name, Record);
2192 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2193 AddSourceLocation(MI->getDefinitionLoc(), Record);
2194 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2195 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002196 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002197 unsigned Code;
2198 if (MI->isObjectLike()) {
2199 Code = PP_MACRO_OBJECT_LIKE;
2200 } else {
2201 Code = PP_MACRO_FUNCTION_LIKE;
2202
2203 Record.push_back(MI->isC99Varargs());
2204 Record.push_back(MI->isGNUVarargs());
2205 Record.push_back(MI->hasCommaPasting());
2206 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002207 for (const IdentifierInfo *Arg : MI->args())
2208 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002209 }
2210
2211 // If we have a detailed preprocessing record, record the macro definition
2212 // ID that corresponds to this macro.
2213 if (PPRec)
2214 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2215
2216 Stream.EmitRecord(Code, Record);
2217 Record.clear();
2218
2219 // Emit the tokens array.
2220 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2221 // Note that we know that the preprocessor does not have any annotation
2222 // tokens in it because they are created by the parser, and thus can't
2223 // be in a macro definition.
2224 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002225 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002226 Stream.EmitRecord(PP_TOKEN, Record);
2227 Record.clear();
2228 }
2229 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002230 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002231
Douglas Gregor92a96f52011-02-08 21:58:10 +00002232 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002233
2234 // Write the offsets table for macro IDs.
2235 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002236
Richard Smith13090a22015-04-10 02:02:24 +00002237 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002238 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2242
2243 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002244 {
2245 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2246 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2247 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2248 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002249}
2250
2251void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002252 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002253 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002254
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002255 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002256
Douglas Gregor92a96f52011-02-08 21:58:10 +00002257 // Enter the preprocessor block.
2258 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002259
Douglas Gregoraae92242010-03-19 21:51:54 +00002260 // If the preprocessor has a preprocessing record, emit it.
2261 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002262 using namespace llvm;
2263
2264 // Set up the abbreviation for
2265 unsigned InclusionAbbrev = 0;
2266 {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002267 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002268 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2274 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2275 }
2276
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002277 unsigned FirstPreprocessorEntityID
2278 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2279 + NUM_PREDEF_PP_ENTITY_IDS;
2280 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002281 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002282 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2283 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002284 E != EEnd;
2285 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002286 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002287
Richard Smith66a81862015-05-04 02:25:31 +00002288 PreprocessedEntityOffsets.push_back(
2289 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002290
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002291 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002292 // Record this macro definition's ID.
2293 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002294
Douglas Gregor92a96f52011-02-08 21:58:10 +00002295 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002296 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2297 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002298 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002299
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002300 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002301 Record.push_back(ME->isBuiltinMacro());
2302 if (ME->isBuiltinMacro())
2303 AddIdentifierRef(ME->getName(), Record);
2304 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002305 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002306 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002307 continue;
2308 }
2309
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002310 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002311 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002312 Record.push_back(ID->getFileName().size());
2313 Record.push_back(ID->wasInQuotes());
2314 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002315 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002316 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002317 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002318 // Check that the FileEntry is not null because it was not resolved and
2319 // we create a PCH even with compiler errors.
2320 if (ID->getFile())
2321 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002322 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002323 continue;
2324 }
2325
2326 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2327 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002328 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002329
Douglas Gregoraae92242010-03-19 21:51:54 +00002330 // Write the offsets table for the preprocessing record.
2331 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002332 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2333
Douglas Gregoraae92242010-03-19 21:51:54 +00002334 // Write the offsets table for identifier IDs.
2335 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002336
2337 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002338 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002341 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002342
Mehdi Amini57a41912015-09-10 01:46:39 +00002343 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2344 FirstPreprocessorEntityID -
2345 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002346 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002347 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002348 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002349}
2350
Richard Smith386bb072015-08-18 23:42:23 +00002351unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002352 if (!Mod)
2353 return 0;
2354
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002355 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2356 if (Known != SubmoduleIDs.end())
2357 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002358
2359 if (Mod->getTopLevelModule() != WritingModule)
2360 return 0;
2361
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002362 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2363}
2364
Richard Smith386bb072015-08-18 23:42:23 +00002365unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2366 // FIXME: This can easily happen, if we have a reference to a submodule that
2367 // did not result in us loading a module file for that submodule. For
2368 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2369 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2370 assert((ID || !Mod) &&
2371 "asked for module ID for non-local, non-imported module");
2372 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002373}
2374
Douglas Gregor253eefe2011-12-01 00:59:36 +00002375/// \brief Compute the number of modules within the given tree (including the
2376/// given module).
2377static unsigned getNumberOfModules(Module *Mod) {
2378 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002379 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002380 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002381 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002382
2383 return ChildModules + 1;
2384}
2385
Douglas Gregorde3ef502011-11-30 23:21:26 +00002386void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002387 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002388 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002389
2390 // Write the abbreviations needed for the submodules block.
2391 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002392
2393 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor69021972011-11-30 17:33:56 +00002394 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2406 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2407
2408 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002409 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2411 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2412
2413 Abbrev = new BitCodeAbbrev();
2414 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2416 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002417
2418 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002419 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2421 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2422
2423 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002424 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2426 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2427
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002428 Abbrev = new BitCodeAbbrev();
2429 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002432 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2433
Douglas Gregor59527662012-10-15 06:28:11 +00002434 Abbrev = new BitCodeAbbrev();
2435 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2437 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2438
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002439 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002440 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2442 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2443
2444 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002445 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2447 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2448
2449 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002450 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2452 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2453
2454 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002455 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2458 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2459
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002460 Abbrev = new BitCodeAbbrev();
2461 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2463 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2464
Douglas Gregorfb912652013-03-20 21:10:35 +00002465 Abbrev = new BitCodeAbbrev();
2466 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2467 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2468 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2469 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2470
Douglas Gregor253eefe2011-12-01 00:59:36 +00002471 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002472 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2473 FirstSubmoduleID -
2474 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002475 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2476
Douglas Gregor69021972011-11-30 17:33:56 +00002477 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002478 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002479 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002480 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002481 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002482 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002483 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002484
2485 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002486 if (Mod->Parent) {
2487 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002488 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002489 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002490
2491 // Emit the definition of the block.
2492 {
2493 RecordData::value_type Record[] = {
2494 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2495 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2496 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2497 Mod->ConfigMacrosExhaustive};
2498 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2499 }
2500
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002501 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002502 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002503 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002504 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002505 }
2506
Douglas Gregor69021972011-11-30 17:33:56 +00002507 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002508 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002509 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002510 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2511 UmbrellaHeader.NameAsWritten);
2512 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002513 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2514 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002515 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002516 }
Richard Smith306d8922014-10-22 23:50:56 +00002517
Douglas Gregor69021972011-11-30 17:33:56 +00002518 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002519 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002520 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002521 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002522 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002523 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002524 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2525 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2526 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002527 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002528 Module::HK_PrivateTextual},
2529 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002530 };
2531 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002532 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002533 for (auto &H : Mod->Headers[HL.HeaderKind])
2534 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2535 }
2536
2537 // Emit the top headers.
2538 {
2539 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002540 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002541 for (auto *H : TopHeaders)
2542 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002543 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002544
2545 // Emit the imports.
2546 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002547 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002548 for (auto *I : Mod->Imports)
2549 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002550 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2551 }
2552
Douglas Gregor24bb9232011-12-02 18:58:38 +00002553 // Emit the exports.
2554 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002555 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002556 for (const auto &E : Mod->Exports) {
2557 // FIXME: This may fail; we don't require that all exported modules
2558 // are local or imported.
2559 Record.push_back(getSubmoduleID(E.getPointer()));
2560 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002561 }
2562 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2563 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002564
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002565 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2566 // Might be unnecessary as use declarations are only used to build the
2567 // module itself.
2568
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002569 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002570 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002571 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2572 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002573 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002574 }
2575
Douglas Gregorfb912652013-03-20 21:10:35 +00002576 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002577 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002578 // FIXME: This may fail; we don't require that all conflicting modules
2579 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002580 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2581 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002582 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002583 }
2584
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002585 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002586 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002587 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002588 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002589 }
2590
Douglas Gregor69021972011-11-30 17:33:56 +00002591 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002592 for (auto *M : Mod->submodules())
2593 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002594 }
2595
2596 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002597
Richard Smith23d8d032015-05-14 00:45:20 +00002598 assert((NextSubmoduleID - FirstSubmoduleID ==
2599 getNumberOfModules(WritingModule)) &&
2600 "Wrong # of submodules; found a reference to a non-local, "
2601 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002602}
2603
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002604serialization::SubmoduleID
2605ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002606 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002607 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002608
2609 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002610 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002611 Module *OwningMod
2612 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002613 if (!OwningMod)
2614 return 0;
2615
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002616 // Check whether this submodule is part of our own module.
2617 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002618 return 0;
2619
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002620 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002621}
2622
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002623void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2624 bool isModule) {
2625 // Make sure set diagnostic pragmas don't affect the translation unit that
2626 // imports the module.
2627 // FIXME: Make diagnostic pragma sections work properly with modules.
2628 if (isModule)
2629 return;
2630
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002631 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2632 DiagStateIDMap;
2633 unsigned CurrID = 0;
2634 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002635 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002636 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002637 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2638 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002639 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002640 if (point.Loc.isInvalid())
2641 continue;
2642
2643 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002644 unsigned &DiagStateID = DiagStateIDMap[point.State];
2645 Record.push_back(DiagStateID);
2646
2647 if (DiagStateID == 0) {
2648 DiagStateID = ++CurrID;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002649 for (const auto &I : *(point.State)) {
2650 if (I.second.isPragma()) {
2651 Record.push_back(I.first);
2652 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002653 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002654 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002655 Record.push_back(-1); // mark the end of the diag/map pairs for this
2656 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002657 }
2658 }
2659
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002660 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002661 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002662}
2663
Richard Smithc2bb8182015-03-24 06:36:48 +00002664void ASTWriter::WriteCXXCtorInitializersOffsets() {
2665 if (CXXCtorInitializersOffsets.empty())
2666 return;
2667
Richard Smithc2bb8182015-03-24 06:36:48 +00002668 // Create a blob abbreviation for the C++ ctor initializer offsets.
2669 using namespace llvm;
2670
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002671 auto *Abbrev = new BitCodeAbbrev();
Richard Smithc2bb8182015-03-24 06:36:48 +00002672 Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2673 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2674 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2675 unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2676
2677 // Write the base specifier offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00002678 RecordData::value_type Record[] = {CXX_CTOR_INITIALIZERS_OFFSETS,
2679 CXXCtorInitializersOffsets.size()};
Richard Smithc2bb8182015-03-24 06:36:48 +00002680 Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002681 bytes(CXXCtorInitializersOffsets));
Richard Smithc2bb8182015-03-24 06:36:48 +00002682}
2683
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002684void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2685 if (CXXBaseSpecifiersOffsets.empty())
2686 return;
2687
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002688 // Create a blob abbreviation for the C++ base specifiers offsets.
2689 using namespace llvm;
2690
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002691 auto *Abbrev = new BitCodeAbbrev();
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002692 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2693 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2694 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2695 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2696
Douglas Gregorc27b2872011-08-04 00:01:48 +00002697 // Write the base specifier offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00002698 RecordData::value_type Record[] = {CXX_BASE_SPECIFIER_OFFSETS,
2699 CXXBaseSpecifiersOffsets.size()};
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002700 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002701 bytes(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002702}
2703
Douglas Gregorc5046832009-04-27 18:38:38 +00002704//===----------------------------------------------------------------------===//
2705// Type Serialization
2706//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002707
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002708/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002709void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002710 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002711 if (Idx.getIndex() == 0) // we haven't seen this type before.
2712 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002713
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002714 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002715
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002716 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002717 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002718 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002719 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002720 else if (TypeOffsets.size() < Index) {
2721 TypeOffsets.resize(Index + 1);
2722 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002723 }
2724
2725 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002726
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002728 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002729 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002730
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002731 if (T.hasLocalNonFastQualifiers()) {
2732 Qualifiers Qs = T.getLocalQualifiers();
2733 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002734 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002735 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002736 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002737 } else {
2738 switch (T->getTypeClass()) {
2739 // For all of the concrete, non-dependent types, call the
2740 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002741#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002742 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002743#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002744#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002745 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002746 }
2747
2748 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002749 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002750
2751 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002752 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002753}
2754
Douglas Gregorc5046832009-04-27 18:38:38 +00002755//===----------------------------------------------------------------------===//
2756// Declaration Serialization
2757//===----------------------------------------------------------------------===//
2758
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002759/// \brief Write the block containing all of the declaration IDs
2760/// lexically declared within the given DeclContext.
2761///
2762/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2763/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002764uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002765 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002766 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002767 return 0;
2768
Douglas Gregor8f45df52009-04-16 22:23:12 +00002769 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002770 SmallVector<uint32_t, 128> KindDeclPairs;
2771 for (const auto *D : DC->decls()) {
2772 KindDeclPairs.push_back(D->getKind());
2773 KindDeclPairs.push_back(GetDeclRef(D));
2774 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002775
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002776 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002777 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002778 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2779 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002780 return Offset;
2781}
2782
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002783void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002784 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002785
2786 // Write the type offsets array
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002787 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002788 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2792 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002793 {
2794 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2795 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2796 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2797 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002798
2799 // Write the declaration offsets array
2800 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002801 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002802 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002803 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2805 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002806 {
2807 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2808 FirstDeclID - NUM_PREDEF_DECL_IDS};
2809 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2810 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002811}
2812
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002813void ASTWriter::WriteFileDeclIDsMap() {
2814 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002815
Chandler Carruthb306d732015-03-27 00:31:20 +00002816 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2817 FileDeclIDs.begin(), FileDeclIDs.end());
2818 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2819 llvm::less_first());
2820
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002821 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002822 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2823 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2824 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2825 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2826 for (auto &LocDeclEntry : Info.DeclIDs)
2827 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002828 }
2829
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002830 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002831 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2834 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002835 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2836 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002837 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002838}
2839
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002840void ASTWriter::WriteComments() {
2841 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002842 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002843 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002844 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002845 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002846 AddSourceRange(I->getSourceRange(), Record);
2847 Record.push_back(I->getKind());
2848 Record.push_back(I->isTrailingComment());
2849 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002850 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2851 }
2852 Stream.ExitBlock();
2853}
2854
Douglas Gregorc5046832009-04-27 18:38:38 +00002855//===----------------------------------------------------------------------===//
2856// Global Method Pool and Selector Serialization
2857//===----------------------------------------------------------------------===//
2858
Douglas Gregore84a9da2009-04-20 20:36:09 +00002859namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002860// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002861class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002862 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002863
2864public:
2865 typedef Selector key_type;
2866 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002867
Sebastian Redl834bb972010-08-04 17:20:04 +00002868 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002869 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002870 ObjCMethodList Instance, Factory;
2871 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002872 typedef const data_type& data_type_ref;
2873
Justin Bogner25463f12014-04-18 20:27:24 +00002874 typedef unsigned hash_value_type;
2875 typedef unsigned offset_type;
2876
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002877 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002878
Justin Bogner25463f12014-04-18 20:27:24 +00002879 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002880 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002881 }
Mike Stump11289f42009-09-09 15:08:12 +00002882
2883 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002884 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002885 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002886 using namespace llvm::support;
2887 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002888 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002889 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002890 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2891 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002892 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002893 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002894 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002895 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002896 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002897 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002898 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002899 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002900 return std::make_pair(KeyLen, DataLen);
2901 }
Mike Stump11289f42009-09-09 15:08:12 +00002902
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002903 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002904 using namespace llvm::support;
2905 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002906 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002907 assert((Start >> 32) == 0 && "Selector key offset too large");
2908 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002909 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002910 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002911 if (N == 0)
2912 N = 1;
2913 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002914 LE.write<uint32_t>(
2915 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002916 }
Mike Stump11289f42009-09-09 15:08:12 +00002917
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002918 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002919 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002920 using namespace llvm::support;
2921 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002922 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002923 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002924 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002925 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002926 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002927 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002928 ++NumInstanceMethods;
2929
2930 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002931 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002932 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002933 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002934 ++NumFactoryMethods;
2935
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002936 unsigned InstanceBits = Methods.Instance.getBits();
2937 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002938 unsigned InstanceHasMoreThanOneDeclBit =
2939 Methods.Instance.hasMoreThanOneDecl();
2940 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2941 (InstanceHasMoreThanOneDeclBit << 2) |
2942 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002943 unsigned FactoryBits = Methods.Factory.getBits();
2944 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002945 unsigned FactoryHasMoreThanOneDeclBit =
2946 Methods.Factory.hasMoreThanOneDecl();
2947 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2948 (FactoryHasMoreThanOneDeclBit << 2) |
2949 FactoryBits;
2950 LE.write<uint16_t>(FullInstanceBits);
2951 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002952 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002953 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002954 if (Method->getMethod())
2955 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002956 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002957 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002958 if (Method->getMethod())
2959 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002960
2961 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002962 }
2963};
2964} // end anonymous namespace
2965
Sebastian Redla19a67f2010-08-03 21:58:15 +00002966/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002967///
2968/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002969/// in an on-disk hash table indexed by the selector. The hash table also
2970/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002971void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002972 using namespace llvm;
2973
Sebastian Redla19a67f2010-08-03 21:58:15 +00002974 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002975 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002976 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002977 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002978 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002979 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002980 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002981 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002982
Sebastian Redla19a67f2010-08-03 21:58:15 +00002983 // Create the on-disk hash table representation. We walk through every
2984 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002985 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002986 for (auto &SelectorAndID : SelectorIDs) {
2987 Selector S = SelectorAndID.first;
2988 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002989 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002990 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002991 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00002992 ObjCMethodList(),
2993 ObjCMethodList()
2994 };
2995 if (F != SemaRef.MethodPool.end()) {
2996 Data.Instance = F->second.first;
2997 Data.Factory = F->second.second;
2998 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002999 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003000 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003001 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003002 // Selector already exists. Did it change?
3003 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003004 for (ObjCMethodList *M = &Data.Instance;
3005 !changed && M && M->getMethod(); M = M->getNext()) {
3006 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003007 changed = true;
3008 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003009 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003010 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003011 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003012 changed = true;
3013 }
3014 if (!changed)
3015 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003016 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003017 // A new method pool entry.
3018 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003019 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003020 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003021 }
3022
Douglas Gregorc78d3462009-04-24 21:10:55 +00003023 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003024 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003025 uint32_t BucketOffset;
3026 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003027 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003028 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003029 llvm::raw_svector_ostream Out(MethodPool);
3030 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003031 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003032 BucketOffset = Generator.Emit(Out, Trait);
3033 }
3034
3035 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003036 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003037 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3041 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3042
Douglas Gregor95c13f52009-04-25 17:48:32 +00003043 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003044 {
3045 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3046 NumTableEntries};
3047 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3048 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003049
3050 // Create a blob abbreviation for the selector table offsets.
3051 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003052 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3056 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3057
3058 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003059 {
3060 RecordData::value_type Record[] = {
3061 SELECTOR_OFFSETS, SelectorOffsets.size(),
3062 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3063 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3064 bytes(SelectorOffsets));
3065 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003066 }
3067}
3068
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003069/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003070void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003071 using namespace llvm;
3072 if (SemaRef.ReferencedSelectors.empty())
3073 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003074
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003075 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003076
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003077 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003078 // very tricky to fix, and given that @selector shouldn't really appear in
3079 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003080 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3081 Selector Sel = SelectorAndLocation.first;
3082 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003083 AddSelectorRef(Sel, Record);
3084 AddSourceLocation(Loc, Record);
3085 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003086 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003087}
3088
Douglas Gregorc5046832009-04-27 18:38:38 +00003089//===----------------------------------------------------------------------===//
3090// Identifier Table Serialization
3091//===----------------------------------------------------------------------===//
3092
Richard Smithb3761912015-02-05 23:08:52 +00003093/// Determine the declaration that should be put into the name lookup table to
3094/// represent the given declaration in this module. This is usually D itself,
3095/// but if D was imported and merged into a local declaration, we want the most
3096/// recent local declaration instead. The chosen declaration will be the most
3097/// recent declaration in any module that imports this one.
3098static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3099 NamedDecl *D) {
3100 if (!LangOpts.Modules || !D->isFromASTFile())
3101 return D;
3102
3103 if (Decl *Redecl = D->getPreviousDecl()) {
3104 // For Redeclarable decls, a prior declaration might be local.
3105 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3106 if (!Redecl->isFromASTFile())
3107 return cast<NamedDecl>(Redecl);
Richard Smithfe620d22015-03-05 23:24:12 +00003108 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003109 // local one.
3110 if (D->getOwningModuleID() == 0)
3111 break;
3112 }
3113 } else if (Decl *First = D->getCanonicalDecl()) {
3114 // For Mergeable decls, the first decl might be local.
3115 if (!First->isFromASTFile())
3116 return cast<NamedDecl>(First);
3117 }
3118
3119 // All declarations are imported. Our most recent declaration will also be
3120 // the most recent one in anyone who imports us.
3121 return D;
3122}
3123
Douglas Gregorc78d3462009-04-24 21:10:55 +00003124namespace {
Richard Smithcf97cf62015-04-19 01:34:23 +00003125class ASTIdentifierTableTrait {
3126 ASTWriter &Writer;
3127 Preprocessor &PP;
3128 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003129 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003130 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003131 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003132
3133 /// \brief Determines whether this is an "interesting" identifier that needs a
3134 /// full IdentifierInfo structure written into the hash table. Notably, this
3135 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3136 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003137 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003138 if (MacroOffset ||
3139 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003140 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003141 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003142 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003143 return true;
3144
3145 return false;
3146 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003147
Douglas Gregore84a9da2009-04-20 20:36:09 +00003148public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003149 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003150 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003151
Sebastian Redl539c5062010-08-18 23:57:32 +00003152 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003153 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003154
Justin Bogner25463f12014-04-18 20:27:24 +00003155 typedef unsigned hash_value_type;
3156 typedef unsigned offset_type;
3157
Richard Smithd7329392015-04-21 21:46:32 +00003158 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003159 IdentifierResolver &IdResolver, bool IsModule,
3160 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003161 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003162 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3163 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003164
Justin Bogner25463f12014-04-18 20:27:24 +00003165 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003166 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003167 }
Mike Stump11289f42009-09-09 15:08:12 +00003168
Richard Smith33e0f7e2015-07-22 02:08:40 +00003169 bool isInterestingIdentifier(const IdentifierInfo *II) {
3170 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3171 return isInterestingIdentifier(II, MacroOffset);
3172 }
Richard Smith9c254182015-07-19 21:41:12 +00003173 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3174 return isInterestingIdentifier(II, 0);
3175 }
3176
Mike Stump11289f42009-09-09 15:08:12 +00003177 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003178 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003179 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003180 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003181 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3182 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003183 DataLen += 2; // 2 bytes for builtin ID
3184 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003185 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003186 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003187
Richard Smitha534a312015-07-21 23:54:07 +00003188 if (NeedDecls) {
3189 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3190 DEnd = IdResolver.end();
3191 D != DEnd; ++D)
3192 DataLen += 4;
3193 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003194 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003195 using namespace llvm::support;
3196 endian::Writer<little> LE(Out);
3197
Richard Smithdf8a8312015-03-13 04:05:01 +00003198 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003199 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003200 // We emit the key length after the data length so that every
3201 // string is preceded by a 16-bit length. This matches the PTH
3202 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003203 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003204 return std::make_pair(KeyLen, DataLen);
3205 }
Mike Stump11289f42009-09-09 15:08:12 +00003206
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003207 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003208 unsigned KeyLen) {
3209 // Record the location of the key data. This is used when generating
3210 // the mapping from persistent IDs to strings.
3211 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003212
3213 // Emit the offset of the key/data length information to the interesting
3214 // identifiers table if necessary.
3215 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3216 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3217
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003218 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003219 }
Mike Stump11289f42009-09-09 15:08:12 +00003220
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003221 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003222 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003223 using namespace llvm::support;
3224 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003225
Richard Smithd7329392015-04-21 21:46:32 +00003226 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3227 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003228 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003229 return;
3230 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003231
Justin Bognere1c147c2014-03-28 22:03:19 +00003232 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003233 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3234 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003235 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003236 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003237 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003238 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003239 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3240 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003241 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003242 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003243 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003244 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003245
Richard Smithd7329392015-04-21 21:46:32 +00003246 if (HadMacroDefinition)
3247 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003248
Richard Smitha534a312015-07-21 23:54:07 +00003249 if (NeedDecls) {
3250 // Emit the declaration IDs in reverse order, because the
3251 // IdentifierResolver provides the declarations as they would be
3252 // visible (e.g., the function "stat" would come before the struct
3253 // "stat"), but the ASTReader adds declarations to the end of the list
3254 // (so we need to see the struct "stat" before the function "stat").
3255 // Only emit declarations that aren't from a chained PCH, though.
3256 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3257 IdResolver.end());
3258 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3259 DEnd = Decls.rend();
3260 D != DEnd; ++D)
3261 LE.write<uint32_t>(
3262 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3263 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003264 }
3265};
3266} // end anonymous namespace
3267
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003268/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003269///
3270/// The identifier table consists of a blob containing string data
3271/// (the actual identifiers themselves) and a separate "offsets" index
3272/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003273void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3274 IdentifierResolver &IdResolver,
3275 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003276 using namespace llvm;
3277
Richard Smith33e0f7e2015-07-22 02:08:40 +00003278 RecordData InterestingIdents;
3279
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003280 // Create and write out the blob that contains the identifier
3281 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003282 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003283 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003284 ASTIdentifierTableTrait Trait(
3285 *this, PP, IdResolver, IsModule,
3286 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003287
Douglas Gregore6648fb2009-04-28 20:33:11 +00003288 // Look for any identifiers that were named while processing the
3289 // headers, but are otherwise not needed. We add these to the hash
3290 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003291 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003292 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003293 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003294 for (const auto &ID : PP.getIdentifierTable())
3295 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003296 // Sort the identifiers lexicographically before getting them references so
3297 // that their order is stable.
3298 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3299 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003300 if (Trait.isInterestingNonMacroIdentifier(II))
3301 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003302
Sebastian Redlff4a2952010-07-23 23:49:55 +00003303 // Create the on-disk hash table representation. We only store offsets
3304 // for identifiers that appear here for the first time.
3305 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003306 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003307 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003308 IdentID ID = IdentIDPair.second;
3309 assert(II && "NULL identifier in identifier table");
3310 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3311 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003312 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003313
Douglas Gregore84a9da2009-04-20 20:36:09 +00003314 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003315 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003316 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003317 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003318 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003319 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003320 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003321 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003322 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003323 }
3324
3325 // Create a blob abbreviation
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003326 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003327 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003330 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003331
3332 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003333 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003334 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003335 }
3336
3337 // Write the offsets table for identifier IDs.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003338 auto *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003339 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3343 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3344
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003345#ifndef NDEBUG
3346 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3347 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3348#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003349
3350 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3351 IdentifierOffsets.size(),
3352 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003353 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003354 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003355
3356 // In C++, write the list of interesting identifiers (those that are
3357 // defined as macros, poisoned, or similar unusual things).
3358 if (!InterestingIdents.empty())
3359 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003360}
3361
Douglas Gregorc5046832009-04-27 18:38:38 +00003362//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003363// DeclContext's Name Lookup Table Serialization
3364//===----------------------------------------------------------------------===//
3365
3366namespace {
3367// Trait used for the on-disk hash table used in the method pool.
3368class ASTDeclContextNameLookupTrait {
3369 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003370 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003371
3372public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003373 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003374 typedef key_type key_type_ref;
3375
Richard Smithd88a7f12015-09-01 20:35:42 +00003376 /// A start and end index into DeclIDs, representing a sequence of decls.
3377 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003378 typedef const data_type& data_type_ref;
3379
Justin Bogner25463f12014-04-18 20:27:24 +00003380 typedef unsigned hash_value_type;
3381 typedef unsigned offset_type;
3382
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003383 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3384
Richard Smithd88a7f12015-09-01 20:35:42 +00003385 template<typename Coll>
3386 data_type getData(const Coll &Decls) {
3387 unsigned Start = DeclIDs.size();
3388 for (NamedDecl *D : Decls) {
3389 DeclIDs.push_back(
3390 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3391 }
3392 return std::make_pair(Start, DeclIDs.size());
3393 }
3394
3395 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3396 unsigned Start = DeclIDs.size();
3397 for (auto ID : FromReader)
3398 DeclIDs.push_back(ID);
3399 return std::make_pair(Start, DeclIDs.size());
3400 }
3401
3402 static bool EqualKey(key_type_ref a, key_type_ref b) {
3403 return a == b;
3404 }
3405
Richard Smitha06c7e62015-08-26 23:55:49 +00003406 hash_value_type ComputeHash(DeclarationNameKey Name) {
3407 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003408 }
3409
Richard Smithd88a7f12015-09-01 20:35:42 +00003410 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3411 assert(Writer.hasChain() &&
3412 "have reference to loaded module file but no chain?");
3413
3414 using namespace llvm::support;
3415 endian::Writer<little>(Out)
3416 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3417 }
3418
Richard Smitha06c7e62015-08-26 23:55:49 +00003419 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3420 DeclarationNameKey Name,
3421 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003422 using namespace llvm::support;
3423 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003424 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003425 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003426 case DeclarationName::Identifier:
3427 case DeclarationName::ObjCZeroArgSelector:
3428 case DeclarationName::ObjCOneArgSelector:
3429 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003430 case DeclarationName::CXXLiteralOperatorName:
3431 KeyLen += 4;
3432 break;
3433 case DeclarationName::CXXOperatorName:
3434 KeyLen += 1;
3435 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003436 case DeclarationName::CXXConstructorName:
3437 case DeclarationName::CXXDestructorName:
3438 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003439 case DeclarationName::CXXUsingDirective:
3440 break;
3441 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003442 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003443
Richard Smithf02662d2015-07-30 03:17:16 +00003444 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003445 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3446 assert(uint16_t(DataLen) == DataLen &&
3447 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003448 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003449
3450 return std::make_pair(KeyLen, DataLen);
3451 }
3452
Richard Smitha06c7e62015-08-26 23:55:49 +00003453 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003454 using namespace llvm::support;
3455 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003456 LE.write<uint8_t>(Name.getKind());
3457 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003458 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003459 case DeclarationName::CXXLiteralOperatorName:
3460 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003461 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003462 case DeclarationName::ObjCZeroArgSelector:
3463 case DeclarationName::ObjCOneArgSelector:
3464 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003465 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003466 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003467 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003468 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003469 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003470 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003471 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003472 case DeclarationName::CXXConstructorName:
3473 case DeclarationName::CXXDestructorName:
3474 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003475 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003476 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003477 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003478
3479 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003480 }
3481
Richard Smitha06c7e62015-08-26 23:55:49 +00003482 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3483 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003484 using namespace llvm::support;
3485 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003486 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003487 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3488 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003489 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3490 }
3491};
3492} // end anonymous namespace
3493
Chandler Carruthe972c362015-03-26 03:11:40 +00003494bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3495 DeclContext *DC) {
3496 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3497}
3498
3499bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3500 DeclContext *DC) {
3501 for (auto *D : Result.getLookupResult())
3502 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3503 return false;
3504
3505 return true;
3506}
3507
Richard Smithd88a7f12015-09-01 20:35:42 +00003508void
Chandler Carruthe972c362015-03-26 03:11:40 +00003509ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003510 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003511 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3512 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003513 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003514
Chandler Carruthe972c362015-03-26 03:11:40 +00003515 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003516 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003517 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3518
3519 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003520 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3521 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003522 ASTDeclContextNameLookupTrait Trait(*this);
3523
Chandler Carruthe972c362015-03-26 03:11:40 +00003524 // The first step is to collect the declaration names which we need to
3525 // serialize into the name lookup table, and to collect them in a stable
3526 // order.
3527 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003528
Chandler Carruthe972c362015-03-26 03:11:40 +00003529 // We also build up small sets of the constructor and conversion function
3530 // names which are visible.
3531 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003532
Chandler Carruthe972c362015-03-26 03:11:40 +00003533 for (auto &Lookup : *DC->buildLookup()) {
3534 auto &Name = Lookup.first;
3535 auto &Result = Lookup.second;
3536
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003537 // If there are no local declarations in our lookup result, we
3538 // don't need to write an entry for the name at all. If we can't
3539 // write out a lookup set without performing more deserialization,
3540 // just skip this entry.
3541 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003542 isLookupResultEntirelyExternal(Result, DC))
3543 continue;
3544
3545 // We also skip empty results. If any of the results could be external and
3546 // the currently available results are empty, then all of the results are
3547 // external and we skip it above. So the only way we get here with an empty
3548 // results is when no results could have been external *and* we have
3549 // external results.
3550 //
3551 // FIXME: While we might want to start emitting on-disk entries for negative
3552 // lookups into a decl context as an optimization, today we *have* to skip
3553 // them because there are names with empty lookup results in decl contexts
3554 // which we can't emit in any stable ordering: we lookup constructors and
3555 // conversion functions in the enclosing namespace scope creating empty
3556 // results for them. This in almost certainly a bug in Clang's name lookup,
3557 // but that is likely to be hard or impossible to fix and so we tolerate it
3558 // here by omitting lookups with empty results.
3559 if (Lookup.second.getLookupResult().empty())
3560 continue;
3561
3562 switch (Lookup.first.getNameKind()) {
3563 default:
3564 Names.push_back(Lookup.first);
3565 break;
3566
Richard Smithcd45dbc2014-04-19 03:48:30 +00003567 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003568 assert(isa<CXXRecordDecl>(DC) &&
3569 "Cannot have a constructor name outside of a class!");
3570 ConstructorNameSet.insert(Name);
3571 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003572
3573 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003574 assert(isa<CXXRecordDecl>(DC) &&
3575 "Cannot have a conversion function name outside of a class!");
3576 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003577 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003578 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003579 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003580
Chandler Carruthe972c362015-03-26 03:11:40 +00003581 // Sort the names into a stable order.
3582 std::sort(Names.begin(), Names.end());
3583
Chandler Carruthffbf7052015-03-26 22:27:09 +00003584 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003585 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003586 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003587
Chandler Carruthffbf7052015-03-26 22:27:09 +00003588 // First we try the easy case by forming the current context's constructor
3589 // name and adding that name first. This is a very useful optimization to
3590 // avoid walking the lexical declarations in many cases, and it also
3591 // handles the only case where a constructor name can come from some other
3592 // lexical context -- when that name is an implicit constructor merged from
3593 // another declaration in the redecl chain. Any non-implicit constructor or
3594 // conversion function which doesn't occur in all the lexical contexts
3595 // would be an ODR violation.
3596 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3597 Context->getCanonicalType(Context->getRecordType(D)));
3598 if (ConstructorNameSet.erase(ImplicitCtorName))
3599 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003600
Chandler Carruthffbf7052015-03-26 22:27:09 +00003601 // If we still have constructors or conversion functions, we walk all the
3602 // names in the decl and add the constructors and conversion functions
3603 // which are visible in the order they lexically occur within the context.
3604 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3605 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3606 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3607 auto Name = ChildND->getDeclName();
3608 switch (Name.getNameKind()) {
3609 default:
3610 continue;
3611
3612 case DeclarationName::CXXConstructorName:
3613 if (ConstructorNameSet.erase(Name))
3614 Names.push_back(Name);
3615 break;
3616
3617 case DeclarationName::CXXConversionFunctionName:
3618 if (ConversionNameSet.erase(Name))
3619 Names.push_back(Name);
3620 break;
3621 }
3622
3623 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3624 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003625 }
3626
Chandler Carruthe972c362015-03-26 03:11:40 +00003627 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3628 "constructors by walking all the "
3629 "lexical members of the context.");
3630 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3631 "conversion functions by walking all "
3632 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003633 }
3634
Chandler Carruthe972c362015-03-26 03:11:40 +00003635 // Next we need to do a lookup with each name into this decl context to fully
3636 // populate any results from external sources. We don't actually use the
3637 // results of these lookups because we only want to use the results after all
3638 // results have been loaded and the pointers into them will be stable.
3639 for (auto &Name : Names)
3640 DC->lookup(Name);
3641
3642 // Now we need to insert the results for each name into the hash table. For
3643 // constructor names and conversion function names, we actually need to merge
3644 // all of the results for them into one list of results each and insert
3645 // those.
3646 SmallVector<NamedDecl *, 8> ConstructorDecls;
3647 SmallVector<NamedDecl *, 8> ConversionDecls;
3648
3649 // Now loop over the names, either inserting them or appending for the two
3650 // special cases.
3651 for (auto &Name : Names) {
3652 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3653
3654 switch (Name.getNameKind()) {
3655 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003656 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003657 break;
3658
3659 case DeclarationName::CXXConstructorName:
3660 ConstructorDecls.append(Result.begin(), Result.end());
3661 break;
3662
3663 case DeclarationName::CXXConversionFunctionName:
3664 ConversionDecls.append(Result.begin(), Result.end());
3665 break;
3666 }
3667 }
3668
3669 // Handle our two special cases if we ended up having any. We arbitrarily use
3670 // the first declaration's name here because the name itself isn't part of
3671 // the key, only the kind of name is used.
3672 if (!ConstructorDecls.empty())
3673 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003674 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003675 if (!ConversionDecls.empty())
3676 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003677 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003678
Richard Smithd88a7f12015-09-01 20:35:42 +00003679 // Create the on-disk hash table. Also emit the existing imported and
3680 // merged table if there is one.
3681 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3682 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003683}
3684
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003685/// \brief Write the block containing all of the declaration IDs
3686/// visible from the given DeclContext.
3687///
3688/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003689/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003690uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3691 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003692 // If we imported a key declaration of this namespace, write the visible
3693 // lookup results as an update record for it rather than including them
3694 // on this declaration. We will only look at key declarations on reload.
3695 if (isa<NamespaceDecl>(DC) && Chain &&
3696 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3697 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003698 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003699 Prev = Prev->getPreviousDecl())
3700 if (!Prev->isFromASTFile())
3701 return 0;
3702
3703 // Note that we need to emit an update record for the primary context.
3704 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3705
3706 // Make sure all visible decls are written. They will be recorded later. We
3707 // do this using a side data structure so we can sort the names into
3708 // a deterministic order.
3709 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3710 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3711 LookupResults;
3712 if (Map) {
3713 LookupResults.reserve(Map->size());
3714 for (auto &Entry : *Map)
3715 LookupResults.push_back(
3716 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3717 }
3718
3719 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3720 for (auto &NameAndResult : LookupResults) {
3721 DeclarationName Name = NameAndResult.first;
3722 DeclContext::lookup_result Result = NameAndResult.second;
3723 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3724 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3725 // We have to work around a name lookup bug here where negative lookup
3726 // results for these names get cached in namespace lookup tables (these
3727 // names should never be looked up in a namespace).
3728 assert(Result.empty() && "Cannot have a constructor or conversion "
3729 "function name in a namespace!");
3730 continue;
3731 }
3732
3733 for (NamedDecl *ND : Result)
3734 if (!ND->isFromASTFile())
3735 GetDeclRef(ND);
3736 }
3737
3738 return 0;
3739 }
3740
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003741 if (DC->getPrimaryContext() != DC)
3742 return 0;
3743
Chandler Carruthe972c362015-03-26 03:11:40 +00003744 // Skip contexts which don't support name lookup.
3745 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003746 return 0;
3747
3748 // If not in C++, we perform name lookup for the translation unit via the
3749 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003750 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003751 return 0;
3752
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003753 // Serialize the contents of the mapping used for lookup. Note that,
3754 // although we have two very different code paths, the serialized
3755 // representation is the same for both cases: a declaration name,
3756 // followed by a size, followed by references to the visible
3757 // declarations that have that name.
3758 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003759 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003760 if (!Map || Map->empty())
3761 return 0;
3762
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003763 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003764 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003765 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003766
3767 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003768 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003769 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003770 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003771 ++NumVisibleDeclContexts;
3772 return Offset;
3773}
3774
Sebastian Redla4071b42010-08-24 00:50:09 +00003775/// \brief Write an UPDATE_VISIBLE block for the given context.
3776///
3777/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3778/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003779/// (in C++), for namespaces, and for classes with forward-declared unscoped
3780/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003781void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003782 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003783 if (!Map || Map->empty())
3784 return;
3785
Sebastian Redla4071b42010-08-24 00:50:09 +00003786 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003787 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003788 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003789
Richard Smith5fc18a92015-07-12 23:43:21 +00003790 // If we're updating a namespace, select a key declaration as the key for the
3791 // update record; those are the only ones that will be checked on reload.
3792 if (isa<NamespaceDecl>(DC))
3793 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3794
Sebastian Redla4071b42010-08-24 00:50:09 +00003795 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003796 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003797 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003798}
3799
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003800/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3801void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003802 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003803 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3804}
3805
3806/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3807void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003808 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003809 return;
3810
3811 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3812 RecordData Record;
3813#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3814#include "clang/Basic/OpenCLExtensions.def"
3815 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3816}
3817
Douglas Gregor404cdde2012-01-27 01:47:08 +00003818void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003819 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003820 RecordData Categories;
3821
3822 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3823 unsigned Size = 0;
3824 unsigned StartIndex = Categories.size();
3825
3826 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3827
3828 // Allocate space for the size.
3829 Categories.push_back(0);
3830
3831 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003832 for (ObjCInterfaceDecl::known_categories_iterator
3833 Cat = Class->known_categories_begin(),
3834 CatEnd = Class->known_categories_end();
3835 Cat != CatEnd; ++Cat, ++Size) {
3836 assert(getDeclID(*Cat) != 0 && "Bogus category");
3837 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003838 }
3839
3840 // Update the size.
3841 Categories[StartIndex] = Size;
3842
3843 // Record this interface -> category map.
3844 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3845 CategoriesMap.push_back(CatInfo);
3846 }
3847
3848 // Sort the categories map by the definition ID, since the reader will be
3849 // performing binary searches on this information.
3850 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3851
3852 // Emit the categories map.
3853 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003854
3855 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003856 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3858 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3859 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00003860
3861 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
3862 Stream.EmitRecordWithBlob(AbbrevID, Record,
3863 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00003864 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00003865
Douglas Gregor404cdde2012-01-27 01:47:08 +00003866 // Emit the category lists.
3867 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3868}
3869
Richard Smithe40f2ba2013-08-07 21:41:30 +00003870void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3871 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3872
3873 if (LPTMap.empty())
3874 return;
3875
3876 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00003877 for (auto LPTMapEntry : LPTMap) {
3878 const FunctionDecl *FD = LPTMapEntry.first;
3879 LateParsedTemplate *LPT = LPTMapEntry.second;
3880 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003881 AddDeclRef(LPT->D, Record);
3882 Record.push_back(LPT->Toks.size());
3883
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003884 for (const auto &Tok : LPT->Toks) {
3885 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003886 }
3887 }
3888 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3889}
3890
Dario Domizioli13a0a382014-05-23 12:13:25 +00003891/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3892void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3893 RecordData Record;
3894 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3895 AddSourceLocation(PragmaLoc, Record);
3896 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3897}
3898
Douglas Gregor8f64ca12015-12-08 22:43:32 +00003899void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
3900 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003901 // Enter the extension block.
3902 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
3903
3904 // Emit the metadata record abbreviation.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003905 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003906 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
3907 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3908 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3909 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3910 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3911 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3912 unsigned Abbrev = Stream.EmitAbbrev(Abv);
3913
3914 // Emit the metadata record.
3915 RecordData Record;
3916 auto Metadata = Writer.getExtension()->getExtensionMetadata();
3917 Record.push_back(EXTENSION_METADATA);
3918 Record.push_back(Metadata.MajorVersion);
3919 Record.push_back(Metadata.MinorVersion);
3920 Record.push_back(Metadata.BlockName.size());
3921 Record.push_back(Metadata.UserInfo.size());
3922 SmallString<64> Buffer;
3923 Buffer += Metadata.BlockName;
3924 Buffer += Metadata.UserInfo;
3925 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
3926
3927 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00003928 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003929
3930 // Exit the extension block.
3931 Stream.ExitBlock();
3932}
3933
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003934//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003935// General Serialization Routines
3936//===----------------------------------------------------------------------===//
3937
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003938/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003939void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3940 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003941 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003942 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003943 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003944 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003945
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003946#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003947
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003948 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003949}
3950
John McCallf413f5e2013-05-03 00:10:13 +00003951void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3952 AddSourceLocation(Tok.getLocation(), Record);
3953 Record.push_back(Tok.getLength());
3954
3955 // FIXME: When reading literal tokens, reconstruct the literal pointer
3956 // if it is needed.
3957 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3958 // FIXME: Should translate token kind to a stable encoding.
3959 Record.push_back(Tok.getKind());
3960 // FIXME: Should translate token flags to a stable encoding.
3961 Record.push_back(Tok.getFlags());
3962}
3963
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003964void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003965 Record.push_back(Str.size());
3966 Record.insert(Record.end(), Str.begin(), Str.end());
3967}
3968
Richard Smith7ed1bc92014-12-05 22:42:13 +00003969bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00003970 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00003971
Richard Smith54cc3c22014-12-11 20:50:24 +00003972 bool Changed =
3973 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00003974
3975 // Remove a prefix to make the path relative, if relevant.
3976 const char *PathBegin = Path.data();
3977 const char *PathPtr =
3978 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
3979 if (PathPtr != PathBegin) {
3980 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
3981 Changed = true;
3982 }
3983
3984 return Changed;
3985}
3986
3987void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
3988 SmallString<128> FilePath(Path);
3989 PreparePathForOutput(FilePath);
3990 AddString(FilePath, Record);
3991}
3992
Mehdi Amini57a41912015-09-10 01:46:39 +00003993void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00003994 StringRef Path) {
3995 SmallString<128> FilePath(Path);
3996 PreparePathForOutput(FilePath);
3997 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
3998}
3999
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004000void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4001 RecordDataImpl &Record) {
4002 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004003 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004004 Record.push_back(*Minor + 1);
4005 else
4006 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004007 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004008 Record.push_back(*Subminor + 1);
4009 else
4010 Record.push_back(0);
4011}
4012
Douglas Gregore84a9da2009-04-20 20:36:09 +00004013/// \brief Note that the identifier II occurs at the given offset
4014/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004015void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004016 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004017 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004018 // up earlier in the chain and thus don't need an offset.
4019 if (ID >= FirstIdentID)
4020 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004021}
4022
Douglas Gregor95c13f52009-04-25 17:48:32 +00004023/// \brief Note that the selector Sel occurs at the given offset
4024/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004025void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004026 unsigned ID = SelectorIDs[Sel];
4027 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004028 // Don't record offsets for selectors that are also available in a different
4029 // file.
4030 if (ID < FirstSelectorID)
4031 return;
4032 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004033}
4034
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004035ASTWriter::ASTWriter(
4036 llvm::BitstreamWriter &Stream,
4037 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
4038 bool IncludeTimestamps)
Richard Smith01b2cb42014-07-26 06:37:51 +00004039 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004040 WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
4041 WritingAST(false), DoneWritingDeclsAndTypes(false),
4042 ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
4043 NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
4044 NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
4045 NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
4046 NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
Richard Smith01b2cb42014-07-26 06:37:51 +00004047 NextSubmoduleID(FirstSubmoduleID),
4048 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4049 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4050 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00004051 NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004052 TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004053 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004054 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004055 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004056 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4057 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004058 ExprImplicitCastAbbrev(0) {
4059 for (const auto &Ext : Extensions) {
4060 if (auto Writer = Ext->createExtensionWriter(*this))
4061 ModuleFileExtensionWriters.push_back(std::move(Writer));
4062 }
4063}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004064
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004065ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004066 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004067}
4068
Richard Smithb3761912015-02-05 23:08:52 +00004069const LangOptions &ASTWriter::getLangOpts() const {
4070 assert(WritingAST && "can't determine lang opts when not writing AST");
4071 return Context->getLangOpts();
4072}
4073
Richard Smithe75ee0f2015-08-17 07:13:32 +00004074time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4075 return IncludeTimestamps ? E->getModificationTime() : 0;
4076}
4077
Adrian Prantladbd2b12015-09-22 23:26:31 +00004078uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4079 Module *WritingModule, StringRef isysroot,
4080 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004081 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004082
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004083 ASTHasCompilerErrors = hasErrors;
4084
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004085 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004086 Stream.Emit((unsigned)'C', 8);
4087 Stream.Emit((unsigned)'P', 8);
4088 Stream.Emit((unsigned)'C', 8);
4089 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004090
Chris Lattner28fa4e62009-04-26 22:26:21 +00004091 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004092
Douglas Gregoreda8e122011-08-09 15:13:55 +00004093 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004094 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004095 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004096 ASTFileSignature Signature =
4097 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004098 Context = nullptr;
4099 PP = nullptr;
4100 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004101 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004102
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004103 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004104 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004105}
4106
Douglas Gregora94a1542011-07-27 21:45:57 +00004107template<typename Vector>
4108static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4109 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004110 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4111 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004112 Writer.AddDeclRef(*I, Record);
4113 }
4114}
4115
Adrian Prantladbd2b12015-09-22 23:26:31 +00004116uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4117 const std::string &OutputFile,
4118 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004119 using namespace llvm;
4120
Craig Toppera13603a2014-05-22 05:54:18 +00004121 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004122
Douglas Gregorcf68c582011-12-01 22:20:10 +00004123 // Make sure that the AST reader knows to finalize itself.
4124 if (Chain)
4125 Chain->finalizeForWriting();
4126
Sebastian Redl143413f2010-07-12 22:02:52 +00004127 ASTContext &Context = SemaRef.Context;
4128 Preprocessor &PP = SemaRef.PP;
4129
Douglas Gregordab42432011-08-12 00:15:20 +00004130 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004131 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4132 if (D) {
4133 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4134 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004135 }
4136 };
4137 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4138 PREDEF_DECL_TRANSLATION_UNIT_ID);
4139 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4140 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4141 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4142 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4143 PREDEF_DECL_OBJC_PROTOCOL_ID);
4144 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4145 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4146 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4147 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4148 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004149 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004150 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4151 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004152 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004153 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4154 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004155 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4156 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004157
Chris Lattner0c797362009-09-08 18:19:27 +00004158 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004159 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004160 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004161 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004162 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004163
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004164 // Build a record containing all of the file scoped decls in this file.
4165 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004166 if (!isModule)
4167 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4168 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004169
Douglas Gregor851443c2011-08-12 01:39:19 +00004170 // Build a record containing all of the delegating constructors we still need
4171 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004172 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004173 if (!isModule)
4174 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004175
Douglas Gregor851443c2011-08-12 01:39:19 +00004176 // Write the set of weak, undeclared identifiers. We always write the
4177 // entire table, since later PCH files in a PCH chain are only interested in
4178 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004179 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004180 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4181 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4182 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4183 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4184 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4185 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4186 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004187 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004188
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004189 // Build a record containing all of the ext_vector declarations.
4190 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004191 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004192
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004193 // Build a record containing all of the VTable uses information.
4194 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004195 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004196 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4197 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4198 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4199 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4200 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004201 }
4202
Nico Weber72889432014-09-06 01:25:55 +00004203 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4204 RecordData UnusedLocalTypedefNameCandidates;
4205 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4206 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4207
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004208 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004209 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004210 for (const auto &I : SemaRef.PendingInstantiations) {
4211 AddDeclRef(I.first, PendingInstantiations);
4212 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004213 }
4214 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4215 "There are local ones at end of translation unit!");
4216
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004217 // Build a record containing some declaration references.
4218 RecordData SemaDeclRefs;
4219 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4220 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4221 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4222 }
4223
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004224 RecordData CUDASpecialDeclRefs;
4225 if (Context.getcudaConfigureCallDecl()) {
4226 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4227 }
4228
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004229 // Build a record containing all of the known namespaces.
4230 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004231 for (const auto &I : SemaRef.KnownNamespaces) {
4232 if (!I.second)
4233 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004234 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004235
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004236 // Build a record of all used, undefined objects that require definitions.
4237 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004238
4239 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004240 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004241 for (const auto &I : Undefined) {
4242 AddDeclRef(I.first, UndefinedButUsed);
4243 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004244 }
4245
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004246 // Build a record containing all delete-expressions that we would like to
4247 // analyze later in AST.
4248 RecordData DeleteExprsToAnalyze;
4249
4250 for (const auto &DeleteExprsInfo :
4251 SemaRef.getMismatchingDeleteExpressions()) {
4252 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4253 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4254 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4255 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4256 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4257 }
4258 }
4259
Douglas Gregor112b9072012-10-18 05:31:06 +00004260 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004261 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004262
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004263 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004264 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004265
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004266 // This is so that older clang versions, before the introduction
4267 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004268 {
4269 RecordData Record = {VERSION_MAJOR};
4270 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4271 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004272
Douglas Gregor851443c2011-08-12 01:39:19 +00004273 // Create a lexical update block containing all of the declarations in the
4274 // translation unit that do not come from other AST files.
4275 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004276 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4277 for (const auto *D : TU->noload_decls()) {
4278 if (!D->isFromASTFile()) {
4279 NewGlobalKindDeclPairs.push_back(D->getKind());
4280 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4281 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004282 }
4283
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004284 auto *Abv = new llvm::BitCodeAbbrev();
Douglas Gregor851443c2011-08-12 01:39:19 +00004285 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4286 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4287 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
Mehdi Amini57a41912015-09-10 01:46:39 +00004288 {
4289 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4290 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4291 bytes(NewGlobalKindDeclPairs));
4292 }
4293
Douglas Gregor851443c2011-08-12 01:39:19 +00004294 // And a visible updates block for the translation unit.
4295 Abv = new llvm::BitCodeAbbrev();
4296 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4297 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004298 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4299 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4300 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004301
4302 // If we have any extern "C" names, write out a visible update for them.
4303 if (Context.ExternCContext)
4304 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004305
4306 // If the translation unit has an anonymous namespace, and we don't already
4307 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004308 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004309 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4310 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004311 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004312 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004313 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004314
Richard Smith5652c0f2014-03-21 01:48:23 +00004315 // Add update records for all mangling numbers and static local numbers.
4316 // These aren't really update records, but this is a convenient way of
4317 // tagging this rare extra data onto the declarations.
4318 for (const auto &Number : Context.MangleNumbers)
4319 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004320 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4321 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004322 for (const auto &Number : Context.StaticLocalNumbers)
4323 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004324 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4325 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004326
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004327 // Make sure visible decls, added to DeclContexts previously loaded from
4328 // an AST file, are registered for serialization.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004329 for (const auto *I : UpdatingVisibleDecls) {
4330 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004331 }
4332
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004333 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004334 // serialization, if we're storing decls with identifiers.
4335 if (!WritingModule || !getLangOpts().CPlusPlus) {
4336 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004337 for (const auto &ID : PP.getIdentifierTable()) {
4338 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004339 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4340 IIs.push_back(II);
4341 }
4342 // Sort the identifiers to visit based on their name.
4343 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4344 for (const IdentifierInfo *II : IIs) {
4345 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4346 DEnd = SemaRef.IdResolver.end();
4347 D != DEnd; ++D) {
4348 GetDeclRef(*D);
4349 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004350 }
4351 }
4352
Douglas Gregor5204bde2011-08-02 16:26:37 +00004353 // Form the record of special types.
4354 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004355 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004356 AddTypeRef(Context.getFILEType(), SpecialTypes);
4357 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4358 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4359 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4360 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004361 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004362 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004363
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004364 if (Chain) {
4365 // Write the mapping information describing our module dependencies and how
4366 // each of those modules were mapped into our own offset/ID space, so that
4367 // the reader can build the appropriate mapping to its own offset/ID space.
4368 // The map consists solely of a blob with the following format:
4369 // *(module-name-len:i16 module-name:len*i8
4370 // source-location-offset:i32
4371 // identifier-id:i32
4372 // preprocessed-entity-id:i32
4373 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004374 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004375 // selector-id:i32
4376 // declaration-id:i32
4377 // c++-base-specifiers-id:i32
4378 // type-id:i32)
4379 //
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004380 auto *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004381 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4383 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004384 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004385 {
4386 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004387 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004388 using namespace llvm::support;
4389 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004390 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004391 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004392 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004393
Ben Langmuir785180e2014-10-20 16:27:30 +00004394 // Note: if a base ID was uint max, it would not be possible to load
4395 // another module after it or have more than one entity inside it.
4396 uint32_t None = std::numeric_limits<uint32_t>::max();
4397
4398 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4399 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4400 if (ShouldWrite)
4401 LE.write<uint32_t>(BaseID);
4402 else
4403 LE.write<uint32_t>(None);
4404 };
4405
Ben Langmuirfe971d92014-08-16 04:54:18 +00004406 // These values should be unique within a chain, since they will be read
4407 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004408 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4409 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4410 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4411 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4412 M->NumPreprocessedEntities);
4413 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4414 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4415 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4416 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004417 }
4418 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004419 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004420 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4421 Buffer.data(), Buffer.size());
4422 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004423
Richard Smithb9eab6d2014-03-20 19:44:17 +00004424 RecordData DeclUpdatesOffsetsRecord;
4425
Richard Smith59442b42014-03-20 20:07:19 +00004426 // Keep writing types, declarations, and declaration update records
4427 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004428 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4429 WriteTypeAbbrevs();
4430 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004431 do {
4432 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4433 while (!DeclTypesToEmit.empty()) {
4434 DeclOrType DOT = DeclTypesToEmit.front();
4435 DeclTypesToEmit.pop();
4436 if (DOT.isType())
4437 WriteType(DOT.getType());
4438 else
4439 WriteDecl(Context, DOT.getDecl());
4440 }
4441 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004442 Stream.ExitBlock();
4443
Richard Smithb9eab6d2014-03-20 19:44:17 +00004444 DoneWritingDeclsAndTypes = true;
4445
4446 // These things can only be done once we've written out decls and types.
4447 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004448 if (!DeclUpdatesOffsetsRecord.empty())
4449 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004450 WriteCXXBaseSpecifiersOffsets();
Richard Smithc2bb8182015-03-24 06:36:48 +00004451 WriteCXXCtorInitializersOffsets();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004452 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004453 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004454 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004455 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004456 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004457 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004458 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004459 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004460 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004461 WriteFPPragmaOptions(SemaRef.getFPOptions());
4462 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004463 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004464
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004465 // If we're emitting a module, write out the submodule information.
4466 if (WritingModule)
4467 WriteSubmodules(WritingModule);
4468
Douglas Gregor5204bde2011-08-02 16:26:37 +00004469 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4470
Douglas Gregord4df8652009-04-22 22:02:47 +00004471 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004472 if (!EagerlyDeserializedDecls.empty())
4473 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004474
4475 // Write the record containing tentative definitions.
4476 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004477 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004478
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004479 // Write the record containing unused file scoped decls.
4480 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004481 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004482
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004483 // Write the record containing weak undeclared identifiers.
4484 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004485 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004486 WeakUndeclaredIdentifiers);
4487
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004488 // Write the record containing ext_vector type names.
4489 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004490 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004491
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004492 // Write the record containing VTable uses information.
4493 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004494 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004495
Nico Weber72889432014-09-06 01:25:55 +00004496 // Write the record containing potentially unused local typedefs.
4497 if (!UnusedLocalTypedefNameCandidates.empty())
4498 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4499 UnusedLocalTypedefNameCandidates);
4500
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004501 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004502 if (!PendingInstantiations.empty())
4503 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004504
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004505 // Write the record containing declaration references of Sema.
4506 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004507 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004508
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004509 // Write the record containing CUDA-specific declaration references.
4510 if (!CUDASpecialDeclRefs.empty())
4511 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004512
4513 // Write the delegating constructors.
4514 if (!DelegatingCtorDecls.empty())
4515 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004516
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004517 // Write the known namespaces.
4518 if (!KnownNamespaces.empty())
4519 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004520
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004521 // Write the undefined internal functions and variables, and inline functions.
4522 if (!UndefinedButUsed.empty())
4523 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004524
4525 if (!DeleteExprsToAnalyze.empty())
4526 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4527
Douglas Gregor851443c2011-08-12 01:39:19 +00004528 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004529 for (auto *DC : UpdatedDeclContexts)
4530 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004531
Douglas Gregor959bb062011-12-03 01:15:29 +00004532 if (!WritingModule) {
4533 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004534 struct ModuleInfo {
4535 uint64_t ID;
4536 Module *M;
4537 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4538 };
Richard Smith56be7542014-03-21 00:33:59 +00004539 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004540 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004541 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004542 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4543 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004544 }
Richard Smith56be7542014-03-21 00:33:59 +00004545
4546 if (!Imports.empty()) {
4547 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4548 return A.ID < B.ID;
4549 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004550 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4551 return A.ID == B.ID;
4552 };
Richard Smith56be7542014-03-21 00:33:59 +00004553
4554 // Sort and deduplicate module IDs.
4555 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004556 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004557 Imports.end());
4558
4559 RecordData ImportedModules;
4560 for (const auto &Import : Imports) {
4561 ImportedModules.push_back(Import.ID);
4562 // FIXME: If the module has macros imported then later has declarations
4563 // imported, this location won't be the right one as a location for the
4564 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004565 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004566 }
4567
Douglas Gregor959bb062011-12-03 01:15:29 +00004568 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4569 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004570 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004571
Douglas Gregor851443c2011-08-12 01:39:19 +00004572 WriteDeclReplacementsBlock();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004573 WriteObjCCategories();
Dario Domizioli13a0a382014-05-23 12:13:25 +00004574 if(!WritingModule)
4575 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004576
Douglas Gregor08f01292009-04-17 22:13:46 +00004577 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004578 RecordData::value_type Record[] = {
4579 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004580 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004581 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004582
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004583 // Write the module file extension blocks.
4584 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004585 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004586
Adrian Prantladbd2b12015-09-22 23:26:31 +00004587 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004588}
4589
Richard Smithb9eab6d2014-03-20 19:44:17 +00004590void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004591 if (DeclUpdates.empty())
4592 return;
4593
Richard Smith59442b42014-03-20 20:07:19 +00004594 DeclUpdateMap LocalUpdates;
4595 LocalUpdates.swap(DeclUpdates);
4596
Richard Smith6ef42932014-03-20 21:02:00 +00004597 for (auto &DeclUpdate : LocalUpdates) {
4598 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004599
Richard Smithd28ac5b2014-03-22 23:33:22 +00004600 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004601 RecordData Record;
4602 for (auto &Update : DeclUpdate.second) {
4603 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4604
4605 Record.push_back(Kind);
4606 switch (Kind) {
4607 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4608 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4609 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004610 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004611 Record.push_back(GetDeclRef(Update.getDecl()));
4612 break;
4613
Richard Smith4d235792014-08-07 18:53:08 +00004614 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004615 // An updated body is emitted last, so that the reader doesn't need
4616 // to skip over the lazy body to reach statements for other records.
4617 Record.pop_back();
4618 HasUpdatedBody = true;
4619 break;
4620
Richard Smith4d235792014-08-07 18:53:08 +00004621 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4622 AddSourceLocation(Update.getLoc(), Record);
4623 break;
4624
John McCall32791cc2016-01-06 22:34:54 +00004625 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
4626 AddStmt(const_cast<Expr*>(
4627 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
4628 break;
4629
Richard Smithcd45dbc2014-04-19 03:48:30 +00004630 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4631 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004632 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004633 AddCXXDefinitionData(RD, Record);
4634 Record.push_back(WriteDeclContextLexicalBlock(
4635 *Context, const_cast<CXXRecordDecl *>(RD)));
4636
4637 // This state is sometimes updated by template instantiation, when we
4638 // switch from the specialization referring to the template declaration
4639 // to it referring to the template definition.
4640 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4641 Record.push_back(MSInfo->getTemplateSpecializationKind());
4642 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4643 } else {
4644 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4645 Record.push_back(Spec->getTemplateSpecializationKind());
4646 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004647
4648 // The instantiation might have been resolved to a partial
4649 // specialization. If so, record which one.
4650 auto From = Spec->getInstantiatedFrom();
4651 if (auto PartialSpec =
4652 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4653 Record.push_back(true);
4654 AddDeclRef(PartialSpec, Record);
4655 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4656 Record);
4657 } else {
4658 Record.push_back(false);
4659 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004660 }
4661 Record.push_back(RD->getTagKind());
4662 AddSourceLocation(RD->getLocation(), Record);
4663 AddSourceLocation(RD->getLocStart(), Record);
4664 AddSourceLocation(RD->getRBraceLoc(), Record);
4665
4666 // Instantiation may change attributes; write them all out afresh.
4667 Record.push_back(D->hasAttrs());
4668 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004669 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4670 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004671
4672 // FIXME: Ensure we don't get here for explicit instantiations.
4673 break;
4674 }
4675
Richard Smithf8134002015-03-10 01:41:22 +00004676 case UPD_CXX_RESOLVED_DTOR_DELETE:
4677 AddDeclRef(Update.getDecl(), Record);
4678 break;
4679
Richard Smith564417a2014-03-20 21:47:22 +00004680 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4681 addExceptionSpec(
4682 *this,
4683 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4684 Record);
4685 break;
4686
Richard Smith6ef42932014-03-20 21:02:00 +00004687 case UPD_CXX_DEDUCED_RETURN_TYPE:
4688 Record.push_back(GetOrCreateTypeID(Update.getType()));
4689 break;
4690
4691 case UPD_DECL_MARKED_USED:
4692 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004693
4694 case UPD_MANGLING_NUMBER:
4695 case UPD_STATIC_LOCAL_NUMBER:
4696 Record.push_back(Update.getNumber());
4697 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004698
Alexey Bataev97720002014-11-11 04:05:39 +00004699 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4700 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4701 Record);
4702 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004703
4704 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004705 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004706 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004707
4708 case UPD_ADDED_ATTR_TO_RECORD:
4709 WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record);
4710 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004711 }
4712 }
4713
Richard Smithd28ac5b2014-03-22 23:33:22 +00004714 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004715 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004716 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004717 Record.push_back(Def->isInlined());
4718 AddSourceLocation(Def->getInnerLocStart(), Record);
4719 AddFunctionDefinition(Def, Record);
4720 }
4721
Richard Smithcd45dbc2014-04-19 03:48:30 +00004722 OffsetsRecord.push_back(GetDeclRef(D));
4723 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4724
Richard Smith6ef42932014-03-20 21:02:00 +00004725 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004726
Richard Smithc2bb8182015-03-24 06:36:48 +00004727 FlushPendingAfterDecl();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004728 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004729}
4730
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004731void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004732 if (ReplacedDecls.empty())
4733 return;
4734
4735 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004736 for (const auto &I : ReplacedDecls) {
4737 Record.push_back(I.ID);
4738 Record.push_back(I.Offset);
4739 Record.push_back(I.Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004740 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004741 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004742}
4743
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004744void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004745 Record.push_back(Loc.getRawEncoding());
4746}
4747
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004748void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004749 AddSourceLocation(Range.getBegin(), Record);
4750 AddSourceLocation(Range.getEnd(), Record);
4751}
4752
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004753void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004754 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004755 const uint64_t *Words = Value.getRawData();
4756 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004757}
4758
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004759void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004760 Record.push_back(Value.isUnsigned());
4761 AddAPInt(Value, Record);
4762}
4763
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004764void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004765 AddAPInt(Value.bitcastToAPInt(), Record);
4766}
4767
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004768void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004769 Record.push_back(getIdentifierRef(II));
4770}
4771
Sebastian Redl539c5062010-08-18 23:57:32 +00004772IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004773 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004774 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004775
Sebastian Redl539c5062010-08-18 23:57:32 +00004776 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004777 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004778 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004779 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004780}
4781
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004782MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004783 // Don't emit builtin macros like __LINE__ to the AST file unless they
4784 // have been redefined by the header (in which case they are not
4785 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004786 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004787 return 0;
4788
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004789 MacroID &ID = MacroIDs[MI];
4790 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004791 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004792 MacroInfoToEmitData Info = { Name, MI, ID };
4793 MacroInfosToEmit.push_back(Info);
4794 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004795 return ID;
4796}
4797
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004798MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004799 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004800 return 0;
4801
4802 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4803 return MacroIDs[MI];
4804}
4805
4806uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004807 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004808}
4809
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004810void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004811 Record.push_back(getSelectorRef(SelRef));
4812}
4813
Sebastian Redl539c5062010-08-18 23:57:32 +00004814SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004815 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004816 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004817 }
4818
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004819 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004820 if (SID == 0 && Chain) {
4821 // This might trigger a ReadSelector callback, which will set the ID for
4822 // this selector.
4823 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004824 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004825 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004826 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004827 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004828 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004829 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004830 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004831}
4832
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004833void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004834 AddDeclRef(Temp->getDestructor(), Record);
4835}
4836
Richard Smithc2bb8182015-03-24 06:36:48 +00004837void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4838 RecordDataImpl &Record) {
4839 assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4840 CXXCtorInitializersToWrite.push_back(
4841 QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4842 Record.push_back(NextCXXCtorInitializersID++);
4843}
4844
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004845void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
Richard Smithc2bb8182015-03-24 06:36:48 +00004846 CXXBaseSpecifier const *BasesEnd,
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004847 RecordDataImpl &Record) {
4848 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4849 CXXBaseSpecifiersToWrite.push_back(
4850 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4851 Bases, BasesEnd));
4852 Record.push_back(NextCXXBaseSpecifiersID++);
4853}
4854
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004855void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004856 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004857 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004858 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004859 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004860 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004861 break;
4862 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004863 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004864 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004865 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004866 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004867 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004868 break;
4869 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004870 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004871 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004872 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004873 break;
John McCall0ad16662009-10-29 08:12:44 +00004874 case TemplateArgument::Null:
4875 case TemplateArgument::Integral:
4876 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004877 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004878 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004879 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004880 break;
4881 }
4882}
4883
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004884void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004885 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004886 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004887
4888 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4889 bool InfoHasSameExpr
4890 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4891 Record.push_back(InfoHasSameExpr);
4892 if (InfoHasSameExpr)
4893 return; // Avoid storing the same expr twice.
4894 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004895 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4896 Record);
4897}
4898
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004899void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4900 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004901 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004902 AddTypeRef(QualType(), Record);
4903 return;
4904 }
4905
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004906 AddTypeLoc(TInfo->getTypeLoc(), Record);
4907}
4908
4909void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4910 AddTypeRef(TL.getType(), Record);
4911
John McCall8f115c62009-10-16 21:56:05 +00004912 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004913 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004914 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004915}
4916
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004917void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004918 Record.push_back(GetOrCreateTypeID(T));
4919}
4920
Richard Smith2095ffe2015-03-16 20:11:03 +00004921TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004922 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004923 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4924 if (T.isNull())
4925 return TypeIdx();
4926 assert(!T.getLocalFastQualifiers());
4927
4928 TypeIdx &Idx = TypeIdxs[T];
4929 if (Idx.getIndex() == 0) {
4930 if (DoneWritingDeclsAndTypes) {
4931 assert(0 && "New type seen after serializing all the types to emit!");
4932 return TypeIdx();
4933 }
4934
4935 // We haven't seen this type before. Assign it a new ID and put it
4936 // into the queue of types to emit.
4937 Idx = TypeIdx(NextTypeID++);
4938 DeclTypesToEmit.push(T);
4939 }
4940 return Idx;
4941 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004942}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004943
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004944TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004945 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004946 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4947 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004948 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00004949 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004950
Richard Smith2095ffe2015-03-16 20:11:03 +00004951 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4952 assert(I != TypeIdxs.end() && "Type not emitted!");
4953 return I->second;
4954 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004955}
4956
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004957void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004958 Record.push_back(GetDeclRef(D));
4959}
4960
Sebastian Redl539c5062010-08-18 23:57:32 +00004961DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004962 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00004963
4964 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004965 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004966 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004967
4968 // If D comes from an AST file, its declaration ID is already known and
4969 // fixed.
4970 if (D->isFromASTFile())
4971 return D->getGlobalID();
4972
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004973 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004974 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004975 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004976 if (DoneWritingDeclsAndTypes) {
4977 assert(0 && "New decl seen after serializing all the decls to emit!");
4978 return 0;
4979 }
4980
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004981 // We haven't seen this declaration before. Give it a new ID and
4982 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004983 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004984 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004985 }
4986
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004987 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004988}
4989
Sebastian Redl539c5062010-08-18 23:57:32 +00004990DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00004991 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00004992 return 0;
4993
Douglas Gregorb3163e52012-01-05 22:33:30 +00004994 // If D comes from an AST file, its declaration ID is already known and
4995 // fixed.
4996 if (D->isFromASTFile())
4997 return D->getGlobalID();
4998
Douglas Gregore84a9da2009-04-20 20:36:09 +00004999 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5000 return DeclIDs[D];
5001}
5002
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005003void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005004 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005005 assert(D);
5006
5007 SourceLocation Loc = D->getLocation();
5008 if (Loc.isInvalid())
5009 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005010
5011 // We only keep track of the file-level declarations of each file.
5012 if (!D->getLexicalDeclContext()->isFileContext())
5013 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005014 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5015 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005016 if (isa<ParmVarDecl>(D))
5017 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005018
5019 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005020 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005021 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005022 FileID FID;
5023 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005024 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005025 if (FID.isInvalid())
5026 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005027 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005028
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005029 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005030 if (!Info)
5031 Info = new DeclIDInFileInfo();
5032
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005033 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005034 LocDeclIDsTy &Decls = Info->DeclIDs;
5035
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005036 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005037 Decls.push_back(LocDecl);
5038 return;
5039 }
5040
Benjamin Kramer45025c02013-08-24 13:22:59 +00005041 LocDeclIDsTy::iterator I =
5042 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005043
5044 Decls.insert(I, LocDecl);
5045}
5046
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005047void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005048 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005049 Record.push_back(Name.getNameKind());
5050 switch (Name.getNameKind()) {
5051 case DeclarationName::Identifier:
5052 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5053 break;
5054
5055 case DeclarationName::ObjCZeroArgSelector:
5056 case DeclarationName::ObjCOneArgSelector:
5057 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005058 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005059 break;
5060
5061 case DeclarationName::CXXConstructorName:
5062 case DeclarationName::CXXDestructorName:
5063 case DeclarationName::CXXConversionFunctionName:
5064 AddTypeRef(Name.getCXXNameType(), Record);
5065 break;
5066
5067 case DeclarationName::CXXOperatorName:
5068 Record.push_back(Name.getCXXOverloadedOperator());
5069 break;
5070
Alexis Hunt3d221f22009-11-29 07:34:05 +00005071 case DeclarationName::CXXLiteralOperatorName:
5072 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5073 break;
5074
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005075 case DeclarationName::CXXUsingDirective:
5076 // No extra data to emit
5077 break;
5078 }
5079}
Chris Lattnerca025db2010-05-07 21:43:38 +00005080
Richard Smithd08aeb62014-08-28 01:33:39 +00005081unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5082 assert(needsAnonymousDeclarationNumber(D) &&
5083 "expected an anonymous declaration");
5084
5085 // Number the anonymous declarations within this context, if we've not
5086 // already done so.
5087 auto It = AnonymousDeclarationNumbers.find(D);
5088 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005089 auto *DC = D->getLexicalDeclContext();
5090 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5091 AnonymousDeclarationNumbers[ND] = Number;
5092 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005093
5094 It = AnonymousDeclarationNumbers.find(D);
5095 assert(It != AnonymousDeclarationNumbers.end() &&
5096 "declaration not found within its lexical context");
5097 }
5098
5099 return It->second;
5100}
5101
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005102void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005103 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005104 switch (Name.getNameKind()) {
5105 case DeclarationName::CXXConstructorName:
5106 case DeclarationName::CXXDestructorName:
5107 case DeclarationName::CXXConversionFunctionName:
5108 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5109 break;
5110
5111 case DeclarationName::CXXOperatorName:
5112 AddSourceLocation(
5113 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5114 Record);
5115 AddSourceLocation(
5116 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5117 Record);
5118 break;
5119
5120 case DeclarationName::CXXLiteralOperatorName:
5121 AddSourceLocation(
5122 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5123 Record);
5124 break;
5125
5126 case DeclarationName::Identifier:
5127 case DeclarationName::ObjCZeroArgSelector:
5128 case DeclarationName::ObjCOneArgSelector:
5129 case DeclarationName::ObjCMultiArgSelector:
5130 case DeclarationName::CXXUsingDirective:
5131 break;
5132 }
5133}
5134
5135void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005136 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005137 AddDeclarationName(NameInfo.getName(), Record);
5138 AddSourceLocation(NameInfo.getLoc(), Record);
5139 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5140}
5141
5142void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005143 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005144 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005145 Record.push_back(Info.NumTemplParamLists);
5146 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5147 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5148}
5149
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005150void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005151 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005152 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005153 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005154 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005155
5156 // Push each of the NNS's onto a stack for serialization in reverse order.
5157 while (NNS) {
5158 NestedNames.push_back(NNS);
5159 NNS = NNS->getPrefix();
5160 }
5161
5162 Record.push_back(NestedNames.size());
5163 while(!NestedNames.empty()) {
5164 NNS = NestedNames.pop_back_val();
5165 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5166 Record.push_back(Kind);
5167 switch (Kind) {
5168 case NestedNameSpecifier::Identifier:
5169 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5170 break;
5171
5172 case NestedNameSpecifier::Namespace:
5173 AddDeclRef(NNS->getAsNamespace(), Record);
5174 break;
5175
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005176 case NestedNameSpecifier::NamespaceAlias:
5177 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5178 break;
5179
Chris Lattnerca025db2010-05-07 21:43:38 +00005180 case NestedNameSpecifier::TypeSpec:
5181 case NestedNameSpecifier::TypeSpecWithTemplate:
5182 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5183 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5184 break;
5185
5186 case NestedNameSpecifier::Global:
5187 // Don't need to write an associated value.
5188 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005189
5190 case NestedNameSpecifier::Super:
5191 AddDeclRef(NNS->getAsRecordDecl(), Record);
5192 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005193 }
5194 }
5195}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005196
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005197void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5198 RecordDataImpl &Record) {
5199 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005200 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005201 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005202
5203 // Push each of the nested-name-specifiers's onto a stack for
5204 // serialization in reverse order.
5205 while (NNS) {
5206 NestedNames.push_back(NNS);
5207 NNS = NNS.getPrefix();
5208 }
5209
5210 Record.push_back(NestedNames.size());
5211 while(!NestedNames.empty()) {
5212 NNS = NestedNames.pop_back_val();
5213 NestedNameSpecifier::SpecifierKind Kind
5214 = NNS.getNestedNameSpecifier()->getKind();
5215 Record.push_back(Kind);
5216 switch (Kind) {
5217 case NestedNameSpecifier::Identifier:
5218 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5219 AddSourceRange(NNS.getLocalSourceRange(), Record);
5220 break;
5221
5222 case NestedNameSpecifier::Namespace:
5223 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5224 AddSourceRange(NNS.getLocalSourceRange(), Record);
5225 break;
5226
5227 case NestedNameSpecifier::NamespaceAlias:
5228 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5229 AddSourceRange(NNS.getLocalSourceRange(), Record);
5230 break;
5231
5232 case NestedNameSpecifier::TypeSpec:
5233 case NestedNameSpecifier::TypeSpecWithTemplate:
5234 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5235 AddTypeLoc(NNS.getTypeLoc(), Record);
5236 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5237 break;
5238
5239 case NestedNameSpecifier::Global:
5240 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5241 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005242
5243 case NestedNameSpecifier::Super:
5244 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5245 AddSourceRange(NNS.getLocalSourceRange(), Record);
5246 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005247 }
5248 }
5249}
5250
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005251void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005252 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005253 Record.push_back(Kind);
5254 switch (Kind) {
5255 case TemplateName::Template:
5256 AddDeclRef(Name.getAsTemplateDecl(), Record);
5257 break;
5258
5259 case TemplateName::OverloadedTemplate: {
5260 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5261 Record.push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005262 for (const auto &I : *OvT)
5263 AddDeclRef(I, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005264 break;
5265 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005266
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005267 case TemplateName::QualifiedTemplate: {
5268 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5269 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5270 Record.push_back(QualT->hasTemplateKeyword());
5271 AddDeclRef(QualT->getTemplateDecl(), Record);
5272 break;
5273 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005274
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005275 case TemplateName::DependentTemplate: {
5276 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5277 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5278 Record.push_back(DepT->isIdentifier());
5279 if (DepT->isIdentifier())
5280 AddIdentifierRef(DepT->getIdentifier(), Record);
5281 else
5282 Record.push_back(DepT->getOperator());
5283 break;
5284 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005285
5286 case TemplateName::SubstTemplateTemplateParm: {
5287 SubstTemplateTemplateParmStorage *subst
5288 = Name.getAsSubstTemplateTemplateParm();
5289 AddDeclRef(subst->getParameter(), Record);
5290 AddTemplateName(subst->getReplacement(), Record);
5291 break;
5292 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005293
5294 case TemplateName::SubstTemplateTemplateParmPack: {
5295 SubstTemplateTemplateParmPackStorage *SubstPack
5296 = Name.getAsSubstTemplateTemplateParmPack();
5297 AddDeclRef(SubstPack->getParameterPack(), Record);
5298 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5299 break;
5300 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005301 }
5302}
5303
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005304void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005305 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005306 Record.push_back(Arg.getKind());
5307 switch (Arg.getKind()) {
5308 case TemplateArgument::Null:
5309 break;
5310 case TemplateArgument::Type:
5311 AddTypeRef(Arg.getAsType(), Record);
5312 break;
5313 case TemplateArgument::Declaration:
5314 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005315 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005316 break;
5317 case TemplateArgument::NullPtr:
5318 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005319 break;
5320 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005321 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005322 AddTypeRef(Arg.getIntegralType(), Record);
5323 break;
5324 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005325 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5326 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005327 case TemplateArgument::TemplateExpansion:
5328 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005329 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005330 Record.push_back(*NumExpansions + 1);
5331 else
5332 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005333 break;
5334 case TemplateArgument::Expression:
5335 AddStmt(Arg.getAsExpr());
5336 break;
5337 case TemplateArgument::Pack:
5338 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005339 for (const auto &P : Arg.pack_elements())
5340 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005341 break;
5342 }
5343}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005344
5345void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005346ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005347 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005348 assert(TemplateParams && "No TemplateParams!");
5349 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5350 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5351 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5352 Record.push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005353 for (const auto &P : *TemplateParams)
5354 AddDeclRef(P, Record);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005355}
5356
5357/// \brief Emit a template argument list.
5358void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005359ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005360 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005361 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005362 Record.push_back(TemplateArgs->size());
5363 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005364 AddTemplateArgument(TemplateArgs->get(i), Record);
5365}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005366
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005367void
5368ASTWriter::AddASTTemplateArgumentListInfo
5369(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5370 assert(ASTTemplArgList && "No ASTTemplArgList!");
5371 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5372 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5373 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5374 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5375 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5376 AddTemplateArgumentLoc(TemplArgs[i], Record);
5377}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005378
5379void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005380ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005381 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005382 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005383 I = Set.begin(), E = Set.end(); I != E; ++I) {
5384 AddDeclRef(I.getDecl(), Record);
5385 Record.push_back(I.getAccess());
5386 }
5387}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005388
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005389void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005390 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005391 Record.push_back(Base.isVirtual());
5392 Record.push_back(Base.isBaseOfClass());
5393 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005394 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005395 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005396 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005397 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5398 : SourceLocation(),
5399 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005400}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005401
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005402void ASTWriter::FlushCXXBaseSpecifiers() {
5403 RecordData Record;
Richard Smithc2bb8182015-03-24 06:36:48 +00005404 unsigned N = CXXBaseSpecifiersToWrite.size();
5405 for (unsigned I = 0; I != N; ++I) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005406 Record.clear();
5407
5408 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005409 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005410 if (Index == CXXBaseSpecifiersOffsets.size())
5411 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5412 else {
5413 if (Index > CXXBaseSpecifiersOffsets.size())
5414 CXXBaseSpecifiersOffsets.resize(Index + 1);
5415 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5416 }
5417
5418 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5419 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5420 Record.push_back(BEnd - B);
5421 for (; B != BEnd; ++B)
5422 AddCXXBaseSpecifier(*B, Record);
5423 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005424
5425 // Flush any expressions that were written as part of the base specifiers.
5426 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005427 }
5428
Richard Smithc2bb8182015-03-24 06:36:48 +00005429 assert(N == CXXBaseSpecifiersToWrite.size() &&
5430 "added more base specifiers while writing base specifiers");
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005431 CXXBaseSpecifiersToWrite.clear();
5432}
5433
Alexis Hunt1d792652011-01-08 20:30:50 +00005434void ASTWriter::AddCXXCtorInitializers(
5435 const CXXCtorInitializer * const *CtorInitializers,
5436 unsigned NumCtorInitializers,
5437 RecordDataImpl &Record) {
5438 Record.push_back(NumCtorInitializers);
5439 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5440 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005441
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005442 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005443 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005444 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005445 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005446 } else if (Init->isDelegatingInitializer()) {
5447 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005448 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005449 } else if (Init->isMemberInitializer()){
5450 Record.push_back(CTOR_INITIALIZER_MEMBER);
5451 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005452 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005453 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5454 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005455 }
Francois Pichetd583da02010-12-04 09:14:42 +00005456
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005457 AddSourceLocation(Init->getMemberLocation(), Record);
5458 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005459 AddSourceLocation(Init->getLParenLoc(), Record);
5460 AddSourceLocation(Init->getRParenLoc(), Record);
5461 Record.push_back(Init->isWritten());
5462 if (Init->isWritten()) {
5463 Record.push_back(Init->getSourceOrder());
5464 } else {
5465 Record.push_back(Init->getNumArrayIndices());
5466 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5467 AddDeclRef(Init->getArrayIndex(i), Record);
5468 }
5469 }
5470}
5471
Richard Smithc2bb8182015-03-24 06:36:48 +00005472void ASTWriter::FlushCXXCtorInitializers() {
5473 RecordData Record;
5474
5475 unsigned N = CXXCtorInitializersToWrite.size();
Daniel Jasperc77b0a12015-03-24 08:06:38 +00005476 (void)N; // Silence unused warning in non-assert builds.
Richard Smithc2bb8182015-03-24 06:36:48 +00005477 for (auto &Init : CXXCtorInitializersToWrite) {
5478 Record.clear();
5479
5480 // Record the offset of this mem-initializer list.
5481 unsigned Index = Init.ID - 1;
5482 if (Index == CXXCtorInitializersOffsets.size())
5483 CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5484 else {
5485 if (Index > CXXCtorInitializersOffsets.size())
5486 CXXCtorInitializersOffsets.resize(Index + 1);
5487 CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5488 }
5489
5490 AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5491 Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5492
5493 // Flush any expressions that were written as part of the initializers.
5494 FlushStmts();
5495 }
5496
5497 assert(N == CXXCtorInitializersToWrite.size() &&
5498 "added more ctor initializers while writing ctor initializers");
5499 CXXCtorInitializersToWrite.clear();
5500}
5501
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005502void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005503 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005504 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005505 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005506 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005507 Record.push_back(Data.Aggregate);
5508 Record.push_back(Data.PlainOldData);
5509 Record.push_back(Data.Empty);
5510 Record.push_back(Data.Polymorphic);
5511 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005512 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005513 Record.push_back(Data.HasNoNonEmptyBases);
5514 Record.push_back(Data.HasPrivateFields);
5515 Record.push_back(Data.HasProtectedFields);
5516 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005517 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005518 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005519 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005520 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005521 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005522 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5523 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5524 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5525 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5526 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5527 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005528 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005529 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005530 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005531 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005532 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005533 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005534 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005535 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005536 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005537 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005538 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5539 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5540 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5541 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005542 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005543
5544 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005545 if (Data.NumBases > 0)
5546 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5547 Record);
5548
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005549 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5550 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005551 if (Data.NumVBases > 0)
5552 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5553 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005554
Richard Smitha4ba74c2013-08-30 04:46:40 +00005555 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5556 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005557 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005558 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005559
5560 // Add lambda-specific data.
5561 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005562 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005563 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005564 Record.push_back(Lambda.IsGenericLambda);
5565 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005566 Record.push_back(Lambda.NumCaptures);
5567 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005568 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005569 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005570 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005571 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005572 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005573 AddSourceLocation(Capture.getLocation(), Record);
5574 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005575 Record.push_back(Capture.getCaptureKind());
5576 switch (Capture.getCaptureKind()) {
5577 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005578 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005579 break;
5580 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005581 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005582 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005583 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005584 AddDeclRef(Var, Record);
5585 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5586 : SourceLocation(),
5587 Record);
5588 break;
5589 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005590 }
5591 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005592}
5593
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005594void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005595 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005596 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005597 assert(FirstDeclID == NextDeclID &&
5598 FirstTypeID == NextTypeID &&
5599 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005600 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005601 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005602 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005603 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005604
Sebastian Redl07a89a82010-07-30 00:29:29 +00005605 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005606
Richard Smith7f330cd2015-03-18 01:42:29 +00005607 // Note, this will get called multiple times, once one the reader starts up
5608 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005609 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5610 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5611 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005612 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005613 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005614 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005615 NextDeclID = FirstDeclID;
5616 NextTypeID = FirstTypeID;
5617 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005618 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005619 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005620 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005621}
5622
Sebastian Redl539c5062010-08-18 23:57:32 +00005623void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005624 // Always keep the highest ID. See \p TypeRead() for more information.
5625 IdentID &StoredID = IdentifierIDs[II];
5626 if (ID > StoredID)
5627 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005628}
5629
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005630void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005631 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005632 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005633 if (ID > StoredID)
5634 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005635}
5636
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005637void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005638 // Always take the highest-numbered type index. This copes with an interesting
5639 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005640 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005641 // keep the higher-numbered entry so that we can properly write it out to
5642 // the AST file.
5643 TypeIdx &StoredIdx = TypeIdxs[T];
5644 if (Idx.getIndex() >= StoredIdx.getIndex())
5645 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005646}
5647
Sebastian Redl539c5062010-08-18 23:57:32 +00005648void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005649 // Always keep the highest ID. See \p TypeRead() for more information.
5650 SelectorID &StoredID = SelectorIDs[S];
5651 if (ID > StoredID)
5652 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005653}
Douglas Gregor91096292010-10-02 19:29:26 +00005654
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005655void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005656 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005657 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005658 MacroDefinitions[MD] = ID;
5659}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005660
Douglas Gregore37a85a2011-12-02 17:30:13 +00005661void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5662 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5663 SubmoduleIDs[Mod] = ID;
5664}
5665
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005666void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005667 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005668 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005669 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005670 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005671 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005672 // A forward reference was mutated into a definition. Rewrite it.
5673 // FIXME: This happens during template instantiation, should we
5674 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005675 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5676 "completed a tag from another module but not by instantiation?");
5677 DeclUpdates[RD].push_back(
5678 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005679 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005680 }
5681}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005682
Richard Smithf983f7f2015-10-13 00:23:25 +00005683static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5684 if (D->isFromASTFile())
5685 return true;
5686
5687 // If we've not loaded any modules, this can't be imported.
5688 if (!Chain || !Chain->getModuleManager().size())
5689 return false;
5690
5691 // The predefined __va_list_tag struct is imported if we imported any decls.
5692 // FIXME: This is a gross hack.
5693 return D == D->getASTContext().getVaListTagDecl();
5694}
5695
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005696void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5697 // TU and namespaces are handled elsewhere.
5698 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5699 return;
5700
Richard Smithf983f7f2015-10-13 00:23:25 +00005701 // We're only interested in cases where a local declaration is added to an
5702 // imported context.
5703 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5704 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005705
Richard Smith0f4e2c42015-08-06 04:23:48 +00005706 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005707 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005708 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005709 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5710 // We're adding a visible declaration to a predefined decl context. Ensure
5711 // that we write out all of its lookup results so we don't get a nasty
5712 // surprise when we try to emit its lookup table.
5713 for (auto *Child : DC->decls())
5714 UpdatingVisibleDecls.push_back(Child);
5715 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005716 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005717}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005718
5719void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5720 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005721
5722 // We're only interested in cases where a local declaration is added to an
5723 // imported context.
5724 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5725 return;
5726
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005727 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005728 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005729
5730 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005731 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005732 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005733 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005734}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005735
Richard Smith564417a2014-03-20 21:47:22 +00005736void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005737 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5738 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005739 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005740 // If we don't already know the exception specification for this redecl
5741 // chain, add an update record for it.
5742 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5743 ->getType()
5744 ->castAs<FunctionProtoType>()
5745 ->getExceptionSpecType()))
5746 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5747 });
Richard Smith564417a2014-03-20 21:47:22 +00005748}
5749
Richard Smith1fa5d642013-05-11 05:45:24 +00005750void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5751 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005752 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005753 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005754 DeclUpdates[D].push_back(
5755 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5756 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005757}
5758
Richard Smithf8134002015-03-10 01:41:22 +00005759void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5760 const FunctionDecl *Delete) {
5761 assert(!WritingAST && "Already writing the AST!");
5762 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005763 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005764 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005765 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5766 });
Richard Smithf8134002015-03-10 01:41:22 +00005767}
5768
Sebastian Redlab238a72011-04-24 16:28:06 +00005769void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005770 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005771 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005772 return; // Declaration not imported from PCH.
5773
Richard Smith4d235792014-08-07 18:53:08 +00005774 // Implicit function decl from a PCH was defined.
5775 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005776}
5777
Richard Smithd28ac5b2014-03-22 23:33:22 +00005778void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5779 assert(!WritingAST && "Already writing the AST!");
5780 if (!D->isFromASTFile())
5781 return;
5782
Richard Smith9e2341d2015-03-23 03:25:59 +00005783 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005784}
5785
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005786void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005787 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005788 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005789 return;
5790
5791 // Since the actual instantiation is delayed, this really means that we need
5792 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005793 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005794 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5795 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005796}
5797
John McCall32791cc2016-01-06 22:34:54 +00005798void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
5799 assert(!WritingAST && "Already writing the AST!");
5800 if (!D->isFromASTFile())
5801 return;
5802
5803 DeclUpdates[D].push_back(
5804 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
5805}
5806
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005807void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5808 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005809 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005810 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005811 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005812
5813 assert(IFD->getDefinition() && "Category on a class without a definition?");
5814 ObjCClassesWithCategories.insert(
5815 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005816}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005817
Eli Friedman276dd182013-09-05 00:02:25 +00005818void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5819 assert(!WritingAST && "Already writing the AST!");
5820 if (!D->isFromASTFile())
5821 return;
5822
Aaron Ballman4f45b712014-03-21 15:22:56 +00005823 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005824}
Alexey Bataev97720002014-11-11 04:05:39 +00005825
5826void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5827 assert(!WritingAST && "Already writing the AST!");
5828 if (!D->isFromASTFile())
5829 return;
5830
5831 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5832}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005833
Richard Smith4caa4492015-05-15 02:34:32 +00005834void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00005835 assert(!WritingAST && "Already writing the AST!");
5836 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005837 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005838}
Alex Denisovfde64952015-06-26 05:28:36 +00005839
5840void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5841 const RecordDecl *Record) {
5842 assert(!WritingAST && "Already writing the AST!");
5843 if (!Record->isFromASTFile())
5844 return;
5845 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5846}