blob: 566c7d7c9f2e866b0d8235b67c075fe2241e2f08 [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);
Douglas Gregorab209d82015-07-07 03:58:42 +0000430 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000431 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000432}
433
Steve Narofffb4330f2009-06-17 22:40:22 +0000434void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000435ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000436 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000437 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000438}
439
Eli Friedman0dfb8892011-10-06 23:00:33 +0000440void
441ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
442 Writer.AddTypeRef(T->getValueType(), Record);
443 Code = TYPE_ATOMIC;
444}
445
John McCall8f115c62009-10-16 21:56:05 +0000446namespace {
447
448class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000449 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000450 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000451
452public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000453 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000454 : Writer(Writer), Record(Record) { }
455
John McCall17001972009-10-18 01:05:36 +0000456#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000457#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000458 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000459#include "clang/AST/TypeLocNodes.def"
460
John McCall17001972009-10-18 01:05:36 +0000461 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
462 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000463};
464
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000465}
John McCall8f115c62009-10-16 21:56:05 +0000466
John McCall17001972009-10-18 01:05:36 +0000467void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
468 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000469}
John McCall17001972009-10-18 01:05:36 +0000470void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000471 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
472 if (TL.needsExtraLocalData()) {
473 Record.push_back(TL.getWrittenTypeSpec());
474 Record.push_back(TL.getWrittenSignSpec());
475 Record.push_back(TL.getWrittenWidthSpec());
476 Record.push_back(TL.hasModeAttr());
477 }
John McCall8f115c62009-10-16 21:56:05 +0000478}
John McCall17001972009-10-18 01:05:36 +0000479void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000481}
John McCall17001972009-10-18 01:05:36 +0000482void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000484}
Reid Kleckner8a365022013-06-24 17:51:48 +0000485void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
486 // nothing to do
487}
Reid Kleckner0503a872013-12-05 01:23:43 +0000488void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
489 // nothing to do
490}
John McCall17001972009-10-18 01:05:36 +0000491void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000493}
John McCall17001972009-10-18 01:05:36 +0000494void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000496}
John McCall17001972009-10-18 01:05:36 +0000497void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000502 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000503}
John McCall17001972009-10-18 01:05:36 +0000504void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
506 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
507 Record.push_back(TL.getSizeExpr() ? 1 : 0);
508 if (TL.getSizeExpr())
509 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000510}
John McCall17001972009-10-18 01:05:36 +0000511void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
512 VisitArrayTypeLoc(TL);
513}
514void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
515 VisitArrayTypeLoc(TL);
516}
517void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
518 VisitArrayTypeLoc(TL);
519}
520void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
521 DependentSizedArrayTypeLoc TL) {
522 VisitArrayTypeLoc(TL);
523}
524void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
525 DependentSizedExtVectorTypeLoc TL) {
526 Writer.AddSourceLocation(TL.getNameLoc(), Record);
527}
528void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
531void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
532 Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
534void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000535 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000536 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
537 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000538 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000539 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
540 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000541}
542void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
543 VisitFunctionTypeLoc(TL);
544}
545void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
546 VisitFunctionTypeLoc(TL);
547}
John McCallb96ec562009-12-04 22:46:56 +0000548void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
John McCall17001972009-10-18 01:05:36 +0000551void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
554void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000555 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
556 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
557 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000558}
559void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000560 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
561 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
562 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
563 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000564}
565void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
Alexis Hunte852b102011-05-24 22:41:36 +0000568void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
569 Writer.AddSourceLocation(TL.getKWLoc(), Record);
570 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
571 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
572 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
573}
Richard Smith30482bc2011-02-20 03:19:35 +0000574void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getNameLoc(), Record);
576}
John McCall17001972009-10-18 01:05:36 +0000577void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
578 Writer.AddSourceLocation(TL.getNameLoc(), Record);
579}
580void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
John McCall81904512011-01-06 01:58:22 +0000583void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
585 if (TL.hasAttrOperand()) {
586 SourceRange range = TL.getAttrOperandParensRange();
587 Writer.AddSourceLocation(range.getBegin(), Record);
588 Writer.AddSourceLocation(range.getEnd(), Record);
589 }
590 if (TL.hasAttrExprOperand()) {
591 Expr *operand = TL.getAttrExprOperand();
592 Record.push_back(operand ? 1 : 0);
593 if (operand) Writer.AddStmt(operand);
594 } else if (TL.hasAttrEnumOperand()) {
595 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
596 }
597}
John McCall17001972009-10-18 01:05:36 +0000598void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
599 Writer.AddSourceLocation(TL.getNameLoc(), Record);
600}
John McCallcebee162009-10-18 09:09:24 +0000601void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
602 SubstTemplateTypeParmTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
604}
Douglas Gregorada4b792011-01-14 02:55:32 +0000605void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
606 SubstTemplateTypeParmPackTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getNameLoc(), Record);
608}
John McCall17001972009-10-18 01:05:36 +0000609void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
610 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000611 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000612 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
613 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
614 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
615 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000616 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
617 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000618}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000619void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
620 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
621 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
622}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000623void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000624 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000625 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000626}
John McCalle78aac42010-03-10 03:28:59 +0000627void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
628 Writer.AddSourceLocation(TL.getNameLoc(), Record);
629}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000630void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000631 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000632 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000633 Writer.AddSourceLocation(TL.getNameLoc(), Record);
634}
John McCallc392f372010-06-11 00:33:02 +0000635void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
636 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000637 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000638 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000639 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000640 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000641 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
642 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
643 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000644 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
645 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000646}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000647void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
648 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
649}
John McCall17001972009-10-18 01:05:36 +0000650void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
651 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000652}
653void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
654 Record.push_back(TL.hasBaseTypeAsWritten());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000655 Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record);
656 Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record);
657 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
658 Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record);
659 Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record);
660 Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000661 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
662 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000663}
John McCallfc93cf92009-10-22 22:37:11 +0000664void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
665 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000666}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000667void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
668 Writer.AddSourceLocation(TL.getKWLoc(), Record);
669 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
670 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
671}
John McCall8f115c62009-10-16 21:56:05 +0000672
Richard Smith01b2cb42014-07-26 06:37:51 +0000673void ASTWriter::WriteTypeAbbrevs() {
674 using namespace llvm;
675
676 BitCodeAbbrev *Abv;
677
678 // Abbreviation for TYPE_EXT_QUAL
679 Abv = new BitCodeAbbrev();
680 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
683 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
684
685 // Abbreviation for TYPE_FUNCTION_PROTO
686 Abv = new BitCodeAbbrev();
687 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
688 // FunctionType
689 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
690 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
691 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
692 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
694 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
695 // FunctionProtoType
696 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
697 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
698 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
699 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
700 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
701 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
702 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
703 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
704 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
705}
706
Chris Lattner19cea4e2009-04-22 05:57:30 +0000707//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000708// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000709//===----------------------------------------------------------------------===//
710
Chris Lattner28fa4e62009-04-26 22:26:21 +0000711static void EmitBlockID(unsigned ID, const char *Name,
712 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000713 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000714 Record.clear();
715 Record.push_back(ID);
716 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
717
718 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000719 if (!Name || Name[0] == 0)
720 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000721 Record.clear();
722 while (*Name)
723 Record.push_back(*Name++);
724 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
725}
726
727static void EmitRecordID(unsigned ID, const char *Name,
728 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000729 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000730 Record.clear();
731 Record.push_back(ID);
732 while (*Name)
733 Record.push_back(*Name++);
734 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000735}
736
737static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000738 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000739#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000740 RECORD(STMT_STOP);
741 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000742 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000743 RECORD(STMT_NULL);
744 RECORD(STMT_COMPOUND);
745 RECORD(STMT_CASE);
746 RECORD(STMT_DEFAULT);
747 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000748 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000749 RECORD(STMT_IF);
750 RECORD(STMT_SWITCH);
751 RECORD(STMT_WHILE);
752 RECORD(STMT_DO);
753 RECORD(STMT_FOR);
754 RECORD(STMT_GOTO);
755 RECORD(STMT_INDIRECT_GOTO);
756 RECORD(STMT_CONTINUE);
757 RECORD(STMT_BREAK);
758 RECORD(STMT_RETURN);
759 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000760 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000761 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000762 RECORD(EXPR_PREDEFINED);
763 RECORD(EXPR_DECL_REF);
764 RECORD(EXPR_INTEGER_LITERAL);
765 RECORD(EXPR_FLOATING_LITERAL);
766 RECORD(EXPR_IMAGINARY_LITERAL);
767 RECORD(EXPR_STRING_LITERAL);
768 RECORD(EXPR_CHARACTER_LITERAL);
769 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000770 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000771 RECORD(EXPR_UNARY_OPERATOR);
772 RECORD(EXPR_SIZEOF_ALIGN_OF);
773 RECORD(EXPR_ARRAY_SUBSCRIPT);
774 RECORD(EXPR_CALL);
775 RECORD(EXPR_MEMBER);
776 RECORD(EXPR_BINARY_OPERATOR);
777 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
778 RECORD(EXPR_CONDITIONAL_OPERATOR);
779 RECORD(EXPR_IMPLICIT_CAST);
780 RECORD(EXPR_CSTYLE_CAST);
781 RECORD(EXPR_COMPOUND_LITERAL);
782 RECORD(EXPR_EXT_VECTOR_ELEMENT);
783 RECORD(EXPR_INIT_LIST);
784 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000785 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000786 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000787 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000788 RECORD(EXPR_VA_ARG);
789 RECORD(EXPR_ADDR_LABEL);
790 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000791 RECORD(EXPR_CHOOSE);
792 RECORD(EXPR_GNU_NULL);
793 RECORD(EXPR_SHUFFLE_VECTOR);
794 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000795 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000796 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000797 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000798 RECORD(EXPR_OBJC_ARRAY_LITERAL);
799 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000800 RECORD(EXPR_OBJC_ENCODE);
801 RECORD(EXPR_OBJC_SELECTOR_EXPR);
802 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
803 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
804 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
805 RECORD(EXPR_OBJC_KVC_REF_EXPR);
806 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000807 RECORD(STMT_OBJC_FOR_COLLECTION);
808 RECORD(STMT_OBJC_CATCH);
809 RECORD(STMT_OBJC_FINALLY);
810 RECORD(STMT_OBJC_AT_TRY);
811 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
812 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000813 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000814 RECORD(STMT_CXX_CATCH);
815 RECORD(STMT_CXX_TRY);
816 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000817 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000818 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000819 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000820 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000821 RECORD(EXPR_CXX_STATIC_CAST);
822 RECORD(EXPR_CXX_DYNAMIC_CAST);
823 RECORD(EXPR_CXX_REINTERPRET_CAST);
824 RECORD(EXPR_CXX_CONST_CAST);
825 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000826 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000827 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000828 RECORD(EXPR_CXX_BOOL_LITERAL);
829 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000830 RECORD(EXPR_CXX_TYPEID_EXPR);
831 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000832 RECORD(EXPR_CXX_THIS);
833 RECORD(EXPR_CXX_THROW);
834 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000835 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000836 RECORD(EXPR_CXX_BIND_TEMPORARY);
837 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
838 RECORD(EXPR_CXX_NEW);
839 RECORD(EXPR_CXX_DELETE);
840 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
841 RECORD(EXPR_EXPR_WITH_CLEANUPS);
842 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
843 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
844 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
845 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
846 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000847 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000848 RECORD(EXPR_CXX_NOEXCEPT);
849 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000850 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
851 RECORD(EXPR_TYPE_TRAIT);
852 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000853 RECORD(EXPR_PACK_EXPANSION);
854 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000855 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000856 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000857 RECORD(EXPR_FUNCTION_PARM_PACK);
858 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000859 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000860 RECORD(EXPR_CXX_UUIDOF_EXPR);
861 RECORD(EXPR_CXX_UUIDOF_TYPE);
862 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000863#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000864}
Mike Stump11289f42009-09-09 15:08:12 +0000865
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000866void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000867 RecordData Record;
868 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000869
Sebastian Redl539c5062010-08-18 23:57:32 +0000870#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
871#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000872
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000873 // Control Block.
874 BLOCK(CONTROL_BLOCK);
875 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000876 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000877 RECORD(MODULE_NAME);
878 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000879 RECORD(IMPORTS);
880 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) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001076 bool Changed = FileMgr.makeAbsolutePath(Path);
1077 return Changed | FileMgr.removeDotPaths(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001078}
1079
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001080/// \brief Adjusts the given filename to only write out the portion of the
1081/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001082///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001083/// \param Filename the file name to adjust.
1084///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001085/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1086/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001087///
1088/// \returns either the original filename (if it needs no adjustment) or the
1089/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001090static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001091adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001092 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001093
Richard Smith7ed1bc92014-12-05 22:42:13 +00001094 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001095 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001096
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001097 // Verify that the filename and the system root have the same prefix.
1098 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001099 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1100 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001101 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001102
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001103 // We hit the end of the filename before we hit the end of the system root.
1104 if (!Filename[Pos])
1105 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001106
Richard Smith7ed1bc92014-12-05 22:42:13 +00001107 // If there's not a path separator at the end of the base directory nor
1108 // immediately after it, then this isn't within the base directory.
1109 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1110 if (!llvm::sys::path::is_separator(BaseDir.back()))
1111 return Filename;
1112 } else {
1113 // If the file name has a '/' at the current position, skip over the '/'.
1114 // We distinguish relative paths from absolute paths by the
1115 // absence of '/' at the beginning of relative paths.
1116 //
1117 // FIXME: This is wrong. We distinguish them by asking if the path is
1118 // absolute, which isn't the same thing. And there might be multiple '/'s
1119 // in a row. Use a better mechanism to indicate whether we have emitted an
1120 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001121 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001122 }
Mike Stump11289f42009-09-09 15:08:12 +00001123
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001124 return Filename + Pos;
1125}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001126
Ben Langmuir487ea142014-10-23 18:05:36 +00001127static ASTFileSignature getSignature() {
1128 while (1) {
1129 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1130 return S;
1131 // Rely on GetRandomNumber to eventually return non-zero...
1132 }
1133}
1134
Douglas Gregor112b9072012-10-18 05:31:06 +00001135/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001136void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1137 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001138 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001139 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001140 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1141 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001142
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001143 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001144 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1145 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1146 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1147 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1148 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1149 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1150 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1151 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1152 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1153 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1154 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001155 Record.push_back(VERSION_MAJOR);
1156 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001157 Record.push_back(CLANG_VERSION_MAJOR);
1158 Record.push_back(CLANG_VERSION_MINOR);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001159 assert((!WritingModule || isysroot.empty()) &&
1160 "writing module as a relocatable PCH?");
Douglas Gregorc567ba22011-07-22 16:35:34 +00001161 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001162 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001163 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1164 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001165
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001166 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001167 // For implicit modules we output a signature that we can use to ensure
1168 // duplicate module builds don't collide in the cache as their output order
1169 // is non-deterministic.
1170 // FIXME: Remove this when output is deterministic.
1171 if (Context.getLangOpts().ImplicitModules) {
1172 Record.clear();
1173 Record.push_back(getSignature());
1174 Stream.EmitRecord(SIGNATURE, Record);
1175 }
1176
Richard Smith7ed1bc92014-12-05 22:42:13 +00001177 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001178 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1179 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1181 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1182 RecordData Record;
1183 Record.push_back(MODULE_NAME);
1184 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1185 }
1186
Richard Smith7ed1bc92014-12-05 22:42:13 +00001187 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001188 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001189 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001190
1191 // If the home of the module is the current working directory, then we
1192 // want to pick up the cwd of the build process loading the module, not
1193 // our cwd, when we load this module.
1194 if (!PP.getHeaderSearchInfo()
1195 .getHeaderSearchOpts()
1196 .ModuleMapFileHomeIsCwd ||
1197 WritingModule->Directory->getName() != StringRef(".")) {
1198 // Module directory.
1199 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1200 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1202 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1203
1204 RecordData Record;
1205 Record.push_back(MODULE_DIRECTORY);
1206 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1207 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001208
1209 // Write out all other paths relative to the base directory if possible.
1210 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1211 } else if (!isysroot.empty()) {
1212 // Write out paths relative to the sysroot if possible.
1213 BaseDirectory = isysroot;
1214 }
1215
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001216 // Module map file
1217 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001218 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001219
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001220 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1221
1222 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001223 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001224
1225 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001226 if (auto *AdditionalModMaps =
1227 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001228 Record.push_back(AdditionalModMaps->size());
1229 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001230 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001231 } else {
1232 Record.push_back(0);
1233 }
1234
1235 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001236 }
1237
Douglas Gregor112b9072012-10-18 05:31:06 +00001238 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001239 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001240 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001241 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001242
Richard Smith7f330cd2015-03-18 01:42:29 +00001243 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001244 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001245 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001246 continue;
1247
Richard Smith7f330cd2015-03-18 01:42:29 +00001248 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1249 AddSourceLocation(M->ImportLoc, Record);
1250 Record.push_back(M->File->getSize());
1251 Record.push_back(M->File->getModificationTime());
1252 Record.push_back(M->Signature);
1253 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001254 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001255 Stream.EmitRecord(IMPORTS, Record);
1256 }
Mike Stump11289f42009-09-09 15:08:12 +00001257
Douglas Gregor112b9072012-10-18 05:31:06 +00001258 // Language options.
1259 Record.clear();
1260 const LangOptions &LangOpts = Context.getLangOpts();
1261#define LANGOPT(Name, Bits, Default, Description) \
1262 Record.push_back(LangOpts.Name);
1263#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1264 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001265#include "clang/Basic/LangOptions.def"
1266#define SANITIZER(NAME, ID) \
1267 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001268#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001269
Ben Langmuircd98cb72015-06-23 18:20:18 +00001270 Record.push_back(LangOpts.ModuleFeatures.size());
1271 for (StringRef Feature : LangOpts.ModuleFeatures)
1272 AddString(Feature, Record);
1273
Douglas Gregor112b9072012-10-18 05:31:06 +00001274 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1275 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001276
1277 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001278
1279 // Comment options.
1280 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1281 for (CommentOptions::BlockCommandNamesTy::const_iterator
1282 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1283 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1284 I != IEnd; ++I) {
1285 AddString(*I, Record);
1286 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001287 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001288
Douglas Gregor112b9072012-10-18 05:31:06 +00001289 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1290
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001291 // Target options.
1292 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001293 const TargetInfo &Target = Context.getTargetInfo();
1294 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001295 AddString(TargetOpts.Triple, Record);
1296 AddString(TargetOpts.CPU, Record);
1297 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001298 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1299 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1300 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1301 }
1302 Record.push_back(TargetOpts.Features.size());
1303 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1304 AddString(TargetOpts.Features[I], Record);
1305 }
1306 Stream.EmitRecord(TARGET_OPTIONS, Record);
1307
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001308 // Diagnostic options.
1309 Record.clear();
1310 const DiagnosticOptions &DiagOpts
1311 = Context.getDiagnostics().getDiagnosticOptions();
1312#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1313#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1314 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1315#include "clang/Basic/DiagnosticOptions.def"
1316 Record.push_back(DiagOpts.Warnings.size());
1317 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1318 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001319 Record.push_back(DiagOpts.Remarks.size());
1320 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1321 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001322 // Note: we don't serialize the log or serialization file names, because they
1323 // are generally transient files and will almost always be overridden.
1324 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1325
Douglas Gregorc6317db2012-10-24 15:49:58 +00001326 // File system options.
1327 Record.clear();
1328 const FileSystemOptions &FSOpts
1329 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1330 AddString(FSOpts.WorkingDir, Record);
1331 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1332
Douglas Gregor2d302362012-10-24 16:50:34 +00001333 // Header search options.
1334 Record.clear();
1335 const HeaderSearchOptions &HSOpts
1336 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1337 AddString(HSOpts.Sysroot, Record);
1338
1339 // Include entries.
1340 Record.push_back(HSOpts.UserEntries.size());
1341 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1342 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1343 AddString(Entry.Path, Record);
1344 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001345 Record.push_back(Entry.IsFramework);
1346 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001347 }
1348
1349 // System header prefixes.
1350 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1351 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1352 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1353 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1354 }
1355
1356 AddString(HSOpts.ResourceDir, Record);
1357 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001358 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001359 Record.push_back(HSOpts.DisableModuleHash);
1360 Record.push_back(HSOpts.UseBuiltinIncludes);
1361 Record.push_back(HSOpts.UseStandardSystemIncludes);
1362 Record.push_back(HSOpts.UseStandardCXXIncludes);
1363 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001364 // Write out the specific module cache path that contains the module files.
1365 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001366 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1367
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001368 // Preprocessor options.
1369 Record.clear();
1370 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1371
1372 // Macro definitions.
1373 Record.push_back(PPOpts.Macros.size());
1374 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1375 AddString(PPOpts.Macros[I].first, Record);
1376 Record.push_back(PPOpts.Macros[I].second);
1377 }
1378
1379 // Includes
1380 Record.push_back(PPOpts.Includes.size());
1381 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1382 AddString(PPOpts.Includes[I], Record);
1383
1384 // Macro includes
1385 Record.push_back(PPOpts.MacroIncludes.size());
1386 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1387 AddString(PPOpts.MacroIncludes[I], Record);
1388
Douglas Gregorb6368752012-10-24 23:41:50 +00001389 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001390 // Detailed record is important since it is used for the module cache hash.
1391 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001392 AddString(PPOpts.ImplicitPCHInclude, Record);
1393 AddString(PPOpts.ImplicitPTHInclude, Record);
1394 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1395 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1396
Douglas Gregora3b20262011-05-06 21:43:30 +00001397 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001398 SourceManager &SM = Context.getSourceManager();
1399 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1400 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001401 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1402 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001403 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1404 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1405
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001406 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001407 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001408 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001409 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001410 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001411
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001412 Record.clear();
1413 Record.push_back(SM.getMainFileID().getOpaqueValue());
1414 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1415
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001416 // Original PCH directory
1417 if (!OutputFile.empty() && OutputFile != "-") {
1418 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1419 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1421 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1422
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001423 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001424
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001425 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001426 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1427
1428 RecordData Record;
1429 Record.push_back(ORIGINAL_PCH_DIR);
1430 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1431 }
1432
Douglas Gregor49491f72013-03-15 22:15:07 +00001433 WriteInputFiles(Context.SourceMgr,
1434 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001435 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001436 Stream.ExitBlock();
1437}
1438
Douglas Gregor49491f72013-03-15 22:15:07 +00001439namespace {
1440 /// \brief An input file.
1441 struct InputFileEntry {
1442 const FileEntry *File;
1443 bool IsSystemFile;
1444 bool BufferOverridden;
1445 };
1446}
1447
1448void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1449 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001450 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001451 using namespace llvm;
1452 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1453 RecordData Record;
1454
1455 // Create input-file abbreviation.
1456 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1457 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001458 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001459 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1460 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001461 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001462 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1463 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1464
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001465 // Get all ContentCache objects for files, sorted by whether the file is a
1466 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001467 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001468 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1469 // Get this source location entry.
1470 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001471 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001472
1473 // We only care about file entries that were not overridden.
1474 if (!SLoc->isFile())
1475 continue;
1476 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001477 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001478 continue;
1479
Douglas Gregor49491f72013-03-15 22:15:07 +00001480 InputFileEntry Entry;
1481 Entry.File = Cache->OrigEntry;
1482 Entry.IsSystemFile = Cache->IsSystemFile;
1483 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001484 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001485 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001486 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001487 SortedFiles.push_front(Entry);
1488 }
1489
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001490 unsigned UserFilesNum = 0;
1491 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001492 std::vector<uint64_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001493 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001494 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001495 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001496
Douglas Gregor49491f72013-03-15 22:15:07 +00001497 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001498 if (InputFileID != 0)
1499 continue; // already recorded this file.
1500
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001501 // Record this entry's offset.
1502 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001503
1504 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001505
Douglas Gregor49491f72013-03-15 22:15:07 +00001506 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001507 ++UserFilesNum;
1508
Douglas Gregor72be3902012-10-19 00:38:02 +00001509 Record.clear();
1510 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001511 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001512
1513 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001514 Record.push_back(Entry.File->getSize());
1515 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001516
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001517 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001518 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001519
Richard Smith7ed1bc92014-12-05 22:42:13 +00001520 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1521 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001522
Douglas Gregor112b9072012-10-18 05:31:06 +00001523 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001524
1525 // Create input file offsets abbreviation.
1526 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1527 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1528 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001529 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1530 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001531 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1532 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1533
1534 // Write input file offsets.
1535 Record.clear();
1536 Record.push_back(INPUT_FILE_OFFSETS);
1537 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001538 Record.push_back(UserFilesNum);
Reid Kleckner012665e2015-05-21 00:13:09 +00001539 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001540}
1541
Douglas Gregora7f71a92009-04-10 03:52:48 +00001542//===----------------------------------------------------------------------===//
1543// Source Manager Serialization
1544//===----------------------------------------------------------------------===//
1545
1546/// \brief Create an abbreviation for the SLocEntry that refers to a
1547/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001548static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001549 using namespace llvm;
1550 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001551 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001556 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001561 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001562}
1563
1564/// \brief Create an abbreviation for the SLocEntry that refers to a
1565/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001566static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001567 using namespace llvm;
1568 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001569 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001575 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001576}
1577
1578/// \brief Create an abbreviation for the SLocEntry that refers to a
1579/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001580static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001581 using namespace llvm;
1582 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001583 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001585 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001586}
1587
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001588/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1589/// expansion.
1590static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001591 using namespace llvm;
1592 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001593 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1595 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1596 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001599 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001600}
1601
Douglas Gregor09b69892011-02-10 17:09:37 +00001602namespace {
1603 // Trait used for the on-disk hash table of header search information.
1604 class HeaderFileInfoTrait {
1605 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001606 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001607
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001608 // Keep track of the framework names we've used during serialization.
1609 SmallVector<char, 128> FrameworkStringData;
1610 llvm::StringMap<unsigned> FrameworkNameOffset;
1611
Douglas Gregor09b69892011-02-10 17:09:37 +00001612 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001613 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1614 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001615
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001616 struct key_type {
1617 const FileEntry *FE;
1618 const char *Filename;
1619 };
1620 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001621
1622 typedef HeaderFileInfo data_type;
1623 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001624 typedef unsigned hash_value_type;
1625 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001626
Justin Bogner25463f12014-04-18 20:27:24 +00001627 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001628 // The hash is based only on size/time of the file, so that the reader can
1629 // match even when symlinking or excess path elements ("foo/../", "../")
1630 // change the form of the name. However, complete path is still the key.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001631 //
1632 // FIXME: Using the mtime here will cause problems for explicit module
1633 // imports.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001634 return llvm::hash_combine(key.FE->getSize(),
1635 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001636 }
1637
1638 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001639 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001640 using namespace llvm::support;
1641 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001642 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001643 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001644 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001645 if (Data.isModuleHeader)
1646 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001647 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001648 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001649 }
1650
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001651 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001652 using namespace llvm::support;
1653 endian::Writer<little> LE(Out);
1654 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001655 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001656 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001657 KeyLen -= 8;
1658 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001659 }
1660
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001661 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001662 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001663 using namespace llvm::support;
1664 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001665 uint64_t Start = Out.tell(); (void)Start;
1666
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001667 unsigned char Flags = (Data.HeaderRole << 6)
1668 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001669 | (Data.isPragmaOnce << 4)
1670 | (Data.DirInfo << 2)
1671 | (Data.Resolved << 1)
1672 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001673 LE.write<uint8_t>(Flags);
1674 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001675
1676 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001677 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001678 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001679 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001680
1681 unsigned Offset = 0;
1682 if (!Data.Framework.empty()) {
1683 // If this header refers into a framework, save the framework name.
1684 llvm::StringMap<unsigned>::iterator Pos
1685 = FrameworkNameOffset.find(Data.Framework);
1686 if (Pos == FrameworkNameOffset.end()) {
1687 Offset = FrameworkStringData.size() + 1;
1688 FrameworkStringData.append(Data.Framework.begin(),
1689 Data.Framework.end());
1690 FrameworkStringData.push_back(0);
1691
1692 FrameworkNameOffset[Data.Framework] = Offset;
1693 } else
1694 Offset = Pos->second;
1695 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001696 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001697
1698 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001699 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001700 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001701 }
1702
Douglas Gregor09b69892011-02-10 17:09:37 +00001703 assert(Out.tell() - Start == DataLen && "Wrong data length");
1704 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001705
1706 const char *strings_begin() const { return FrameworkStringData.begin(); }
1707 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001708 };
1709} // end anonymous namespace
1710
1711/// \brief Write the header search block for the list of files that
1712///
1713/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001714void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001715 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001716 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1717
1718 if (FilesByUID.size() > HS.header_file_size())
1719 FilesByUID.resize(HS.header_file_size());
1720
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001721 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001722 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001723 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001724 unsigned NumHeaderSearchEntries = 0;
1725 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1726 const FileEntry *File = FilesByUID[UID];
1727 if (!File)
1728 continue;
1729
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001730 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1731 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001732 HeaderFileInfo HFI;
1733 if (!HS.tryGetFileInfo(File, HFI) ||
1734 (HFI.External && Chain) ||
1735 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001736 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001737
Richard Smith7ed1bc92014-12-05 22:42:13 +00001738 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001739 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001740 SmallString<128> FilenameTmp(Filename);
1741 if (PreparePathForOutput(FilenameTmp)) {
1742 // If we performed any translation on the file name at all, we need to
1743 // save this string, since the generator will refer to it later.
1744 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001745 SavedStrings.push_back(Filename);
1746 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001747
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001748 HeaderFileInfoTrait::key_type key = { File, Filename };
1749 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001750 ++NumHeaderSearchEntries;
1751 }
1752
1753 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001754 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001755 uint32_t BucketOffset;
1756 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001757 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001758 llvm::raw_svector_ostream Out(TableData);
1759 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001760 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001761 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1762 }
1763
1764 // Create a blob abbreviation
1765 using namespace llvm;
1766 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1767 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1772 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1773
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001774 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001775 RecordData Record;
1776 Record.push_back(HEADER_SEARCH_TABLE);
1777 Record.push_back(BucketOffset);
1778 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001779 Record.push_back(TableData.size());
1780 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001781 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001782
1783 // Free all of the strings we had to duplicate.
1784 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001785 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001786}
1787
Douglas Gregora7f71a92009-04-10 03:52:48 +00001788/// \brief Writes the block containing the serialized form of the
1789/// source manager.
1790///
1791/// TODO: We should probably use an on-disk hash table (stored in a
1792/// blob), indexed based on the file name, so that we only create
1793/// entries for files that we actually need. In the common case (no
1794/// errors), we probably won't have to create file entries for any of
1795/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001796void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001797 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001798 RecordData Record;
1799
Chris Lattner0910e3b2009-04-10 17:16:57 +00001800 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001801 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001802
1803 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001804 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1805 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1806 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001807 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001808
Douglas Gregor258ae542009-04-27 06:38:32 +00001809 // Write out the source location entry table. We skip the first
1810 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001811 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001812 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001813 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1814 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001815 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001816 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001817 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001818 FileID FID = FileID::get(I);
1819 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001820
Douglas Gregor258ae542009-04-27 06:38:32 +00001821 // Record the offset of this source-location entry.
1822 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1823
1824 // Figure out which record code to use.
1825 unsigned Code;
1826 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001827 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1828 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001829 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001830 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001831 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001832 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001833 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001834 Record.clear();
1835 Record.push_back(Code);
1836
Douglas Gregor925296b2011-07-19 16:10:42 +00001837 // Starting offset of this entry within this module, so skip the dummy.
1838 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001839 if (SLoc->isFile()) {
1840 const SrcMgr::FileInfo &File = SLoc->getFile();
1841 Record.push_back(File.getIncludeLoc().getRawEncoding());
1842 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1843 Record.push_back(File.hasLineDirectives());
1844
1845 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001846 if (Content->OrigEntry) {
1847 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001848 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001849
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001850 // The source location entry is a file. Emit input file ID.
1851 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1852 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001853
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001854 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001855
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001856 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001857 if (FDI != FileDeclIDs.end()) {
1858 Record.push_back(FDI->second->FirstDeclIndex);
1859 Record.push_back(FDI->second->DeclIDs.size());
1860 } else {
1861 Record.push_back(0);
1862 Record.push_back(0);
1863 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001864
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001865 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001866
1867 if (Content->BufferOverridden) {
1868 Record.clear();
1869 Record.push_back(SM_SLOC_BUFFER_BLOB);
1870 const llvm::MemoryBuffer *Buffer
1871 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1872 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1873 StringRef(Buffer->getBufferStart(),
1874 Buffer->getBufferSize() + 1));
1875 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001876 } else {
1877 // The source location entry is a buffer. The blob associated
1878 // with this entry contains the contents of the buffer.
1879
1880 // We add one to the size so that we capture the trailing NULL
1881 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1882 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001883 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001884 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001885 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001886 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001887 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001888 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001889 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001890 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001891 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001892 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001893
Douglas Gregor925296b2011-07-19 16:10:42 +00001894 if (strcmp(Name, "<built-in>") == 0) {
1895 PreloadSLocs.push_back(SLocEntryOffsets.size());
1896 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001897 }
1898 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001899 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001900 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001901 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1902 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001903 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1904 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001905
1906 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001907 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001908 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001909 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001910 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001911 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001912 }
1913 }
1914
Douglas Gregor8f45df52009-04-16 22:23:12 +00001915 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001916
1917 if (SLocEntryOffsets.empty())
1918 return;
1919
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001920 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001921 // table is used for lazily loading source-location information.
1922 using namespace llvm;
1923 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001924 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1928 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001929
Douglas Gregor258ae542009-04-27 06:38:32 +00001930 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001931 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001932 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001933 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Reid Kleckner012665e2015-05-21 00:13:09 +00001934 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, bytes(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001935
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001936 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001937 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001938 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001939
1940 // Write the line table. It depends on remapping working, so it must come
1941 // after the source location offsets.
1942 if (SourceMgr.hasLineTable()) {
1943 LineTableInfo &LineTable = SourceMgr.getLineTable();
1944
1945 Record.clear();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001946 // Emit the file names.
Douglas Gregor925296b2011-07-19 16:10:42 +00001947 Record.push_back(LineTable.getNumFilenames());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001948 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I)
1949 AddPath(LineTable.getFilename(I), Record);
Douglas Gregor925296b2011-07-19 16:10:42 +00001950
1951 // Emit the line entries
1952 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1953 L != LEnd; ++L) {
1954 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001955 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001956 continue;
1957
1958 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001959 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001960
1961 // Emit the line entries
1962 Record.push_back(L->second.size());
1963 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1964 LEEnd = L->second.end();
1965 LE != LEEnd; ++LE) {
1966 Record.push_back(LE->FileOffset);
1967 Record.push_back(LE->LineNo);
1968 Record.push_back(LE->FilenameID);
1969 Record.push_back((unsigned)LE->FileKind);
1970 Record.push_back(LE->IncludeOffset);
1971 }
1972 }
1973 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1974 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001975}
1976
Douglas Gregorc5046832009-04-27 18:38:38 +00001977//===----------------------------------------------------------------------===//
1978// Preprocessor Serialization
1979//===----------------------------------------------------------------------===//
1980
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001981static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1982 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001983 if (MacroInfo *MI = MD->getMacroInfo())
1984 if (MI->isBuiltinMacro())
1985 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001986
1987 if (IsModule) {
1988 SourceLocation Loc = MD->getLocation();
1989 if (Loc.isInvalid())
1990 return true;
1991 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1992 return true;
1993 }
1994
1995 return false;
1996}
1997
Chris Lattnereeffaef2009-04-10 17:15:23 +00001998/// \brief Writes the block containing the serialized form of the
1999/// preprocessor.
2000///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002001void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002002 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2003 if (PPRec)
2004 WritePreprocessorDetail(*PPRec);
2005
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002006 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002007 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002008
Chris Lattner0af3ba12009-04-13 01:29:17 +00002009 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2010 if (PP.getCounterValue() != 0) {
2011 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002012 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002013 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002014 }
2015
2016 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002017 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002018
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002019 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002020 // FIXME: use diagnostics subsystem for localization etc.
2021 if (PP.SawDateOrTime())
2022 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00002023
Douglas Gregor796d76a2010-10-20 22:00:55 +00002024
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002025 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002026 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002027
Richard Smithee977932015-05-01 21:22:17 +00002028 // Construct the list of identifiers with macro directives that need to be
2029 // serialized.
2030 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2031 for (auto &Id : PP.getIdentifierTable())
2032 if (Id.second->hadMacroDefinition() &&
2033 (!Id.second->isFromAST() ||
2034 Id.second->hasChangedSinceDeserialization()))
2035 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002036 // Sort the set of macro definitions that need to be serialized by the
2037 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002038 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2039 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002040
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002041 // Emit the macro directives as a list and associate the offset with the
2042 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002043 for (const IdentifierInfo *Name : MacroIdentifiers) {
2044 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002045 auto StartOffset = Stream.GetCurrentBitNo();
2046
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002047 // Emit the macro directives in reverse source order.
2048 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002049 // Once we hit an ignored macro, we're done: the rest of the chain
2050 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002051 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002052 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002053
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002054 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002055 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002056 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002057 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002058 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002059 Record.push_back(VisMD->isPublic());
2060 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002061 }
Richard Smithd7329392015-04-21 21:46:32 +00002062
Richard Smithb8b2ed62015-04-23 18:18:26 +00002063 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002064 bool EmittedModuleMacros = false;
Richard Smithb8b2ed62015-04-23 18:18:26 +00002065 if (IsModule) {
2066 auto Leafs = PP.getLeafModuleMacros(Name);
2067 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2068 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2069 while (!Worklist.empty()) {
2070 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002071
Richard Smithb8b2ed62015-04-23 18:18:26 +00002072 // Emit a record indicating this submodule exports this macro.
2073 ModuleMacroRecord.push_back(
2074 getSubmoduleID(Macro->getOwningModule()));
Richard Smithee977932015-05-01 21:22:17 +00002075 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
Richard Smithb8b2ed62015-04-23 18:18:26 +00002076 for (auto *M : Macro->overrides())
2077 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002078
Richard Smithb8b2ed62015-04-23 18:18:26 +00002079 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2080 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002081
Richard Smithb8b2ed62015-04-23 18:18:26 +00002082 // Enqueue overridden macros once we've visited all their ancestors.
2083 for (auto *M : Macro->overrides())
2084 if (++Visits[M] == M->getNumOverridingMacros())
2085 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002086
2087 EmittedModuleMacros = true;
Richard Smithd7329392015-04-21 21:46:32 +00002088 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002089 }
Richard Smithd7329392015-04-21 21:46:32 +00002090
Richard Smithee977932015-05-01 21:22:17 +00002091 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002092 continue;
2093
Richard Smithd7329392015-04-21 21:46:32 +00002094 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002095 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2096 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002097 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002098
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002099 /// \brief Offsets of each of the macros into the bitstream, indexed by
2100 /// the local macro ID
2101 ///
2102 /// For each identifier that is associated with a macro, this map
2103 /// provides the offset into the bitstream where that macro is
2104 /// defined.
2105 std::vector<uint32_t> MacroOffsets;
2106
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002107 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2108 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2109 MacroInfo *MI = MacroInfosToEmit[I].MI;
2110 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002111
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002112 if (ID < FirstMacroID) {
2113 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2114 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002115 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002116
2117 // Record the local offset of this macro.
2118 unsigned Index = ID - FirstMacroID;
2119 if (Index == MacroOffsets.size())
2120 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2121 else {
2122 if (Index > MacroOffsets.size())
2123 MacroOffsets.resize(Index + 1);
2124
2125 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2126 }
2127
2128 AddIdentifierRef(Name, Record);
2129 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2130 AddSourceLocation(MI->getDefinitionLoc(), Record);
2131 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2132 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002133 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002134 unsigned Code;
2135 if (MI->isObjectLike()) {
2136 Code = PP_MACRO_OBJECT_LIKE;
2137 } else {
2138 Code = PP_MACRO_FUNCTION_LIKE;
2139
2140 Record.push_back(MI->isC99Varargs());
2141 Record.push_back(MI->isGNUVarargs());
2142 Record.push_back(MI->hasCommaPasting());
2143 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002144 for (const IdentifierInfo *Arg : MI->args())
2145 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002146 }
2147
2148 // If we have a detailed preprocessing record, record the macro definition
2149 // ID that corresponds to this macro.
2150 if (PPRec)
2151 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2152
2153 Stream.EmitRecord(Code, Record);
2154 Record.clear();
2155
2156 // Emit the tokens array.
2157 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2158 // Note that we know that the preprocessor does not have any annotation
2159 // tokens in it because they are created by the parser, and thus can't
2160 // be in a macro definition.
2161 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002162 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002163 Stream.EmitRecord(PP_TOKEN, Record);
2164 Record.clear();
2165 }
2166 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002167 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002168
Douglas Gregor92a96f52011-02-08 21:58:10 +00002169 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002170
2171 // Write the offsets table for macro IDs.
2172 using namespace llvm;
Richard Smith13090a22015-04-10 02:02:24 +00002173 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002174 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2178
2179 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2180 Record.clear();
2181 Record.push_back(MACRO_OFFSET);
2182 Record.push_back(MacroOffsets.size());
2183 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2184 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002185 bytes(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002186}
2187
2188void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002189 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002190 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002191
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002192 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002193
Douglas Gregor92a96f52011-02-08 21:58:10 +00002194 // Enter the preprocessor block.
2195 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002196
Douglas Gregoraae92242010-03-19 21:51:54 +00002197 // If the preprocessor has a preprocessing record, emit it.
2198 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002199 using namespace llvm;
2200
2201 // Set up the abbreviation for
2202 unsigned InclusionAbbrev = 0;
2203 {
2204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2205 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2211 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2212 }
2213
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002214 unsigned FirstPreprocessorEntityID
2215 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2216 + NUM_PREDEF_PP_ENTITY_IDS;
2217 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002218 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002219 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2220 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002221 E != EEnd;
2222 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002223 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002224
Richard Smith66a81862015-05-04 02:25:31 +00002225 PreprocessedEntityOffsets.push_back(
2226 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002227
Richard Smith66a81862015-05-04 02:25:31 +00002228 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002229 // Record this macro definition's ID.
2230 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002231
Douglas Gregor92a96f52011-02-08 21:58:10 +00002232 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002233 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2234 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002235 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002236
Chandler Carrutha88a22182011-07-14 08:20:46 +00002237 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002238 Record.push_back(ME->isBuiltinMacro());
2239 if (ME->isBuiltinMacro())
2240 AddIdentifierRef(ME->getName(), Record);
2241 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002242 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002243 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002244 continue;
2245 }
2246
2247 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2248 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002249 Record.push_back(ID->getFileName().size());
2250 Record.push_back(ID->wasInQuotes());
2251 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002252 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002253 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002254 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002255 // Check that the FileEntry is not null because it was not resolved and
2256 // we create a PCH even with compiler errors.
2257 if (ID->getFile())
2258 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002259 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2260 continue;
2261 }
2262
2263 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2264 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002265 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002266
Douglas Gregoraae92242010-03-19 21:51:54 +00002267 // Write the offsets table for the preprocessing record.
2268 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002269 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2270
Douglas Gregoraae92242010-03-19 21:51:54 +00002271 // Write the offsets table for identifier IDs.
2272 using namespace llvm;
2273 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002274 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002277 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002278
Douglas Gregoraae92242010-03-19 21:51:54 +00002279 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002280 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002281 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002282 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002283 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002284 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002285}
2286
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002287unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2288 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2289 if (Known != SubmoduleIDs.end())
2290 return Known->second;
2291
2292 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2293}
2294
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002295unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2296 if (!Mod)
2297 return 0;
2298
2299 llvm::DenseMap<Module *, unsigned>::const_iterator
2300 Known = SubmoduleIDs.find(Mod);
2301 if (Known != SubmoduleIDs.end())
2302 return Known->second;
2303
2304 return 0;
2305}
2306
Douglas Gregor253eefe2011-12-01 00:59:36 +00002307/// \brief Compute the number of modules within the given tree (including the
2308/// given module).
2309static unsigned getNumberOfModules(Module *Mod) {
2310 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002311 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2312 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002313 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002314 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002315
2316 return ChildModules + 1;
2317}
2318
Douglas Gregorde3ef502011-11-30 23:21:26 +00002319void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002320 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002321 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002322
2323 // Write the abbreviations needed for the submodules block.
2324 using namespace llvm;
2325 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2326 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2338 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2339
2340 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002341 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2343 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2344
2345 Abbrev = new BitCodeAbbrev();
2346 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2348 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002349
2350 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002351 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2353 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2354
2355 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002356 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2358 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2359
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002360 Abbrev = new BitCodeAbbrev();
2361 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002364 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2365
Douglas Gregor59527662012-10-15 06:28:11 +00002366 Abbrev = new BitCodeAbbrev();
2367 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2369 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2370
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002371 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002372 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2374 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2375
2376 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002377 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2379 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2380
2381 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002382 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2384 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2385
2386 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002387 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2390 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2391
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002392 Abbrev = new BitCodeAbbrev();
2393 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2395 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2396
Douglas Gregorfb912652013-03-20 21:10:35 +00002397 Abbrev = new BitCodeAbbrev();
2398 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2401 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2402
Douglas Gregor253eefe2011-12-01 00:59:36 +00002403 // Write the submodule metadata block.
2404 RecordData Record;
2405 Record.push_back(getNumberOfModules(WritingModule));
2406 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2407 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2408
Douglas Gregor69021972011-11-30 17:33:56 +00002409 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002410 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002411 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002412 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002413 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002414 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002415 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002416
2417 // Emit the definition of the block.
2418 Record.clear();
2419 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002420 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002421 if (Mod->Parent) {
2422 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2423 Record.push_back(SubmoduleIDs[Mod->Parent]);
2424 } else {
2425 Record.push_back(0);
2426 }
2427 Record.push_back(Mod->IsFramework);
2428 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002429 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002430 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002431 Record.push_back(Mod->InferSubmodules);
2432 Record.push_back(Mod->InferExplicitSubmodules);
2433 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002434 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002435 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2436
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002437 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002438 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002439 Record.clear();
2440 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002441 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002442 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002443 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002444 }
2445
Douglas Gregor69021972011-11-30 17:33:56 +00002446 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002447 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002448 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002449 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Richard Smith2b63d152015-05-16 02:28:53 +00002450 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2451 UmbrellaHeader.NameAsWritten);
2452 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Douglas Gregor524e33e2011-12-08 19:11:24 +00002453 Record.clear();
2454 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2455 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002456 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002457 }
Richard Smith306d8922014-10-22 23:50:56 +00002458
Douglas Gregor69021972011-11-30 17:33:56 +00002459 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002460 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002461 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002462 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002463 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002464 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002465 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2466 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2467 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002468 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002469 Module::HK_PrivateTextual},
2470 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002471 };
2472 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002473 Record.clear();
Richard Smith3c1a41a2014-12-02 00:08:08 +00002474 Record.push_back(HL.RecordKind);
2475 for (auto &H : Mod->Headers[HL.HeaderKind])
2476 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2477 }
2478
2479 // Emit the top headers.
2480 {
2481 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2482 Record.clear();
2483 Record.push_back(SUBMODULE_TOPHEADER);
2484 for (auto *H : TopHeaders)
2485 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002486 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002487
2488 // Emit the imports.
2489 if (!Mod->Imports.empty()) {
2490 Record.clear();
2491 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002492 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00002493 assert(ImportedID && "Unknown submodule!");
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002494 Record.push_back(ImportedID);
2495 }
2496 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2497 }
2498
Douglas Gregor24bb9232011-12-02 18:58:38 +00002499 // Emit the exports.
2500 if (!Mod->Exports.empty()) {
2501 Record.clear();
2502 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002503 if (Module *Exported = Mod->Exports[I].getPointer()) {
Richard Smith23d8d032015-05-14 00:45:20 +00002504 unsigned ExportedID = getSubmoduleID(Exported);
Douglas Gregor18b58642011-12-12 23:17:57 +00002505 Record.push_back(ExportedID);
2506 } else {
2507 Record.push_back(0);
2508 }
2509
Douglas Gregor24bb9232011-12-02 18:58:38 +00002510 Record.push_back(Mod->Exports[I].getInt());
2511 }
2512 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2513 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002514
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002515 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2516 // Might be unnecessary as use declarations are only used to build the
2517 // module itself.
2518
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002519 // Emit the link libraries.
2520 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2521 Record.clear();
2522 Record.push_back(SUBMODULE_LINK_LIBRARY);
2523 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2524 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2525 Mod->LinkLibraries[I].Library);
2526 }
2527
Douglas Gregorfb912652013-03-20 21:10:35 +00002528 // Emit the conflicts.
2529 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2530 Record.clear();
2531 Record.push_back(SUBMODULE_CONFLICT);
2532 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2533 assert(OtherID && "Unknown submodule!");
2534 Record.push_back(OtherID);
2535 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2536 Mod->Conflicts[I].Message);
2537 }
2538
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002539 // Emit the configuration macros.
2540 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2541 Record.clear();
2542 Record.push_back(SUBMODULE_CONFIG_MACRO);
2543 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2544 Mod->ConfigMacros[I]);
2545 }
2546
Douglas Gregor69021972011-11-30 17:33:56 +00002547 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002548 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2549 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002550 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002551 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002552 }
2553
2554 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002555
2556 // FIXME: This can easily happen, if we have a reference to a submodule that
2557 // did not result in us loading a module file for that submodule. For
2558 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2559 assert((NextSubmoduleID - FirstSubmoduleID ==
2560 getNumberOfModules(WritingModule)) &&
2561 "Wrong # of submodules; found a reference to a non-local, "
2562 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002563}
2564
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002565serialization::SubmoduleID
2566ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002567 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002568 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002569
2570 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002571 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002572 Module *OwningMod
2573 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002574 if (!OwningMod)
2575 return 0;
2576
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002577 // Check whether this submodule is part of our own module.
2578 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002579 return 0;
2580
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002581 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002582}
2583
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002584void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2585 bool isModule) {
2586 // Make sure set diagnostic pragmas don't affect the translation unit that
2587 // imports the module.
2588 // FIXME: Make diagnostic pragma sections work properly with modules.
2589 if (isModule)
2590 return;
2591
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002592 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2593 DiagStateIDMap;
2594 unsigned CurrID = 0;
2595 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002596 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002597 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002598 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2599 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002600 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002601 if (point.Loc.isInvalid())
2602 continue;
2603
2604 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002605 unsigned &DiagStateID = DiagStateIDMap[point.State];
2606 Record.push_back(DiagStateID);
2607
2608 if (DiagStateID == 0) {
2609 DiagStateID = ++CurrID;
2610 for (DiagnosticsEngine::DiagState::const_iterator
2611 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2612 if (I->second.isPragma()) {
2613 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002614 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002615 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002616 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002617 Record.push_back(-1); // mark the end of the diag/map pairs for this
2618 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002619 }
2620 }
2621
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002622 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002623 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002624}
2625
Richard Smithc2bb8182015-03-24 06:36:48 +00002626void ASTWriter::WriteCXXCtorInitializersOffsets() {
2627 if (CXXCtorInitializersOffsets.empty())
2628 return;
2629
2630 RecordData Record;
2631
2632 // Create a blob abbreviation for the C++ ctor initializer offsets.
2633 using namespace llvm;
2634
2635 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2636 Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2639 unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2640
2641 // Write the base specifier offsets table.
2642 Record.clear();
2643 Record.push_back(CXX_CTOR_INITIALIZERS_OFFSETS);
2644 Record.push_back(CXXCtorInitializersOffsets.size());
2645 Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002646 bytes(CXXCtorInitializersOffsets));
Richard Smithc2bb8182015-03-24 06:36:48 +00002647}
2648
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002649void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2650 if (CXXBaseSpecifiersOffsets.empty())
2651 return;
2652
2653 RecordData Record;
2654
2655 // Create a blob abbreviation for the C++ base specifiers offsets.
2656 using namespace llvm;
2657
2658 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2659 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2660 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2661 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2662 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2663
Douglas Gregorc27b2872011-08-04 00:01:48 +00002664 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002665 Record.clear();
2666 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2667 Record.push_back(CXXBaseSpecifiersOffsets.size());
2668 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002669 bytes(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002670}
2671
Douglas Gregorc5046832009-04-27 18:38:38 +00002672//===----------------------------------------------------------------------===//
2673// Type Serialization
2674//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002675
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002676/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002677void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002678 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002679 if (Idx.getIndex() == 0) // we haven't seen this type before.
2680 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002681
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002682 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002683
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002684 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002685 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002686 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002687 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002688 else if (TypeOffsets.size() < Index) {
2689 TypeOffsets.resize(Index + 1);
2690 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002691 }
2692
2693 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002694
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002695 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002696 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002697 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002698
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002699 if (T.hasLocalNonFastQualifiers()) {
2700 Qualifiers Qs = T.getLocalQualifiers();
2701 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002702 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002703 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002704 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002705 } else {
2706 switch (T->getTypeClass()) {
2707 // For all of the concrete, non-dependent types, call the
2708 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002709#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002710 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002711#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002712#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002713 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002714 }
2715
2716 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002717 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002718
2719 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002720 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002721}
2722
Douglas Gregorc5046832009-04-27 18:38:38 +00002723//===----------------------------------------------------------------------===//
2724// Declaration Serialization
2725//===----------------------------------------------------------------------===//
2726
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727/// \brief Write the block containing all of the declaration IDs
2728/// lexically declared within the given DeclContext.
2729///
2730/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2731/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002732uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002733 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002734 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002735 return 0;
2736
Douglas Gregor8f45df52009-04-16 22:23:12 +00002737 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002738 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002739 Record.push_back(DECL_CONTEXT_LEXICAL);
Richard Smith82f8fcd2015-08-06 22:07:25 +00002740 SmallVector<uint32_t, 128> KindDeclPairs;
2741 for (const auto *D : DC->decls()) {
2742 KindDeclPairs.push_back(D->getKind());
2743 KindDeclPairs.push_back(GetDeclRef(D));
2744 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002745
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002746 ++NumLexicalDeclContexts;
Richard Smith82f8fcd2015-08-06 22:07:25 +00002747 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2748 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002749 return Offset;
2750}
2751
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002752void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002753 using namespace llvm;
2754 RecordData Record;
2755
2756 // Write the type offsets array
2757 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002758 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2762 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2763 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002764 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002765 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002766 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002767 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002768
2769 // Write the declaration offsets array
2770 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002771 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2775 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2776 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002778 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002779 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002780 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002781}
2782
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002783void ASTWriter::WriteFileDeclIDsMap() {
2784 using namespace llvm;
2785 RecordData Record;
2786
Chandler Carruthb306d732015-03-27 00:31:20 +00002787 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2788 FileDeclIDs.begin(), FileDeclIDs.end());
2789 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2790 llvm::less_first());
2791
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002792 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002793 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2794 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2795 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2796 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2797 for (auto &LocDeclEntry : Info.DeclIDs)
2798 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002799 }
2800
2801 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2802 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002803 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2805 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2806 Record.push_back(FILE_SORTED_DECLS);
Chandler Carruthb306d732015-03-27 00:31:20 +00002807 Record.push_back(FileGroupedDeclIDs.size());
Reid Kleckner012665e2015-05-21 00:13:09 +00002808 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002809}
2810
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002811void ASTWriter::WriteComments() {
2812 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002813 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002814 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002815 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2816 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002817 I != E; ++I) {
2818 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002819 AddSourceRange((*I)->getSourceRange(), Record);
2820 Record.push_back((*I)->getKind());
2821 Record.push_back((*I)->isTrailingComment());
2822 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002823 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2824 }
2825 Stream.ExitBlock();
2826}
2827
Douglas Gregorc5046832009-04-27 18:38:38 +00002828//===----------------------------------------------------------------------===//
2829// Global Method Pool and Selector Serialization
2830//===----------------------------------------------------------------------===//
2831
Douglas Gregore84a9da2009-04-20 20:36:09 +00002832namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002833// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002834class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002835 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002836
2837public:
2838 typedef Selector key_type;
2839 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002840
Sebastian Redl834bb972010-08-04 17:20:04 +00002841 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002842 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002843 ObjCMethodList Instance, Factory;
2844 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002845 typedef const data_type& data_type_ref;
2846
Justin Bogner25463f12014-04-18 20:27:24 +00002847 typedef unsigned hash_value_type;
2848 typedef unsigned offset_type;
2849
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002850 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002851
Justin Bogner25463f12014-04-18 20:27:24 +00002852 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002853 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002854 }
Mike Stump11289f42009-09-09 15:08:12 +00002855
2856 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002857 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002858 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002859 using namespace llvm::support;
2860 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002861 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002862 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002863 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2864 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002865 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002866 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002867 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002868 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002869 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002870 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002871 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002872 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002873 return std::make_pair(KeyLen, DataLen);
2874 }
Mike Stump11289f42009-09-09 15:08:12 +00002875
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002876 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002877 using namespace llvm::support;
2878 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002879 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002880 assert((Start >> 32) == 0 && "Selector key offset too large");
2881 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002882 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002883 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002884 if (N == 0)
2885 N = 1;
2886 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002887 LE.write<uint32_t>(
2888 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002889 }
Mike Stump11289f42009-09-09 15:08:12 +00002890
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002891 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002892 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002893 using namespace llvm::support;
2894 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002895 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002896 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002897 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002898 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002899 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002900 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002901 ++NumInstanceMethods;
2902
2903 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002904 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002905 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002906 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002907 ++NumFactoryMethods;
2908
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002909 unsigned InstanceBits = Methods.Instance.getBits();
2910 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002911 unsigned InstanceHasMoreThanOneDeclBit =
2912 Methods.Instance.hasMoreThanOneDecl();
2913 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2914 (InstanceHasMoreThanOneDeclBit << 2) |
2915 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002916 unsigned FactoryBits = Methods.Factory.getBits();
2917 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002918 unsigned FactoryHasMoreThanOneDeclBit =
2919 Methods.Factory.hasMoreThanOneDecl();
2920 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2921 (FactoryHasMoreThanOneDeclBit << 2) |
2922 FactoryBits;
2923 LE.write<uint16_t>(FullInstanceBits);
2924 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002925 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002926 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002927 if (Method->getMethod())
2928 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002929 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002930 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002931 if (Method->getMethod())
2932 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002933
2934 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002935 }
2936};
2937} // end anonymous namespace
2938
Sebastian Redla19a67f2010-08-03 21:58:15 +00002939/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002940///
2941/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002942/// in an on-disk hash table indexed by the selector. The hash table also
2943/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002944void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002945 using namespace llvm;
2946
Sebastian Redla19a67f2010-08-03 21:58:15 +00002947 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002948 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002949 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002950 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002951 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002952 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002953 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002954 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002955
Sebastian Redla19a67f2010-08-03 21:58:15 +00002956 // Create the on-disk hash table representation. We walk through every
2957 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002958 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002959 for (auto &SelectorAndID : SelectorIDs) {
2960 Selector S = SelectorAndID.first;
2961 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002962 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002963 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002964 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00002965 ObjCMethodList(),
2966 ObjCMethodList()
2967 };
2968 if (F != SemaRef.MethodPool.end()) {
2969 Data.Instance = F->second.first;
2970 Data.Factory = F->second.second;
2971 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002972 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002973 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002974 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002975 // Selector already exists. Did it change?
2976 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002977 for (ObjCMethodList *M = &Data.Instance;
2978 !changed && M && M->getMethod(); M = M->getNext()) {
2979 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002980 changed = true;
2981 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002982 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002983 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00002984 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002985 changed = true;
2986 }
2987 if (!changed)
2988 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002989 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002990 // A new method pool entry.
2991 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002992 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002993 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002994 }
2995
Douglas Gregorc78d3462009-04-24 21:10:55 +00002996 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002997 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002998 uint32_t BucketOffset;
2999 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003000 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003001 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003002 llvm::raw_svector_ostream Out(MethodPool);
3003 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003004 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003005 BucketOffset = Generator.Emit(Out, Trait);
3006 }
3007
3008 // Create a blob abbreviation
3009 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003010 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3014 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3015
Douglas Gregor95c13f52009-04-25 17:48:32 +00003016 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003017 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003018 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003019 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003020 Record.push_back(NumTableEntries);
Yaron Keren92e1b622015-03-18 10:17:07 +00003021 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003022
3023 // Create a blob abbreviation for the selector table offsets.
3024 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003025 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3029 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3030
3031 // Write the selector offsets table.
3032 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003033 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003034 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003035 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003036 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003037 bytes(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003038 }
3039}
3040
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003041/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003042void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003043 using namespace llvm;
3044 if (SemaRef.ReferencedSelectors.empty())
3045 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003046
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003047 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003048
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003049 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003050 // very tricky to fix, and given that @selector shouldn't really appear in
3051 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003052 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3053 Selector Sel = SelectorAndLocation.first;
3054 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003055 AddSelectorRef(Sel, Record);
3056 AddSourceLocation(Loc, Record);
3057 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003058 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003059}
3060
Douglas Gregorc5046832009-04-27 18:38:38 +00003061//===----------------------------------------------------------------------===//
3062// Identifier Table Serialization
3063//===----------------------------------------------------------------------===//
3064
Richard Smithb3761912015-02-05 23:08:52 +00003065/// Determine the declaration that should be put into the name lookup table to
3066/// represent the given declaration in this module. This is usually D itself,
3067/// but if D was imported and merged into a local declaration, we want the most
3068/// recent local declaration instead. The chosen declaration will be the most
3069/// recent declaration in any module that imports this one.
3070static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3071 NamedDecl *D) {
3072 if (!LangOpts.Modules || !D->isFromASTFile())
3073 return D;
3074
3075 if (Decl *Redecl = D->getPreviousDecl()) {
3076 // For Redeclarable decls, a prior declaration might be local.
3077 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3078 if (!Redecl->isFromASTFile())
3079 return cast<NamedDecl>(Redecl);
Richard Smithfe620d22015-03-05 23:24:12 +00003080 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003081 // local one.
3082 if (D->getOwningModuleID() == 0)
3083 break;
3084 }
3085 } else if (Decl *First = D->getCanonicalDecl()) {
3086 // For Mergeable decls, the first decl might be local.
3087 if (!First->isFromASTFile())
3088 return cast<NamedDecl>(First);
3089 }
3090
3091 // All declarations are imported. Our most recent declaration will also be
3092 // the most recent one in anyone who imports us.
3093 return D;
3094}
3095
Douglas Gregorc78d3462009-04-24 21:10:55 +00003096namespace {
Richard Smithcf97cf62015-04-19 01:34:23 +00003097class ASTIdentifierTableTrait {
3098 ASTWriter &Writer;
3099 Preprocessor &PP;
3100 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003101 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003102 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003103 ASTWriter::RecordData *InterestingIdentifierOffsets;
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 Smith9c254182015-07-19 21:41:12 +00003109 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003110 if (MacroOffset ||
3111 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003112 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003113 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003114 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003115 return true;
3116
3117 return false;
3118 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003119
Douglas Gregore84a9da2009-04-20 20:36:09 +00003120public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003121 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003122 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003123
Sebastian Redl539c5062010-08-18 23:57:32 +00003124 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003125 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003126
Justin Bogner25463f12014-04-18 20:27:24 +00003127 typedef unsigned hash_value_type;
3128 typedef unsigned offset_type;
3129
Richard Smithd7329392015-04-21 21:46:32 +00003130 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003131 IdentifierResolver &IdResolver, bool IsModule,
3132 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003133 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003134 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3135 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003136
Justin Bogner25463f12014-04-18 20:27:24 +00003137 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003138 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003139 }
Mike Stump11289f42009-09-09 15:08:12 +00003140
Richard Smith33e0f7e2015-07-22 02:08:40 +00003141 bool isInterestingIdentifier(const IdentifierInfo *II) {
3142 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3143 return isInterestingIdentifier(II, MacroOffset);
3144 }
Richard Smith9c254182015-07-19 21:41:12 +00003145 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3146 return isInterestingIdentifier(II, 0);
3147 }
3148
Mike Stump11289f42009-09-09 15:08:12 +00003149 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003150 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003151 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003152 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003153 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3154 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003155 DataLen += 2; // 2 bytes for builtin ID
3156 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003157 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003158 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003159
Richard Smitha534a312015-07-21 23:54:07 +00003160 if (NeedDecls) {
3161 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3162 DEnd = IdResolver.end();
3163 D != DEnd; ++D)
3164 DataLen += 4;
3165 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003166 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003167 using namespace llvm::support;
3168 endian::Writer<little> LE(Out);
3169
Richard Smithdf8a8312015-03-13 04:05:01 +00003170 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003171 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003172 // We emit the key length after the data length so that every
3173 // string is preceded by a 16-bit length. This matches the PTH
3174 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003175 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003176 return std::make_pair(KeyLen, DataLen);
3177 }
Mike Stump11289f42009-09-09 15:08:12 +00003178
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003179 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003180 unsigned KeyLen) {
3181 // Record the location of the key data. This is used when generating
3182 // the mapping from persistent IDs to strings.
3183 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003184
3185 // Emit the offset of the key/data length information to the interesting
3186 // identifiers table if necessary.
3187 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3188 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3189
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003190 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003191 }
Mike Stump11289f42009-09-09 15:08:12 +00003192
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003193 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003194 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003195 using namespace llvm::support;
3196 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003197
Richard Smithd7329392015-04-21 21:46:32 +00003198 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3199 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003200 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003201 return;
3202 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003203
Justin Bognere1c147c2014-03-28 22:03:19 +00003204 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003205 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3206 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003207 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003208 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003209 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003210 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003211 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3212 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003213 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003214 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003215 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003216 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003217
Richard Smithd7329392015-04-21 21:46:32 +00003218 if (HadMacroDefinition)
3219 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003220
Richard Smitha534a312015-07-21 23:54:07 +00003221 if (NeedDecls) {
3222 // Emit the declaration IDs in reverse order, because the
3223 // IdentifierResolver provides the declarations as they would be
3224 // visible (e.g., the function "stat" would come before the struct
3225 // "stat"), but the ASTReader adds declarations to the end of the list
3226 // (so we need to see the struct "stat" before the function "stat").
3227 // Only emit declarations that aren't from a chained PCH, though.
3228 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3229 IdResolver.end());
3230 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3231 DEnd = Decls.rend();
3232 D != DEnd; ++D)
3233 LE.write<uint32_t>(
3234 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3235 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003236 }
3237};
3238} // end anonymous namespace
3239
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003240/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003241///
3242/// The identifier table consists of a blob containing string data
3243/// (the actual identifiers themselves) and a separate "offsets" index
3244/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003245void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3246 IdentifierResolver &IdResolver,
3247 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003248 using namespace llvm;
3249
Richard Smith33e0f7e2015-07-22 02:08:40 +00003250 RecordData InterestingIdents;
3251
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003252 // Create and write out the blob that contains the identifier
3253 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003254 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003255 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003256 ASTIdentifierTableTrait Trait(
3257 *this, PP, IdResolver, IsModule,
3258 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003259
Douglas Gregore6648fb2009-04-28 20:33:11 +00003260 // Look for any identifiers that were named while processing the
3261 // headers, but are otherwise not needed. We add these to the hash
3262 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003263 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003264 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003265 SmallVector<const IdentifierInfo *, 128> IIs;
Douglas Gregore6648fb2009-04-28 20:33:11 +00003266 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3267 IDEnd = PP.getIdentifierTable().end();
3268 ID != IDEnd; ++ID)
Chandler Carruth8440c982015-03-26 23:54:15 +00003269 IIs.push_back(ID->second);
3270 // Sort the identifiers lexicographically before getting them references so
3271 // that their order is stable.
3272 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3273 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003274 if (Trait.isInterestingNonMacroIdentifier(II))
3275 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003276
Sebastian Redlff4a2952010-07-23 23:49:55 +00003277 // Create the on-disk hash table representation. We only store offsets
3278 // for identifiers that appear here for the first time.
3279 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003280 for (auto IdentIDPair : IdentifierIDs) {
3281 IdentifierInfo *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3282 IdentID ID = IdentIDPair.second;
3283 assert(II && "NULL identifier in identifier table");
3284 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3285 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003286 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003287
Douglas Gregore84a9da2009-04-20 20:36:09 +00003288 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003289 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003290 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003291 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003292 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003293 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003294 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003295 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003296 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003297 }
3298
3299 // Create a blob abbreviation
3300 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003301 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003304 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003305
3306 // Write the identifier table
3307 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003308 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003309 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003310 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003311 }
3312
3313 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003314 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003315 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3319 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3320
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003321#ifndef NDEBUG
3322 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3323 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3324#endif
3325
Douglas Gregor0e149972009-04-25 19:10:14 +00003326 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003327 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003328 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003329 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003330 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003331 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003332
3333 // In C++, write the list of interesting identifiers (those that are
3334 // defined as macros, poisoned, or similar unusual things).
3335 if (!InterestingIdents.empty())
3336 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003337}
3338
Douglas Gregorc5046832009-04-27 18:38:38 +00003339//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003340// DeclContext's Name Lookup Table Serialization
3341//===----------------------------------------------------------------------===//
3342
3343namespace {
3344// Trait used for the on-disk hash table used in the method pool.
3345class ASTDeclContextNameLookupTrait {
3346 ASTWriter &Writer;
3347
3348public:
3349 typedef DeclarationName key_type;
3350 typedef key_type key_type_ref;
3351
3352 typedef DeclContext::lookup_result data_type;
3353 typedef const data_type& data_type_ref;
3354
Justin Bogner25463f12014-04-18 20:27:24 +00003355 typedef unsigned hash_value_type;
3356 typedef unsigned offset_type;
3357
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003358 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3359
Justin Bogner25463f12014-04-18 20:27:24 +00003360 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003361 llvm::FoldingSetNodeID ID;
3362 ID.AddInteger(Name.getNameKind());
3363
3364 switch (Name.getNameKind()) {
3365 case DeclarationName::Identifier:
3366 ID.AddString(Name.getAsIdentifierInfo()->getName());
3367 break;
3368 case DeclarationName::ObjCZeroArgSelector:
3369 case DeclarationName::ObjCOneArgSelector:
3370 case DeclarationName::ObjCMultiArgSelector:
3371 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3372 break;
3373 case DeclarationName::CXXConstructorName:
3374 case DeclarationName::CXXDestructorName:
3375 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003376 break;
3377 case DeclarationName::CXXOperatorName:
3378 ID.AddInteger(Name.getCXXOverloadedOperator());
3379 break;
3380 case DeclarationName::CXXLiteralOperatorName:
3381 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3382 case DeclarationName::CXXUsingDirective:
3383 break;
3384 }
3385
3386 return ID.ComputeHash();
3387 }
3388
3389 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003390 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003391 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003392 using namespace llvm::support;
3393 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003394 unsigned KeyLen = 1;
3395 switch (Name.getNameKind()) {
3396 case DeclarationName::Identifier:
3397 case DeclarationName::ObjCZeroArgSelector:
3398 case DeclarationName::ObjCOneArgSelector:
3399 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003400 case DeclarationName::CXXLiteralOperatorName:
3401 KeyLen += 4;
3402 break;
3403 case DeclarationName::CXXOperatorName:
3404 KeyLen += 1;
3405 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003406 case DeclarationName::CXXConstructorName:
3407 case DeclarationName::CXXDestructorName:
3408 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003409 case DeclarationName::CXXUsingDirective:
3410 break;
3411 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003412 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003413
Richard Smithf02662d2015-07-30 03:17:16 +00003414 // 4 bytes for each DeclID.
3415 unsigned DataLen = 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003416 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003417
3418 return std::make_pair(KeyLen, DataLen);
3419 }
3420
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003421 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003422 using namespace llvm::support;
3423 endian::Writer<little> LE(Out);
3424 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003425 switch (Name.getNameKind()) {
3426 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003427 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003428 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003429 case DeclarationName::ObjCZeroArgSelector:
3430 case DeclarationName::ObjCOneArgSelector:
3431 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003432 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003433 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003434 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003435 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3436 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003437 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003438 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003439 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003440 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003441 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003442 case DeclarationName::CXXConstructorName:
3443 case DeclarationName::CXXDestructorName:
3444 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003445 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003446 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003447 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003448
3449 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003450 }
3451
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003452 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003453 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003454 using namespace llvm::support;
3455 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003456 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003457 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3458 I != E; ++I)
Richard Smithb3761912015-02-05 23:08:52 +00003459 LE.write<uint32_t>(
3460 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003461
3462 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3463 }
3464};
3465} // end anonymous namespace
3466
Chandler Carruthe972c362015-03-26 03:11:40 +00003467bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3468 DeclContext *DC) {
3469 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3470}
3471
3472bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3473 DeclContext *DC) {
3474 for (auto *D : Result.getLookupResult())
3475 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3476 return false;
3477
3478 return true;
3479}
3480
Richard Smithcd45dbc2014-04-19 03:48:30 +00003481uint32_t
Chandler Carruthe972c362015-03-26 03:11:40 +00003482ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003483 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003484 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3485 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003486 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003487
Chandler Carruthe972c362015-03-26 03:11:40 +00003488 // FIXME: We need to build the lookups table, which is logically const.
3489 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
3490 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3491
3492 // Create the on-disk hash table representation.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003493 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3494 Generator;
3495 ASTDeclContextNameLookupTrait Trait(*this);
3496
Chandler Carruthe972c362015-03-26 03:11:40 +00003497 // The first step is to collect the declaration names which we need to
3498 // serialize into the name lookup table, and to collect them in a stable
3499 // order.
3500 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003501
Chandler Carruthe972c362015-03-26 03:11:40 +00003502 // We also build up small sets of the constructor and conversion function
3503 // names which are visible.
3504 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003505
Chandler Carruthe972c362015-03-26 03:11:40 +00003506 for (auto &Lookup : *DC->buildLookup()) {
3507 auto &Name = Lookup.first;
3508 auto &Result = Lookup.second;
3509
3510 // If there are no local declarations in our lookup result, we don't
3511 // need to write an entry for the name at all unless we're rewriting
3512 // the decl context. If we can't write out a lookup set without
3513 // performing more deserialization, just skip this entry.
3514 if (isLookupResultExternal(Result, DC) && !isRewritten(cast<Decl>(DC)) &&
3515 isLookupResultEntirelyExternal(Result, DC))
3516 continue;
3517
3518 // We also skip empty results. If any of the results could be external and
3519 // the currently available results are empty, then all of the results are
3520 // external and we skip it above. So the only way we get here with an empty
3521 // results is when no results could have been external *and* we have
3522 // external results.
3523 //
3524 // FIXME: While we might want to start emitting on-disk entries for negative
3525 // lookups into a decl context as an optimization, today we *have* to skip
3526 // them because there are names with empty lookup results in decl contexts
3527 // which we can't emit in any stable ordering: we lookup constructors and
3528 // conversion functions in the enclosing namespace scope creating empty
3529 // results for them. This in almost certainly a bug in Clang's name lookup,
3530 // but that is likely to be hard or impossible to fix and so we tolerate it
3531 // here by omitting lookups with empty results.
3532 if (Lookup.second.getLookupResult().empty())
3533 continue;
3534
3535 switch (Lookup.first.getNameKind()) {
3536 default:
3537 Names.push_back(Lookup.first);
3538 break;
3539
Richard Smithcd45dbc2014-04-19 03:48:30 +00003540 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003541 assert(isa<CXXRecordDecl>(DC) &&
3542 "Cannot have a constructor name outside of a class!");
3543 ConstructorNameSet.insert(Name);
3544 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003545
3546 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003547 assert(isa<CXXRecordDecl>(DC) &&
3548 "Cannot have a conversion function name outside of a class!");
3549 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003550 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003551 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003552 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003553
Chandler Carruthe972c362015-03-26 03:11:40 +00003554 // Sort the names into a stable order.
3555 std::sort(Names.begin(), Names.end());
3556
Chandler Carruthffbf7052015-03-26 22:27:09 +00003557 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003558 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003559 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003560
Chandler Carruthffbf7052015-03-26 22:27:09 +00003561 // First we try the easy case by forming the current context's constructor
3562 // name and adding that name first. This is a very useful optimization to
3563 // avoid walking the lexical declarations in many cases, and it also
3564 // handles the only case where a constructor name can come from some other
3565 // lexical context -- when that name is an implicit constructor merged from
3566 // another declaration in the redecl chain. Any non-implicit constructor or
3567 // conversion function which doesn't occur in all the lexical contexts
3568 // would be an ODR violation.
3569 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3570 Context->getCanonicalType(Context->getRecordType(D)));
3571 if (ConstructorNameSet.erase(ImplicitCtorName))
3572 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003573
Chandler Carruthffbf7052015-03-26 22:27:09 +00003574 // If we still have constructors or conversion functions, we walk all the
3575 // names in the decl and add the constructors and conversion functions
3576 // which are visible in the order they lexically occur within the context.
3577 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3578 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3579 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3580 auto Name = ChildND->getDeclName();
3581 switch (Name.getNameKind()) {
3582 default:
3583 continue;
3584
3585 case DeclarationName::CXXConstructorName:
3586 if (ConstructorNameSet.erase(Name))
3587 Names.push_back(Name);
3588 break;
3589
3590 case DeclarationName::CXXConversionFunctionName:
3591 if (ConversionNameSet.erase(Name))
3592 Names.push_back(Name);
3593 break;
3594 }
3595
3596 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3597 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003598 }
3599
Chandler Carruthe972c362015-03-26 03:11:40 +00003600 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3601 "constructors by walking all the "
3602 "lexical members of the context.");
3603 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3604 "conversion functions by walking all "
3605 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003606 }
3607
Chandler Carruthe972c362015-03-26 03:11:40 +00003608 // Next we need to do a lookup with each name into this decl context to fully
3609 // populate any results from external sources. We don't actually use the
3610 // results of these lookups because we only want to use the results after all
3611 // results have been loaded and the pointers into them will be stable.
3612 for (auto &Name : Names)
3613 DC->lookup(Name);
3614
3615 // Now we need to insert the results for each name into the hash table. For
3616 // constructor names and conversion function names, we actually need to merge
3617 // all of the results for them into one list of results each and insert
3618 // those.
3619 SmallVector<NamedDecl *, 8> ConstructorDecls;
3620 SmallVector<NamedDecl *, 8> ConversionDecls;
3621
3622 // Now loop over the names, either inserting them or appending for the two
3623 // special cases.
3624 for (auto &Name : Names) {
3625 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3626
3627 switch (Name.getNameKind()) {
3628 default:
3629 Generator.insert(Name, Result, Trait);
3630 break;
3631
3632 case DeclarationName::CXXConstructorName:
3633 ConstructorDecls.append(Result.begin(), Result.end());
3634 break;
3635
3636 case DeclarationName::CXXConversionFunctionName:
3637 ConversionDecls.append(Result.begin(), Result.end());
3638 break;
3639 }
3640 }
3641
3642 // Handle our two special cases if we ended up having any. We arbitrarily use
3643 // the first declaration's name here because the name itself isn't part of
3644 // the key, only the kind of name is used.
3645 if (!ConstructorDecls.empty())
3646 Generator.insert(ConstructorDecls.front()->getDeclName(),
3647 DeclContext::lookup_result(ConstructorDecls), Trait);
3648 if (!ConversionDecls.empty())
3649 Generator.insert(ConversionDecls.front()->getDeclName(),
3650 DeclContext::lookup_result(ConversionDecls), Trait);
3651
Richard Smith961eae52014-03-25 01:14:22 +00003652 // Create the on-disk hash table in a buffer.
3653 llvm::raw_svector_ostream Out(LookupTable);
3654 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003655 using namespace llvm::support;
3656 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003657 return Generator.Emit(Out, Trait);
3658}
3659
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003660/// \brief Write the block containing all of the declaration IDs
3661/// visible from the given DeclContext.
3662///
3663/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003664/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003665uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3666 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003667 // If we imported a key declaration of this namespace, write the visible
3668 // lookup results as an update record for it rather than including them
3669 // on this declaration. We will only look at key declarations on reload.
3670 if (isa<NamespaceDecl>(DC) && Chain &&
3671 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3672 // Only do this once, for the first local declaration of the namespace.
3673 for (NamespaceDecl *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
3674 Prev = Prev->getPreviousDecl())
3675 if (!Prev->isFromASTFile())
3676 return 0;
3677
3678 // Note that we need to emit an update record for the primary context.
3679 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3680
3681 // Make sure all visible decls are written. They will be recorded later. We
3682 // do this using a side data structure so we can sort the names into
3683 // a deterministic order.
3684 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3685 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3686 LookupResults;
3687 if (Map) {
3688 LookupResults.reserve(Map->size());
3689 for (auto &Entry : *Map)
3690 LookupResults.push_back(
3691 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3692 }
3693
3694 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3695 for (auto &NameAndResult : LookupResults) {
3696 DeclarationName Name = NameAndResult.first;
3697 DeclContext::lookup_result Result = NameAndResult.second;
3698 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3699 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3700 // We have to work around a name lookup bug here where negative lookup
3701 // results for these names get cached in namespace lookup tables (these
3702 // names should never be looked up in a namespace).
3703 assert(Result.empty() && "Cannot have a constructor or conversion "
3704 "function name in a namespace!");
3705 continue;
3706 }
3707
3708 for (NamedDecl *ND : Result)
3709 if (!ND->isFromASTFile())
3710 GetDeclRef(ND);
3711 }
3712
3713 return 0;
3714 }
3715
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003716 if (DC->getPrimaryContext() != DC)
3717 return 0;
3718
Chandler Carruthe972c362015-03-26 03:11:40 +00003719 // Skip contexts which don't support name lookup.
3720 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003721 return 0;
3722
3723 // If not in C++, we perform name lookup for the translation unit via the
3724 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003725 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003726 return 0;
3727
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003728 // Serialize the contents of the mapping used for lookup. Note that,
3729 // although we have two very different code paths, the serialized
3730 // representation is the same for both cases: a declaration name,
3731 // followed by a size, followed by references to the visible
3732 // declarations that have that name.
3733 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003734 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003735 if (!Map || Map->empty())
3736 return 0;
3737
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003738 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003739 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003740 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003741
3742 // Write the lookup table
3743 RecordData Record;
3744 Record.push_back(DECL_CONTEXT_VISIBLE);
3745 Record.push_back(BucketOffset);
3746 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003747 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003748 ++NumVisibleDeclContexts;
3749 return Offset;
3750}
3751
Sebastian Redla4071b42010-08-24 00:50:09 +00003752/// \brief Write an UPDATE_VISIBLE block for the given context.
3753///
3754/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3755/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003756/// (in C++), for namespaces, and for classes with forward-declared unscoped
3757/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003758void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003759 if (isRewritten(cast<Decl>(DC)))
3760 return;
3761
Richard Smith961eae52014-03-25 01:14:22 +00003762 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003763 if (!Map || Map->empty())
3764 return;
3765
Sebastian Redla4071b42010-08-24 00:50:09 +00003766 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003767 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003768 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003769
Richard Smith5fc18a92015-07-12 23:43:21 +00003770 // If we're updating a namespace, select a key declaration as the key for the
3771 // update record; those are the only ones that will be checked on reload.
3772 if (isa<NamespaceDecl>(DC))
3773 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3774
Sebastian Redla4071b42010-08-24 00:50:09 +00003775 // Write the lookup table
3776 RecordData Record;
3777 Record.push_back(UPDATE_VISIBLE);
3778 Record.push_back(getDeclID(cast<Decl>(DC)));
3779 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003780 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003781}
3782
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003783/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3784void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3785 RecordData Record;
3786 Record.push_back(Opts.fp_contract);
3787 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3788}
3789
3790/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3791void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003792 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003793 return;
3794
3795 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3796 RecordData Record;
3797#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3798#include "clang/Basic/OpenCLExtensions.def"
3799 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3800}
3801
Douglas Gregor358cd442012-01-15 16:58:34 +00003802void ASTWriter::WriteRedeclarations() {
3803 RecordData LocalRedeclChains;
3804 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3805
3806 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003807 const Decl *Key = Redeclarations[I];
3808 assert((Chain ? Chain->getKeyDeclaration(Key) == Key
3809 : Key->isFirstDecl()) &&
3810 "not the key declaration");
3811
3812 const Decl *First = Key->getCanonicalDecl();
3813 const Decl *MostRecent = First->getMostRecentDecl();
3814
3815 assert((getDeclID(First) >= NUM_PREDEF_DECL_IDS || First == Key) &&
3816 "should not have imported key decls for predefined decl");
3817
Douglas Gregor358cd442012-01-15 16:58:34 +00003818 // If we only have a single declaration, there is no point in storing
3819 // a redeclaration chain.
3820 if (First == MostRecent)
3821 continue;
3822
3823 unsigned Offset = LocalRedeclChains.size();
3824 unsigned Size = 0;
3825 LocalRedeclChains.push_back(0); // Placeholder for the size.
Richard Smith5fc18a92015-07-12 23:43:21 +00003826
Richard Smithe2f8ce92015-07-15 00:02:40 +00003827 // Collect the set of local redeclarations of this declaration, from newest
3828 // to oldest.
Richard Smith5fc18a92015-07-12 23:43:21 +00003829 for (const Decl *Prev = MostRecent; Prev;
Douglas Gregor358cd442012-01-15 16:58:34 +00003830 Prev = Prev->getPreviousDecl()) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003831 if (!Prev->isFromASTFile() && Prev != Key) {
Douglas Gregor358cd442012-01-15 16:58:34 +00003832 AddDeclRef(Prev, LocalRedeclChains);
3833 ++Size;
3834 }
3835 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003836
Douglas Gregor358cd442012-01-15 16:58:34 +00003837 LocalRedeclChains[Offset] = Size;
Richard Smith5fc18a92015-07-12 23:43:21 +00003838
Douglas Gregor6168bd22013-02-18 15:53:43 +00003839 // Add the mapping from the first ID from the AST to the set of local
3840 // declarations.
Richard Smith5fc18a92015-07-12 23:43:21 +00003841 LocalRedeclarationsInfo Info = { getDeclID(Key), Offset };
Douglas Gregor358cd442012-01-15 16:58:34 +00003842 LocalRedeclsMap.push_back(Info);
Richard Smith5fc18a92015-07-12 23:43:21 +00003843
Douglas Gregor358cd442012-01-15 16:58:34 +00003844 assert(N == Redeclarations.size() &&
3845 "Deserialized a declaration we shouldn't have");
3846 }
Richard Smith5fc18a92015-07-12 23:43:21 +00003847
Douglas Gregor358cd442012-01-15 16:58:34 +00003848 if (LocalRedeclChains.empty())
3849 return;
Richard Smith5fc18a92015-07-12 23:43:21 +00003850
Douglas Gregor358cd442012-01-15 16:58:34 +00003851 // Sort the local redeclarations map by the first declaration ID,
3852 // since the reader will be performing binary searches on this information.
3853 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3854
3855 // Emit the local redeclarations map.
3856 using namespace llvm;
3857 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3858 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3859 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3860 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3861 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3862
3863 RecordData Record;
3864 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3865 Record.push_back(LocalRedeclsMap.size());
3866 Stream.EmitRecordWithBlob(AbbrevID, Record,
3867 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3868 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3869
3870 // Emit the redeclaration chains.
3871 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3872}
3873
Douglas Gregor404cdde2012-01-27 01:47:08 +00003874void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003875 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003876 RecordData Categories;
3877
3878 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3879 unsigned Size = 0;
3880 unsigned StartIndex = Categories.size();
3881
3882 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3883
3884 // Allocate space for the size.
3885 Categories.push_back(0);
3886
3887 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003888 for (ObjCInterfaceDecl::known_categories_iterator
3889 Cat = Class->known_categories_begin(),
3890 CatEnd = Class->known_categories_end();
3891 Cat != CatEnd; ++Cat, ++Size) {
3892 assert(getDeclID(*Cat) != 0 && "Bogus category");
3893 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003894 }
3895
3896 // Update the size.
3897 Categories[StartIndex] = Size;
3898
3899 // Record this interface -> category map.
3900 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3901 CategoriesMap.push_back(CatInfo);
3902 }
3903
3904 // Sort the categories map by the definition ID, since the reader will be
3905 // performing binary searches on this information.
3906 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3907
3908 // Emit the categories map.
3909 using namespace llvm;
3910 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3911 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3914 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3915
3916 RecordData Record;
3917 Record.push_back(OBJC_CATEGORIES_MAP);
3918 Record.push_back(CategoriesMap.size());
3919 Stream.EmitRecordWithBlob(AbbrevID, Record,
3920 reinterpret_cast<char*>(CategoriesMap.data()),
3921 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3922
3923 // Emit the category lists.
3924 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3925}
3926
Richard Smithe40f2ba2013-08-07 21:41:30 +00003927void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3928 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3929
3930 if (LPTMap.empty())
3931 return;
3932
3933 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00003934 for (auto LPTMapEntry : LPTMap) {
3935 const FunctionDecl *FD = LPTMapEntry.first;
3936 LateParsedTemplate *LPT = LPTMapEntry.second;
3937 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003938 AddDeclRef(LPT->D, Record);
3939 Record.push_back(LPT->Toks.size());
3940
3941 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3942 TokEnd = LPT->Toks.end();
3943 TokIt != TokEnd; ++TokIt) {
3944 AddToken(*TokIt, Record);
3945 }
3946 }
3947 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3948}
3949
Dario Domizioli13a0a382014-05-23 12:13:25 +00003950/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3951void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3952 RecordData Record;
3953 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3954 AddSourceLocation(PragmaLoc, Record);
3955 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3956}
3957
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003958//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003959// General Serialization Routines
3960//===----------------------------------------------------------------------===//
3961
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003962/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003963void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3964 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003965 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003966 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3967 e = Attrs.end(); i != e; ++i){
3968 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003969 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003970 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003971
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003972#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003973
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003974 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003975}
3976
John McCallf413f5e2013-05-03 00:10:13 +00003977void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3978 AddSourceLocation(Tok.getLocation(), Record);
3979 Record.push_back(Tok.getLength());
3980
3981 // FIXME: When reading literal tokens, reconstruct the literal pointer
3982 // if it is needed.
3983 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3984 // FIXME: Should translate token kind to a stable encoding.
3985 Record.push_back(Tok.getKind());
3986 // FIXME: Should translate token flags to a stable encoding.
3987 Record.push_back(Tok.getFlags());
3988}
3989
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003990void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003991 Record.push_back(Str.size());
3992 Record.insert(Record.end(), Str.begin(), Str.end());
3993}
3994
Richard Smith7ed1bc92014-12-05 22:42:13 +00003995bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00003996 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00003997
Richard Smith54cc3c22014-12-11 20:50:24 +00003998 bool Changed =
3999 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004000
4001 // Remove a prefix to make the path relative, if relevant.
4002 const char *PathBegin = Path.data();
4003 const char *PathPtr =
4004 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4005 if (PathPtr != PathBegin) {
4006 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4007 Changed = true;
4008 }
4009
4010 return Changed;
4011}
4012
4013void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4014 SmallString<128> FilePath(Path);
4015 PreparePathForOutput(FilePath);
4016 AddString(FilePath, Record);
4017}
4018
4019void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
4020 StringRef Path) {
4021 SmallString<128> FilePath(Path);
4022 PreparePathForOutput(FilePath);
4023 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4024}
4025
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004026void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4027 RecordDataImpl &Record) {
4028 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004029 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004030 Record.push_back(*Minor + 1);
4031 else
4032 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004033 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004034 Record.push_back(*Subminor + 1);
4035 else
4036 Record.push_back(0);
4037}
4038
Douglas Gregore84a9da2009-04-20 20:36:09 +00004039/// \brief Note that the identifier II occurs at the given offset
4040/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004041void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004042 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004043 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004044 // up earlier in the chain and thus don't need an offset.
4045 if (ID >= FirstIdentID)
4046 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004047}
4048
Douglas Gregor95c13f52009-04-25 17:48:32 +00004049/// \brief Note that the selector Sel occurs at the given offset
4050/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004051void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004052 unsigned ID = SelectorIDs[Sel];
4053 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004054 // Don't record offsets for selectors that are also available in a different
4055 // file.
4056 if (ID < FirstSelectorID)
4057 return;
4058 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004059}
4060
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004061ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00004062 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4063 WritingModule(nullptr), WritingAST(false),
4064 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4065 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4066 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4067 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4068 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4069 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4070 NextSubmoduleID(FirstSubmoduleID),
4071 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4072 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4073 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00004074 NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
4075 TypeExtQualAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004076 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4077 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004078 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004079 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004080 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4081 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4082 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004083
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004084ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004085 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004086}
4087
Richard Smithb3761912015-02-05 23:08:52 +00004088const LangOptions &ASTWriter::getLangOpts() const {
4089 assert(WritingAST && "can't determine lang opts when not writing AST");
4090 return Context->getLangOpts();
4091}
4092
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004093void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004094 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004095 Module *WritingModule, StringRef isysroot,
4096 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004097 WritingAST = true;
4098
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004099 ASTHasCompilerErrors = hasErrors;
4100
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004101 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004102 Stream.Emit((unsigned)'C', 8);
4103 Stream.Emit((unsigned)'P', 8);
4104 Stream.Emit((unsigned)'C', 8);
4105 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004106
Chris Lattner28fa4e62009-04-26 22:26:21 +00004107 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004108
Douglas Gregoreda8e122011-08-09 15:13:55 +00004109 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004110 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004111 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004112 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004113 Context = nullptr;
4114 PP = nullptr;
4115 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004116 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004117
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004118 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004119}
4120
Douglas Gregora94a1542011-07-27 21:45:57 +00004121template<typename Vector>
4122static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4123 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004124 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4125 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004126 Writer.AddDeclRef(*I, Record);
4127 }
4128}
4129
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004130void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004131 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004132 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004133 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004134 using namespace llvm;
4135
Craig Toppera13603a2014-05-22 05:54:18 +00004136 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004137
Douglas Gregorcf68c582011-12-01 22:20:10 +00004138 // Make sure that the AST reader knows to finalize itself.
4139 if (Chain)
4140 Chain->finalizeForWriting();
4141
Sebastian Redl143413f2010-07-12 22:02:52 +00004142 ASTContext &Context = SemaRef.Context;
4143 Preprocessor &PP = SemaRef.PP;
4144
Douglas Gregordab42432011-08-12 00:15:20 +00004145 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004146 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4147 if (D) {
4148 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4149 DeclIDs[D] = ID;
4150 if (D->getMostRecentDecl() != D)
4151 Redeclarations.push_back(D);
4152 }
4153 };
4154 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4155 PREDEF_DECL_TRANSLATION_UNIT_ID);
4156 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4157 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4158 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4159 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4160 PREDEF_DECL_OBJC_PROTOCOL_ID);
4161 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4162 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4163 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4164 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4165 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004166 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Richard Smith5fc18a92015-07-12 23:43:21 +00004167 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004168
Chris Lattner0c797362009-09-08 18:19:27 +00004169 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004170 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004171 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004172 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004173 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004174
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004175 // Build a record containing all of the file scoped decls in this file.
4176 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004177 if (!isModule)
4178 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4179 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004180
Douglas Gregor851443c2011-08-12 01:39:19 +00004181 // Build a record containing all of the delegating constructors we still need
4182 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004183 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004184 if (!isModule)
4185 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004186
Douglas Gregor851443c2011-08-12 01:39:19 +00004187 // Write the set of weak, undeclared identifiers. We always write the
4188 // entire table, since later PCH files in a PCH chain are only interested in
4189 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004190 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004191 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4192 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4193 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4194 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4195 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4196 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4197 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004198 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004199
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004200 // Build a record containing all of the ext_vector declarations.
4201 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004202 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004203
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004204 // Build a record containing all of the VTable uses information.
4205 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004206 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004207 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4208 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4209 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4210 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4211 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004212 }
4213
Nico Weber72889432014-09-06 01:25:55 +00004214 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4215 RecordData UnusedLocalTypedefNameCandidates;
4216 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4217 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4218
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004219 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004220 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004221 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004222 I = SemaRef.PendingInstantiations.begin(),
4223 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4224 AddDeclRef(I->first, PendingInstantiations);
4225 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004226 }
4227 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4228 "There are local ones at end of translation unit!");
4229
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004230 // Build a record containing some declaration references.
4231 RecordData SemaDeclRefs;
4232 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4233 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4234 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4235 }
4236
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004237 RecordData CUDASpecialDeclRefs;
4238 if (Context.getcudaConfigureCallDecl()) {
4239 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4240 }
4241
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004242 // Build a record containing all of the known namespaces.
4243 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004244 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004245 I = SemaRef.KnownNamespaces.begin(),
4246 IEnd = SemaRef.KnownNamespaces.end();
4247 I != IEnd; ++I) {
4248 if (!I->second)
4249 AddDeclRef(I->first, KnownNamespaces);
4250 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004251
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004252 // Build a record of all used, undefined objects that require definitions.
4253 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004254
4255 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004256 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004257 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4258 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004259 AddDeclRef(I->first, UndefinedButUsed);
4260 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004261 }
4262
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004263 // Build a record containing all delete-expressions that we would like to
4264 // analyze later in AST.
4265 RecordData DeleteExprsToAnalyze;
4266
4267 for (const auto &DeleteExprsInfo :
4268 SemaRef.getMismatchingDeleteExpressions()) {
4269 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4270 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4271 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4272 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4273 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4274 }
4275 }
4276
Douglas Gregor112b9072012-10-18 05:31:06 +00004277 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004278 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004279
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004280 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004281 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004282 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004283
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004284 // This is so that older clang versions, before the introduction
4285 // of the control block, can read and reject the newer PCH format.
4286 Record.clear();
4287 Record.push_back(VERSION_MAJOR);
4288 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4289
Douglas Gregor851443c2011-08-12 01:39:19 +00004290 // Create a lexical update block containing all of the declarations in the
4291 // translation unit that do not come from other AST files.
4292 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004293 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4294 for (const auto *D : TU->noload_decls()) {
4295 if (!D->isFromASTFile()) {
4296 NewGlobalKindDeclPairs.push_back(D->getKind());
4297 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4298 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004299 }
4300
4301 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4302 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4303 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4304 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4305 Record.clear();
4306 Record.push_back(TU_UPDATE_LEXICAL);
4307 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Richard Smith82f8fcd2015-08-06 22:07:25 +00004308 bytes(NewGlobalKindDeclPairs));
Douglas Gregor851443c2011-08-12 01:39:19 +00004309
4310 // And a visible updates block for the translation unit.
4311 Abv = new llvm::BitCodeAbbrev();
4312 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4313 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4314 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4315 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4316 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4317 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004318
4319 // If we have any extern "C" names, write out a visible update for them.
4320 if (Context.ExternCContext)
4321 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004322
4323 // If the translation unit has an anonymous namespace, and we don't already
4324 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004325 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004326 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4327 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004328 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004329 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004330 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004331
Richard Smith5652c0f2014-03-21 01:48:23 +00004332 // Add update records for all mangling numbers and static local numbers.
4333 // These aren't really update records, but this is a convenient way of
4334 // tagging this rare extra data onto the declarations.
4335 for (const auto &Number : Context.MangleNumbers)
4336 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004337 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4338 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004339 for (const auto &Number : Context.StaticLocalNumbers)
4340 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004341 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4342 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004343
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004344 // Make sure visible decls, added to DeclContexts previously loaded from
4345 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004346 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004347 I = UpdatingVisibleDecls.begin(),
4348 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4349 GetDeclRef(*I);
4350 }
4351
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004352 // Make sure all decls associated with an identifier are registered for
4353 // serialization.
Chandler Carruth8440c982015-03-26 23:54:15 +00004354 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004355 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4356 IDEnd = PP.getIdentifierTable().end();
4357 ID != IDEnd; ++ID) {
4358 const IdentifierInfo *II = ID->second;
Richard Smith9ab4cce2015-03-11 00:00:51 +00004359 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
Chandler Carruth8440c982015-03-26 23:54:15 +00004360 IIs.push_back(II);
Richard Smith9ab4cce2015-03-11 00:00:51 +00004361 }
Chandler Carruth8440c982015-03-26 23:54:15 +00004362 // Sort the identifiers to visit based on their name.
4363 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4364 for (const IdentifierInfo *II : IIs) {
Richard Smith9ab4cce2015-03-11 00:00:51 +00004365 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4366 DEnd = SemaRef.IdResolver.end();
4367 D != DEnd; ++D) {
4368 GetDeclRef(*D);
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004369 }
4370 }
4371
Douglas Gregor5204bde2011-08-02 16:26:37 +00004372 // Form the record of special types.
4373 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004374 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004375 AddTypeRef(Context.getFILEType(), SpecialTypes);
4376 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4377 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4378 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4379 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004380 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004381 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004382
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004383 if (Chain) {
4384 // Write the mapping information describing our module dependencies and how
4385 // each of those modules were mapped into our own offset/ID space, so that
4386 // the reader can build the appropriate mapping to its own offset/ID space.
4387 // The map consists solely of a blob with the following format:
4388 // *(module-name-len:i16 module-name:len*i8
4389 // source-location-offset:i32
4390 // identifier-id:i32
4391 // preprocessed-entity-id:i32
4392 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004393 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004394 // selector-id:i32
4395 // declaration-id:i32
4396 // c++-base-specifiers-id:i32
4397 // type-id:i32)
4398 //
4399 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4400 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4402 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004403 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004404 {
4405 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004406 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004407 using namespace llvm::support;
4408 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004409 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004410 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004411 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004412
Ben Langmuir785180e2014-10-20 16:27:30 +00004413 // Note: if a base ID was uint max, it would not be possible to load
4414 // another module after it or have more than one entity inside it.
4415 uint32_t None = std::numeric_limits<uint32_t>::max();
4416
4417 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4418 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4419 if (ShouldWrite)
4420 LE.write<uint32_t>(BaseID);
4421 else
4422 LE.write<uint32_t>(None);
4423 };
4424
Ben Langmuirfe971d92014-08-16 04:54:18 +00004425 // These values should be unique within a chain, since they will be read
4426 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004427 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4428 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4429 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4430 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4431 M->NumPreprocessedEntities);
4432 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4433 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4434 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4435 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004436 }
4437 }
4438 Record.clear();
4439 Record.push_back(MODULE_OFFSET_MAP);
4440 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4441 Buffer.data(), Buffer.size());
4442 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004443
Richard Smithb9eab6d2014-03-20 19:44:17 +00004444 RecordData DeclUpdatesOffsetsRecord;
4445
Richard Smith59442b42014-03-20 20:07:19 +00004446 // Keep writing types, declarations, and declaration update records
4447 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004448 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4449 WriteTypeAbbrevs();
4450 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004451 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4452 E = DeclsToRewrite.end();
4453 I != E; ++I)
4454 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004455 do {
4456 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4457 while (!DeclTypesToEmit.empty()) {
4458 DeclOrType DOT = DeclTypesToEmit.front();
4459 DeclTypesToEmit.pop();
4460 if (DOT.isType())
4461 WriteType(DOT.getType());
4462 else
4463 WriteDecl(Context, DOT.getDecl());
4464 }
4465 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004466 Stream.ExitBlock();
4467
Richard Smithb9eab6d2014-03-20 19:44:17 +00004468 DoneWritingDeclsAndTypes = true;
4469
4470 // These things can only be done once we've written out decls and types.
4471 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004472 if (!DeclUpdatesOffsetsRecord.empty())
4473 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004474 WriteCXXBaseSpecifiersOffsets();
Richard Smithc2bb8182015-03-24 06:36:48 +00004475 WriteCXXCtorInitializersOffsets();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004476 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004477 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004478
4479 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004480 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004481 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004482 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004483 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004484 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004485 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004486 WriteFPPragmaOptions(SemaRef.getFPOptions());
4487 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004488 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004489
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004490 // If we're emitting a module, write out the submodule information.
4491 if (WritingModule)
4492 WriteSubmodules(WritingModule);
4493
Douglas Gregor5204bde2011-08-02 16:26:37 +00004494 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4495
Douglas Gregord4df8652009-04-22 22:02:47 +00004496 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004497 if (!EagerlyDeserializedDecls.empty())
4498 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004499
4500 // Write the record containing tentative definitions.
4501 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004502 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004503
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004504 // Write the record containing unused file scoped decls.
4505 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004506 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004507
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004508 // Write the record containing weak undeclared identifiers.
4509 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004510 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004511 WeakUndeclaredIdentifiers);
4512
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004513 // Write the record containing ext_vector type names.
4514 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004515 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004516
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004517 // Write the record containing VTable uses information.
4518 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004519 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004520
Nico Weber72889432014-09-06 01:25:55 +00004521 // Write the record containing potentially unused local typedefs.
4522 if (!UnusedLocalTypedefNameCandidates.empty())
4523 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4524 UnusedLocalTypedefNameCandidates);
4525
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004526 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004527 if (!PendingInstantiations.empty())
4528 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004529
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004530 // Write the record containing declaration references of Sema.
4531 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004532 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004533
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004534 // Write the record containing CUDA-specific declaration references.
4535 if (!CUDASpecialDeclRefs.empty())
4536 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004537
4538 // Write the delegating constructors.
4539 if (!DelegatingCtorDecls.empty())
4540 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004541
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004542 // Write the known namespaces.
4543 if (!KnownNamespaces.empty())
4544 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004545
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004546 // Write the undefined internal functions and variables, and inline functions.
4547 if (!UndefinedButUsed.empty())
4548 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004549
4550 if (!DeleteExprsToAnalyze.empty())
4551 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4552
Douglas Gregor851443c2011-08-12 01:39:19 +00004553 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004554 for (auto *DC : UpdatedDeclContexts)
4555 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004556
Douglas Gregor959bb062011-12-03 01:15:29 +00004557 if (!WritingModule) {
4558 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004559 struct ModuleInfo {
4560 uint64_t ID;
4561 Module *M;
4562 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4563 };
Richard Smith56be7542014-03-21 00:33:59 +00004564 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004565 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004566 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004567 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4568 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004569 }
Richard Smith56be7542014-03-21 00:33:59 +00004570
4571 if (!Imports.empty()) {
4572 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4573 return A.ID < B.ID;
4574 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004575 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4576 return A.ID == B.ID;
4577 };
Richard Smith56be7542014-03-21 00:33:59 +00004578
4579 // Sort and deduplicate module IDs.
4580 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004581 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004582 Imports.end());
4583
4584 RecordData ImportedModules;
4585 for (const auto &Import : Imports) {
4586 ImportedModules.push_back(Import.ID);
4587 // FIXME: If the module has macros imported then later has declarations
4588 // imported, this location won't be the right one as a location for the
4589 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004590 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004591 }
4592
Douglas Gregor959bb062011-12-03 01:15:29 +00004593 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4594 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004595 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004596
Douglas Gregor851443c2011-08-12 01:39:19 +00004597 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004598 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004599 WriteObjCCategories();
Dario Domizioli13a0a382014-05-23 12:13:25 +00004600 if(!WritingModule)
4601 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004602
Douglas Gregor08f01292009-04-17 22:13:46 +00004603 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004604 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004605 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004606 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004607 Record.push_back(NumLexicalDeclContexts);
4608 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004609 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004610 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004611}
4612
Richard Smithb9eab6d2014-03-20 19:44:17 +00004613void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004614 if (DeclUpdates.empty())
4615 return;
4616
Richard Smith59442b42014-03-20 20:07:19 +00004617 DeclUpdateMap LocalUpdates;
4618 LocalUpdates.swap(DeclUpdates);
4619
Richard Smith6ef42932014-03-20 21:02:00 +00004620 for (auto &DeclUpdate : LocalUpdates) {
4621 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004622 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004623 continue; // The decl will be written completely,no need to store updates.
4624
Richard Smithd28ac5b2014-03-22 23:33:22 +00004625 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004626 RecordData Record;
4627 for (auto &Update : DeclUpdate.second) {
4628 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4629
4630 Record.push_back(Kind);
4631 switch (Kind) {
4632 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4633 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4634 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004635 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004636 Record.push_back(GetDeclRef(Update.getDecl()));
4637 break;
4638
Richard Smith4d235792014-08-07 18:53:08 +00004639 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004640 // An updated body is emitted last, so that the reader doesn't need
4641 // to skip over the lazy body to reach statements for other records.
4642 Record.pop_back();
4643 HasUpdatedBody = true;
4644 break;
4645
Richard Smith4d235792014-08-07 18:53:08 +00004646 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4647 AddSourceLocation(Update.getLoc(), Record);
4648 break;
4649
Richard Smithcd45dbc2014-04-19 03:48:30 +00004650 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4651 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004652 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004653 AddCXXDefinitionData(RD, Record);
4654 Record.push_back(WriteDeclContextLexicalBlock(
4655 *Context, const_cast<CXXRecordDecl *>(RD)));
4656
4657 // This state is sometimes updated by template instantiation, when we
4658 // switch from the specialization referring to the template declaration
4659 // to it referring to the template definition.
4660 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4661 Record.push_back(MSInfo->getTemplateSpecializationKind());
4662 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4663 } else {
4664 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4665 Record.push_back(Spec->getTemplateSpecializationKind());
4666 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004667
4668 // The instantiation might have been resolved to a partial
4669 // specialization. If so, record which one.
4670 auto From = Spec->getInstantiatedFrom();
4671 if (auto PartialSpec =
4672 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4673 Record.push_back(true);
4674 AddDeclRef(PartialSpec, Record);
4675 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4676 Record);
4677 } else {
4678 Record.push_back(false);
4679 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004680 }
4681 Record.push_back(RD->getTagKind());
4682 AddSourceLocation(RD->getLocation(), Record);
4683 AddSourceLocation(RD->getLocStart(), Record);
4684 AddSourceLocation(RD->getRBraceLoc(), Record);
4685
4686 // Instantiation may change attributes; write them all out afresh.
4687 Record.push_back(D->hasAttrs());
4688 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004689 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4690 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004691
4692 // FIXME: Ensure we don't get here for explicit instantiations.
4693 break;
4694 }
4695
Richard Smithf8134002015-03-10 01:41:22 +00004696 case UPD_CXX_RESOLVED_DTOR_DELETE:
4697 AddDeclRef(Update.getDecl(), Record);
4698 break;
4699
Richard Smith564417a2014-03-20 21:47:22 +00004700 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4701 addExceptionSpec(
4702 *this,
4703 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4704 Record);
4705 break;
4706
Richard Smith6ef42932014-03-20 21:02:00 +00004707 case UPD_CXX_DEDUCED_RETURN_TYPE:
4708 Record.push_back(GetOrCreateTypeID(Update.getType()));
4709 break;
4710
4711 case UPD_DECL_MARKED_USED:
4712 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004713
4714 case UPD_MANGLING_NUMBER:
4715 case UPD_STATIC_LOCAL_NUMBER:
4716 Record.push_back(Update.getNumber());
4717 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004718
Alexey Bataev97720002014-11-11 04:05:39 +00004719 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4720 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4721 Record);
4722 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004723
4724 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004725 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004726 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004727
4728 case UPD_ADDED_ATTR_TO_RECORD:
4729 WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record);
4730 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004731 }
4732 }
4733
Richard Smithd28ac5b2014-03-22 23:33:22 +00004734 if (HasUpdatedBody) {
4735 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004736 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004737 Record.push_back(Def->isInlined());
4738 AddSourceLocation(Def->getInnerLocStart(), Record);
4739 AddFunctionDefinition(Def, Record);
4740 }
4741
Richard Smithcd45dbc2014-04-19 03:48:30 +00004742 OffsetsRecord.push_back(GetDeclRef(D));
4743 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4744
Richard Smith6ef42932014-03-20 21:02:00 +00004745 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004746
Richard Smithc2bb8182015-03-24 06:36:48 +00004747 FlushPendingAfterDecl();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004748 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004749}
4750
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004751void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004752 if (ReplacedDecls.empty())
4753 return;
4754
4755 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004756 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4757 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004758 Record.push_back(I->ID);
4759 Record.push_back(I->Offset);
4760 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004761 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004762 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004763}
4764
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004765void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004766 Record.push_back(Loc.getRawEncoding());
4767}
4768
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004769void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004770 AddSourceLocation(Range.getBegin(), Record);
4771 AddSourceLocation(Range.getEnd(), Record);
4772}
4773
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004774void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004775 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004776 const uint64_t *Words = Value.getRawData();
4777 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004778}
4779
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004780void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004781 Record.push_back(Value.isUnsigned());
4782 AddAPInt(Value, Record);
4783}
4784
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004785void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004786 AddAPInt(Value.bitcastToAPInt(), Record);
4787}
4788
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004789void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004790 Record.push_back(getIdentifierRef(II));
4791}
4792
Sebastian Redl539c5062010-08-18 23:57:32 +00004793IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004794 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004795 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004796
Sebastian Redl539c5062010-08-18 23:57:32 +00004797 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004798 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004799 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004800 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004801}
4802
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004803MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004804 // Don't emit builtin macros like __LINE__ to the AST file unless they
4805 // have been redefined by the header (in which case they are not
4806 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004807 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004808 return 0;
4809
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004810 MacroID &ID = MacroIDs[MI];
4811 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004812 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004813 MacroInfoToEmitData Info = { Name, MI, ID };
4814 MacroInfosToEmit.push_back(Info);
4815 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004816 return ID;
4817}
4818
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004819MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004820 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004821 return 0;
4822
4823 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4824 return MacroIDs[MI];
4825}
4826
4827uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004828 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004829}
4830
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004831void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004832 Record.push_back(getSelectorRef(SelRef));
4833}
4834
Sebastian Redl539c5062010-08-18 23:57:32 +00004835SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004836 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004837 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004838 }
4839
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004840 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004841 if (SID == 0 && Chain) {
4842 // This might trigger a ReadSelector callback, which will set the ID for
4843 // this selector.
4844 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004845 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004846 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004847 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004848 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004849 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004850 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004851 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004852}
4853
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004854void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004855 AddDeclRef(Temp->getDestructor(), Record);
4856}
4857
Richard Smithc2bb8182015-03-24 06:36:48 +00004858void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4859 RecordDataImpl &Record) {
4860 assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4861 CXXCtorInitializersToWrite.push_back(
4862 QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4863 Record.push_back(NextCXXCtorInitializersID++);
4864}
4865
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004866void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
Richard Smithc2bb8182015-03-24 06:36:48 +00004867 CXXBaseSpecifier const *BasesEnd,
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004868 RecordDataImpl &Record) {
4869 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4870 CXXBaseSpecifiersToWrite.push_back(
4871 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4872 Bases, BasesEnd));
4873 Record.push_back(NextCXXBaseSpecifiersID++);
4874}
4875
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004876void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004877 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004878 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004879 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004880 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004881 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004882 break;
4883 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004884 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004885 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004886 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004887 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004888 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004889 break;
4890 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004891 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004892 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004893 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004894 break;
John McCall0ad16662009-10-29 08:12:44 +00004895 case TemplateArgument::Null:
4896 case TemplateArgument::Integral:
4897 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004898 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004899 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004900 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004901 break;
4902 }
4903}
4904
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004905void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004906 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004907 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004908
4909 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4910 bool InfoHasSameExpr
4911 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4912 Record.push_back(InfoHasSameExpr);
4913 if (InfoHasSameExpr)
4914 return; // Avoid storing the same expr twice.
4915 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004916 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4917 Record);
4918}
4919
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004920void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4921 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004922 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004923 AddTypeRef(QualType(), Record);
4924 return;
4925 }
4926
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004927 AddTypeLoc(TInfo->getTypeLoc(), Record);
4928}
4929
4930void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4931 AddTypeRef(TL.getType(), Record);
4932
John McCall8f115c62009-10-16 21:56:05 +00004933 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004934 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004935 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004936}
4937
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004938void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004939 Record.push_back(GetOrCreateTypeID(T));
4940}
4941
Richard Smith2095ffe2015-03-16 20:11:03 +00004942TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004943 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004944 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4945 if (T.isNull())
4946 return TypeIdx();
4947 assert(!T.getLocalFastQualifiers());
4948
4949 TypeIdx &Idx = TypeIdxs[T];
4950 if (Idx.getIndex() == 0) {
4951 if (DoneWritingDeclsAndTypes) {
4952 assert(0 && "New type seen after serializing all the types to emit!");
4953 return TypeIdx();
4954 }
4955
4956 // We haven't seen this type before. Assign it a new ID and put it
4957 // into the queue of types to emit.
4958 Idx = TypeIdx(NextTypeID++);
4959 DeclTypesToEmit.push(T);
4960 }
4961 return Idx;
4962 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004963}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004964
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004965TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004966 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004967 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4968 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004969 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00004970 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004971
Richard Smith2095ffe2015-03-16 20:11:03 +00004972 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4973 assert(I != TypeIdxs.end() && "Type not emitted!");
4974 return I->second;
4975 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004976}
4977
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004978void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004979 Record.push_back(GetDeclRef(D));
4980}
4981
Sebastian Redl539c5062010-08-18 23:57:32 +00004982DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004983 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00004984
4985 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004986 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004987 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004988
4989 // If D comes from an AST file, its declaration ID is already known and
4990 // fixed.
4991 if (D->isFromASTFile())
4992 return D->getGlobalID();
4993
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004994 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004995 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004996 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004997 if (DoneWritingDeclsAndTypes) {
4998 assert(0 && "New decl seen after serializing all the decls to emit!");
4999 return 0;
5000 }
5001
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005002 // We haven't seen this declaration before. Give it a new ID and
5003 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005004 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005005 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005006 }
5007
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005008 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005009}
5010
Sebastian Redl539c5062010-08-18 23:57:32 +00005011DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005012 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005013 return 0;
5014
Douglas Gregorb3163e52012-01-05 22:33:30 +00005015 // If D comes from an AST file, its declaration ID is already known and
5016 // fixed.
5017 if (D->isFromASTFile())
5018 return D->getGlobalID();
5019
Douglas Gregore84a9da2009-04-20 20:36:09 +00005020 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5021 return DeclIDs[D];
5022}
5023
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005024void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005025 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005026 assert(D);
5027
5028 SourceLocation Loc = D->getLocation();
5029 if (Loc.isInvalid())
5030 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005031
5032 // We only keep track of the file-level declarations of each file.
5033 if (!D->getLexicalDeclContext()->isFileContext())
5034 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005035 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5036 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005037 if (isa<ParmVarDecl>(D))
5038 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005039
5040 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005041 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005042 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005043 FileID FID;
5044 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005045 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005046 if (FID.isInvalid())
5047 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005048 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005049
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005050 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005051 if (!Info)
5052 Info = new DeclIDInFileInfo();
5053
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005054 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005055 LocDeclIDsTy &Decls = Info->DeclIDs;
5056
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005057 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005058 Decls.push_back(LocDecl);
5059 return;
5060 }
5061
Benjamin Kramer45025c02013-08-24 13:22:59 +00005062 LocDeclIDsTy::iterator I =
5063 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005064
5065 Decls.insert(I, LocDecl);
5066}
5067
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005068void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005069 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005070 Record.push_back(Name.getNameKind());
5071 switch (Name.getNameKind()) {
5072 case DeclarationName::Identifier:
5073 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5074 break;
5075
5076 case DeclarationName::ObjCZeroArgSelector:
5077 case DeclarationName::ObjCOneArgSelector:
5078 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005079 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005080 break;
5081
5082 case DeclarationName::CXXConstructorName:
5083 case DeclarationName::CXXDestructorName:
5084 case DeclarationName::CXXConversionFunctionName:
5085 AddTypeRef(Name.getCXXNameType(), Record);
5086 break;
5087
5088 case DeclarationName::CXXOperatorName:
5089 Record.push_back(Name.getCXXOverloadedOperator());
5090 break;
5091
Alexis Hunt3d221f22009-11-29 07:34:05 +00005092 case DeclarationName::CXXLiteralOperatorName:
5093 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5094 break;
5095
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005096 case DeclarationName::CXXUsingDirective:
5097 // No extra data to emit
5098 break;
5099 }
5100}
Chris Lattnerca025db2010-05-07 21:43:38 +00005101
Richard Smithd08aeb62014-08-28 01:33:39 +00005102unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5103 assert(needsAnonymousDeclarationNumber(D) &&
5104 "expected an anonymous declaration");
5105
5106 // Number the anonymous declarations within this context, if we've not
5107 // already done so.
5108 auto It = AnonymousDeclarationNumbers.find(D);
5109 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005110 auto *DC = D->getLexicalDeclContext();
5111 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5112 AnonymousDeclarationNumbers[ND] = Number;
5113 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005114
5115 It = AnonymousDeclarationNumbers.find(D);
5116 assert(It != AnonymousDeclarationNumbers.end() &&
5117 "declaration not found within its lexical context");
5118 }
5119
5120 return It->second;
5121}
5122
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005123void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005124 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005125 switch (Name.getNameKind()) {
5126 case DeclarationName::CXXConstructorName:
5127 case DeclarationName::CXXDestructorName:
5128 case DeclarationName::CXXConversionFunctionName:
5129 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5130 break;
5131
5132 case DeclarationName::CXXOperatorName:
5133 AddSourceLocation(
5134 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5135 Record);
5136 AddSourceLocation(
5137 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5138 Record);
5139 break;
5140
5141 case DeclarationName::CXXLiteralOperatorName:
5142 AddSourceLocation(
5143 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5144 Record);
5145 break;
5146
5147 case DeclarationName::Identifier:
5148 case DeclarationName::ObjCZeroArgSelector:
5149 case DeclarationName::ObjCOneArgSelector:
5150 case DeclarationName::ObjCMultiArgSelector:
5151 case DeclarationName::CXXUsingDirective:
5152 break;
5153 }
5154}
5155
5156void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005157 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005158 AddDeclarationName(NameInfo.getName(), Record);
5159 AddSourceLocation(NameInfo.getLoc(), Record);
5160 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5161}
5162
5163void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005164 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005165 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005166 Record.push_back(Info.NumTemplParamLists);
5167 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5168 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5169}
5170
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005171void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005172 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005173 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005174 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005175 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005176
5177 // Push each of the NNS's onto a stack for serialization in reverse order.
5178 while (NNS) {
5179 NestedNames.push_back(NNS);
5180 NNS = NNS->getPrefix();
5181 }
5182
5183 Record.push_back(NestedNames.size());
5184 while(!NestedNames.empty()) {
5185 NNS = NestedNames.pop_back_val();
5186 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5187 Record.push_back(Kind);
5188 switch (Kind) {
5189 case NestedNameSpecifier::Identifier:
5190 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5191 break;
5192
5193 case NestedNameSpecifier::Namespace:
5194 AddDeclRef(NNS->getAsNamespace(), Record);
5195 break;
5196
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005197 case NestedNameSpecifier::NamespaceAlias:
5198 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5199 break;
5200
Chris Lattnerca025db2010-05-07 21:43:38 +00005201 case NestedNameSpecifier::TypeSpec:
5202 case NestedNameSpecifier::TypeSpecWithTemplate:
5203 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5204 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5205 break;
5206
5207 case NestedNameSpecifier::Global:
5208 // Don't need to write an associated value.
5209 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005210
5211 case NestedNameSpecifier::Super:
5212 AddDeclRef(NNS->getAsRecordDecl(), Record);
5213 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005214 }
5215 }
5216}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005217
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005218void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5219 RecordDataImpl &Record) {
5220 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005221 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005222 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005223
5224 // Push each of the nested-name-specifiers's onto a stack for
5225 // serialization in reverse order.
5226 while (NNS) {
5227 NestedNames.push_back(NNS);
5228 NNS = NNS.getPrefix();
5229 }
5230
5231 Record.push_back(NestedNames.size());
5232 while(!NestedNames.empty()) {
5233 NNS = NestedNames.pop_back_val();
5234 NestedNameSpecifier::SpecifierKind Kind
5235 = NNS.getNestedNameSpecifier()->getKind();
5236 Record.push_back(Kind);
5237 switch (Kind) {
5238 case NestedNameSpecifier::Identifier:
5239 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5240 AddSourceRange(NNS.getLocalSourceRange(), Record);
5241 break;
5242
5243 case NestedNameSpecifier::Namespace:
5244 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5245 AddSourceRange(NNS.getLocalSourceRange(), Record);
5246 break;
5247
5248 case NestedNameSpecifier::NamespaceAlias:
5249 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5250 AddSourceRange(NNS.getLocalSourceRange(), Record);
5251 break;
5252
5253 case NestedNameSpecifier::TypeSpec:
5254 case NestedNameSpecifier::TypeSpecWithTemplate:
5255 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5256 AddTypeLoc(NNS.getTypeLoc(), Record);
5257 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5258 break;
5259
5260 case NestedNameSpecifier::Global:
5261 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5262 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005263
5264 case NestedNameSpecifier::Super:
5265 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5266 AddSourceRange(NNS.getLocalSourceRange(), Record);
5267 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005268 }
5269 }
5270}
5271
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005272void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005273 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005274 Record.push_back(Kind);
5275 switch (Kind) {
5276 case TemplateName::Template:
5277 AddDeclRef(Name.getAsTemplateDecl(), Record);
5278 break;
5279
5280 case TemplateName::OverloadedTemplate: {
5281 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5282 Record.push_back(OvT->size());
5283 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5284 I != E; ++I)
5285 AddDeclRef(*I, Record);
5286 break;
5287 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005288
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005289 case TemplateName::QualifiedTemplate: {
5290 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5291 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5292 Record.push_back(QualT->hasTemplateKeyword());
5293 AddDeclRef(QualT->getTemplateDecl(), Record);
5294 break;
5295 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005296
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005297 case TemplateName::DependentTemplate: {
5298 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5299 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5300 Record.push_back(DepT->isIdentifier());
5301 if (DepT->isIdentifier())
5302 AddIdentifierRef(DepT->getIdentifier(), Record);
5303 else
5304 Record.push_back(DepT->getOperator());
5305 break;
5306 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005307
5308 case TemplateName::SubstTemplateTemplateParm: {
5309 SubstTemplateTemplateParmStorage *subst
5310 = Name.getAsSubstTemplateTemplateParm();
5311 AddDeclRef(subst->getParameter(), Record);
5312 AddTemplateName(subst->getReplacement(), Record);
5313 break;
5314 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005315
5316 case TemplateName::SubstTemplateTemplateParmPack: {
5317 SubstTemplateTemplateParmPackStorage *SubstPack
5318 = Name.getAsSubstTemplateTemplateParmPack();
5319 AddDeclRef(SubstPack->getParameterPack(), Record);
5320 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5321 break;
5322 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005323 }
5324}
5325
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005326void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005327 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005328 Record.push_back(Arg.getKind());
5329 switch (Arg.getKind()) {
5330 case TemplateArgument::Null:
5331 break;
5332 case TemplateArgument::Type:
5333 AddTypeRef(Arg.getAsType(), Record);
5334 break;
5335 case TemplateArgument::Declaration:
5336 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005337 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005338 break;
5339 case TemplateArgument::NullPtr:
5340 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005341 break;
5342 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005343 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005344 AddTypeRef(Arg.getIntegralType(), Record);
5345 break;
5346 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005347 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5348 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005349 case TemplateArgument::TemplateExpansion:
5350 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005351 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005352 Record.push_back(*NumExpansions + 1);
5353 else
5354 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005355 break;
5356 case TemplateArgument::Expression:
5357 AddStmt(Arg.getAsExpr());
5358 break;
5359 case TemplateArgument::Pack:
5360 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005361 for (const auto &P : Arg.pack_elements())
5362 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005363 break;
5364 }
5365}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005366
5367void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005368ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005369 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005370 assert(TemplateParams && "No TemplateParams!");
5371 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5372 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5373 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5374 Record.push_back(TemplateParams->size());
5375 for (TemplateParameterList::const_iterator
5376 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5377 P != PEnd; ++P)
5378 AddDeclRef(*P, Record);
5379}
5380
5381/// \brief Emit a template argument list.
5382void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005383ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005384 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005385 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005386 Record.push_back(TemplateArgs->size());
5387 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005388 AddTemplateArgument(TemplateArgs->get(i), Record);
5389}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005390
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005391void
5392ASTWriter::AddASTTemplateArgumentListInfo
5393(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5394 assert(ASTTemplArgList && "No ASTTemplArgList!");
5395 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5396 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5397 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5398 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5399 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5400 AddTemplateArgumentLoc(TemplArgs[i], Record);
5401}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005402
5403void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005404ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005405 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005406 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005407 I = Set.begin(), E = Set.end(); I != E; ++I) {
5408 AddDeclRef(I.getDecl(), Record);
5409 Record.push_back(I.getAccess());
5410 }
5411}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005412
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005413void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005414 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005415 Record.push_back(Base.isVirtual());
5416 Record.push_back(Base.isBaseOfClass());
5417 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005418 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005419 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005420 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005421 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5422 : SourceLocation(),
5423 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005424}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005425
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005426void ASTWriter::FlushCXXBaseSpecifiers() {
5427 RecordData Record;
Richard Smithc2bb8182015-03-24 06:36:48 +00005428 unsigned N = CXXBaseSpecifiersToWrite.size();
5429 for (unsigned I = 0; I != N; ++I) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005430 Record.clear();
5431
5432 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005433 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005434 if (Index == CXXBaseSpecifiersOffsets.size())
5435 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5436 else {
5437 if (Index > CXXBaseSpecifiersOffsets.size())
5438 CXXBaseSpecifiersOffsets.resize(Index + 1);
5439 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5440 }
5441
5442 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5443 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5444 Record.push_back(BEnd - B);
5445 for (; B != BEnd; ++B)
5446 AddCXXBaseSpecifier(*B, Record);
5447 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005448
5449 // Flush any expressions that were written as part of the base specifiers.
5450 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005451 }
5452
Richard Smithc2bb8182015-03-24 06:36:48 +00005453 assert(N == CXXBaseSpecifiersToWrite.size() &&
5454 "added more base specifiers while writing base specifiers");
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005455 CXXBaseSpecifiersToWrite.clear();
5456}
5457
Alexis Hunt1d792652011-01-08 20:30:50 +00005458void ASTWriter::AddCXXCtorInitializers(
5459 const CXXCtorInitializer * const *CtorInitializers,
5460 unsigned NumCtorInitializers,
5461 RecordDataImpl &Record) {
5462 Record.push_back(NumCtorInitializers);
5463 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5464 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005465
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005466 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005467 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005468 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005469 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005470 } else if (Init->isDelegatingInitializer()) {
5471 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005472 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005473 } else if (Init->isMemberInitializer()){
5474 Record.push_back(CTOR_INITIALIZER_MEMBER);
5475 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005476 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005477 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5478 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005479 }
Francois Pichetd583da02010-12-04 09:14:42 +00005480
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005481 AddSourceLocation(Init->getMemberLocation(), Record);
5482 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005483 AddSourceLocation(Init->getLParenLoc(), Record);
5484 AddSourceLocation(Init->getRParenLoc(), Record);
5485 Record.push_back(Init->isWritten());
5486 if (Init->isWritten()) {
5487 Record.push_back(Init->getSourceOrder());
5488 } else {
5489 Record.push_back(Init->getNumArrayIndices());
5490 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5491 AddDeclRef(Init->getArrayIndex(i), Record);
5492 }
5493 }
5494}
5495
Richard Smithc2bb8182015-03-24 06:36:48 +00005496void ASTWriter::FlushCXXCtorInitializers() {
5497 RecordData Record;
5498
5499 unsigned N = CXXCtorInitializersToWrite.size();
Daniel Jasperc77b0a12015-03-24 08:06:38 +00005500 (void)N; // Silence unused warning in non-assert builds.
Richard Smithc2bb8182015-03-24 06:36:48 +00005501 for (auto &Init : CXXCtorInitializersToWrite) {
5502 Record.clear();
5503
5504 // Record the offset of this mem-initializer list.
5505 unsigned Index = Init.ID - 1;
5506 if (Index == CXXCtorInitializersOffsets.size())
5507 CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5508 else {
5509 if (Index > CXXCtorInitializersOffsets.size())
5510 CXXCtorInitializersOffsets.resize(Index + 1);
5511 CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5512 }
5513
5514 AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5515 Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5516
5517 // Flush any expressions that were written as part of the initializers.
5518 FlushStmts();
5519 }
5520
5521 assert(N == CXXCtorInitializersToWrite.size() &&
5522 "added more ctor initializers while writing ctor initializers");
5523 CXXCtorInitializersToWrite.clear();
5524}
5525
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005526void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005527 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005528 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005529 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005530 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005531 Record.push_back(Data.Aggregate);
5532 Record.push_back(Data.PlainOldData);
5533 Record.push_back(Data.Empty);
5534 Record.push_back(Data.Polymorphic);
5535 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005536 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005537 Record.push_back(Data.HasNoNonEmptyBases);
5538 Record.push_back(Data.HasPrivateFields);
5539 Record.push_back(Data.HasProtectedFields);
5540 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005541 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005542 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005543 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005544 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005545 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005546 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5547 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5548 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5549 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5550 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5551 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005552 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005553 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005554 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005555 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005556 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005557 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005558 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005559 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005560 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005561 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005562 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5563 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5564 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5565 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005566 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005567
5568 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005569 if (Data.NumBases > 0)
5570 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5571 Record);
5572
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005573 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5574 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005575 if (Data.NumVBases > 0)
5576 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5577 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005578
Richard Smitha4ba74c2013-08-30 04:46:40 +00005579 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5580 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005581 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005582 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005583
5584 // Add lambda-specific data.
5585 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005586 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005587 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005588 Record.push_back(Lambda.IsGenericLambda);
5589 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005590 Record.push_back(Lambda.NumCaptures);
5591 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005592 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005593 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005594 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005595 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005596 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005597 AddSourceLocation(Capture.getLocation(), Record);
5598 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005599 Record.push_back(Capture.getCaptureKind());
5600 switch (Capture.getCaptureKind()) {
5601 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005602 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005603 break;
5604 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005605 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005606 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005607 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005608 AddDeclRef(Var, Record);
5609 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5610 : SourceLocation(),
5611 Record);
5612 break;
5613 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005614 }
5615 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005616}
5617
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005618void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005619 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005620 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005621 assert(FirstDeclID == NextDeclID &&
5622 FirstTypeID == NextTypeID &&
5623 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005624 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005625 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005626 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005627 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005628
Sebastian Redl07a89a82010-07-30 00:29:29 +00005629 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005630
Richard Smith7f330cd2015-03-18 01:42:29 +00005631 // Note, this will get called multiple times, once one the reader starts up
5632 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005633 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5634 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5635 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005636 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005637 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005638 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005639 NextDeclID = FirstDeclID;
5640 NextTypeID = FirstTypeID;
5641 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005642 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005643 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005644 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005645}
5646
Sebastian Redl539c5062010-08-18 23:57:32 +00005647void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005648 // Always keep the highest ID. See \p TypeRead() for more information.
5649 IdentID &StoredID = IdentifierIDs[II];
5650 if (ID > StoredID)
5651 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005652}
5653
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005654void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005655 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005656 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005657 if (ID > StoredID)
5658 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005659}
5660
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005661void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005662 // Always take the highest-numbered type index. This copes with an interesting
5663 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005664 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005665 // keep the higher-numbered entry so that we can properly write it out to
5666 // the AST file.
5667 TypeIdx &StoredIdx = TypeIdxs[T];
5668 if (Idx.getIndex() >= StoredIdx.getIndex())
5669 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005670}
5671
Sebastian Redl539c5062010-08-18 23:57:32 +00005672void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005673 // Always keep the highest ID. See \p TypeRead() for more information.
5674 SelectorID &StoredID = SelectorIDs[S];
5675 if (ID > StoredID)
5676 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005677}
Douglas Gregor91096292010-10-02 19:29:26 +00005678
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005679void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005680 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005681 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005682 MacroDefinitions[MD] = ID;
5683}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005684
Douglas Gregore37a85a2011-12-02 17:30:13 +00005685void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5686 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5687 SubmoduleIDs[Mod] = ID;
5688}
5689
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005690void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005691 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005692 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005693 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5694 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005695 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005696 // A forward reference was mutated into a definition. Rewrite it.
5697 // FIXME: This happens during template instantiation, should we
5698 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005699 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5700 "completed a tag from another module but not by instantiation?");
5701 DeclUpdates[RD].push_back(
5702 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005703 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005704 }
5705}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005706
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005707void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5708 // TU and namespaces are handled elsewhere.
5709 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5710 return;
5711
Douglas Gregorb3722e22011-09-09 23:01:35 +00005712 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005713 return; // Not a source decl added to a DeclContext from PCH.
5714
Richard Smith0f4e2c42015-08-06 04:23:48 +00005715 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005716 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005717 assert(!WritingAST && "Already writing the AST!");
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005718 UpdatedDeclContexts.insert(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005719 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005720}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005721
5722void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5723 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005724 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005725 return; // Not a source member added to a class from PCH.
5726 if (!isa<CXXMethodDecl>(D))
5727 return; // We are interested in lazily declared implicit methods.
5728
5729 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005730 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005731 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005732 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005733}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005734
5735void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5736 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005737 // The specializations set is kept in the canonical template.
5738 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005739 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005740 return; // Not a source specialization added to a template from PCH.
5741
Richard Smithe9a8bc32014-09-30 00:45:29 +00005742 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005743 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5744 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005745}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005746
Larisse Voufo39a1e502013-08-06 01:03:05 +00005747void ASTWriter::AddedCXXTemplateSpecialization(
5748 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5749 // The specializations set is kept in the canonical template.
Larisse Voufo39a1e502013-08-06 01:03:05 +00005750 TD = TD->getCanonicalDecl();
5751 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5752 return; // Not a source specialization added to a template from PCH.
5753
Richard Smithe9a8bc32014-09-30 00:45:29 +00005754 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005755 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5756 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005757}
5758
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005759void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5760 const FunctionDecl *D) {
5761 // The specializations set is kept in the canonical template.
5762 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005763 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005764 return; // Not a source specialization added to a template from PCH.
5765
Richard Smithe9a8bc32014-09-30 00:45:29 +00005766 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005767 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5768 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005769}
5770
Richard Smith564417a2014-03-20 21:47:22 +00005771void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005772 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5773 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005774 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005775 // If we don't already know the exception specification for this redecl
5776 // chain, add an update record for it.
5777 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5778 ->getType()
5779 ->castAs<FunctionProtoType>()
5780 ->getExceptionSpecType()))
5781 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5782 });
Richard Smith564417a2014-03-20 21:47:22 +00005783}
5784
Richard Smith1fa5d642013-05-11 05:45:24 +00005785void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5786 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005787 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005788 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005789 DeclUpdates[D].push_back(
5790 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5791 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005792}
5793
Richard Smithf8134002015-03-10 01:41:22 +00005794void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5795 const FunctionDecl *Delete) {
5796 assert(!WritingAST && "Already writing the AST!");
5797 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005798 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005799 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005800 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5801 });
Richard Smithf8134002015-03-10 01:41:22 +00005802}
5803
Sebastian Redlab238a72011-04-24 16:28:06 +00005804void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005805 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005806 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005807 return; // Declaration not imported from PCH.
5808
Richard Smith4d235792014-08-07 18:53:08 +00005809 // Implicit function decl from a PCH was defined.
5810 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005811}
5812
Richard Smithd28ac5b2014-03-22 23:33:22 +00005813void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5814 assert(!WritingAST && "Already writing the AST!");
5815 if (!D->isFromASTFile())
5816 return;
5817
Richard Smith9e2341d2015-03-23 03:25:59 +00005818 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005819}
5820
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005821void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005822 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005823 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005824 return;
5825
5826 // Since the actual instantiation is delayed, this really means that we need
5827 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005828 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005829 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5830 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005831}
5832
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005833void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5834 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005835 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005836 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005837 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005838
5839 assert(IFD->getDefinition() && "Category on a class without a definition?");
5840 ObjCClassesWithCategories.insert(
5841 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005842}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005843
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005844
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005845void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5846 const ObjCPropertyDecl *OrigProp,
5847 const ObjCCategoryDecl *ClassExt) {
5848 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5849 if (!D)
5850 return;
5851
5852 assert(!WritingAST && "Already writing the AST!");
5853 if (!D->isFromASTFile())
5854 return; // Declaration not imported from PCH.
5855
5856 RewriteDecl(D);
5857}
Eli Friedman276dd182013-09-05 00:02:25 +00005858
5859void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5860 assert(!WritingAST && "Already writing the AST!");
5861 if (!D->isFromASTFile())
5862 return;
5863
Aaron Ballman4f45b712014-03-21 15:22:56 +00005864 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005865}
Alexey Bataev97720002014-11-11 04:05:39 +00005866
5867void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5868 assert(!WritingAST && "Already writing the AST!");
5869 if (!D->isFromASTFile())
5870 return;
5871
5872 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5873}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005874
Richard Smith4caa4492015-05-15 02:34:32 +00005875void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00005876 assert(!WritingAST && "Already writing the AST!");
5877 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005878 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005879}
Alex Denisovfde64952015-06-26 05:28:36 +00005880
5881void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5882 const RecordDecl *Record) {
5883 assert(!WritingAST && "Already writing the AST!");
5884 if (!Record->isFromASTFile())
5885 return;
5886 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5887}