blob: e992685a83db235c8d962b50e75794f41d42f9d0 [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>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000063static StringRef data(const std::vector<T, Allocator> &v) {
64 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>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000070static StringRef data(const SmallVectorImpl<T> &v) {
71 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 };
101}
102
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 Gregoref84c4b2009-04-09 22:27:44 +0000424 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000425 for (const auto *I : T->quals())
426 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000427 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000428}
429
Steve Narofffb4330f2009-06-17 22:40:22 +0000430void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000431ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000432 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000433 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000434}
435
Eli Friedman0dfb8892011-10-06 23:00:33 +0000436void
437ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
438 Writer.AddTypeRef(T->getValueType(), Record);
439 Code = TYPE_ATOMIC;
440}
441
John McCall8f115c62009-10-16 21:56:05 +0000442namespace {
443
444class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000445 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000446 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000447
448public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000449 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000450 : Writer(Writer), Record(Record) { }
451
John McCall17001972009-10-18 01:05:36 +0000452#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000453#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000454 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000455#include "clang/AST/TypeLocNodes.def"
456
John McCall17001972009-10-18 01:05:36 +0000457 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
458 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000459};
460
461}
462
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
464 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000465}
John McCall17001972009-10-18 01:05:36 +0000466void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000467 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
468 if (TL.needsExtraLocalData()) {
469 Record.push_back(TL.getWrittenTypeSpec());
470 Record.push_back(TL.getWrittenSignSpec());
471 Record.push_back(TL.getWrittenWidthSpec());
472 Record.push_back(TL.hasModeAttr());
473 }
John McCall8f115c62009-10-16 21:56:05 +0000474}
John McCall17001972009-10-18 01:05:36 +0000475void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000477}
John McCall17001972009-10-18 01:05:36 +0000478void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000480}
Reid Kleckner8a365022013-06-24 17:51:48 +0000481void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
482 // nothing to do
483}
Reid Kleckner0503a872013-12-05 01:23:43 +0000484void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
485 // nothing to do
486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000489}
John McCall17001972009-10-18 01:05:36 +0000490void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000492}
John McCall17001972009-10-18 01:05:36 +0000493void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000495}
John McCall17001972009-10-18 01:05:36 +0000496void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000498 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
502 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
503 Record.push_back(TL.getSizeExpr() ? 1 : 0);
504 if (TL.getSizeExpr())
505 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000506}
John McCall17001972009-10-18 01:05:36 +0000507void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
508 VisitArrayTypeLoc(TL);
509}
510void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
511 VisitArrayTypeLoc(TL);
512}
513void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
514 VisitArrayTypeLoc(TL);
515}
516void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
517 DependentSizedArrayTypeLoc TL) {
518 VisitArrayTypeLoc(TL);
519}
520void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
521 DependentSizedExtVectorTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
527void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000531 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000532 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000534 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000535 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
536 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000537}
538void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
539 VisitFunctionTypeLoc(TL);
540}
541void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
542 VisitFunctionTypeLoc(TL);
543}
John McCallb96ec562009-12-04 22:46:56 +0000544void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
John McCall17001972009-10-18 01:05:36 +0000547void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
550void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000551 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
552 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
553 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000554}
555void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000556 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
557 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
558 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
559 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000560}
561void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
Alexis Hunte852b102011-05-24 22:41:36 +0000564void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getKWLoc(), Record);
566 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
567 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
568 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
569}
Richard Smith30482bc2011-02-20 03:19:35 +0000570void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall17001972009-10-18 01:05:36 +0000573void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
576void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578}
John McCall81904512011-01-06 01:58:22 +0000579void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
581 if (TL.hasAttrOperand()) {
582 SourceRange range = TL.getAttrOperandParensRange();
583 Writer.AddSourceLocation(range.getBegin(), Record);
584 Writer.AddSourceLocation(range.getEnd(), Record);
585 }
586 if (TL.hasAttrExprOperand()) {
587 Expr *operand = TL.getAttrExprOperand();
588 Record.push_back(operand ? 1 : 0);
589 if (operand) Writer.AddStmt(operand);
590 } else if (TL.hasAttrEnumOperand()) {
591 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
592 }
593}
John McCall17001972009-10-18 01:05:36 +0000594void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getNameLoc(), Record);
596}
John McCallcebee162009-10-18 09:09:24 +0000597void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
598 SubstTemplateTypeParmTypeLoc TL) {
599 Writer.AddSourceLocation(TL.getNameLoc(), Record);
600}
Douglas Gregorada4b792011-01-14 02:55:32 +0000601void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
602 SubstTemplateTypeParmPackTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
604}
John McCall17001972009-10-18 01:05:36 +0000605void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
606 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000607 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000608 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
609 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
610 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
611 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000612 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
613 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000614}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000615void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
617 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
618}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000619void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000620 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000621 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000622}
John McCalle78aac42010-03-10 03:28:59 +0000623void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
624 Writer.AddSourceLocation(TL.getNameLoc(), Record);
625}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000626void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000627 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000628 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000629 Writer.AddSourceLocation(TL.getNameLoc(), Record);
630}
John McCallc392f372010-06-11 00:33:02 +0000631void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
632 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000633 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000634 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000635 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000636 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000637 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
638 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
639 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000640 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
641 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000642}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000643void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
644 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
645}
John McCall17001972009-10-18 01:05:36 +0000646void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
647 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000648}
649void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
650 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000651 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
652 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
653 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
654 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000655}
John McCallfc93cf92009-10-22 22:37:11 +0000656void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
657 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000658}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000659void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
660 Writer.AddSourceLocation(TL.getKWLoc(), Record);
661 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
662 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
663}
John McCall8f115c62009-10-16 21:56:05 +0000664
Richard Smith01b2cb42014-07-26 06:37:51 +0000665void ASTWriter::WriteTypeAbbrevs() {
666 using namespace llvm;
667
668 BitCodeAbbrev *Abv;
669
670 // Abbreviation for TYPE_EXT_QUAL
671 Abv = new BitCodeAbbrev();
672 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
673 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
674 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
675 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
676
677 // Abbreviation for TYPE_FUNCTION_PROTO
678 Abv = new BitCodeAbbrev();
679 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
680 // FunctionType
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
683 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
684 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
685 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
686 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
687 // FunctionProtoType
688 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
689 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
690 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
691 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
692 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
694 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
695 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
696 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
697}
698
Chris Lattner19cea4e2009-04-22 05:57:30 +0000699//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000700// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000701//===----------------------------------------------------------------------===//
702
Chris Lattner28fa4e62009-04-26 22:26:21 +0000703static void EmitBlockID(unsigned ID, const char *Name,
704 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000705 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000706 Record.clear();
707 Record.push_back(ID);
708 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
709
710 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000711 if (!Name || Name[0] == 0)
712 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 Record.clear();
714 while (*Name)
715 Record.push_back(*Name++);
716 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
717}
718
719static void EmitRecordID(unsigned ID, const char *Name,
720 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000721 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000722 Record.clear();
723 Record.push_back(ID);
724 while (*Name)
725 Record.push_back(*Name++);
726 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000727}
728
729static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000730 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000731#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000732 RECORD(STMT_STOP);
733 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000734 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000735 RECORD(STMT_NULL);
736 RECORD(STMT_COMPOUND);
737 RECORD(STMT_CASE);
738 RECORD(STMT_DEFAULT);
739 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000740 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000741 RECORD(STMT_IF);
742 RECORD(STMT_SWITCH);
743 RECORD(STMT_WHILE);
744 RECORD(STMT_DO);
745 RECORD(STMT_FOR);
746 RECORD(STMT_GOTO);
747 RECORD(STMT_INDIRECT_GOTO);
748 RECORD(STMT_CONTINUE);
749 RECORD(STMT_BREAK);
750 RECORD(STMT_RETURN);
751 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000752 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000753 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000754 RECORD(EXPR_PREDEFINED);
755 RECORD(EXPR_DECL_REF);
756 RECORD(EXPR_INTEGER_LITERAL);
757 RECORD(EXPR_FLOATING_LITERAL);
758 RECORD(EXPR_IMAGINARY_LITERAL);
759 RECORD(EXPR_STRING_LITERAL);
760 RECORD(EXPR_CHARACTER_LITERAL);
761 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000762 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000763 RECORD(EXPR_UNARY_OPERATOR);
764 RECORD(EXPR_SIZEOF_ALIGN_OF);
765 RECORD(EXPR_ARRAY_SUBSCRIPT);
766 RECORD(EXPR_CALL);
767 RECORD(EXPR_MEMBER);
768 RECORD(EXPR_BINARY_OPERATOR);
769 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
770 RECORD(EXPR_CONDITIONAL_OPERATOR);
771 RECORD(EXPR_IMPLICIT_CAST);
772 RECORD(EXPR_CSTYLE_CAST);
773 RECORD(EXPR_COMPOUND_LITERAL);
774 RECORD(EXPR_EXT_VECTOR_ELEMENT);
775 RECORD(EXPR_INIT_LIST);
776 RECORD(EXPR_DESIGNATED_INIT);
777 RECORD(EXPR_IMPLICIT_VALUE_INIT);
778 RECORD(EXPR_VA_ARG);
779 RECORD(EXPR_ADDR_LABEL);
780 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000781 RECORD(EXPR_CHOOSE);
782 RECORD(EXPR_GNU_NULL);
783 RECORD(EXPR_SHUFFLE_VECTOR);
784 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000785 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000786 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000787 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000788 RECORD(EXPR_OBJC_ARRAY_LITERAL);
789 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000790 RECORD(EXPR_OBJC_ENCODE);
791 RECORD(EXPR_OBJC_SELECTOR_EXPR);
792 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
793 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
794 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
795 RECORD(EXPR_OBJC_KVC_REF_EXPR);
796 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000797 RECORD(STMT_OBJC_FOR_COLLECTION);
798 RECORD(STMT_OBJC_CATCH);
799 RECORD(STMT_OBJC_FINALLY);
800 RECORD(STMT_OBJC_AT_TRY);
801 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
802 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000803 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000804 RECORD(STMT_CXX_CATCH);
805 RECORD(STMT_CXX_TRY);
806 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000807 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000808 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000809 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000810 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000811 RECORD(EXPR_CXX_STATIC_CAST);
812 RECORD(EXPR_CXX_DYNAMIC_CAST);
813 RECORD(EXPR_CXX_REINTERPRET_CAST);
814 RECORD(EXPR_CXX_CONST_CAST);
815 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000816 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000817 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000818 RECORD(EXPR_CXX_BOOL_LITERAL);
819 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000820 RECORD(EXPR_CXX_TYPEID_EXPR);
821 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000822 RECORD(EXPR_CXX_THIS);
823 RECORD(EXPR_CXX_THROW);
824 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000825 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000826 RECORD(EXPR_CXX_BIND_TEMPORARY);
827 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
828 RECORD(EXPR_CXX_NEW);
829 RECORD(EXPR_CXX_DELETE);
830 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
831 RECORD(EXPR_EXPR_WITH_CLEANUPS);
832 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
833 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
834 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
835 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
836 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000837 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000838 RECORD(EXPR_CXX_NOEXCEPT);
839 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000840 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
841 RECORD(EXPR_TYPE_TRAIT);
842 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000843 RECORD(EXPR_PACK_EXPANSION);
844 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000845 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000846 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000847 RECORD(EXPR_FUNCTION_PARM_PACK);
848 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000849 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000850 RECORD(EXPR_CXX_UUIDOF_EXPR);
851 RECORD(EXPR_CXX_UUIDOF_TYPE);
852 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000853#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000854}
Mike Stump11289f42009-09-09 15:08:12 +0000855
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000856void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000857 RecordData Record;
858 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000859
Sebastian Redl539c5062010-08-18 23:57:32 +0000860#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
861#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000862
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000863 // Control Block.
864 BLOCK(CONTROL_BLOCK);
865 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000866 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000867 RECORD(MODULE_NAME);
868 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000869 RECORD(IMPORTS);
Richard Smith7f330cd2015-03-18 01:42:29 +0000870 RECORD(KNOWN_MODULE_FILES);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000871 RECORD(LANGUAGE_OPTIONS);
872 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000873 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000874 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000875 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000876 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000877 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000878 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000879 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000880 RECORD(PREPROCESSOR_OPTIONS);
881
Douglas Gregor108cb222012-10-19 00:45:00 +0000882 BLOCK(INPUT_FILES_BLOCK);
883 RECORD(INPUT_FILE);
884
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000885 // AST Top-Level Block.
886 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000887 RECORD(TYPE_OFFSET);
888 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000889 RECORD(IDENTIFIER_OFFSET);
890 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000891 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000892 RECORD(SPECIAL_TYPES);
893 RECORD(STATISTICS);
894 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000895 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000896 RECORD(SELECTOR_OFFSETS);
897 RECORD(METHOD_POOL);
898 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000899 RECORD(SOURCE_LOCATION_OFFSETS);
900 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000901 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000902 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000903 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000904 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000905 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000906 RECORD(SEMA_DECL_REFS);
907 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
908 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
909 RECORD(DECL_REPLACEMENTS);
910 RECORD(UPDATE_VISIBLE);
911 RECORD(DECL_UPDATE_OFFSETS);
912 RECORD(DECL_UPDATES);
913 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
914 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000915 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000916 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000917 RECORD(FP_PRAGMA_OPTIONS);
918 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000919 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000920 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000921 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000922 RECORD(MODULE_OFFSET_MAP);
923 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000924 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000925 RECORD(FILE_SORTED_DECLS);
926 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000927 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000928 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000929 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000930 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000931 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000932 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor358cd442012-01-15 16:58:34 +0000933
Chris Lattner28fa4e62009-04-26 22:26:21 +0000934 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000935 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000936 RECORD(SM_SLOC_FILE_ENTRY);
937 RECORD(SM_SLOC_BUFFER_ENTRY);
938 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000939 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000940
Chris Lattner28fa4e62009-04-26 22:26:21 +0000941 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000942 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000943 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000944 RECORD(PP_MACRO_OBJECT_LIKE);
945 RECORD(PP_MACRO_FUNCTION_LIKE);
946 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000947
Douglas Gregor12bfa382009-10-17 00:13:19 +0000948 // Decls and Types block.
949 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000950 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000951 RECORD(TYPE_COMPLEX);
952 RECORD(TYPE_POINTER);
953 RECORD(TYPE_BLOCK_POINTER);
954 RECORD(TYPE_LVALUE_REFERENCE);
955 RECORD(TYPE_RVALUE_REFERENCE);
956 RECORD(TYPE_MEMBER_POINTER);
957 RECORD(TYPE_CONSTANT_ARRAY);
958 RECORD(TYPE_INCOMPLETE_ARRAY);
959 RECORD(TYPE_VARIABLE_ARRAY);
960 RECORD(TYPE_VECTOR);
961 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000962 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000963 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000964 RECORD(TYPE_TYPEDEF);
965 RECORD(TYPE_TYPEOF_EXPR);
966 RECORD(TYPE_TYPEOF);
967 RECORD(TYPE_RECORD);
968 RECORD(TYPE_ENUM);
969 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000970 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000971 RECORD(TYPE_DECLTYPE);
972 RECORD(TYPE_ELABORATED);
973 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
974 RECORD(TYPE_UNRESOLVED_USING);
975 RECORD(TYPE_INJECTED_CLASS_NAME);
976 RECORD(TYPE_OBJC_OBJECT);
977 RECORD(TYPE_TEMPLATE_TYPE_PARM);
978 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
979 RECORD(TYPE_DEPENDENT_NAME);
980 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
981 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
982 RECORD(TYPE_PAREN);
983 RECORD(TYPE_PACK_EXPANSION);
984 RECORD(TYPE_ATTRIBUTED);
985 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000986 RECORD(TYPE_AUTO);
987 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000988 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000989 RECORD(TYPE_DECAYED);
990 RECORD(TYPE_ADJUSTED);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000991 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +0000992 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000993 RECORD(DECL_ENUM);
994 RECORD(DECL_RECORD);
995 RECORD(DECL_ENUM_CONSTANT);
996 RECORD(DECL_FUNCTION);
997 RECORD(DECL_OBJC_METHOD);
998 RECORD(DECL_OBJC_INTERFACE);
999 RECORD(DECL_OBJC_PROTOCOL);
1000 RECORD(DECL_OBJC_IVAR);
1001 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001002 RECORD(DECL_OBJC_CATEGORY);
1003 RECORD(DECL_OBJC_CATEGORY_IMPL);
1004 RECORD(DECL_OBJC_IMPLEMENTATION);
1005 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1006 RECORD(DECL_OBJC_PROPERTY);
1007 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001008 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001009 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001010 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001011 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001012 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001013 RECORD(DECL_FILE_SCOPE_ASM);
1014 RECORD(DECL_BLOCK);
1015 RECORD(DECL_CONTEXT_LEXICAL);
1016 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001017 RECORD(DECL_NAMESPACE);
1018 RECORD(DECL_NAMESPACE_ALIAS);
1019 RECORD(DECL_USING);
1020 RECORD(DECL_USING_SHADOW);
1021 RECORD(DECL_USING_DIRECTIVE);
1022 RECORD(DECL_UNRESOLVED_USING_VALUE);
1023 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1024 RECORD(DECL_LINKAGE_SPEC);
1025 RECORD(DECL_CXX_RECORD);
1026 RECORD(DECL_CXX_METHOD);
1027 RECORD(DECL_CXX_CONSTRUCTOR);
1028 RECORD(DECL_CXX_DESTRUCTOR);
1029 RECORD(DECL_CXX_CONVERSION);
1030 RECORD(DECL_ACCESS_SPEC);
1031 RECORD(DECL_FRIEND);
1032 RECORD(DECL_FRIEND_TEMPLATE);
1033 RECORD(DECL_CLASS_TEMPLATE);
1034 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1035 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001036 RECORD(DECL_VAR_TEMPLATE);
1037 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1038 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001039 RECORD(DECL_FUNCTION_TEMPLATE);
1040 RECORD(DECL_TEMPLATE_TYPE_PARM);
1041 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1042 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1043 RECORD(DECL_STATIC_ASSERT);
1044 RECORD(DECL_CXX_BASE_SPECIFIERS);
1045 RECORD(DECL_INDIRECTFIELD);
1046 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1047
Douglas Gregor03412ba2011-06-03 02:27:19 +00001048 // Statements and Exprs can occur in the Decls and Types block.
1049 AddStmtsExprs(Stream, Record);
1050
Douglas Gregor92a96f52011-02-08 21:58:10 +00001051 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001052 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001053 RECORD(PPD_MACRO_DEFINITION);
1054 RECORD(PPD_INCLUSION_DIRECTIVE);
1055
Chris Lattner28fa4e62009-04-26 22:26:21 +00001056#undef RECORD
1057#undef BLOCK
1058 Stream.ExitBlock();
1059}
1060
Richard Smith54cc3c22014-12-11 20:50:24 +00001061/// \brief Prepares a path for being written to an AST file by converting it
1062/// to an absolute path and removing nested './'s.
1063///
1064/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001065static bool cleanPathForOutput(FileManager &FileMgr,
1066 SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00001067 bool Changed = false;
1068
1069 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
1070 llvm::sys::fs::make_absolute(Path);
1071 Changed = true;
1072 }
1073
1074 return Changed | FileMgr.removeDotPaths(Path);
1075}
1076
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001077/// \brief Adjusts the given filename to only write out the portion of the
1078/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001079///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001080/// \param Filename the file name to adjust.
1081///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001082/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1083/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001084///
1085/// \returns either the original filename (if it needs no adjustment) or the
1086/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001087static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001088adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001089 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001090
Richard Smith7ed1bc92014-12-05 22:42:13 +00001091 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001092 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001093
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001094 // Verify that the filename and the system root have the same prefix.
1095 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001096 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1097 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001098 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001099
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001100 // We hit the end of the filename before we hit the end of the system root.
1101 if (!Filename[Pos])
1102 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001103
Richard Smith7ed1bc92014-12-05 22:42:13 +00001104 // If there's not a path separator at the end of the base directory nor
1105 // immediately after it, then this isn't within the base directory.
1106 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1107 if (!llvm::sys::path::is_separator(BaseDir.back()))
1108 return Filename;
1109 } else {
1110 // If the file name has a '/' at the current position, skip over the '/'.
1111 // We distinguish relative paths from absolute paths by the
1112 // absence of '/' at the beginning of relative paths.
1113 //
1114 // FIXME: This is wrong. We distinguish them by asking if the path is
1115 // absolute, which isn't the same thing. And there might be multiple '/'s
1116 // in a row. Use a better mechanism to indicate whether we have emitted an
1117 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001118 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001121 return Filename + Pos;
1122}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001123
Ben Langmuir487ea142014-10-23 18:05:36 +00001124static ASTFileSignature getSignature() {
1125 while (1) {
1126 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1127 return S;
1128 // Rely on GetRandomNumber to eventually return non-zero...
1129 }
1130}
1131
Douglas Gregor112b9072012-10-18 05:31:06 +00001132/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001133void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1134 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001135 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001136 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001137 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1138 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001139
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001140 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001141 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1142 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1143 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1144 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1145 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1146 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1147 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1148 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1149 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1150 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1151 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001152 Record.push_back(VERSION_MAJOR);
1153 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001154 Record.push_back(CLANG_VERSION_MAJOR);
1155 Record.push_back(CLANG_VERSION_MINOR);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001156 assert((!WritingModule || isysroot.empty()) &&
1157 "writing module as a relocatable PCH?");
Douglas Gregorc567ba22011-07-22 16:35:34 +00001158 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001159 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001160 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1161 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001162
Ben Langmuir487ea142014-10-23 18:05:36 +00001163 // Signature
1164 Record.clear();
1165 Record.push_back(getSignature());
1166 Stream.EmitRecord(SIGNATURE, Record);
1167
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001168 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001169 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001170 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1171 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1173 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1174 RecordData Record;
1175 Record.push_back(MODULE_NAME);
1176 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1177 }
1178
Richard Smith7ed1bc92014-12-05 22:42:13 +00001179 if (WritingModule && WritingModule->Directory) {
1180 // Module directory.
1181 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1182 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1184 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1185 RecordData Record;
1186 Record.push_back(MODULE_DIRECTORY);
1187
1188 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001189 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001190 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1191
1192 // Write out all other paths relative to the base directory if possible.
1193 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1194 } else if (!isysroot.empty()) {
1195 // Write out paths relative to the sysroot if possible.
1196 BaseDirectory = isysroot;
1197 }
1198
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001199 // Module map file
1200 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001201 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001202
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001203 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1204
1205 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001206 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001207
1208 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001209 if (auto *AdditionalModMaps =
1210 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001211 Record.push_back(AdditionalModMaps->size());
1212 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001213 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001214 } else {
1215 Record.push_back(0);
1216 }
1217
1218 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001219 }
1220
Douglas Gregor112b9072012-10-18 05:31:06 +00001221 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001222 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001223 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001224 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001225
Richard Smith7f330cd2015-03-18 01:42:29 +00001226 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001227 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001228 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001229 continue;
1230
Richard Smith7f330cd2015-03-18 01:42:29 +00001231 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1232 AddSourceLocation(M->ImportLoc, Record);
1233 Record.push_back(M->File->getSize());
1234 Record.push_back(M->File->getModificationTime());
1235 Record.push_back(M->Signature);
1236 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001237 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001238 Stream.EmitRecord(IMPORTS, Record);
Richard Smith7f330cd2015-03-18 01:42:29 +00001239
1240 // Also emit a list of known module files that were not imported,
1241 // but are made available by this module.
1242 // FIXME: Should we also include a signature here?
1243 Record.clear();
1244 for (auto *E : Mgr.getAdditionalKnownModuleFiles())
1245 AddPath(E->getName(), Record);
1246 if (!Record.empty())
1247 Stream.EmitRecord(KNOWN_MODULE_FILES, Record);
Douglas Gregor29cc6422011-08-17 21:07:30 +00001248 }
Mike Stump11289f42009-09-09 15:08:12 +00001249
Douglas Gregor112b9072012-10-18 05:31:06 +00001250 // Language options.
1251 Record.clear();
1252 const LangOptions &LangOpts = Context.getLangOpts();
1253#define LANGOPT(Name, Bits, Default, Description) \
1254 Record.push_back(LangOpts.Name);
1255#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1256 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001257#include "clang/Basic/LangOptions.def"
1258#define SANITIZER(NAME, ID) \
1259 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001260#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001261
1262 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1263 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1264
1265 Record.push_back(LangOpts.CurrentModule.size());
1266 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001267
1268 // Comment options.
1269 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1270 for (CommentOptions::BlockCommandNamesTy::const_iterator
1271 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1272 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1273 I != IEnd; ++I) {
1274 AddString(*I, Record);
1275 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001276 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001277
Douglas Gregor112b9072012-10-18 05:31:06 +00001278 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1279
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001280 // Target options.
1281 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001282 const TargetInfo &Target = Context.getTargetInfo();
1283 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001284 AddString(TargetOpts.Triple, Record);
1285 AddString(TargetOpts.CPU, Record);
1286 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001287 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1288 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1289 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1290 }
1291 Record.push_back(TargetOpts.Features.size());
1292 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1293 AddString(TargetOpts.Features[I], Record);
1294 }
1295 Stream.EmitRecord(TARGET_OPTIONS, Record);
1296
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001297 // Diagnostic options.
1298 Record.clear();
1299 const DiagnosticOptions &DiagOpts
1300 = Context.getDiagnostics().getDiagnosticOptions();
1301#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1302#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1303 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1304#include "clang/Basic/DiagnosticOptions.def"
1305 Record.push_back(DiagOpts.Warnings.size());
1306 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1307 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001308 Record.push_back(DiagOpts.Remarks.size());
1309 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1310 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001311 // Note: we don't serialize the log or serialization file names, because they
1312 // are generally transient files and will almost always be overridden.
1313 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1314
Douglas Gregorc6317db2012-10-24 15:49:58 +00001315 // File system options.
1316 Record.clear();
1317 const FileSystemOptions &FSOpts
1318 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1319 AddString(FSOpts.WorkingDir, Record);
1320 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1321
Douglas Gregor2d302362012-10-24 16:50:34 +00001322 // Header search options.
1323 Record.clear();
1324 const HeaderSearchOptions &HSOpts
1325 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1326 AddString(HSOpts.Sysroot, Record);
1327
1328 // Include entries.
1329 Record.push_back(HSOpts.UserEntries.size());
1330 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1331 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1332 AddString(Entry.Path, Record);
1333 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001334 Record.push_back(Entry.IsFramework);
1335 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001336 }
1337
1338 // System header prefixes.
1339 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1340 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1341 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1342 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1343 }
1344
1345 AddString(HSOpts.ResourceDir, Record);
1346 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001347 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001348 Record.push_back(HSOpts.DisableModuleHash);
1349 Record.push_back(HSOpts.UseBuiltinIncludes);
1350 Record.push_back(HSOpts.UseStandardSystemIncludes);
1351 Record.push_back(HSOpts.UseStandardCXXIncludes);
1352 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001353 // Write out the specific module cache path that contains the module files.
1354 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001355 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1356
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001357 // Preprocessor options.
1358 Record.clear();
1359 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1360
1361 // Macro definitions.
1362 Record.push_back(PPOpts.Macros.size());
1363 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1364 AddString(PPOpts.Macros[I].first, Record);
1365 Record.push_back(PPOpts.Macros[I].second);
1366 }
1367
1368 // Includes
1369 Record.push_back(PPOpts.Includes.size());
1370 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1371 AddString(PPOpts.Includes[I], Record);
1372
1373 // Macro includes
1374 Record.push_back(PPOpts.MacroIncludes.size());
1375 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1376 AddString(PPOpts.MacroIncludes[I], Record);
1377
Douglas Gregorb6368752012-10-24 23:41:50 +00001378 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001379 // Detailed record is important since it is used for the module cache hash.
1380 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001381 AddString(PPOpts.ImplicitPCHInclude, Record);
1382 AddString(PPOpts.ImplicitPTHInclude, Record);
1383 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1384 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1385
Douglas Gregora3b20262011-05-06 21:43:30 +00001386 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001387 SourceManager &SM = Context.getSourceManager();
1388 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1389 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001390 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1391 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001392 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1393 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1394
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001395 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001396 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001397 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001398 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001399 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001400
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001401 Record.clear();
1402 Record.push_back(SM.getMainFileID().getOpaqueValue());
1403 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1404
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001405 // Original PCH directory
1406 if (!OutputFile.empty() && OutputFile != "-") {
1407 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1408 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1410 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1411
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001412 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001413
1414 llvm::sys::fs::make_absolute(OutputPath);
1415 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1416
1417 RecordData Record;
1418 Record.push_back(ORIGINAL_PCH_DIR);
1419 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1420 }
1421
Douglas Gregor49491f72013-03-15 22:15:07 +00001422 WriteInputFiles(Context.SourceMgr,
1423 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001424 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001425 Stream.ExitBlock();
1426}
1427
Douglas Gregor49491f72013-03-15 22:15:07 +00001428namespace {
1429 /// \brief An input file.
1430 struct InputFileEntry {
1431 const FileEntry *File;
1432 bool IsSystemFile;
1433 bool BufferOverridden;
1434 };
1435}
1436
1437void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1438 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001439 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001440 using namespace llvm;
1441 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1442 RecordData Record;
1443
1444 // Create input-file abbreviation.
1445 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1446 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001447 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001448 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1449 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001450 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001451 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1452 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1453
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001454 // Get all ContentCache objects for files, sorted by whether the file is a
1455 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001456 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001457 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1458 // Get this source location entry.
1459 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001460 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001461
1462 // We only care about file entries that were not overridden.
1463 if (!SLoc->isFile())
1464 continue;
1465 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001466 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001467 continue;
1468
Douglas Gregor49491f72013-03-15 22:15:07 +00001469 InputFileEntry Entry;
1470 Entry.File = Cache->OrigEntry;
1471 Entry.IsSystemFile = Cache->IsSystemFile;
1472 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001473 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001474 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001475 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001476 SortedFiles.push_front(Entry);
1477 }
1478
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001479 unsigned UserFilesNum = 0;
1480 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001481 std::vector<uint64_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001482 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001483 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001484 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001485
Douglas Gregor49491f72013-03-15 22:15:07 +00001486 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001487 if (InputFileID != 0)
1488 continue; // already recorded this file.
1489
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001490 // Record this entry's offset.
1491 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001492
1493 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001494
Douglas Gregor49491f72013-03-15 22:15:07 +00001495 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001496 ++UserFilesNum;
1497
Douglas Gregor72be3902012-10-19 00:38:02 +00001498 Record.clear();
1499 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001500 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001501
1502 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001503 Record.push_back(Entry.File->getSize());
1504 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001505
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001506 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001507 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001508
Richard Smith7ed1bc92014-12-05 22:42:13 +00001509 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1510 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001511
Douglas Gregor112b9072012-10-18 05:31:06 +00001512 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001513
1514 // Create input file offsets abbreviation.
1515 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1516 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1517 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001518 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1519 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001520 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1521 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1522
1523 // Write input file offsets.
1524 Record.clear();
1525 Record.push_back(INPUT_FILE_OFFSETS);
1526 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001527 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001528 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001529}
1530
Douglas Gregora7f71a92009-04-10 03:52:48 +00001531//===----------------------------------------------------------------------===//
1532// Source Manager Serialization
1533//===----------------------------------------------------------------------===//
1534
1535/// \brief Create an abbreviation for the SLocEntry that refers to a
1536/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001537static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001538 using namespace llvm;
1539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001540 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001545 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001550 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001551}
1552
1553/// \brief Create an abbreviation for the SLocEntry that refers to a
1554/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001555static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001556 using namespace llvm;
1557 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001558 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001564 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001565}
1566
1567/// \brief Create an abbreviation for the SLocEntry that refers to a
1568/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001569static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001570 using namespace llvm;
1571 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001572 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001574 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001575}
1576
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001577/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1578/// expansion.
1579static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001580 using namespace llvm;
1581 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001582 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001588 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001589}
1590
Douglas Gregor09b69892011-02-10 17:09:37 +00001591namespace {
1592 // Trait used for the on-disk hash table of header search information.
1593 class HeaderFileInfoTrait {
1594 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001595 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001596
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001597 // Keep track of the framework names we've used during serialization.
1598 SmallVector<char, 128> FrameworkStringData;
1599 llvm::StringMap<unsigned> FrameworkNameOffset;
1600
Douglas Gregor09b69892011-02-10 17:09:37 +00001601 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001602 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1603 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001604
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001605 struct key_type {
1606 const FileEntry *FE;
1607 const char *Filename;
1608 };
1609 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001610
1611 typedef HeaderFileInfo data_type;
1612 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001613 typedef unsigned hash_value_type;
1614 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001615
Justin Bogner25463f12014-04-18 20:27:24 +00001616 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001617 // The hash is based only on size/time of the file, so that the reader can
1618 // match even when symlinking or excess path elements ("foo/../", "../")
1619 // change the form of the name. However, complete path is still the key.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001620 //
1621 // FIXME: Using the mtime here will cause problems for explicit module
1622 // imports.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001623 return llvm::hash_combine(key.FE->getSize(),
1624 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001625 }
1626
1627 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001628 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001629 using namespace llvm::support;
1630 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001631 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001632 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001633 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001634 if (Data.isModuleHeader)
1635 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001636 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001637 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001638 }
1639
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001640 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001641 using namespace llvm::support;
1642 endian::Writer<little> LE(Out);
1643 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001644 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001645 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001646 KeyLen -= 8;
1647 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001648 }
1649
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001650 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001651 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001652 using namespace llvm::support;
1653 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001654 uint64_t Start = Out.tell(); (void)Start;
1655
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001656 unsigned char Flags = (Data.HeaderRole << 6)
1657 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001658 | (Data.isPragmaOnce << 4)
1659 | (Data.DirInfo << 2)
1660 | (Data.Resolved << 1)
1661 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001662 LE.write<uint8_t>(Flags);
1663 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001664
1665 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001666 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001667 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001668 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001669
1670 unsigned Offset = 0;
1671 if (!Data.Framework.empty()) {
1672 // If this header refers into a framework, save the framework name.
1673 llvm::StringMap<unsigned>::iterator Pos
1674 = FrameworkNameOffset.find(Data.Framework);
1675 if (Pos == FrameworkNameOffset.end()) {
1676 Offset = FrameworkStringData.size() + 1;
1677 FrameworkStringData.append(Data.Framework.begin(),
1678 Data.Framework.end());
1679 FrameworkStringData.push_back(0);
1680
1681 FrameworkNameOffset[Data.Framework] = Offset;
1682 } else
1683 Offset = Pos->second;
1684 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001685 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001686
1687 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001688 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001689 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001690 }
1691
Douglas Gregor09b69892011-02-10 17:09:37 +00001692 assert(Out.tell() - Start == DataLen && "Wrong data length");
1693 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001694
1695 const char *strings_begin() const { return FrameworkStringData.begin(); }
1696 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001697 };
1698} // end anonymous namespace
1699
1700/// \brief Write the header search block for the list of files that
1701///
1702/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001703void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001704 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001705 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1706
1707 if (FilesByUID.size() > HS.header_file_size())
1708 FilesByUID.resize(HS.header_file_size());
1709
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001710 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001711 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001712 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001713 unsigned NumHeaderSearchEntries = 0;
1714 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1715 const FileEntry *File = FilesByUID[UID];
1716 if (!File)
1717 continue;
1718
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001719 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1720 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001721 HeaderFileInfo HFI;
1722 if (!HS.tryGetFileInfo(File, HFI) ||
1723 (HFI.External && Chain) ||
1724 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001725 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001726
Richard Smith7ed1bc92014-12-05 22:42:13 +00001727 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001728 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001729 SmallString<128> FilenameTmp(Filename);
1730 if (PreparePathForOutput(FilenameTmp)) {
1731 // If we performed any translation on the file name at all, we need to
1732 // save this string, since the generator will refer to it later.
1733 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001734 SavedStrings.push_back(Filename);
1735 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001736
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001737 HeaderFileInfoTrait::key_type key = { File, Filename };
1738 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001739 ++NumHeaderSearchEntries;
1740 }
1741
1742 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001743 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001744 uint32_t BucketOffset;
1745 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001746 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001747 llvm::raw_svector_ostream Out(TableData);
1748 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001749 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001750 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1751 }
1752
1753 // Create a blob abbreviation
1754 using namespace llvm;
1755 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1756 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1761 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1762
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001763 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001764 RecordData Record;
1765 Record.push_back(HEADER_SEARCH_TABLE);
1766 Record.push_back(BucketOffset);
1767 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001768 Record.push_back(TableData.size());
1769 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001770 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001771
1772 // Free all of the strings we had to duplicate.
1773 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001774 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001775}
1776
Douglas Gregora7f71a92009-04-10 03:52:48 +00001777/// \brief Writes the block containing the serialized form of the
1778/// source manager.
1779///
1780/// TODO: We should probably use an on-disk hash table (stored in a
1781/// blob), indexed based on the file name, so that we only create
1782/// entries for files that we actually need. In the common case (no
1783/// errors), we probably won't have to create file entries for any of
1784/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001785void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001786 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001787 RecordData Record;
1788
Chris Lattner0910e3b2009-04-10 17:16:57 +00001789 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001790 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001791
1792 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001793 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1794 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1795 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001796 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001797
Douglas Gregor258ae542009-04-27 06:38:32 +00001798 // Write out the source location entry table. We skip the first
1799 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001800 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001801 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001802 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1803 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001804 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001805 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001806 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001807 FileID FID = FileID::get(I);
1808 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001809
Douglas Gregor258ae542009-04-27 06:38:32 +00001810 // Record the offset of this source-location entry.
1811 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1812
1813 // Figure out which record code to use.
1814 unsigned Code;
1815 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001816 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1817 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001818 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001819 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001820 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001821 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001822 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001823 Record.clear();
1824 Record.push_back(Code);
1825
Douglas Gregor925296b2011-07-19 16:10:42 +00001826 // Starting offset of this entry within this module, so skip the dummy.
1827 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001828 if (SLoc->isFile()) {
1829 const SrcMgr::FileInfo &File = SLoc->getFile();
1830 Record.push_back(File.getIncludeLoc().getRawEncoding());
1831 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1832 Record.push_back(File.hasLineDirectives());
1833
1834 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001835 if (Content->OrigEntry) {
1836 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001837 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001838
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001839 // The source location entry is a file. Emit input file ID.
1840 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1841 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001842
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001843 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001844
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001845 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001846 if (FDI != FileDeclIDs.end()) {
1847 Record.push_back(FDI->second->FirstDeclIndex);
1848 Record.push_back(FDI->second->DeclIDs.size());
1849 } else {
1850 Record.push_back(0);
1851 Record.push_back(0);
1852 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001853
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001854 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001855
1856 if (Content->BufferOverridden) {
1857 Record.clear();
1858 Record.push_back(SM_SLOC_BUFFER_BLOB);
1859 const llvm::MemoryBuffer *Buffer
1860 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1861 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1862 StringRef(Buffer->getBufferStart(),
1863 Buffer->getBufferSize() + 1));
1864 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001865 } else {
1866 // The source location entry is a buffer. The blob associated
1867 // with this entry contains the contents of the buffer.
1868
1869 // We add one to the size so that we capture the trailing NULL
1870 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1871 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001872 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001873 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001874 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001875 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001876 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001877 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001878 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001879 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001880 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001881 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001882
Douglas Gregor925296b2011-07-19 16:10:42 +00001883 if (strcmp(Name, "<built-in>") == 0) {
1884 PreloadSLocs.push_back(SLocEntryOffsets.size());
1885 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001886 }
1887 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001888 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001889 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001890 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1891 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001892 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1893 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001894
1895 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001896 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001897 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001898 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001899 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001900 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001901 }
1902 }
1903
Douglas Gregor8f45df52009-04-16 22:23:12 +00001904 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001905
1906 if (SLocEntryOffsets.empty())
1907 return;
1908
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001909 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001910 // table is used for lazily loading source-location information.
1911 using namespace llvm;
1912 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001913 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1917 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001918
Douglas Gregor258ae542009-04-27 06:38:32 +00001919 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001920 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001921 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001922 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001923 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001924
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001925 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001926 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001927 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001928
1929 // Write the line table. It depends on remapping working, so it must come
1930 // after the source location offsets.
1931 if (SourceMgr.hasLineTable()) {
1932 LineTableInfo &LineTable = SourceMgr.getLineTable();
1933
1934 Record.clear();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001935 // Emit the file names.
Douglas Gregor925296b2011-07-19 16:10:42 +00001936 Record.push_back(LineTable.getNumFilenames());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001937 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I)
1938 AddPath(LineTable.getFilename(I), Record);
Douglas Gregor925296b2011-07-19 16:10:42 +00001939
1940 // Emit the line entries
1941 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1942 L != LEnd; ++L) {
1943 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001944 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001945 continue;
1946
1947 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001948 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001949
1950 // Emit the line entries
1951 Record.push_back(L->second.size());
1952 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1953 LEEnd = L->second.end();
1954 LE != LEEnd; ++LE) {
1955 Record.push_back(LE->FileOffset);
1956 Record.push_back(LE->LineNo);
1957 Record.push_back(LE->FilenameID);
1958 Record.push_back((unsigned)LE->FileKind);
1959 Record.push_back(LE->IncludeOffset);
1960 }
1961 }
1962 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1963 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001964}
1965
Douglas Gregorc5046832009-04-27 18:38:38 +00001966//===----------------------------------------------------------------------===//
1967// Preprocessor Serialization
1968//===----------------------------------------------------------------------===//
1969
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001970namespace {
1971class ASTMacroTableTrait {
1972public:
1973 typedef IdentID key_type;
1974 typedef key_type key_type_ref;
1975
1976 struct Data {
1977 uint32_t MacroDirectivesOffset;
1978 };
1979
1980 typedef Data data_type;
1981 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001982 typedef unsigned hash_value_type;
1983 typedef unsigned offset_type;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001984
Justin Bogner25463f12014-04-18 20:27:24 +00001985 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001986 return llvm::hash_value(IdID);
1987 }
1988
1989 std::pair<unsigned,unsigned>
1990 static EmitKeyDataLength(raw_ostream& Out,
1991 key_type_ref Key, data_type_ref Data) {
1992 unsigned KeyLen = 4; // IdentID.
1993 unsigned DataLen = 4; // MacroDirectivesOffset.
1994 return std::make_pair(KeyLen, DataLen);
1995 }
1996
1997 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001998 using namespace llvm::support;
1999 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002000 }
2001
2002 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
2003 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002004 using namespace llvm::support;
2005 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002006 }
2007};
2008} // end anonymous namespace
2009
Benjamin Kramer04bf1872013-09-22 14:10:29 +00002010static int compareMacroDirectives(
2011 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
2012 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
2013 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002014}
2015
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002016static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2017 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002018 if (MacroInfo *MI = MD->getMacroInfo())
2019 if (MI->isBuiltinMacro())
2020 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002021
2022 if (IsModule) {
Richard Smithe657bbd2014-07-18 22:13:40 +00002023 // Re-export any imported directives.
Richard Smithdaa69e02014-07-25 04:40:03 +00002024 if (MD->isImported())
2025 return false;
Richard Smithe657bbd2014-07-18 22:13:40 +00002026
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002027 SourceLocation Loc = MD->getLocation();
2028 if (Loc.isInvalid())
2029 return true;
2030 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2031 return true;
2032 }
2033
2034 return false;
2035}
2036
Chris Lattnereeffaef2009-04-10 17:15:23 +00002037/// \brief Writes the block containing the serialized form of the
2038/// preprocessor.
2039///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002040void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002041 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2042 if (PPRec)
2043 WritePreprocessorDetail(*PPRec);
2044
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002045 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002046
Chris Lattner0af3ba12009-04-13 01:29:17 +00002047 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2048 if (PP.getCounterValue() != 0) {
2049 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002050 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002051 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002052 }
2053
2054 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002055 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002056
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002057 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002058 // FIXME: use diagnostics subsystem for localization etc.
2059 if (PP.SawDateOrTime())
2060 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00002061
Douglas Gregor796d76a2010-10-20 22:00:55 +00002062
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002063 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002064 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002065
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002066 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002067 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002068 MacroDirectives;
2069 for (Preprocessor::macro_iterator
2070 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2071 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002072 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002073 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002074 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002075
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002076 // Sort the set of macro definitions that need to be serialized by the
2077 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002078 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2079 &compareMacroDirectives);
2080
Justin Bognerbb094f02014-04-18 19:57:06 +00002081 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002082
2083 // Emit the macro directives as a list and associate the offset with the
2084 // identifier they belong to.
2085 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2086 const IdentifierInfo *Name = MacroDirectives[I].first;
2087 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2088 MacroDirective *MD = MacroDirectives[I].second;
2089
2090 // If the macro or identifier need no updates, don't write the macro history
2091 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002092 // FIXME: Chain the macro history instead of re-writing it.
2093 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002094 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2095 continue;
2096
2097 // Emit the macro directives in reverse source order.
2098 for (; MD; MD = MD->getPrevious()) {
2099 if (shouldIgnoreMacro(MD, IsModule, PP))
2100 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002101
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002102 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002103 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002104 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002105 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2106 Record.push_back(InfoID);
Richard Smithdaa69e02014-07-25 04:40:03 +00002107 Record.push_back(DefMD->getOwningModuleID());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002108 Record.push_back(DefMD->isAmbiguous());
Richard Smithdaa69e02014-07-25 04:40:03 +00002109 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2110 Record.push_back(UndefMD->getOwningModuleID());
2111 } else {
2112 auto *VisMD = cast<VisibilityMacroDirective>(MD);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002113 Record.push_back(VisMD->isPublic());
2114 }
Richard Smithdaa69e02014-07-25 04:40:03 +00002115
2116 if (MD->isImported()) {
2117 auto Overrides = MD->getOverriddenModules();
2118 Record.push_back(Overrides.size());
Benjamin Kramerf9890422015-02-17 16:48:30 +00002119 Record.append(Overrides.begin(), Overrides.end());
Richard Smithdaa69e02014-07-25 04:40:03 +00002120 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002121 }
2122 if (Record.empty())
2123 continue;
2124
2125 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2126 Record.clear();
2127
2128 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2129
2130 IdentID NameID = getIdentifierRef(Name);
2131 ASTMacroTableTrait::Data data;
2132 data.MacroDirectivesOffset = MacroDirectiveOffset;
2133 Generator.insert(NameID, data);
2134 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002135
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002136 /// \brief Offsets of each of the macros into the bitstream, indexed by
2137 /// the local macro ID
2138 ///
2139 /// For each identifier that is associated with a macro, this map
2140 /// provides the offset into the bitstream where that macro is
2141 /// defined.
2142 std::vector<uint32_t> MacroOffsets;
2143
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002144 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2145 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2146 MacroInfo *MI = MacroInfosToEmit[I].MI;
2147 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002148
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002149 if (ID < FirstMacroID) {
2150 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2151 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002152 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002153
2154 // Record the local offset of this macro.
2155 unsigned Index = ID - FirstMacroID;
2156 if (Index == MacroOffsets.size())
2157 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2158 else {
2159 if (Index > MacroOffsets.size())
2160 MacroOffsets.resize(Index + 1);
2161
2162 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2163 }
2164
2165 AddIdentifierRef(Name, Record);
2166 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2167 AddSourceLocation(MI->getDefinitionLoc(), Record);
2168 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2169 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002170 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002171 unsigned Code;
2172 if (MI->isObjectLike()) {
2173 Code = PP_MACRO_OBJECT_LIKE;
2174 } else {
2175 Code = PP_MACRO_FUNCTION_LIKE;
2176
2177 Record.push_back(MI->isC99Varargs());
2178 Record.push_back(MI->isGNUVarargs());
2179 Record.push_back(MI->hasCommaPasting());
2180 Record.push_back(MI->getNumArgs());
2181 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2182 I != E; ++I)
2183 AddIdentifierRef(*I, Record);
2184 }
2185
2186 // If we have a detailed preprocessing record, record the macro definition
2187 // ID that corresponds to this macro.
2188 if (PPRec)
2189 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2190
2191 Stream.EmitRecord(Code, Record);
2192 Record.clear();
2193
2194 // Emit the tokens array.
2195 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2196 // Note that we know that the preprocessor does not have any annotation
2197 // tokens in it because they are created by the parser, and thus can't
2198 // be in a macro definition.
2199 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002200 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002201 Stream.EmitRecord(PP_TOKEN, Record);
2202 Record.clear();
2203 }
2204 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002205 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002206
Douglas Gregor92a96f52011-02-08 21:58:10 +00002207 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002208
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002209 // Create the on-disk hash table in a buffer.
2210 SmallString<4096> MacroTable;
2211 uint32_t BucketOffset;
2212 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002213 using namespace llvm::support;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002214 llvm::raw_svector_ostream Out(MacroTable);
2215 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002216 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002217 BucketOffset = Generator.Emit(Out);
2218 }
2219
2220 // Write the macro table
2221 using namespace llvm;
2222 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2223 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2226 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2227
2228 Record.push_back(MACRO_TABLE);
2229 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00002230 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002231 Record.clear();
2232
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002233 // Write the offsets table for macro IDs.
2234 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002235 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002236 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2240
2241 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2242 Record.clear();
2243 Record.push_back(MACRO_OFFSET);
2244 Record.push_back(MacroOffsets.size());
2245 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2246 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2247 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002248}
2249
2250void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002251 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002252 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002253
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002254 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002255
Douglas Gregor92a96f52011-02-08 21:58:10 +00002256 // Enter the preprocessor block.
2257 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002258
Douglas Gregoraae92242010-03-19 21:51:54 +00002259 // If the preprocessor has a preprocessing record, emit it.
2260 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002261 using namespace llvm;
2262
2263 // Set up the abbreviation for
2264 unsigned InclusionAbbrev = 0;
2265 {
2266 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2267 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2273 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2274 }
2275
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002276 unsigned FirstPreprocessorEntityID
2277 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2278 + NUM_PREDEF_PP_ENTITY_IDS;
2279 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002280 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002281 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2282 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002283 E != EEnd;
2284 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002285 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002286
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002287 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2288 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002289
Douglas Gregor92a96f52011-02-08 21:58:10 +00002290 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002291 // Record this macro definition's ID.
2292 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002293
Douglas Gregor92a96f52011-02-08 21:58:10 +00002294 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002295 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2296 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002297 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002298
Chandler Carrutha88a22182011-07-14 08:20:46 +00002299 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002300 Record.push_back(ME->isBuiltinMacro());
2301 if (ME->isBuiltinMacro())
2302 AddIdentifierRef(ME->getName(), Record);
2303 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002304 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002305 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002306 continue;
2307 }
2308
2309 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2310 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002311 Record.push_back(ID->getFileName().size());
2312 Record.push_back(ID->wasInQuotes());
2313 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002314 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002315 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002316 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002317 // Check that the FileEntry is not null because it was not resolved and
2318 // we create a PCH even with compiler errors.
2319 if (ID->getFile())
2320 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002321 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2322 continue;
2323 }
2324
2325 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2326 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002327 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002328
Douglas Gregoraae92242010-03-19 21:51:54 +00002329 // Write the offsets table for the preprocessing record.
2330 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002331 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2332
Douglas Gregoraae92242010-03-19 21:51:54 +00002333 // Write the offsets table for identifier IDs.
2334 using namespace llvm;
2335 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002336 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002339 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002340
Douglas Gregoraae92242010-03-19 21:51:54 +00002341 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002342 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002343 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002344 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2345 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002346 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002347}
2348
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002349unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2350 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2351 if (Known != SubmoduleIDs.end())
2352 return Known->second;
2353
2354 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2355}
2356
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002357unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2358 if (!Mod)
2359 return 0;
2360
2361 llvm::DenseMap<Module *, unsigned>::const_iterator
2362 Known = SubmoduleIDs.find(Mod);
2363 if (Known != SubmoduleIDs.end())
2364 return Known->second;
2365
2366 return 0;
2367}
2368
Douglas Gregor253eefe2011-12-01 00:59:36 +00002369/// \brief Compute the number of modules within the given tree (including the
2370/// given module).
2371static unsigned getNumberOfModules(Module *Mod) {
2372 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002373 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2374 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002375 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002376 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002377
2378 return ChildModules + 1;
2379}
2380
Douglas Gregorde3ef502011-11-30 23:21:26 +00002381void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002382 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002383 // FIXME: This feels like it belongs somewhere else, but there are no
2384 // other consumers of this information.
2385 SourceManager &SrcMgr = PP->getSourceManager();
2386 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002387 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002388 if (Module *ImportedFrom
2389 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2390 SrcMgr))) {
2391 ImportedFrom->Imports.push_back(I->getImportedModule());
2392 }
2393 }
2394
Douglas Gregor69021972011-11-30 17:33:56 +00002395 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002396 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002397
2398 // Write the abbreviations needed for the submodules block.
2399 using namespace llvm;
2400 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2401 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2413 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2414
2415 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002416 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2418 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2419
2420 Abbrev = new BitCodeAbbrev();
2421 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2423 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002424
2425 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002426 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2428 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2429
2430 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002431 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2433 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2434
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002435 Abbrev = new BitCodeAbbrev();
2436 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002439 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2440
Douglas Gregor59527662012-10-15 06:28:11 +00002441 Abbrev = new BitCodeAbbrev();
2442 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2444 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2445
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002446 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002447 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2449 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2450
2451 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002452 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2454 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2455
2456 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002457 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2459 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2460
2461 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002462 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2463 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2465 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2466
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002467 Abbrev = new BitCodeAbbrev();
2468 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2470 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2471
Douglas Gregorfb912652013-03-20 21:10:35 +00002472 Abbrev = new BitCodeAbbrev();
2473 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2475 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2476 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2477
Douglas Gregor253eefe2011-12-01 00:59:36 +00002478 // Write the submodule metadata block.
2479 RecordData Record;
2480 Record.push_back(getNumberOfModules(WritingModule));
2481 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2482 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2483
Douglas Gregor69021972011-11-30 17:33:56 +00002484 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002485 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002486 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002487 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002488 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002489 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002490 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002491
2492 // Emit the definition of the block.
2493 Record.clear();
2494 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002495 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002496 if (Mod->Parent) {
2497 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2498 Record.push_back(SubmoduleIDs[Mod->Parent]);
2499 } else {
2500 Record.push_back(0);
2501 }
2502 Record.push_back(Mod->IsFramework);
2503 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002504 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002505 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002506 Record.push_back(Mod->InferSubmodules);
2507 Record.push_back(Mod->InferExplicitSubmodules);
2508 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002509 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002510 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2511
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002512 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002513 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002514 Record.clear();
2515 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002516 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002517 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002518 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002519 }
2520
Douglas Gregor69021972011-11-30 17:33:56 +00002521 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002522 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002523 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002524 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002525 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002526 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002527 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2528 Record.clear();
2529 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2530 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2531 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002532 }
Richard Smith306d8922014-10-22 23:50:56 +00002533
Douglas Gregor69021972011-11-30 17:33:56 +00002534 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002535 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002536 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002537 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002538 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002539 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002540 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2541 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2542 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002543 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002544 Module::HK_PrivateTextual},
2545 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002546 };
2547 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002548 Record.clear();
Richard Smith3c1a41a2014-12-02 00:08:08 +00002549 Record.push_back(HL.RecordKind);
2550 for (auto &H : Mod->Headers[HL.HeaderKind])
2551 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2552 }
2553
2554 // Emit the top headers.
2555 {
2556 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2557 Record.clear();
2558 Record.push_back(SUBMODULE_TOPHEADER);
2559 for (auto *H : TopHeaders)
2560 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002561 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002562
2563 // Emit the imports.
2564 if (!Mod->Imports.empty()) {
2565 Record.clear();
2566 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002567 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00002568 assert(ImportedID && "Unknown submodule!");
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002569 Record.push_back(ImportedID);
2570 }
2571 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2572 }
2573
Douglas Gregor24bb9232011-12-02 18:58:38 +00002574 // Emit the exports.
2575 if (!Mod->Exports.empty()) {
2576 Record.clear();
2577 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002578 if (Module *Exported = Mod->Exports[I].getPointer()) {
2579 unsigned ExportedID = SubmoduleIDs[Exported];
2580 assert(ExportedID > 0 && "Unknown submodule ID?");
2581 Record.push_back(ExportedID);
2582 } else {
2583 Record.push_back(0);
2584 }
2585
Douglas Gregor24bb9232011-12-02 18:58:38 +00002586 Record.push_back(Mod->Exports[I].getInt());
2587 }
2588 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2589 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002590
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002591 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2592 // Might be unnecessary as use declarations are only used to build the
2593 // module itself.
2594
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002595 // Emit the link libraries.
2596 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2597 Record.clear();
2598 Record.push_back(SUBMODULE_LINK_LIBRARY);
2599 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2600 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2601 Mod->LinkLibraries[I].Library);
2602 }
2603
Douglas Gregorfb912652013-03-20 21:10:35 +00002604 // Emit the conflicts.
2605 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2606 Record.clear();
2607 Record.push_back(SUBMODULE_CONFLICT);
2608 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2609 assert(OtherID && "Unknown submodule!");
2610 Record.push_back(OtherID);
2611 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2612 Mod->Conflicts[I].Message);
2613 }
2614
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002615 // Emit the configuration macros.
2616 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2617 Record.clear();
2618 Record.push_back(SUBMODULE_CONFIG_MACRO);
2619 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2620 Mod->ConfigMacros[I]);
2621 }
2622
Douglas Gregor69021972011-11-30 17:33:56 +00002623 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002624 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2625 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002626 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002627 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002628 }
2629
2630 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002631
2632 assert((NextSubmoduleID - FirstSubmoduleID
2633 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002634}
2635
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002636serialization::SubmoduleID
2637ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002638 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002639 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002640
2641 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002642 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002643 Module *OwningMod
2644 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002645 if (!OwningMod)
2646 return 0;
2647
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002648 // Check whether this submodule is part of our own module.
2649 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002650 return 0;
2651
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002652 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002653}
2654
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002655void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2656 bool isModule) {
2657 // Make sure set diagnostic pragmas don't affect the translation unit that
2658 // imports the module.
2659 // FIXME: Make diagnostic pragma sections work properly with modules.
2660 if (isModule)
2661 return;
2662
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002663 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2664 DiagStateIDMap;
2665 unsigned CurrID = 0;
2666 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002667 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002668 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002669 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2670 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002671 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002672 if (point.Loc.isInvalid())
2673 continue;
2674
2675 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002676 unsigned &DiagStateID = DiagStateIDMap[point.State];
2677 Record.push_back(DiagStateID);
2678
2679 if (DiagStateID == 0) {
2680 DiagStateID = ++CurrID;
2681 for (DiagnosticsEngine::DiagState::const_iterator
2682 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2683 if (I->second.isPragma()) {
2684 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002685 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002686 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002687 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002688 Record.push_back(-1); // mark the end of the diag/map pairs for this
2689 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002690 }
2691 }
2692
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002693 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002694 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002695}
2696
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002697void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2698 if (CXXBaseSpecifiersOffsets.empty())
2699 return;
2700
2701 RecordData Record;
2702
2703 // Create a blob abbreviation for the C++ base specifiers offsets.
2704 using namespace llvm;
2705
2706 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2707 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2710 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2711
Douglas Gregorc27b2872011-08-04 00:01:48 +00002712 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002713 Record.clear();
2714 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2715 Record.push_back(CXXBaseSpecifiersOffsets.size());
2716 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002717 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002718}
2719
Douglas Gregorc5046832009-04-27 18:38:38 +00002720//===----------------------------------------------------------------------===//
2721// Type Serialization
2722//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002723
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002724/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002725void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002726 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002727 if (Idx.getIndex() == 0) // we haven't seen this type before.
2728 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002729
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002730 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002731
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002732 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002733 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002734 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002735 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002736 else if (TypeOffsets.size() < Index) {
2737 TypeOffsets.resize(Index + 1);
2738 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002739 }
2740
2741 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002742
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002743 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002744 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002745 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002746
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002747 if (T.hasLocalNonFastQualifiers()) {
2748 Qualifiers Qs = T.getLocalQualifiers();
2749 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002750 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002751 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002752 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002753 } else {
2754 switch (T->getTypeClass()) {
2755 // For all of the concrete, non-dependent types, call the
2756 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002757#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002758 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002759#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002760#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002761 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002762 }
2763
2764 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002765 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002766
2767 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002768 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002769}
2770
Douglas Gregorc5046832009-04-27 18:38:38 +00002771//===----------------------------------------------------------------------===//
2772// Declaration Serialization
2773//===----------------------------------------------------------------------===//
2774
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002775/// \brief Write the block containing all of the declaration IDs
2776/// lexically declared within the given DeclContext.
2777///
2778/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2779/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002780uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002781 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002782 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002783 return 0;
2784
Douglas Gregor8f45df52009-04-16 22:23:12 +00002785 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002786 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002787 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002788 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002789 for (const auto *D : DC->decls())
2790 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002791
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002792 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002793 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002794 return Offset;
2795}
2796
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002797void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002798 using namespace llvm;
2799 RecordData Record;
2800
2801 // Write the type offsets array
2802 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002803 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2807 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2808 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002809 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002810 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002811 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002812 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002813
2814 // Write the declaration offsets array
2815 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002816 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002818 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002819 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2820 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2821 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002822 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002823 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002824 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002825 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002826}
2827
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002828void ASTWriter::WriteFileDeclIDsMap() {
2829 using namespace llvm;
2830 RecordData Record;
2831
2832 // Join the vectors of DeclIDs from all files.
2833 SmallVector<DeclID, 256> FileSortedIDs;
2834 for (FileDeclIDsTy::iterator
2835 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2836 DeclIDInFileInfo &Info = *FI->second;
2837 Info.FirstDeclIndex = FileSortedIDs.size();
2838 for (LocDeclIDsTy::iterator
2839 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2840 FileSortedIDs.push_back(DI->second);
2841 }
2842
2843 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2844 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2847 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2848 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002849 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002850 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2851}
2852
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002853void ASTWriter::WriteComments() {
2854 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002855 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002856 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002857 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2858 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002859 I != E; ++I) {
2860 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002861 AddSourceRange((*I)->getSourceRange(), Record);
2862 Record.push_back((*I)->getKind());
2863 Record.push_back((*I)->isTrailingComment());
2864 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002865 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2866 }
2867 Stream.ExitBlock();
2868}
2869
Douglas Gregorc5046832009-04-27 18:38:38 +00002870//===----------------------------------------------------------------------===//
2871// Global Method Pool and Selector Serialization
2872//===----------------------------------------------------------------------===//
2873
Douglas Gregore84a9da2009-04-20 20:36:09 +00002874namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002875// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002876class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002877 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002878
2879public:
2880 typedef Selector key_type;
2881 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002882
Sebastian Redl834bb972010-08-04 17:20:04 +00002883 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002884 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002885 ObjCMethodList Instance, Factory;
2886 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002887 typedef const data_type& data_type_ref;
2888
Justin Bogner25463f12014-04-18 20:27:24 +00002889 typedef unsigned hash_value_type;
2890 typedef unsigned offset_type;
2891
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002892 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002893
Justin Bogner25463f12014-04-18 20:27:24 +00002894 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002895 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002896 }
Mike Stump11289f42009-09-09 15:08:12 +00002897
2898 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002899 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002900 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002901 using namespace llvm::support;
2902 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002903 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002904 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002905 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2906 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002907 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002908 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002909 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002910 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002911 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002912 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002913 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002914 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002915 return std::make_pair(KeyLen, DataLen);
2916 }
Mike Stump11289f42009-09-09 15:08:12 +00002917
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002918 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002919 using namespace llvm::support;
2920 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002921 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002922 assert((Start >> 32) == 0 && "Selector key offset too large");
2923 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002924 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002925 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002926 if (N == 0)
2927 N = 1;
2928 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002929 LE.write<uint32_t>(
2930 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002931 }
Mike Stump11289f42009-09-09 15:08:12 +00002932
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002933 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002934 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002935 using namespace llvm::support;
2936 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002937 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002938 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002939 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002940 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002941 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002942 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002943 ++NumInstanceMethods;
2944
2945 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002946 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002947 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002948 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002949 ++NumFactoryMethods;
2950
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002951 unsigned InstanceBits = Methods.Instance.getBits();
2952 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002953 unsigned InstanceHasMoreThanOneDeclBit =
2954 Methods.Instance.hasMoreThanOneDecl();
2955 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2956 (InstanceHasMoreThanOneDeclBit << 2) |
2957 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002958 unsigned FactoryBits = Methods.Factory.getBits();
2959 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002960 unsigned FactoryHasMoreThanOneDeclBit =
2961 Methods.Factory.hasMoreThanOneDecl();
2962 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2963 (FactoryHasMoreThanOneDeclBit << 2) |
2964 FactoryBits;
2965 LE.write<uint16_t>(FullInstanceBits);
2966 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002967 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002968 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002969 if (Method->getMethod())
2970 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002971 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002972 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002973 if (Method->getMethod())
2974 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002975
2976 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002977 }
2978};
2979} // end anonymous namespace
2980
Sebastian Redla19a67f2010-08-03 21:58:15 +00002981/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002982///
2983/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002984/// in an on-disk hash table indexed by the selector. The hash table also
2985/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002986void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002987 using namespace llvm;
2988
Sebastian Redla19a67f2010-08-03 21:58:15 +00002989 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002990 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002991 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002992 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002993 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002994 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002995 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002996 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002997
Sebastian Redla19a67f2010-08-03 21:58:15 +00002998 // Create the on-disk hash table representation. We walk through every
2999 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003000 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003001 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00003002 I = SelectorIDs.begin(), E = SelectorIDs.end();
3003 I != E; ++I) {
3004 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003005 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003006 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00003007 I->second,
3008 ObjCMethodList(),
3009 ObjCMethodList()
3010 };
3011 if (F != SemaRef.MethodPool.end()) {
3012 Data.Instance = F->second.first;
3013 Data.Factory = F->second.second;
3014 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003015 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003016 // changed.
3017 if (Chain && I->second < FirstSelectorID) {
3018 // Selector already exists. Did it change?
3019 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003020 for (ObjCMethodList *M = &Data.Instance;
3021 !changed && M && M->getMethod(); M = M->getNext()) {
3022 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003023 changed = true;
3024 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003025 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003026 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003027 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003028 changed = true;
3029 }
3030 if (!changed)
3031 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003032 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003033 // A new method pool entry.
3034 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003035 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003036 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003037 }
3038
Douglas Gregorc78d3462009-04-24 21:10:55 +00003039 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003040 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003041 uint32_t BucketOffset;
3042 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003043 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003044 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003045 llvm::raw_svector_ostream Out(MethodPool);
3046 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003047 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003048 BucketOffset = Generator.Emit(Out, Trait);
3049 }
3050
3051 // Create a blob abbreviation
3052 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003053 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3057 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3058
Douglas Gregor95c13f52009-04-25 17:48:32 +00003059 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003060 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003061 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003062 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003063 Record.push_back(NumTableEntries);
Yaron Keren92e1b622015-03-18 10:17:07 +00003064 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003065
3066 // Create a blob abbreviation for the selector table offsets.
3067 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003068 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003069 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3072 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3073
3074 // Write the selector offsets table.
3075 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003076 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003077 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003078 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003079 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003080 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003081 }
3082}
3083
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003084/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003085void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003086 using namespace llvm;
3087 if (SemaRef.ReferencedSelectors.empty())
3088 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003089
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003090 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003091
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003092 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003093 // very tricky to fix, and given that @selector shouldn't really appear in
3094 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003095 for (DenseMap<Selector, SourceLocation>::iterator S =
3096 SemaRef.ReferencedSelectors.begin(),
3097 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3098 Selector Sel = (*S).first;
3099 SourceLocation Loc = (*S).second;
3100 AddSelectorRef(Sel, Record);
3101 AddSourceLocation(Loc, Record);
3102 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003103 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003104}
3105
Douglas Gregorc5046832009-04-27 18:38:38 +00003106//===----------------------------------------------------------------------===//
3107// Identifier Table Serialization
3108//===----------------------------------------------------------------------===//
3109
Richard Smithb3761912015-02-05 23:08:52 +00003110/// Determine the declaration that should be put into the name lookup table to
3111/// represent the given declaration in this module. This is usually D itself,
3112/// but if D was imported and merged into a local declaration, we want the most
3113/// recent local declaration instead. The chosen declaration will be the most
3114/// recent declaration in any module that imports this one.
3115static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3116 NamedDecl *D) {
3117 if (!LangOpts.Modules || !D->isFromASTFile())
3118 return D;
3119
3120 if (Decl *Redecl = D->getPreviousDecl()) {
3121 // For Redeclarable decls, a prior declaration might be local.
3122 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3123 if (!Redecl->isFromASTFile())
3124 return cast<NamedDecl>(Redecl);
Richard Smithfe620d22015-03-05 23:24:12 +00003125 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003126 // local one.
3127 if (D->getOwningModuleID() == 0)
3128 break;
3129 }
3130 } else if (Decl *First = D->getCanonicalDecl()) {
3131 // For Mergeable decls, the first decl might be local.
3132 if (!First->isFromASTFile())
3133 return cast<NamedDecl>(First);
3134 }
3135
3136 // All declarations are imported. Our most recent declaration will also be
3137 // the most recent one in anyone who imports us.
3138 return D;
3139}
3140
Douglas Gregorc78d3462009-04-24 21:10:55 +00003141namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003142class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003143 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00003144 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003145 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003146 bool IsModule;
3147
Douglas Gregor1d583f22009-04-28 21:18:29 +00003148 /// \brief Determines whether this is an "interesting" identifier
3149 /// that needs a full IdentifierInfo structure written into the hash
3150 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003151 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003152 if (II->isPoisoned() ||
3153 II->isExtensionToken() ||
3154 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003155 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003156 II->getFETokenInfo<void>())
3157 return true;
3158
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003159 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00003160 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003161
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003162 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003163 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003164 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003165
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003166 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3167 if (!IsModule)
3168 return !shouldIgnoreMacro(Macro, IsModule, PP);
Richard Smithdaa69e02014-07-25 04:40:03 +00003169
3170 MacroState State;
3171 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003172 return true;
3173 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003174
3175 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003176 }
3177
Richard Smithdaa69e02014-07-25 04:40:03 +00003178 enum class SubmoduleMacroState {
3179 /// We've seen nothing about this macro.
3180 None,
3181 /// We've seen a public visibility directive.
3182 Public,
3183 /// We've either exported a macro for this module or found that the
3184 /// module's definition of this macro is private.
3185 Done
3186 };
3187 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Richard Smith49f906a2014-03-01 00:08:04 +00003188
3189 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003190 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3191 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3192 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003193 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003194 }
3195
Richard Smith49f906a2014-03-01 00:08:04 +00003196 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003197 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Richard Smith49f906a2014-03-01 00:08:04 +00003198 if (MacroDirective *NextMD =
Richard Smithdaa69e02014-07-25 04:40:03 +00003199 getPublicSubmoduleMacro(MD->getPrevious(), State))
3200 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003201 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003202 }
3203
Richard Smithdaa69e02014-07-25 04:40:03 +00003204 /// \brief Traverses the macro directives history and returns the next
3205 /// public macro definition or undefinition that has not been found so far.
3206 ///
Richard Smith49f906a2014-03-01 00:08:04 +00003207 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003208 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003209 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Richard Smithdaa69e02014-07-25 04:40:03 +00003210 MacroState &State) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003211 if (!MD)
Craig Toppera13603a2014-05-22 05:54:18 +00003212 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003213
Richard Smith49f906a2014-03-01 00:08:04 +00003214 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003215 for (; MD; MD = MD->getPrevious()) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003216 // Once we hit an ignored macro, we're done: the rest of the chain
3217 // will all be ignored macros.
3218 if (shouldIgnoreMacro(MD, IsModule, PP))
3219 break;
Richard Smith57721ac2014-07-21 04:10:40 +00003220
Richard Smithdaa69e02014-07-25 04:40:03 +00003221 // If this macro was imported, re-export it.
3222 if (MD->isImported())
3223 return MD;
Richard Smith57721ac2014-07-21 04:10:40 +00003224
Richard Smithdaa69e02014-07-25 04:40:03 +00003225 SubmoduleID ModID = getSubmoduleID(MD);
3226 auto &S = State[ModID];
3227 assert(ModID && "found macro in no submodule");
Richard Smith49f906a2014-03-01 00:08:04 +00003228
Richard Smithdaa69e02014-07-25 04:40:03 +00003229 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003230 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003231
Richard Smithdaa69e02014-07-25 04:40:03 +00003232 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3233 // The latest visibility directive for a name in a submodule affects all
3234 // the directives that come before it.
3235 if (S == SubmoduleMacroState::None)
3236 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3237 : SubmoduleMacroState::Done;
3238 } else {
3239 S = SubmoduleMacroState::Done;
Richard Smith49f906a2014-03-01 00:08:04 +00003240 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003241 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003242 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003243
Craig Toppera13603a2014-05-22 05:54:18 +00003244 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003245 }
3246
Richard Smithdaa69e02014-07-25 04:40:03 +00003247 ArrayRef<SubmoduleID>
3248 getOverriddenSubmodules(MacroDirective *MD,
3249 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3250 assert(!isa<VisibilityMacroDirective>(MD) &&
3251 "only #define and #undef can override");
3252 if (MD->isImported())
3253 return MD->getOverriddenModules();
3254
3255 ScratchSpace.clear();
3256 SubmoduleID ModID = getSubmoduleID(MD);
3257 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3258 if (shouldIgnoreMacro(MD, IsModule, PP))
3259 break;
3260
3261 // If this is a definition from a submodule import, that submodule's
3262 // definition is overridden by the definition or undefinition that we
3263 // started with.
3264 if (MD->isImported()) {
3265 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3266 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3267 assert(DefModuleID && "imported macro has no owning module");
3268 ScratchSpace.push_back(DefModuleID);
3269 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3270 // If we override a #undef, we override anything that #undef overrides.
3271 // We don't need to override it, since an active #undef doesn't affect
3272 // the meaning of a macro.
3273 auto Overrides = UndefMD->getOverriddenModules();
3274 ScratchSpace.insert(ScratchSpace.end(),
3275 Overrides.begin(), Overrides.end());
3276 }
3277 }
3278
3279 // Stop once we leave the original macro's submodule.
3280 //
3281 // Either this submodule #included another submodule of the same
3282 // module or it just happened to be built after the other module.
3283 // In the former case, we override the submodule's macro.
3284 //
3285 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3286 // these cases apart.
3287 //
3288 // FIXME: We can leave this submodule and re-enter it if it #includes a
3289 // header within a different submodule of the same module. In such cases
3290 // the overrides list will be incomplete.
3291 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3292 if (DirectiveModuleID != ModID) {
3293 if (DirectiveModuleID && !MD->isImported())
3294 ScratchSpace.push_back(DirectiveModuleID);
3295 break;
3296 }
3297 }
3298
3299 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3300 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3301 ScratchSpace.end());
3302 return ScratchSpace;
3303 }
3304
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003305 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Richard Smith57721ac2014-07-21 04:10:40 +00003306 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003307 }
3308
Douglas Gregore84a9da2009-04-20 20:36:09 +00003309public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003310 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003311 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003312
Sebastian Redl539c5062010-08-18 23:57:32 +00003313 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003314 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003315
Justin Bogner25463f12014-04-18 20:27:24 +00003316 typedef unsigned hash_value_type;
3317 typedef unsigned offset_type;
3318
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003319 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3320 IdentifierResolver &IdResolver, bool IsModule)
3321 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003322
Justin Bogner25463f12014-04-18 20:27:24 +00003323 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003324 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003325 }
Mike Stump11289f42009-09-09 15:08:12 +00003326
3327 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003328 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003329 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003330 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Craig Toppera13603a2014-05-22 05:54:18 +00003331 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003332 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003333 DataLen += 2; // 2 bytes for builtin ID
3334 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003335 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003336 DataLen += 4; // MacroDirectives offset.
3337 if (IsModule) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003338 MacroState State;
3339 SmallVector<SubmoduleID, 16> Scratch;
3340 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3341 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003342 DataLen += 4; // MacroInfo ID or ModuleID.
Richard Smithdaa69e02014-07-25 04:40:03 +00003343 if (unsigned NumOverrides =
3344 getOverriddenSubmodules(MD, Scratch).size())
3345 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003346 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003347 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003348 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003349 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003350
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003351 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3352 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003353 D != DEnd; ++D)
Richard Smithdaa69e02014-07-25 04:40:03 +00003354 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003355 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003356 using namespace llvm::support;
3357 endian::Writer<little> LE(Out);
3358
Richard Smithdf8a8312015-03-13 04:05:01 +00003359 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003360 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003361 // We emit the key length after the data length so that every
3362 // string is preceded by a 16-bit length. This matches the PTH
3363 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003364 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003365 return std::make_pair(KeyLen, DataLen);
3366 }
Mike Stump11289f42009-09-09 15:08:12 +00003367
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003368 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003369 unsigned KeyLen) {
3370 // Record the location of the key data. This is used when generating
3371 // the mapping from persistent IDs to strings.
3372 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003373 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003374 }
Mike Stump11289f42009-09-09 15:08:12 +00003375
Richard Smith49f906a2014-03-01 00:08:04 +00003376 static void emitMacroOverrides(raw_ostream &Out,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003377 ArrayRef<SubmoduleID> Overridden) {
Richard Smith49f906a2014-03-01 00:08:04 +00003378 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003379 using namespace llvm::support;
3380 endian::Writer<little> LE(Out);
3381 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smithdaa69e02014-07-25 04:40:03 +00003382 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3383 assert(Overridden[I] && "zero module ID for override");
Justin Bognere1c147c2014-03-28 22:03:19 +00003384 LE.write<uint32_t>(Overridden[I]);
Richard Smithdaa69e02014-07-25 04:40:03 +00003385 }
Richard Smith49f906a2014-03-01 00:08:04 +00003386 }
3387 }
3388
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003389 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003390 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003391 using namespace llvm::support;
3392 endian::Writer<little> LE(Out);
Craig Toppera13603a2014-05-22 05:54:18 +00003393 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003394 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003395 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003396 return;
3397 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003398
Justin Bognere1c147c2014-03-28 22:03:19 +00003399 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003400 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3401 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003402 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003403 Bits = 0;
3404 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003405 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003406 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003407 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3408 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003409 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003410 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003411 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003412
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003413 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003414 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003415 if (IsModule) {
3416 // Write the IDs of macros coming from different submodules.
Richard Smithdaa69e02014-07-25 04:40:03 +00003417 MacroState State;
3418 SmallVector<SubmoduleID, 16> Scratch;
3419 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3420 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003421 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003422 // FIXME: If this macro directive was created by #pragma pop_macros,
3423 // or if it was created implicitly by resolving conflicting macros,
3424 // it may be for a different submodule from the one in the MacroInfo
3425 // object. If so, we should write out its owning ModuleID.
3426 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Richard Smith49f906a2014-03-01 00:08:04 +00003427 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003428 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003429 } else {
Richard Smithdaa69e02014-07-25 04:40:03 +00003430 auto *UndefMD = cast<UndefMacroDirective>(MD);
3431 SubmoduleID Mod = UndefMD->isImported()
3432 ? UndefMD->getOwningModuleID()
3433 : getSubmoduleID(UndefMD);
3434 LE.write<uint32_t>((Mod << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003435 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003436 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003437 }
Richard Smithdf8a8312015-03-13 04:05:01 +00003438 LE.write<uint32_t>((uint32_t)-1);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003439 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003440 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003441
Douglas Gregora868bbd2009-04-21 22:25:48 +00003442 // Emit the declaration IDs in reverse order, because the
3443 // IdentifierResolver provides the declarations as they would be
3444 // visible (e.g., the function "stat" would come before the struct
Richard Smithb3761912015-02-05 23:08:52 +00003445 // "stat"), but the ASTReader adds declarations to the end of the list
3446 // (so we need to see the struct "stat" before the function "stat").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003447 // Only emit declarations that aren't from a chained PCH, though.
Richard Smithb3761912015-02-05 23:08:52 +00003448 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II), IdResolver.end());
3449 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3450 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003451 D != DEnd; ++D)
Richard Smithb3761912015-02-05 23:08:52 +00003452 LE.write<uint32_t>(
3453 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003454 }
3455};
3456} // end anonymous namespace
3457
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003458/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003459///
3460/// The identifier table consists of a blob containing string data
3461/// (the actual identifiers themselves) and a separate "offsets" index
3462/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003463void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3464 IdentifierResolver &IdResolver,
3465 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003466 using namespace llvm;
3467
3468 // Create and write out the blob that contains the identifier
3469 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003470 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003471 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003472 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003473
Douglas Gregore6648fb2009-04-28 20:33:11 +00003474 // Look for any identifiers that were named while processing the
3475 // headers, but are otherwise not needed. We add these to the hash
3476 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003477 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003478 // file.
3479 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3480 IDEnd = PP.getIdentifierTable().end();
3481 ID != IDEnd; ++ID)
3482 getIdentifierRef(ID->second);
3483
Sebastian Redlff4a2952010-07-23 23:49:55 +00003484 // Create the on-disk hash table representation. We only store offsets
3485 // for identifiers that appear here for the first time.
3486 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003487 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003488 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3489 ID != IDEnd; ++ID) {
3490 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003491 if (!Chain || !ID->first->isFromAST() ||
3492 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003493 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003494 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003495 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003496
Douglas Gregore84a9da2009-04-20 20:36:09 +00003497 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003498 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003499 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003500 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003501 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003502 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003503 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003504 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003505 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003506 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003507 }
3508
3509 // Create a blob abbreviation
3510 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003511 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003514 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003515
3516 // Write the identifier table
3517 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003518 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003519 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003520 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003521 }
3522
3523 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003524 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003525 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3529 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3530
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003531#ifndef NDEBUG
3532 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3533 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3534#endif
3535
Douglas Gregor0e149972009-04-25 19:10:14 +00003536 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003537 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003538 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003539 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003540 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003541 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003542}
3543
Douglas Gregorc5046832009-04-27 18:38:38 +00003544//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003545// DeclContext's Name Lookup Table Serialization
3546//===----------------------------------------------------------------------===//
3547
3548namespace {
3549// Trait used for the on-disk hash table used in the method pool.
3550class ASTDeclContextNameLookupTrait {
3551 ASTWriter &Writer;
3552
3553public:
3554 typedef DeclarationName key_type;
3555 typedef key_type key_type_ref;
3556
3557 typedef DeclContext::lookup_result data_type;
3558 typedef const data_type& data_type_ref;
3559
Justin Bogner25463f12014-04-18 20:27:24 +00003560 typedef unsigned hash_value_type;
3561 typedef unsigned offset_type;
3562
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003563 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3564
Justin Bogner25463f12014-04-18 20:27:24 +00003565 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003566 llvm::FoldingSetNodeID ID;
3567 ID.AddInteger(Name.getNameKind());
3568
3569 switch (Name.getNameKind()) {
3570 case DeclarationName::Identifier:
3571 ID.AddString(Name.getAsIdentifierInfo()->getName());
3572 break;
3573 case DeclarationName::ObjCZeroArgSelector:
3574 case DeclarationName::ObjCOneArgSelector:
3575 case DeclarationName::ObjCMultiArgSelector:
3576 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3577 break;
3578 case DeclarationName::CXXConstructorName:
3579 case DeclarationName::CXXDestructorName:
3580 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003581 break;
3582 case DeclarationName::CXXOperatorName:
3583 ID.AddInteger(Name.getCXXOverloadedOperator());
3584 break;
3585 case DeclarationName::CXXLiteralOperatorName:
3586 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3587 case DeclarationName::CXXUsingDirective:
3588 break;
3589 }
3590
3591 return ID.ComputeHash();
3592 }
3593
3594 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003595 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003596 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003597 using namespace llvm::support;
3598 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003599 unsigned KeyLen = 1;
3600 switch (Name.getNameKind()) {
3601 case DeclarationName::Identifier:
3602 case DeclarationName::ObjCZeroArgSelector:
3603 case DeclarationName::ObjCOneArgSelector:
3604 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003605 case DeclarationName::CXXLiteralOperatorName:
3606 KeyLen += 4;
3607 break;
3608 case DeclarationName::CXXOperatorName:
3609 KeyLen += 1;
3610 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003611 case DeclarationName::CXXConstructorName:
3612 case DeclarationName::CXXDestructorName:
3613 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003614 case DeclarationName::CXXUsingDirective:
3615 break;
3616 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003617 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003618
3619 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003620 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003621 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003622
3623 return std::make_pair(KeyLen, DataLen);
3624 }
3625
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003626 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003627 using namespace llvm::support;
3628 endian::Writer<little> LE(Out);
3629 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003630 switch (Name.getNameKind()) {
3631 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003632 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003633 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003634 case DeclarationName::ObjCZeroArgSelector:
3635 case DeclarationName::ObjCOneArgSelector:
3636 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003637 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003638 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003639 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003640 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3641 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003642 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003643 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003644 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003645 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003646 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003647 case DeclarationName::CXXConstructorName:
3648 case DeclarationName::CXXDestructorName:
3649 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003650 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003651 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003652 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003653
3654 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003655 }
3656
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003657 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003658 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003659 using namespace llvm::support;
3660 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003661 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003662 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003663 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3664 I != E; ++I)
Richard Smithb3761912015-02-05 23:08:52 +00003665 LE.write<uint32_t>(
3666 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003667
3668 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3669 }
3670};
3671} // end anonymous namespace
3672
Richard Smithcd45dbc2014-04-19 03:48:30 +00003673template<typename Visitor>
Richard Smith4a7e3902015-02-27 03:40:09 +00003674void ASTWriter::visitLocalLookupResults(const DeclContext *ConstDC,
3675 Visitor AddLookupResult) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003676 // FIXME: We need to build the lookups table, which is logically const.
3677 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Richard Smith961eae52014-03-25 01:14:22 +00003678 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3679
Richard Smith961eae52014-03-25 01:14:22 +00003680 SmallVector<DeclarationName, 16> ExternalNames;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003681 for (auto &Lookup : *DC->buildLookup()) {
Richard Smith961eae52014-03-25 01:14:22 +00003682 if (Lookup.second.hasExternalDecls() ||
Richard Smith4a7e3902015-02-27 03:40:09 +00003683 DC->NeedToReconcileExternalVisibleStorage) {
Richard Smithfe620d22015-03-05 23:24:12 +00003684 // If there are no local declarations in our lookup result, we don't
3685 // need to write an entry for the name at all unless we're rewriting
3686 // the decl context. If we can't write out a lookup set without
3687 // performing more deserialization, just skip this entry.
3688 if (!isRewritten(cast<Decl>(DC))) {
3689 bool AllFromASTFile = true;
3690 for (auto *D : Lookup.second.getLookupResult()) {
3691 AllFromASTFile &=
3692 getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile();
3693 if (!AllFromASTFile)
3694 break;
3695 }
3696 if (AllFromASTFile)
3697 continue;
3698 }
3699
Richard Smith961eae52014-03-25 01:14:22 +00003700 // We don't know for sure what declarations are found by this name,
3701 // because the external source might have a different set from the set
3702 // that are in the lookup map, and we can't update it now without
3703 // risking invalidating our lookup iterator. So add it to a queue to
3704 // deal with later.
3705 ExternalNames.push_back(Lookup.first);
3706 continue;
3707 }
3708
3709 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3710 }
3711
3712 // Add the names we needed to defer. Note, this shouldn't add any new decls
3713 // to the list we need to serialize: any new declarations we find here should
3714 // be imported from an external source.
3715 // FIXME: What if the external source isn't an ASTReader?
3716 for (const auto &Name : ExternalNames)
Richard Smithcd45dbc2014-04-19 03:48:30 +00003717 AddLookupResult(Name, DC->lookup(Name));
3718}
3719
3720void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
David Blaikie82e95a32014-11-19 07:49:47 +00003721 if (UpdatedDeclContexts.insert(DC).second && WritingAST) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003722 // Ensure we emit all the visible declarations.
Richard Smith4a7e3902015-02-27 03:40:09 +00003723 visitLocalLookupResults(DC, [&](DeclarationName Name,
3724 DeclContext::lookup_result Result) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003725 for (auto *Decl : Result)
Richard Smithb3761912015-02-05 23:08:52 +00003726 GetDeclRef(getDeclForLocalLookup(getLangOpts(), Decl));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003727 });
3728 }
3729}
3730
3731uint32_t
3732ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3733 llvm::SmallVectorImpl<char> &LookupTable) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003734 assert(!DC->HasLazyLocalLexicalLookups &&
3735 !DC->HasLazyExternalLexicalLookups &&
3736 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003737
3738 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3739 Generator;
3740 ASTDeclContextNameLookupTrait Trait(*this);
3741
3742 // Create the on-disk hash table representation.
3743 DeclarationName ConstructorName;
3744 DeclarationName ConversionName;
3745 SmallVector<NamedDecl *, 8> ConstructorDecls;
3746 SmallVector<NamedDecl *, 4> ConversionDecls;
3747
Richard Smith4a7e3902015-02-27 03:40:09 +00003748 visitLocalLookupResults(DC, [&](DeclarationName Name,
3749 DeclContext::lookup_result Result) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003750 if (Result.empty())
3751 return;
3752
3753 // Different DeclarationName values of certain kinds are mapped to
3754 // identical serialized keys, because we don't want to use type
3755 // identifiers in the keys (since type ids are local to the module).
3756 switch (Name.getNameKind()) {
3757 case DeclarationName::CXXConstructorName:
3758 // There may be different CXXConstructorName DeclarationName values
3759 // in a DeclContext because a UsingDecl that inherits constructors
3760 // has the DeclarationName of the inherited constructors.
3761 if (!ConstructorName)
3762 ConstructorName = Name;
3763 ConstructorDecls.append(Result.begin(), Result.end());
3764 return;
3765
3766 case DeclarationName::CXXConversionFunctionName:
3767 if (!ConversionName)
3768 ConversionName = Name;
3769 ConversionDecls.append(Result.begin(), Result.end());
3770 return;
3771
3772 default:
3773 break;
3774 }
3775
3776 Generator.insert(Name, Result, Trait);
3777 });
Richard Smith961eae52014-03-25 01:14:22 +00003778
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003779 // Add the constructors.
3780 if (!ConstructorDecls.empty()) {
3781 Generator.insert(ConstructorName,
Richard Smith40c78062015-02-21 02:31:57 +00003782 DeclContext::lookup_result(ConstructorDecls),
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003783 Trait);
3784 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003785
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003786 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003787 if (!ConversionDecls.empty()) {
3788 Generator.insert(ConversionName,
Richard Smith40c78062015-02-21 02:31:57 +00003789 DeclContext::lookup_result(ConversionDecls),
Richard Smith961eae52014-03-25 01:14:22 +00003790 Trait);
3791 }
3792
3793 // Create the on-disk hash table in a buffer.
3794 llvm::raw_svector_ostream Out(LookupTable);
3795 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003796 using namespace llvm::support;
3797 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003798 return Generator.Emit(Out, Trait);
3799}
3800
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003801/// \brief Write the block containing all of the declaration IDs
3802/// visible from the given DeclContext.
3803///
3804/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003805/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003806uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3807 DeclContext *DC) {
3808 if (DC->getPrimaryContext() != DC)
3809 return 0;
3810
3811 // Since there is no name lookup into functions or methods, don't bother to
3812 // build a visible-declarations table for these entities.
3813 if (DC->isFunctionOrMethod())
3814 return 0;
3815
3816 // If not in C++, we perform name lookup for the translation unit via the
3817 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003818 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003819 return 0;
3820
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003821 // Serialize the contents of the mapping used for lookup. Note that,
3822 // although we have two very different code paths, the serialized
3823 // representation is the same for both cases: a declaration name,
3824 // followed by a size, followed by references to the visible
3825 // declarations that have that name.
3826 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003827 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003828 if (!Map || Map->empty())
3829 return 0;
3830
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003831 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003832 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003833 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003834
3835 // Write the lookup table
3836 RecordData Record;
3837 Record.push_back(DECL_CONTEXT_VISIBLE);
3838 Record.push_back(BucketOffset);
3839 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003840 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003841 ++NumVisibleDeclContexts;
3842 return Offset;
3843}
3844
Sebastian Redla4071b42010-08-24 00:50:09 +00003845/// \brief Write an UPDATE_VISIBLE block for the given context.
3846///
3847/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3848/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003849/// (in C++), for namespaces, and for classes with forward-declared unscoped
3850/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003851void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003852 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003853 if (!Map || Map->empty())
3854 return;
3855
Sebastian Redla4071b42010-08-24 00:50:09 +00003856 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003857 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003858 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003859
3860 // Write the lookup table
3861 RecordData Record;
3862 Record.push_back(UPDATE_VISIBLE);
3863 Record.push_back(getDeclID(cast<Decl>(DC)));
3864 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003865 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003866}
3867
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003868/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3869void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3870 RecordData Record;
3871 Record.push_back(Opts.fp_contract);
3872 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3873}
3874
3875/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3876void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003877 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003878 return;
3879
3880 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3881 RecordData Record;
3882#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3883#include "clang/Basic/OpenCLExtensions.def"
3884 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3885}
3886
Douglas Gregor358cd442012-01-15 16:58:34 +00003887void ASTWriter::WriteRedeclarations() {
3888 RecordData LocalRedeclChains;
3889 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3890
3891 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3892 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003893 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003894
3895 Decl *MostRecent = First->getMostRecentDecl();
3896
3897 // If we only have a single declaration, there is no point in storing
3898 // a redeclaration chain.
3899 if (First == MostRecent)
3900 continue;
3901
3902 unsigned Offset = LocalRedeclChains.size();
3903 unsigned Size = 0;
3904 LocalRedeclChains.push_back(0); // Placeholder for the size.
3905
3906 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003907 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003908 Prev = Prev->getPreviousDecl()) {
3909 if (!Prev->isFromASTFile()) {
3910 AddDeclRef(Prev, LocalRedeclChains);
3911 ++Size;
3912 }
3913 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003914
Douglas Gregor358cd442012-01-15 16:58:34 +00003915 LocalRedeclChains[Offset] = Size;
3916
3917 // Reverse the set of local redeclarations, so that we store them in
3918 // order (since we found them in reverse order).
3919 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3920
Douglas Gregor6168bd22013-02-18 15:53:43 +00003921 // Add the mapping from the first ID from the AST to the set of local
3922 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003923 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3924 LocalRedeclsMap.push_back(Info);
3925
3926 assert(N == Redeclarations.size() &&
3927 "Deserialized a declaration we shouldn't have");
3928 }
3929
3930 if (LocalRedeclChains.empty())
3931 return;
3932
3933 // Sort the local redeclarations map by the first declaration ID,
3934 // since the reader will be performing binary searches on this information.
3935 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3936
3937 // Emit the local redeclarations map.
3938 using namespace llvm;
3939 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3940 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3943 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3944
3945 RecordData Record;
3946 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3947 Record.push_back(LocalRedeclsMap.size());
3948 Stream.EmitRecordWithBlob(AbbrevID, Record,
3949 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3950 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3951
3952 // Emit the redeclaration chains.
3953 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3954}
3955
Douglas Gregor404cdde2012-01-27 01:47:08 +00003956void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003957 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003958 RecordData Categories;
3959
3960 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3961 unsigned Size = 0;
3962 unsigned StartIndex = Categories.size();
3963
3964 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3965
3966 // Allocate space for the size.
3967 Categories.push_back(0);
3968
3969 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003970 for (ObjCInterfaceDecl::known_categories_iterator
3971 Cat = Class->known_categories_begin(),
3972 CatEnd = Class->known_categories_end();
3973 Cat != CatEnd; ++Cat, ++Size) {
3974 assert(getDeclID(*Cat) != 0 && "Bogus category");
3975 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003976 }
3977
3978 // Update the size.
3979 Categories[StartIndex] = Size;
3980
3981 // Record this interface -> category map.
3982 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3983 CategoriesMap.push_back(CatInfo);
3984 }
3985
3986 // Sort the categories map by the definition ID, since the reader will be
3987 // performing binary searches on this information.
3988 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3989
3990 // Emit the categories map.
3991 using namespace llvm;
3992 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3993 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3996 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3997
3998 RecordData Record;
3999 Record.push_back(OBJC_CATEGORIES_MAP);
4000 Record.push_back(CategoriesMap.size());
4001 Stream.EmitRecordWithBlob(AbbrevID, Record,
4002 reinterpret_cast<char*>(CategoriesMap.data()),
4003 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4004
4005 // Emit the category lists.
4006 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4007}
4008
Richard Smithe40f2ba2013-08-07 21:41:30 +00004009void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4010 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4011
4012 if (LPTMap.empty())
4013 return;
4014
4015 RecordData Record;
4016 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4017 ItEnd = LPTMap.end();
4018 It != ItEnd; ++It) {
4019 LateParsedTemplate *LPT = It->second;
4020 AddDeclRef(It->first, Record);
4021 AddDeclRef(LPT->D, Record);
4022 Record.push_back(LPT->Toks.size());
4023
4024 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4025 TokEnd = LPT->Toks.end();
4026 TokIt != TokEnd; ++TokIt) {
4027 AddToken(*TokIt, Record);
4028 }
4029 }
4030 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4031}
4032
Dario Domizioli13a0a382014-05-23 12:13:25 +00004033/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4034void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4035 RecordData Record;
4036 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4037 AddSourceLocation(PragmaLoc, Record);
4038 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4039}
4040
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004041//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004042// General Serialization Routines
4043//===----------------------------------------------------------------------===//
4044
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004045/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004046void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4047 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004048 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004049 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4050 e = Attrs.end(); i != e; ++i){
4051 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004052 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004053 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004054
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004055#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004056
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004057 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004058}
4059
John McCallf413f5e2013-05-03 00:10:13 +00004060void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4061 AddSourceLocation(Tok.getLocation(), Record);
4062 Record.push_back(Tok.getLength());
4063
4064 // FIXME: When reading literal tokens, reconstruct the literal pointer
4065 // if it is needed.
4066 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4067 // FIXME: Should translate token kind to a stable encoding.
4068 Record.push_back(Tok.getKind());
4069 // FIXME: Should translate token flags to a stable encoding.
4070 Record.push_back(Tok.getFlags());
4071}
4072
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004073void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004074 Record.push_back(Str.size());
4075 Record.insert(Record.end(), Str.begin(), Str.end());
4076}
4077
Richard Smith7ed1bc92014-12-05 22:42:13 +00004078bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004079 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004080
Richard Smith54cc3c22014-12-11 20:50:24 +00004081 bool Changed =
4082 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004083
4084 // Remove a prefix to make the path relative, if relevant.
4085 const char *PathBegin = Path.data();
4086 const char *PathPtr =
4087 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4088 if (PathPtr != PathBegin) {
4089 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4090 Changed = true;
4091 }
4092
4093 return Changed;
4094}
4095
4096void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4097 SmallString<128> FilePath(Path);
4098 PreparePathForOutput(FilePath);
4099 AddString(FilePath, Record);
4100}
4101
4102void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
4103 StringRef Path) {
4104 SmallString<128> FilePath(Path);
4105 PreparePathForOutput(FilePath);
4106 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4107}
4108
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004109void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4110 RecordDataImpl &Record) {
4111 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004112 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004113 Record.push_back(*Minor + 1);
4114 else
4115 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004116 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004117 Record.push_back(*Subminor + 1);
4118 else
4119 Record.push_back(0);
4120}
4121
Douglas Gregore84a9da2009-04-20 20:36:09 +00004122/// \brief Note that the identifier II occurs at the given offset
4123/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004124void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004125 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004126 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004127 // up earlier in the chain and thus don't need an offset.
4128 if (ID >= FirstIdentID)
4129 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004130}
4131
Douglas Gregor95c13f52009-04-25 17:48:32 +00004132/// \brief Note that the selector Sel occurs at the given offset
4133/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004134void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004135 unsigned ID = SelectorIDs[Sel];
4136 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004137 // Don't record offsets for selectors that are also available in a different
4138 // file.
4139 if (ID < FirstSelectorID)
4140 return;
4141 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004142}
4143
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004144ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00004145 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4146 WritingModule(nullptr), WritingAST(false),
4147 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4148 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4149 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4150 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4151 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4152 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4153 NextSubmoduleID(FirstSubmoduleID),
4154 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4155 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4156 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4157 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4158 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4159 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004160 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004161 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004162 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4163 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4164 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004165
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004166ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004167 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004168}
4169
Richard Smithb3761912015-02-05 23:08:52 +00004170const LangOptions &ASTWriter::getLangOpts() const {
4171 assert(WritingAST && "can't determine lang opts when not writing AST");
4172 return Context->getLangOpts();
4173}
4174
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004175void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004176 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004177 Module *WritingModule, StringRef isysroot,
4178 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004179 WritingAST = true;
4180
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004181 ASTHasCompilerErrors = hasErrors;
4182
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004183 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004184 Stream.Emit((unsigned)'C', 8);
4185 Stream.Emit((unsigned)'P', 8);
4186 Stream.Emit((unsigned)'C', 8);
4187 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004188
Chris Lattner28fa4e62009-04-26 22:26:21 +00004189 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004190
Douglas Gregoreda8e122011-08-09 15:13:55 +00004191 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004192 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004193 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004194 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004195 Context = nullptr;
4196 PP = nullptr;
4197 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004198 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004199
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004200 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004201}
4202
Douglas Gregora94a1542011-07-27 21:45:57 +00004203template<typename Vector>
4204static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4205 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004206 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4207 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004208 Writer.AddDeclRef(*I, Record);
4209 }
4210}
4211
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004212void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004213 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004214 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004215 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004216 using namespace llvm;
4217
Craig Toppera13603a2014-05-22 05:54:18 +00004218 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004219
Douglas Gregorcf68c582011-12-01 22:20:10 +00004220 // Make sure that the AST reader knows to finalize itself.
4221 if (Chain)
4222 Chain->finalizeForWriting();
4223
Sebastian Redl143413f2010-07-12 22:02:52 +00004224 ASTContext &Context = SemaRef.Context;
4225 Preprocessor &PP = SemaRef.PP;
4226
Douglas Gregordab42432011-08-12 00:15:20 +00004227 // Set up predefined declaration IDs.
4228 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004229 if (Context.ObjCIdDecl)
4230 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004231 if (Context.ObjCSelDecl)
4232 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004233 if (Context.ObjCClassDecl)
4234 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004235 if (Context.ObjCProtocolClassDecl)
4236 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004237 if (Context.Int128Decl)
4238 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4239 if (Context.UInt128Decl)
4240 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004241 if (Context.ObjCInstanceTypeDecl)
4242 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004243 if (Context.BuiltinVaListDecl)
4244 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
Richard Smithf19e1272015-03-07 00:04:49 +00004245 if (Context.ExternCContext)
4246 DeclIDs[Context.ExternCContext] = PREDEF_DECL_EXTERN_C_CONTEXT_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004247
Douglas Gregor851443c2011-08-12 01:39:19 +00004248 if (!Chain) {
4249 // Make sure that we emit IdentifierInfos (and any attached
4250 // declarations) for builtins. We don't need to do this when we're
4251 // emitting chained PCH files, because all of the builtins will be
4252 // in the original PCH file.
4253 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004254 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004255 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00004256 if (!Context.getLangOpts().NoBuiltin) {
4257 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4258 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004259 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4260 getIdentifierRef(&Table.get(BuiltinNames[I]));
4261 }
4262
Richard Smithcd45dbc2014-04-19 03:48:30 +00004263 // If we saw any DeclContext updates before we started writing the AST file,
4264 // make sure all visible decls in those DeclContexts are written out.
4265 if (!UpdatedDeclContexts.empty()) {
4266 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4267 UpdatedDeclContexts.clear();
4268 for (auto *DC : OldUpdatedDeclContexts)
4269 AddUpdatedDeclContext(DC);
4270 }
4271
Chris Lattner0c797362009-09-08 18:19:27 +00004272 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004273 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004274 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004275 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004276 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004277
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004278 // Build a record containing all of the file scoped decls in this file.
4279 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004280 if (!isModule)
4281 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4282 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004283
Douglas Gregor851443c2011-08-12 01:39:19 +00004284 // Build a record containing all of the delegating constructors we still need
4285 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004286 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004287 if (!isModule)
4288 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004289
Douglas Gregor851443c2011-08-12 01:39:19 +00004290 // Write the set of weak, undeclared identifiers. We always write the
4291 // entire table, since later PCH files in a PCH chain are only interested in
4292 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004293 RecordData WeakUndeclaredIdentifiers;
4294 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004295 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004296 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4297 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4298 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4299 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4300 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4301 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4302 }
4303 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004304
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004305 // Build a record containing all of the ext_vector declarations.
4306 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004307 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004308
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004309 // Build a record containing all of the VTable uses information.
4310 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004311 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004312 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4313 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4314 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4315 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4316 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004317 }
4318
Nico Weber72889432014-09-06 01:25:55 +00004319 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4320 RecordData UnusedLocalTypedefNameCandidates;
4321 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4322 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4323
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004324 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004325 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004326 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004327 I = SemaRef.PendingInstantiations.begin(),
4328 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4329 AddDeclRef(I->first, PendingInstantiations);
4330 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004331 }
4332 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4333 "There are local ones at end of translation unit!");
4334
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004335 // Build a record containing some declaration references.
4336 RecordData SemaDeclRefs;
4337 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4338 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4339 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4340 }
4341
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004342 RecordData CUDASpecialDeclRefs;
4343 if (Context.getcudaConfigureCallDecl()) {
4344 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4345 }
4346
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004347 // Build a record containing all of the known namespaces.
4348 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004349 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004350 I = SemaRef.KnownNamespaces.begin(),
4351 IEnd = SemaRef.KnownNamespaces.end();
4352 I != IEnd; ++I) {
4353 if (!I->second)
4354 AddDeclRef(I->first, KnownNamespaces);
4355 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004356
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004357 // Build a record of all used, undefined objects that require definitions.
4358 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004359
4360 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004361 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004362 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4363 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004364 AddDeclRef(I->first, UndefinedButUsed);
4365 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004366 }
4367
Douglas Gregor112b9072012-10-18 05:31:06 +00004368 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004369 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004370
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004371 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004372 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004373 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004374
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004375 // This is so that older clang versions, before the introduction
4376 // of the control block, can read and reject the newer PCH format.
4377 Record.clear();
4378 Record.push_back(VERSION_MAJOR);
4379 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4380
Douglas Gregor851443c2011-08-12 01:39:19 +00004381 // Create a lexical update block containing all of the declarations in the
4382 // translation unit that do not come from other AST files.
4383 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4384 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004385 for (const auto *I : TU->noload_decls()) {
4386 if (!I->isFromASTFile())
4387 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004388 }
4389
4390 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4391 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4392 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4393 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4394 Record.clear();
4395 Record.push_back(TU_UPDATE_LEXICAL);
4396 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4397 data(NewGlobalDecls));
4398
4399 // And a visible updates block for the translation unit.
4400 Abv = new llvm::BitCodeAbbrev();
4401 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4402 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4403 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4404 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4405 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4406 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004407
4408 // If we have any extern "C" names, write out a visible update for them.
4409 if (Context.ExternCContext)
4410 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004411
4412 // If the translation unit has an anonymous namespace, and we don't already
4413 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004414 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004415 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4416 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004417 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004418 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004419 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004420
Richard Smith5652c0f2014-03-21 01:48:23 +00004421 // Add update records for all mangling numbers and static local numbers.
4422 // These aren't really update records, but this is a convenient way of
4423 // tagging this rare extra data onto the declarations.
4424 for (const auto &Number : Context.MangleNumbers)
4425 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004426 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4427 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004428 for (const auto &Number : Context.StaticLocalNumbers)
4429 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004430 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4431 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004432
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004433 // Make sure visible decls, added to DeclContexts previously loaded from
4434 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004435 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004436 I = UpdatingVisibleDecls.begin(),
4437 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4438 GetDeclRef(*I);
4439 }
4440
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004441 // Make sure all decls associated with an identifier are registered for
4442 // serialization.
Richard Smith9ab4cce2015-03-11 00:00:51 +00004443 llvm::SmallVector<const IdentifierInfo*, 256> IIsToVisit;
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004444 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4445 IDEnd = PP.getIdentifierTable().end();
4446 ID != IDEnd; ++ID) {
4447 const IdentifierInfo *II = ID->second;
Richard Smith9ab4cce2015-03-11 00:00:51 +00004448 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4449 IIsToVisit.push_back(II);
4450 }
4451 for (const IdentifierInfo *II : IIsToVisit) {
4452 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4453 DEnd = SemaRef.IdResolver.end();
4454 D != DEnd; ++D) {
4455 GetDeclRef(*D);
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004456 }
4457 }
4458
Douglas Gregor5204bde2011-08-02 16:26:37 +00004459 // Form the record of special types.
4460 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004461 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004462 AddTypeRef(Context.getFILEType(), SpecialTypes);
4463 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4464 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4465 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4466 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004467 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004468 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004469
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004470 if (Chain) {
4471 // Write the mapping information describing our module dependencies and how
4472 // each of those modules were mapped into our own offset/ID space, so that
4473 // the reader can build the appropriate mapping to its own offset/ID space.
4474 // The map consists solely of a blob with the following format:
4475 // *(module-name-len:i16 module-name:len*i8
4476 // source-location-offset:i32
4477 // identifier-id:i32
4478 // preprocessed-entity-id:i32
4479 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004480 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004481 // selector-id:i32
4482 // declaration-id:i32
4483 // c++-base-specifiers-id:i32
4484 // type-id:i32)
4485 //
4486 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4487 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4489 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004490 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004491 {
4492 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004493 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004494 using namespace llvm::support;
4495 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004496 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004497 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004498 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004499
Ben Langmuir785180e2014-10-20 16:27:30 +00004500 // Note: if a base ID was uint max, it would not be possible to load
4501 // another module after it or have more than one entity inside it.
4502 uint32_t None = std::numeric_limits<uint32_t>::max();
4503
4504 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4505 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4506 if (ShouldWrite)
4507 LE.write<uint32_t>(BaseID);
4508 else
4509 LE.write<uint32_t>(None);
4510 };
4511
Ben Langmuirfe971d92014-08-16 04:54:18 +00004512 // These values should be unique within a chain, since they will be read
4513 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004514 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4515 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4516 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4517 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4518 M->NumPreprocessedEntities);
4519 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4520 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4521 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4522 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004523 }
4524 }
4525 Record.clear();
4526 Record.push_back(MODULE_OFFSET_MAP);
4527 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4528 Buffer.data(), Buffer.size());
4529 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004530
Richard Smithb9eab6d2014-03-20 19:44:17 +00004531 RecordData DeclUpdatesOffsetsRecord;
4532
Richard Smith59442b42014-03-20 20:07:19 +00004533 // Keep writing types, declarations, and declaration update records
4534 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004535 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4536 WriteTypeAbbrevs();
4537 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004538 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4539 E = DeclsToRewrite.end();
4540 I != E; ++I)
4541 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004542 do {
4543 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4544 while (!DeclTypesToEmit.empty()) {
4545 DeclOrType DOT = DeclTypesToEmit.front();
4546 DeclTypesToEmit.pop();
4547 if (DOT.isType())
4548 WriteType(DOT.getType());
4549 else
4550 WriteDecl(Context, DOT.getDecl());
4551 }
4552 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004553 Stream.ExitBlock();
4554
Richard Smithb9eab6d2014-03-20 19:44:17 +00004555 DoneWritingDeclsAndTypes = true;
4556
4557 // These things can only be done once we've written out decls and types.
4558 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004559 if (!DeclUpdatesOffsetsRecord.empty())
4560 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004561 WriteCXXBaseSpecifiersOffsets();
4562 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004563 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004564
4565 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004566 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004567 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004568 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004569 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004570 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004571 WriteFPPragmaOptions(SemaRef.getFPOptions());
4572 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004573 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004574
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004575 // If we're emitting a module, write out the submodule information.
4576 if (WritingModule)
4577 WriteSubmodules(WritingModule);
4578
Douglas Gregor5204bde2011-08-02 16:26:37 +00004579 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4580
Douglas Gregord4df8652009-04-22 22:02:47 +00004581 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004582 if (!EagerlyDeserializedDecls.empty())
4583 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004584
4585 // Write the record containing tentative definitions.
4586 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004587 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004588
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004589 // Write the record containing unused file scoped decls.
4590 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004591 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004592
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004593 // Write the record containing weak undeclared identifiers.
4594 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004595 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004596 WeakUndeclaredIdentifiers);
4597
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004598 // Write the record containing ext_vector type names.
4599 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004600 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004601
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004602 // Write the record containing VTable uses information.
4603 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004604 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004605
Nico Weber72889432014-09-06 01:25:55 +00004606 // Write the record containing potentially unused local typedefs.
4607 if (!UnusedLocalTypedefNameCandidates.empty())
4608 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4609 UnusedLocalTypedefNameCandidates);
4610
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004611 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004612 if (!PendingInstantiations.empty())
4613 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004614
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004615 // Write the record containing declaration references of Sema.
4616 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004617 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004618
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004619 // Write the record containing CUDA-specific declaration references.
4620 if (!CUDASpecialDeclRefs.empty())
4621 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004622
4623 // Write the delegating constructors.
4624 if (!DelegatingCtorDecls.empty())
4625 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004626
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004627 // Write the known namespaces.
4628 if (!KnownNamespaces.empty())
4629 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004630
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004631 // Write the undefined internal functions and variables, and inline functions.
4632 if (!UndefinedButUsed.empty())
4633 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004634
Douglas Gregor851443c2011-08-12 01:39:19 +00004635 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004636 for (auto *DC : UpdatedDeclContexts)
4637 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004638
Douglas Gregor959bb062011-12-03 01:15:29 +00004639 if (!WritingModule) {
4640 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004641 struct ModuleInfo {
4642 uint64_t ID;
4643 Module *M;
4644 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4645 };
Richard Smith56be7542014-03-21 00:33:59 +00004646 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004647 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004648 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004649 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4650 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004651 }
Richard Smith56be7542014-03-21 00:33:59 +00004652
4653 if (!Imports.empty()) {
4654 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4655 return A.ID < B.ID;
4656 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004657 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4658 return A.ID == B.ID;
4659 };
Richard Smith56be7542014-03-21 00:33:59 +00004660
4661 // Sort and deduplicate module IDs.
4662 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004663 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004664 Imports.end());
4665
4666 RecordData ImportedModules;
4667 for (const auto &Import : Imports) {
4668 ImportedModules.push_back(Import.ID);
4669 // FIXME: If the module has macros imported then later has declarations
4670 // imported, this location won't be the right one as a location for the
4671 // declaration imports.
4672 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4673 }
4674
Douglas Gregor959bb062011-12-03 01:15:29 +00004675 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4676 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004677 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004678
Douglas Gregor851443c2011-08-12 01:39:19 +00004679 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004680 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004681 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004682 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004683 if(!WritingModule)
4684 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004685
Douglas Gregor08f01292009-04-17 22:13:46 +00004686 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004687 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004688 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004689 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004690 Record.push_back(NumLexicalDeclContexts);
4691 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004692 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004693 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004694}
4695
Richard Smithb9eab6d2014-03-20 19:44:17 +00004696void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004697 if (DeclUpdates.empty())
4698 return;
4699
Richard Smith59442b42014-03-20 20:07:19 +00004700 DeclUpdateMap LocalUpdates;
4701 LocalUpdates.swap(DeclUpdates);
4702
Richard Smith6ef42932014-03-20 21:02:00 +00004703 for (auto &DeclUpdate : LocalUpdates) {
4704 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004705 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004706 continue; // The decl will be written completely,no need to store updates.
4707
Richard Smithd28ac5b2014-03-22 23:33:22 +00004708 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004709 RecordData Record;
4710 for (auto &Update : DeclUpdate.second) {
4711 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4712
4713 Record.push_back(Kind);
4714 switch (Kind) {
4715 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4716 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4717 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004718 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004719 Record.push_back(GetDeclRef(Update.getDecl()));
4720 break;
4721
Richard Smith4d235792014-08-07 18:53:08 +00004722 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004723 // An updated body is emitted last, so that the reader doesn't need
4724 // to skip over the lazy body to reach statements for other records.
4725 Record.pop_back();
4726 HasUpdatedBody = true;
4727 break;
4728
Richard Smith4d235792014-08-07 18:53:08 +00004729 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4730 AddSourceLocation(Update.getLoc(), Record);
4731 break;
4732
Richard Smithcd45dbc2014-04-19 03:48:30 +00004733 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4734 auto *RD = cast<CXXRecordDecl>(D);
4735 AddUpdatedDeclContext(RD->getPrimaryContext());
4736 AddCXXDefinitionData(RD, Record);
4737 Record.push_back(WriteDeclContextLexicalBlock(
4738 *Context, const_cast<CXXRecordDecl *>(RD)));
4739
4740 // This state is sometimes updated by template instantiation, when we
4741 // switch from the specialization referring to the template declaration
4742 // to it referring to the template definition.
4743 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4744 Record.push_back(MSInfo->getTemplateSpecializationKind());
4745 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4746 } else {
4747 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4748 Record.push_back(Spec->getTemplateSpecializationKind());
4749 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004750
4751 // The instantiation might have been resolved to a partial
4752 // specialization. If so, record which one.
4753 auto From = Spec->getInstantiatedFrom();
4754 if (auto PartialSpec =
4755 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4756 Record.push_back(true);
4757 AddDeclRef(PartialSpec, Record);
4758 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4759 Record);
4760 } else {
4761 Record.push_back(false);
4762 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004763 }
4764 Record.push_back(RD->getTagKind());
4765 AddSourceLocation(RD->getLocation(), Record);
4766 AddSourceLocation(RD->getLocStart(), Record);
4767 AddSourceLocation(RD->getRBraceLoc(), Record);
4768
4769 // Instantiation may change attributes; write them all out afresh.
4770 Record.push_back(D->hasAttrs());
4771 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004772 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4773 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004774
4775 // FIXME: Ensure we don't get here for explicit instantiations.
4776 break;
4777 }
4778
Richard Smithf8134002015-03-10 01:41:22 +00004779 case UPD_CXX_RESOLVED_DTOR_DELETE:
4780 AddDeclRef(Update.getDecl(), Record);
4781 break;
4782
Richard Smith564417a2014-03-20 21:47:22 +00004783 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4784 addExceptionSpec(
4785 *this,
4786 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4787 Record);
4788 break;
4789
Richard Smith6ef42932014-03-20 21:02:00 +00004790 case UPD_CXX_DEDUCED_RETURN_TYPE:
4791 Record.push_back(GetOrCreateTypeID(Update.getType()));
4792 break;
4793
4794 case UPD_DECL_MARKED_USED:
4795 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004796
4797 case UPD_MANGLING_NUMBER:
4798 case UPD_STATIC_LOCAL_NUMBER:
4799 Record.push_back(Update.getNumber());
4800 break;
Alexey Bataev97720002014-11-11 04:05:39 +00004801 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4802 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4803 Record);
4804 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004805 }
4806 }
4807
Richard Smithd28ac5b2014-03-22 23:33:22 +00004808 if (HasUpdatedBody) {
4809 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004810 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004811 Record.push_back(Def->isInlined());
4812 AddSourceLocation(Def->getInnerLocStart(), Record);
4813 AddFunctionDefinition(Def, Record);
4814 }
4815
Richard Smithcd45dbc2014-04-19 03:48:30 +00004816 OffsetsRecord.push_back(GetDeclRef(D));
4817 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4818
Richard Smith6ef42932014-03-20 21:02:00 +00004819 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004820
Richard Smithb9eab6d2014-03-20 19:44:17 +00004821 // Flush any statements that were written as part of this update record.
4822 FlushStmts();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004823
4824 // Flush C++ base specifiers, if there are any.
4825 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004826 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004827}
4828
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004829void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004830 if (ReplacedDecls.empty())
4831 return;
4832
4833 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004834 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4835 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004836 Record.push_back(I->ID);
4837 Record.push_back(I->Offset);
4838 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004839 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004840 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004841}
4842
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004843void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004844 Record.push_back(Loc.getRawEncoding());
4845}
4846
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004847void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004848 AddSourceLocation(Range.getBegin(), Record);
4849 AddSourceLocation(Range.getEnd(), Record);
4850}
4851
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004852void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004853 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004854 const uint64_t *Words = Value.getRawData();
4855 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004856}
4857
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004858void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004859 Record.push_back(Value.isUnsigned());
4860 AddAPInt(Value, Record);
4861}
4862
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004863void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004864 AddAPInt(Value.bitcastToAPInt(), Record);
4865}
4866
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004867void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004868 Record.push_back(getIdentifierRef(II));
4869}
4870
Sebastian Redl539c5062010-08-18 23:57:32 +00004871IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004872 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004873 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004874
Sebastian Redl539c5062010-08-18 23:57:32 +00004875 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004876 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004877 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004878 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004879}
4880
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004881MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004882 // Don't emit builtin macros like __LINE__ to the AST file unless they
4883 // have been redefined by the header (in which case they are not
4884 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004885 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004886 return 0;
4887
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004888 MacroID &ID = MacroIDs[MI];
4889 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004890 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004891 MacroInfoToEmitData Info = { Name, MI, ID };
4892 MacroInfosToEmit.push_back(Info);
4893 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004894 return ID;
4895}
4896
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004897MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004898 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004899 return 0;
4900
4901 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4902 return MacroIDs[MI];
4903}
4904
4905uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4906 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4907 return IdentMacroDirectivesOffsetMap[Name];
4908}
4909
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004910void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004911 Record.push_back(getSelectorRef(SelRef));
4912}
4913
Sebastian Redl539c5062010-08-18 23:57:32 +00004914SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004915 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004916 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004917 }
4918
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004919 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004920 if (SID == 0 && Chain) {
4921 // This might trigger a ReadSelector callback, which will set the ID for
4922 // this selector.
4923 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004924 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004925 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004926 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004927 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004928 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004929 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004930 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004931}
4932
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004933void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004934 AddDeclRef(Temp->getDestructor(), Record);
4935}
4936
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004937void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4938 CXXBaseSpecifier const *BasesEnd,
4939 RecordDataImpl &Record) {
4940 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4941 CXXBaseSpecifiersToWrite.push_back(
4942 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4943 Bases, BasesEnd));
4944 Record.push_back(NextCXXBaseSpecifiersID++);
4945}
4946
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004947void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004948 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004949 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004950 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004951 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004952 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004953 break;
4954 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004955 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004956 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004957 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004958 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004959 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004960 break;
4961 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004962 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004963 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004964 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004965 break;
John McCall0ad16662009-10-29 08:12:44 +00004966 case TemplateArgument::Null:
4967 case TemplateArgument::Integral:
4968 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004969 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004970 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004971 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004972 break;
4973 }
4974}
4975
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004976void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004977 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004978 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004979
4980 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4981 bool InfoHasSameExpr
4982 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4983 Record.push_back(InfoHasSameExpr);
4984 if (InfoHasSameExpr)
4985 return; // Avoid storing the same expr twice.
4986 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004987 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4988 Record);
4989}
4990
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004991void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4992 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004993 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004994 AddTypeRef(QualType(), Record);
4995 return;
4996 }
4997
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004998 AddTypeLoc(TInfo->getTypeLoc(), Record);
4999}
5000
5001void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
5002 AddTypeRef(TL.getType(), Record);
5003
John McCall8f115c62009-10-16 21:56:05 +00005004 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005005 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005006 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005007}
5008
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005009void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005010 Record.push_back(GetOrCreateTypeID(T));
5011}
5012
Richard Smith2095ffe2015-03-16 20:11:03 +00005013TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005014 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005015 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5016 if (T.isNull())
5017 return TypeIdx();
5018 assert(!T.getLocalFastQualifiers());
5019
5020 TypeIdx &Idx = TypeIdxs[T];
5021 if (Idx.getIndex() == 0) {
5022 if (DoneWritingDeclsAndTypes) {
5023 assert(0 && "New type seen after serializing all the types to emit!");
5024 return TypeIdx();
5025 }
5026
5027 // We haven't seen this type before. Assign it a new ID and put it
5028 // into the queue of types to emit.
5029 Idx = TypeIdx(NextTypeID++);
5030 DeclTypesToEmit.push(T);
5031 }
5032 return Idx;
5033 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005034}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005035
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005036TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005037 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005038 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5039 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005040 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005041 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005042
Richard Smith2095ffe2015-03-16 20:11:03 +00005043 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5044 assert(I != TypeIdxs.end() && "Type not emitted!");
5045 return I->second;
5046 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005047}
5048
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005049void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005050 Record.push_back(GetDeclRef(D));
5051}
5052
Sebastian Redl539c5062010-08-18 23:57:32 +00005053DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005054 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005055
5056 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005057 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005058 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005059
5060 // If D comes from an AST file, its declaration ID is already known and
5061 // fixed.
5062 if (D->isFromASTFile())
5063 return D->getGlobalID();
5064
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005065 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005066 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005067 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005068 if (DoneWritingDeclsAndTypes) {
5069 assert(0 && "New decl seen after serializing all the decls to emit!");
5070 return 0;
5071 }
5072
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005073 // We haven't seen this declaration before. Give it a new ID and
5074 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005075 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005076 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005077 }
5078
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005079 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005080}
5081
Sebastian Redl539c5062010-08-18 23:57:32 +00005082DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005083 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005084 return 0;
5085
Douglas Gregorb3163e52012-01-05 22:33:30 +00005086 // If D comes from an AST file, its declaration ID is already known and
5087 // fixed.
5088 if (D->isFromASTFile())
5089 return D->getGlobalID();
5090
Douglas Gregore84a9da2009-04-20 20:36:09 +00005091 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5092 return DeclIDs[D];
5093}
5094
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005095void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005096 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005097 assert(D);
5098
5099 SourceLocation Loc = D->getLocation();
5100 if (Loc.isInvalid())
5101 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005102
5103 // We only keep track of the file-level declarations of each file.
5104 if (!D->getLexicalDeclContext()->isFileContext())
5105 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005106 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5107 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005108 if (isa<ParmVarDecl>(D))
5109 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005110
5111 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005112 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005113 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005114 FileID FID;
5115 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005116 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005117 if (FID.isInvalid())
5118 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005119 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005120
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005121 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005122 if (!Info)
5123 Info = new DeclIDInFileInfo();
5124
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005125 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005126 LocDeclIDsTy &Decls = Info->DeclIDs;
5127
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005128 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005129 Decls.push_back(LocDecl);
5130 return;
5131 }
5132
Benjamin Kramer45025c02013-08-24 13:22:59 +00005133 LocDeclIDsTy::iterator I =
5134 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005135
5136 Decls.insert(I, LocDecl);
5137}
5138
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005139void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005140 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005141 Record.push_back(Name.getNameKind());
5142 switch (Name.getNameKind()) {
5143 case DeclarationName::Identifier:
5144 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5145 break;
5146
5147 case DeclarationName::ObjCZeroArgSelector:
5148 case DeclarationName::ObjCOneArgSelector:
5149 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005150 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005151 break;
5152
5153 case DeclarationName::CXXConstructorName:
5154 case DeclarationName::CXXDestructorName:
5155 case DeclarationName::CXXConversionFunctionName:
5156 AddTypeRef(Name.getCXXNameType(), Record);
5157 break;
5158
5159 case DeclarationName::CXXOperatorName:
5160 Record.push_back(Name.getCXXOverloadedOperator());
5161 break;
5162
Alexis Hunt3d221f22009-11-29 07:34:05 +00005163 case DeclarationName::CXXLiteralOperatorName:
5164 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5165 break;
5166
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005167 case DeclarationName::CXXUsingDirective:
5168 // No extra data to emit
5169 break;
5170 }
5171}
Chris Lattnerca025db2010-05-07 21:43:38 +00005172
Richard Smithd08aeb62014-08-28 01:33:39 +00005173unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5174 assert(needsAnonymousDeclarationNumber(D) &&
5175 "expected an anonymous declaration");
5176
5177 // Number the anonymous declarations within this context, if we've not
5178 // already done so.
5179 auto It = AnonymousDeclarationNumbers.find(D);
5180 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005181 auto *DC = D->getLexicalDeclContext();
5182 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5183 AnonymousDeclarationNumbers[ND] = Number;
5184 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005185
5186 It = AnonymousDeclarationNumbers.find(D);
5187 assert(It != AnonymousDeclarationNumbers.end() &&
5188 "declaration not found within its lexical context");
5189 }
5190
5191 return It->second;
5192}
5193
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005194void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005195 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005196 switch (Name.getNameKind()) {
5197 case DeclarationName::CXXConstructorName:
5198 case DeclarationName::CXXDestructorName:
5199 case DeclarationName::CXXConversionFunctionName:
5200 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5201 break;
5202
5203 case DeclarationName::CXXOperatorName:
5204 AddSourceLocation(
5205 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5206 Record);
5207 AddSourceLocation(
5208 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5209 Record);
5210 break;
5211
5212 case DeclarationName::CXXLiteralOperatorName:
5213 AddSourceLocation(
5214 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5215 Record);
5216 break;
5217
5218 case DeclarationName::Identifier:
5219 case DeclarationName::ObjCZeroArgSelector:
5220 case DeclarationName::ObjCOneArgSelector:
5221 case DeclarationName::ObjCMultiArgSelector:
5222 case DeclarationName::CXXUsingDirective:
5223 break;
5224 }
5225}
5226
5227void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005228 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005229 AddDeclarationName(NameInfo.getName(), Record);
5230 AddSourceLocation(NameInfo.getLoc(), Record);
5231 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5232}
5233
5234void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005235 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005236 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005237 Record.push_back(Info.NumTemplParamLists);
5238 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5239 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5240}
5241
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005242void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005243 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005244 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005245 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005246 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005247
5248 // Push each of the NNS's onto a stack for serialization in reverse order.
5249 while (NNS) {
5250 NestedNames.push_back(NNS);
5251 NNS = NNS->getPrefix();
5252 }
5253
5254 Record.push_back(NestedNames.size());
5255 while(!NestedNames.empty()) {
5256 NNS = NestedNames.pop_back_val();
5257 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5258 Record.push_back(Kind);
5259 switch (Kind) {
5260 case NestedNameSpecifier::Identifier:
5261 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5262 break;
5263
5264 case NestedNameSpecifier::Namespace:
5265 AddDeclRef(NNS->getAsNamespace(), Record);
5266 break;
5267
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005268 case NestedNameSpecifier::NamespaceAlias:
5269 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5270 break;
5271
Chris Lattnerca025db2010-05-07 21:43:38 +00005272 case NestedNameSpecifier::TypeSpec:
5273 case NestedNameSpecifier::TypeSpecWithTemplate:
5274 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5275 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5276 break;
5277
5278 case NestedNameSpecifier::Global:
5279 // Don't need to write an associated value.
5280 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005281
5282 case NestedNameSpecifier::Super:
5283 AddDeclRef(NNS->getAsRecordDecl(), Record);
5284 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005285 }
5286 }
5287}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005288
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005289void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5290 RecordDataImpl &Record) {
5291 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005292 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005293 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005294
5295 // Push each of the nested-name-specifiers's onto a stack for
5296 // serialization in reverse order.
5297 while (NNS) {
5298 NestedNames.push_back(NNS);
5299 NNS = NNS.getPrefix();
5300 }
5301
5302 Record.push_back(NestedNames.size());
5303 while(!NestedNames.empty()) {
5304 NNS = NestedNames.pop_back_val();
5305 NestedNameSpecifier::SpecifierKind Kind
5306 = NNS.getNestedNameSpecifier()->getKind();
5307 Record.push_back(Kind);
5308 switch (Kind) {
5309 case NestedNameSpecifier::Identifier:
5310 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5311 AddSourceRange(NNS.getLocalSourceRange(), Record);
5312 break;
5313
5314 case NestedNameSpecifier::Namespace:
5315 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5316 AddSourceRange(NNS.getLocalSourceRange(), Record);
5317 break;
5318
5319 case NestedNameSpecifier::NamespaceAlias:
5320 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5321 AddSourceRange(NNS.getLocalSourceRange(), Record);
5322 break;
5323
5324 case NestedNameSpecifier::TypeSpec:
5325 case NestedNameSpecifier::TypeSpecWithTemplate:
5326 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5327 AddTypeLoc(NNS.getTypeLoc(), Record);
5328 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5329 break;
5330
5331 case NestedNameSpecifier::Global:
5332 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5333 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005334
5335 case NestedNameSpecifier::Super:
5336 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5337 AddSourceRange(NNS.getLocalSourceRange(), Record);
5338 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005339 }
5340 }
5341}
5342
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005343void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005344 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005345 Record.push_back(Kind);
5346 switch (Kind) {
5347 case TemplateName::Template:
5348 AddDeclRef(Name.getAsTemplateDecl(), Record);
5349 break;
5350
5351 case TemplateName::OverloadedTemplate: {
5352 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5353 Record.push_back(OvT->size());
5354 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5355 I != E; ++I)
5356 AddDeclRef(*I, Record);
5357 break;
5358 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005359
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005360 case TemplateName::QualifiedTemplate: {
5361 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5362 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5363 Record.push_back(QualT->hasTemplateKeyword());
5364 AddDeclRef(QualT->getTemplateDecl(), Record);
5365 break;
5366 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005367
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005368 case TemplateName::DependentTemplate: {
5369 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5370 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5371 Record.push_back(DepT->isIdentifier());
5372 if (DepT->isIdentifier())
5373 AddIdentifierRef(DepT->getIdentifier(), Record);
5374 else
5375 Record.push_back(DepT->getOperator());
5376 break;
5377 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005378
5379 case TemplateName::SubstTemplateTemplateParm: {
5380 SubstTemplateTemplateParmStorage *subst
5381 = Name.getAsSubstTemplateTemplateParm();
5382 AddDeclRef(subst->getParameter(), Record);
5383 AddTemplateName(subst->getReplacement(), Record);
5384 break;
5385 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005386
5387 case TemplateName::SubstTemplateTemplateParmPack: {
5388 SubstTemplateTemplateParmPackStorage *SubstPack
5389 = Name.getAsSubstTemplateTemplateParmPack();
5390 AddDeclRef(SubstPack->getParameterPack(), Record);
5391 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5392 break;
5393 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005394 }
5395}
5396
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005397void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005398 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005399 Record.push_back(Arg.getKind());
5400 switch (Arg.getKind()) {
5401 case TemplateArgument::Null:
5402 break;
5403 case TemplateArgument::Type:
5404 AddTypeRef(Arg.getAsType(), Record);
5405 break;
5406 case TemplateArgument::Declaration:
5407 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005408 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005409 break;
5410 case TemplateArgument::NullPtr:
5411 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005412 break;
5413 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005414 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005415 AddTypeRef(Arg.getIntegralType(), Record);
5416 break;
5417 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005418 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5419 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005420 case TemplateArgument::TemplateExpansion:
5421 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005422 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005423 Record.push_back(*NumExpansions + 1);
5424 else
5425 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005426 break;
5427 case TemplateArgument::Expression:
5428 AddStmt(Arg.getAsExpr());
5429 break;
5430 case TemplateArgument::Pack:
5431 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005432 for (const auto &P : Arg.pack_elements())
5433 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005434 break;
5435 }
5436}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005437
5438void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005439ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005440 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005441 assert(TemplateParams && "No TemplateParams!");
5442 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5443 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5444 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5445 Record.push_back(TemplateParams->size());
5446 for (TemplateParameterList::const_iterator
5447 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5448 P != PEnd; ++P)
5449 AddDeclRef(*P, Record);
5450}
5451
5452/// \brief Emit a template argument list.
5453void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005454ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005455 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005456 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005457 Record.push_back(TemplateArgs->size());
5458 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005459 AddTemplateArgument(TemplateArgs->get(i), Record);
5460}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005461
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005462void
5463ASTWriter::AddASTTemplateArgumentListInfo
5464(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5465 assert(ASTTemplArgList && "No ASTTemplArgList!");
5466 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5467 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5468 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5469 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5470 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5471 AddTemplateArgumentLoc(TemplArgs[i], Record);
5472}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005473
5474void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005475ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005476 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005477 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005478 I = Set.begin(), E = Set.end(); I != E; ++I) {
5479 AddDeclRef(I.getDecl(), Record);
5480 Record.push_back(I.getAccess());
5481 }
5482}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005483
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005484void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005485 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005486 Record.push_back(Base.isVirtual());
5487 Record.push_back(Base.isBaseOfClass());
5488 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005489 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005490 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005491 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005492 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5493 : SourceLocation(),
5494 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005495}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005496
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005497void ASTWriter::FlushCXXBaseSpecifiers() {
5498 RecordData Record;
5499 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5500 Record.clear();
5501
5502 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005503 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005504 if (Index == CXXBaseSpecifiersOffsets.size())
5505 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5506 else {
5507 if (Index > CXXBaseSpecifiersOffsets.size())
5508 CXXBaseSpecifiersOffsets.resize(Index + 1);
5509 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5510 }
5511
5512 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5513 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5514 Record.push_back(BEnd - B);
5515 for (; B != BEnd; ++B)
5516 AddCXXBaseSpecifier(*B, Record);
5517 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005518
5519 // Flush any expressions that were written as part of the base specifiers.
5520 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005521 }
5522
5523 CXXBaseSpecifiersToWrite.clear();
5524}
5525
Alexis Hunt1d792652011-01-08 20:30:50 +00005526void ASTWriter::AddCXXCtorInitializers(
5527 const CXXCtorInitializer * const *CtorInitializers,
5528 unsigned NumCtorInitializers,
5529 RecordDataImpl &Record) {
5530 Record.push_back(NumCtorInitializers);
5531 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5532 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005533
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005534 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005535 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005536 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005537 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005538 } else if (Init->isDelegatingInitializer()) {
5539 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005540 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005541 } else if (Init->isMemberInitializer()){
5542 Record.push_back(CTOR_INITIALIZER_MEMBER);
5543 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005544 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005545 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5546 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005547 }
Francois Pichetd583da02010-12-04 09:14:42 +00005548
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005549 AddSourceLocation(Init->getMemberLocation(), Record);
5550 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005551 AddSourceLocation(Init->getLParenLoc(), Record);
5552 AddSourceLocation(Init->getRParenLoc(), Record);
5553 Record.push_back(Init->isWritten());
5554 if (Init->isWritten()) {
5555 Record.push_back(Init->getSourceOrder());
5556 } else {
5557 Record.push_back(Init->getNumArrayIndices());
5558 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5559 AddDeclRef(Init->getArrayIndex(i), Record);
5560 }
5561 }
5562}
5563
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005564void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005565 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005566 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005567 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005568 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005569 Record.push_back(Data.Aggregate);
5570 Record.push_back(Data.PlainOldData);
5571 Record.push_back(Data.Empty);
5572 Record.push_back(Data.Polymorphic);
5573 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005574 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005575 Record.push_back(Data.HasNoNonEmptyBases);
5576 Record.push_back(Data.HasPrivateFields);
5577 Record.push_back(Data.HasProtectedFields);
5578 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005579 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005580 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005581 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005582 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005583 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005584 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5585 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5586 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5587 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5588 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5589 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005590 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005591 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005592 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005593 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005594 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005595 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005596 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005597 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005598 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005599 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005600 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5601 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5602 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5603 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005604 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005605
5606 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005607 if (Data.NumBases > 0)
5608 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5609 Record);
5610
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005611 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5612 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005613 if (Data.NumVBases > 0)
5614 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5615 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005616
Richard Smitha4ba74c2013-08-30 04:46:40 +00005617 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5618 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005619 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005620 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005621
5622 // Add lambda-specific data.
5623 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005624 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005625 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005626 Record.push_back(Lambda.IsGenericLambda);
5627 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005628 Record.push_back(Lambda.NumCaptures);
5629 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005630 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005631 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005632 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005633 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005634 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005635 AddSourceLocation(Capture.getLocation(), Record);
5636 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005637 Record.push_back(Capture.getCaptureKind());
5638 switch (Capture.getCaptureKind()) {
5639 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005640 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005641 break;
5642 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005643 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005644 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005645 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005646 AddDeclRef(Var, Record);
5647 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5648 : SourceLocation(),
5649 Record);
5650 break;
5651 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005652 }
5653 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005654}
5655
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005656void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005657 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005658 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005659 assert(FirstDeclID == NextDeclID &&
5660 FirstTypeID == NextTypeID &&
5661 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005662 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005663 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005664 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005665 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005666
Sebastian Redl07a89a82010-07-30 00:29:29 +00005667 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005668
Richard Smith7f330cd2015-03-18 01:42:29 +00005669 // Note, this will get called multiple times, once one the reader starts up
5670 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005671 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5672 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5673 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005674 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005675 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005676 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005677 NextDeclID = FirstDeclID;
5678 NextTypeID = FirstTypeID;
5679 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005680 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005681 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005682 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005683}
5684
Sebastian Redl539c5062010-08-18 23:57:32 +00005685void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005686 // Always keep the highest ID. See \p TypeRead() for more information.
5687 IdentID &StoredID = IdentifierIDs[II];
5688 if (ID > StoredID)
5689 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005690}
5691
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005692void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005693 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005694 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005695 if (ID > StoredID)
5696 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005697}
5698
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005699void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005700 // Always take the highest-numbered type index. This copes with an interesting
5701 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005702 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005703 // keep the higher-numbered entry so that we can properly write it out to
5704 // the AST file.
5705 TypeIdx &StoredIdx = TypeIdxs[T];
5706 if (Idx.getIndex() >= StoredIdx.getIndex())
5707 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005708}
5709
Sebastian Redl539c5062010-08-18 23:57:32 +00005710void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005711 // Always keep the highest ID. See \p TypeRead() for more information.
5712 SelectorID &StoredID = SelectorIDs[S];
5713 if (ID > StoredID)
5714 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005715}
Douglas Gregor91096292010-10-02 19:29:26 +00005716
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005717void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005718 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005719 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005720 MacroDefinitions[MD] = ID;
5721}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005722
Douglas Gregore37a85a2011-12-02 17:30:13 +00005723void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5724 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5725 SubmoduleIDs[Mod] = ID;
5726}
5727
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005728void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005729 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005730 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005731 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5732 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005733 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005734 // A forward reference was mutated into a definition. Rewrite it.
5735 // FIXME: This happens during template instantiation, should we
5736 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005737 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5738 "completed a tag from another module but not by instantiation?");
5739 DeclUpdates[RD].push_back(
5740 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005741 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005742 }
5743}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005744
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005745void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5746 // TU and namespaces are handled elsewhere.
5747 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5748 return;
5749
Douglas Gregorb3722e22011-09-09 23:01:35 +00005750 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005751 return; // Not a source decl added to a DeclContext from PCH.
5752
Douglas Gregor9f782892013-01-21 15:25:38 +00005753 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005754 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005755 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005756 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005757}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005758
5759void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5760 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005761 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005762 return; // Not a source member added to a class from PCH.
5763 if (!isa<CXXMethodDecl>(D))
5764 return; // We are interested in lazily declared implicit methods.
5765
5766 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005767 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005768 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005769 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005770}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005771
5772void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5773 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005774 // The specializations set is kept in the canonical template.
5775 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005776 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005777 return; // Not a source specialization added to a template from PCH.
5778
Richard Smithe9a8bc32014-09-30 00:45:29 +00005779 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005780 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5781 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005782}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005783
Larisse Voufo39a1e502013-08-06 01:03:05 +00005784void ASTWriter::AddedCXXTemplateSpecialization(
5785 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5786 // The specializations set is kept in the canonical template.
Larisse Voufo39a1e502013-08-06 01:03:05 +00005787 TD = TD->getCanonicalDecl();
5788 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5789 return; // Not a source specialization added to a template from PCH.
5790
Richard Smithe9a8bc32014-09-30 00:45:29 +00005791 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005792 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5793 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005794}
5795
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005796void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5797 const FunctionDecl *D) {
5798 // The specializations set is kept in the canonical template.
5799 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005800 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005801 return; // Not a source specialization added to a template from PCH.
5802
Richard Smithe9a8bc32014-09-30 00:45:29 +00005803 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005804 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5805 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005806}
5807
Richard Smith564417a2014-03-20 21:47:22 +00005808void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005809 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5810 if (!Chain) return;
5811 Chain->forEachFormerlyCanonicalImportedDecl(FD, [&](const Decl *D) {
5812 // If we don't already know the exception specification for this redecl
5813 // chain, add an update record for it.
5814 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5815 ->getType()
5816 ->castAs<FunctionProtoType>()
5817 ->getExceptionSpecType()))
5818 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5819 });
Richard Smith564417a2014-03-20 21:47:22 +00005820}
5821
Richard Smith1fa5d642013-05-11 05:45:24 +00005822void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5823 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005824 if (!Chain) return;
5825 Chain->forEachFormerlyCanonicalImportedDecl(FD, [&](const Decl *D) {
5826 DeclUpdates[D].push_back(
5827 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5828 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005829}
5830
Richard Smithf8134002015-03-10 01:41:22 +00005831void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5832 const FunctionDecl *Delete) {
5833 assert(!WritingAST && "Already writing the AST!");
5834 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005835 if (!Chain) return;
5836 Chain->forEachFormerlyCanonicalImportedDecl(DD, [&](const Decl *D) {
5837 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5838 });
Richard Smithf8134002015-03-10 01:41:22 +00005839}
5840
Sebastian Redlab238a72011-04-24 16:28:06 +00005841void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005842 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005843 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005844 return; // Declaration not imported from PCH.
5845
Richard Smith4d235792014-08-07 18:53:08 +00005846 // Implicit function decl from a PCH was defined.
5847 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005848}
5849
Richard Smithd28ac5b2014-03-22 23:33:22 +00005850void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5851 assert(!WritingAST && "Already writing the AST!");
5852 if (!D->isFromASTFile())
5853 return;
5854
Richard Smith9e2341d2015-03-23 03:25:59 +00005855 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005856}
5857
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005858void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005859 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005860 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005861 return;
5862
5863 // Since the actual instantiation is delayed, this really means that we need
5864 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005865 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005866 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5867 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005868}
5869
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005870void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5871 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005872 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005873 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005874 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005875
5876 assert(IFD->getDefinition() && "Category on a class without a definition?");
5877 ObjCClassesWithCategories.insert(
5878 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005879}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005880
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005881
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005882void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5883 const ObjCPropertyDecl *OrigProp,
5884 const ObjCCategoryDecl *ClassExt) {
5885 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5886 if (!D)
5887 return;
5888
5889 assert(!WritingAST && "Already writing the AST!");
5890 if (!D->isFromASTFile())
5891 return; // Declaration not imported from PCH.
5892
5893 RewriteDecl(D);
5894}
Eli Friedman276dd182013-09-05 00:02:25 +00005895
5896void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5897 assert(!WritingAST && "Already writing the AST!");
5898 if (!D->isFromASTFile())
5899 return;
5900
Aaron Ballman4f45b712014-03-21 15:22:56 +00005901 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005902}
Alexey Bataev97720002014-11-11 04:05:39 +00005903
5904void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5905 assert(!WritingAST && "Already writing the AST!");
5906 if (!D->isFromASTFile())
5907 return;
5908
5909 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5910}