blob: 4b1dbccdf67a4c3e77e92b5db0fb11ec4cca406b [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"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000044#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000045#include "llvm/ADT/APFloat.h"
46#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000047#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000048#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000050#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000051#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000052#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000053#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000054#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000055#include "llvm/Support/Process.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000056#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000057#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000058#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000059#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000061using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000062
Sebastian Redl3df5a082010-07-30 17:03:48 +000063template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000064static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000065 if (v.empty()) return StringRef();
66 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000067 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000068}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000069
70template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +000071static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000072 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000073 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000074}
75
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076//===----------------------------------------------------------------------===//
77// Type serialization
78//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000079
Douglas Gregoref84c4b2009-04-09 22:27:44 +000080namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000081 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000082 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000083 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084
85 public:
86 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000087 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +000088 /// \brief Abbreviation to use for the record, if any.
89 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000091 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000092 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093
94 void VisitArrayType(const ArrayType *T);
95 void VisitFunctionType(const FunctionType *T);
96 void VisitTagType(const TagType *T);
97
98#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
99#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100#include "clang/AST/TypeNodes.def"
101 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000102}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000105 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106}
107
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000110 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111}
112
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000115 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116}
117
Reid Kleckner8a365022013-06-24 17:51:48 +0000118void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
119 Writer.AddTypeRef(T->getOriginalType(), Record);
120 Code = TYPE_DECAYED;
121}
122
Reid Kleckner0503a872013-12-05 01:23:43 +0000123void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
124 Writer.AddTypeRef(T->getOriginalType(), Record);
125 Writer.AddTypeRef(T->getAdjustedType(), Record);
126 Code = TYPE_ADJUSTED;
127}
128
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000130 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000131 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000135 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
136 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000137 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000138}
139
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000141 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000142 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143}
144
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000146 Writer.AddTypeRef(T->getPointeeType(), Record);
147 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000148 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000149}
150
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000151void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152 Writer.AddTypeRef(T->getElementType(), Record);
153 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000154 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155}
156
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158 VisitArrayType(T);
159 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000160 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161}
162
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000165 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166}
167
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000170 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
171 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000172 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000173 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174}
175
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000176void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177 Writer.AddTypeRef(T->getElementType(), Record);
178 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000179 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000180 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181}
182
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000185 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000186}
187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000188void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000189 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000190 FunctionType::ExtInfo C = T->getExtInfo();
191 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000192 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000193 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000194 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000195 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000196 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000197
198 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
199 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000200}
201
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000203 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000204 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205}
206
Richard Smith564417a2014-03-20 21:47:22 +0000207static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
208 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000209 Record.push_back(T->getExceptionSpecType());
210 if (T->getExceptionSpecType() == EST_Dynamic) {
211 Record.push_back(T->getNumExceptions());
212 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
213 Writer.AddTypeRef(T->getExceptionType(I), Record);
214 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
215 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000216 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
217 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
218 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000219 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
220 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000221 }
Richard Smith564417a2014-03-20 21:47:22 +0000222}
223
224void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
225 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000226
Richard Smith564417a2014-03-20 21:47:22 +0000227 Record.push_back(T->isVariadic());
228 Record.push_back(T->hasTrailingReturn());
229 Record.push_back(T->getTypeQuals());
230 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
231 addExceptionSpec(Writer, T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000232
233 Record.push_back(T->getNumParams());
234 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
235 Writer.AddTypeRef(T->getParamType(I), Record);
236
237 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
238 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
239 AbbrevToUse = 0;
240
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000245 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000246 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000247}
John McCallb96ec562009-12-04 22:46:56 +0000248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000251 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
252 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000253 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000254}
255
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000256void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000257 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000258 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000259}
260
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000261void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000263 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000264}
265
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000266void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000267 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000268 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000269 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000270}
271
Alexis Hunte852b102011-05-24 22:41:36 +0000272void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
273 Writer.AddTypeRef(T->getBaseType(), Record);
274 Writer.AddTypeRef(T->getUnderlyingType(), Record);
275 Record.push_back(T->getUTTKind());
276 Code = TYPE_UNARY_TRANSFORM;
277}
278
Richard Smith30482bc2011-02-20 03:19:35 +0000279void ASTTypeWriter::VisitAutoType(const AutoType *T) {
280 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000281 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000282 if (T->getDeducedType().isNull())
283 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000284 Code = TYPE_AUTO;
285}
286
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000287void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000288 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000289 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000290 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000291 "Cannot serialize in the middle of a type definition");
292}
293
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000294void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000295 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000296 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000297}
298
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000299void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000300 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000301 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000302}
303
John McCall81904512011-01-06 01:58:22 +0000304void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
305 Writer.AddTypeRef(T->getModifiedType(), Record);
306 Writer.AddTypeRef(T->getEquivalentType(), Record);
307 Record.push_back(T->getAttrKind());
308 Code = TYPE_ATTRIBUTED;
309}
310
Mike Stump11289f42009-09-09 15:08:12 +0000311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000313 const SubstTemplateTypeParmType *T) {
314 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
315 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000316 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000317}
318
319void
Douglas Gregorada4b792011-01-14 02:55:32 +0000320ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
321 const SubstTemplateTypeParmPackType *T) {
322 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
323 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
324 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
325}
326
327void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000328ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000329 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000330 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000331 Writer.AddTemplateName(T->getTemplateName(), Record);
332 Record.push_back(T->getNumArgs());
333 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
334 ArgI != ArgE; ++ArgI)
335 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000336 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
337 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000338 : T->getCanonicalTypeInternal(),
339 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000340 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000341}
342
343void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000344ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000345 VisitArrayType(T);
346 Writer.AddStmt(T->getSizeExpr());
347 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000349}
350
351void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000352ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000353 const DependentSizedExtVectorType *T) {
354 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000355 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000356}
357
358void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000359ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000360 Record.push_back(T->getDepth());
361 Record.push_back(T->getIndex());
362 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000363 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000364 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000365}
366
367void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000368ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000369 Record.push_back(T->getKeyword());
370 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
371 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000372 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
373 : T->getCanonicalTypeInternal(),
374 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000375 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000376}
377
378void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000379ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000380 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000381 Record.push_back(T->getKeyword());
382 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
383 Writer.AddIdentifierRef(T->getIdentifier(), Record);
384 Record.push_back(T->getNumArgs());
385 for (DependentTemplateSpecializationType::iterator
386 I = T->begin(), E = T->end(); I != E; ++I)
387 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000388 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000389}
390
Douglas Gregord2fa7662010-12-20 02:24:11 +0000391void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
392 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000393 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000394 Record.push_back(*NumExpansions + 1);
395 else
396 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000397 Code = TYPE_PACK_EXPANSION;
398}
399
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000400void ASTTypeWriter::VisitParenType(const ParenType *T) {
401 Writer.AddTypeRef(T->getInnerType(), Record);
402 Code = TYPE_PAREN;
403}
404
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000405void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000406 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000407 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
408 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000409 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000410}
411
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000412void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000413 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000414 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000415 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000416}
417
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000418void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000419 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000420 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000421}
422
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000423void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000424 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregore83b9562015-07-07 03:57:53 +0000425 Record.push_back(T->getTypeArgsAsWritten().size());
426 for (auto TypeArg : T->getTypeArgsAsWritten())
Douglas Gregore9d95f12015-07-07 03:57:35 +0000427 Writer.AddTypeRef(TypeArg, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000428 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000429 for (const auto *I : T->quals())
430 Writer.AddDeclRef(I, Record);
Douglas Gregorab209d82015-07-07 03:58:42 +0000431 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000432 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000433}
434
Steve Narofffb4330f2009-06-17 22:40:22 +0000435void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000436ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000437 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000438 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000439}
440
Eli Friedman0dfb8892011-10-06 23:00:33 +0000441void
442ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
443 Writer.AddTypeRef(T->getValueType(), Record);
444 Code = TYPE_ATOMIC;
445}
446
John McCall8f115c62009-10-16 21:56:05 +0000447namespace {
448
449class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000450 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000451 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000452
453public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000454 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000455 : Writer(Writer), Record(Record) { }
456
John McCall17001972009-10-18 01:05:36 +0000457#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000458#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000459 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000460#include "clang/AST/TypeLocNodes.def"
461
John McCall17001972009-10-18 01:05:36 +0000462 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
463 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000464};
465
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000466}
John McCall8f115c62009-10-16 21:56:05 +0000467
John McCall17001972009-10-18 01:05:36 +0000468void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
469 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000470}
John McCall17001972009-10-18 01:05:36 +0000471void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000472 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
473 if (TL.needsExtraLocalData()) {
474 Record.push_back(TL.getWrittenTypeSpec());
475 Record.push_back(TL.getWrittenSignSpec());
476 Record.push_back(TL.getWrittenWidthSpec());
477 Record.push_back(TL.hasModeAttr());
478 }
John McCall8f115c62009-10-16 21:56:05 +0000479}
John McCall17001972009-10-18 01:05:36 +0000480void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000482}
John McCall17001972009-10-18 01:05:36 +0000483void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000485}
Reid Kleckner8a365022013-06-24 17:51:48 +0000486void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
487 // nothing to do
488}
Reid Kleckner0503a872013-12-05 01:23:43 +0000489void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
490 // nothing to do
491}
John McCall17001972009-10-18 01:05:36 +0000492void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000494}
John McCall17001972009-10-18 01:05:36 +0000495void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000497}
John McCall17001972009-10-18 01:05:36 +0000498void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000500}
John McCall17001972009-10-18 01:05:36 +0000501void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000503 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000504}
John McCall17001972009-10-18 01:05:36 +0000505void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
507 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
508 Record.push_back(TL.getSizeExpr() ? 1 : 0);
509 if (TL.getSizeExpr())
510 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000511}
John McCall17001972009-10-18 01:05:36 +0000512void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
513 VisitArrayTypeLoc(TL);
514}
515void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
516 VisitArrayTypeLoc(TL);
517}
518void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
519 VisitArrayTypeLoc(TL);
520}
521void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
522 DependentSizedArrayTypeLoc TL) {
523 VisitArrayTypeLoc(TL);
524}
525void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
526 DependentSizedExtVectorTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
529void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
532void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
535void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000536 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000537 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
538 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000539 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000540 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
541 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000542}
543void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
544 VisitFunctionTypeLoc(TL);
545}
546void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
547 VisitFunctionTypeLoc(TL);
548}
John McCallb96ec562009-12-04 22:46:56 +0000549void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
551}
John McCall17001972009-10-18 01:05:36 +0000552void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getNameLoc(), Record);
554}
555void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc 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);
John McCall17001972009-10-18 01:05:36 +0000559}
560void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000561 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
562 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
563 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
564 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000565}
566void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
567 Writer.AddSourceLocation(TL.getNameLoc(), Record);
568}
Alexis Hunte852b102011-05-24 22:41:36 +0000569void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getKWLoc(), Record);
571 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
572 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
573 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
574}
Richard Smith30482bc2011-02-20 03:19:35 +0000575void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577}
John McCall17001972009-10-18 01:05:36 +0000578void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
579 Writer.AddSourceLocation(TL.getNameLoc(), Record);
580}
581void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583}
John McCall81904512011-01-06 01:58:22 +0000584void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
586 if (TL.hasAttrOperand()) {
587 SourceRange range = TL.getAttrOperandParensRange();
588 Writer.AddSourceLocation(range.getBegin(), Record);
589 Writer.AddSourceLocation(range.getEnd(), Record);
590 }
591 if (TL.hasAttrExprOperand()) {
592 Expr *operand = TL.getAttrExprOperand();
593 Record.push_back(operand ? 1 : 0);
594 if (operand) Writer.AddStmt(operand);
595 } else if (TL.hasAttrEnumOperand()) {
596 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
597 }
598}
John McCall17001972009-10-18 01:05:36 +0000599void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
600 Writer.AddSourceLocation(TL.getNameLoc(), Record);
601}
John McCallcebee162009-10-18 09:09:24 +0000602void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
603 SubstTemplateTypeParmTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
605}
Douglas Gregorada4b792011-01-14 02:55:32 +0000606void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
607 SubstTemplateTypeParmPackTypeLoc TL) {
608 Writer.AddSourceLocation(TL.getNameLoc(), Record);
609}
John McCall17001972009-10-18 01:05:36 +0000610void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
611 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000612 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000613 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
614 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
615 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
616 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000617 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
618 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000619}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000620void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
621 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
622 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
623}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000624void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000625 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000626 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000627}
John McCalle78aac42010-03-10 03:28:59 +0000628void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
629 Writer.AddSourceLocation(TL.getNameLoc(), Record);
630}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000631void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000632 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000633 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000634 Writer.AddSourceLocation(TL.getNameLoc(), Record);
635}
John McCallc392f372010-06-11 00:33:02 +0000636void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
637 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000638 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000639 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000640 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000641 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000642 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
643 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
644 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000645 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
646 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000647}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000648void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
649 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
650}
John McCall17001972009-10-18 01:05:36 +0000651void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
652 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000653}
654void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
655 Record.push_back(TL.hasBaseTypeAsWritten());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000656 Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record);
657 Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record);
658 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
659 Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record);
660 Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record);
661 Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000662 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
663 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000664}
John McCallfc93cf92009-10-22 22:37:11 +0000665void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
666 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000667}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000668void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
669 Writer.AddSourceLocation(TL.getKWLoc(), Record);
670 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
671 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
672}
John McCall8f115c62009-10-16 21:56:05 +0000673
Richard Smith01b2cb42014-07-26 06:37:51 +0000674void ASTWriter::WriteTypeAbbrevs() {
675 using namespace llvm;
676
677 BitCodeAbbrev *Abv;
678
679 // Abbreviation for TYPE_EXT_QUAL
680 Abv = new BitCodeAbbrev();
681 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
683 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
684 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
685
686 // Abbreviation for TYPE_FUNCTION_PROTO
687 Abv = new BitCodeAbbrev();
688 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
689 // FunctionType
690 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
691 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
692 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
693 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
694 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
695 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
696 // FunctionProtoType
697 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
698 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
699 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
700 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
701 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
702 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
703 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
704 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
705 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
706}
707
Chris Lattner19cea4e2009-04-22 05:57:30 +0000708//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000709// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000710//===----------------------------------------------------------------------===//
711
Chris Lattner28fa4e62009-04-26 22:26:21 +0000712static void EmitBlockID(unsigned ID, const char *Name,
713 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000714 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000715 Record.clear();
716 Record.push_back(ID);
717 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
718
719 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000720 if (!Name || Name[0] == 0)
721 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000722 Record.clear();
723 while (*Name)
724 Record.push_back(*Name++);
725 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
726}
727
728static void EmitRecordID(unsigned ID, const char *Name,
729 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000730 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000731 Record.clear();
732 Record.push_back(ID);
733 while (*Name)
734 Record.push_back(*Name++);
735 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000736}
737
738static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000739 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000740#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000741 RECORD(STMT_STOP);
742 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000743 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000744 RECORD(STMT_NULL);
745 RECORD(STMT_COMPOUND);
746 RECORD(STMT_CASE);
747 RECORD(STMT_DEFAULT);
748 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000749 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000750 RECORD(STMT_IF);
751 RECORD(STMT_SWITCH);
752 RECORD(STMT_WHILE);
753 RECORD(STMT_DO);
754 RECORD(STMT_FOR);
755 RECORD(STMT_GOTO);
756 RECORD(STMT_INDIRECT_GOTO);
757 RECORD(STMT_CONTINUE);
758 RECORD(STMT_BREAK);
759 RECORD(STMT_RETURN);
760 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000761 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000762 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000763 RECORD(EXPR_PREDEFINED);
764 RECORD(EXPR_DECL_REF);
765 RECORD(EXPR_INTEGER_LITERAL);
766 RECORD(EXPR_FLOATING_LITERAL);
767 RECORD(EXPR_IMAGINARY_LITERAL);
768 RECORD(EXPR_STRING_LITERAL);
769 RECORD(EXPR_CHARACTER_LITERAL);
770 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000771 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000772 RECORD(EXPR_UNARY_OPERATOR);
773 RECORD(EXPR_SIZEOF_ALIGN_OF);
774 RECORD(EXPR_ARRAY_SUBSCRIPT);
775 RECORD(EXPR_CALL);
776 RECORD(EXPR_MEMBER);
777 RECORD(EXPR_BINARY_OPERATOR);
778 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
779 RECORD(EXPR_CONDITIONAL_OPERATOR);
780 RECORD(EXPR_IMPLICIT_CAST);
781 RECORD(EXPR_CSTYLE_CAST);
782 RECORD(EXPR_COMPOUND_LITERAL);
783 RECORD(EXPR_EXT_VECTOR_ELEMENT);
784 RECORD(EXPR_INIT_LIST);
785 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000786 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000787 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000788 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000789 RECORD(EXPR_VA_ARG);
790 RECORD(EXPR_ADDR_LABEL);
791 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000792 RECORD(EXPR_CHOOSE);
793 RECORD(EXPR_GNU_NULL);
794 RECORD(EXPR_SHUFFLE_VECTOR);
795 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000796 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000797 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000798 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000799 RECORD(EXPR_OBJC_ARRAY_LITERAL);
800 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000801 RECORD(EXPR_OBJC_ENCODE);
802 RECORD(EXPR_OBJC_SELECTOR_EXPR);
803 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
804 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
805 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
806 RECORD(EXPR_OBJC_KVC_REF_EXPR);
807 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000808 RECORD(STMT_OBJC_FOR_COLLECTION);
809 RECORD(STMT_OBJC_CATCH);
810 RECORD(STMT_OBJC_FINALLY);
811 RECORD(STMT_OBJC_AT_TRY);
812 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
813 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000814 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000815 RECORD(STMT_CXX_CATCH);
816 RECORD(STMT_CXX_TRY);
817 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000818 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000819 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000820 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000821 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000822 RECORD(EXPR_CXX_STATIC_CAST);
823 RECORD(EXPR_CXX_DYNAMIC_CAST);
824 RECORD(EXPR_CXX_REINTERPRET_CAST);
825 RECORD(EXPR_CXX_CONST_CAST);
826 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000827 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000828 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000829 RECORD(EXPR_CXX_BOOL_LITERAL);
830 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000831 RECORD(EXPR_CXX_TYPEID_EXPR);
832 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000833 RECORD(EXPR_CXX_THIS);
834 RECORD(EXPR_CXX_THROW);
835 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000836 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000837 RECORD(EXPR_CXX_BIND_TEMPORARY);
838 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
839 RECORD(EXPR_CXX_NEW);
840 RECORD(EXPR_CXX_DELETE);
841 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
842 RECORD(EXPR_EXPR_WITH_CLEANUPS);
843 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
844 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
845 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
846 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
847 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000848 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000849 RECORD(EXPR_CXX_NOEXCEPT);
850 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000851 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
852 RECORD(EXPR_TYPE_TRAIT);
853 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000854 RECORD(EXPR_PACK_EXPANSION);
855 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000856 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000857 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000858 RECORD(EXPR_FUNCTION_PARM_PACK);
859 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000860 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000861 RECORD(EXPR_CXX_UUIDOF_EXPR);
862 RECORD(EXPR_CXX_UUIDOF_TYPE);
863 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000864#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000865}
Mike Stump11289f42009-09-09 15:08:12 +0000866
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000867void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000868 RecordData Record;
869 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000870
Sebastian Redl539c5062010-08-18 23:57:32 +0000871#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
872#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000873
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000874 // Control Block.
875 BLOCK(CONTROL_BLOCK);
876 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000877 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000878 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +0000879 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000880 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000881 RECORD(IMPORTS);
882 RECORD(LANGUAGE_OPTIONS);
883 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000884 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000885 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000886 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000887 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000888 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000889 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000890 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000891 RECORD(PREPROCESSOR_OPTIONS);
892
Douglas Gregor108cb222012-10-19 00:45:00 +0000893 BLOCK(INPUT_FILES_BLOCK);
894 RECORD(INPUT_FILE);
895
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000896 // AST Top-Level Block.
897 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000898 RECORD(TYPE_OFFSET);
899 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000900 RECORD(IDENTIFIER_OFFSET);
901 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000902 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000903 RECORD(SPECIAL_TYPES);
904 RECORD(STATISTICS);
905 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000906 RECORD(SELECTOR_OFFSETS);
907 RECORD(METHOD_POOL);
908 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000909 RECORD(SOURCE_LOCATION_OFFSETS);
910 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000911 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +0000912 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000913 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +0000914 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000915 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000916 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000917 RECORD(SEMA_DECL_REFS);
918 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
919 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
920 RECORD(DECL_REPLACEMENTS);
921 RECORD(UPDATE_VISIBLE);
922 RECORD(DECL_UPDATE_OFFSETS);
923 RECORD(DECL_UPDATES);
924 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
925 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000926 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000927 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000928 RECORD(FP_PRAGMA_OPTIONS);
929 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000930 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000931 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000932 RECORD(MODULE_OFFSET_MAP);
933 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000934 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000935 RECORD(FILE_SORTED_DECLS);
936 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000937 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000938 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +0000939 RECORD(INTERESTING_IDENTIFIERS);
940 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000941 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000942 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +0000943 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
944 RECORD(CXX_CTOR_INITIALIZERS_OFFSETS);
945 RECORD(DELETE_EXPRS_TO_ANALYZE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000946
Chris Lattner28fa4e62009-04-26 22:26:21 +0000947 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000948 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000949 RECORD(SM_SLOC_FILE_ENTRY);
950 RECORD(SM_SLOC_BUFFER_ENTRY);
951 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000952 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000953
Chris Lattner28fa4e62009-04-26 22:26:21 +0000954 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000955 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000956 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000957 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +0000958 RECORD(PP_MACRO_OBJECT_LIKE);
959 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000960 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000961
Richard Smithfa715652015-08-31 22:43:10 +0000962 // Submodule Block.
963 BLOCK(SUBMODULE_BLOCK);
964 RECORD(SUBMODULE_METADATA);
965 RECORD(SUBMODULE_DEFINITION);
966 RECORD(SUBMODULE_UMBRELLA_HEADER);
967 RECORD(SUBMODULE_HEADER);
968 RECORD(SUBMODULE_TOPHEADER);
969 RECORD(SUBMODULE_UMBRELLA_DIR);
970 RECORD(SUBMODULE_IMPORTS);
971 RECORD(SUBMODULE_EXPORTS);
972 RECORD(SUBMODULE_REQUIRES);
973 RECORD(SUBMODULE_EXCLUDED_HEADER);
974 RECORD(SUBMODULE_LINK_LIBRARY);
975 RECORD(SUBMODULE_CONFIG_MACRO);
976 RECORD(SUBMODULE_CONFLICT);
977 RECORD(SUBMODULE_PRIVATE_HEADER);
978 RECORD(SUBMODULE_TEXTUAL_HEADER);
979 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
980
981 // Comments Block.
982 BLOCK(COMMENTS_BLOCK);
983 RECORD(COMMENTS_RAW_COMMENT);
984
Douglas Gregor12bfa382009-10-17 00:13:19 +0000985 // Decls and Types block.
986 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000987 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000988 RECORD(TYPE_COMPLEX);
989 RECORD(TYPE_POINTER);
990 RECORD(TYPE_BLOCK_POINTER);
991 RECORD(TYPE_LVALUE_REFERENCE);
992 RECORD(TYPE_RVALUE_REFERENCE);
993 RECORD(TYPE_MEMBER_POINTER);
994 RECORD(TYPE_CONSTANT_ARRAY);
995 RECORD(TYPE_INCOMPLETE_ARRAY);
996 RECORD(TYPE_VARIABLE_ARRAY);
997 RECORD(TYPE_VECTOR);
998 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000999 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001000 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001001 RECORD(TYPE_TYPEDEF);
1002 RECORD(TYPE_TYPEOF_EXPR);
1003 RECORD(TYPE_TYPEOF);
1004 RECORD(TYPE_RECORD);
1005 RECORD(TYPE_ENUM);
1006 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001007 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001008 RECORD(TYPE_DECLTYPE);
1009 RECORD(TYPE_ELABORATED);
1010 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1011 RECORD(TYPE_UNRESOLVED_USING);
1012 RECORD(TYPE_INJECTED_CLASS_NAME);
1013 RECORD(TYPE_OBJC_OBJECT);
1014 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1015 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1016 RECORD(TYPE_DEPENDENT_NAME);
1017 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1018 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1019 RECORD(TYPE_PAREN);
1020 RECORD(TYPE_PACK_EXPANSION);
1021 RECORD(TYPE_ATTRIBUTED);
1022 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001023 RECORD(TYPE_AUTO);
1024 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001025 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001026 RECORD(TYPE_DECAYED);
1027 RECORD(TYPE_ADJUSTED);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001028 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001029 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001030 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001031 RECORD(DECL_ENUM);
1032 RECORD(DECL_RECORD);
1033 RECORD(DECL_ENUM_CONSTANT);
1034 RECORD(DECL_FUNCTION);
1035 RECORD(DECL_OBJC_METHOD);
1036 RECORD(DECL_OBJC_INTERFACE);
1037 RECORD(DECL_OBJC_PROTOCOL);
1038 RECORD(DECL_OBJC_IVAR);
1039 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001040 RECORD(DECL_OBJC_CATEGORY);
1041 RECORD(DECL_OBJC_CATEGORY_IMPL);
1042 RECORD(DECL_OBJC_IMPLEMENTATION);
1043 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1044 RECORD(DECL_OBJC_PROPERTY);
1045 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001046 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001047 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001048 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001049 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001050 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001051 RECORD(DECL_FILE_SCOPE_ASM);
1052 RECORD(DECL_BLOCK);
1053 RECORD(DECL_CONTEXT_LEXICAL);
1054 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001055 RECORD(DECL_NAMESPACE);
1056 RECORD(DECL_NAMESPACE_ALIAS);
1057 RECORD(DECL_USING);
1058 RECORD(DECL_USING_SHADOW);
1059 RECORD(DECL_USING_DIRECTIVE);
1060 RECORD(DECL_UNRESOLVED_USING_VALUE);
1061 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1062 RECORD(DECL_LINKAGE_SPEC);
1063 RECORD(DECL_CXX_RECORD);
1064 RECORD(DECL_CXX_METHOD);
1065 RECORD(DECL_CXX_CONSTRUCTOR);
1066 RECORD(DECL_CXX_DESTRUCTOR);
1067 RECORD(DECL_CXX_CONVERSION);
1068 RECORD(DECL_ACCESS_SPEC);
1069 RECORD(DECL_FRIEND);
1070 RECORD(DECL_FRIEND_TEMPLATE);
1071 RECORD(DECL_CLASS_TEMPLATE);
1072 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1073 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001074 RECORD(DECL_VAR_TEMPLATE);
1075 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1076 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001077 RECORD(DECL_FUNCTION_TEMPLATE);
1078 RECORD(DECL_TEMPLATE_TYPE_PARM);
1079 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1080 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1081 RECORD(DECL_STATIC_ASSERT);
1082 RECORD(DECL_CXX_BASE_SPECIFIERS);
1083 RECORD(DECL_INDIRECTFIELD);
1084 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1085
Douglas Gregor03412ba2011-06-03 02:27:19 +00001086 // Statements and Exprs can occur in the Decls and Types block.
1087 AddStmtsExprs(Stream, Record);
1088
Douglas Gregor92a96f52011-02-08 21:58:10 +00001089 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001090 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001091 RECORD(PPD_MACRO_DEFINITION);
1092 RECORD(PPD_INCLUSION_DIRECTIVE);
1093
Chris Lattner28fa4e62009-04-26 22:26:21 +00001094#undef RECORD
1095#undef BLOCK
1096 Stream.ExitBlock();
1097}
1098
Richard Smith54cc3c22014-12-11 20:50:24 +00001099/// \brief Prepares a path for being written to an AST file by converting it
1100/// to an absolute path and removing nested './'s.
1101///
1102/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001103static bool cleanPathForOutput(FileManager &FileMgr,
1104 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001105 bool Changed = FileMgr.makeAbsolutePath(Path);
1106 return Changed | FileMgr.removeDotPaths(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001107}
1108
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001109/// \brief Adjusts the given filename to only write out the portion of the
1110/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001111///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001112/// \param Filename the file name to adjust.
1113///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001114/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1115/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001116///
1117/// \returns either the original filename (if it needs no adjustment) or the
1118/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001119static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001120adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001121 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001122
Richard Smith7ed1bc92014-12-05 22:42:13 +00001123 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001124 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001125
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001126 // Verify that the filename and the system root have the same prefix.
1127 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001128 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1129 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001130 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001131
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001132 // We hit the end of the filename before we hit the end of the system root.
1133 if (!Filename[Pos])
1134 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Richard Smith7ed1bc92014-12-05 22:42:13 +00001136 // If there's not a path separator at the end of the base directory nor
1137 // immediately after it, then this isn't within the base directory.
1138 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1139 if (!llvm::sys::path::is_separator(BaseDir.back()))
1140 return Filename;
1141 } else {
1142 // If the file name has a '/' at the current position, skip over the '/'.
1143 // We distinguish relative paths from absolute paths by the
1144 // absence of '/' at the beginning of relative paths.
1145 //
1146 // FIXME: This is wrong. We distinguish them by asking if the path is
1147 // absolute, which isn't the same thing. And there might be multiple '/'s
1148 // in a row. Use a better mechanism to indicate whether we have emitted an
1149 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001150 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001151 }
Mike Stump11289f42009-09-09 15:08:12 +00001152
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001153 return Filename + Pos;
1154}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001155
Ben Langmuir487ea142014-10-23 18:05:36 +00001156static ASTFileSignature getSignature() {
1157 while (1) {
1158 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1159 return S;
1160 // Rely on GetRandomNumber to eventually return non-zero...
1161 }
1162}
1163
Douglas Gregor112b9072012-10-18 05:31:06 +00001164/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001165void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1166 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001167 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001168 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001169 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1170 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001171
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001172 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001173 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1174 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1175 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1176 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1177 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1178 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1179 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001180 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001181 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1182 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1183 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1184 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001185 Record.push_back(VERSION_MAJOR);
1186 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001187 Record.push_back(CLANG_VERSION_MAJOR);
1188 Record.push_back(CLANG_VERSION_MINOR);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001189 assert((!WritingModule || isysroot.empty()) &&
1190 "writing module as a relocatable PCH?");
Douglas Gregorc567ba22011-07-22 16:35:34 +00001191 Record.push_back(!isysroot.empty());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001192 Record.push_back(IncludeTimestamps);
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001193 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001194 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1195 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001196
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001197 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001198 // For implicit modules we output a signature that we can use to ensure
1199 // duplicate module builds don't collide in the cache as their output order
1200 // is non-deterministic.
1201 // FIXME: Remove this when output is deterministic.
1202 if (Context.getLangOpts().ImplicitModules) {
1203 Record.clear();
1204 Record.push_back(getSignature());
1205 Stream.EmitRecord(SIGNATURE, Record);
1206 }
1207
Richard Smith7ed1bc92014-12-05 22:42:13 +00001208 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1210 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1212 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1213 RecordData Record;
1214 Record.push_back(MODULE_NAME);
1215 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1216 }
1217
Richard Smith7ed1bc92014-12-05 22:42:13 +00001218 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001219 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001220 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001221
1222 // If the home of the module is the current working directory, then we
1223 // want to pick up the cwd of the build process loading the module, not
1224 // our cwd, when we load this module.
1225 if (!PP.getHeaderSearchInfo()
1226 .getHeaderSearchOpts()
1227 .ModuleMapFileHomeIsCwd ||
1228 WritingModule->Directory->getName() != StringRef(".")) {
1229 // Module directory.
1230 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1231 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1233 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1234
1235 RecordData Record;
1236 Record.push_back(MODULE_DIRECTORY);
1237 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1238 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001239
1240 // Write out all other paths relative to the base directory if possible.
1241 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1242 } else if (!isysroot.empty()) {
1243 // Write out paths relative to the sysroot if possible.
1244 BaseDirectory = isysroot;
1245 }
1246
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001247 // Module map file
1248 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001249 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001250
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001251 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1252
1253 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001254 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001255
1256 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001257 if (auto *AdditionalModMaps =
1258 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001259 Record.push_back(AdditionalModMaps->size());
1260 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001261 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001262 } else {
1263 Record.push_back(0);
1264 }
1265
1266 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001267 }
1268
Douglas Gregor112b9072012-10-18 05:31:06 +00001269 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001270 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001271 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001272 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001273
Richard Smith7f330cd2015-03-18 01:42:29 +00001274 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001275 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001276 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001277 continue;
1278
Richard Smith7f330cd2015-03-18 01:42:29 +00001279 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1280 AddSourceLocation(M->ImportLoc, Record);
1281 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001282 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001283 Record.push_back(M->Signature);
1284 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001285 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001286 Stream.EmitRecord(IMPORTS, Record);
1287 }
Mike Stump11289f42009-09-09 15:08:12 +00001288
Douglas Gregor112b9072012-10-18 05:31:06 +00001289 // Language options.
1290 Record.clear();
1291 const LangOptions &LangOpts = Context.getLangOpts();
1292#define LANGOPT(Name, Bits, Default, Description) \
1293 Record.push_back(LangOpts.Name);
1294#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1295 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001296#include "clang/Basic/LangOptions.def"
1297#define SANITIZER(NAME, ID) \
1298 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001299#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001300
Ben Langmuircd98cb72015-06-23 18:20:18 +00001301 Record.push_back(LangOpts.ModuleFeatures.size());
1302 for (StringRef Feature : LangOpts.ModuleFeatures)
1303 AddString(Feature, Record);
1304
Douglas Gregor112b9072012-10-18 05:31:06 +00001305 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1306 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001307
1308 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001309
1310 // Comment options.
1311 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1312 for (CommentOptions::BlockCommandNamesTy::const_iterator
1313 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1314 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1315 I != IEnd; ++I) {
1316 AddString(*I, Record);
1317 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001318 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001319
Douglas Gregor112b9072012-10-18 05:31:06 +00001320 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1321
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001322 // Target options.
1323 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001324 const TargetInfo &Target = Context.getTargetInfo();
1325 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001326 AddString(TargetOpts.Triple, Record);
1327 AddString(TargetOpts.CPU, Record);
1328 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001329 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1330 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1331 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1332 }
1333 Record.push_back(TargetOpts.Features.size());
1334 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1335 AddString(TargetOpts.Features[I], Record);
1336 }
1337 Stream.EmitRecord(TARGET_OPTIONS, Record);
1338
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001339 // Diagnostic options.
1340 Record.clear();
1341 const DiagnosticOptions &DiagOpts
1342 = Context.getDiagnostics().getDiagnosticOptions();
1343#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1344#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1345 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1346#include "clang/Basic/DiagnosticOptions.def"
1347 Record.push_back(DiagOpts.Warnings.size());
1348 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1349 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001350 Record.push_back(DiagOpts.Remarks.size());
1351 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1352 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001353 // Note: we don't serialize the log or serialization file names, because they
1354 // are generally transient files and will almost always be overridden.
1355 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1356
Douglas Gregorc6317db2012-10-24 15:49:58 +00001357 // File system options.
1358 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001359 const FileSystemOptions &FSOpts =
1360 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001361 AddString(FSOpts.WorkingDir, Record);
1362 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1363
Douglas Gregor2d302362012-10-24 16:50:34 +00001364 // Header search options.
1365 Record.clear();
1366 const HeaderSearchOptions &HSOpts
1367 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1368 AddString(HSOpts.Sysroot, Record);
1369
1370 // Include entries.
1371 Record.push_back(HSOpts.UserEntries.size());
1372 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1373 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1374 AddString(Entry.Path, Record);
1375 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001376 Record.push_back(Entry.IsFramework);
1377 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001378 }
1379
1380 // System header prefixes.
1381 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1382 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1383 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1384 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1385 }
1386
1387 AddString(HSOpts.ResourceDir, Record);
1388 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001389 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001390 Record.push_back(HSOpts.DisableModuleHash);
1391 Record.push_back(HSOpts.UseBuiltinIncludes);
1392 Record.push_back(HSOpts.UseStandardSystemIncludes);
1393 Record.push_back(HSOpts.UseStandardCXXIncludes);
1394 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001395 // Write out the specific module cache path that contains the module files.
1396 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001397 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1398
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001399 // Preprocessor options.
1400 Record.clear();
1401 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1402
1403 // Macro definitions.
1404 Record.push_back(PPOpts.Macros.size());
1405 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1406 AddString(PPOpts.Macros[I].first, Record);
1407 Record.push_back(PPOpts.Macros[I].second);
1408 }
1409
1410 // Includes
1411 Record.push_back(PPOpts.Includes.size());
1412 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1413 AddString(PPOpts.Includes[I], Record);
1414
1415 // Macro includes
1416 Record.push_back(PPOpts.MacroIncludes.size());
1417 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1418 AddString(PPOpts.MacroIncludes[I], Record);
1419
Douglas Gregorb6368752012-10-24 23:41:50 +00001420 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001421 // Detailed record is important since it is used for the module cache hash.
1422 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001423 AddString(PPOpts.ImplicitPCHInclude, Record);
1424 AddString(PPOpts.ImplicitPTHInclude, Record);
1425 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1426 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1427
Douglas Gregora3b20262011-05-06 21:43:30 +00001428 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001429 SourceManager &SM = Context.getSourceManager();
1430 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1431 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001432 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1433 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001434 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1435 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1436
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001437 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001438 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001439 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001440 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001441 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001442
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001443 Record.clear();
1444 Record.push_back(SM.getMainFileID().getOpaqueValue());
1445 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1446
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001447 // Original PCH directory
1448 if (!OutputFile.empty() && OutputFile != "-") {
1449 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1450 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1452 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1453
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001454 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001455
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001456 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001457 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1458
1459 RecordData Record;
1460 Record.push_back(ORIGINAL_PCH_DIR);
1461 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1462 }
1463
Douglas Gregor49491f72013-03-15 22:15:07 +00001464 WriteInputFiles(Context.SourceMgr,
1465 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001466 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001467 Stream.ExitBlock();
1468}
1469
Douglas Gregor49491f72013-03-15 22:15:07 +00001470namespace {
1471 /// \brief An input file.
1472 struct InputFileEntry {
1473 const FileEntry *File;
1474 bool IsSystemFile;
1475 bool BufferOverridden;
1476 };
1477}
1478
1479void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1480 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001481 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001482 using namespace llvm;
1483 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1484 RecordData Record;
1485
1486 // Create input-file abbreviation.
1487 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1488 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001489 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001490 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1491 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001492 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001493 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1494 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1495
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001496 // Get all ContentCache objects for files, sorted by whether the file is a
1497 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001498 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001499 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1500 // Get this source location entry.
1501 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001502 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001503
1504 // We only care about file entries that were not overridden.
1505 if (!SLoc->isFile())
1506 continue;
1507 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001508 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001509 continue;
1510
Douglas Gregor49491f72013-03-15 22:15:07 +00001511 InputFileEntry Entry;
1512 Entry.File = Cache->OrigEntry;
1513 Entry.IsSystemFile = Cache->IsSystemFile;
1514 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001515 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001516 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001517 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001518 SortedFiles.push_front(Entry);
1519 }
1520
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001521 unsigned UserFilesNum = 0;
1522 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001523 std::vector<uint64_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001524 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001525 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001526 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001527
Douglas Gregor49491f72013-03-15 22:15:07 +00001528 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001529 if (InputFileID != 0)
1530 continue; // already recorded this file.
1531
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001532 // Record this entry's offset.
1533 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001534
1535 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001536
Douglas Gregor49491f72013-03-15 22:15:07 +00001537 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001538 ++UserFilesNum;
1539
Douglas Gregor72be3902012-10-19 00:38:02 +00001540 Record.clear();
1541 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001542 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001543
1544 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001545 Record.push_back(Entry.File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001546 Record.push_back(getTimestampForOutput(Entry.File));
Douglas Gregor72be3902012-10-19 00:38:02 +00001547
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001548 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001549 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001550
Richard Smith7ed1bc92014-12-05 22:42:13 +00001551 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1552 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001553
Douglas Gregor112b9072012-10-18 05:31:06 +00001554 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001555
1556 // Create input file offsets abbreviation.
1557 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1558 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1559 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001560 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1561 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001562 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1563 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1564
1565 // Write input file offsets.
1566 Record.clear();
1567 Record.push_back(INPUT_FILE_OFFSETS);
1568 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001569 Record.push_back(UserFilesNum);
Reid Kleckner012665e2015-05-21 00:13:09 +00001570 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001571}
1572
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573//===----------------------------------------------------------------------===//
1574// Source Manager Serialization
1575//===----------------------------------------------------------------------===//
1576
1577/// \brief Create an abbreviation for the SLocEntry that refers to a
1578/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001579static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001580 using namespace llvm;
1581 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001582 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001587 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001592 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001593}
1594
1595/// \brief Create an abbreviation for the SLocEntry that refers to a
1596/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001597static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001598 using namespace llvm;
1599 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001600 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001606 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001607}
1608
1609/// \brief Create an abbreviation for the SLocEntry that refers to a
1610/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001611static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001612 using namespace llvm;
1613 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001614 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001616 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001617}
1618
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001619/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1620/// expansion.
1621static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001622 using namespace llvm;
1623 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001624 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001630 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001631}
1632
Douglas Gregor09b69892011-02-10 17:09:37 +00001633namespace {
1634 // Trait used for the on-disk hash table of header search information.
1635 class HeaderFileInfoTrait {
1636 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001637 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001638
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001639 // Keep track of the framework names we've used during serialization.
1640 SmallVector<char, 128> FrameworkStringData;
1641 llvm::StringMap<unsigned> FrameworkNameOffset;
1642
Douglas Gregor09b69892011-02-10 17:09:37 +00001643 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001644 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1645 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001646
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001647 struct key_type {
1648 const FileEntry *FE;
1649 const char *Filename;
1650 };
1651 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001652
1653 typedef HeaderFileInfo data_type;
1654 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001655 typedef unsigned hash_value_type;
1656 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001657
Richard Smithe75ee0f2015-08-17 07:13:32 +00001658 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001659 // The hash is based only on size/time of the file, so that the reader can
1660 // match even when symlinking or excess path elements ("foo/../", "../")
1661 // change the form of the name. However, complete path is still the key.
1662 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001663 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001664 }
1665
1666 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001667 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001668 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001669 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001670 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001671 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001672 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001673 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1674 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1675 DataLen += 4;
1676 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001677 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001678 }
1679
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001680 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001681 using namespace llvm::support;
1682 endian::Writer<little> LE(Out);
1683 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001684 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001685 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001686 KeyLen -= 8;
1687 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001688 }
1689
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001690 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001691 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001692 using namespace llvm::support;
1693 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001694 uint64_t Start = Out.tell(); (void)Start;
1695
Richard Smith386bb072015-08-18 23:42:23 +00001696 unsigned char Flags = (Data.isImport << 4)
1697 | (Data.isPragmaOnce << 3)
1698 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001699 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001700 LE.write<uint8_t>(Flags);
1701 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001702
1703 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001704 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001705 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001706 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001707
1708 unsigned Offset = 0;
1709 if (!Data.Framework.empty()) {
1710 // If this header refers into a framework, save the framework name.
1711 llvm::StringMap<unsigned>::iterator Pos
1712 = FrameworkNameOffset.find(Data.Framework);
1713 if (Pos == FrameworkNameOffset.end()) {
1714 Offset = FrameworkStringData.size() + 1;
1715 FrameworkStringData.append(Data.Framework.begin(),
1716 Data.Framework.end());
1717 FrameworkStringData.push_back(0);
1718
1719 FrameworkNameOffset[Data.Framework] = Offset;
1720 } else
1721 Offset = Pos->second;
1722 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001723 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001724
Richard Smith386bb072015-08-18 23:42:23 +00001725 // FIXME: If the header is excluded, we should write out some
1726 // record of that fact.
1727 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1728 if (uint32_t ModID =
1729 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1730 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1731 assert((Value >> 2) == ModID && "overflow in header module info");
1732 LE.write<uint32_t>(Value);
1733 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001734 }
1735
Douglas Gregor09b69892011-02-10 17:09:37 +00001736 assert(Out.tell() - Start == DataLen && "Wrong data length");
1737 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001738
1739 const char *strings_begin() const { return FrameworkStringData.begin(); }
1740 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001741 };
1742} // end anonymous namespace
1743
1744/// \brief Write the header search block for the list of files that
1745///
1746/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001747void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001748 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001749 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1750
1751 if (FilesByUID.size() > HS.header_file_size())
1752 FilesByUID.resize(HS.header_file_size());
1753
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001754 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001755 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001756 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001757 unsigned NumHeaderSearchEntries = 0;
1758 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1759 const FileEntry *File = FilesByUID[UID];
1760 if (!File)
1761 continue;
1762
Richard Smith386bb072015-08-18 23:42:23 +00001763 // Get the file info. This will load info from the external source if
1764 // necessary. Skip emitting this file if we have no information on it
1765 // as a header file (in which case HFI will be null) or if it hasn't
1766 // changed since it was loaded. Also skip it if it's for a modular header
1767 // from a different module; in that case, we rely on the module(s)
1768 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001769 const HeaderFileInfo *HFI =
1770 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1771 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001772 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001773
Richard Smith7ed1bc92014-12-05 22:42:13 +00001774 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001775 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001776 SmallString<128> FilenameTmp(Filename);
1777 if (PreparePathForOutput(FilenameTmp)) {
1778 // If we performed any translation on the file name at all, we need to
1779 // save this string, since the generator will refer to it later.
1780 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001781 SavedStrings.push_back(Filename);
1782 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001783
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001784 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001785 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001786 ++NumHeaderSearchEntries;
1787 }
1788
1789 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001790 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001791 uint32_t BucketOffset;
1792 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001793 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001794 llvm::raw_svector_ostream Out(TableData);
1795 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001796 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001797 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1798 }
1799
1800 // Create a blob abbreviation
1801 using namespace llvm;
1802 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1803 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1808 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1809
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001810 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001811 RecordData Record;
1812 Record.push_back(HEADER_SEARCH_TABLE);
1813 Record.push_back(BucketOffset);
1814 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001815 Record.push_back(TableData.size());
1816 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001817 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001818
1819 // Free all of the strings we had to duplicate.
1820 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001821 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001822}
1823
Douglas Gregora7f71a92009-04-10 03:52:48 +00001824/// \brief Writes the block containing the serialized form of the
1825/// source manager.
1826///
1827/// TODO: We should probably use an on-disk hash table (stored in a
1828/// blob), indexed based on the file name, so that we only create
1829/// entries for files that we actually need. In the common case (no
1830/// errors), we probably won't have to create file entries for any of
1831/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001832void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001833 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001834 RecordData Record;
1835
Chris Lattner0910e3b2009-04-10 17:16:57 +00001836 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001837 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001838
1839 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001840 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1841 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1842 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001843 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001844
Douglas Gregor258ae542009-04-27 06:38:32 +00001845 // Write out the source location entry table. We skip the first
1846 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001847 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001848 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001849 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1850 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001851 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001852 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001853 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001854 FileID FID = FileID::get(I);
1855 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001856
Douglas Gregor258ae542009-04-27 06:38:32 +00001857 // Record the offset of this source-location entry.
1858 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1859
1860 // Figure out which record code to use.
1861 unsigned Code;
1862 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001863 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1864 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001865 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001866 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001867 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001868 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001869 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001870 Record.clear();
1871 Record.push_back(Code);
1872
Douglas Gregor925296b2011-07-19 16:10:42 +00001873 // Starting offset of this entry within this module, so skip the dummy.
1874 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001875 if (SLoc->isFile()) {
1876 const SrcMgr::FileInfo &File = SLoc->getFile();
1877 Record.push_back(File.getIncludeLoc().getRawEncoding());
1878 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1879 Record.push_back(File.hasLineDirectives());
1880
1881 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001882 if (Content->OrigEntry) {
1883 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001884 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001885
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001886 // The source location entry is a file. Emit input file ID.
1887 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1888 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001889
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001890 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001891
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001892 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001893 if (FDI != FileDeclIDs.end()) {
1894 Record.push_back(FDI->second->FirstDeclIndex);
1895 Record.push_back(FDI->second->DeclIDs.size());
1896 } else {
1897 Record.push_back(0);
1898 Record.push_back(0);
1899 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001900
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001901 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001902
1903 if (Content->BufferOverridden) {
1904 Record.clear();
1905 Record.push_back(SM_SLOC_BUFFER_BLOB);
1906 const llvm::MemoryBuffer *Buffer
1907 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1908 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1909 StringRef(Buffer->getBufferStart(),
1910 Buffer->getBufferSize() + 1));
1911 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001912 } else {
1913 // The source location entry is a buffer. The blob associated
1914 // with this entry contains the contents of the buffer.
1915
1916 // We add one to the size so that we capture the trailing NULL
1917 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1918 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001919 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001920 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001921 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001922 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001923 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001924 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001925 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001926 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001927 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001928 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001929
Douglas Gregor925296b2011-07-19 16:10:42 +00001930 if (strcmp(Name, "<built-in>") == 0) {
1931 PreloadSLocs.push_back(SLocEntryOffsets.size());
1932 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001933 }
1934 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001935 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001936 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001937 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1938 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001939 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1940 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001941
1942 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001943 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001944 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001945 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001946 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001947 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001948 }
1949 }
1950
Douglas Gregor8f45df52009-04-16 22:23:12 +00001951 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001952
1953 if (SLocEntryOffsets.empty())
1954 return;
1955
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001956 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001957 // table is used for lazily loading source-location information.
1958 using namespace llvm;
1959 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001960 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1964 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001965
Douglas Gregor258ae542009-04-27 06:38:32 +00001966 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001967 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001968 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001969 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Reid Kleckner012665e2015-05-21 00:13:09 +00001970 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, bytes(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001971
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001972 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001973 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001974 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001975
1976 // Write the line table. It depends on remapping working, so it must come
1977 // after the source location offsets.
1978 if (SourceMgr.hasLineTable()) {
1979 LineTableInfo &LineTable = SourceMgr.getLineTable();
1980
1981 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00001982
1983 // Emit the needed file names.
1984 llvm::DenseMap<int, int> FilenameMap;
1985 for (const auto &L : LineTable) {
1986 if (L.first.ID < 0)
1987 continue;
1988 for (auto &LE : L.second) {
1989 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
1990 FilenameMap.size())).second)
1991 AddPath(LineTable.getFilename(LE.FilenameID), Record);
1992 }
1993 }
1994 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00001995
1996 // Emit the line entries
1997 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1998 L != LEnd; ++L) {
1999 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00002000 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002001 continue;
2002
2003 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00002004 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002005
2006 // Emit the line entries
2007 Record.push_back(L->second.size());
2008 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
2009 LEEnd = L->second.end();
2010 LE != LEEnd; ++LE) {
2011 Record.push_back(LE->FileOffset);
2012 Record.push_back(LE->LineNo);
Richard Smith63078492015-09-01 07:41:55 +00002013 Record.push_back(FilenameMap[LE->FilenameID]);
Douglas Gregor925296b2011-07-19 16:10:42 +00002014 Record.push_back((unsigned)LE->FileKind);
2015 Record.push_back(LE->IncludeOffset);
2016 }
2017 }
Richard Smith63078492015-09-01 07:41:55 +00002018
Douglas Gregor925296b2011-07-19 16:10:42 +00002019 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2020 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002021}
2022
Douglas Gregorc5046832009-04-27 18:38:38 +00002023//===----------------------------------------------------------------------===//
2024// Preprocessor Serialization
2025//===----------------------------------------------------------------------===//
2026
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002027static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2028 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002029 if (MacroInfo *MI = MD->getMacroInfo())
2030 if (MI->isBuiltinMacro())
2031 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002032
2033 if (IsModule) {
2034 SourceLocation Loc = MD->getLocation();
2035 if (Loc.isInvalid())
2036 return true;
2037 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2038 return true;
2039 }
2040
2041 return false;
2042}
2043
Chris Lattnereeffaef2009-04-10 17:15:23 +00002044/// \brief Writes the block containing the serialized form of the
2045/// preprocessor.
2046///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002047void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002048 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2049 if (PPRec)
2050 WritePreprocessorDetail(*PPRec);
2051
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002052 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002053 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002054
Chris Lattner0af3ba12009-04-13 01:29:17 +00002055 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2056 if (PP.getCounterValue() != 0) {
2057 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002058 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002059 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002060 }
2061
2062 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002063 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002064
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002065 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002066 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002067 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002068 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002069
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002070 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002071 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002072
Richard Smithee977932015-05-01 21:22:17 +00002073 // Construct the list of identifiers with macro directives that need to be
2074 // serialized.
2075 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2076 for (auto &Id : PP.getIdentifierTable())
2077 if (Id.second->hadMacroDefinition() &&
2078 (!Id.second->isFromAST() ||
2079 Id.second->hasChangedSinceDeserialization()))
2080 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002081 // Sort the set of macro definitions that need to be serialized by the
2082 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002083 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2084 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002085
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002086 // Emit the macro directives as a list and associate the offset with the
2087 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002088 for (const IdentifierInfo *Name : MacroIdentifiers) {
2089 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002090 auto StartOffset = Stream.GetCurrentBitNo();
2091
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002092 // Emit the macro directives in reverse source order.
2093 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002094 // Once we hit an ignored macro, we're done: the rest of the chain
2095 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002096 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002097 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002098
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002099 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002100 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002101 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002102 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002103 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002104 Record.push_back(VisMD->isPublic());
2105 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002106 }
Richard Smithd7329392015-04-21 21:46:32 +00002107
Richard Smithb8b2ed62015-04-23 18:18:26 +00002108 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002109 bool EmittedModuleMacros = false;
Richard Smithb8b2ed62015-04-23 18:18:26 +00002110 if (IsModule) {
2111 auto Leafs = PP.getLeafModuleMacros(Name);
2112 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2113 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2114 while (!Worklist.empty()) {
2115 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002116
Richard Smithb8b2ed62015-04-23 18:18:26 +00002117 // Emit a record indicating this submodule exports this macro.
2118 ModuleMacroRecord.push_back(
2119 getSubmoduleID(Macro->getOwningModule()));
Richard Smithee977932015-05-01 21:22:17 +00002120 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
Richard Smithb8b2ed62015-04-23 18:18:26 +00002121 for (auto *M : Macro->overrides())
2122 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002123
Richard Smithb8b2ed62015-04-23 18:18:26 +00002124 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2125 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002126
Richard Smithb8b2ed62015-04-23 18:18:26 +00002127 // Enqueue overridden macros once we've visited all their ancestors.
2128 for (auto *M : Macro->overrides())
2129 if (++Visits[M] == M->getNumOverridingMacros())
2130 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002131
2132 EmittedModuleMacros = true;
Richard Smithd7329392015-04-21 21:46:32 +00002133 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002134 }
Richard Smithd7329392015-04-21 21:46:32 +00002135
Richard Smithee977932015-05-01 21:22:17 +00002136 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002137 continue;
2138
Richard Smithd7329392015-04-21 21:46:32 +00002139 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002140 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2141 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002142 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002143
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002144 /// \brief Offsets of each of the macros into the bitstream, indexed by
2145 /// the local macro ID
2146 ///
2147 /// For each identifier that is associated with a macro, this map
2148 /// provides the offset into the bitstream where that macro is
2149 /// defined.
2150 std::vector<uint32_t> MacroOffsets;
2151
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002152 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2153 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2154 MacroInfo *MI = MacroInfosToEmit[I].MI;
2155 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002156
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002157 if (ID < FirstMacroID) {
2158 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2159 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002160 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002161
2162 // Record the local offset of this macro.
2163 unsigned Index = ID - FirstMacroID;
2164 if (Index == MacroOffsets.size())
2165 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2166 else {
2167 if (Index > MacroOffsets.size())
2168 MacroOffsets.resize(Index + 1);
2169
2170 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2171 }
2172
2173 AddIdentifierRef(Name, Record);
2174 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2175 AddSourceLocation(MI->getDefinitionLoc(), Record);
2176 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2177 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002178 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002179 unsigned Code;
2180 if (MI->isObjectLike()) {
2181 Code = PP_MACRO_OBJECT_LIKE;
2182 } else {
2183 Code = PP_MACRO_FUNCTION_LIKE;
2184
2185 Record.push_back(MI->isC99Varargs());
2186 Record.push_back(MI->isGNUVarargs());
2187 Record.push_back(MI->hasCommaPasting());
2188 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002189 for (const IdentifierInfo *Arg : MI->args())
2190 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002191 }
2192
2193 // If we have a detailed preprocessing record, record the macro definition
2194 // ID that corresponds to this macro.
2195 if (PPRec)
2196 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2197
2198 Stream.EmitRecord(Code, Record);
2199 Record.clear();
2200
2201 // Emit the tokens array.
2202 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2203 // Note that we know that the preprocessor does not have any annotation
2204 // tokens in it because they are created by the parser, and thus can't
2205 // be in a macro definition.
2206 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002207 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002208 Stream.EmitRecord(PP_TOKEN, Record);
2209 Record.clear();
2210 }
2211 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002212 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002213
Douglas Gregor92a96f52011-02-08 21:58:10 +00002214 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002215
2216 // Write the offsets table for macro IDs.
2217 using namespace llvm;
Richard Smith13090a22015-04-10 02:02:24 +00002218 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002219 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2223
2224 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2225 Record.clear();
2226 Record.push_back(MACRO_OFFSET);
2227 Record.push_back(MacroOffsets.size());
2228 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2229 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002230 bytes(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002231}
2232
2233void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002234 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002235 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002236
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002237 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002238
Douglas Gregor92a96f52011-02-08 21:58:10 +00002239 // Enter the preprocessor block.
2240 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002241
Douglas Gregoraae92242010-03-19 21:51:54 +00002242 // If the preprocessor has a preprocessing record, emit it.
2243 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002244 using namespace llvm;
2245
2246 // Set up the abbreviation for
2247 unsigned InclusionAbbrev = 0;
2248 {
2249 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2250 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2256 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2257 }
2258
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002259 unsigned FirstPreprocessorEntityID
2260 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2261 + NUM_PREDEF_PP_ENTITY_IDS;
2262 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002263 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002264 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2265 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002266 E != EEnd;
2267 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002268 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002269
Richard Smith66a81862015-05-04 02:25:31 +00002270 PreprocessedEntityOffsets.push_back(
2271 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002272
Richard Smith66a81862015-05-04 02:25:31 +00002273 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002274 // Record this macro definition's ID.
2275 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002276
Douglas Gregor92a96f52011-02-08 21:58:10 +00002277 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002278 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2279 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002280 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002281
Chandler Carrutha88a22182011-07-14 08:20:46 +00002282 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002283 Record.push_back(ME->isBuiltinMacro());
2284 if (ME->isBuiltinMacro())
2285 AddIdentifierRef(ME->getName(), Record);
2286 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002287 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002288 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002289 continue;
2290 }
2291
2292 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2293 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002294 Record.push_back(ID->getFileName().size());
2295 Record.push_back(ID->wasInQuotes());
2296 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002297 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002298 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002299 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002300 // Check that the FileEntry is not null because it was not resolved and
2301 // we create a PCH even with compiler errors.
2302 if (ID->getFile())
2303 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002304 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2305 continue;
2306 }
2307
2308 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2309 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002310 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002311
Douglas Gregoraae92242010-03-19 21:51:54 +00002312 // Write the offsets table for the preprocessing record.
2313 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002314 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2315
Douglas Gregoraae92242010-03-19 21:51:54 +00002316 // Write the offsets table for identifier IDs.
2317 using namespace llvm;
2318 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002319 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002321 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002322 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002323
Douglas Gregoraae92242010-03-19 21:51:54 +00002324 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002325 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002326 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002327 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002328 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002329 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002330}
2331
Richard Smith386bb072015-08-18 23:42:23 +00002332unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002333 if (!Mod)
2334 return 0;
2335
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002336 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2337 if (Known != SubmoduleIDs.end())
2338 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002339
2340 if (Mod->getTopLevelModule() != WritingModule)
2341 return 0;
2342
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002343 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2344}
2345
Richard Smith386bb072015-08-18 23:42:23 +00002346unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2347 // FIXME: This can easily happen, if we have a reference to a submodule that
2348 // did not result in us loading a module file for that submodule. For
2349 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2350 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2351 assert((ID || !Mod) &&
2352 "asked for module ID for non-local, non-imported module");
2353 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002354}
2355
Douglas Gregor253eefe2011-12-01 00:59:36 +00002356/// \brief Compute the number of modules within the given tree (including the
2357/// given module).
2358static unsigned getNumberOfModules(Module *Mod) {
2359 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002360 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2361 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002362 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002363 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002364
2365 return ChildModules + 1;
2366}
2367
Douglas Gregorde3ef502011-11-30 23:21:26 +00002368void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002369 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002370 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002371
2372 // Write the abbreviations needed for the submodules block.
2373 using namespace llvm;
2374 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2375 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2387 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2388
2389 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002390 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2392 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2393
2394 Abbrev = new BitCodeAbbrev();
2395 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2397 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002398
2399 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002400 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2402 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2403
2404 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002405 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2407 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2408
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002409 Abbrev = new BitCodeAbbrev();
2410 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002413 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2414
Douglas Gregor59527662012-10-15 06:28:11 +00002415 Abbrev = new BitCodeAbbrev();
2416 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2418 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2419
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002420 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002421 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2423 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2424
2425 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002426 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2428 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2429
2430 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002431 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2433 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2434
2435 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002436 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2439 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2440
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002441 Abbrev = new BitCodeAbbrev();
2442 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2444 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2445
Douglas Gregorfb912652013-03-20 21:10:35 +00002446 Abbrev = new BitCodeAbbrev();
2447 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2450 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2451
Douglas Gregor253eefe2011-12-01 00:59:36 +00002452 // Write the submodule metadata block.
2453 RecordData Record;
2454 Record.push_back(getNumberOfModules(WritingModule));
2455 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2456 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2457
Douglas Gregor69021972011-11-30 17:33:56 +00002458 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002459 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002460 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002461 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002462 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002463 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002464 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002465
2466 // Emit the definition of the block.
2467 Record.clear();
2468 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002469 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002470 if (Mod->Parent) {
2471 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2472 Record.push_back(SubmoduleIDs[Mod->Parent]);
2473 } else {
2474 Record.push_back(0);
2475 }
2476 Record.push_back(Mod->IsFramework);
2477 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002478 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002479 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002480 Record.push_back(Mod->InferSubmodules);
2481 Record.push_back(Mod->InferExplicitSubmodules);
2482 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002483 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002484 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2485
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002486 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002487 for (const auto &R : Mod->Requirements) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002488 Record.clear();
2489 Record.push_back(SUBMODULE_REQUIRES);
Richard Smith080056b2015-08-18 21:53:42 +00002490 Record.push_back(R.second);
2491 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002492 }
2493
Douglas Gregor69021972011-11-30 17:33:56 +00002494 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002495 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002496 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002497 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Richard Smith2b63d152015-05-16 02:28:53 +00002498 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2499 UmbrellaHeader.NameAsWritten);
2500 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Douglas Gregor524e33e2011-12-08 19:11:24 +00002501 Record.clear();
2502 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2503 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002504 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002505 }
Richard Smith306d8922014-10-22 23:50:56 +00002506
Douglas Gregor69021972011-11-30 17:33:56 +00002507 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002508 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002509 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002510 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002511 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002512 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002513 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2514 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2515 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002516 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002517 Module::HK_PrivateTextual},
2518 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002519 };
2520 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002521 Record.clear();
Richard Smith3c1a41a2014-12-02 00:08:08 +00002522 Record.push_back(HL.RecordKind);
2523 for (auto &H : Mod->Headers[HL.HeaderKind])
2524 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2525 }
2526
2527 // Emit the top headers.
2528 {
2529 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2530 Record.clear();
2531 Record.push_back(SUBMODULE_TOPHEADER);
2532 for (auto *H : TopHeaders)
2533 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002534 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002535
2536 // Emit the imports.
2537 if (!Mod->Imports.empty()) {
2538 Record.clear();
Richard Smith080056b2015-08-18 21:53:42 +00002539 for (auto *I : Mod->Imports)
2540 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002541 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2542 }
2543
Douglas Gregor24bb9232011-12-02 18:58:38 +00002544 // Emit the exports.
2545 if (!Mod->Exports.empty()) {
2546 Record.clear();
Richard Smith080056b2015-08-18 21:53:42 +00002547 for (const auto &E : Mod->Exports) {
2548 // FIXME: This may fail; we don't require that all exported modules
2549 // are local or imported.
2550 Record.push_back(getSubmoduleID(E.getPointer()));
2551 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002552 }
2553 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2554 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002555
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002556 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2557 // Might be unnecessary as use declarations are only used to build the
2558 // module itself.
2559
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002560 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002561 for (const auto &LL : Mod->LinkLibraries) {
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002562 Record.clear();
2563 Record.push_back(SUBMODULE_LINK_LIBRARY);
Richard Smith080056b2015-08-18 21:53:42 +00002564 Record.push_back(LL.IsFramework);
2565 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002566 }
2567
Douglas Gregorfb912652013-03-20 21:10:35 +00002568 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002569 for (const auto &C : Mod->Conflicts) {
Douglas Gregorfb912652013-03-20 21:10:35 +00002570 Record.clear();
2571 Record.push_back(SUBMODULE_CONFLICT);
Richard Smith080056b2015-08-18 21:53:42 +00002572 // FIXME: This may fail; we don't require that all conflicting modules
2573 // are local or imported.
2574 Record.push_back(getSubmoduleID(C.Other));
2575 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002576 }
2577
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002578 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002579 for (const auto &CM : Mod->ConfigMacros) {
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002580 Record.clear();
2581 Record.push_back(SUBMODULE_CONFIG_MACRO);
Richard Smith080056b2015-08-18 21:53:42 +00002582 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002583 }
2584
Douglas Gregor69021972011-11-30 17:33:56 +00002585 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002586 for (auto *M : Mod->submodules())
2587 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002588 }
2589
2590 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002591
Richard Smith23d8d032015-05-14 00:45:20 +00002592 assert((NextSubmoduleID - FirstSubmoduleID ==
2593 getNumberOfModules(WritingModule)) &&
2594 "Wrong # of submodules; found a reference to a non-local, "
2595 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002596}
2597
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002598serialization::SubmoduleID
2599ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002600 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002601 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002602
2603 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002604 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002605 Module *OwningMod
2606 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002607 if (!OwningMod)
2608 return 0;
2609
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002610 // Check whether this submodule is part of our own module.
2611 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002612 return 0;
2613
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002614 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002615}
2616
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002617void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2618 bool isModule) {
2619 // Make sure set diagnostic pragmas don't affect the translation unit that
2620 // imports the module.
2621 // FIXME: Make diagnostic pragma sections work properly with modules.
2622 if (isModule)
2623 return;
2624
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002625 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2626 DiagStateIDMap;
2627 unsigned CurrID = 0;
2628 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002629 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002630 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002631 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2632 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002633 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002634 if (point.Loc.isInvalid())
2635 continue;
2636
2637 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002638 unsigned &DiagStateID = DiagStateIDMap[point.State];
2639 Record.push_back(DiagStateID);
2640
2641 if (DiagStateID == 0) {
2642 DiagStateID = ++CurrID;
2643 for (DiagnosticsEngine::DiagState::const_iterator
2644 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2645 if (I->second.isPragma()) {
2646 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002647 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002648 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002649 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002650 Record.push_back(-1); // mark the end of the diag/map pairs for this
2651 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002652 }
2653 }
2654
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002655 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002656 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002657}
2658
Richard Smithc2bb8182015-03-24 06:36:48 +00002659void ASTWriter::WriteCXXCtorInitializersOffsets() {
2660 if (CXXCtorInitializersOffsets.empty())
2661 return;
2662
2663 RecordData Record;
2664
2665 // Create a blob abbreviation for the C++ ctor initializer offsets.
2666 using namespace llvm;
2667
2668 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2669 Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2671 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2672 unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2673
2674 // Write the base specifier offsets table.
2675 Record.clear();
2676 Record.push_back(CXX_CTOR_INITIALIZERS_OFFSETS);
2677 Record.push_back(CXXCtorInitializersOffsets.size());
2678 Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002679 bytes(CXXCtorInitializersOffsets));
Richard Smithc2bb8182015-03-24 06:36:48 +00002680}
2681
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002682void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2683 if (CXXBaseSpecifiersOffsets.empty())
2684 return;
2685
2686 RecordData Record;
2687
2688 // Create a blob abbreviation for the C++ base specifiers offsets.
2689 using namespace llvm;
2690
2691 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2692 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2693 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2694 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2695 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2696
Douglas Gregorc27b2872011-08-04 00:01:48 +00002697 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002698 Record.clear();
2699 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2700 Record.push_back(CXXBaseSpecifiersOffsets.size());
2701 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002702 bytes(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002703}
2704
Douglas Gregorc5046832009-04-27 18:38:38 +00002705//===----------------------------------------------------------------------===//
2706// Type Serialization
2707//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002708
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002709/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002710void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002711 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002712 if (Idx.getIndex() == 0) // we haven't seen this type before.
2713 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002714
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002715 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002716
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002717 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002718 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002719 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002720 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002721 else if (TypeOffsets.size() < Index) {
2722 TypeOffsets.resize(Index + 1);
2723 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002724 }
2725
2726 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002727
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002728 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002729 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002730 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002731
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002732 if (T.hasLocalNonFastQualifiers()) {
2733 Qualifiers Qs = T.getLocalQualifiers();
2734 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002735 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002737 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002738 } else {
2739 switch (T->getTypeClass()) {
2740 // For all of the concrete, non-dependent types, call the
2741 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002742#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002743 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002744#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002745#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002746 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002747 }
2748
2749 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002750 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002751
2752 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002753 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002754}
2755
Douglas Gregorc5046832009-04-27 18:38:38 +00002756//===----------------------------------------------------------------------===//
2757// Declaration Serialization
2758//===----------------------------------------------------------------------===//
2759
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002760/// \brief Write the block containing all of the declaration IDs
2761/// lexically declared within the given DeclContext.
2762///
2763/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2764/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002765uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002766 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002767 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002768 return 0;
2769
Douglas Gregor8f45df52009-04-16 22:23:12 +00002770 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002771 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002772 Record.push_back(DECL_CONTEXT_LEXICAL);
Richard Smith82f8fcd2015-08-06 22:07:25 +00002773 SmallVector<uint32_t, 128> KindDeclPairs;
2774 for (const auto *D : DC->decls()) {
2775 KindDeclPairs.push_back(D->getKind());
2776 KindDeclPairs.push_back(GetDeclRef(D));
2777 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002778
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002779 ++NumLexicalDeclContexts;
Richard Smith82f8fcd2015-08-06 22:07:25 +00002780 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2781 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002782 return Offset;
2783}
2784
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002785void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002786 using namespace llvm;
2787 RecordData Record;
2788
2789 // Write the type offsets array
2790 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002791 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2795 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2796 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002797 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002798 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002799 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002800 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002801
2802 // Write the declaration offsets array
2803 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002804 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2808 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2809 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002810 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002811 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002812 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002813 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002814}
2815
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002816void ASTWriter::WriteFileDeclIDsMap() {
2817 using namespace llvm;
2818 RecordData Record;
2819
Chandler Carruthb306d732015-03-27 00:31:20 +00002820 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2821 FileDeclIDs.begin(), FileDeclIDs.end());
2822 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2823 llvm::less_first());
2824
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002825 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002826 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2827 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2828 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2829 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2830 for (auto &LocDeclEntry : Info.DeclIDs)
2831 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002832 }
2833
2834 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2835 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002837 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2838 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2839 Record.push_back(FILE_SORTED_DECLS);
Chandler Carruthb306d732015-03-27 00:31:20 +00002840 Record.push_back(FileGroupedDeclIDs.size());
Reid Kleckner012665e2015-05-21 00:13:09 +00002841 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002842}
2843
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002844void ASTWriter::WriteComments() {
2845 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002846 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002847 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002848 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2849 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002850 I != E; ++I) {
2851 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002852 AddSourceRange((*I)->getSourceRange(), Record);
2853 Record.push_back((*I)->getKind());
2854 Record.push_back((*I)->isTrailingComment());
2855 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002856 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2857 }
2858 Stream.ExitBlock();
2859}
2860
Douglas Gregorc5046832009-04-27 18:38:38 +00002861//===----------------------------------------------------------------------===//
2862// Global Method Pool and Selector Serialization
2863//===----------------------------------------------------------------------===//
2864
Douglas Gregore84a9da2009-04-20 20:36:09 +00002865namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002866// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002867class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002868 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002869
2870public:
2871 typedef Selector key_type;
2872 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002873
Sebastian Redl834bb972010-08-04 17:20:04 +00002874 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002875 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002876 ObjCMethodList Instance, Factory;
2877 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002878 typedef const data_type& data_type_ref;
2879
Justin Bogner25463f12014-04-18 20:27:24 +00002880 typedef unsigned hash_value_type;
2881 typedef unsigned offset_type;
2882
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002883 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002884
Justin Bogner25463f12014-04-18 20:27:24 +00002885 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002886 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002887 }
Mike Stump11289f42009-09-09 15:08:12 +00002888
2889 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002890 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002891 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002892 using namespace llvm::support;
2893 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002894 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002895 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002896 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2897 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002898 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002899 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002900 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002901 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002902 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002903 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002904 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002905 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002906 return std::make_pair(KeyLen, DataLen);
2907 }
Mike Stump11289f42009-09-09 15:08:12 +00002908
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002909 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002910 using namespace llvm::support;
2911 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002912 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002913 assert((Start >> 32) == 0 && "Selector key offset too large");
2914 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002915 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002916 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002917 if (N == 0)
2918 N = 1;
2919 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002920 LE.write<uint32_t>(
2921 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002922 }
Mike Stump11289f42009-09-09 15:08:12 +00002923
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002924 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002925 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002926 using namespace llvm::support;
2927 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002928 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002929 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002930 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002931 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002932 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002933 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002934 ++NumInstanceMethods;
2935
2936 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002937 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002938 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002939 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002940 ++NumFactoryMethods;
2941
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002942 unsigned InstanceBits = Methods.Instance.getBits();
2943 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002944 unsigned InstanceHasMoreThanOneDeclBit =
2945 Methods.Instance.hasMoreThanOneDecl();
2946 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2947 (InstanceHasMoreThanOneDeclBit << 2) |
2948 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002949 unsigned FactoryBits = Methods.Factory.getBits();
2950 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002951 unsigned FactoryHasMoreThanOneDeclBit =
2952 Methods.Factory.hasMoreThanOneDecl();
2953 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2954 (FactoryHasMoreThanOneDeclBit << 2) |
2955 FactoryBits;
2956 LE.write<uint16_t>(FullInstanceBits);
2957 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002958 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002959 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002960 if (Method->getMethod())
2961 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002962 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002963 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002964 if (Method->getMethod())
2965 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002966
2967 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002968 }
2969};
2970} // end anonymous namespace
2971
Sebastian Redla19a67f2010-08-03 21:58:15 +00002972/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002973///
2974/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002975/// in an on-disk hash table indexed by the selector. The hash table also
2976/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002977void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002978 using namespace llvm;
2979
Sebastian Redla19a67f2010-08-03 21:58:15 +00002980 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002981 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002982 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002983 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002984 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002985 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002986 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002987 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002988
Sebastian Redla19a67f2010-08-03 21:58:15 +00002989 // Create the on-disk hash table representation. We walk through every
2990 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002991 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002992 for (auto &SelectorAndID : SelectorIDs) {
2993 Selector S = SelectorAndID.first;
2994 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002995 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002996 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002997 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00002998 ObjCMethodList(),
2999 ObjCMethodList()
3000 };
3001 if (F != SemaRef.MethodPool.end()) {
3002 Data.Instance = F->second.first;
3003 Data.Factory = F->second.second;
3004 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003005 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003006 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003007 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003008 // Selector already exists. Did it change?
3009 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003010 for (ObjCMethodList *M = &Data.Instance;
3011 !changed && M && M->getMethod(); M = M->getNext()) {
3012 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003013 changed = true;
3014 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003015 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003016 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003017 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003018 changed = true;
3019 }
3020 if (!changed)
3021 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003022 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003023 // A new method pool entry.
3024 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003025 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003026 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003027 }
3028
Douglas Gregorc78d3462009-04-24 21:10:55 +00003029 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003030 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003031 uint32_t BucketOffset;
3032 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003033 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003034 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003035 llvm::raw_svector_ostream Out(MethodPool);
3036 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003037 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003038 BucketOffset = Generator.Emit(Out, Trait);
3039 }
3040
3041 // Create a blob abbreviation
3042 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003043 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3047 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3048
Douglas Gregor95c13f52009-04-25 17:48:32 +00003049 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003050 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003051 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003052 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003053 Record.push_back(NumTableEntries);
Yaron Keren92e1b622015-03-18 10:17:07 +00003054 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003055
3056 // Create a blob abbreviation for the selector table offsets.
3057 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003058 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3062 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3063
3064 // Write the selector offsets table.
3065 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003066 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003067 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003068 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003069 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003070 bytes(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003071 }
3072}
3073
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003074/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003075void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003076 using namespace llvm;
3077 if (SemaRef.ReferencedSelectors.empty())
3078 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003079
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003080 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003081
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003082 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003083 // very tricky to fix, and given that @selector shouldn't really appear in
3084 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003085 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3086 Selector Sel = SelectorAndLocation.first;
3087 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003088 AddSelectorRef(Sel, Record);
3089 AddSourceLocation(Loc, Record);
3090 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003091 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003092}
3093
Douglas Gregorc5046832009-04-27 18:38:38 +00003094//===----------------------------------------------------------------------===//
3095// Identifier Table Serialization
3096//===----------------------------------------------------------------------===//
3097
Richard Smithb3761912015-02-05 23:08:52 +00003098/// Determine the declaration that should be put into the name lookup table to
3099/// represent the given declaration in this module. This is usually D itself,
3100/// but if D was imported and merged into a local declaration, we want the most
3101/// recent local declaration instead. The chosen declaration will be the most
3102/// recent declaration in any module that imports this one.
3103static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3104 NamedDecl *D) {
3105 if (!LangOpts.Modules || !D->isFromASTFile())
3106 return D;
3107
3108 if (Decl *Redecl = D->getPreviousDecl()) {
3109 // For Redeclarable decls, a prior declaration might be local.
3110 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3111 if (!Redecl->isFromASTFile())
3112 return cast<NamedDecl>(Redecl);
Richard Smithfe620d22015-03-05 23:24:12 +00003113 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003114 // local one.
3115 if (D->getOwningModuleID() == 0)
3116 break;
3117 }
3118 } else if (Decl *First = D->getCanonicalDecl()) {
3119 // For Mergeable decls, the first decl might be local.
3120 if (!First->isFromASTFile())
3121 return cast<NamedDecl>(First);
3122 }
3123
3124 // All declarations are imported. Our most recent declaration will also be
3125 // the most recent one in anyone who imports us.
3126 return D;
3127}
3128
Douglas Gregorc78d3462009-04-24 21:10:55 +00003129namespace {
Richard Smithcf97cf62015-04-19 01:34:23 +00003130class ASTIdentifierTableTrait {
3131 ASTWriter &Writer;
3132 Preprocessor &PP;
3133 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003134 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003135 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003136 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003137
3138 /// \brief Determines whether this is an "interesting" identifier that needs a
3139 /// full IdentifierInfo structure written into the hash table. Notably, this
3140 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3141 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003142 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003143 if (MacroOffset ||
3144 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003145 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003146 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003147 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003148 return true;
3149
3150 return false;
3151 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003152
Douglas Gregore84a9da2009-04-20 20:36:09 +00003153public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003154 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003155 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003156
Sebastian Redl539c5062010-08-18 23:57:32 +00003157 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003158 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003159
Justin Bogner25463f12014-04-18 20:27:24 +00003160 typedef unsigned hash_value_type;
3161 typedef unsigned offset_type;
3162
Richard Smithd7329392015-04-21 21:46:32 +00003163 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003164 IdentifierResolver &IdResolver, bool IsModule,
3165 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003166 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003167 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3168 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003169
Justin Bogner25463f12014-04-18 20:27:24 +00003170 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003171 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003172 }
Mike Stump11289f42009-09-09 15:08:12 +00003173
Richard Smith33e0f7e2015-07-22 02:08:40 +00003174 bool isInterestingIdentifier(const IdentifierInfo *II) {
3175 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3176 return isInterestingIdentifier(II, MacroOffset);
3177 }
Richard Smith9c254182015-07-19 21:41:12 +00003178 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3179 return isInterestingIdentifier(II, 0);
3180 }
3181
Mike Stump11289f42009-09-09 15:08:12 +00003182 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003183 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003184 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003185 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003186 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3187 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003188 DataLen += 2; // 2 bytes for builtin ID
3189 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003190 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003191 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003192
Richard Smitha534a312015-07-21 23:54:07 +00003193 if (NeedDecls) {
3194 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3195 DEnd = IdResolver.end();
3196 D != DEnd; ++D)
3197 DataLen += 4;
3198 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003199 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003200 using namespace llvm::support;
3201 endian::Writer<little> LE(Out);
3202
Richard Smithdf8a8312015-03-13 04:05:01 +00003203 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003204 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003205 // We emit the key length after the data length so that every
3206 // string is preceded by a 16-bit length. This matches the PTH
3207 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003208 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003209 return std::make_pair(KeyLen, DataLen);
3210 }
Mike Stump11289f42009-09-09 15:08:12 +00003211
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003212 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003213 unsigned KeyLen) {
3214 // Record the location of the key data. This is used when generating
3215 // the mapping from persistent IDs to strings.
3216 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003217
3218 // Emit the offset of the key/data length information to the interesting
3219 // identifiers table if necessary.
3220 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3221 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3222
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003223 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003224 }
Mike Stump11289f42009-09-09 15:08:12 +00003225
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003226 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003228 using namespace llvm::support;
3229 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003230
Richard Smithd7329392015-04-21 21:46:32 +00003231 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3232 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003233 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003234 return;
3235 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003236
Justin Bognere1c147c2014-03-28 22:03:19 +00003237 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003238 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3239 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003240 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003241 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003242 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003243 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003244 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3245 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003246 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003247 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003248 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003249 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003250
Richard Smithd7329392015-04-21 21:46:32 +00003251 if (HadMacroDefinition)
3252 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003253
Richard Smitha534a312015-07-21 23:54:07 +00003254 if (NeedDecls) {
3255 // Emit the declaration IDs in reverse order, because the
3256 // IdentifierResolver provides the declarations as they would be
3257 // visible (e.g., the function "stat" would come before the struct
3258 // "stat"), but the ASTReader adds declarations to the end of the list
3259 // (so we need to see the struct "stat" before the function "stat").
3260 // Only emit declarations that aren't from a chained PCH, though.
3261 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3262 IdResolver.end());
3263 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3264 DEnd = Decls.rend();
3265 D != DEnd; ++D)
3266 LE.write<uint32_t>(
3267 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3268 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003269 }
3270};
3271} // end anonymous namespace
3272
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003273/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003274///
3275/// The identifier table consists of a blob containing string data
3276/// (the actual identifiers themselves) and a separate "offsets" index
3277/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003278void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3279 IdentifierResolver &IdResolver,
3280 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003281 using namespace llvm;
3282
Richard Smith33e0f7e2015-07-22 02:08:40 +00003283 RecordData InterestingIdents;
3284
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003285 // Create and write out the blob that contains the identifier
3286 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003287 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003288 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003289 ASTIdentifierTableTrait Trait(
3290 *this, PP, IdResolver, IsModule,
3291 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003292
Douglas Gregore6648fb2009-04-28 20:33:11 +00003293 // Look for any identifiers that were named while processing the
3294 // headers, but are otherwise not needed. We add these to the hash
3295 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003296 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003297 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003298 SmallVector<const IdentifierInfo *, 128> IIs;
Douglas Gregore6648fb2009-04-28 20:33:11 +00003299 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3300 IDEnd = PP.getIdentifierTable().end();
3301 ID != IDEnd; ++ID)
Chandler Carruth8440c982015-03-26 23:54:15 +00003302 IIs.push_back(ID->second);
3303 // Sort the identifiers lexicographically before getting them references so
3304 // that their order is stable.
3305 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3306 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003307 if (Trait.isInterestingNonMacroIdentifier(II))
3308 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003309
Sebastian Redlff4a2952010-07-23 23:49:55 +00003310 // Create the on-disk hash table representation. We only store offsets
3311 // for identifiers that appear here for the first time.
3312 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003313 for (auto IdentIDPair : IdentifierIDs) {
3314 IdentifierInfo *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3315 IdentID ID = IdentIDPair.second;
3316 assert(II && "NULL identifier in identifier table");
3317 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3318 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003319 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003320
Douglas Gregore84a9da2009-04-20 20:36:09 +00003321 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003322 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003323 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003324 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003325 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003326 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003327 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003328 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003329 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003330 }
3331
3332 // Create a blob abbreviation
3333 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003334 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003337 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003338
3339 // Write the identifier table
3340 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003341 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003342 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003343 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003344 }
3345
3346 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003347 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003348 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003349 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3352 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3353
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003354#ifndef NDEBUG
3355 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3356 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3357#endif
3358
Douglas Gregor0e149972009-04-25 19:10:14 +00003359 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003360 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003361 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003362 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003363 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003364 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003365
3366 // In C++, write the list of interesting identifiers (those that are
3367 // defined as macros, poisoned, or similar unusual things).
3368 if (!InterestingIdents.empty())
3369 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003370}
3371
Douglas Gregorc5046832009-04-27 18:38:38 +00003372//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003373// DeclContext's Name Lookup Table Serialization
3374//===----------------------------------------------------------------------===//
3375
3376namespace {
3377// Trait used for the on-disk hash table used in the method pool.
3378class ASTDeclContextNameLookupTrait {
3379 ASTWriter &Writer;
3380
3381public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003382 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003383 typedef key_type key_type_ref;
3384
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003385 typedef DeclContext::lookup_result data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003386 typedef const data_type& data_type_ref;
3387
Justin Bogner25463f12014-04-18 20:27:24 +00003388 typedef unsigned hash_value_type;
3389 typedef unsigned offset_type;
3390
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003391 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3392
Richard Smitha06c7e62015-08-26 23:55:49 +00003393 hash_value_type ComputeHash(DeclarationNameKey Name) {
3394 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003395 }
3396
Richard Smitha06c7e62015-08-26 23:55:49 +00003397 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3398 DeclarationNameKey Name,
3399 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003400 using namespace llvm::support;
3401 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003402 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003403 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003404 case DeclarationName::Identifier:
3405 case DeclarationName::ObjCZeroArgSelector:
3406 case DeclarationName::ObjCOneArgSelector:
3407 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003408 case DeclarationName::CXXLiteralOperatorName:
3409 KeyLen += 4;
3410 break;
3411 case DeclarationName::CXXOperatorName:
3412 KeyLen += 1;
3413 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003414 case DeclarationName::CXXConstructorName:
3415 case DeclarationName::CXXDestructorName:
3416 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003417 case DeclarationName::CXXUsingDirective:
3418 break;
3419 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003420 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003421
Richard Smithf02662d2015-07-30 03:17:16 +00003422 // 4 bytes for each DeclID.
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003423 unsigned DataLen = 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003424 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003425
3426 return std::make_pair(KeyLen, DataLen);
3427 }
3428
Richard Smitha06c7e62015-08-26 23:55:49 +00003429 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003430 using namespace llvm::support;
3431 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003432 LE.write<uint8_t>(Name.getKind());
3433 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003434 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003435 case DeclarationName::CXXLiteralOperatorName:
3436 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003437 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003438 case DeclarationName::ObjCZeroArgSelector:
3439 case DeclarationName::ObjCOneArgSelector:
3440 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003441 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003442 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003443 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003444 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003445 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003446 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003447 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003448 case DeclarationName::CXXConstructorName:
3449 case DeclarationName::CXXDestructorName:
3450 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003451 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003452 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003453 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003454
3455 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003456 }
3457
Richard Smitha06c7e62015-08-26 23:55:49 +00003458 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3459 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003460 using namespace llvm::support;
3461 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003462 uint64_t Start = Out.tell(); (void)Start;
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003463 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3464 I != E; ++I)
3465 LE.write<uint32_t>(
3466 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I)));
3467
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003468 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3469 }
3470};
3471} // end anonymous namespace
3472
Chandler Carruthe972c362015-03-26 03:11:40 +00003473bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3474 DeclContext *DC) {
3475 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3476}
3477
3478bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3479 DeclContext *DC) {
3480 for (auto *D : Result.getLookupResult())
3481 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3482 return false;
3483
3484 return true;
3485}
3486
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003487uint32_t
Chandler Carruthe972c362015-03-26 03:11:40 +00003488ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003489 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003490 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3491 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003492 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003493
Chandler Carruthe972c362015-03-26 03:11:40 +00003494 // FIXME: We need to build the lookups table, which is logically const.
3495 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
3496 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3497
3498 // Create the on-disk hash table representation.
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003499 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3500 Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003501 ASTDeclContextNameLookupTrait Trait(*this);
3502
Chandler Carruthe972c362015-03-26 03:11:40 +00003503 // The first step is to collect the declaration names which we need to
3504 // serialize into the name lookup table, and to collect them in a stable
3505 // order.
3506 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003507
Chandler Carruthe972c362015-03-26 03:11:40 +00003508 // We also build up small sets of the constructor and conversion function
3509 // names which are visible.
3510 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003511
Chandler Carruthe972c362015-03-26 03:11:40 +00003512 for (auto &Lookup : *DC->buildLookup()) {
3513 auto &Name = Lookup.first;
3514 auto &Result = Lookup.second;
3515
3516 // If there are no local declarations in our lookup result, we don't
3517 // need to write an entry for the name at all unless we're rewriting
3518 // the decl context. If we can't write out a lookup set without
3519 // performing more deserialization, just skip this entry.
3520 if (isLookupResultExternal(Result, DC) && !isRewritten(cast<Decl>(DC)) &&
3521 isLookupResultEntirelyExternal(Result, DC))
3522 continue;
3523
3524 // We also skip empty results. If any of the results could be external and
3525 // the currently available results are empty, then all of the results are
3526 // external and we skip it above. So the only way we get here with an empty
3527 // results is when no results could have been external *and* we have
3528 // external results.
3529 //
3530 // FIXME: While we might want to start emitting on-disk entries for negative
3531 // lookups into a decl context as an optimization, today we *have* to skip
3532 // them because there are names with empty lookup results in decl contexts
3533 // which we can't emit in any stable ordering: we lookup constructors and
3534 // conversion functions in the enclosing namespace scope creating empty
3535 // results for them. This in almost certainly a bug in Clang's name lookup,
3536 // but that is likely to be hard or impossible to fix and so we tolerate it
3537 // here by omitting lookups with empty results.
3538 if (Lookup.second.getLookupResult().empty())
3539 continue;
3540
3541 switch (Lookup.first.getNameKind()) {
3542 default:
3543 Names.push_back(Lookup.first);
3544 break;
3545
Richard Smithcd45dbc2014-04-19 03:48:30 +00003546 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003547 assert(isa<CXXRecordDecl>(DC) &&
3548 "Cannot have a constructor name outside of a class!");
3549 ConstructorNameSet.insert(Name);
3550 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003551
3552 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003553 assert(isa<CXXRecordDecl>(DC) &&
3554 "Cannot have a conversion function name outside of a class!");
3555 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003556 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003557 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003558 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003559
Chandler Carruthe972c362015-03-26 03:11:40 +00003560 // Sort the names into a stable order.
3561 std::sort(Names.begin(), Names.end());
3562
Chandler Carruthffbf7052015-03-26 22:27:09 +00003563 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003564 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003565 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003566
Chandler Carruthffbf7052015-03-26 22:27:09 +00003567 // First we try the easy case by forming the current context's constructor
3568 // name and adding that name first. This is a very useful optimization to
3569 // avoid walking the lexical declarations in many cases, and it also
3570 // handles the only case where a constructor name can come from some other
3571 // lexical context -- when that name is an implicit constructor merged from
3572 // another declaration in the redecl chain. Any non-implicit constructor or
3573 // conversion function which doesn't occur in all the lexical contexts
3574 // would be an ODR violation.
3575 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3576 Context->getCanonicalType(Context->getRecordType(D)));
3577 if (ConstructorNameSet.erase(ImplicitCtorName))
3578 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003579
Chandler Carruthffbf7052015-03-26 22:27:09 +00003580 // If we still have constructors or conversion functions, we walk all the
3581 // names in the decl and add the constructors and conversion functions
3582 // which are visible in the order they lexically occur within the context.
3583 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3584 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3585 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3586 auto Name = ChildND->getDeclName();
3587 switch (Name.getNameKind()) {
3588 default:
3589 continue;
3590
3591 case DeclarationName::CXXConstructorName:
3592 if (ConstructorNameSet.erase(Name))
3593 Names.push_back(Name);
3594 break;
3595
3596 case DeclarationName::CXXConversionFunctionName:
3597 if (ConversionNameSet.erase(Name))
3598 Names.push_back(Name);
3599 break;
3600 }
3601
3602 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3603 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003604 }
3605
Chandler Carruthe972c362015-03-26 03:11:40 +00003606 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3607 "constructors by walking all the "
3608 "lexical members of the context.");
3609 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3610 "conversion functions by walking all "
3611 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003612 }
3613
Chandler Carruthe972c362015-03-26 03:11:40 +00003614 // Next we need to do a lookup with each name into this decl context to fully
3615 // populate any results from external sources. We don't actually use the
3616 // results of these lookups because we only want to use the results after all
3617 // results have been loaded and the pointers into them will be stable.
3618 for (auto &Name : Names)
3619 DC->lookup(Name);
3620
3621 // Now we need to insert the results for each name into the hash table. For
3622 // constructor names and conversion function names, we actually need to merge
3623 // all of the results for them into one list of results each and insert
3624 // those.
3625 SmallVector<NamedDecl *, 8> ConstructorDecls;
3626 SmallVector<NamedDecl *, 8> ConversionDecls;
3627
3628 // Now loop over the names, either inserting them or appending for the two
3629 // special cases.
3630 for (auto &Name : Names) {
3631 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3632
3633 switch (Name.getNameKind()) {
3634 default:
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003635 Generator.insert(Name, Result, Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003636 break;
3637
3638 case DeclarationName::CXXConstructorName:
3639 ConstructorDecls.append(Result.begin(), Result.end());
3640 break;
3641
3642 case DeclarationName::CXXConversionFunctionName:
3643 ConversionDecls.append(Result.begin(), Result.end());
3644 break;
3645 }
3646 }
3647
3648 // Handle our two special cases if we ended up having any. We arbitrarily use
3649 // the first declaration's name here because the name itself isn't part of
3650 // the key, only the kind of name is used.
3651 if (!ConstructorDecls.empty())
3652 Generator.insert(ConstructorDecls.front()->getDeclName(),
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003653 DeclContext::lookup_result(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003654 if (!ConversionDecls.empty())
3655 Generator.insert(ConversionDecls.front()->getDeclName(),
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003656 DeclContext::lookup_result(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003657
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003658 // Create the on-disk hash table in a buffer.
3659 llvm::raw_svector_ostream Out(LookupTable);
3660 // Make sure that no bucket is at offset 0
3661 using namespace llvm::support;
3662 endian::Writer<little>(Out).write<uint32_t>(0);
3663 return Generator.Emit(Out, Trait);
Richard Smith961eae52014-03-25 01:14:22 +00003664}
3665
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003666/// \brief Write the block containing all of the declaration IDs
3667/// visible from the given DeclContext.
3668///
3669/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003670/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003671uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3672 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003673 // If we imported a key declaration of this namespace, write the visible
3674 // lookup results as an update record for it rather than including them
3675 // on this declaration. We will only look at key declarations on reload.
3676 if (isa<NamespaceDecl>(DC) && Chain &&
3677 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3678 // Only do this once, for the first local declaration of the namespace.
3679 for (NamespaceDecl *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
3680 Prev = Prev->getPreviousDecl())
3681 if (!Prev->isFromASTFile())
3682 return 0;
3683
3684 // Note that we need to emit an update record for the primary context.
3685 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3686
3687 // Make sure all visible decls are written. They will be recorded later. We
3688 // do this using a side data structure so we can sort the names into
3689 // a deterministic order.
3690 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3691 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3692 LookupResults;
3693 if (Map) {
3694 LookupResults.reserve(Map->size());
3695 for (auto &Entry : *Map)
3696 LookupResults.push_back(
3697 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3698 }
3699
3700 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3701 for (auto &NameAndResult : LookupResults) {
3702 DeclarationName Name = NameAndResult.first;
3703 DeclContext::lookup_result Result = NameAndResult.second;
3704 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3705 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3706 // We have to work around a name lookup bug here where negative lookup
3707 // results for these names get cached in namespace lookup tables (these
3708 // names should never be looked up in a namespace).
3709 assert(Result.empty() && "Cannot have a constructor or conversion "
3710 "function name in a namespace!");
3711 continue;
3712 }
3713
3714 for (NamedDecl *ND : Result)
3715 if (!ND->isFromASTFile())
3716 GetDeclRef(ND);
3717 }
3718
3719 return 0;
3720 }
3721
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003722 if (DC->getPrimaryContext() != DC)
3723 return 0;
3724
Chandler Carruthe972c362015-03-26 03:11:40 +00003725 // Skip contexts which don't support name lookup.
3726 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003727 return 0;
3728
3729 // If not in C++, we perform name lookup for the translation unit via the
3730 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003731 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003732 return 0;
3733
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003734 // Serialize the contents of the mapping used for lookup. Note that,
3735 // although we have two very different code paths, the serialized
3736 // representation is the same for both cases: a declaration name,
3737 // followed by a size, followed by references to the visible
3738 // declarations that have that name.
3739 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003740 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003741 if (!Map || Map->empty())
3742 return 0;
3743
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003744 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003745 SmallString<4096> LookupTable;
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003746 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003747
3748 // Write the lookup table
3749 RecordData Record;
3750 Record.push_back(DECL_CONTEXT_VISIBLE);
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003751 Record.push_back(BucketOffset);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003752 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003753 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003754 ++NumVisibleDeclContexts;
3755 return Offset;
3756}
3757
Sebastian Redla4071b42010-08-24 00:50:09 +00003758/// \brief Write an UPDATE_VISIBLE block for the given context.
3759///
3760/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3761/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003762/// (in C++), for namespaces, and for classes with forward-declared unscoped
3763/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003764void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003765 if (isRewritten(cast<Decl>(DC)))
3766 return;
3767
Richard Smith961eae52014-03-25 01:14:22 +00003768 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003769 if (!Map || Map->empty())
3770 return;
3771
Sebastian Redla4071b42010-08-24 00:50:09 +00003772 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003773 SmallString<4096> LookupTable;
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003774 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003775
Richard Smith5fc18a92015-07-12 23:43:21 +00003776 // If we're updating a namespace, select a key declaration as the key for the
3777 // update record; those are the only ones that will be checked on reload.
3778 if (isa<NamespaceDecl>(DC))
3779 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3780
Sebastian Redla4071b42010-08-24 00:50:09 +00003781 // Write the lookup table
3782 RecordData Record;
3783 Record.push_back(UPDATE_VISIBLE);
3784 Record.push_back(getDeclID(cast<Decl>(DC)));
Aaron Ballman6a4a2102015-09-01 13:24:39 +00003785 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003786 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003787}
3788
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003789/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3790void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3791 RecordData Record;
3792 Record.push_back(Opts.fp_contract);
3793 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3794}
3795
3796/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3797void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003798 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003799 return;
3800
3801 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3802 RecordData Record;
3803#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3804#include "clang/Basic/OpenCLExtensions.def"
3805 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3806}
3807
Douglas Gregor404cdde2012-01-27 01:47:08 +00003808void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003809 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003810 RecordData Categories;
3811
3812 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3813 unsigned Size = 0;
3814 unsigned StartIndex = Categories.size();
3815
3816 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3817
3818 // Allocate space for the size.
3819 Categories.push_back(0);
3820
3821 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003822 for (ObjCInterfaceDecl::known_categories_iterator
3823 Cat = Class->known_categories_begin(),
3824 CatEnd = Class->known_categories_end();
3825 Cat != CatEnd; ++Cat, ++Size) {
3826 assert(getDeclID(*Cat) != 0 && "Bogus category");
3827 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003828 }
3829
3830 // Update the size.
3831 Categories[StartIndex] = Size;
3832
3833 // Record this interface -> category map.
3834 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3835 CategoriesMap.push_back(CatInfo);
3836 }
3837
3838 // Sort the categories map by the definition ID, since the reader will be
3839 // performing binary searches on this information.
3840 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3841
3842 // Emit the categories map.
3843 using namespace llvm;
3844 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3845 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3848 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3849
3850 RecordData Record;
3851 Record.push_back(OBJC_CATEGORIES_MAP);
3852 Record.push_back(CategoriesMap.size());
3853 Stream.EmitRecordWithBlob(AbbrevID, Record,
3854 reinterpret_cast<char*>(CategoriesMap.data()),
3855 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3856
3857 // Emit the category lists.
3858 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3859}
3860
Richard Smithe40f2ba2013-08-07 21:41:30 +00003861void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3862 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3863
3864 if (LPTMap.empty())
3865 return;
3866
3867 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00003868 for (auto LPTMapEntry : LPTMap) {
3869 const FunctionDecl *FD = LPTMapEntry.first;
3870 LateParsedTemplate *LPT = LPTMapEntry.second;
3871 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003872 AddDeclRef(LPT->D, Record);
3873 Record.push_back(LPT->Toks.size());
3874
3875 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3876 TokEnd = LPT->Toks.end();
3877 TokIt != TokEnd; ++TokIt) {
3878 AddToken(*TokIt, Record);
3879 }
3880 }
3881 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3882}
3883
Dario Domizioli13a0a382014-05-23 12:13:25 +00003884/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3885void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3886 RecordData Record;
3887 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3888 AddSourceLocation(PragmaLoc, Record);
3889 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3890}
3891
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003892//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003893// General Serialization Routines
3894//===----------------------------------------------------------------------===//
3895
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003896/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003897void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3898 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003899 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003900 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3901 e = Attrs.end(); i != e; ++i){
3902 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003903 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003904 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003905
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003906#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003907
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003908 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003909}
3910
John McCallf413f5e2013-05-03 00:10:13 +00003911void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3912 AddSourceLocation(Tok.getLocation(), Record);
3913 Record.push_back(Tok.getLength());
3914
3915 // FIXME: When reading literal tokens, reconstruct the literal pointer
3916 // if it is needed.
3917 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3918 // FIXME: Should translate token kind to a stable encoding.
3919 Record.push_back(Tok.getKind());
3920 // FIXME: Should translate token flags to a stable encoding.
3921 Record.push_back(Tok.getFlags());
3922}
3923
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003924void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003925 Record.push_back(Str.size());
3926 Record.insert(Record.end(), Str.begin(), Str.end());
3927}
3928
Richard Smith7ed1bc92014-12-05 22:42:13 +00003929bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00003930 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00003931
Richard Smith54cc3c22014-12-11 20:50:24 +00003932 bool Changed =
3933 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00003934
3935 // Remove a prefix to make the path relative, if relevant.
3936 const char *PathBegin = Path.data();
3937 const char *PathPtr =
3938 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
3939 if (PathPtr != PathBegin) {
3940 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
3941 Changed = true;
3942 }
3943
3944 return Changed;
3945}
3946
3947void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
3948 SmallString<128> FilePath(Path);
3949 PreparePathForOutput(FilePath);
3950 AddString(FilePath, Record);
3951}
3952
3953void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
3954 StringRef Path) {
3955 SmallString<128> FilePath(Path);
3956 PreparePathForOutput(FilePath);
3957 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
3958}
3959
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003960void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3961 RecordDataImpl &Record) {
3962 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003963 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003964 Record.push_back(*Minor + 1);
3965 else
3966 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003967 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003968 Record.push_back(*Subminor + 1);
3969 else
3970 Record.push_back(0);
3971}
3972
Douglas Gregore84a9da2009-04-20 20:36:09 +00003973/// \brief Note that the identifier II occurs at the given offset
3974/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003975void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003976 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003977 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003978 // up earlier in the chain and thus don't need an offset.
3979 if (ID >= FirstIdentID)
3980 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003981}
3982
Douglas Gregor95c13f52009-04-25 17:48:32 +00003983/// \brief Note that the selector Sel occurs at the given offset
3984/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003985void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003986 unsigned ID = SelectorIDs[Sel];
3987 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003988 // Don't record offsets for selectors that are also available in a different
3989 // file.
3990 if (ID < FirstSelectorID)
3991 return;
3992 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003993}
3994
Richard Smithe75ee0f2015-08-17 07:13:32 +00003995ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream, bool IncludeTimestamps)
Richard Smith01b2cb42014-07-26 06:37:51 +00003996 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
Richard Smithe75ee0f2015-08-17 07:13:32 +00003997 WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
3998 WritingAST(false), DoneWritingDeclsAndTypes(false),
3999 ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
4000 NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
4001 NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
4002 NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
4003 NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
Richard Smith01b2cb42014-07-26 06:37:51 +00004004 NextSubmoduleID(FirstSubmoduleID),
4005 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4006 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4007 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00004008 NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004009 TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004010 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004011 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004012 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004013 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4014 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4015 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004016
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004017ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004018 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004019}
4020
Richard Smithb3761912015-02-05 23:08:52 +00004021const LangOptions &ASTWriter::getLangOpts() const {
4022 assert(WritingAST && "can't determine lang opts when not writing AST");
4023 return Context->getLangOpts();
4024}
4025
Richard Smithe75ee0f2015-08-17 07:13:32 +00004026time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4027 return IncludeTimestamps ? E->getModificationTime() : 0;
4028}
4029
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004030void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004031 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004032 Module *WritingModule, StringRef isysroot,
4033 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004034 WritingAST = true;
4035
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004036 ASTHasCompilerErrors = hasErrors;
4037
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004038 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004039 Stream.Emit((unsigned)'C', 8);
4040 Stream.Emit((unsigned)'P', 8);
4041 Stream.Emit((unsigned)'C', 8);
4042 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004043
Chris Lattner28fa4e62009-04-26 22:26:21 +00004044 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004045
Douglas Gregoreda8e122011-08-09 15:13:55 +00004046 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004047 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004048 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004049 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004050 Context = nullptr;
4051 PP = nullptr;
4052 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004053 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004054
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004055 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004056}
4057
Douglas Gregora94a1542011-07-27 21:45:57 +00004058template<typename Vector>
4059static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4060 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004061 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4062 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004063 Writer.AddDeclRef(*I, Record);
4064 }
4065}
4066
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004067void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004068 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004069 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004070 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004071 using namespace llvm;
4072
Craig Toppera13603a2014-05-22 05:54:18 +00004073 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004074
Douglas Gregorcf68c582011-12-01 22:20:10 +00004075 // Make sure that the AST reader knows to finalize itself.
4076 if (Chain)
4077 Chain->finalizeForWriting();
4078
Sebastian Redl143413f2010-07-12 22:02:52 +00004079 ASTContext &Context = SemaRef.Context;
4080 Preprocessor &PP = SemaRef.PP;
4081
Douglas Gregordab42432011-08-12 00:15:20 +00004082 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004083 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4084 if (D) {
4085 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4086 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004087 }
4088 };
4089 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4090 PREDEF_DECL_TRANSLATION_UNIT_ID);
4091 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4092 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4093 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4094 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4095 PREDEF_DECL_OBJC_PROTOCOL_ID);
4096 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4097 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4098 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4099 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4100 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004101 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Richard Smith5fc18a92015-07-12 23:43:21 +00004102 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004103
Chris Lattner0c797362009-09-08 18:19:27 +00004104 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004105 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004106 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004107 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004108 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004109
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004110 // Build a record containing all of the file scoped decls in this file.
4111 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004112 if (!isModule)
4113 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4114 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004115
Douglas Gregor851443c2011-08-12 01:39:19 +00004116 // Build a record containing all of the delegating constructors we still need
4117 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004118 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004119 if (!isModule)
4120 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004121
Douglas Gregor851443c2011-08-12 01:39:19 +00004122 // Write the set of weak, undeclared identifiers. We always write the
4123 // entire table, since later PCH files in a PCH chain are only interested in
4124 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004125 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004126 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4127 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4128 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4129 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4130 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4131 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4132 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004133 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004134
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004135 // Build a record containing all of the ext_vector declarations.
4136 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004137 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004138
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004139 // Build a record containing all of the VTable uses information.
4140 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004141 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004142 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4143 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4144 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4145 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4146 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004147 }
4148
Nico Weber72889432014-09-06 01:25:55 +00004149 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4150 RecordData UnusedLocalTypedefNameCandidates;
4151 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4152 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4153
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004154 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004155 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004156 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004157 I = SemaRef.PendingInstantiations.begin(),
4158 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4159 AddDeclRef(I->first, PendingInstantiations);
4160 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004161 }
4162 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4163 "There are local ones at end of translation unit!");
4164
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004165 // Build a record containing some declaration references.
4166 RecordData SemaDeclRefs;
4167 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4168 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4169 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4170 }
4171
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004172 RecordData CUDASpecialDeclRefs;
4173 if (Context.getcudaConfigureCallDecl()) {
4174 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4175 }
4176
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004177 // Build a record containing all of the known namespaces.
4178 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004179 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004180 I = SemaRef.KnownNamespaces.begin(),
4181 IEnd = SemaRef.KnownNamespaces.end();
4182 I != IEnd; ++I) {
4183 if (!I->second)
4184 AddDeclRef(I->first, KnownNamespaces);
4185 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004186
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004187 // Build a record of all used, undefined objects that require definitions.
4188 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004189
4190 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004191 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004192 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4193 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004194 AddDeclRef(I->first, UndefinedButUsed);
4195 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004196 }
4197
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004198 // Build a record containing all delete-expressions that we would like to
4199 // analyze later in AST.
4200 RecordData DeleteExprsToAnalyze;
4201
4202 for (const auto &DeleteExprsInfo :
4203 SemaRef.getMismatchingDeleteExpressions()) {
4204 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4205 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4206 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4207 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4208 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4209 }
4210 }
4211
Douglas Gregor112b9072012-10-18 05:31:06 +00004212 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004213 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004215 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004216 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004217 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004218
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004219 // This is so that older clang versions, before the introduction
4220 // of the control block, can read and reject the newer PCH format.
4221 Record.clear();
4222 Record.push_back(VERSION_MAJOR);
4223 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4224
Douglas Gregor851443c2011-08-12 01:39:19 +00004225 // Create a lexical update block containing all of the declarations in the
4226 // translation unit that do not come from other AST files.
4227 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004228 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4229 for (const auto *D : TU->noload_decls()) {
4230 if (!D->isFromASTFile()) {
4231 NewGlobalKindDeclPairs.push_back(D->getKind());
4232 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4233 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004234 }
4235
4236 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4237 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4238 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4239 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4240 Record.clear();
4241 Record.push_back(TU_UPDATE_LEXICAL);
4242 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Richard Smith82f8fcd2015-08-06 22:07:25 +00004243 bytes(NewGlobalKindDeclPairs));
Douglas Gregor851443c2011-08-12 01:39:19 +00004244
4245 // And a visible updates block for the translation unit.
4246 Abv = new llvm::BitCodeAbbrev();
4247 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4248 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Aaron Ballman6a4a2102015-09-01 13:24:39 +00004249 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor851443c2011-08-12 01:39:19 +00004250 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4251 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4252 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004253
4254 // If we have any extern "C" names, write out a visible update for them.
4255 if (Context.ExternCContext)
4256 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004257
4258 // If the translation unit has an anonymous namespace, and we don't already
4259 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004260 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004261 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4262 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004263 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004264 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004265 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004266
Richard Smith5652c0f2014-03-21 01:48:23 +00004267 // Add update records for all mangling numbers and static local numbers.
4268 // These aren't really update records, but this is a convenient way of
4269 // tagging this rare extra data onto the declarations.
4270 for (const auto &Number : Context.MangleNumbers)
4271 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004272 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4273 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004274 for (const auto &Number : Context.StaticLocalNumbers)
4275 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004276 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4277 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004278
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004279 // Make sure visible decls, added to DeclContexts previously loaded from
4280 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004281 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004282 I = UpdatingVisibleDecls.begin(),
4283 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4284 GetDeclRef(*I);
4285 }
4286
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004287 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004288 // serialization, if we're storing decls with identifiers.
4289 if (!WritingModule || !getLangOpts().CPlusPlus) {
4290 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4291 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4292 IDEnd = PP.getIdentifierTable().end();
4293 ID != IDEnd; ++ID) {
4294 const IdentifierInfo *II = ID->second;
4295 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4296 IIs.push_back(II);
4297 }
4298 // Sort the identifiers to visit based on their name.
4299 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4300 for (const IdentifierInfo *II : IIs) {
4301 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4302 DEnd = SemaRef.IdResolver.end();
4303 D != DEnd; ++D) {
4304 GetDeclRef(*D);
4305 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004306 }
4307 }
4308
Douglas Gregor5204bde2011-08-02 16:26:37 +00004309 // Form the record of special types.
4310 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004311 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004312 AddTypeRef(Context.getFILEType(), SpecialTypes);
4313 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4314 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4315 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4316 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004317 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004318 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004319
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004320 if (Chain) {
4321 // Write the mapping information describing our module dependencies and how
4322 // each of those modules were mapped into our own offset/ID space, so that
4323 // the reader can build the appropriate mapping to its own offset/ID space.
4324 // The map consists solely of a blob with the following format:
4325 // *(module-name-len:i16 module-name:len*i8
4326 // source-location-offset:i32
4327 // identifier-id:i32
4328 // preprocessed-entity-id:i32
4329 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004330 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004331 // selector-id:i32
4332 // declaration-id:i32
4333 // c++-base-specifiers-id:i32
4334 // type-id:i32)
4335 //
4336 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4337 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4339 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004340 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004341 {
4342 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004343 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004344 using namespace llvm::support;
4345 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004346 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004347 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004348 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004349
Ben Langmuir785180e2014-10-20 16:27:30 +00004350 // Note: if a base ID was uint max, it would not be possible to load
4351 // another module after it or have more than one entity inside it.
4352 uint32_t None = std::numeric_limits<uint32_t>::max();
4353
4354 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4355 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4356 if (ShouldWrite)
4357 LE.write<uint32_t>(BaseID);
4358 else
4359 LE.write<uint32_t>(None);
4360 };
4361
Ben Langmuirfe971d92014-08-16 04:54:18 +00004362 // These values should be unique within a chain, since they will be read
4363 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004364 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4365 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4366 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4367 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4368 M->NumPreprocessedEntities);
4369 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4370 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4371 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4372 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004373 }
4374 }
4375 Record.clear();
4376 Record.push_back(MODULE_OFFSET_MAP);
4377 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4378 Buffer.data(), Buffer.size());
4379 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004380
Richard Smithb9eab6d2014-03-20 19:44:17 +00004381 RecordData DeclUpdatesOffsetsRecord;
4382
Richard Smith59442b42014-03-20 20:07:19 +00004383 // Keep writing types, declarations, and declaration update records
4384 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004385 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4386 WriteTypeAbbrevs();
4387 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004388 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4389 E = DeclsToRewrite.end();
4390 I != E; ++I)
4391 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004392 do {
4393 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4394 while (!DeclTypesToEmit.empty()) {
4395 DeclOrType DOT = DeclTypesToEmit.front();
4396 DeclTypesToEmit.pop();
4397 if (DOT.isType())
4398 WriteType(DOT.getType());
4399 else
4400 WriteDecl(Context, DOT.getDecl());
4401 }
4402 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004403 Stream.ExitBlock();
4404
Richard Smithb9eab6d2014-03-20 19:44:17 +00004405 DoneWritingDeclsAndTypes = true;
4406
4407 // These things can only be done once we've written out decls and types.
4408 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004409 if (!DeclUpdatesOffsetsRecord.empty())
4410 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004411 WriteCXXBaseSpecifiersOffsets();
Richard Smithc2bb8182015-03-24 06:36:48 +00004412 WriteCXXCtorInitializersOffsets();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004413 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004414 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004415
4416 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004417 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004418 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004419 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004420 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004421 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004422 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004423 WriteFPPragmaOptions(SemaRef.getFPOptions());
4424 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004425 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004426
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004427 // If we're emitting a module, write out the submodule information.
4428 if (WritingModule)
4429 WriteSubmodules(WritingModule);
4430
Douglas Gregor5204bde2011-08-02 16:26:37 +00004431 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4432
Douglas Gregord4df8652009-04-22 22:02:47 +00004433 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004434 if (!EagerlyDeserializedDecls.empty())
4435 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004436
4437 // Write the record containing tentative definitions.
4438 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004439 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004440
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004441 // Write the record containing unused file scoped decls.
4442 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004443 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004444
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004445 // Write the record containing weak undeclared identifiers.
4446 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004447 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004448 WeakUndeclaredIdentifiers);
4449
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004450 // Write the record containing ext_vector type names.
4451 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004452 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004453
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004454 // Write the record containing VTable uses information.
4455 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004456 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004457
Nico Weber72889432014-09-06 01:25:55 +00004458 // Write the record containing potentially unused local typedefs.
4459 if (!UnusedLocalTypedefNameCandidates.empty())
4460 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4461 UnusedLocalTypedefNameCandidates);
4462
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004463 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004464 if (!PendingInstantiations.empty())
4465 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004466
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004467 // Write the record containing declaration references of Sema.
4468 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004469 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004470
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004471 // Write the record containing CUDA-specific declaration references.
4472 if (!CUDASpecialDeclRefs.empty())
4473 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004474
4475 // Write the delegating constructors.
4476 if (!DelegatingCtorDecls.empty())
4477 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004478
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004479 // Write the known namespaces.
4480 if (!KnownNamespaces.empty())
4481 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004482
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004483 // Write the undefined internal functions and variables, and inline functions.
4484 if (!UndefinedButUsed.empty())
4485 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004486
4487 if (!DeleteExprsToAnalyze.empty())
4488 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4489
Douglas Gregor851443c2011-08-12 01:39:19 +00004490 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004491 for (auto *DC : UpdatedDeclContexts)
4492 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004493
Douglas Gregor959bb062011-12-03 01:15:29 +00004494 if (!WritingModule) {
4495 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004496 struct ModuleInfo {
4497 uint64_t ID;
4498 Module *M;
4499 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4500 };
Richard Smith56be7542014-03-21 00:33:59 +00004501 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004502 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004503 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004504 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4505 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004506 }
Richard Smith56be7542014-03-21 00:33:59 +00004507
4508 if (!Imports.empty()) {
4509 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4510 return A.ID < B.ID;
4511 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004512 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4513 return A.ID == B.ID;
4514 };
Richard Smith56be7542014-03-21 00:33:59 +00004515
4516 // Sort and deduplicate module IDs.
4517 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004518 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004519 Imports.end());
4520
4521 RecordData ImportedModules;
4522 for (const auto &Import : Imports) {
4523 ImportedModules.push_back(Import.ID);
4524 // FIXME: If the module has macros imported then later has declarations
4525 // imported, this location won't be the right one as a location for the
4526 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004527 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004528 }
4529
Douglas Gregor959bb062011-12-03 01:15:29 +00004530 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4531 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004532 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004533
Douglas Gregor851443c2011-08-12 01:39:19 +00004534 WriteDeclReplacementsBlock();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004535 WriteObjCCategories();
Dario Domizioli13a0a382014-05-23 12:13:25 +00004536 if(!WritingModule)
4537 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004538
Douglas Gregor08f01292009-04-17 22:13:46 +00004539 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004540 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004541 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004542 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004543 Record.push_back(NumLexicalDeclContexts);
4544 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004545 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004546 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004547}
4548
Richard Smithb9eab6d2014-03-20 19:44:17 +00004549void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004550 if (DeclUpdates.empty())
4551 return;
4552
Richard Smith59442b42014-03-20 20:07:19 +00004553 DeclUpdateMap LocalUpdates;
4554 LocalUpdates.swap(DeclUpdates);
4555
Richard Smith6ef42932014-03-20 21:02:00 +00004556 for (auto &DeclUpdate : LocalUpdates) {
4557 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004558 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004559 continue; // The decl will be written completely,no need to store updates.
4560
Richard Smithd28ac5b2014-03-22 23:33:22 +00004561 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004562 RecordData Record;
4563 for (auto &Update : DeclUpdate.second) {
4564 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4565
4566 Record.push_back(Kind);
4567 switch (Kind) {
4568 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4569 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4570 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004571 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004572 Record.push_back(GetDeclRef(Update.getDecl()));
4573 break;
4574
Richard Smith4d235792014-08-07 18:53:08 +00004575 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004576 // An updated body is emitted last, so that the reader doesn't need
4577 // to skip over the lazy body to reach statements for other records.
4578 Record.pop_back();
4579 HasUpdatedBody = true;
4580 break;
4581
Richard Smith4d235792014-08-07 18:53:08 +00004582 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4583 AddSourceLocation(Update.getLoc(), Record);
4584 break;
4585
Richard Smithcd45dbc2014-04-19 03:48:30 +00004586 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4587 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004588 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004589 AddCXXDefinitionData(RD, Record);
4590 Record.push_back(WriteDeclContextLexicalBlock(
4591 *Context, const_cast<CXXRecordDecl *>(RD)));
4592
4593 // This state is sometimes updated by template instantiation, when we
4594 // switch from the specialization referring to the template declaration
4595 // to it referring to the template definition.
4596 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4597 Record.push_back(MSInfo->getTemplateSpecializationKind());
4598 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4599 } else {
4600 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4601 Record.push_back(Spec->getTemplateSpecializationKind());
4602 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004603
4604 // The instantiation might have been resolved to a partial
4605 // specialization. If so, record which one.
4606 auto From = Spec->getInstantiatedFrom();
4607 if (auto PartialSpec =
4608 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4609 Record.push_back(true);
4610 AddDeclRef(PartialSpec, Record);
4611 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4612 Record);
4613 } else {
4614 Record.push_back(false);
4615 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004616 }
4617 Record.push_back(RD->getTagKind());
4618 AddSourceLocation(RD->getLocation(), Record);
4619 AddSourceLocation(RD->getLocStart(), Record);
4620 AddSourceLocation(RD->getRBraceLoc(), Record);
4621
4622 // Instantiation may change attributes; write them all out afresh.
4623 Record.push_back(D->hasAttrs());
4624 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004625 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4626 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004627
4628 // FIXME: Ensure we don't get here for explicit instantiations.
4629 break;
4630 }
4631
Richard Smithf8134002015-03-10 01:41:22 +00004632 case UPD_CXX_RESOLVED_DTOR_DELETE:
4633 AddDeclRef(Update.getDecl(), Record);
4634 break;
4635
Richard Smith564417a2014-03-20 21:47:22 +00004636 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4637 addExceptionSpec(
4638 *this,
4639 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4640 Record);
4641 break;
4642
Richard Smith6ef42932014-03-20 21:02:00 +00004643 case UPD_CXX_DEDUCED_RETURN_TYPE:
4644 Record.push_back(GetOrCreateTypeID(Update.getType()));
4645 break;
4646
4647 case UPD_DECL_MARKED_USED:
4648 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004649
4650 case UPD_MANGLING_NUMBER:
4651 case UPD_STATIC_LOCAL_NUMBER:
4652 Record.push_back(Update.getNumber());
4653 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004654
Alexey Bataev97720002014-11-11 04:05:39 +00004655 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4656 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4657 Record);
4658 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004659
4660 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004661 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004662 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004663
4664 case UPD_ADDED_ATTR_TO_RECORD:
4665 WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record);
4666 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004667 }
4668 }
4669
Richard Smithd28ac5b2014-03-22 23:33:22 +00004670 if (HasUpdatedBody) {
4671 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004672 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004673 Record.push_back(Def->isInlined());
4674 AddSourceLocation(Def->getInnerLocStart(), Record);
4675 AddFunctionDefinition(Def, Record);
4676 }
4677
Richard Smithcd45dbc2014-04-19 03:48:30 +00004678 OffsetsRecord.push_back(GetDeclRef(D));
4679 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4680
Richard Smith6ef42932014-03-20 21:02:00 +00004681 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004682
Richard Smithc2bb8182015-03-24 06:36:48 +00004683 FlushPendingAfterDecl();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004684 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004685}
4686
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004687void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004688 if (ReplacedDecls.empty())
4689 return;
4690
4691 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004692 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4693 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004694 Record.push_back(I->ID);
4695 Record.push_back(I->Offset);
4696 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004697 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004698 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004699}
4700
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004701void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004702 Record.push_back(Loc.getRawEncoding());
4703}
4704
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004705void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004706 AddSourceLocation(Range.getBegin(), Record);
4707 AddSourceLocation(Range.getEnd(), Record);
4708}
4709
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004710void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004711 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004712 const uint64_t *Words = Value.getRawData();
4713 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004714}
4715
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004716void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004717 Record.push_back(Value.isUnsigned());
4718 AddAPInt(Value, Record);
4719}
4720
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004721void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004722 AddAPInt(Value.bitcastToAPInt(), Record);
4723}
4724
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004725void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004726 Record.push_back(getIdentifierRef(II));
4727}
4728
Sebastian Redl539c5062010-08-18 23:57:32 +00004729IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004730 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004731 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004732
Sebastian Redl539c5062010-08-18 23:57:32 +00004733 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004734 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004735 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004736 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004737}
4738
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004739MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004740 // Don't emit builtin macros like __LINE__ to the AST file unless they
4741 // have been redefined by the header (in which case they are not
4742 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004743 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004744 return 0;
4745
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004746 MacroID &ID = MacroIDs[MI];
4747 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004748 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004749 MacroInfoToEmitData Info = { Name, MI, ID };
4750 MacroInfosToEmit.push_back(Info);
4751 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004752 return ID;
4753}
4754
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004755MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004756 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004757 return 0;
4758
4759 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4760 return MacroIDs[MI];
4761}
4762
4763uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004764 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004765}
4766
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004767void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004768 Record.push_back(getSelectorRef(SelRef));
4769}
4770
Sebastian Redl539c5062010-08-18 23:57:32 +00004771SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004772 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004773 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004774 }
4775
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004776 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004777 if (SID == 0 && Chain) {
4778 // This might trigger a ReadSelector callback, which will set the ID for
4779 // this selector.
4780 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004781 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004782 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004783 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004784 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004785 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004786 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004787 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004788}
4789
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004790void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004791 AddDeclRef(Temp->getDestructor(), Record);
4792}
4793
Richard Smithc2bb8182015-03-24 06:36:48 +00004794void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4795 RecordDataImpl &Record) {
4796 assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4797 CXXCtorInitializersToWrite.push_back(
4798 QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4799 Record.push_back(NextCXXCtorInitializersID++);
4800}
4801
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004802void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
Richard Smithc2bb8182015-03-24 06:36:48 +00004803 CXXBaseSpecifier const *BasesEnd,
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004804 RecordDataImpl &Record) {
4805 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4806 CXXBaseSpecifiersToWrite.push_back(
4807 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4808 Bases, BasesEnd));
4809 Record.push_back(NextCXXBaseSpecifiersID++);
4810}
4811
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004812void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004813 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004814 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004815 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004816 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004817 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004818 break;
4819 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004820 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004821 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004822 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004823 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004824 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004825 break;
4826 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004827 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004828 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004829 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004830 break;
John McCall0ad16662009-10-29 08:12:44 +00004831 case TemplateArgument::Null:
4832 case TemplateArgument::Integral:
4833 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004834 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004835 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004836 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004837 break;
4838 }
4839}
4840
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004841void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004842 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004843 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004844
4845 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4846 bool InfoHasSameExpr
4847 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4848 Record.push_back(InfoHasSameExpr);
4849 if (InfoHasSameExpr)
4850 return; // Avoid storing the same expr twice.
4851 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004852 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4853 Record);
4854}
4855
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004856void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4857 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004858 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004859 AddTypeRef(QualType(), Record);
4860 return;
4861 }
4862
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004863 AddTypeLoc(TInfo->getTypeLoc(), Record);
4864}
4865
4866void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4867 AddTypeRef(TL.getType(), Record);
4868
John McCall8f115c62009-10-16 21:56:05 +00004869 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004870 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004871 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004872}
4873
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004874void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004875 Record.push_back(GetOrCreateTypeID(T));
4876}
4877
Richard Smith2095ffe2015-03-16 20:11:03 +00004878TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004879 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004880 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4881 if (T.isNull())
4882 return TypeIdx();
4883 assert(!T.getLocalFastQualifiers());
4884
4885 TypeIdx &Idx = TypeIdxs[T];
4886 if (Idx.getIndex() == 0) {
4887 if (DoneWritingDeclsAndTypes) {
4888 assert(0 && "New type seen after serializing all the types to emit!");
4889 return TypeIdx();
4890 }
4891
4892 // We haven't seen this type before. Assign it a new ID and put it
4893 // into the queue of types to emit.
4894 Idx = TypeIdx(NextTypeID++);
4895 DeclTypesToEmit.push(T);
4896 }
4897 return Idx;
4898 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004899}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004900
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004901TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004902 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004903 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4904 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004905 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00004906 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004907
Richard Smith2095ffe2015-03-16 20:11:03 +00004908 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4909 assert(I != TypeIdxs.end() && "Type not emitted!");
4910 return I->second;
4911 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004912}
4913
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004914void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004915 Record.push_back(GetDeclRef(D));
4916}
4917
Sebastian Redl539c5062010-08-18 23:57:32 +00004918DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004919 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00004920
4921 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004922 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004923 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004924
4925 // If D comes from an AST file, its declaration ID is already known and
4926 // fixed.
4927 if (D->isFromASTFile())
4928 return D->getGlobalID();
4929
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004930 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004931 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004932 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004933 if (DoneWritingDeclsAndTypes) {
4934 assert(0 && "New decl seen after serializing all the decls to emit!");
4935 return 0;
4936 }
4937
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004938 // We haven't seen this declaration before. Give it a new ID and
4939 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004940 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004941 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004942 }
4943
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004944 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004945}
4946
Sebastian Redl539c5062010-08-18 23:57:32 +00004947DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00004948 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00004949 return 0;
4950
Douglas Gregorb3163e52012-01-05 22:33:30 +00004951 // If D comes from an AST file, its declaration ID is already known and
4952 // fixed.
4953 if (D->isFromASTFile())
4954 return D->getGlobalID();
4955
Douglas Gregore84a9da2009-04-20 20:36:09 +00004956 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4957 return DeclIDs[D];
4958}
4959
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004960void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004961 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004962 assert(D);
4963
4964 SourceLocation Loc = D->getLocation();
4965 if (Loc.isInvalid())
4966 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004967
4968 // We only keep track of the file-level declarations of each file.
4969 if (!D->getLexicalDeclContext()->isFileContext())
4970 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004971 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4972 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004973 if (isa<ParmVarDecl>(D))
4974 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004975
4976 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004977 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004978 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004979 FileID FID;
4980 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004981 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004982 if (FID.isInvalid())
4983 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004984 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004985
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004986 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004987 if (!Info)
4988 Info = new DeclIDInFileInfo();
4989
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004990 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004991 LocDeclIDsTy &Decls = Info->DeclIDs;
4992
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004993 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004994 Decls.push_back(LocDecl);
4995 return;
4996 }
4997
Benjamin Kramer45025c02013-08-24 13:22:59 +00004998 LocDeclIDsTy::iterator I =
4999 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005000
5001 Decls.insert(I, LocDecl);
5002}
5003
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005004void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005005 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005006 Record.push_back(Name.getNameKind());
5007 switch (Name.getNameKind()) {
5008 case DeclarationName::Identifier:
5009 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5010 break;
5011
5012 case DeclarationName::ObjCZeroArgSelector:
5013 case DeclarationName::ObjCOneArgSelector:
5014 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005015 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005016 break;
5017
5018 case DeclarationName::CXXConstructorName:
5019 case DeclarationName::CXXDestructorName:
5020 case DeclarationName::CXXConversionFunctionName:
5021 AddTypeRef(Name.getCXXNameType(), Record);
5022 break;
5023
5024 case DeclarationName::CXXOperatorName:
5025 Record.push_back(Name.getCXXOverloadedOperator());
5026 break;
5027
Alexis Hunt3d221f22009-11-29 07:34:05 +00005028 case DeclarationName::CXXLiteralOperatorName:
5029 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5030 break;
5031
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005032 case DeclarationName::CXXUsingDirective:
5033 // No extra data to emit
5034 break;
5035 }
5036}
Chris Lattnerca025db2010-05-07 21:43:38 +00005037
Richard Smithd08aeb62014-08-28 01:33:39 +00005038unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5039 assert(needsAnonymousDeclarationNumber(D) &&
5040 "expected an anonymous declaration");
5041
5042 // Number the anonymous declarations within this context, if we've not
5043 // already done so.
5044 auto It = AnonymousDeclarationNumbers.find(D);
5045 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005046 auto *DC = D->getLexicalDeclContext();
5047 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5048 AnonymousDeclarationNumbers[ND] = Number;
5049 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005050
5051 It = AnonymousDeclarationNumbers.find(D);
5052 assert(It != AnonymousDeclarationNumbers.end() &&
5053 "declaration not found within its lexical context");
5054 }
5055
5056 return It->second;
5057}
5058
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005059void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005060 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005061 switch (Name.getNameKind()) {
5062 case DeclarationName::CXXConstructorName:
5063 case DeclarationName::CXXDestructorName:
5064 case DeclarationName::CXXConversionFunctionName:
5065 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5066 break;
5067
5068 case DeclarationName::CXXOperatorName:
5069 AddSourceLocation(
5070 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5071 Record);
5072 AddSourceLocation(
5073 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5074 Record);
5075 break;
5076
5077 case DeclarationName::CXXLiteralOperatorName:
5078 AddSourceLocation(
5079 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5080 Record);
5081 break;
5082
5083 case DeclarationName::Identifier:
5084 case DeclarationName::ObjCZeroArgSelector:
5085 case DeclarationName::ObjCOneArgSelector:
5086 case DeclarationName::ObjCMultiArgSelector:
5087 case DeclarationName::CXXUsingDirective:
5088 break;
5089 }
5090}
5091
5092void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005093 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005094 AddDeclarationName(NameInfo.getName(), Record);
5095 AddSourceLocation(NameInfo.getLoc(), Record);
5096 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5097}
5098
5099void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005100 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005101 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005102 Record.push_back(Info.NumTemplParamLists);
5103 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5104 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5105}
5106
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005107void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005108 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005109 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005110 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005111 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005112
5113 // Push each of the NNS's onto a stack for serialization in reverse order.
5114 while (NNS) {
5115 NestedNames.push_back(NNS);
5116 NNS = NNS->getPrefix();
5117 }
5118
5119 Record.push_back(NestedNames.size());
5120 while(!NestedNames.empty()) {
5121 NNS = NestedNames.pop_back_val();
5122 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5123 Record.push_back(Kind);
5124 switch (Kind) {
5125 case NestedNameSpecifier::Identifier:
5126 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5127 break;
5128
5129 case NestedNameSpecifier::Namespace:
5130 AddDeclRef(NNS->getAsNamespace(), Record);
5131 break;
5132
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005133 case NestedNameSpecifier::NamespaceAlias:
5134 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5135 break;
5136
Chris Lattnerca025db2010-05-07 21:43:38 +00005137 case NestedNameSpecifier::TypeSpec:
5138 case NestedNameSpecifier::TypeSpecWithTemplate:
5139 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5140 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5141 break;
5142
5143 case NestedNameSpecifier::Global:
5144 // Don't need to write an associated value.
5145 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005146
5147 case NestedNameSpecifier::Super:
5148 AddDeclRef(NNS->getAsRecordDecl(), Record);
5149 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005150 }
5151 }
5152}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005153
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005154void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5155 RecordDataImpl &Record) {
5156 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005157 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005158 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005159
5160 // Push each of the nested-name-specifiers's onto a stack for
5161 // serialization in reverse order.
5162 while (NNS) {
5163 NestedNames.push_back(NNS);
5164 NNS = NNS.getPrefix();
5165 }
5166
5167 Record.push_back(NestedNames.size());
5168 while(!NestedNames.empty()) {
5169 NNS = NestedNames.pop_back_val();
5170 NestedNameSpecifier::SpecifierKind Kind
5171 = NNS.getNestedNameSpecifier()->getKind();
5172 Record.push_back(Kind);
5173 switch (Kind) {
5174 case NestedNameSpecifier::Identifier:
5175 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5176 AddSourceRange(NNS.getLocalSourceRange(), Record);
5177 break;
5178
5179 case NestedNameSpecifier::Namespace:
5180 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5181 AddSourceRange(NNS.getLocalSourceRange(), Record);
5182 break;
5183
5184 case NestedNameSpecifier::NamespaceAlias:
5185 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5186 AddSourceRange(NNS.getLocalSourceRange(), Record);
5187 break;
5188
5189 case NestedNameSpecifier::TypeSpec:
5190 case NestedNameSpecifier::TypeSpecWithTemplate:
5191 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5192 AddTypeLoc(NNS.getTypeLoc(), Record);
5193 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5194 break;
5195
5196 case NestedNameSpecifier::Global:
5197 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5198 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005199
5200 case NestedNameSpecifier::Super:
5201 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5202 AddSourceRange(NNS.getLocalSourceRange(), Record);
5203 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005204 }
5205 }
5206}
5207
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005208void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005209 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005210 Record.push_back(Kind);
5211 switch (Kind) {
5212 case TemplateName::Template:
5213 AddDeclRef(Name.getAsTemplateDecl(), Record);
5214 break;
5215
5216 case TemplateName::OverloadedTemplate: {
5217 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5218 Record.push_back(OvT->size());
5219 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5220 I != E; ++I)
5221 AddDeclRef(*I, Record);
5222 break;
5223 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005224
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005225 case TemplateName::QualifiedTemplate: {
5226 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5227 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5228 Record.push_back(QualT->hasTemplateKeyword());
5229 AddDeclRef(QualT->getTemplateDecl(), Record);
5230 break;
5231 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005232
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005233 case TemplateName::DependentTemplate: {
5234 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5235 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5236 Record.push_back(DepT->isIdentifier());
5237 if (DepT->isIdentifier())
5238 AddIdentifierRef(DepT->getIdentifier(), Record);
5239 else
5240 Record.push_back(DepT->getOperator());
5241 break;
5242 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005243
5244 case TemplateName::SubstTemplateTemplateParm: {
5245 SubstTemplateTemplateParmStorage *subst
5246 = Name.getAsSubstTemplateTemplateParm();
5247 AddDeclRef(subst->getParameter(), Record);
5248 AddTemplateName(subst->getReplacement(), Record);
5249 break;
5250 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005251
5252 case TemplateName::SubstTemplateTemplateParmPack: {
5253 SubstTemplateTemplateParmPackStorage *SubstPack
5254 = Name.getAsSubstTemplateTemplateParmPack();
5255 AddDeclRef(SubstPack->getParameterPack(), Record);
5256 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5257 break;
5258 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005259 }
5260}
5261
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005262void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005263 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005264 Record.push_back(Arg.getKind());
5265 switch (Arg.getKind()) {
5266 case TemplateArgument::Null:
5267 break;
5268 case TemplateArgument::Type:
5269 AddTypeRef(Arg.getAsType(), Record);
5270 break;
5271 case TemplateArgument::Declaration:
5272 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005273 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005274 break;
5275 case TemplateArgument::NullPtr:
5276 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005277 break;
5278 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005279 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005280 AddTypeRef(Arg.getIntegralType(), Record);
5281 break;
5282 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005283 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5284 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005285 case TemplateArgument::TemplateExpansion:
5286 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005287 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005288 Record.push_back(*NumExpansions + 1);
5289 else
5290 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005291 break;
5292 case TemplateArgument::Expression:
5293 AddStmt(Arg.getAsExpr());
5294 break;
5295 case TemplateArgument::Pack:
5296 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005297 for (const auto &P : Arg.pack_elements())
5298 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005299 break;
5300 }
5301}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005302
5303void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005304ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005305 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005306 assert(TemplateParams && "No TemplateParams!");
5307 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5308 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5309 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5310 Record.push_back(TemplateParams->size());
5311 for (TemplateParameterList::const_iterator
5312 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5313 P != PEnd; ++P)
5314 AddDeclRef(*P, Record);
5315}
5316
5317/// \brief Emit a template argument list.
5318void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005319ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005320 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005321 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005322 Record.push_back(TemplateArgs->size());
5323 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005324 AddTemplateArgument(TemplateArgs->get(i), Record);
5325}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005326
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005327void
5328ASTWriter::AddASTTemplateArgumentListInfo
5329(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5330 assert(ASTTemplArgList && "No ASTTemplArgList!");
5331 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5332 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5333 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5334 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5335 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5336 AddTemplateArgumentLoc(TemplArgs[i], Record);
5337}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005338
5339void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005340ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005341 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005342 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005343 I = Set.begin(), E = Set.end(); I != E; ++I) {
5344 AddDeclRef(I.getDecl(), Record);
5345 Record.push_back(I.getAccess());
5346 }
5347}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005348
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005349void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005350 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005351 Record.push_back(Base.isVirtual());
5352 Record.push_back(Base.isBaseOfClass());
5353 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005354 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005355 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005356 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005357 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5358 : SourceLocation(),
5359 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005360}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005361
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005362void ASTWriter::FlushCXXBaseSpecifiers() {
5363 RecordData Record;
Richard Smithc2bb8182015-03-24 06:36:48 +00005364 unsigned N = CXXBaseSpecifiersToWrite.size();
5365 for (unsigned I = 0; I != N; ++I) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005366 Record.clear();
5367
5368 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005369 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005370 if (Index == CXXBaseSpecifiersOffsets.size())
5371 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5372 else {
5373 if (Index > CXXBaseSpecifiersOffsets.size())
5374 CXXBaseSpecifiersOffsets.resize(Index + 1);
5375 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5376 }
5377
5378 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5379 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5380 Record.push_back(BEnd - B);
5381 for (; B != BEnd; ++B)
5382 AddCXXBaseSpecifier(*B, Record);
5383 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005384
5385 // Flush any expressions that were written as part of the base specifiers.
5386 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005387 }
5388
Richard Smithc2bb8182015-03-24 06:36:48 +00005389 assert(N == CXXBaseSpecifiersToWrite.size() &&
5390 "added more base specifiers while writing base specifiers");
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005391 CXXBaseSpecifiersToWrite.clear();
5392}
5393
Alexis Hunt1d792652011-01-08 20:30:50 +00005394void ASTWriter::AddCXXCtorInitializers(
5395 const CXXCtorInitializer * const *CtorInitializers,
5396 unsigned NumCtorInitializers,
5397 RecordDataImpl &Record) {
5398 Record.push_back(NumCtorInitializers);
5399 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5400 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005401
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005402 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005403 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005404 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005405 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005406 } else if (Init->isDelegatingInitializer()) {
5407 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005408 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005409 } else if (Init->isMemberInitializer()){
5410 Record.push_back(CTOR_INITIALIZER_MEMBER);
5411 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005412 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005413 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5414 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005415 }
Francois Pichetd583da02010-12-04 09:14:42 +00005416
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005417 AddSourceLocation(Init->getMemberLocation(), Record);
5418 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005419 AddSourceLocation(Init->getLParenLoc(), Record);
5420 AddSourceLocation(Init->getRParenLoc(), Record);
5421 Record.push_back(Init->isWritten());
5422 if (Init->isWritten()) {
5423 Record.push_back(Init->getSourceOrder());
5424 } else {
5425 Record.push_back(Init->getNumArrayIndices());
5426 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5427 AddDeclRef(Init->getArrayIndex(i), Record);
5428 }
5429 }
5430}
5431
Richard Smithc2bb8182015-03-24 06:36:48 +00005432void ASTWriter::FlushCXXCtorInitializers() {
5433 RecordData Record;
5434
5435 unsigned N = CXXCtorInitializersToWrite.size();
Daniel Jasperc77b0a12015-03-24 08:06:38 +00005436 (void)N; // Silence unused warning in non-assert builds.
Richard Smithc2bb8182015-03-24 06:36:48 +00005437 for (auto &Init : CXXCtorInitializersToWrite) {
5438 Record.clear();
5439
5440 // Record the offset of this mem-initializer list.
5441 unsigned Index = Init.ID - 1;
5442 if (Index == CXXCtorInitializersOffsets.size())
5443 CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5444 else {
5445 if (Index > CXXCtorInitializersOffsets.size())
5446 CXXCtorInitializersOffsets.resize(Index + 1);
5447 CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5448 }
5449
5450 AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5451 Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5452
5453 // Flush any expressions that were written as part of the initializers.
5454 FlushStmts();
5455 }
5456
5457 assert(N == CXXCtorInitializersToWrite.size() &&
5458 "added more ctor initializers while writing ctor initializers");
5459 CXXCtorInitializersToWrite.clear();
5460}
5461
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005462void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005463 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005464 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005465 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005466 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005467 Record.push_back(Data.Aggregate);
5468 Record.push_back(Data.PlainOldData);
5469 Record.push_back(Data.Empty);
5470 Record.push_back(Data.Polymorphic);
5471 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005472 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005473 Record.push_back(Data.HasNoNonEmptyBases);
5474 Record.push_back(Data.HasPrivateFields);
5475 Record.push_back(Data.HasProtectedFields);
5476 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005477 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005478 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005479 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005480 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005481 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005482 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5483 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5484 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5485 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5486 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5487 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005488 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005489 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005490 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005491 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005492 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005493 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005494 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005495 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005496 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005497 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005498 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5499 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5500 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5501 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005502 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005503
5504 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005505 if (Data.NumBases > 0)
5506 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5507 Record);
5508
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005509 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5510 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005511 if (Data.NumVBases > 0)
5512 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5513 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005514
Richard Smitha4ba74c2013-08-30 04:46:40 +00005515 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5516 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005517 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005518 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005519
5520 // Add lambda-specific data.
5521 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005522 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005523 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005524 Record.push_back(Lambda.IsGenericLambda);
5525 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005526 Record.push_back(Lambda.NumCaptures);
5527 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005528 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005529 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005530 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005531 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005532 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005533 AddSourceLocation(Capture.getLocation(), Record);
5534 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005535 Record.push_back(Capture.getCaptureKind());
5536 switch (Capture.getCaptureKind()) {
5537 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005538 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005539 break;
5540 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005541 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005542 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005543 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005544 AddDeclRef(Var, Record);
5545 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5546 : SourceLocation(),
5547 Record);
5548 break;
5549 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005550 }
5551 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005552}
5553
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005554void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005555 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005556 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005557 assert(FirstDeclID == NextDeclID &&
5558 FirstTypeID == NextTypeID &&
5559 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005560 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005561 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005562 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005563 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005564
Sebastian Redl07a89a82010-07-30 00:29:29 +00005565 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005566
Richard Smith7f330cd2015-03-18 01:42:29 +00005567 // Note, this will get called multiple times, once one the reader starts up
5568 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005569 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5570 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5571 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005572 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005573 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005574 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005575 NextDeclID = FirstDeclID;
5576 NextTypeID = FirstTypeID;
5577 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005578 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005579 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005580 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005581}
5582
Sebastian Redl539c5062010-08-18 23:57:32 +00005583void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005584 // Always keep the highest ID. See \p TypeRead() for more information.
5585 IdentID &StoredID = IdentifierIDs[II];
5586 if (ID > StoredID)
5587 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005588}
5589
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005590void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005591 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005592 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005593 if (ID > StoredID)
5594 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005595}
5596
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005597void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005598 // Always take the highest-numbered type index. This copes with an interesting
5599 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005600 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005601 // keep the higher-numbered entry so that we can properly write it out to
5602 // the AST file.
5603 TypeIdx &StoredIdx = TypeIdxs[T];
5604 if (Idx.getIndex() >= StoredIdx.getIndex())
5605 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005606}
5607
Sebastian Redl539c5062010-08-18 23:57:32 +00005608void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005609 // Always keep the highest ID. See \p TypeRead() for more information.
5610 SelectorID &StoredID = SelectorIDs[S];
5611 if (ID > StoredID)
5612 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005613}
Douglas Gregor91096292010-10-02 19:29:26 +00005614
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005615void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005616 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005617 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005618 MacroDefinitions[MD] = ID;
5619}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005620
Douglas Gregore37a85a2011-12-02 17:30:13 +00005621void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5622 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5623 SubmoduleIDs[Mod] = ID;
5624}
5625
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005626void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005627 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005628 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005629 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5630 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005631 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005632 // A forward reference was mutated into a definition. Rewrite it.
5633 // FIXME: This happens during template instantiation, should we
5634 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005635 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5636 "completed a tag from another module but not by instantiation?");
5637 DeclUpdates[RD].push_back(
5638 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005639 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005640 }
5641}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005642
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005643void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5644 // TU and namespaces are handled elsewhere.
5645 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5646 return;
5647
Douglas Gregorb3722e22011-09-09 23:01:35 +00005648 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005649 return; // Not a source decl added to a DeclContext from PCH.
5650
Richard Smith0f4e2c42015-08-06 04:23:48 +00005651 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005652 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005653 assert(!WritingAST && "Already writing the AST!");
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005654 UpdatedDeclContexts.insert(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005655 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005656}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005657
5658void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5659 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005660 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005661 return; // Not a source member added to a class from PCH.
5662 if (!isa<CXXMethodDecl>(D))
5663 return; // We are interested in lazily declared implicit methods.
5664
5665 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005666 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005667 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005668 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005669}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005670
Richard Smith564417a2014-03-20 21:47:22 +00005671void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005672 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5673 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005674 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005675 // If we don't already know the exception specification for this redecl
5676 // chain, add an update record for it.
5677 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5678 ->getType()
5679 ->castAs<FunctionProtoType>()
5680 ->getExceptionSpecType()))
5681 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5682 });
Richard Smith564417a2014-03-20 21:47:22 +00005683}
5684
Richard Smith1fa5d642013-05-11 05:45:24 +00005685void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5686 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005687 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005688 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005689 DeclUpdates[D].push_back(
5690 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5691 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005692}
5693
Richard Smithf8134002015-03-10 01:41:22 +00005694void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5695 const FunctionDecl *Delete) {
5696 assert(!WritingAST && "Already writing the AST!");
5697 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005698 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005699 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005700 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5701 });
Richard Smithf8134002015-03-10 01:41:22 +00005702}
5703
Sebastian Redlab238a72011-04-24 16:28:06 +00005704void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005705 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005706 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005707 return; // Declaration not imported from PCH.
5708
Richard Smith4d235792014-08-07 18:53:08 +00005709 // Implicit function decl from a PCH was defined.
5710 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005711}
5712
Richard Smithd28ac5b2014-03-22 23:33:22 +00005713void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5714 assert(!WritingAST && "Already writing the AST!");
5715 if (!D->isFromASTFile())
5716 return;
5717
Richard Smith9e2341d2015-03-23 03:25:59 +00005718 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005719}
5720
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005721void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005722 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005723 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005724 return;
5725
5726 // Since the actual instantiation is delayed, this really means that we need
5727 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005728 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005729 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5730 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005731}
5732
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005733void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5734 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005735 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005736 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005737 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005738
5739 assert(IFD->getDefinition() && "Category on a class without a definition?");
5740 ObjCClassesWithCategories.insert(
5741 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005742}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005743
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005744
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005745void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5746 const ObjCPropertyDecl *OrigProp,
5747 const ObjCCategoryDecl *ClassExt) {
5748 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5749 if (!D)
5750 return;
5751
5752 assert(!WritingAST && "Already writing the AST!");
5753 if (!D->isFromASTFile())
5754 return; // Declaration not imported from PCH.
5755
5756 RewriteDecl(D);
5757}
Eli Friedman276dd182013-09-05 00:02:25 +00005758
5759void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5760 assert(!WritingAST && "Already writing the AST!");
5761 if (!D->isFromASTFile())
5762 return;
5763
Aaron Ballman4f45b712014-03-21 15:22:56 +00005764 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005765}
Alexey Bataev97720002014-11-11 04:05:39 +00005766
5767void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5768 assert(!WritingAST && "Already writing the AST!");
5769 if (!D->isFromASTFile())
5770 return;
5771
5772 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5773}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005774
Richard Smith4caa4492015-05-15 02:34:32 +00005775void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00005776 assert(!WritingAST && "Already writing the AST!");
5777 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005778 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005779}
Alex Denisovfde64952015-06-26 05:28:36 +00005780
5781void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5782 const RecordDecl *Record) {
5783 assert(!WritingAST && "Already writing the AST!");
5784 if (!Record->isFromASTFile())
5785 return;
5786 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5787}