blob: f54bc5f491e0e111a9ac2ed31239216c471e3333 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Richard Smith961eae52014-03-25 01:14:22 +000020#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000026#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000028#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000032#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000034#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/IdentifierResolver.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000044#include "llvm/ADT/APFloat.h"
45#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000047#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000049#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000050#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000051#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000052#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000053#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000054#include "llvm/Support/Process.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000055#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000056#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000057#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000058#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000059using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000060using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061
Sebastian Redl3df5a082010-07-30 17:03:48 +000062template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000063static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000064 if (v.empty()) return StringRef();
65 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000066 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000067}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000068
69template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +000070static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000071 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000072 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000073}
74
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075//===----------------------------------------------------------------------===//
76// Type serialization
77//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000078
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000080 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000081 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000082 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
84 public:
85 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000086 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +000087 /// \brief Abbreviation to use for the record, if any.
88 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000090 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000091 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092
93 void VisitArrayType(const ArrayType *T);
94 void VisitFunctionType(const FunctionType *T);
95 void VisitTagType(const TagType *T);
96
97#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
98#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099#include "clang/AST/TypeNodes.def"
100 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000101}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000104 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Reid Kleckner8a365022013-06-24 17:51:48 +0000117void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
118 Writer.AddTypeRef(T->getOriginalType(), Record);
119 Code = TYPE_DECAYED;
120}
121
Reid Kleckner0503a872013-12-05 01:23:43 +0000122void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
123 Writer.AddTypeRef(T->getOriginalType(), Record);
124 Writer.AddTypeRef(T->getAdjustedType(), Record);
125 Code = TYPE_ADJUSTED;
126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
135 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000140 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000145 Writer.AddTypeRef(T->getPointeeType(), Record);
146 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000147 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151 Writer.AddTypeRef(T->getElementType(), Record);
152 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000153 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 VisitArrayType(T);
158 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000159 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160}
161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000164 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000169 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
170 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000171 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000172 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173}
174
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 Writer.AddTypeRef(T->getElementType(), Record);
177 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000178 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000188 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000189 FunctionType::ExtInfo C = T->getExtInfo();
190 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000191 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000192 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000193 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000194 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000195 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000196
197 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
198 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Richard Smith564417a2014-03-20 21:47:22 +0000206static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
207 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000208 Record.push_back(T->getExceptionSpecType());
209 if (T->getExceptionSpecType() == EST_Dynamic) {
210 Record.push_back(T->getNumExceptions());
211 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
212 Writer.AddTypeRef(T->getExceptionType(I), Record);
213 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
214 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000215 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
216 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
217 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000218 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
219 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000220 }
Richard Smith564417a2014-03-20 21:47:22 +0000221}
222
223void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
224 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000225
Richard Smith564417a2014-03-20 21:47:22 +0000226 Record.push_back(T->isVariadic());
227 Record.push_back(T->hasTrailingReturn());
228 Record.push_back(T->getTypeQuals());
229 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
230 addExceptionSpec(Writer, T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000231
232 Record.push_back(T->getNumParams());
233 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
234 Writer.AddTypeRef(T->getParamType(I), Record);
235
236 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
237 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
238 AbbrevToUse = 0;
239
Sebastian Redl539c5062010-08-18 23:57:32 +0000240 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241}
242
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000243void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000244 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000246}
John McCallb96ec562009-12-04 22:46:56 +0000247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000250 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
251 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000252 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253}
254
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000256 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000257 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000266 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000267 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000268 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000269}
270
Alexis Hunte852b102011-05-24 22:41:36 +0000271void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
272 Writer.AddTypeRef(T->getBaseType(), Record);
273 Writer.AddTypeRef(T->getUnderlyingType(), Record);
274 Record.push_back(T->getUTTKind());
275 Code = TYPE_UNARY_TRANSFORM;
276}
277
Richard Smith30482bc2011-02-20 03:19:35 +0000278void ASTTypeWriter::VisitAutoType(const AutoType *T) {
279 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000280 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000281 if (T->getDeducedType().isNull())
282 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000283 Code = TYPE_AUTO;
284}
285
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000287 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000288 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000289 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000290 "Cannot serialize in the middle of a type definition");
291}
292
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000293void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000294 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000295 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000296}
297
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000299 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000301}
302
John McCall81904512011-01-06 01:58:22 +0000303void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
304 Writer.AddTypeRef(T->getModifiedType(), Record);
305 Writer.AddTypeRef(T->getEquivalentType(), Record);
306 Record.push_back(T->getAttrKind());
307 Code = TYPE_ATTRIBUTED;
308}
309
Mike Stump11289f42009-09-09 15:08:12 +0000310void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000311ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000312 const SubstTemplateTypeParmType *T) {
313 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
314 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000315 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000316}
317
318void
Douglas Gregorada4b792011-01-14 02:55:32 +0000319ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
320 const SubstTemplateTypeParmPackType *T) {
321 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
322 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
323 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
324}
325
326void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000327ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000328 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000329 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000330 Writer.AddTemplateName(T->getTemplateName(), Record);
331 Record.push_back(T->getNumArgs());
332 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
333 ArgI != ArgE; ++ArgI)
334 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000335 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
336 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000339 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000344 VisitArrayType(T);
345 Writer.AddStmt(T->getSizeExpr());
346 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000347 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000348}
349
350void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000352 const DependentSizedExtVectorType *T) {
353 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000354 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000355}
356
357void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000358ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000359 Record.push_back(T->getDepth());
360 Record.push_back(T->getIndex());
361 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000362 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000363 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000364}
365
366void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000367ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000368 Record.push_back(T->getKeyword());
369 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
370 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000371 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
372 : T->getCanonicalTypeInternal(),
373 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000374 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000375}
376
377void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000378ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000379 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000380 Record.push_back(T->getKeyword());
381 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
382 Writer.AddIdentifierRef(T->getIdentifier(), Record);
383 Record.push_back(T->getNumArgs());
384 for (DependentTemplateSpecializationType::iterator
385 I = T->begin(), E = T->end(); I != E; ++I)
386 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000387 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388}
389
Douglas Gregord2fa7662010-12-20 02:24:11 +0000390void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
391 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000392 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000393 Record.push_back(*NumExpansions + 1);
394 else
395 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000396 Code = TYPE_PACK_EXPANSION;
397}
398
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000399void ASTTypeWriter::VisitParenType(const ParenType *T) {
400 Writer.AddTypeRef(T->getInnerType(), Record);
401 Code = TYPE_PAREN;
402}
403
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000404void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000405 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000406 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
407 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000408 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000409}
410
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000411void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000412 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000413 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000414 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000415}
416
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000417void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000418 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000419 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000420}
421
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000422void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000423 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregore83b9562015-07-07 03:57:53 +0000424 Record.push_back(T->getTypeArgsAsWritten().size());
425 for (auto TypeArg : T->getTypeArgsAsWritten())
Douglas Gregore9d95f12015-07-07 03:57:35 +0000426 Writer.AddTypeRef(TypeArg, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000427 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000428 for (const auto *I : T->quals())
429 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000430 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000431}
432
Steve Narofffb4330f2009-06-17 22:40:22 +0000433void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000434ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000435 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000436 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000437}
438
Eli Friedman0dfb8892011-10-06 23:00:33 +0000439void
440ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
441 Writer.AddTypeRef(T->getValueType(), Record);
442 Code = TYPE_ATOMIC;
443}
444
John McCall8f115c62009-10-16 21:56:05 +0000445namespace {
446
447class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000448 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000449 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000450
451public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000452 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000453 : Writer(Writer), Record(Record) { }
454
John McCall17001972009-10-18 01:05:36 +0000455#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000456#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000457 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000458#include "clang/AST/TypeLocNodes.def"
459
John McCall17001972009-10-18 01:05:36 +0000460 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
461 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000462};
463
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000464}
John McCall8f115c62009-10-16 21:56:05 +0000465
John McCall17001972009-10-18 01:05:36 +0000466void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
467 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000468}
John McCall17001972009-10-18 01:05:36 +0000469void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000470 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
471 if (TL.needsExtraLocalData()) {
472 Record.push_back(TL.getWrittenTypeSpec());
473 Record.push_back(TL.getWrittenSignSpec());
474 Record.push_back(TL.getWrittenWidthSpec());
475 Record.push_back(TL.hasModeAttr());
476 }
John McCall8f115c62009-10-16 21:56:05 +0000477}
John McCall17001972009-10-18 01:05:36 +0000478void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000480}
John McCall17001972009-10-18 01:05:36 +0000481void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000483}
Reid Kleckner8a365022013-06-24 17:51:48 +0000484void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
485 // nothing to do
486}
Reid Kleckner0503a872013-12-05 01:23:43 +0000487void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
488 // nothing to do
489}
John McCall17001972009-10-18 01:05:36 +0000490void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000492}
John McCall17001972009-10-18 01:05:36 +0000493void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000495}
John McCall17001972009-10-18 01:05:36 +0000496void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000498}
John McCall17001972009-10-18 01:05:36 +0000499void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000501 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000502}
John McCall17001972009-10-18 01:05:36 +0000503void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
505 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
506 Record.push_back(TL.getSizeExpr() ? 1 : 0);
507 if (TL.getSizeExpr())
508 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000509}
John McCall17001972009-10-18 01:05:36 +0000510void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
511 VisitArrayTypeLoc(TL);
512}
513void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
514 VisitArrayTypeLoc(TL);
515}
516void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
517 VisitArrayTypeLoc(TL);
518}
519void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
520 DependentSizedArrayTypeLoc TL) {
521 VisitArrayTypeLoc(TL);
522}
523void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
524 DependentSizedExtVectorTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
527void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
533void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000534 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000535 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
536 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000537 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000538 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
539 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000540}
541void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
542 VisitFunctionTypeLoc(TL);
543}
544void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
545 VisitFunctionTypeLoc(TL);
546}
John McCallb96ec562009-12-04 22:46:56 +0000547void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
John McCall17001972009-10-18 01:05:36 +0000550void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
553void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000554 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
555 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
556 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000557}
558void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000559 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
560 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
561 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
562 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000563}
564void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getNameLoc(), Record);
566}
Alexis Hunte852b102011-05-24 22:41:36 +0000567void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
568 Writer.AddSourceLocation(TL.getKWLoc(), Record);
569 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
570 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
571 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
572}
Richard Smith30482bc2011-02-20 03:19:35 +0000573void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
John McCall17001972009-10-18 01:05:36 +0000576void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578}
579void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getNameLoc(), Record);
581}
John McCall81904512011-01-06 01:58:22 +0000582void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
584 if (TL.hasAttrOperand()) {
585 SourceRange range = TL.getAttrOperandParensRange();
586 Writer.AddSourceLocation(range.getBegin(), Record);
587 Writer.AddSourceLocation(range.getEnd(), Record);
588 }
589 if (TL.hasAttrExprOperand()) {
590 Expr *operand = TL.getAttrExprOperand();
591 Record.push_back(operand ? 1 : 0);
592 if (operand) Writer.AddStmt(operand);
593 } else if (TL.hasAttrEnumOperand()) {
594 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
595 }
596}
John McCall17001972009-10-18 01:05:36 +0000597void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getNameLoc(), Record);
599}
John McCallcebee162009-10-18 09:09:24 +0000600void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
601 SubstTemplateTypeParmTypeLoc TL) {
602 Writer.AddSourceLocation(TL.getNameLoc(), Record);
603}
Douglas Gregorada4b792011-01-14 02:55:32 +0000604void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
605 SubstTemplateTypeParmPackTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getNameLoc(), Record);
607}
John McCall17001972009-10-18 01:05:36 +0000608void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
609 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000610 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000611 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
612 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
613 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
614 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000615 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
616 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000617}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000618void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
619 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
620 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
621}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000622void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000623 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000624 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000625}
John McCalle78aac42010-03-10 03:28:59 +0000626void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
627 Writer.AddSourceLocation(TL.getNameLoc(), Record);
628}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000629void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000630 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000631 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000632 Writer.AddSourceLocation(TL.getNameLoc(), Record);
633}
John McCallc392f372010-06-11 00:33:02 +0000634void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
635 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000636 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000637 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000638 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000639 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000640 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
641 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
642 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000643 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
644 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000645}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000646void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
647 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
648}
John McCall17001972009-10-18 01:05:36 +0000649void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
650 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000651}
652void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
653 Record.push_back(TL.hasBaseTypeAsWritten());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000654 Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record);
655 Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record);
656 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
657 Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record);
658 Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record);
659 Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000660 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
661 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000662}
John McCallfc93cf92009-10-22 22:37:11 +0000663void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
664 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000665}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000666void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
667 Writer.AddSourceLocation(TL.getKWLoc(), Record);
668 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
669 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
670}
John McCall8f115c62009-10-16 21:56:05 +0000671
Richard Smith01b2cb42014-07-26 06:37:51 +0000672void ASTWriter::WriteTypeAbbrevs() {
673 using namespace llvm;
674
675 BitCodeAbbrev *Abv;
676
677 // Abbreviation for TYPE_EXT_QUAL
678 Abv = new BitCodeAbbrev();
679 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
680 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
682 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
683
684 // Abbreviation for TYPE_FUNCTION_PROTO
685 Abv = new BitCodeAbbrev();
686 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
687 // FunctionType
688 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
689 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
690 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
691 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
692 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
693 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
694 // FunctionProtoType
695 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
696 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
697 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
698 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
699 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
700 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
701 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
702 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
703 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
704}
705
Chris Lattner19cea4e2009-04-22 05:57:30 +0000706//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000707// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000708//===----------------------------------------------------------------------===//
709
Chris Lattner28fa4e62009-04-26 22:26:21 +0000710static void EmitBlockID(unsigned ID, const char *Name,
711 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000712 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 Record.clear();
714 Record.push_back(ID);
715 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
716
717 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000718 if (!Name || Name[0] == 0)
719 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000720 Record.clear();
721 while (*Name)
722 Record.push_back(*Name++);
723 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
724}
725
726static void EmitRecordID(unsigned ID, const char *Name,
727 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000728 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000729 Record.clear();
730 Record.push_back(ID);
731 while (*Name)
732 Record.push_back(*Name++);
733 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000734}
735
736static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000737 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000738#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000739 RECORD(STMT_STOP);
740 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000741 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000742 RECORD(STMT_NULL);
743 RECORD(STMT_COMPOUND);
744 RECORD(STMT_CASE);
745 RECORD(STMT_DEFAULT);
746 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000747 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000748 RECORD(STMT_IF);
749 RECORD(STMT_SWITCH);
750 RECORD(STMT_WHILE);
751 RECORD(STMT_DO);
752 RECORD(STMT_FOR);
753 RECORD(STMT_GOTO);
754 RECORD(STMT_INDIRECT_GOTO);
755 RECORD(STMT_CONTINUE);
756 RECORD(STMT_BREAK);
757 RECORD(STMT_RETURN);
758 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000759 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000760 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000761 RECORD(EXPR_PREDEFINED);
762 RECORD(EXPR_DECL_REF);
763 RECORD(EXPR_INTEGER_LITERAL);
764 RECORD(EXPR_FLOATING_LITERAL);
765 RECORD(EXPR_IMAGINARY_LITERAL);
766 RECORD(EXPR_STRING_LITERAL);
767 RECORD(EXPR_CHARACTER_LITERAL);
768 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000769 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000770 RECORD(EXPR_UNARY_OPERATOR);
771 RECORD(EXPR_SIZEOF_ALIGN_OF);
772 RECORD(EXPR_ARRAY_SUBSCRIPT);
773 RECORD(EXPR_CALL);
774 RECORD(EXPR_MEMBER);
775 RECORD(EXPR_BINARY_OPERATOR);
776 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
777 RECORD(EXPR_CONDITIONAL_OPERATOR);
778 RECORD(EXPR_IMPLICIT_CAST);
779 RECORD(EXPR_CSTYLE_CAST);
780 RECORD(EXPR_COMPOUND_LITERAL);
781 RECORD(EXPR_EXT_VECTOR_ELEMENT);
782 RECORD(EXPR_INIT_LIST);
783 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000784 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000785 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000786 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000787 RECORD(EXPR_VA_ARG);
788 RECORD(EXPR_ADDR_LABEL);
789 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000790 RECORD(EXPR_CHOOSE);
791 RECORD(EXPR_GNU_NULL);
792 RECORD(EXPR_SHUFFLE_VECTOR);
793 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000794 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000795 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000796 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000797 RECORD(EXPR_OBJC_ARRAY_LITERAL);
798 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000799 RECORD(EXPR_OBJC_ENCODE);
800 RECORD(EXPR_OBJC_SELECTOR_EXPR);
801 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
802 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
803 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
804 RECORD(EXPR_OBJC_KVC_REF_EXPR);
805 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000806 RECORD(STMT_OBJC_FOR_COLLECTION);
807 RECORD(STMT_OBJC_CATCH);
808 RECORD(STMT_OBJC_FINALLY);
809 RECORD(STMT_OBJC_AT_TRY);
810 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
811 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000812 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000813 RECORD(STMT_CXX_CATCH);
814 RECORD(STMT_CXX_TRY);
815 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000816 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000817 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000818 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000819 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000820 RECORD(EXPR_CXX_STATIC_CAST);
821 RECORD(EXPR_CXX_DYNAMIC_CAST);
822 RECORD(EXPR_CXX_REINTERPRET_CAST);
823 RECORD(EXPR_CXX_CONST_CAST);
824 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000825 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000826 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000827 RECORD(EXPR_CXX_BOOL_LITERAL);
828 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000829 RECORD(EXPR_CXX_TYPEID_EXPR);
830 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000831 RECORD(EXPR_CXX_THIS);
832 RECORD(EXPR_CXX_THROW);
833 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000834 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000835 RECORD(EXPR_CXX_BIND_TEMPORARY);
836 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
837 RECORD(EXPR_CXX_NEW);
838 RECORD(EXPR_CXX_DELETE);
839 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
840 RECORD(EXPR_EXPR_WITH_CLEANUPS);
841 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
842 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
843 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
844 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
845 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000846 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000847 RECORD(EXPR_CXX_NOEXCEPT);
848 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000849 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
850 RECORD(EXPR_TYPE_TRAIT);
851 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000852 RECORD(EXPR_PACK_EXPANSION);
853 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000854 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000855 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000856 RECORD(EXPR_FUNCTION_PARM_PACK);
857 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000858 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000859 RECORD(EXPR_CXX_UUIDOF_EXPR);
860 RECORD(EXPR_CXX_UUIDOF_TYPE);
861 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000862#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000863}
Mike Stump11289f42009-09-09 15:08:12 +0000864
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000865void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000866 RecordData Record;
867 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000868
Sebastian Redl539c5062010-08-18 23:57:32 +0000869#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
870#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000871
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000872 // Control Block.
873 BLOCK(CONTROL_BLOCK);
874 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000875 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000876 RECORD(MODULE_NAME);
877 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000878 RECORD(IMPORTS);
Richard Smith7f330cd2015-03-18 01:42:29 +0000879 RECORD(KNOWN_MODULE_FILES);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000880 RECORD(LANGUAGE_OPTIONS);
881 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000882 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000883 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000884 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000885 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000886 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000887 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000888 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000889 RECORD(PREPROCESSOR_OPTIONS);
890
Douglas Gregor108cb222012-10-19 00:45:00 +0000891 BLOCK(INPUT_FILES_BLOCK);
892 RECORD(INPUT_FILE);
893
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000894 // AST Top-Level Block.
895 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000896 RECORD(TYPE_OFFSET);
897 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000898 RECORD(IDENTIFIER_OFFSET);
899 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000900 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000901 RECORD(SPECIAL_TYPES);
902 RECORD(STATISTICS);
903 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000904 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000905 RECORD(SELECTOR_OFFSETS);
906 RECORD(METHOD_POOL);
907 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000908 RECORD(SOURCE_LOCATION_OFFSETS);
909 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000910 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000911 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000912 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000913 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000914 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000915 RECORD(SEMA_DECL_REFS);
916 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
917 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
918 RECORD(DECL_REPLACEMENTS);
919 RECORD(UPDATE_VISIBLE);
920 RECORD(DECL_UPDATE_OFFSETS);
921 RECORD(DECL_UPDATES);
922 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
923 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000924 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000925 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000926 RECORD(FP_PRAGMA_OPTIONS);
927 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000928 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000929 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000930 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000931 RECORD(MODULE_OFFSET_MAP);
932 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000933 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000934 RECORD(FILE_SORTED_DECLS);
935 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000936 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000937 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000938 RECORD(MACRO_OFFSET);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000939 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000940 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor358cd442012-01-15 16:58:34 +0000941
Chris Lattner28fa4e62009-04-26 22:26:21 +0000942 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000943 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000944 RECORD(SM_SLOC_FILE_ENTRY);
945 RECORD(SM_SLOC_BUFFER_ENTRY);
946 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000947 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000948
Chris Lattner28fa4e62009-04-26 22:26:21 +0000949 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000950 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000951 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000952 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +0000953 RECORD(PP_MACRO_OBJECT_LIKE);
954 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000955 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000956
Douglas Gregor12bfa382009-10-17 00:13:19 +0000957 // Decls and Types block.
958 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000959 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000960 RECORD(TYPE_COMPLEX);
961 RECORD(TYPE_POINTER);
962 RECORD(TYPE_BLOCK_POINTER);
963 RECORD(TYPE_LVALUE_REFERENCE);
964 RECORD(TYPE_RVALUE_REFERENCE);
965 RECORD(TYPE_MEMBER_POINTER);
966 RECORD(TYPE_CONSTANT_ARRAY);
967 RECORD(TYPE_INCOMPLETE_ARRAY);
968 RECORD(TYPE_VARIABLE_ARRAY);
969 RECORD(TYPE_VECTOR);
970 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000971 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000972 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000973 RECORD(TYPE_TYPEDEF);
974 RECORD(TYPE_TYPEOF_EXPR);
975 RECORD(TYPE_TYPEOF);
976 RECORD(TYPE_RECORD);
977 RECORD(TYPE_ENUM);
978 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000979 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000980 RECORD(TYPE_DECLTYPE);
981 RECORD(TYPE_ELABORATED);
982 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
983 RECORD(TYPE_UNRESOLVED_USING);
984 RECORD(TYPE_INJECTED_CLASS_NAME);
985 RECORD(TYPE_OBJC_OBJECT);
986 RECORD(TYPE_TEMPLATE_TYPE_PARM);
987 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
988 RECORD(TYPE_DEPENDENT_NAME);
989 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
990 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
991 RECORD(TYPE_PAREN);
992 RECORD(TYPE_PACK_EXPANSION);
993 RECORD(TYPE_ATTRIBUTED);
994 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000995 RECORD(TYPE_AUTO);
996 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000997 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000998 RECORD(TYPE_DECAYED);
999 RECORD(TYPE_ADJUSTED);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001000 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001001 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001002 RECORD(DECL_ENUM);
1003 RECORD(DECL_RECORD);
1004 RECORD(DECL_ENUM_CONSTANT);
1005 RECORD(DECL_FUNCTION);
1006 RECORD(DECL_OBJC_METHOD);
1007 RECORD(DECL_OBJC_INTERFACE);
1008 RECORD(DECL_OBJC_PROTOCOL);
1009 RECORD(DECL_OBJC_IVAR);
1010 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001011 RECORD(DECL_OBJC_CATEGORY);
1012 RECORD(DECL_OBJC_CATEGORY_IMPL);
1013 RECORD(DECL_OBJC_IMPLEMENTATION);
1014 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1015 RECORD(DECL_OBJC_PROPERTY);
1016 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001017 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001018 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001019 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001020 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001021 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001022 RECORD(DECL_FILE_SCOPE_ASM);
1023 RECORD(DECL_BLOCK);
1024 RECORD(DECL_CONTEXT_LEXICAL);
1025 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001026 RECORD(DECL_NAMESPACE);
1027 RECORD(DECL_NAMESPACE_ALIAS);
1028 RECORD(DECL_USING);
1029 RECORD(DECL_USING_SHADOW);
1030 RECORD(DECL_USING_DIRECTIVE);
1031 RECORD(DECL_UNRESOLVED_USING_VALUE);
1032 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1033 RECORD(DECL_LINKAGE_SPEC);
1034 RECORD(DECL_CXX_RECORD);
1035 RECORD(DECL_CXX_METHOD);
1036 RECORD(DECL_CXX_CONSTRUCTOR);
1037 RECORD(DECL_CXX_DESTRUCTOR);
1038 RECORD(DECL_CXX_CONVERSION);
1039 RECORD(DECL_ACCESS_SPEC);
1040 RECORD(DECL_FRIEND);
1041 RECORD(DECL_FRIEND_TEMPLATE);
1042 RECORD(DECL_CLASS_TEMPLATE);
1043 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1044 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001045 RECORD(DECL_VAR_TEMPLATE);
1046 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1047 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001048 RECORD(DECL_FUNCTION_TEMPLATE);
1049 RECORD(DECL_TEMPLATE_TYPE_PARM);
1050 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1051 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1052 RECORD(DECL_STATIC_ASSERT);
1053 RECORD(DECL_CXX_BASE_SPECIFIERS);
1054 RECORD(DECL_INDIRECTFIELD);
1055 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1056
Douglas Gregor03412ba2011-06-03 02:27:19 +00001057 // Statements and Exprs can occur in the Decls and Types block.
1058 AddStmtsExprs(Stream, Record);
1059
Douglas Gregor92a96f52011-02-08 21:58:10 +00001060 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001061 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001062 RECORD(PPD_MACRO_DEFINITION);
1063 RECORD(PPD_INCLUSION_DIRECTIVE);
1064
Chris Lattner28fa4e62009-04-26 22:26:21 +00001065#undef RECORD
1066#undef BLOCK
1067 Stream.ExitBlock();
1068}
1069
Richard Smith54cc3c22014-12-11 20:50:24 +00001070/// \brief Prepares a path for being written to an AST file by converting it
1071/// to an absolute path and removing nested './'s.
1072///
1073/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001074static bool cleanPathForOutput(FileManager &FileMgr,
1075 SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00001076 bool Changed = false;
1077
1078 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
1079 llvm::sys::fs::make_absolute(Path);
1080 Changed = true;
1081 }
1082
1083 return Changed | FileMgr.removeDotPaths(Path);
1084}
1085
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001086/// \brief Adjusts the given filename to only write out the portion of the
1087/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001088///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001089/// \param Filename the file name to adjust.
1090///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001091/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1092/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001093///
1094/// \returns either the original filename (if it needs no adjustment) or the
1095/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001096static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001097adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001098 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001099
Richard Smith7ed1bc92014-12-05 22:42:13 +00001100 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001101 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001102
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001103 // Verify that the filename and the system root have the same prefix.
1104 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001105 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1106 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001107 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001108
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001109 // We hit the end of the filename before we hit the end of the system root.
1110 if (!Filename[Pos])
1111 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001112
Richard Smith7ed1bc92014-12-05 22:42:13 +00001113 // If there's not a path separator at the end of the base directory nor
1114 // immediately after it, then this isn't within the base directory.
1115 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1116 if (!llvm::sys::path::is_separator(BaseDir.back()))
1117 return Filename;
1118 } else {
1119 // If the file name has a '/' at the current position, skip over the '/'.
1120 // We distinguish relative paths from absolute paths by the
1121 // absence of '/' at the beginning of relative paths.
1122 //
1123 // FIXME: This is wrong. We distinguish them by asking if the path is
1124 // absolute, which isn't the same thing. And there might be multiple '/'s
1125 // in a row. Use a better mechanism to indicate whether we have emitted an
1126 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001127 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001128 }
Mike Stump11289f42009-09-09 15:08:12 +00001129
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001130 return Filename + Pos;
1131}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001132
Ben Langmuir487ea142014-10-23 18:05:36 +00001133static ASTFileSignature getSignature() {
1134 while (1) {
1135 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1136 return S;
1137 // Rely on GetRandomNumber to eventually return non-zero...
1138 }
1139}
1140
Douglas Gregor112b9072012-10-18 05:31:06 +00001141/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001142void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1143 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001144 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001145 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001146 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1147 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001148
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001149 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001150 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1151 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1152 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1153 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1154 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1155 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1156 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1157 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1158 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1159 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1160 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001161 Record.push_back(VERSION_MAJOR);
1162 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001163 Record.push_back(CLANG_VERSION_MAJOR);
1164 Record.push_back(CLANG_VERSION_MINOR);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001165 assert((!WritingModule || isysroot.empty()) &&
1166 "writing module as a relocatable PCH?");
Douglas Gregorc567ba22011-07-22 16:35:34 +00001167 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001168 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001169 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1170 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001171
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001172 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001173 // For implicit modules we output a signature that we can use to ensure
1174 // duplicate module builds don't collide in the cache as their output order
1175 // is non-deterministic.
1176 // FIXME: Remove this when output is deterministic.
1177 if (Context.getLangOpts().ImplicitModules) {
1178 Record.clear();
1179 Record.push_back(getSignature());
1180 Stream.EmitRecord(SIGNATURE, Record);
1181 }
1182
Richard Smith7ed1bc92014-12-05 22:42:13 +00001183 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001184 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1185 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1187 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1188 RecordData Record;
1189 Record.push_back(MODULE_NAME);
1190 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1191 }
1192
Richard Smith7ed1bc92014-12-05 22:42:13 +00001193 if (WritingModule && WritingModule->Directory) {
1194 // Module directory.
1195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1196 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1198 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1199 RecordData Record;
1200 Record.push_back(MODULE_DIRECTORY);
1201
1202 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001203 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001204 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1205
1206 // Write out all other paths relative to the base directory if possible.
1207 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1208 } else if (!isysroot.empty()) {
1209 // Write out paths relative to the sysroot if possible.
1210 BaseDirectory = isysroot;
1211 }
1212
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001213 // Module map file
1214 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001215 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001216
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001217 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1218
1219 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001220 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001221
1222 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001223 if (auto *AdditionalModMaps =
1224 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001225 Record.push_back(AdditionalModMaps->size());
1226 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001227 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001228 } else {
1229 Record.push_back(0);
1230 }
1231
1232 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001233 }
1234
Douglas Gregor112b9072012-10-18 05:31:06 +00001235 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001236 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001237 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001238 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001239
Richard Smith7f330cd2015-03-18 01:42:29 +00001240 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001241 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001242 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001243 continue;
1244
Richard Smith7f330cd2015-03-18 01:42:29 +00001245 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1246 AddSourceLocation(M->ImportLoc, Record);
1247 Record.push_back(M->File->getSize());
1248 Record.push_back(M->File->getModificationTime());
1249 Record.push_back(M->Signature);
1250 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001251 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001252 Stream.EmitRecord(IMPORTS, Record);
Richard Smith7f330cd2015-03-18 01:42:29 +00001253
1254 // Also emit a list of known module files that were not imported,
1255 // but are made available by this module.
1256 // FIXME: Should we also include a signature here?
1257 Record.clear();
1258 for (auto *E : Mgr.getAdditionalKnownModuleFiles())
1259 AddPath(E->getName(), Record);
1260 if (!Record.empty())
1261 Stream.EmitRecord(KNOWN_MODULE_FILES, Record);
Douglas Gregor29cc6422011-08-17 21:07:30 +00001262 }
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregor112b9072012-10-18 05:31:06 +00001264 // Language options.
1265 Record.clear();
1266 const LangOptions &LangOpts = Context.getLangOpts();
1267#define LANGOPT(Name, Bits, Default, Description) \
1268 Record.push_back(LangOpts.Name);
1269#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1270 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001271#include "clang/Basic/LangOptions.def"
1272#define SANITIZER(NAME, ID) \
1273 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001274#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001275
Ben Langmuircd98cb72015-06-23 18:20:18 +00001276 Record.push_back(LangOpts.ModuleFeatures.size());
1277 for (StringRef Feature : LangOpts.ModuleFeatures)
1278 AddString(Feature, Record);
1279
Douglas Gregor112b9072012-10-18 05:31:06 +00001280 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1281 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001282
1283 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001284
1285 // Comment options.
1286 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1287 for (CommentOptions::BlockCommandNamesTy::const_iterator
1288 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1289 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1290 I != IEnd; ++I) {
1291 AddString(*I, Record);
1292 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001293 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001294
Douglas Gregor112b9072012-10-18 05:31:06 +00001295 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1296
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001297 // Target options.
1298 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001299 const TargetInfo &Target = Context.getTargetInfo();
1300 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001301 AddString(TargetOpts.Triple, Record);
1302 AddString(TargetOpts.CPU, Record);
1303 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001304 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1305 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1306 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1307 }
1308 Record.push_back(TargetOpts.Features.size());
1309 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1310 AddString(TargetOpts.Features[I], Record);
1311 }
1312 Stream.EmitRecord(TARGET_OPTIONS, Record);
1313
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001314 // Diagnostic options.
1315 Record.clear();
1316 const DiagnosticOptions &DiagOpts
1317 = Context.getDiagnostics().getDiagnosticOptions();
1318#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1319#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1320 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1321#include "clang/Basic/DiagnosticOptions.def"
1322 Record.push_back(DiagOpts.Warnings.size());
1323 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1324 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001325 Record.push_back(DiagOpts.Remarks.size());
1326 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1327 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001328 // Note: we don't serialize the log or serialization file names, because they
1329 // are generally transient files and will almost always be overridden.
1330 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1331
Douglas Gregorc6317db2012-10-24 15:49:58 +00001332 // File system options.
1333 Record.clear();
1334 const FileSystemOptions &FSOpts
1335 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1336 AddString(FSOpts.WorkingDir, Record);
1337 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1338
Douglas Gregor2d302362012-10-24 16:50:34 +00001339 // Header search options.
1340 Record.clear();
1341 const HeaderSearchOptions &HSOpts
1342 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1343 AddString(HSOpts.Sysroot, Record);
1344
1345 // Include entries.
1346 Record.push_back(HSOpts.UserEntries.size());
1347 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1348 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1349 AddString(Entry.Path, Record);
1350 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001351 Record.push_back(Entry.IsFramework);
1352 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001353 }
1354
1355 // System header prefixes.
1356 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1357 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1358 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1359 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1360 }
1361
1362 AddString(HSOpts.ResourceDir, Record);
1363 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001364 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001365 Record.push_back(HSOpts.DisableModuleHash);
1366 Record.push_back(HSOpts.UseBuiltinIncludes);
1367 Record.push_back(HSOpts.UseStandardSystemIncludes);
1368 Record.push_back(HSOpts.UseStandardCXXIncludes);
1369 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001370 // Write out the specific module cache path that contains the module files.
1371 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001372 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1373
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001374 // Preprocessor options.
1375 Record.clear();
1376 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1377
1378 // Macro definitions.
1379 Record.push_back(PPOpts.Macros.size());
1380 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1381 AddString(PPOpts.Macros[I].first, Record);
1382 Record.push_back(PPOpts.Macros[I].second);
1383 }
1384
1385 // Includes
1386 Record.push_back(PPOpts.Includes.size());
1387 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1388 AddString(PPOpts.Includes[I], Record);
1389
1390 // Macro includes
1391 Record.push_back(PPOpts.MacroIncludes.size());
1392 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1393 AddString(PPOpts.MacroIncludes[I], Record);
1394
Douglas Gregorb6368752012-10-24 23:41:50 +00001395 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001396 // Detailed record is important since it is used for the module cache hash.
1397 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001398 AddString(PPOpts.ImplicitPCHInclude, Record);
1399 AddString(PPOpts.ImplicitPTHInclude, Record);
1400 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1401 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1402
Douglas Gregora3b20262011-05-06 21:43:30 +00001403 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001404 SourceManager &SM = Context.getSourceManager();
1405 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1406 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001407 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1408 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001409 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1410 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1411
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001412 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001413 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001414 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001415 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001416 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001417
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001418 Record.clear();
1419 Record.push_back(SM.getMainFileID().getOpaqueValue());
1420 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1421
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001422 // Original PCH directory
1423 if (!OutputFile.empty() && OutputFile != "-") {
1424 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1425 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1427 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1428
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001429 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001430
1431 llvm::sys::fs::make_absolute(OutputPath);
1432 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1433
1434 RecordData Record;
1435 Record.push_back(ORIGINAL_PCH_DIR);
1436 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1437 }
1438
Douglas Gregor49491f72013-03-15 22:15:07 +00001439 WriteInputFiles(Context.SourceMgr,
1440 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001441 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001442 Stream.ExitBlock();
1443}
1444
Douglas Gregor49491f72013-03-15 22:15:07 +00001445namespace {
1446 /// \brief An input file.
1447 struct InputFileEntry {
1448 const FileEntry *File;
1449 bool IsSystemFile;
1450 bool BufferOverridden;
1451 };
1452}
1453
1454void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1455 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001456 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001457 using namespace llvm;
1458 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1459 RecordData Record;
1460
1461 // Create input-file abbreviation.
1462 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1463 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001464 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001465 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1466 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001467 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001468 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1469 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1470
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001471 // Get all ContentCache objects for files, sorted by whether the file is a
1472 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001473 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001474 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1475 // Get this source location entry.
1476 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001477 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001478
1479 // We only care about file entries that were not overridden.
1480 if (!SLoc->isFile())
1481 continue;
1482 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001483 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001484 continue;
1485
Douglas Gregor49491f72013-03-15 22:15:07 +00001486 InputFileEntry Entry;
1487 Entry.File = Cache->OrigEntry;
1488 Entry.IsSystemFile = Cache->IsSystemFile;
1489 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001490 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001491 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001492 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001493 SortedFiles.push_front(Entry);
1494 }
1495
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001496 unsigned UserFilesNum = 0;
1497 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001498 std::vector<uint64_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001499 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001500 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001501 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001502
Douglas Gregor49491f72013-03-15 22:15:07 +00001503 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001504 if (InputFileID != 0)
1505 continue; // already recorded this file.
1506
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001507 // Record this entry's offset.
1508 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001509
1510 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001511
Douglas Gregor49491f72013-03-15 22:15:07 +00001512 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001513 ++UserFilesNum;
1514
Douglas Gregor72be3902012-10-19 00:38:02 +00001515 Record.clear();
1516 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001517 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001518
1519 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001520 Record.push_back(Entry.File->getSize());
1521 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001522
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001523 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001524 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001525
Richard Smith7ed1bc92014-12-05 22:42:13 +00001526 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1527 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001528
Douglas Gregor112b9072012-10-18 05:31:06 +00001529 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001530
1531 // Create input file offsets abbreviation.
1532 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1533 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1534 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001535 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1536 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001537 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1538 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1539
1540 // Write input file offsets.
1541 Record.clear();
1542 Record.push_back(INPUT_FILE_OFFSETS);
1543 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001544 Record.push_back(UserFilesNum);
Reid Kleckner012665e2015-05-21 00:13:09 +00001545 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001546}
1547
Douglas Gregora7f71a92009-04-10 03:52:48 +00001548//===----------------------------------------------------------------------===//
1549// Source Manager Serialization
1550//===----------------------------------------------------------------------===//
1551
1552/// \brief Create an abbreviation for the SLocEntry that refers to a
1553/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001554static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001555 using namespace llvm;
1556 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001557 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001562 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001567 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001568}
1569
1570/// \brief Create an abbreviation for the SLocEntry that refers to a
1571/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001572static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573 using namespace llvm;
1574 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001575 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001581 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001582}
1583
1584/// \brief Create an abbreviation for the SLocEntry that refers to a
1585/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001586static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001587 using namespace llvm;
1588 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001589 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001591 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001592}
1593
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001594/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1595/// expansion.
1596static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001597 using namespace llvm;
1598 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001599 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001600 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001605 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001606}
1607
Douglas Gregor09b69892011-02-10 17:09:37 +00001608namespace {
1609 // Trait used for the on-disk hash table of header search information.
1610 class HeaderFileInfoTrait {
1611 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001612 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001613
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001614 // Keep track of the framework names we've used during serialization.
1615 SmallVector<char, 128> FrameworkStringData;
1616 llvm::StringMap<unsigned> FrameworkNameOffset;
1617
Douglas Gregor09b69892011-02-10 17:09:37 +00001618 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001619 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1620 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001621
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001622 struct key_type {
1623 const FileEntry *FE;
1624 const char *Filename;
1625 };
1626 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001627
1628 typedef HeaderFileInfo data_type;
1629 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001630 typedef unsigned hash_value_type;
1631 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001632
Justin Bogner25463f12014-04-18 20:27:24 +00001633 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001634 // The hash is based only on size/time of the file, so that the reader can
1635 // match even when symlinking or excess path elements ("foo/../", "../")
1636 // change the form of the name. However, complete path is still the key.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001637 //
1638 // FIXME: Using the mtime here will cause problems for explicit module
1639 // imports.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001640 return llvm::hash_combine(key.FE->getSize(),
1641 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001642 }
1643
1644 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001645 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001646 using namespace llvm::support;
1647 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001648 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001649 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001650 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001651 if (Data.isModuleHeader)
1652 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001653 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001654 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001655 }
1656
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001657 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001658 using namespace llvm::support;
1659 endian::Writer<little> LE(Out);
1660 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001661 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001662 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001663 KeyLen -= 8;
1664 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001665 }
1666
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001667 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001668 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001669 using namespace llvm::support;
1670 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001671 uint64_t Start = Out.tell(); (void)Start;
1672
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001673 unsigned char Flags = (Data.HeaderRole << 6)
1674 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001675 | (Data.isPragmaOnce << 4)
1676 | (Data.DirInfo << 2)
1677 | (Data.Resolved << 1)
1678 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001679 LE.write<uint8_t>(Flags);
1680 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001681
1682 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001683 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001684 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001685 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001686
1687 unsigned Offset = 0;
1688 if (!Data.Framework.empty()) {
1689 // If this header refers into a framework, save the framework name.
1690 llvm::StringMap<unsigned>::iterator Pos
1691 = FrameworkNameOffset.find(Data.Framework);
1692 if (Pos == FrameworkNameOffset.end()) {
1693 Offset = FrameworkStringData.size() + 1;
1694 FrameworkStringData.append(Data.Framework.begin(),
1695 Data.Framework.end());
1696 FrameworkStringData.push_back(0);
1697
1698 FrameworkNameOffset[Data.Framework] = Offset;
1699 } else
1700 Offset = Pos->second;
1701 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001702 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001703
1704 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001705 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001706 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001707 }
1708
Douglas Gregor09b69892011-02-10 17:09:37 +00001709 assert(Out.tell() - Start == DataLen && "Wrong data length");
1710 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001711
1712 const char *strings_begin() const { return FrameworkStringData.begin(); }
1713 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001714 };
1715} // end anonymous namespace
1716
1717/// \brief Write the header search block for the list of files that
1718///
1719/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001720void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001721 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001722 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1723
1724 if (FilesByUID.size() > HS.header_file_size())
1725 FilesByUID.resize(HS.header_file_size());
1726
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001727 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001728 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001729 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001730 unsigned NumHeaderSearchEntries = 0;
1731 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1732 const FileEntry *File = FilesByUID[UID];
1733 if (!File)
1734 continue;
1735
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001736 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1737 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001738 HeaderFileInfo HFI;
1739 if (!HS.tryGetFileInfo(File, HFI) ||
1740 (HFI.External && Chain) ||
1741 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001742 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001743
Richard Smith7ed1bc92014-12-05 22:42:13 +00001744 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001745 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001746 SmallString<128> FilenameTmp(Filename);
1747 if (PreparePathForOutput(FilenameTmp)) {
1748 // If we performed any translation on the file name at all, we need to
1749 // save this string, since the generator will refer to it later.
1750 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001751 SavedStrings.push_back(Filename);
1752 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001753
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001754 HeaderFileInfoTrait::key_type key = { File, Filename };
1755 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001756 ++NumHeaderSearchEntries;
1757 }
1758
1759 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001760 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001761 uint32_t BucketOffset;
1762 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001763 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001764 llvm::raw_svector_ostream Out(TableData);
1765 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001766 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001767 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1768 }
1769
1770 // Create a blob abbreviation
1771 using namespace llvm;
1772 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1773 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1778 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1779
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001780 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001781 RecordData Record;
1782 Record.push_back(HEADER_SEARCH_TABLE);
1783 Record.push_back(BucketOffset);
1784 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001785 Record.push_back(TableData.size());
1786 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001787 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001788
1789 // Free all of the strings we had to duplicate.
1790 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001791 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001792}
1793
Douglas Gregora7f71a92009-04-10 03:52:48 +00001794/// \brief Writes the block containing the serialized form of the
1795/// source manager.
1796///
1797/// TODO: We should probably use an on-disk hash table (stored in a
1798/// blob), indexed based on the file name, so that we only create
1799/// entries for files that we actually need. In the common case (no
1800/// errors), we probably won't have to create file entries for any of
1801/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001802void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001803 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001804 RecordData Record;
1805
Chris Lattner0910e3b2009-04-10 17:16:57 +00001806 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001807 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001808
1809 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001810 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1811 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1812 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001813 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001814
Douglas Gregor258ae542009-04-27 06:38:32 +00001815 // Write out the source location entry table. We skip the first
1816 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001817 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001818 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001819 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1820 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001821 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001822 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001823 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001824 FileID FID = FileID::get(I);
1825 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001826
Douglas Gregor258ae542009-04-27 06:38:32 +00001827 // Record the offset of this source-location entry.
1828 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1829
1830 // Figure out which record code to use.
1831 unsigned Code;
1832 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001833 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1834 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001835 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001836 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001837 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001838 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001839 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001840 Record.clear();
1841 Record.push_back(Code);
1842
Douglas Gregor925296b2011-07-19 16:10:42 +00001843 // Starting offset of this entry within this module, so skip the dummy.
1844 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001845 if (SLoc->isFile()) {
1846 const SrcMgr::FileInfo &File = SLoc->getFile();
1847 Record.push_back(File.getIncludeLoc().getRawEncoding());
1848 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1849 Record.push_back(File.hasLineDirectives());
1850
1851 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001852 if (Content->OrigEntry) {
1853 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001854 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001855
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001856 // The source location entry is a file. Emit input file ID.
1857 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1858 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001859
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001860 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001861
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001862 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001863 if (FDI != FileDeclIDs.end()) {
1864 Record.push_back(FDI->second->FirstDeclIndex);
1865 Record.push_back(FDI->second->DeclIDs.size());
1866 } else {
1867 Record.push_back(0);
1868 Record.push_back(0);
1869 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001870
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001871 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001872
1873 if (Content->BufferOverridden) {
1874 Record.clear();
1875 Record.push_back(SM_SLOC_BUFFER_BLOB);
1876 const llvm::MemoryBuffer *Buffer
1877 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1878 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1879 StringRef(Buffer->getBufferStart(),
1880 Buffer->getBufferSize() + 1));
1881 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001882 } else {
1883 // The source location entry is a buffer. The blob associated
1884 // with this entry contains the contents of the buffer.
1885
1886 // We add one to the size so that we capture the trailing NULL
1887 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1888 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001889 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001890 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001891 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001892 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001893 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001894 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001895 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001896 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001897 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001898 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001899
Douglas Gregor925296b2011-07-19 16:10:42 +00001900 if (strcmp(Name, "<built-in>") == 0) {
1901 PreloadSLocs.push_back(SLocEntryOffsets.size());
1902 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001903 }
1904 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001905 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001906 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001907 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1908 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001909 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1910 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001911
1912 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001913 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001914 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001915 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001916 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001917 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001918 }
1919 }
1920
Douglas Gregor8f45df52009-04-16 22:23:12 +00001921 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001922
1923 if (SLocEntryOffsets.empty())
1924 return;
1925
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001926 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001927 // table is used for lazily loading source-location information.
1928 using namespace llvm;
1929 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1934 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001935
Douglas Gregor258ae542009-04-27 06:38:32 +00001936 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001937 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001938 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001939 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Reid Kleckner012665e2015-05-21 00:13:09 +00001940 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, bytes(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001941
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001942 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001943 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001944 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001945
1946 // Write the line table. It depends on remapping working, so it must come
1947 // after the source location offsets.
1948 if (SourceMgr.hasLineTable()) {
1949 LineTableInfo &LineTable = SourceMgr.getLineTable();
1950
1951 Record.clear();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001952 // Emit the file names.
Douglas Gregor925296b2011-07-19 16:10:42 +00001953 Record.push_back(LineTable.getNumFilenames());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001954 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I)
1955 AddPath(LineTable.getFilename(I), Record);
Douglas Gregor925296b2011-07-19 16:10:42 +00001956
1957 // Emit the line entries
1958 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1959 L != LEnd; ++L) {
1960 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001961 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001962 continue;
1963
1964 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001965 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001966
1967 // Emit the line entries
1968 Record.push_back(L->second.size());
1969 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1970 LEEnd = L->second.end();
1971 LE != LEEnd; ++LE) {
1972 Record.push_back(LE->FileOffset);
1973 Record.push_back(LE->LineNo);
1974 Record.push_back(LE->FilenameID);
1975 Record.push_back((unsigned)LE->FileKind);
1976 Record.push_back(LE->IncludeOffset);
1977 }
1978 }
1979 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1980 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001981}
1982
Douglas Gregorc5046832009-04-27 18:38:38 +00001983//===----------------------------------------------------------------------===//
1984// Preprocessor Serialization
1985//===----------------------------------------------------------------------===//
1986
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001987static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1988 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001989 if (MacroInfo *MI = MD->getMacroInfo())
1990 if (MI->isBuiltinMacro())
1991 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001992
1993 if (IsModule) {
1994 SourceLocation Loc = MD->getLocation();
1995 if (Loc.isInvalid())
1996 return true;
1997 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1998 return true;
1999 }
2000
2001 return false;
2002}
2003
Chris Lattnereeffaef2009-04-10 17:15:23 +00002004/// \brief Writes the block containing the serialized form of the
2005/// preprocessor.
2006///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002007void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002008 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2009 if (PPRec)
2010 WritePreprocessorDetail(*PPRec);
2011
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002012 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002013 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002014
Chris Lattner0af3ba12009-04-13 01:29:17 +00002015 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2016 if (PP.getCounterValue() != 0) {
2017 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002018 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002019 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002020 }
2021
2022 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002023 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002024
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002025 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002026 // FIXME: use diagnostics subsystem for localization etc.
2027 if (PP.SawDateOrTime())
2028 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00002029
Douglas Gregor796d76a2010-10-20 22:00:55 +00002030
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002031 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002032 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002033
Richard Smithee977932015-05-01 21:22:17 +00002034 // Construct the list of identifiers with macro directives that need to be
2035 // serialized.
2036 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2037 for (auto &Id : PP.getIdentifierTable())
2038 if (Id.second->hadMacroDefinition() &&
2039 (!Id.second->isFromAST() ||
2040 Id.second->hasChangedSinceDeserialization()))
2041 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002042 // Sort the set of macro definitions that need to be serialized by the
2043 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002044 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2045 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002046
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002047 // Emit the macro directives as a list and associate the offset with the
2048 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002049 for (const IdentifierInfo *Name : MacroIdentifiers) {
2050 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002051 auto StartOffset = Stream.GetCurrentBitNo();
2052
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002053 // Emit the macro directives in reverse source order.
2054 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002055 // Once we hit an ignored macro, we're done: the rest of the chain
2056 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002057 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002058 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002059
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002060 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002061 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002062 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002063 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002064 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002065 Record.push_back(VisMD->isPublic());
2066 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002067 }
Richard Smithd7329392015-04-21 21:46:32 +00002068
Richard Smithb8b2ed62015-04-23 18:18:26 +00002069 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002070 bool EmittedModuleMacros = false;
Richard Smithb8b2ed62015-04-23 18:18:26 +00002071 if (IsModule) {
2072 auto Leafs = PP.getLeafModuleMacros(Name);
2073 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2074 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2075 while (!Worklist.empty()) {
2076 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002077
Richard Smithb8b2ed62015-04-23 18:18:26 +00002078 // Emit a record indicating this submodule exports this macro.
2079 ModuleMacroRecord.push_back(
2080 getSubmoduleID(Macro->getOwningModule()));
Richard Smithee977932015-05-01 21:22:17 +00002081 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
Richard Smithb8b2ed62015-04-23 18:18:26 +00002082 for (auto *M : Macro->overrides())
2083 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002084
Richard Smithb8b2ed62015-04-23 18:18:26 +00002085 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2086 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002087
Richard Smithb8b2ed62015-04-23 18:18:26 +00002088 // Enqueue overridden macros once we've visited all their ancestors.
2089 for (auto *M : Macro->overrides())
2090 if (++Visits[M] == M->getNumOverridingMacros())
2091 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002092
2093 EmittedModuleMacros = true;
Richard Smithd7329392015-04-21 21:46:32 +00002094 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002095 }
Richard Smithd7329392015-04-21 21:46:32 +00002096
Richard Smithee977932015-05-01 21:22:17 +00002097 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002098 continue;
2099
Richard Smithd7329392015-04-21 21:46:32 +00002100 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002101 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2102 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002103 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002104
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002105 /// \brief Offsets of each of the macros into the bitstream, indexed by
2106 /// the local macro ID
2107 ///
2108 /// For each identifier that is associated with a macro, this map
2109 /// provides the offset into the bitstream where that macro is
2110 /// defined.
2111 std::vector<uint32_t> MacroOffsets;
2112
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002113 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2114 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2115 MacroInfo *MI = MacroInfosToEmit[I].MI;
2116 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002117
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002118 if (ID < FirstMacroID) {
2119 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2120 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002121 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002122
2123 // Record the local offset of this macro.
2124 unsigned Index = ID - FirstMacroID;
2125 if (Index == MacroOffsets.size())
2126 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2127 else {
2128 if (Index > MacroOffsets.size())
2129 MacroOffsets.resize(Index + 1);
2130
2131 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2132 }
2133
2134 AddIdentifierRef(Name, Record);
2135 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2136 AddSourceLocation(MI->getDefinitionLoc(), Record);
2137 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2138 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002139 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002140 unsigned Code;
2141 if (MI->isObjectLike()) {
2142 Code = PP_MACRO_OBJECT_LIKE;
2143 } else {
2144 Code = PP_MACRO_FUNCTION_LIKE;
2145
2146 Record.push_back(MI->isC99Varargs());
2147 Record.push_back(MI->isGNUVarargs());
2148 Record.push_back(MI->hasCommaPasting());
2149 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002150 for (const IdentifierInfo *Arg : MI->args())
2151 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002152 }
2153
2154 // If we have a detailed preprocessing record, record the macro definition
2155 // ID that corresponds to this macro.
2156 if (PPRec)
2157 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2158
2159 Stream.EmitRecord(Code, Record);
2160 Record.clear();
2161
2162 // Emit the tokens array.
2163 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2164 // Note that we know that the preprocessor does not have any annotation
2165 // tokens in it because they are created by the parser, and thus can't
2166 // be in a macro definition.
2167 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002168 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002169 Stream.EmitRecord(PP_TOKEN, Record);
2170 Record.clear();
2171 }
2172 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002173 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002174
Douglas Gregor92a96f52011-02-08 21:58:10 +00002175 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002176
2177 // Write the offsets table for macro IDs.
2178 using namespace llvm;
Richard Smith13090a22015-04-10 02:02:24 +00002179 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002180 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2184
2185 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2186 Record.clear();
2187 Record.push_back(MACRO_OFFSET);
2188 Record.push_back(MacroOffsets.size());
2189 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2190 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002191 bytes(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002192}
2193
2194void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002195 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002196 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002197
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002198 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002199
Douglas Gregor92a96f52011-02-08 21:58:10 +00002200 // Enter the preprocessor block.
2201 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002202
Douglas Gregoraae92242010-03-19 21:51:54 +00002203 // If the preprocessor has a preprocessing record, emit it.
2204 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002205 using namespace llvm;
2206
2207 // Set up the abbreviation for
2208 unsigned InclusionAbbrev = 0;
2209 {
2210 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2211 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2217 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2218 }
2219
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002220 unsigned FirstPreprocessorEntityID
2221 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2222 + NUM_PREDEF_PP_ENTITY_IDS;
2223 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002224 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002225 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2226 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002227 E != EEnd;
2228 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002229 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002230
Richard Smith66a81862015-05-04 02:25:31 +00002231 PreprocessedEntityOffsets.push_back(
2232 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002233
Richard Smith66a81862015-05-04 02:25:31 +00002234 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002235 // Record this macro definition's ID.
2236 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002237
Douglas Gregor92a96f52011-02-08 21:58:10 +00002238 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002239 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2240 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002241 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002242
Chandler Carrutha88a22182011-07-14 08:20:46 +00002243 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002244 Record.push_back(ME->isBuiltinMacro());
2245 if (ME->isBuiltinMacro())
2246 AddIdentifierRef(ME->getName(), Record);
2247 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002248 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002249 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002250 continue;
2251 }
2252
2253 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2254 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002255 Record.push_back(ID->getFileName().size());
2256 Record.push_back(ID->wasInQuotes());
2257 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002258 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002259 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002260 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002261 // Check that the FileEntry is not null because it was not resolved and
2262 // we create a PCH even with compiler errors.
2263 if (ID->getFile())
2264 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002265 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2266 continue;
2267 }
2268
2269 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2270 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002271 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002272
Douglas Gregoraae92242010-03-19 21:51:54 +00002273 // Write the offsets table for the preprocessing record.
2274 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002275 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2276
Douglas Gregoraae92242010-03-19 21:51:54 +00002277 // Write the offsets table for identifier IDs.
2278 using namespace llvm;
2279 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002280 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002283 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002284
Douglas Gregoraae92242010-03-19 21:51:54 +00002285 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002286 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002287 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002288 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002289 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002290 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002291}
2292
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002293unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2294 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2295 if (Known != SubmoduleIDs.end())
2296 return Known->second;
2297
2298 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2299}
2300
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002301unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2302 if (!Mod)
2303 return 0;
2304
2305 llvm::DenseMap<Module *, unsigned>::const_iterator
2306 Known = SubmoduleIDs.find(Mod);
2307 if (Known != SubmoduleIDs.end())
2308 return Known->second;
2309
2310 return 0;
2311}
2312
Douglas Gregor253eefe2011-12-01 00:59:36 +00002313/// \brief Compute the number of modules within the given tree (including the
2314/// given module).
2315static unsigned getNumberOfModules(Module *Mod) {
2316 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002317 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2318 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002319 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002320 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002321
2322 return ChildModules + 1;
2323}
2324
Douglas Gregorde3ef502011-11-30 23:21:26 +00002325void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002326 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002327 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002328
2329 // Write the abbreviations needed for the submodules block.
2330 using namespace llvm;
2331 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2332 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2344 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2345
2346 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002347 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2349 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2350
2351 Abbrev = new BitCodeAbbrev();
2352 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2354 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002355
2356 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002357 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2359 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2360
2361 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002362 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2364 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2365
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002366 Abbrev = new BitCodeAbbrev();
2367 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002370 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2371
Douglas Gregor59527662012-10-15 06:28:11 +00002372 Abbrev = new BitCodeAbbrev();
2373 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2375 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2376
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002377 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002378 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2380 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2381
2382 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002383 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2385 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2386
2387 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002388 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2390 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2391
2392 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002393 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2396 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2397
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002398 Abbrev = new BitCodeAbbrev();
2399 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2401 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2402
Douglas Gregorfb912652013-03-20 21:10:35 +00002403 Abbrev = new BitCodeAbbrev();
2404 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2407 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2408
Douglas Gregor253eefe2011-12-01 00:59:36 +00002409 // Write the submodule metadata block.
2410 RecordData Record;
2411 Record.push_back(getNumberOfModules(WritingModule));
2412 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2413 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2414
Douglas Gregor69021972011-11-30 17:33:56 +00002415 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002416 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002417 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002418 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002419 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002420 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002421 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002422
2423 // Emit the definition of the block.
2424 Record.clear();
2425 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002426 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002427 if (Mod->Parent) {
2428 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2429 Record.push_back(SubmoduleIDs[Mod->Parent]);
2430 } else {
2431 Record.push_back(0);
2432 }
2433 Record.push_back(Mod->IsFramework);
2434 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002435 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002436 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002437 Record.push_back(Mod->InferSubmodules);
2438 Record.push_back(Mod->InferExplicitSubmodules);
2439 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002440 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002441 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2442
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002443 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002444 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002445 Record.clear();
2446 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002447 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002448 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002449 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002450 }
2451
Douglas Gregor69021972011-11-30 17:33:56 +00002452 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002453 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002454 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002455 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Richard Smith2b63d152015-05-16 02:28:53 +00002456 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2457 UmbrellaHeader.NameAsWritten);
2458 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Douglas Gregor524e33e2011-12-08 19:11:24 +00002459 Record.clear();
2460 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2461 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002462 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002463 }
Richard Smith306d8922014-10-22 23:50:56 +00002464
Douglas Gregor69021972011-11-30 17:33:56 +00002465 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002466 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002467 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002468 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002469 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002470 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002471 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2472 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2473 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002474 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002475 Module::HK_PrivateTextual},
2476 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002477 };
2478 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002479 Record.clear();
Richard Smith3c1a41a2014-12-02 00:08:08 +00002480 Record.push_back(HL.RecordKind);
2481 for (auto &H : Mod->Headers[HL.HeaderKind])
2482 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2483 }
2484
2485 // Emit the top headers.
2486 {
2487 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2488 Record.clear();
2489 Record.push_back(SUBMODULE_TOPHEADER);
2490 for (auto *H : TopHeaders)
2491 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002492 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002493
2494 // Emit the imports.
2495 if (!Mod->Imports.empty()) {
2496 Record.clear();
2497 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002498 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00002499 assert(ImportedID && "Unknown submodule!");
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002500 Record.push_back(ImportedID);
2501 }
2502 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2503 }
2504
Douglas Gregor24bb9232011-12-02 18:58:38 +00002505 // Emit the exports.
2506 if (!Mod->Exports.empty()) {
2507 Record.clear();
2508 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002509 if (Module *Exported = Mod->Exports[I].getPointer()) {
Richard Smith23d8d032015-05-14 00:45:20 +00002510 unsigned ExportedID = getSubmoduleID(Exported);
Douglas Gregor18b58642011-12-12 23:17:57 +00002511 Record.push_back(ExportedID);
2512 } else {
2513 Record.push_back(0);
2514 }
2515
Douglas Gregor24bb9232011-12-02 18:58:38 +00002516 Record.push_back(Mod->Exports[I].getInt());
2517 }
2518 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2519 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002520
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002521 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2522 // Might be unnecessary as use declarations are only used to build the
2523 // module itself.
2524
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002525 // Emit the link libraries.
2526 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2527 Record.clear();
2528 Record.push_back(SUBMODULE_LINK_LIBRARY);
2529 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2530 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2531 Mod->LinkLibraries[I].Library);
2532 }
2533
Douglas Gregorfb912652013-03-20 21:10:35 +00002534 // Emit the conflicts.
2535 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2536 Record.clear();
2537 Record.push_back(SUBMODULE_CONFLICT);
2538 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2539 assert(OtherID && "Unknown submodule!");
2540 Record.push_back(OtherID);
2541 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2542 Mod->Conflicts[I].Message);
2543 }
2544
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002545 // Emit the configuration macros.
2546 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2547 Record.clear();
2548 Record.push_back(SUBMODULE_CONFIG_MACRO);
2549 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2550 Mod->ConfigMacros[I]);
2551 }
2552
Douglas Gregor69021972011-11-30 17:33:56 +00002553 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002554 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2555 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002556 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002557 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002558 }
2559
2560 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002561
2562 // FIXME: This can easily happen, if we have a reference to a submodule that
2563 // did not result in us loading a module file for that submodule. For
2564 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2565 assert((NextSubmoduleID - FirstSubmoduleID ==
2566 getNumberOfModules(WritingModule)) &&
2567 "Wrong # of submodules; found a reference to a non-local, "
2568 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002569}
2570
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002571serialization::SubmoduleID
2572ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002573 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002574 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002575
2576 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002577 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002578 Module *OwningMod
2579 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002580 if (!OwningMod)
2581 return 0;
2582
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002583 // Check whether this submodule is part of our own module.
2584 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002585 return 0;
2586
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002587 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002588}
2589
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002590void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2591 bool isModule) {
2592 // Make sure set diagnostic pragmas don't affect the translation unit that
2593 // imports the module.
2594 // FIXME: Make diagnostic pragma sections work properly with modules.
2595 if (isModule)
2596 return;
2597
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002598 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2599 DiagStateIDMap;
2600 unsigned CurrID = 0;
2601 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002602 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002603 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002604 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2605 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002606 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002607 if (point.Loc.isInvalid())
2608 continue;
2609
2610 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002611 unsigned &DiagStateID = DiagStateIDMap[point.State];
2612 Record.push_back(DiagStateID);
2613
2614 if (DiagStateID == 0) {
2615 DiagStateID = ++CurrID;
2616 for (DiagnosticsEngine::DiagState::const_iterator
2617 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2618 if (I->second.isPragma()) {
2619 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002620 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002621 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002622 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002623 Record.push_back(-1); // mark the end of the diag/map pairs for this
2624 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002625 }
2626 }
2627
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002628 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002629 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002630}
2631
Richard Smithc2bb8182015-03-24 06:36:48 +00002632void ASTWriter::WriteCXXCtorInitializersOffsets() {
2633 if (CXXCtorInitializersOffsets.empty())
2634 return;
2635
2636 RecordData Record;
2637
2638 // Create a blob abbreviation for the C++ ctor initializer offsets.
2639 using namespace llvm;
2640
2641 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2642 Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2645 unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2646
2647 // Write the base specifier offsets table.
2648 Record.clear();
2649 Record.push_back(CXX_CTOR_INITIALIZERS_OFFSETS);
2650 Record.push_back(CXXCtorInitializersOffsets.size());
2651 Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002652 bytes(CXXCtorInitializersOffsets));
Richard Smithc2bb8182015-03-24 06:36:48 +00002653}
2654
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002655void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2656 if (CXXBaseSpecifiersOffsets.empty())
2657 return;
2658
2659 RecordData Record;
2660
2661 // Create a blob abbreviation for the C++ base specifiers offsets.
2662 using namespace llvm;
2663
2664 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2665 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2668 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2669
Douglas Gregorc27b2872011-08-04 00:01:48 +00002670 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002671 Record.clear();
2672 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2673 Record.push_back(CXXBaseSpecifiersOffsets.size());
2674 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002675 bytes(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002676}
2677
Douglas Gregorc5046832009-04-27 18:38:38 +00002678//===----------------------------------------------------------------------===//
2679// Type Serialization
2680//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002681
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002682/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002683void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002684 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002685 if (Idx.getIndex() == 0) // we haven't seen this type before.
2686 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002687
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002688 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002689
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002690 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002691 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002692 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002693 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002694 else if (TypeOffsets.size() < Index) {
2695 TypeOffsets.resize(Index + 1);
2696 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002697 }
2698
2699 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002700
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002701 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002702 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002703 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002704
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002705 if (T.hasLocalNonFastQualifiers()) {
2706 Qualifiers Qs = T.getLocalQualifiers();
2707 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002708 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002709 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002710 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002711 } else {
2712 switch (T->getTypeClass()) {
2713 // For all of the concrete, non-dependent types, call the
2714 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002715#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002716 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002717#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002718#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002719 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002720 }
2721
2722 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002723 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002724
2725 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002726 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727}
2728
Douglas Gregorc5046832009-04-27 18:38:38 +00002729//===----------------------------------------------------------------------===//
2730// Declaration Serialization
2731//===----------------------------------------------------------------------===//
2732
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002733/// \brief Write the block containing all of the declaration IDs
2734/// lexically declared within the given DeclContext.
2735///
2736/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2737/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002738uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002739 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002740 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002741 return 0;
2742
Douglas Gregor8f45df52009-04-16 22:23:12 +00002743 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002744 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002745 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002746 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002747 for (const auto *D : DC->decls())
2748 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002749
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002750 ++NumLexicalDeclContexts;
Reid Kleckner012665e2015-05-21 00:13:09 +00002751 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, bytes(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002752 return Offset;
2753}
2754
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002755void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002756 using namespace llvm;
2757 RecordData Record;
2758
2759 // Write the type offsets array
2760 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002761 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2765 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2766 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002767 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002768 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002769 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002770 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002771
2772 // Write the declaration offsets array
2773 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2778 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2779 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002780 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002781 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002782 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002783 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002784}
2785
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002786void ASTWriter::WriteFileDeclIDsMap() {
2787 using namespace llvm;
2788 RecordData Record;
2789
Chandler Carruthb306d732015-03-27 00:31:20 +00002790 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2791 FileDeclIDs.begin(), FileDeclIDs.end());
2792 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2793 llvm::less_first());
2794
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002795 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002796 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2797 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2798 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2799 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2800 for (auto &LocDeclEntry : Info.DeclIDs)
2801 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002802 }
2803
2804 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2805 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2808 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2809 Record.push_back(FILE_SORTED_DECLS);
Chandler Carruthb306d732015-03-27 00:31:20 +00002810 Record.push_back(FileGroupedDeclIDs.size());
Reid Kleckner012665e2015-05-21 00:13:09 +00002811 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002812}
2813
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002814void ASTWriter::WriteComments() {
2815 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002816 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002817 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002818 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2819 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002820 I != E; ++I) {
2821 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002822 AddSourceRange((*I)->getSourceRange(), Record);
2823 Record.push_back((*I)->getKind());
2824 Record.push_back((*I)->isTrailingComment());
2825 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002826 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2827 }
2828 Stream.ExitBlock();
2829}
2830
Douglas Gregorc5046832009-04-27 18:38:38 +00002831//===----------------------------------------------------------------------===//
2832// Global Method Pool and Selector Serialization
2833//===----------------------------------------------------------------------===//
2834
Douglas Gregore84a9da2009-04-20 20:36:09 +00002835namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002836// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002837class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002838 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002839
2840public:
2841 typedef Selector key_type;
2842 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002843
Sebastian Redl834bb972010-08-04 17:20:04 +00002844 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002845 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002846 ObjCMethodList Instance, Factory;
2847 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002848 typedef const data_type& data_type_ref;
2849
Justin Bogner25463f12014-04-18 20:27:24 +00002850 typedef unsigned hash_value_type;
2851 typedef unsigned offset_type;
2852
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002853 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002854
Justin Bogner25463f12014-04-18 20:27:24 +00002855 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002856 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002857 }
Mike Stump11289f42009-09-09 15:08:12 +00002858
2859 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002860 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002861 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002862 using namespace llvm::support;
2863 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002864 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002865 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002866 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2867 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002868 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002869 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002870 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002871 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002872 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002873 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002874 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002875 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002876 return std::make_pair(KeyLen, DataLen);
2877 }
Mike Stump11289f42009-09-09 15:08:12 +00002878
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002879 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002880 using namespace llvm::support;
2881 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002882 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002883 assert((Start >> 32) == 0 && "Selector key offset too large");
2884 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002885 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002886 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002887 if (N == 0)
2888 N = 1;
2889 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002890 LE.write<uint32_t>(
2891 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002892 }
Mike Stump11289f42009-09-09 15:08:12 +00002893
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002894 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002895 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002896 using namespace llvm::support;
2897 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002898 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002899 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002900 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002901 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002902 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002903 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002904 ++NumInstanceMethods;
2905
2906 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002907 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002908 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002909 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002910 ++NumFactoryMethods;
2911
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002912 unsigned InstanceBits = Methods.Instance.getBits();
2913 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002914 unsigned InstanceHasMoreThanOneDeclBit =
2915 Methods.Instance.hasMoreThanOneDecl();
2916 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2917 (InstanceHasMoreThanOneDeclBit << 2) |
2918 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002919 unsigned FactoryBits = Methods.Factory.getBits();
2920 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002921 unsigned FactoryHasMoreThanOneDeclBit =
2922 Methods.Factory.hasMoreThanOneDecl();
2923 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2924 (FactoryHasMoreThanOneDeclBit << 2) |
2925 FactoryBits;
2926 LE.write<uint16_t>(FullInstanceBits);
2927 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002928 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002929 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002930 if (Method->getMethod())
2931 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002932 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002933 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002934 if (Method->getMethod())
2935 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002936
2937 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002938 }
2939};
2940} // end anonymous namespace
2941
Sebastian Redla19a67f2010-08-03 21:58:15 +00002942/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002943///
2944/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002945/// in an on-disk hash table indexed by the selector. The hash table also
2946/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002947void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002948 using namespace llvm;
2949
Sebastian Redla19a67f2010-08-03 21:58:15 +00002950 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002951 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002952 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002953 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002954 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002955 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002956 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002957 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002958
Sebastian Redla19a67f2010-08-03 21:58:15 +00002959 // Create the on-disk hash table representation. We walk through every
2960 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002961 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002962 for (auto &SelectorAndID : SelectorIDs) {
2963 Selector S = SelectorAndID.first;
2964 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002965 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002966 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002967 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00002968 ObjCMethodList(),
2969 ObjCMethodList()
2970 };
2971 if (F != SemaRef.MethodPool.end()) {
2972 Data.Instance = F->second.first;
2973 Data.Factory = F->second.second;
2974 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002975 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002976 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002977 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002978 // Selector already exists. Did it change?
2979 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002980 for (ObjCMethodList *M = &Data.Instance;
2981 !changed && M && M->getMethod(); M = M->getNext()) {
2982 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002983 changed = true;
2984 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002985 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002986 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00002987 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002988 changed = true;
2989 }
2990 if (!changed)
2991 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002992 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002993 // A new method pool entry.
2994 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002995 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002996 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002997 }
2998
Douglas Gregorc78d3462009-04-24 21:10:55 +00002999 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003000 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003001 uint32_t BucketOffset;
3002 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003003 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003004 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003005 llvm::raw_svector_ostream Out(MethodPool);
3006 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003007 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003008 BucketOffset = Generator.Emit(Out, Trait);
3009 }
3010
3011 // Create a blob abbreviation
3012 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003013 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3017 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3018
Douglas Gregor95c13f52009-04-25 17:48:32 +00003019 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003020 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003021 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003022 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003023 Record.push_back(NumTableEntries);
Yaron Keren92e1b622015-03-18 10:17:07 +00003024 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003025
3026 // Create a blob abbreviation for the selector table offsets.
3027 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003028 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3032 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3033
3034 // Write the selector offsets table.
3035 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003036 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003037 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003038 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003039 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003040 bytes(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003041 }
3042}
3043
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003044/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003045void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003046 using namespace llvm;
3047 if (SemaRef.ReferencedSelectors.empty())
3048 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003049
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003050 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003051
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003052 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003053 // very tricky to fix, and given that @selector shouldn't really appear in
3054 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003055 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3056 Selector Sel = SelectorAndLocation.first;
3057 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003058 AddSelectorRef(Sel, Record);
3059 AddSourceLocation(Loc, Record);
3060 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003061 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003062}
3063
Douglas Gregorc5046832009-04-27 18:38:38 +00003064//===----------------------------------------------------------------------===//
3065// Identifier Table Serialization
3066//===----------------------------------------------------------------------===//
3067
Richard Smithb3761912015-02-05 23:08:52 +00003068/// Determine the declaration that should be put into the name lookup table to
3069/// represent the given declaration in this module. This is usually D itself,
3070/// but if D was imported and merged into a local declaration, we want the most
3071/// recent local declaration instead. The chosen declaration will be the most
3072/// recent declaration in any module that imports this one.
3073static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3074 NamedDecl *D) {
3075 if (!LangOpts.Modules || !D->isFromASTFile())
3076 return D;
3077
3078 if (Decl *Redecl = D->getPreviousDecl()) {
3079 // For Redeclarable decls, a prior declaration might be local.
3080 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3081 if (!Redecl->isFromASTFile())
3082 return cast<NamedDecl>(Redecl);
Richard Smithfe620d22015-03-05 23:24:12 +00003083 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003084 // local one.
3085 if (D->getOwningModuleID() == 0)
3086 break;
3087 }
3088 } else if (Decl *First = D->getCanonicalDecl()) {
3089 // For Mergeable decls, the first decl might be local.
3090 if (!First->isFromASTFile())
3091 return cast<NamedDecl>(First);
3092 }
3093
3094 // All declarations are imported. Our most recent declaration will also be
3095 // the most recent one in anyone who imports us.
3096 return D;
3097}
3098
Douglas Gregorc78d3462009-04-24 21:10:55 +00003099namespace {
Richard Smithcf97cf62015-04-19 01:34:23 +00003100class ASTIdentifierTableTrait {
3101 ASTWriter &Writer;
3102 Preprocessor &PP;
3103 IdentifierResolver &IdResolver;
Richard Smithcf97cf62015-04-19 01:34:23 +00003104
3105 /// \brief Determines whether this is an "interesting" identifier that needs a
3106 /// full IdentifierInfo structure written into the hash table. Notably, this
3107 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3108 /// to check that.
Richard Smithd7329392015-04-21 21:46:32 +00003109 bool isInterestingIdentifier(IdentifierInfo *II, uint64_t MacroOffset) {
3110 if (MacroOffset ||
3111 II->isPoisoned() ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003112 II->isExtensionToken() ||
3113 II->getObjCOrBuiltinID() ||
3114 II->hasRevertedTokenIDToIdentifier() ||
3115 II->getFETokenInfo<void>())
3116 return true;
3117
3118 return false;
3119 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003120
Douglas Gregore84a9da2009-04-20 20:36:09 +00003121public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003122 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003123 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003124
Sebastian Redl539c5062010-08-18 23:57:32 +00003125 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003126 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003127
Justin Bogner25463f12014-04-18 20:27:24 +00003128 typedef unsigned hash_value_type;
3129 typedef unsigned offset_type;
3130
Richard Smithd7329392015-04-21 21:46:32 +00003131 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3132 IdentifierResolver &IdResolver)
3133 : Writer(Writer), PP(PP), IdResolver(IdResolver) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003134
Justin Bogner25463f12014-04-18 20:27:24 +00003135 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003136 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003137 }
Mike Stump11289f42009-09-09 15:08:12 +00003138
3139 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003140 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003141 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003142 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003143 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3144 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003145 DataLen += 2; // 2 bytes for builtin ID
3146 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003147 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003148 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003149
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003150 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3151 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003152 D != DEnd; ++D)
Richard Smithdaa69e02014-07-25 04:40:03 +00003153 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003154 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003155 using namespace llvm::support;
3156 endian::Writer<little> LE(Out);
3157
Richard Smithdf8a8312015-03-13 04:05:01 +00003158 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003159 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003160 // We emit the key length after the data length so that every
3161 // string is preceded by a 16-bit length. This matches the PTH
3162 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003163 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003164 return std::make_pair(KeyLen, DataLen);
3165 }
Mike Stump11289f42009-09-09 15:08:12 +00003166
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003167 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003168 unsigned KeyLen) {
3169 // Record the location of the key data. This is used when generating
3170 // the mapping from persistent IDs to strings.
3171 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003172 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003173 }
Mike Stump11289f42009-09-09 15:08:12 +00003174
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003175 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003176 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003177 using namespace llvm::support;
3178 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003179
Richard Smithd7329392015-04-21 21:46:32 +00003180 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3181 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003182 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003183 return;
3184 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003185
Justin Bognere1c147c2014-03-28 22:03:19 +00003186 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003187 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3188 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003189 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003190 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003191 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003192 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003193 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3194 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003195 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003196 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003197 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003198
Richard Smithd7329392015-04-21 21:46:32 +00003199 if (HadMacroDefinition)
3200 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003201
Douglas Gregora868bbd2009-04-21 22:25:48 +00003202 // Emit the declaration IDs in reverse order, because the
3203 // IdentifierResolver provides the declarations as they would be
3204 // visible (e.g., the function "stat" would come before the struct
Richard Smithb3761912015-02-05 23:08:52 +00003205 // "stat"), but the ASTReader adds declarations to the end of the list
3206 // (so we need to see the struct "stat" before the function "stat").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003207 // Only emit declarations that aren't from a chained PCH, though.
Richard Smithb3761912015-02-05 23:08:52 +00003208 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II), IdResolver.end());
3209 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3210 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003211 D != DEnd; ++D)
Richard Smithb3761912015-02-05 23:08:52 +00003212 LE.write<uint32_t>(
3213 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003214 }
3215};
3216} // end anonymous namespace
3217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003218/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003219///
3220/// The identifier table consists of a blob containing string data
3221/// (the actual identifiers themselves) and a separate "offsets" index
3222/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003223void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3224 IdentifierResolver &IdResolver,
3225 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003226 using namespace llvm;
3227
3228 // Create and write out the blob that contains the identifier
3229 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003230 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003231 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smithd7329392015-04-21 21:46:32 +00003232 ASTIdentifierTableTrait Trait(*this, PP, IdResolver);
Mike Stump11289f42009-09-09 15:08:12 +00003233
Douglas Gregore6648fb2009-04-28 20:33:11 +00003234 // Look for any identifiers that were named while processing the
3235 // headers, but are otherwise not needed. We add these to the hash
3236 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003237 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003238 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003239 SmallVector<const IdentifierInfo *, 128> IIs;
Douglas Gregore6648fb2009-04-28 20:33:11 +00003240 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3241 IDEnd = PP.getIdentifierTable().end();
3242 ID != IDEnd; ++ID)
Chandler Carruth8440c982015-03-26 23:54:15 +00003243 IIs.push_back(ID->second);
3244 // Sort the identifiers lexicographically before getting them references so
3245 // that their order is stable.
3246 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3247 for (const IdentifierInfo *II : IIs)
3248 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003249
Sebastian Redlff4a2952010-07-23 23:49:55 +00003250 // Create the on-disk hash table representation. We only store offsets
3251 // for identifiers that appear here for the first time.
3252 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003253 for (auto IdentIDPair : IdentifierIDs) {
3254 IdentifierInfo *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3255 IdentID ID = IdentIDPair.second;
3256 assert(II && "NULL identifier in identifier table");
3257 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3258 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003259 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003260
Douglas Gregore84a9da2009-04-20 20:36:09 +00003261 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003262 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003263 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003264 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003265 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003266 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003267 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003268 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003269 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003270 }
3271
3272 // Create a blob abbreviation
3273 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003274 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003277 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003278
3279 // Write the identifier table
3280 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003281 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003282 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003283 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003284 }
3285
3286 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003287 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003288 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3292 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3293
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003294#ifndef NDEBUG
3295 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3296 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3297#endif
3298
Douglas Gregor0e149972009-04-25 19:10:14 +00003299 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003300 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003301 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003302 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003303 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003304 bytes(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003305}
3306
Douglas Gregorc5046832009-04-27 18:38:38 +00003307//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003308// DeclContext's Name Lookup Table Serialization
3309//===----------------------------------------------------------------------===//
3310
3311namespace {
3312// Trait used for the on-disk hash table used in the method pool.
3313class ASTDeclContextNameLookupTrait {
3314 ASTWriter &Writer;
3315
3316public:
3317 typedef DeclarationName key_type;
3318 typedef key_type key_type_ref;
3319
3320 typedef DeclContext::lookup_result data_type;
3321 typedef const data_type& data_type_ref;
3322
Justin Bogner25463f12014-04-18 20:27:24 +00003323 typedef unsigned hash_value_type;
3324 typedef unsigned offset_type;
3325
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003326 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3327
Justin Bogner25463f12014-04-18 20:27:24 +00003328 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003329 llvm::FoldingSetNodeID ID;
3330 ID.AddInteger(Name.getNameKind());
3331
3332 switch (Name.getNameKind()) {
3333 case DeclarationName::Identifier:
3334 ID.AddString(Name.getAsIdentifierInfo()->getName());
3335 break;
3336 case DeclarationName::ObjCZeroArgSelector:
3337 case DeclarationName::ObjCOneArgSelector:
3338 case DeclarationName::ObjCMultiArgSelector:
3339 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3340 break;
3341 case DeclarationName::CXXConstructorName:
3342 case DeclarationName::CXXDestructorName:
3343 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003344 break;
3345 case DeclarationName::CXXOperatorName:
3346 ID.AddInteger(Name.getCXXOverloadedOperator());
3347 break;
3348 case DeclarationName::CXXLiteralOperatorName:
3349 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3350 case DeclarationName::CXXUsingDirective:
3351 break;
3352 }
3353
3354 return ID.ComputeHash();
3355 }
3356
3357 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003358 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003359 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003360 using namespace llvm::support;
3361 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003362 unsigned KeyLen = 1;
3363 switch (Name.getNameKind()) {
3364 case DeclarationName::Identifier:
3365 case DeclarationName::ObjCZeroArgSelector:
3366 case DeclarationName::ObjCOneArgSelector:
3367 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003368 case DeclarationName::CXXLiteralOperatorName:
3369 KeyLen += 4;
3370 break;
3371 case DeclarationName::CXXOperatorName:
3372 KeyLen += 1;
3373 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003374 case DeclarationName::CXXConstructorName:
3375 case DeclarationName::CXXDestructorName:
3376 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003377 case DeclarationName::CXXUsingDirective:
3378 break;
3379 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003380 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003381
3382 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003383 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003384 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003385
3386 return std::make_pair(KeyLen, DataLen);
3387 }
3388
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003389 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003390 using namespace llvm::support;
3391 endian::Writer<little> LE(Out);
3392 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003393 switch (Name.getNameKind()) {
3394 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003395 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003396 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003397 case DeclarationName::ObjCZeroArgSelector:
3398 case DeclarationName::ObjCOneArgSelector:
3399 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003400 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003401 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003402 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003403 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3404 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003405 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003406 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003407 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003408 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003409 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003410 case DeclarationName::CXXConstructorName:
3411 case DeclarationName::CXXDestructorName:
3412 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003413 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003414 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003415 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003416
3417 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003418 }
3419
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003420 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003421 data_type Lookup, unsigned DataLen) {
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 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003425 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003426 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3427 I != E; ++I)
Richard Smithb3761912015-02-05 23:08:52 +00003428 LE.write<uint32_t>(
3429 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003430
3431 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3432 }
3433};
3434} // end anonymous namespace
3435
Chandler Carruthe972c362015-03-26 03:11:40 +00003436bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3437 DeclContext *DC) {
3438 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3439}
3440
3441bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3442 DeclContext *DC) {
3443 for (auto *D : Result.getLookupResult())
3444 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3445 return false;
3446
3447 return true;
3448}
3449
Richard Smithcd45dbc2014-04-19 03:48:30 +00003450uint32_t
Chandler Carruthe972c362015-03-26 03:11:40 +00003451ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003452 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003453 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3454 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003455 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003456
Chandler Carruthe972c362015-03-26 03:11:40 +00003457 // FIXME: We need to build the lookups table, which is logically const.
3458 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
3459 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3460
3461 // Create the on-disk hash table representation.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003462 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3463 Generator;
3464 ASTDeclContextNameLookupTrait Trait(*this);
3465
Chandler Carruthe972c362015-03-26 03:11:40 +00003466 // The first step is to collect the declaration names which we need to
3467 // serialize into the name lookup table, and to collect them in a stable
3468 // order.
3469 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003470
Chandler Carruthe972c362015-03-26 03:11:40 +00003471 // We also build up small sets of the constructor and conversion function
3472 // names which are visible.
3473 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003474
Chandler Carruthe972c362015-03-26 03:11:40 +00003475 for (auto &Lookup : *DC->buildLookup()) {
3476 auto &Name = Lookup.first;
3477 auto &Result = Lookup.second;
3478
3479 // If there are no local declarations in our lookup result, we don't
3480 // need to write an entry for the name at all unless we're rewriting
3481 // the decl context. If we can't write out a lookup set without
3482 // performing more deserialization, just skip this entry.
3483 if (isLookupResultExternal(Result, DC) && !isRewritten(cast<Decl>(DC)) &&
3484 isLookupResultEntirelyExternal(Result, DC))
3485 continue;
3486
3487 // We also skip empty results. If any of the results could be external and
3488 // the currently available results are empty, then all of the results are
3489 // external and we skip it above. So the only way we get here with an empty
3490 // results is when no results could have been external *and* we have
3491 // external results.
3492 //
3493 // FIXME: While we might want to start emitting on-disk entries for negative
3494 // lookups into a decl context as an optimization, today we *have* to skip
3495 // them because there are names with empty lookup results in decl contexts
3496 // which we can't emit in any stable ordering: we lookup constructors and
3497 // conversion functions in the enclosing namespace scope creating empty
3498 // results for them. This in almost certainly a bug in Clang's name lookup,
3499 // but that is likely to be hard or impossible to fix and so we tolerate it
3500 // here by omitting lookups with empty results.
3501 if (Lookup.second.getLookupResult().empty())
3502 continue;
3503
3504 switch (Lookup.first.getNameKind()) {
3505 default:
3506 Names.push_back(Lookup.first);
3507 break;
3508
Richard Smithcd45dbc2014-04-19 03:48:30 +00003509 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003510 assert(isa<CXXRecordDecl>(DC) &&
3511 "Cannot have a constructor name outside of a class!");
3512 ConstructorNameSet.insert(Name);
3513 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003514
3515 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003516 assert(isa<CXXRecordDecl>(DC) &&
3517 "Cannot have a conversion function name outside of a class!");
3518 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003519 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003520 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003521 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003522
Chandler Carruthe972c362015-03-26 03:11:40 +00003523 // Sort the names into a stable order.
3524 std::sort(Names.begin(), Names.end());
3525
Chandler Carruthffbf7052015-03-26 22:27:09 +00003526 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003527 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003528 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003529
Chandler Carruthffbf7052015-03-26 22:27:09 +00003530 // First we try the easy case by forming the current context's constructor
3531 // name and adding that name first. This is a very useful optimization to
3532 // avoid walking the lexical declarations in many cases, and it also
3533 // handles the only case where a constructor name can come from some other
3534 // lexical context -- when that name is an implicit constructor merged from
3535 // another declaration in the redecl chain. Any non-implicit constructor or
3536 // conversion function which doesn't occur in all the lexical contexts
3537 // would be an ODR violation.
3538 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3539 Context->getCanonicalType(Context->getRecordType(D)));
3540 if (ConstructorNameSet.erase(ImplicitCtorName))
3541 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003542
Chandler Carruthffbf7052015-03-26 22:27:09 +00003543 // If we still have constructors or conversion functions, we walk all the
3544 // names in the decl and add the constructors and conversion functions
3545 // which are visible in the order they lexically occur within the context.
3546 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3547 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3548 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3549 auto Name = ChildND->getDeclName();
3550 switch (Name.getNameKind()) {
3551 default:
3552 continue;
3553
3554 case DeclarationName::CXXConstructorName:
3555 if (ConstructorNameSet.erase(Name))
3556 Names.push_back(Name);
3557 break;
3558
3559 case DeclarationName::CXXConversionFunctionName:
3560 if (ConversionNameSet.erase(Name))
3561 Names.push_back(Name);
3562 break;
3563 }
3564
3565 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3566 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003567 }
3568
Chandler Carruthe972c362015-03-26 03:11:40 +00003569 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3570 "constructors by walking all the "
3571 "lexical members of the context.");
3572 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3573 "conversion functions by walking all "
3574 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003575 }
3576
Chandler Carruthe972c362015-03-26 03:11:40 +00003577 // Next we need to do a lookup with each name into this decl context to fully
3578 // populate any results from external sources. We don't actually use the
3579 // results of these lookups because we only want to use the results after all
3580 // results have been loaded and the pointers into them will be stable.
3581 for (auto &Name : Names)
3582 DC->lookup(Name);
3583
3584 // Now we need to insert the results for each name into the hash table. For
3585 // constructor names and conversion function names, we actually need to merge
3586 // all of the results for them into one list of results each and insert
3587 // those.
3588 SmallVector<NamedDecl *, 8> ConstructorDecls;
3589 SmallVector<NamedDecl *, 8> ConversionDecls;
3590
3591 // Now loop over the names, either inserting them or appending for the two
3592 // special cases.
3593 for (auto &Name : Names) {
3594 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3595
3596 switch (Name.getNameKind()) {
3597 default:
3598 Generator.insert(Name, Result, Trait);
3599 break;
3600
3601 case DeclarationName::CXXConstructorName:
3602 ConstructorDecls.append(Result.begin(), Result.end());
3603 break;
3604
3605 case DeclarationName::CXXConversionFunctionName:
3606 ConversionDecls.append(Result.begin(), Result.end());
3607 break;
3608 }
3609 }
3610
3611 // Handle our two special cases if we ended up having any. We arbitrarily use
3612 // the first declaration's name here because the name itself isn't part of
3613 // the key, only the kind of name is used.
3614 if (!ConstructorDecls.empty())
3615 Generator.insert(ConstructorDecls.front()->getDeclName(),
3616 DeclContext::lookup_result(ConstructorDecls), Trait);
3617 if (!ConversionDecls.empty())
3618 Generator.insert(ConversionDecls.front()->getDeclName(),
3619 DeclContext::lookup_result(ConversionDecls), Trait);
3620
Richard Smith961eae52014-03-25 01:14:22 +00003621 // Create the on-disk hash table in a buffer.
3622 llvm::raw_svector_ostream Out(LookupTable);
3623 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003624 using namespace llvm::support;
3625 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003626 return Generator.Emit(Out, Trait);
3627}
3628
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003629/// \brief Write the block containing all of the declaration IDs
3630/// visible from the given DeclContext.
3631///
3632/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003633/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003634uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3635 DeclContext *DC) {
3636 if (DC->getPrimaryContext() != DC)
3637 return 0;
3638
Chandler Carruthe972c362015-03-26 03:11:40 +00003639 // Skip contexts which don't support name lookup.
3640 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003641 return 0;
3642
3643 // If not in C++, we perform name lookup for the translation unit via the
3644 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003645 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003646 return 0;
3647
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003648 // Serialize the contents of the mapping used for lookup. Note that,
3649 // although we have two very different code paths, the serialized
3650 // representation is the same for both cases: a declaration name,
3651 // followed by a size, followed by references to the visible
3652 // declarations that have that name.
3653 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003654 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003655 if (!Map || Map->empty())
3656 return 0;
3657
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003658 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003659 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003660 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003661
3662 // Write the lookup table
3663 RecordData Record;
3664 Record.push_back(DECL_CONTEXT_VISIBLE);
3665 Record.push_back(BucketOffset);
3666 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003667 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003668 ++NumVisibleDeclContexts;
3669 return Offset;
3670}
3671
Sebastian Redla4071b42010-08-24 00:50:09 +00003672/// \brief Write an UPDATE_VISIBLE block for the given context.
3673///
3674/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3675/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003676/// (in C++), for namespaces, and for classes with forward-declared unscoped
3677/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003678void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003679 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003680 if (!Map || Map->empty())
3681 return;
3682
Sebastian Redla4071b42010-08-24 00:50:09 +00003683 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003684 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003685 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003686
3687 // Write the lookup table
3688 RecordData Record;
3689 Record.push_back(UPDATE_VISIBLE);
3690 Record.push_back(getDeclID(cast<Decl>(DC)));
3691 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003692 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003693}
3694
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003695/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3696void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3697 RecordData Record;
3698 Record.push_back(Opts.fp_contract);
3699 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3700}
3701
3702/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3703void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003704 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003705 return;
3706
3707 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3708 RecordData Record;
3709#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3710#include "clang/Basic/OpenCLExtensions.def"
3711 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3712}
3713
Douglas Gregor358cd442012-01-15 16:58:34 +00003714void ASTWriter::WriteRedeclarations() {
3715 RecordData LocalRedeclChains;
3716 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3717
3718 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3719 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003720 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003721
3722 Decl *MostRecent = First->getMostRecentDecl();
3723
3724 // If we only have a single declaration, there is no point in storing
3725 // a redeclaration chain.
3726 if (First == MostRecent)
3727 continue;
3728
3729 unsigned Offset = LocalRedeclChains.size();
3730 unsigned Size = 0;
3731 LocalRedeclChains.push_back(0); // Placeholder for the size.
3732
3733 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003734 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003735 Prev = Prev->getPreviousDecl()) {
3736 if (!Prev->isFromASTFile()) {
3737 AddDeclRef(Prev, LocalRedeclChains);
3738 ++Size;
3739 }
3740 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003741
Douglas Gregor358cd442012-01-15 16:58:34 +00003742 LocalRedeclChains[Offset] = Size;
3743
3744 // Reverse the set of local redeclarations, so that we store them in
3745 // order (since we found them in reverse order).
3746 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3747
Douglas Gregor6168bd22013-02-18 15:53:43 +00003748 // Add the mapping from the first ID from the AST to the set of local
3749 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003750 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3751 LocalRedeclsMap.push_back(Info);
3752
3753 assert(N == Redeclarations.size() &&
3754 "Deserialized a declaration we shouldn't have");
3755 }
3756
3757 if (LocalRedeclChains.empty())
3758 return;
3759
3760 // Sort the local redeclarations map by the first declaration ID,
3761 // since the reader will be performing binary searches on this information.
3762 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3763
3764 // Emit the local redeclarations map.
3765 using namespace llvm;
3766 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3767 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3770 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3771
3772 RecordData Record;
3773 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3774 Record.push_back(LocalRedeclsMap.size());
3775 Stream.EmitRecordWithBlob(AbbrevID, Record,
3776 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3777 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3778
3779 // Emit the redeclaration chains.
3780 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3781}
3782
Douglas Gregor404cdde2012-01-27 01:47:08 +00003783void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003784 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003785 RecordData Categories;
3786
3787 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3788 unsigned Size = 0;
3789 unsigned StartIndex = Categories.size();
3790
3791 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3792
3793 // Allocate space for the size.
3794 Categories.push_back(0);
3795
3796 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003797 for (ObjCInterfaceDecl::known_categories_iterator
3798 Cat = Class->known_categories_begin(),
3799 CatEnd = Class->known_categories_end();
3800 Cat != CatEnd; ++Cat, ++Size) {
3801 assert(getDeclID(*Cat) != 0 && "Bogus category");
3802 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003803 }
3804
3805 // Update the size.
3806 Categories[StartIndex] = Size;
3807
3808 // Record this interface -> category map.
3809 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3810 CategoriesMap.push_back(CatInfo);
3811 }
3812
3813 // Sort the categories map by the definition ID, since the reader will be
3814 // performing binary searches on this information.
3815 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3816
3817 // Emit the categories map.
3818 using namespace llvm;
3819 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3820 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3821 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3822 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3823 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3824
3825 RecordData Record;
3826 Record.push_back(OBJC_CATEGORIES_MAP);
3827 Record.push_back(CategoriesMap.size());
3828 Stream.EmitRecordWithBlob(AbbrevID, Record,
3829 reinterpret_cast<char*>(CategoriesMap.data()),
3830 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3831
3832 // Emit the category lists.
3833 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3834}
3835
Richard Smithe40f2ba2013-08-07 21:41:30 +00003836void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3837 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3838
3839 if (LPTMap.empty())
3840 return;
3841
3842 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00003843 for (auto LPTMapEntry : LPTMap) {
3844 const FunctionDecl *FD = LPTMapEntry.first;
3845 LateParsedTemplate *LPT = LPTMapEntry.second;
3846 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003847 AddDeclRef(LPT->D, Record);
3848 Record.push_back(LPT->Toks.size());
3849
3850 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3851 TokEnd = LPT->Toks.end();
3852 TokIt != TokEnd; ++TokIt) {
3853 AddToken(*TokIt, Record);
3854 }
3855 }
3856 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3857}
3858
Dario Domizioli13a0a382014-05-23 12:13:25 +00003859/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3860void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3861 RecordData Record;
3862 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3863 AddSourceLocation(PragmaLoc, Record);
3864 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3865}
3866
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003867//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003868// General Serialization Routines
3869//===----------------------------------------------------------------------===//
3870
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003871/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003872void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3873 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003874 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003875 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3876 e = Attrs.end(); i != e; ++i){
3877 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003878 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003879 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003880
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003881#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003882
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003883 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003884}
3885
John McCallf413f5e2013-05-03 00:10:13 +00003886void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3887 AddSourceLocation(Tok.getLocation(), Record);
3888 Record.push_back(Tok.getLength());
3889
3890 // FIXME: When reading literal tokens, reconstruct the literal pointer
3891 // if it is needed.
3892 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3893 // FIXME: Should translate token kind to a stable encoding.
3894 Record.push_back(Tok.getKind());
3895 // FIXME: Should translate token flags to a stable encoding.
3896 Record.push_back(Tok.getFlags());
3897}
3898
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003899void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003900 Record.push_back(Str.size());
3901 Record.insert(Record.end(), Str.begin(), Str.end());
3902}
3903
Richard Smith7ed1bc92014-12-05 22:42:13 +00003904bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00003905 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00003906
Richard Smith54cc3c22014-12-11 20:50:24 +00003907 bool Changed =
3908 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00003909
3910 // Remove a prefix to make the path relative, if relevant.
3911 const char *PathBegin = Path.data();
3912 const char *PathPtr =
3913 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
3914 if (PathPtr != PathBegin) {
3915 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
3916 Changed = true;
3917 }
3918
3919 return Changed;
3920}
3921
3922void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
3923 SmallString<128> FilePath(Path);
3924 PreparePathForOutput(FilePath);
3925 AddString(FilePath, Record);
3926}
3927
3928void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
3929 StringRef Path) {
3930 SmallString<128> FilePath(Path);
3931 PreparePathForOutput(FilePath);
3932 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
3933}
3934
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003935void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3936 RecordDataImpl &Record) {
3937 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003938 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003939 Record.push_back(*Minor + 1);
3940 else
3941 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003942 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003943 Record.push_back(*Subminor + 1);
3944 else
3945 Record.push_back(0);
3946}
3947
Douglas Gregore84a9da2009-04-20 20:36:09 +00003948/// \brief Note that the identifier II occurs at the given offset
3949/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003950void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003951 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003952 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003953 // up earlier in the chain and thus don't need an offset.
3954 if (ID >= FirstIdentID)
3955 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003956}
3957
Douglas Gregor95c13f52009-04-25 17:48:32 +00003958/// \brief Note that the selector Sel occurs at the given offset
3959/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003960void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003961 unsigned ID = SelectorIDs[Sel];
3962 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003963 // Don't record offsets for selectors that are also available in a different
3964 // file.
3965 if (ID < FirstSelectorID)
3966 return;
3967 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003968}
3969
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003970ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00003971 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
3972 WritingModule(nullptr), WritingAST(false),
3973 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
3974 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
3975 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
3976 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3977 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
3978 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3979 NextSubmoduleID(FirstSubmoduleID),
3980 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
3981 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
3982 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00003983 NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
3984 TypeExtQualAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00003985 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
3986 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00003987 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00003988 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00003989 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
3990 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
3991 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003992
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003993ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003994 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003995}
3996
Richard Smithb3761912015-02-05 23:08:52 +00003997const LangOptions &ASTWriter::getLangOpts() const {
3998 assert(WritingAST && "can't determine lang opts when not writing AST");
3999 return Context->getLangOpts();
4000}
4001
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004002void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004003 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004004 Module *WritingModule, StringRef isysroot,
4005 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004006 WritingAST = true;
4007
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004008 ASTHasCompilerErrors = hasErrors;
4009
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004010 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004011 Stream.Emit((unsigned)'C', 8);
4012 Stream.Emit((unsigned)'P', 8);
4013 Stream.Emit((unsigned)'C', 8);
4014 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004015
Chris Lattner28fa4e62009-04-26 22:26:21 +00004016 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004017
Douglas Gregoreda8e122011-08-09 15:13:55 +00004018 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004019 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004020 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004021 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004022 Context = nullptr;
4023 PP = nullptr;
4024 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004025 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004026
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004027 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004028}
4029
Douglas Gregora94a1542011-07-27 21:45:57 +00004030template<typename Vector>
4031static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4032 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004033 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4034 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004035 Writer.AddDeclRef(*I, Record);
4036 }
4037}
4038
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004039void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004040 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004041 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004042 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004043 using namespace llvm;
4044
Craig Toppera13603a2014-05-22 05:54:18 +00004045 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004046
Douglas Gregorcf68c582011-12-01 22:20:10 +00004047 // Make sure that the AST reader knows to finalize itself.
4048 if (Chain)
4049 Chain->finalizeForWriting();
4050
Sebastian Redl143413f2010-07-12 22:02:52 +00004051 ASTContext &Context = SemaRef.Context;
4052 Preprocessor &PP = SemaRef.PP;
4053
Douglas Gregordab42432011-08-12 00:15:20 +00004054 // Set up predefined declaration IDs.
4055 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004056 if (Context.ObjCIdDecl)
4057 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004058 if (Context.ObjCSelDecl)
4059 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004060 if (Context.ObjCClassDecl)
4061 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004062 if (Context.ObjCProtocolClassDecl)
4063 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004064 if (Context.Int128Decl)
4065 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4066 if (Context.UInt128Decl)
4067 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004068 if (Context.ObjCInstanceTypeDecl)
4069 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004070 if (Context.BuiltinVaListDecl)
4071 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
Richard Smithf19e1272015-03-07 00:04:49 +00004072 if (Context.ExternCContext)
4073 DeclIDs[Context.ExternCContext] = PREDEF_DECL_EXTERN_C_CONTEXT_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004074
Chris Lattner0c797362009-09-08 18:19:27 +00004075 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004076 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004077 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004078 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004079 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004080
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004081 // Build a record containing all of the file scoped decls in this file.
4082 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004083 if (!isModule)
4084 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4085 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004086
Douglas Gregor851443c2011-08-12 01:39:19 +00004087 // Build a record containing all of the delegating constructors we still need
4088 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004089 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004090 if (!isModule)
4091 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004092
Douglas Gregor851443c2011-08-12 01:39:19 +00004093 // Write the set of weak, undeclared identifiers. We always write the
4094 // entire table, since later PCH files in a PCH chain are only interested in
4095 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004096 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004097 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4098 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4099 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4100 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4101 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4102 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4103 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004104 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004105
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004106 // Build a record containing all of the ext_vector declarations.
4107 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004108 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004109
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004110 // Build a record containing all of the VTable uses information.
4111 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004112 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004113 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4114 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4115 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4116 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4117 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004118 }
4119
Nico Weber72889432014-09-06 01:25:55 +00004120 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4121 RecordData UnusedLocalTypedefNameCandidates;
4122 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4123 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4124
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004125 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004126 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004127 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004128 I = SemaRef.PendingInstantiations.begin(),
4129 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4130 AddDeclRef(I->first, PendingInstantiations);
4131 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004132 }
4133 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4134 "There are local ones at end of translation unit!");
4135
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004136 // Build a record containing some declaration references.
4137 RecordData SemaDeclRefs;
4138 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4139 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4140 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4141 }
4142
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004143 RecordData CUDASpecialDeclRefs;
4144 if (Context.getcudaConfigureCallDecl()) {
4145 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4146 }
4147
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004148 // Build a record containing all of the known namespaces.
4149 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004150 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004151 I = SemaRef.KnownNamespaces.begin(),
4152 IEnd = SemaRef.KnownNamespaces.end();
4153 I != IEnd; ++I) {
4154 if (!I->second)
4155 AddDeclRef(I->first, KnownNamespaces);
4156 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004157
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004158 // Build a record of all used, undefined objects that require definitions.
4159 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004160
4161 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004162 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004163 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4164 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004165 AddDeclRef(I->first, UndefinedButUsed);
4166 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004167 }
4168
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004169 // Build a record containing all delete-expressions that we would like to
4170 // analyze later in AST.
4171 RecordData DeleteExprsToAnalyze;
4172
4173 for (const auto &DeleteExprsInfo :
4174 SemaRef.getMismatchingDeleteExpressions()) {
4175 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4176 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4177 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4178 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4179 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4180 }
4181 }
4182
Douglas Gregor112b9072012-10-18 05:31:06 +00004183 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004184 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004185
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004186 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004187 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004188 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004189
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004190 // This is so that older clang versions, before the introduction
4191 // of the control block, can read and reject the newer PCH format.
4192 Record.clear();
4193 Record.push_back(VERSION_MAJOR);
4194 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4195
Douglas Gregor851443c2011-08-12 01:39:19 +00004196 // Create a lexical update block containing all of the declarations in the
4197 // translation unit that do not come from other AST files.
4198 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4199 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004200 for (const auto *I : TU->noload_decls()) {
4201 if (!I->isFromASTFile())
4202 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004203 }
4204
4205 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4206 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4207 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4208 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4209 Record.clear();
4210 Record.push_back(TU_UPDATE_LEXICAL);
4211 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00004212 bytes(NewGlobalDecls));
Douglas Gregor851443c2011-08-12 01:39:19 +00004213
4214 // And a visible updates block for the translation unit.
4215 Abv = new llvm::BitCodeAbbrev();
4216 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4217 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4218 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4219 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4220 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4221 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004222
4223 // If we have any extern "C" names, write out a visible update for them.
4224 if (Context.ExternCContext)
4225 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004226
4227 // If the translation unit has an anonymous namespace, and we don't already
4228 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004229 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004230 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4231 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004232 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004233 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004234 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004235
Richard Smith5652c0f2014-03-21 01:48:23 +00004236 // Add update records for all mangling numbers and static local numbers.
4237 // These aren't really update records, but this is a convenient way of
4238 // tagging this rare extra data onto the declarations.
4239 for (const auto &Number : Context.MangleNumbers)
4240 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004241 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4242 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004243 for (const auto &Number : Context.StaticLocalNumbers)
4244 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004245 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4246 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004247
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004248 // Make sure visible decls, added to DeclContexts previously loaded from
4249 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004250 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004251 I = UpdatingVisibleDecls.begin(),
4252 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4253 GetDeclRef(*I);
4254 }
4255
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004256 // Make sure all decls associated with an identifier are registered for
4257 // serialization.
Chandler Carruth8440c982015-03-26 23:54:15 +00004258 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004259 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4260 IDEnd = PP.getIdentifierTable().end();
4261 ID != IDEnd; ++ID) {
4262 const IdentifierInfo *II = ID->second;
Richard Smith9ab4cce2015-03-11 00:00:51 +00004263 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
Chandler Carruth8440c982015-03-26 23:54:15 +00004264 IIs.push_back(II);
Richard Smith9ab4cce2015-03-11 00:00:51 +00004265 }
Chandler Carruth8440c982015-03-26 23:54:15 +00004266 // Sort the identifiers to visit based on their name.
4267 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4268 for (const IdentifierInfo *II : IIs) {
Richard Smith9ab4cce2015-03-11 00:00:51 +00004269 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4270 DEnd = SemaRef.IdResolver.end();
4271 D != DEnd; ++D) {
4272 GetDeclRef(*D);
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004273 }
4274 }
4275
Douglas Gregor5204bde2011-08-02 16:26:37 +00004276 // Form the record of special types.
4277 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004278 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004279 AddTypeRef(Context.getFILEType(), SpecialTypes);
4280 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4281 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4282 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4283 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004284 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004285 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004286
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004287 if (Chain) {
4288 // Write the mapping information describing our module dependencies and how
4289 // each of those modules were mapped into our own offset/ID space, so that
4290 // the reader can build the appropriate mapping to its own offset/ID space.
4291 // The map consists solely of a blob with the following format:
4292 // *(module-name-len:i16 module-name:len*i8
4293 // source-location-offset:i32
4294 // identifier-id:i32
4295 // preprocessed-entity-id:i32
4296 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004297 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004298 // selector-id:i32
4299 // declaration-id:i32
4300 // c++-base-specifiers-id:i32
4301 // type-id:i32)
4302 //
4303 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4304 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4306 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004307 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004308 {
4309 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004310 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004311 using namespace llvm::support;
4312 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004313 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004314 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004315 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004316
Ben Langmuir785180e2014-10-20 16:27:30 +00004317 // Note: if a base ID was uint max, it would not be possible to load
4318 // another module after it or have more than one entity inside it.
4319 uint32_t None = std::numeric_limits<uint32_t>::max();
4320
4321 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4322 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4323 if (ShouldWrite)
4324 LE.write<uint32_t>(BaseID);
4325 else
4326 LE.write<uint32_t>(None);
4327 };
4328
Ben Langmuirfe971d92014-08-16 04:54:18 +00004329 // These values should be unique within a chain, since they will be read
4330 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004331 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4332 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4333 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4334 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4335 M->NumPreprocessedEntities);
4336 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4337 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4338 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4339 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004340 }
4341 }
4342 Record.clear();
4343 Record.push_back(MODULE_OFFSET_MAP);
4344 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4345 Buffer.data(), Buffer.size());
4346 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004347
Richard Smithb9eab6d2014-03-20 19:44:17 +00004348 RecordData DeclUpdatesOffsetsRecord;
4349
Richard Smith59442b42014-03-20 20:07:19 +00004350 // Keep writing types, declarations, and declaration update records
4351 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004352 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4353 WriteTypeAbbrevs();
4354 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004355 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4356 E = DeclsToRewrite.end();
4357 I != E; ++I)
4358 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004359 do {
4360 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4361 while (!DeclTypesToEmit.empty()) {
4362 DeclOrType DOT = DeclTypesToEmit.front();
4363 DeclTypesToEmit.pop();
4364 if (DOT.isType())
4365 WriteType(DOT.getType());
4366 else
4367 WriteDecl(Context, DOT.getDecl());
4368 }
4369 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004370 Stream.ExitBlock();
4371
Richard Smithb9eab6d2014-03-20 19:44:17 +00004372 DoneWritingDeclsAndTypes = true;
4373
4374 // These things can only be done once we've written out decls and types.
4375 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004376 if (!DeclUpdatesOffsetsRecord.empty())
4377 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004378 WriteCXXBaseSpecifiersOffsets();
Richard Smithc2bb8182015-03-24 06:36:48 +00004379 WriteCXXCtorInitializersOffsets();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004380 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004381 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004382
4383 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004384 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004385 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004386 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004387 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004388 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004389 WriteFPPragmaOptions(SemaRef.getFPOptions());
4390 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004391 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004392
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004393 // If we're emitting a module, write out the submodule information.
4394 if (WritingModule)
4395 WriteSubmodules(WritingModule);
4396
Douglas Gregor5204bde2011-08-02 16:26:37 +00004397 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4398
Douglas Gregord4df8652009-04-22 22:02:47 +00004399 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004400 if (!EagerlyDeserializedDecls.empty())
4401 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004402
4403 // Write the record containing tentative definitions.
4404 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004405 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004406
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004407 // Write the record containing unused file scoped decls.
4408 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004409 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004410
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004411 // Write the record containing weak undeclared identifiers.
4412 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004413 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004414 WeakUndeclaredIdentifiers);
4415
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004416 // Write the record containing ext_vector type names.
4417 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004418 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004419
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004420 // Write the record containing VTable uses information.
4421 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004422 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004423
Nico Weber72889432014-09-06 01:25:55 +00004424 // Write the record containing potentially unused local typedefs.
4425 if (!UnusedLocalTypedefNameCandidates.empty())
4426 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4427 UnusedLocalTypedefNameCandidates);
4428
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004429 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004430 if (!PendingInstantiations.empty())
4431 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004432
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004433 // Write the record containing declaration references of Sema.
4434 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004435 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004436
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004437 // Write the record containing CUDA-specific declaration references.
4438 if (!CUDASpecialDeclRefs.empty())
4439 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004440
4441 // Write the delegating constructors.
4442 if (!DelegatingCtorDecls.empty())
4443 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004444
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004445 // Write the known namespaces.
4446 if (!KnownNamespaces.empty())
4447 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004448
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004449 // Write the undefined internal functions and variables, and inline functions.
4450 if (!UndefinedButUsed.empty())
4451 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004452
4453 if (!DeleteExprsToAnalyze.empty())
4454 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4455
Douglas Gregor851443c2011-08-12 01:39:19 +00004456 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004457 for (auto *DC : UpdatedDeclContexts)
4458 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004459
Douglas Gregor959bb062011-12-03 01:15:29 +00004460 if (!WritingModule) {
4461 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004462 struct ModuleInfo {
4463 uint64_t ID;
4464 Module *M;
4465 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4466 };
Richard Smith56be7542014-03-21 00:33:59 +00004467 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004468 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004469 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004470 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4471 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004472 }
Richard Smith56be7542014-03-21 00:33:59 +00004473
4474 if (!Imports.empty()) {
4475 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4476 return A.ID < B.ID;
4477 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004478 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4479 return A.ID == B.ID;
4480 };
Richard Smith56be7542014-03-21 00:33:59 +00004481
4482 // Sort and deduplicate module IDs.
4483 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004484 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004485 Imports.end());
4486
4487 RecordData ImportedModules;
4488 for (const auto &Import : Imports) {
4489 ImportedModules.push_back(Import.ID);
4490 // FIXME: If the module has macros imported then later has declarations
4491 // imported, this location won't be the right one as a location for the
4492 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004493 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004494 }
4495
Douglas Gregor959bb062011-12-03 01:15:29 +00004496 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4497 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004498 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004499
Douglas Gregor851443c2011-08-12 01:39:19 +00004500 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004501 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004502 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004503 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004504 if(!WritingModule)
4505 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004506
Douglas Gregor08f01292009-04-17 22:13:46 +00004507 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004508 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004509 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004510 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004511 Record.push_back(NumLexicalDeclContexts);
4512 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004513 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004514 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004515}
4516
Richard Smithb9eab6d2014-03-20 19:44:17 +00004517void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004518 if (DeclUpdates.empty())
4519 return;
4520
Richard Smith59442b42014-03-20 20:07:19 +00004521 DeclUpdateMap LocalUpdates;
4522 LocalUpdates.swap(DeclUpdates);
4523
Richard Smith6ef42932014-03-20 21:02:00 +00004524 for (auto &DeclUpdate : LocalUpdates) {
4525 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004526 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004527 continue; // The decl will be written completely,no need to store updates.
4528
Richard Smithd28ac5b2014-03-22 23:33:22 +00004529 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004530 RecordData Record;
4531 for (auto &Update : DeclUpdate.second) {
4532 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4533
4534 Record.push_back(Kind);
4535 switch (Kind) {
4536 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4537 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4538 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004539 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004540 Record.push_back(GetDeclRef(Update.getDecl()));
4541 break;
4542
Richard Smith4d235792014-08-07 18:53:08 +00004543 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004544 // An updated body is emitted last, so that the reader doesn't need
4545 // to skip over the lazy body to reach statements for other records.
4546 Record.pop_back();
4547 HasUpdatedBody = true;
4548 break;
4549
Richard Smith4d235792014-08-07 18:53:08 +00004550 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4551 AddSourceLocation(Update.getLoc(), Record);
4552 break;
4553
Richard Smithcd45dbc2014-04-19 03:48:30 +00004554 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4555 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004556 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004557 AddCXXDefinitionData(RD, Record);
4558 Record.push_back(WriteDeclContextLexicalBlock(
4559 *Context, const_cast<CXXRecordDecl *>(RD)));
4560
4561 // This state is sometimes updated by template instantiation, when we
4562 // switch from the specialization referring to the template declaration
4563 // to it referring to the template definition.
4564 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4565 Record.push_back(MSInfo->getTemplateSpecializationKind());
4566 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4567 } else {
4568 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4569 Record.push_back(Spec->getTemplateSpecializationKind());
4570 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004571
4572 // The instantiation might have been resolved to a partial
4573 // specialization. If so, record which one.
4574 auto From = Spec->getInstantiatedFrom();
4575 if (auto PartialSpec =
4576 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4577 Record.push_back(true);
4578 AddDeclRef(PartialSpec, Record);
4579 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4580 Record);
4581 } else {
4582 Record.push_back(false);
4583 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004584 }
4585 Record.push_back(RD->getTagKind());
4586 AddSourceLocation(RD->getLocation(), Record);
4587 AddSourceLocation(RD->getLocStart(), Record);
4588 AddSourceLocation(RD->getRBraceLoc(), Record);
4589
4590 // Instantiation may change attributes; write them all out afresh.
4591 Record.push_back(D->hasAttrs());
4592 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004593 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4594 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004595
4596 // FIXME: Ensure we don't get here for explicit instantiations.
4597 break;
4598 }
4599
Richard Smithf8134002015-03-10 01:41:22 +00004600 case UPD_CXX_RESOLVED_DTOR_DELETE:
4601 AddDeclRef(Update.getDecl(), Record);
4602 break;
4603
Richard Smith564417a2014-03-20 21:47:22 +00004604 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4605 addExceptionSpec(
4606 *this,
4607 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4608 Record);
4609 break;
4610
Richard Smith6ef42932014-03-20 21:02:00 +00004611 case UPD_CXX_DEDUCED_RETURN_TYPE:
4612 Record.push_back(GetOrCreateTypeID(Update.getType()));
4613 break;
4614
4615 case UPD_DECL_MARKED_USED:
4616 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004617
4618 case UPD_MANGLING_NUMBER:
4619 case UPD_STATIC_LOCAL_NUMBER:
4620 Record.push_back(Update.getNumber());
4621 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004622
Alexey Bataev97720002014-11-11 04:05:39 +00004623 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4624 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4625 Record);
4626 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004627
4628 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004629 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004630 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004631
4632 case UPD_ADDED_ATTR_TO_RECORD:
4633 WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record);
4634 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004635 }
4636 }
4637
Richard Smithd28ac5b2014-03-22 23:33:22 +00004638 if (HasUpdatedBody) {
4639 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004640 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004641 Record.push_back(Def->isInlined());
4642 AddSourceLocation(Def->getInnerLocStart(), Record);
4643 AddFunctionDefinition(Def, Record);
4644 }
4645
Richard Smithcd45dbc2014-04-19 03:48:30 +00004646 OffsetsRecord.push_back(GetDeclRef(D));
4647 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4648
Richard Smith6ef42932014-03-20 21:02:00 +00004649 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004650
Richard Smithc2bb8182015-03-24 06:36:48 +00004651 FlushPendingAfterDecl();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004652 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004653}
4654
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004655void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004656 if (ReplacedDecls.empty())
4657 return;
4658
4659 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004660 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4661 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004662 Record.push_back(I->ID);
4663 Record.push_back(I->Offset);
4664 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004665 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004666 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004667}
4668
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004669void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004670 Record.push_back(Loc.getRawEncoding());
4671}
4672
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004673void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004674 AddSourceLocation(Range.getBegin(), Record);
4675 AddSourceLocation(Range.getEnd(), Record);
4676}
4677
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004678void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004679 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004680 const uint64_t *Words = Value.getRawData();
4681 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004682}
4683
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004684void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004685 Record.push_back(Value.isUnsigned());
4686 AddAPInt(Value, Record);
4687}
4688
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004689void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004690 AddAPInt(Value.bitcastToAPInt(), Record);
4691}
4692
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004693void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004694 Record.push_back(getIdentifierRef(II));
4695}
4696
Sebastian Redl539c5062010-08-18 23:57:32 +00004697IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004698 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004699 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004700
Sebastian Redl539c5062010-08-18 23:57:32 +00004701 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004702 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004703 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004704 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004705}
4706
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004707MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004708 // Don't emit builtin macros like __LINE__ to the AST file unless they
4709 // have been redefined by the header (in which case they are not
4710 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004711 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004712 return 0;
4713
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004714 MacroID &ID = MacroIDs[MI];
4715 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004716 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004717 MacroInfoToEmitData Info = { Name, MI, ID };
4718 MacroInfosToEmit.push_back(Info);
4719 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004720 return ID;
4721}
4722
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004723MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004724 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004725 return 0;
4726
4727 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4728 return MacroIDs[MI];
4729}
4730
4731uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004732 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004733}
4734
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004735void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004736 Record.push_back(getSelectorRef(SelRef));
4737}
4738
Sebastian Redl539c5062010-08-18 23:57:32 +00004739SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004740 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004741 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004742 }
4743
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004744 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004745 if (SID == 0 && Chain) {
4746 // This might trigger a ReadSelector callback, which will set the ID for
4747 // this selector.
4748 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004749 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004750 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004751 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004752 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004753 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004754 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004755 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004756}
4757
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004758void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004759 AddDeclRef(Temp->getDestructor(), Record);
4760}
4761
Richard Smithc2bb8182015-03-24 06:36:48 +00004762void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4763 RecordDataImpl &Record) {
4764 assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4765 CXXCtorInitializersToWrite.push_back(
4766 QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4767 Record.push_back(NextCXXCtorInitializersID++);
4768}
4769
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004770void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
Richard Smithc2bb8182015-03-24 06:36:48 +00004771 CXXBaseSpecifier const *BasesEnd,
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004772 RecordDataImpl &Record) {
4773 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4774 CXXBaseSpecifiersToWrite.push_back(
4775 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4776 Bases, BasesEnd));
4777 Record.push_back(NextCXXBaseSpecifiersID++);
4778}
4779
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004780void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004781 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004782 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004783 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004784 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004785 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004786 break;
4787 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004788 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004789 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004790 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004791 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004792 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004793 break;
4794 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004795 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004796 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004797 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004798 break;
John McCall0ad16662009-10-29 08:12:44 +00004799 case TemplateArgument::Null:
4800 case TemplateArgument::Integral:
4801 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004802 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004803 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004804 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004805 break;
4806 }
4807}
4808
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004809void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004810 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004811 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004812
4813 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4814 bool InfoHasSameExpr
4815 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4816 Record.push_back(InfoHasSameExpr);
4817 if (InfoHasSameExpr)
4818 return; // Avoid storing the same expr twice.
4819 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004820 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4821 Record);
4822}
4823
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004824void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4825 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004826 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004827 AddTypeRef(QualType(), Record);
4828 return;
4829 }
4830
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004831 AddTypeLoc(TInfo->getTypeLoc(), Record);
4832}
4833
4834void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4835 AddTypeRef(TL.getType(), Record);
4836
John McCall8f115c62009-10-16 21:56:05 +00004837 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004838 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004839 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004840}
4841
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004842void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004843 Record.push_back(GetOrCreateTypeID(T));
4844}
4845
Richard Smith2095ffe2015-03-16 20:11:03 +00004846TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004847 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004848 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4849 if (T.isNull())
4850 return TypeIdx();
4851 assert(!T.getLocalFastQualifiers());
4852
4853 TypeIdx &Idx = TypeIdxs[T];
4854 if (Idx.getIndex() == 0) {
4855 if (DoneWritingDeclsAndTypes) {
4856 assert(0 && "New type seen after serializing all the types to emit!");
4857 return TypeIdx();
4858 }
4859
4860 // We haven't seen this type before. Assign it a new ID and put it
4861 // into the queue of types to emit.
4862 Idx = TypeIdx(NextTypeID++);
4863 DeclTypesToEmit.push(T);
4864 }
4865 return Idx;
4866 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004867}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004868
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004869TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004870 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004871 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4872 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004873 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00004874 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004875
Richard Smith2095ffe2015-03-16 20:11:03 +00004876 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4877 assert(I != TypeIdxs.end() && "Type not emitted!");
4878 return I->second;
4879 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004880}
4881
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004882void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004883 Record.push_back(GetDeclRef(D));
4884}
4885
Sebastian Redl539c5062010-08-18 23:57:32 +00004886DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004887 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00004888
4889 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004890 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004891 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004892
4893 // If D comes from an AST file, its declaration ID is already known and
4894 // fixed.
4895 if (D->isFromASTFile())
4896 return D->getGlobalID();
4897
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004898 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004899 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004900 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004901 if (DoneWritingDeclsAndTypes) {
4902 assert(0 && "New decl seen after serializing all the decls to emit!");
4903 return 0;
4904 }
4905
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004906 // We haven't seen this declaration before. Give it a new ID and
4907 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004908 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004909 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004910 }
4911
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004912 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004913}
4914
Sebastian Redl539c5062010-08-18 23:57:32 +00004915DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00004916 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00004917 return 0;
4918
Douglas Gregorb3163e52012-01-05 22:33:30 +00004919 // If D comes from an AST file, its declaration ID is already known and
4920 // fixed.
4921 if (D->isFromASTFile())
4922 return D->getGlobalID();
4923
Douglas Gregore84a9da2009-04-20 20:36:09 +00004924 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4925 return DeclIDs[D];
4926}
4927
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004928void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004929 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004930 assert(D);
4931
4932 SourceLocation Loc = D->getLocation();
4933 if (Loc.isInvalid())
4934 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004935
4936 // We only keep track of the file-level declarations of each file.
4937 if (!D->getLexicalDeclContext()->isFileContext())
4938 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004939 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4940 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004941 if (isa<ParmVarDecl>(D))
4942 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004943
4944 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004945 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004946 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004947 FileID FID;
4948 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004949 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004950 if (FID.isInvalid())
4951 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004952 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004953
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004954 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004955 if (!Info)
4956 Info = new DeclIDInFileInfo();
4957
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004958 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004959 LocDeclIDsTy &Decls = Info->DeclIDs;
4960
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004961 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004962 Decls.push_back(LocDecl);
4963 return;
4964 }
4965
Benjamin Kramer45025c02013-08-24 13:22:59 +00004966 LocDeclIDsTy::iterator I =
4967 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004968
4969 Decls.insert(I, LocDecl);
4970}
4971
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004972void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004973 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004974 Record.push_back(Name.getNameKind());
4975 switch (Name.getNameKind()) {
4976 case DeclarationName::Identifier:
4977 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4978 break;
4979
4980 case DeclarationName::ObjCZeroArgSelector:
4981 case DeclarationName::ObjCOneArgSelector:
4982 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004983 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004984 break;
4985
4986 case DeclarationName::CXXConstructorName:
4987 case DeclarationName::CXXDestructorName:
4988 case DeclarationName::CXXConversionFunctionName:
4989 AddTypeRef(Name.getCXXNameType(), Record);
4990 break;
4991
4992 case DeclarationName::CXXOperatorName:
4993 Record.push_back(Name.getCXXOverloadedOperator());
4994 break;
4995
Alexis Hunt3d221f22009-11-29 07:34:05 +00004996 case DeclarationName::CXXLiteralOperatorName:
4997 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4998 break;
4999
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005000 case DeclarationName::CXXUsingDirective:
5001 // No extra data to emit
5002 break;
5003 }
5004}
Chris Lattnerca025db2010-05-07 21:43:38 +00005005
Richard Smithd08aeb62014-08-28 01:33:39 +00005006unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5007 assert(needsAnonymousDeclarationNumber(D) &&
5008 "expected an anonymous declaration");
5009
5010 // Number the anonymous declarations within this context, if we've not
5011 // already done so.
5012 auto It = AnonymousDeclarationNumbers.find(D);
5013 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005014 auto *DC = D->getLexicalDeclContext();
5015 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5016 AnonymousDeclarationNumbers[ND] = Number;
5017 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005018
5019 It = AnonymousDeclarationNumbers.find(D);
5020 assert(It != AnonymousDeclarationNumbers.end() &&
5021 "declaration not found within its lexical context");
5022 }
5023
5024 return It->second;
5025}
5026
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005027void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005028 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005029 switch (Name.getNameKind()) {
5030 case DeclarationName::CXXConstructorName:
5031 case DeclarationName::CXXDestructorName:
5032 case DeclarationName::CXXConversionFunctionName:
5033 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5034 break;
5035
5036 case DeclarationName::CXXOperatorName:
5037 AddSourceLocation(
5038 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5039 Record);
5040 AddSourceLocation(
5041 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5042 Record);
5043 break;
5044
5045 case DeclarationName::CXXLiteralOperatorName:
5046 AddSourceLocation(
5047 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5048 Record);
5049 break;
5050
5051 case DeclarationName::Identifier:
5052 case DeclarationName::ObjCZeroArgSelector:
5053 case DeclarationName::ObjCOneArgSelector:
5054 case DeclarationName::ObjCMultiArgSelector:
5055 case DeclarationName::CXXUsingDirective:
5056 break;
5057 }
5058}
5059
5060void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005061 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005062 AddDeclarationName(NameInfo.getName(), Record);
5063 AddSourceLocation(NameInfo.getLoc(), Record);
5064 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5065}
5066
5067void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005068 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005069 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005070 Record.push_back(Info.NumTemplParamLists);
5071 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5072 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5073}
5074
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005075void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005076 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005077 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005078 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005079 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005080
5081 // Push each of the NNS's onto a stack for serialization in reverse order.
5082 while (NNS) {
5083 NestedNames.push_back(NNS);
5084 NNS = NNS->getPrefix();
5085 }
5086
5087 Record.push_back(NestedNames.size());
5088 while(!NestedNames.empty()) {
5089 NNS = NestedNames.pop_back_val();
5090 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5091 Record.push_back(Kind);
5092 switch (Kind) {
5093 case NestedNameSpecifier::Identifier:
5094 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5095 break;
5096
5097 case NestedNameSpecifier::Namespace:
5098 AddDeclRef(NNS->getAsNamespace(), Record);
5099 break;
5100
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005101 case NestedNameSpecifier::NamespaceAlias:
5102 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5103 break;
5104
Chris Lattnerca025db2010-05-07 21:43:38 +00005105 case NestedNameSpecifier::TypeSpec:
5106 case NestedNameSpecifier::TypeSpecWithTemplate:
5107 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5108 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5109 break;
5110
5111 case NestedNameSpecifier::Global:
5112 // Don't need to write an associated value.
5113 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005114
5115 case NestedNameSpecifier::Super:
5116 AddDeclRef(NNS->getAsRecordDecl(), Record);
5117 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005118 }
5119 }
5120}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005121
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005122void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5123 RecordDataImpl &Record) {
5124 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005125 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005126 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005127
5128 // Push each of the nested-name-specifiers's onto a stack for
5129 // serialization in reverse order.
5130 while (NNS) {
5131 NestedNames.push_back(NNS);
5132 NNS = NNS.getPrefix();
5133 }
5134
5135 Record.push_back(NestedNames.size());
5136 while(!NestedNames.empty()) {
5137 NNS = NestedNames.pop_back_val();
5138 NestedNameSpecifier::SpecifierKind Kind
5139 = NNS.getNestedNameSpecifier()->getKind();
5140 Record.push_back(Kind);
5141 switch (Kind) {
5142 case NestedNameSpecifier::Identifier:
5143 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5144 AddSourceRange(NNS.getLocalSourceRange(), Record);
5145 break;
5146
5147 case NestedNameSpecifier::Namespace:
5148 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5149 AddSourceRange(NNS.getLocalSourceRange(), Record);
5150 break;
5151
5152 case NestedNameSpecifier::NamespaceAlias:
5153 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5154 AddSourceRange(NNS.getLocalSourceRange(), Record);
5155 break;
5156
5157 case NestedNameSpecifier::TypeSpec:
5158 case NestedNameSpecifier::TypeSpecWithTemplate:
5159 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5160 AddTypeLoc(NNS.getTypeLoc(), Record);
5161 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5162 break;
5163
5164 case NestedNameSpecifier::Global:
5165 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5166 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005167
5168 case NestedNameSpecifier::Super:
5169 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5170 AddSourceRange(NNS.getLocalSourceRange(), Record);
5171 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005172 }
5173 }
5174}
5175
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005176void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005177 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005178 Record.push_back(Kind);
5179 switch (Kind) {
5180 case TemplateName::Template:
5181 AddDeclRef(Name.getAsTemplateDecl(), Record);
5182 break;
5183
5184 case TemplateName::OverloadedTemplate: {
5185 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5186 Record.push_back(OvT->size());
5187 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5188 I != E; ++I)
5189 AddDeclRef(*I, Record);
5190 break;
5191 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005192
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005193 case TemplateName::QualifiedTemplate: {
5194 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5195 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5196 Record.push_back(QualT->hasTemplateKeyword());
5197 AddDeclRef(QualT->getTemplateDecl(), Record);
5198 break;
5199 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005200
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005201 case TemplateName::DependentTemplate: {
5202 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5203 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5204 Record.push_back(DepT->isIdentifier());
5205 if (DepT->isIdentifier())
5206 AddIdentifierRef(DepT->getIdentifier(), Record);
5207 else
5208 Record.push_back(DepT->getOperator());
5209 break;
5210 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005211
5212 case TemplateName::SubstTemplateTemplateParm: {
5213 SubstTemplateTemplateParmStorage *subst
5214 = Name.getAsSubstTemplateTemplateParm();
5215 AddDeclRef(subst->getParameter(), Record);
5216 AddTemplateName(subst->getReplacement(), Record);
5217 break;
5218 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005219
5220 case TemplateName::SubstTemplateTemplateParmPack: {
5221 SubstTemplateTemplateParmPackStorage *SubstPack
5222 = Name.getAsSubstTemplateTemplateParmPack();
5223 AddDeclRef(SubstPack->getParameterPack(), Record);
5224 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5225 break;
5226 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005227 }
5228}
5229
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005230void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005231 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005232 Record.push_back(Arg.getKind());
5233 switch (Arg.getKind()) {
5234 case TemplateArgument::Null:
5235 break;
5236 case TemplateArgument::Type:
5237 AddTypeRef(Arg.getAsType(), Record);
5238 break;
5239 case TemplateArgument::Declaration:
5240 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005241 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005242 break;
5243 case TemplateArgument::NullPtr:
5244 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005245 break;
5246 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005247 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005248 AddTypeRef(Arg.getIntegralType(), Record);
5249 break;
5250 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005251 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5252 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005253 case TemplateArgument::TemplateExpansion:
5254 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005255 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005256 Record.push_back(*NumExpansions + 1);
5257 else
5258 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005259 break;
5260 case TemplateArgument::Expression:
5261 AddStmt(Arg.getAsExpr());
5262 break;
5263 case TemplateArgument::Pack:
5264 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005265 for (const auto &P : Arg.pack_elements())
5266 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005267 break;
5268 }
5269}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005270
5271void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005272ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005273 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005274 assert(TemplateParams && "No TemplateParams!");
5275 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5276 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5277 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5278 Record.push_back(TemplateParams->size());
5279 for (TemplateParameterList::const_iterator
5280 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5281 P != PEnd; ++P)
5282 AddDeclRef(*P, Record);
5283}
5284
5285/// \brief Emit a template argument list.
5286void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005287ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005288 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005289 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005290 Record.push_back(TemplateArgs->size());
5291 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005292 AddTemplateArgument(TemplateArgs->get(i), Record);
5293}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005294
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005295void
5296ASTWriter::AddASTTemplateArgumentListInfo
5297(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5298 assert(ASTTemplArgList && "No ASTTemplArgList!");
5299 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5300 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5301 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5302 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5303 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5304 AddTemplateArgumentLoc(TemplArgs[i], Record);
5305}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005306
5307void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005308ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005309 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005310 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005311 I = Set.begin(), E = Set.end(); I != E; ++I) {
5312 AddDeclRef(I.getDecl(), Record);
5313 Record.push_back(I.getAccess());
5314 }
5315}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005316
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005317void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005318 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005319 Record.push_back(Base.isVirtual());
5320 Record.push_back(Base.isBaseOfClass());
5321 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005322 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005323 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005324 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005325 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5326 : SourceLocation(),
5327 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005328}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005329
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005330void ASTWriter::FlushCXXBaseSpecifiers() {
5331 RecordData Record;
Richard Smithc2bb8182015-03-24 06:36:48 +00005332 unsigned N = CXXBaseSpecifiersToWrite.size();
5333 for (unsigned I = 0; I != N; ++I) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005334 Record.clear();
5335
5336 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005337 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005338 if (Index == CXXBaseSpecifiersOffsets.size())
5339 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5340 else {
5341 if (Index > CXXBaseSpecifiersOffsets.size())
5342 CXXBaseSpecifiersOffsets.resize(Index + 1);
5343 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5344 }
5345
5346 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5347 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5348 Record.push_back(BEnd - B);
5349 for (; B != BEnd; ++B)
5350 AddCXXBaseSpecifier(*B, Record);
5351 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005352
5353 // Flush any expressions that were written as part of the base specifiers.
5354 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005355 }
5356
Richard Smithc2bb8182015-03-24 06:36:48 +00005357 assert(N == CXXBaseSpecifiersToWrite.size() &&
5358 "added more base specifiers while writing base specifiers");
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005359 CXXBaseSpecifiersToWrite.clear();
5360}
5361
Alexis Hunt1d792652011-01-08 20:30:50 +00005362void ASTWriter::AddCXXCtorInitializers(
5363 const CXXCtorInitializer * const *CtorInitializers,
5364 unsigned NumCtorInitializers,
5365 RecordDataImpl &Record) {
5366 Record.push_back(NumCtorInitializers);
5367 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5368 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005369
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005370 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005371 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005372 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005373 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005374 } else if (Init->isDelegatingInitializer()) {
5375 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005376 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005377 } else if (Init->isMemberInitializer()){
5378 Record.push_back(CTOR_INITIALIZER_MEMBER);
5379 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005380 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005381 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5382 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005383 }
Francois Pichetd583da02010-12-04 09:14:42 +00005384
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005385 AddSourceLocation(Init->getMemberLocation(), Record);
5386 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005387 AddSourceLocation(Init->getLParenLoc(), Record);
5388 AddSourceLocation(Init->getRParenLoc(), Record);
5389 Record.push_back(Init->isWritten());
5390 if (Init->isWritten()) {
5391 Record.push_back(Init->getSourceOrder());
5392 } else {
5393 Record.push_back(Init->getNumArrayIndices());
5394 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5395 AddDeclRef(Init->getArrayIndex(i), Record);
5396 }
5397 }
5398}
5399
Richard Smithc2bb8182015-03-24 06:36:48 +00005400void ASTWriter::FlushCXXCtorInitializers() {
5401 RecordData Record;
5402
5403 unsigned N = CXXCtorInitializersToWrite.size();
Daniel Jasperc77b0a12015-03-24 08:06:38 +00005404 (void)N; // Silence unused warning in non-assert builds.
Richard Smithc2bb8182015-03-24 06:36:48 +00005405 for (auto &Init : CXXCtorInitializersToWrite) {
5406 Record.clear();
5407
5408 // Record the offset of this mem-initializer list.
5409 unsigned Index = Init.ID - 1;
5410 if (Index == CXXCtorInitializersOffsets.size())
5411 CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5412 else {
5413 if (Index > CXXCtorInitializersOffsets.size())
5414 CXXCtorInitializersOffsets.resize(Index + 1);
5415 CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5416 }
5417
5418 AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5419 Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5420
5421 // Flush any expressions that were written as part of the initializers.
5422 FlushStmts();
5423 }
5424
5425 assert(N == CXXCtorInitializersToWrite.size() &&
5426 "added more ctor initializers while writing ctor initializers");
5427 CXXCtorInitializersToWrite.clear();
5428}
5429
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005430void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005431 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005432 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005433 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005434 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005435 Record.push_back(Data.Aggregate);
5436 Record.push_back(Data.PlainOldData);
5437 Record.push_back(Data.Empty);
5438 Record.push_back(Data.Polymorphic);
5439 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005440 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005441 Record.push_back(Data.HasNoNonEmptyBases);
5442 Record.push_back(Data.HasPrivateFields);
5443 Record.push_back(Data.HasProtectedFields);
5444 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005445 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005446 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005447 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005448 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005449 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005450 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5451 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5452 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5453 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5454 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5455 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005456 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005457 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005458 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005459 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005460 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005461 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005462 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005463 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005464 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005465 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005466 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5467 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5468 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5469 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005470 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005471
5472 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005473 if (Data.NumBases > 0)
5474 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5475 Record);
5476
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005477 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5478 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005479 if (Data.NumVBases > 0)
5480 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5481 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005482
Richard Smitha4ba74c2013-08-30 04:46:40 +00005483 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5484 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005485 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005486 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005487
5488 // Add lambda-specific data.
5489 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005490 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005491 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005492 Record.push_back(Lambda.IsGenericLambda);
5493 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005494 Record.push_back(Lambda.NumCaptures);
5495 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005496 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005497 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005498 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005499 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005500 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005501 AddSourceLocation(Capture.getLocation(), Record);
5502 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005503 Record.push_back(Capture.getCaptureKind());
5504 switch (Capture.getCaptureKind()) {
5505 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005506 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005507 break;
5508 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005509 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005510 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005511 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005512 AddDeclRef(Var, Record);
5513 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5514 : SourceLocation(),
5515 Record);
5516 break;
5517 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005518 }
5519 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005520}
5521
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005522void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005523 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005524 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005525 assert(FirstDeclID == NextDeclID &&
5526 FirstTypeID == NextTypeID &&
5527 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005528 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005529 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005530 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005531 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005532
Sebastian Redl07a89a82010-07-30 00:29:29 +00005533 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005534
Richard Smith7f330cd2015-03-18 01:42:29 +00005535 // Note, this will get called multiple times, once one the reader starts up
5536 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005537 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5538 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5539 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005540 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005541 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005542 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005543 NextDeclID = FirstDeclID;
5544 NextTypeID = FirstTypeID;
5545 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005546 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005547 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005548 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005549}
5550
Sebastian Redl539c5062010-08-18 23:57:32 +00005551void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005552 // Always keep the highest ID. See \p TypeRead() for more information.
5553 IdentID &StoredID = IdentifierIDs[II];
5554 if (ID > StoredID)
5555 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005556}
5557
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005558void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005559 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005560 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005561 if (ID > StoredID)
5562 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005563}
5564
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005565void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005566 // Always take the highest-numbered type index. This copes with an interesting
5567 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005568 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005569 // keep the higher-numbered entry so that we can properly write it out to
5570 // the AST file.
5571 TypeIdx &StoredIdx = TypeIdxs[T];
5572 if (Idx.getIndex() >= StoredIdx.getIndex())
5573 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005574}
5575
Sebastian Redl539c5062010-08-18 23:57:32 +00005576void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005577 // Always keep the highest ID. See \p TypeRead() for more information.
5578 SelectorID &StoredID = SelectorIDs[S];
5579 if (ID > StoredID)
5580 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005581}
Douglas Gregor91096292010-10-02 19:29:26 +00005582
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005583void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005584 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005585 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005586 MacroDefinitions[MD] = ID;
5587}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005588
Douglas Gregore37a85a2011-12-02 17:30:13 +00005589void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5590 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5591 SubmoduleIDs[Mod] = ID;
5592}
5593
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005594void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005595 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005596 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005597 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5598 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005599 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005600 // A forward reference was mutated into a definition. Rewrite it.
5601 // FIXME: This happens during template instantiation, should we
5602 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005603 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5604 "completed a tag from another module but not by instantiation?");
5605 DeclUpdates[RD].push_back(
5606 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005607 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005608 }
5609}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005610
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005611void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5612 // TU and namespaces are handled elsewhere.
5613 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5614 return;
5615
Douglas Gregorb3722e22011-09-09 23:01:35 +00005616 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005617 return; // Not a source decl added to a DeclContext from PCH.
5618
Douglas Gregor9f782892013-01-21 15:25:38 +00005619 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005620 assert(!WritingAST && "Already writing the AST!");
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005621 UpdatedDeclContexts.insert(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005622 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005623}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005624
5625void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5626 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005627 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005628 return; // Not a source member added to a class from PCH.
5629 if (!isa<CXXMethodDecl>(D))
5630 return; // We are interested in lazily declared implicit methods.
5631
5632 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005633 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005634 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005635 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005636}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005637
5638void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5639 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005640 // The specializations set is kept in the canonical template.
5641 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005642 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005643 return; // Not a source specialization added to a template from PCH.
5644
Richard Smithe9a8bc32014-09-30 00:45:29 +00005645 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005646 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5647 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005648}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005649
Larisse Voufo39a1e502013-08-06 01:03:05 +00005650void ASTWriter::AddedCXXTemplateSpecialization(
5651 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5652 // The specializations set is kept in the canonical template.
Larisse Voufo39a1e502013-08-06 01:03:05 +00005653 TD = TD->getCanonicalDecl();
5654 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5655 return; // Not a source specialization added to a template from PCH.
5656
Richard Smithe9a8bc32014-09-30 00:45:29 +00005657 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005658 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5659 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005660}
5661
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005662void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5663 const FunctionDecl *D) {
5664 // The specializations set is kept in the canonical template.
5665 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005666 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005667 return; // Not a source specialization added to a template from PCH.
5668
Richard Smithe9a8bc32014-09-30 00:45:29 +00005669 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005670 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5671 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005672}
5673
Richard Smith564417a2014-03-20 21:47:22 +00005674void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005675 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5676 if (!Chain) return;
5677 Chain->forEachFormerlyCanonicalImportedDecl(FD, [&](const Decl *D) {
5678 // If we don't already know the exception specification for this redecl
5679 // chain, add an update record for it.
5680 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5681 ->getType()
5682 ->castAs<FunctionProtoType>()
5683 ->getExceptionSpecType()))
5684 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5685 });
Richard Smith564417a2014-03-20 21:47:22 +00005686}
5687
Richard Smith1fa5d642013-05-11 05:45:24 +00005688void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5689 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005690 if (!Chain) return;
5691 Chain->forEachFormerlyCanonicalImportedDecl(FD, [&](const Decl *D) {
5692 DeclUpdates[D].push_back(
5693 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5694 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005695}
5696
Richard Smithf8134002015-03-10 01:41:22 +00005697void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5698 const FunctionDecl *Delete) {
5699 assert(!WritingAST && "Already writing the AST!");
5700 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005701 if (!Chain) return;
5702 Chain->forEachFormerlyCanonicalImportedDecl(DD, [&](const Decl *D) {
5703 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5704 });
Richard Smithf8134002015-03-10 01:41:22 +00005705}
5706
Sebastian Redlab238a72011-04-24 16:28:06 +00005707void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005708 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005709 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005710 return; // Declaration not imported from PCH.
5711
Richard Smith4d235792014-08-07 18:53:08 +00005712 // Implicit function decl from a PCH was defined.
5713 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005714}
5715
Richard Smithd28ac5b2014-03-22 23:33:22 +00005716void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5717 assert(!WritingAST && "Already writing the AST!");
5718 if (!D->isFromASTFile())
5719 return;
5720
Richard Smith9e2341d2015-03-23 03:25:59 +00005721 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005722}
5723
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005724void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005725 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005726 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005727 return;
5728
5729 // Since the actual instantiation is delayed, this really means that we need
5730 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005731 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005732 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5733 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005734}
5735
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005736void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5737 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005738 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005739 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005740 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005741
5742 assert(IFD->getDefinition() && "Category on a class without a definition?");
5743 ObjCClassesWithCategories.insert(
5744 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005745}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005746
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005747
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005748void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5749 const ObjCPropertyDecl *OrigProp,
5750 const ObjCCategoryDecl *ClassExt) {
5751 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5752 if (!D)
5753 return;
5754
5755 assert(!WritingAST && "Already writing the AST!");
5756 if (!D->isFromASTFile())
5757 return; // Declaration not imported from PCH.
5758
5759 RewriteDecl(D);
5760}
Eli Friedman276dd182013-09-05 00:02:25 +00005761
5762void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5763 assert(!WritingAST && "Already writing the AST!");
5764 if (!D->isFromASTFile())
5765 return;
5766
Aaron Ballman4f45b712014-03-21 15:22:56 +00005767 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005768}
Alexey Bataev97720002014-11-11 04:05:39 +00005769
5770void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5771 assert(!WritingAST && "Already writing the AST!");
5772 if (!D->isFromASTFile())
5773 return;
5774
5775 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5776}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005777
Richard Smith4caa4492015-05-15 02:34:32 +00005778void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00005779 assert(!WritingAST && "Already writing the AST!");
5780 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005781 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005782}
Alex Denisovfde64952015-06-26 05:28:36 +00005783
5784void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5785 const RecordDecl *Record) {
5786 assert(!WritingAST && "Already writing the AST!");
5787 if (!Record->isFromASTFile())
5788 return;
5789 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5790}